blob: ec5c57346417d4ea3557fe81f8ae0656cdd20e23 [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
194
195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 * Return the name of the executed function.
197 */
198 char_u *
199func_name(cookie)
200 void *cookie;
201{
202 return ((struct funccall *)cookie)->func->name;
203}
204
205/*
206 * Return the address holding the next breakpoint line for a funccall cookie.
207 */
208 linenr_T *
209func_breakpoint(cookie)
210 void *cookie;
211{
212 return &((struct funccall *)cookie)->breakpoint;
213}
214
215/*
216 * Return the address holding the debug tick for a funccall cookie.
217 */
218 int *
219func_dbg_tick(cookie)
220 void *cookie;
221{
222 return &((struct funccall *)cookie)->dbg_tick;
223}
224
225/*
226 * Return the nesting level for a funccall cookie.
227 */
228 int
229func_level(cookie)
230 void *cookie;
231{
232 return ((struct funccall *)cookie)->level;
233}
234
235/* pointer to funccal for currently active function */
236struct funccall *current_funccal = NULL;
237
238/*
239 * Return TRUE when a function was ended by a ":return" command.
240 */
241 int
242current_func_returned()
243{
244 return current_funccal->returned;
245}
246
247
248/*
249 * Array to hold the value of v: variables.
250 */
251#include "version.h"
252
253/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000254#define VV_COMPAT 1 /* compatible, also used without "v:" */
255#define VV_RO 2 /* read-only */
256#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258struct vimvar
259{
260 char *name; /* name of variable, without v: */
261 int len; /* length of name */
262 char_u *val; /* current value (can also be a number!) */
263 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000264 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265} vimvars[VV_LEN] =
266{ /* The order here must match the VV_ defines in vim.h! */
267 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
268 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
269 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
270 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
271 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
272 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
273 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
274 VV_COMPAT+VV_RO},
275 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
276 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
277 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000278 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
280 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
281 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
282 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
283 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
284 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
285 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
286 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
287 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
288 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
289 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
290 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000291 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
292 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
293 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
294 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
296 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
297 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
298 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
299 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
300 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
301 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000302 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303};
304
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000305static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
306static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
307static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
308static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
309static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
310static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
311static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
312static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
313static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
314static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
315static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
316static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
317static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000318static listvar *list_alloc __ARGS((void));
319static void list_unref __ARGS((listvar *l));
320static void list_free __ARGS((listvar *l));
321static listitem *listitem_alloc __ARGS((void));
322static void listitem_free __ARGS((listitem *item));
323static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000324static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
325static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000326static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000327static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000328static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000329static void list_append __ARGS((listvar *l, listitem *item));
330static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000331static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
332static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
333static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000334static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000335static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000336static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000337static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000338static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000340static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000341static 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));
342static 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 +0000343
344static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000345static void f_append __ARGS((typeval *argvars, typeval *rettv));
346static void f_argc __ARGS((typeval *argvars, typeval *rettv));
347static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
348static void f_argv __ARGS((typeval *argvars, typeval *rettv));
349static void f_browse __ARGS((typeval *argvars, typeval *rettv));
350static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000351static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
352static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
353static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000354static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
355static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
356static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
357static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
358static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000359static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000360static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
361static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
362static void f_col __ARGS((typeval *argvars, typeval *rettv));
363static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
364static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000365static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000366static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
367static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
368static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
369static void f_delete __ARGS((typeval *argvars, typeval *rettv));
370static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
371static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
372static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000373static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000374static void f_escape __ARGS((typeval *argvars, typeval *rettv));
375static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
376static void f_executable __ARGS((typeval *argvars, typeval *rettv));
377static void f_exists __ARGS((typeval *argvars, typeval *rettv));
378static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000379static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000380static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
381static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
382static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
383static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000384static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
385static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
386static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000387static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
388static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
389static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
390static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
391static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000392static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000393static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
394static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
395static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
396static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
397static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
398static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
399static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
400static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
401static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
402static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
403static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
404static void f_getline __ARGS((typeval *argvars, typeval *rettv));
405static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
406static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
407static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
408static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
409static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
410static void f_glob __ARGS((typeval *argvars, typeval *rettv));
411static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
412static void f_has __ARGS((typeval *argvars, typeval *rettv));
413static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
414static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
415static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
416static void f_histget __ARGS((typeval *argvars, typeval *rettv));
417static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000418static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000419static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000420static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
421static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
422static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000423static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000424static void f_input __ARGS((typeval *argvars, typeval *rettv));
425static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
426static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
427static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
428static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000429static void f_insert __ARGS((typeval *argvars, typeval *rettv));
430static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000431static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
432static void f_len __ARGS((typeval *argvars, typeval *rettv));
433static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
434static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000435static void f_line __ARGS((typeval *argvars, typeval *rettv));
436static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
437static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
438static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
439static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
440static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000441static void f_match __ARGS((typeval *argvars, typeval *rettv));
442static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
443static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000444static void f_max __ARGS((typeval *argvars, typeval *rettv));
445static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000446static void f_mode __ARGS((typeval *argvars, typeval *rettv));
447static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
448static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
449static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000450static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
451static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
452static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
453static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
454static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000455static void f_remove __ARGS((typeval *argvars, typeval *rettv));
456static void f_rename __ARGS((typeval *argvars, typeval *rettv));
457static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
458static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
459static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
460static void f_search __ARGS((typeval *argvars, typeval *rettv));
461static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
463static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000464static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
465static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000466static void f_setline __ARGS((typeval *argvars, typeval *rettv));
467static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000468static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000469static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000470static void f_sort __ARGS((typeval *argvars, typeval *rettv));
471static void f_str2list __ARGS((typeval *argvars, typeval *rettv));
472#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000473static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000475static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
476static void f_string __ARGS((typeval *argvars, typeval *rettv));
477static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
478static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
479static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
480static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000481static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
482static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000483static void f_synID __ARGS((typeval *argvars, typeval *rettv));
484static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
485static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
486static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000487static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
488static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
489static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
490static void f_tr __ARGS((typeval *argvars, typeval *rettv));
491static void f_type __ARGS((typeval *argvars, typeval *rettv));
492static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
493static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
494static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
495static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
496static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
497static void f_winline __ARGS((typeval *argvars, typeval *rettv));
498static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
499static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
500static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000501
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000502static win_T *find_win_by_nr __ARGS((typeval *vp));
503static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504static int get_env_len __ARGS((char_u **arg));
505static int get_id_len __ARGS((char_u **arg));
506static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000507static 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 +0000508static int eval_isnamec __ARGS((int c));
509static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000510static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
511static typeval *alloc_tv __ARGS((void));
512static typeval *alloc_string_tv __ARGS((char_u *string));
513static void free_tv __ARGS((typeval *varp));
514static void clear_tv __ARGS((typeval *varp));
515static void init_tv __ARGS((typeval *varp));
516static long get_tv_number __ARGS((typeval *varp));
517static linenr_T get_tv_lnum __ARGS((typeval *argvars));
518static char_u *get_tv_string __ARGS((typeval *varp));
519static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520static VAR find_var __ARGS((char_u *name, int writing));
521static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
522static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000523static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524static void list_one_var __ARGS((VAR v, char_u *prefix));
525static void list_vim_var __ARGS((int i));
526static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000527static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
530static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
531static int eval_fname_script __ARGS((char_u *p));
532static int eval_fname_sid __ARGS((char_u *p));
533static void list_func_head __ARGS((ufunc_T *fp, int indent));
534static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
535static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000536static int function_exists __ARGS((char_u *name));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000537static 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 +0000538
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000539#define get_var_string(p) get_tv_string(&(p)->tv)
540#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
541#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543static 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 +0000544
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000545static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
546static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
547static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000548static void list_all_vars __ARGS((void));
549static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
550static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
551static 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 +0000552static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554/*
555 * Set an internal variable to a string value. Creates the variable if it does
556 * not already exist.
557 */
558 void
559set_internal_string_var(name, value)
560 char_u *name;
561 char_u *value;
562{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000563 char_u *val;
564 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566 val = vim_strsave(value);
567 if (val != NULL)
568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000569 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000570 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000572 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000573 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575 }
576}
577
578# if defined(FEAT_MBYTE) || defined(PROTO)
579 int
580eval_charconvert(enc_from, enc_to, fname_from, fname_to)
581 char_u *enc_from;
582 char_u *enc_to;
583 char_u *fname_from;
584 char_u *fname_to;
585{
586 int err = FALSE;
587
588 set_vim_var_string(VV_CC_FROM, enc_from, -1);
589 set_vim_var_string(VV_CC_TO, enc_to, -1);
590 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
591 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
592 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
593 err = TRUE;
594 set_vim_var_string(VV_CC_FROM, NULL, -1);
595 set_vim_var_string(VV_CC_TO, NULL, -1);
596 set_vim_var_string(VV_FNAME_IN, NULL, -1);
597 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
598
599 if (err)
600 return FAIL;
601 return OK;
602}
603# endif
604
605# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
606 int
607eval_printexpr(fname, args)
608 char_u *fname;
609 char_u *args;
610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
631eval_diff(origfile, newfile, outfile)
632 char_u *origfile;
633 char_u *newfile;
634 char_u *outfile;
635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
648eval_patch(origfile, difffile, outfile)
649 char_u *origfile;
650 char_u *difffile;
651 char_u *outfile;
652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
671eval_to_bool(arg, error, nextcmd, skip)
672 char_u *arg;
673 int *error;
674 char_u **nextcmd;
675 int skip; /* only parse, don't execute */
676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000677 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int retval = FALSE;
679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689 retval = (get_tv_number(&tv) != 0);
690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
696 return retval;
697}
698
699/*
700 * Top level evaluation function, returning a string. If "skip" is TRUE,
701 * only parsing to "nextcmd" is done, without reporting errors. Return
702 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
703 */
704 char_u *
705eval_to_string_skip(arg, nextcmd, skip)
706 char_u *arg;
707 char_u **nextcmd;
708 int skip; /* only parse, don't execute */
709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000710 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 char_u *retval;
712
713 if (skip)
714 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000715 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 retval = NULL;
717 else
718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 retval = vim_strsave(get_tv_string(&tv));
720 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722 if (skip)
723 --emsg_skip;
724
725 return retval;
726}
727
728/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000729 * Skip over an expression at "*pp".
730 * Return FAIL for an error, OK otherwise.
731 */
732 int
733skip_expr(pp)
734 char_u **pp;
735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000736 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000737
738 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000740}
741
742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * Top level evaluation function, returning a string.
744 * Return pointer to allocated memory, or NULL for failure.
745 */
746 char_u *
747eval_to_string(arg, nextcmd)
748 char_u *arg;
749 char_u **nextcmd;
750{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000751 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *retval;
753
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 retval = NULL;
756 else
757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000758 retval = vim_strsave(get_tv_string(&tv));
759 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761
762 return retval;
763}
764
765/*
766 * Call eval_to_string() with "sandbox" set and not using local variables.
767 */
768 char_u *
769eval_to_string_safe(arg, nextcmd)
770 char_u *arg;
771 char_u **nextcmd;
772{
773 char_u *retval;
774 void *save_funccalp;
775
776 save_funccalp = save_funccal();
777 ++sandbox;
778 retval = eval_to_string(arg, nextcmd);
779 --sandbox;
780 restore_funccal(save_funccalp);
781 return retval;
782}
783
784#if 0 /* not used */
785/*
786 * Top level evaluation function, returning a string.
787 * Advances "arg" to the first non-blank after the evaluated expression.
788 * Return pointer to allocated memory, or NULL for failure.
789 * Doesn't give error messages.
790 */
791 char_u *
792eval_arg_to_string(arg)
793 char_u **arg;
794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000795 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 char_u *retval;
797 int ret;
798
799 ++emsg_off;
800
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (ret == FAIL)
803 retval = NULL;
804 else
805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000806 retval = vim_strsave(get_tv_string(&rettv));
807 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809
810 --emsg_off;
811
812 return retval;
813}
814#endif
815
816/*
817 * Top level evaluation function, returning a number.
818 * Evaluates "expr" silently.
819 * Returns -1 for an error.
820 */
821 int
822eval_to_number(expr)
823 char_u *expr;
824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 int retval;
827 char_u *p = expr;
828
829 ++emsg_off;
830
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 retval = -1;
833 else
834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 retval = get_tv_number(&rettv);
836 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 --emsg_off;
839
840 return retval;
841}
842
843#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
844/*
845 * Call some vimL function and return the result as a string
846 * Uses argv[argc] for the function arguments.
847 */
848 char_u *
849call_vim_function(func, argc, argv, safe)
850 char_u *func;
851 int argc;
852 char_u **argv;
853 int safe; /* use the sandbox */
854{
855 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000857 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 long n;
859 int len;
860 int i;
861 int doesrange;
862 void *save_funccalp = NULL;
863
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000864 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (argvars == NULL)
866 return NULL;
867
868 for (i = 0; i < argc; i++)
869 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000870 /* Pass a NULL or empty argument as an empty string */
871 if (argv[i] == NULL || *argv[i] == NUL)
872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000873 argvars[i].v_type = VAR_STRING;
874 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000875 continue;
876 }
877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 /* Recognize a number argument, the others must be strings. */
879 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
880 if (len != 0 && len == (int)STRLEN(argv[i]))
881 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000882 argvars[i].v_type = VAR_NUMBER;
883 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885 else
886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000887 argvars[i].v_type = VAR_STRING;
888 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
890 }
891
892 if (safe)
893 {
894 save_funccalp = save_funccal();
895 ++sandbox;
896 }
897
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
899 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
901 &doesrange, TRUE) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 vim_free(argvars);
906
907 if (safe)
908 {
909 --sandbox;
910 restore_funccal(save_funccalp);
911 }
912 return retval;
913}
914#endif
915
916/*
917 * Save the current function call pointer, and set it to NULL.
918 * Used when executing autocommands and for ":source".
919 */
920 void *
921save_funccal()
922{
923 struct funccall *fc;
924
925 fc = current_funccal;
926 current_funccal = NULL;
927 return (void *)fc;
928}
929
930 void
931restore_funccal(fc)
932 void *fc;
933{
934 current_funccal = (struct funccall *)fc;
935}
936
937#ifdef FEAT_FOLDING
938/*
939 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
940 * it in "*cp". Doesn't give error messages.
941 */
942 int
943eval_foldexpr(arg, cp)
944 char_u *arg;
945 int *cp;
946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 int retval;
949 char_u *s;
950
951 ++emsg_off;
952 ++sandbox;
953 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 retval = 0;
956 else
957 {
958 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959 if (tv.v_type == VAR_NUMBER)
960 retval = tv.vval.v_number;
961 else if (tv.v_type == VAR_UNKNOWN
962 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 retval = 0;
964 else
965 {
966 /* If the result is a string, check if there is a non-digit before
967 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (!VIM_ISDIGIT(*s) && *s != '-')
970 *cp = *s++;
971 retval = atol((char *)s);
972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 --emsg_off;
976 --sandbox;
977
978 return retval;
979}
980#endif
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Expands out the 'magic' {}'s in a variable/function name.
984 * Note that this can call itself recursively, to deal with
985 * constructs like foo{bar}{baz}{bam}
986 * The four pointer arguments point to "foo{expre}ss{ion}bar"
987 * "in_start" ^
988 * "expr_start" ^
989 * "expr_end" ^
990 * "in_end" ^
991 *
992 * Returns a new allocated string, which the caller must free.
993 * Returns NULL for failure.
994 */
995 static char_u *
996make_expanded_name(in_start, expr_start, expr_end, in_end)
997 char_u *in_start;
998 char_u *expr_start;
999 char_u *expr_end;
1000 char_u *in_end;
1001{
1002 char_u c1;
1003 char_u *retval = NULL;
1004 char_u *temp_result;
1005 char_u *nextcmd = NULL;
1006
1007 if (expr_end == NULL || in_end == NULL)
1008 return NULL;
1009 *expr_start = NUL;
1010 *expr_end = NUL;
1011 c1 = *in_end;
1012 *in_end = NUL;
1013
1014 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1015 if (temp_result != NULL && nextcmd == NULL)
1016 {
1017 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1018 + (in_end - expr_end) + 1));
1019
1020 if (retval != NULL)
1021 {
1022 STRCPY(retval, in_start);
1023 STRCAT(retval, temp_result);
1024 STRCAT(retval, expr_end + 1);
1025 }
1026 }
1027 vim_free(temp_result);
1028
1029 *in_end = c1; /* put char back for error messages */
1030 *expr_start = '{';
1031 *expr_end = '}';
1032
1033 if (retval != NULL)
1034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (expr_start != NULL)
1037 {
1038 /* Further expansion! */
1039 temp_result = make_expanded_name(retval, expr_start,
1040 expr_end, temp_result);
1041 vim_free(retval);
1042 retval = temp_result;
1043 }
1044 }
1045
1046 return retval;
1047
1048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001051 * ":let" list all variable values
1052 * ":let var1 var2" list variable values
1053 * ":let var = expr" assignment command.
1054 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 */
1056 void
1057ex_let(eap)
1058 exarg_T *eap;
1059{
1060 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001061 char_u *expr = NULL;
1062 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001064 int var_count = 0;
1065 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001067 expr = skip_var_list(arg, &var_count, &semicolon);
1068 if (expr == NULL)
1069 return;
1070 expr = vim_strchr(expr, '=');
1071 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001073 if (*arg == '[')
1074 EMSG(_(e_invarg));
1075 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 /* ":let var1 var2" */
1077 arg = list_arg_vars(eap, arg);
1078 else if (!eap->skip)
1079 /* ":let" */
1080 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 eap->nextcmd = check_nextcmd(arg);
1082 }
1083 else
1084 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001085 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (eap->skip)
1088 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001089 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (eap->skip)
1091 {
1092 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 --emsg_skip;
1095 }
1096 else if (i != FAIL)
1097 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001098 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1099 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 }
1103}
1104
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001105/*
1106 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1107 * Handles both "var" with any type and "[var, var; var]" with a list type.
1108 * Returns OK or FAIL;
1109 */
1110 static int
1111ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1112 char_u *arg_start;
1113 typeval *tv;
1114 int copy; /* copy values from "tv", don't move */
1115 int semicolon; /* from skip_var_list() */
1116 int var_count; /* from skip_var_list() */
1117 char_u *nextchars; /* characters that must follow or NULL */
1118{
1119 char_u *arg = arg_start;
1120 listvar *l;
1121 int i;
1122 listitem *item;
1123 typeval ltv;
1124
1125 if (*arg != '[')
1126 {
1127 /*
1128 * ":let var = expr" or ":for var in list"
1129 */
1130 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1131 return FAIL;
1132 return OK;
1133 }
1134
1135 /*
1136 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1137 */
1138 l = tv->vval.v_list;
1139 if (tv->v_type != VAR_LIST || l == NULL)
1140 {
1141 EMSG(_(e_listreq));
1142 return FAIL;
1143 }
1144
1145 i = list_len(l);
1146 if (semicolon == 0 && var_count < i)
1147 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001148 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001149 return FAIL;
1150 }
1151 if (var_count - semicolon > i)
1152 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001153 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001154 return FAIL;
1155 }
1156
1157 item = l->lv_first;
1158 while (*arg != ']')
1159 {
1160 arg = skipwhite(arg + 1);
1161 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1162 item = item->li_next;
1163 if (arg == NULL)
1164 return FAIL;
1165
1166 arg = skipwhite(arg);
1167 if (*arg == ';')
1168 {
1169 /* Put the rest of the list (may be empty) in the var after ';'.
1170 * Create a new list for this. */
1171 l = list_alloc();
1172 if (l == NULL)
1173 return FAIL;
1174 while (item != NULL)
1175 {
1176 list_append_tv(l, &item->li_tv);
1177 item = item->li_next;
1178 }
1179
1180 ltv.v_type = VAR_LIST;
1181 ltv.vval.v_list = l;
1182 l->lv_refcount = 1;
1183
1184 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1185 clear_tv(&ltv);
1186 if (arg == NULL)
1187 return FAIL;
1188 break;
1189 }
1190 else if (*arg != ',' && *arg != ']')
1191 {
1192 EMSG2(_(e_intern2), "ex_let_vars()");
1193 return FAIL;
1194 }
1195 }
1196
1197 return OK;
1198}
1199
1200/*
1201 * Skip over assignable variable "var" or list of variables "[var, var]".
1202 * Used for ":let varvar = expr" and ":for varvar in expr".
1203 * For "[var, var]" increment "*var_count" for each variable.
1204 * for "[var, var; var]" set "semicolon".
1205 * Return NULL for an error.
1206 */
1207 static char_u *
1208skip_var_list(arg, var_count, semicolon)
1209 char_u *arg;
1210 int *var_count;
1211 int *semicolon;
1212{
1213 char_u *p, *s;
1214
1215 if (*arg == '[')
1216 {
1217 /* "[var, var]": find the matching ']'. */
1218 p = arg;
1219 while (1)
1220 {
1221 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1222 s = skip_var_one(p);
1223 if (s == p)
1224 {
1225 EMSG2(_(e_invarg2), p);
1226 return NULL;
1227 }
1228 ++*var_count;
1229
1230 p = skipwhite(s);
1231 if (*p == ']')
1232 break;
1233 else if (*p == ';')
1234 {
1235 if (*semicolon == 1)
1236 {
1237 EMSG(_("Double ; in list of variables"));
1238 return NULL;
1239 }
1240 *semicolon = 1;
1241 }
1242 else if (*p != ',')
1243 {
1244 EMSG2(_(e_invarg2), p);
1245 return NULL;
1246 }
1247 }
1248 return p + 1;
1249 }
1250 else
1251 return skip_var_one(arg);
1252}
1253
1254 static char_u *
1255skip_var_one(arg)
1256 char_u *arg;
1257{
1258 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1259 ++arg;
1260 return find_name_end(arg, NULL, NULL, TRUE);
1261}
1262
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 static void
1264list_all_vars()
1265{
1266 int i;
1267
1268 /*
1269 * List all variables.
1270 */
1271 for (i = 0; i < variables.ga_len && !got_int; ++i)
1272 if (VAR_ENTRY(i).v_name != NULL)
1273 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1274 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1275 if (BVAR_ENTRY(i).v_name != NULL)
1276 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1277 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1278 if (WVAR_ENTRY(i).v_name != NULL)
1279 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1280 for (i = 0; i < VV_LEN && !got_int; ++i)
1281 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
1282 list_vim_var(i);
1283}
1284
1285/*
1286 * List variables in "arg".
1287 */
1288 static char_u *
1289list_arg_vars(eap, arg)
1290 exarg_T *eap;
1291 char_u *arg;
1292{
1293 int error = FALSE;
1294 char_u *temp_string = NULL;
1295 int arg_len;
1296 char_u *expr_start;
1297 char_u *expr_end;
1298 char_u *name_end;
1299 int c1 = 0, c2;
1300 int i;
1301 VAR varp;
1302 char_u *name;
1303
1304 while (!ends_excmd(*arg) && !got_int)
1305 {
1306 /* Find the end of the name. */
1307 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1308
1309 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1310 {
1311 emsg_severe = TRUE;
1312 EMSG(_(e_trailing));
1313 break;
1314 }
1315 if (!error && !eap->skip)
1316 {
1317 if (expr_start != NULL)
1318 {
1319 temp_string = make_expanded_name(arg, expr_start,
1320 expr_end, name_end);
1321 if (temp_string == NULL)
1322 {
1323 /*
1324 * Report an invalid expression in braces, unless
1325 * the expression evaluation has been cancelled due
1326 * to an aborting error, an interrupt, or an
1327 * exception.
1328 */
1329 if (!aborting())
1330 {
1331 emsg_severe = TRUE;
1332 EMSG2(_(e_invarg2), arg);
1333 break;
1334 }
1335 error = TRUE;
1336 arg = skipwhite(name_end);
1337 continue;
1338 }
1339 arg = temp_string;
1340 arg_len = STRLEN(temp_string);
1341 }
1342 else
1343 {
1344 c1 = *name_end;
1345 *name_end = NUL;
1346 arg_len = (int)(name_end - arg);
1347 }
1348 i = find_vim_var(arg, arg_len);
1349 if (i >= 0)
1350 list_vim_var(i);
1351 else if (STRCMP("b:changedtick", arg) == 0)
1352 {
1353 char_u numbuf[NUMBUFLEN];
1354
1355 sprintf((char *)numbuf, "%ld",
1356 (long)curbuf->b_changedtick);
1357 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1358 VAR_NUMBER, numbuf);
1359 }
1360 else
1361 {
1362 varp = find_var(arg, FALSE);
1363 if (varp == NULL)
1364 {
1365 /* Skip further arguments but do continue to
1366 * search for a trailing command. */
1367 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1368 error = TRUE;
1369 }
1370 else
1371 {
1372 name = vim_strchr(arg, ':');
1373 if (name != NULL)
1374 {
1375 /* "a:" vars have no name stored, use whole arg */
1376 if (arg[0] == 'a' && arg[1] == ':')
1377 c2 = NUL;
1378 else
1379 {
1380 c2 = *++name;
1381 *name = NUL;
1382 }
1383 list_one_var(varp, arg);
1384 if (c2 != NUL)
1385 *name = c2;
1386 }
1387 else
1388 list_one_var(varp, (char_u *)"");
1389 }
1390 }
1391 if (expr_start != NULL)
1392 vim_free(temp_string);
1393 else
1394 *name_end = c1;
1395 }
1396 arg = skipwhite(name_end);
1397 }
1398
1399 return arg;
1400}
1401
1402/*
1403 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1404 * Returns a pointer to the char just after the var name.
1405 * Returns NULL if there is an error.
1406 */
1407 static char_u *
1408ex_let_one(arg, tv, copy, endchars)
1409 char_u *arg; /* points to variable name */
1410 typeval *tv; /* value to assign to variable */
1411 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413{
1414 int c1;
1415 char_u *name;
1416 char_u *p;
1417 char_u *arg_end = NULL;
1418 int len;
1419 int opt_flags;
1420
1421 /*
1422 * ":let $VAR = expr": Set environment variable.
1423 */
1424 if (*arg == '$')
1425 {
1426 /* Find the end of the name. */
1427 ++arg;
1428 name = arg;
1429 len = get_env_len(&arg);
1430 if (len == 0)
1431 EMSG2(_(e_invarg2), name - 1);
1432 else
1433 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001434 if (endchars != NULL
1435 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 EMSG(_(e_letunexp));
1437 else
1438 {
1439 c1 = name[len];
1440 name[len] = NUL;
1441 p = get_tv_string(tv);
1442 vim_setenv(name, p);
1443 if (STRICMP(name, "HOME") == 0)
1444 init_homedir();
1445 else if (didset_vim && STRICMP(name, "VIM") == 0)
1446 didset_vim = FALSE;
1447 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1448 didset_vimruntime = FALSE;
1449 name[len] = c1;
1450 arg_end = arg;
1451 }
1452 }
1453 }
1454
1455 /*
1456 * ":let &option = expr": Set option value.
1457 * ":let &l:option = expr": Set local option value.
1458 * ":let &g:option = expr": Set global option value.
1459 */
1460 else if (*arg == '&')
1461 {
1462 /* Find the end of the name. */
1463 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001464 if (p == NULL || (endchars != NULL
1465 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001466 EMSG(_(e_letunexp));
1467 else
1468 {
1469 c1 = *p;
1470 *p = NUL;
1471 set_option_value(arg, get_tv_number(tv),
1472 get_tv_string(tv), opt_flags);
1473 *p = c1;
1474 arg_end = p;
1475 }
1476 }
1477
1478 /*
1479 * ":let @r = expr": Set register contents.
1480 */
1481 else if (*arg == '@')
1482 {
1483 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 if (endchars != NULL
1485 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 EMSG(_(e_letunexp));
1487 else
1488 {
1489 write_reg_contents(*arg == '@' ? '"' : *arg,
1490 get_tv_string(tv), -1, FALSE);
1491 arg_end = arg + 1;
1492 }
1493 }
1494
1495 /*
1496 * ":let var = expr": Set internal variable.
1497 */
1498 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1499 {
1500 char_u *exp_name = NULL;
1501 char_u *expr_start, *expr_end;
1502
1503 /* Find the end of the name. */
1504 p = find_name_end(arg, &expr_start, &expr_end, FALSE);
1505 if (expr_start != NULL)
1506 {
1507 exp_name = make_expanded_name(arg, expr_start, expr_end, p);
1508 arg = exp_name;
1509 }
1510
1511 if (arg == NULL)
1512 {
1513 /* Report an invalid expression in braces, unless the
1514 * expression evaluation has been cancelled due to an
1515 * aborting error, an interrupt, or an exception. */
1516 if (!aborting())
1517 EMSG2(_(e_invarg2), arg);
1518 }
1519 else if (*p == '[')
1520 arg_end = set_var_idx(arg, p, tv, copy, endchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 else if (endchars != NULL
1522 && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 EMSG(_(e_letunexp));
1524 else if (STRNCMP(arg, "b:changedtick", 13) == 0
1525 && !eval_isnamec(arg[13]))
1526 EMSG2(_(e_readonlyvar), arg);
1527 else
1528 {
1529 c1 = *p;
1530 *p = NUL;
1531 set_var(arg, tv, copy);
1532 *p = c1;
1533 arg_end = p;
1534 }
1535
1536 vim_free(exp_name);
1537 }
1538
1539 else
1540 EMSG2(_(e_invarg2), arg);
1541
1542 return arg_end;
1543}
1544
1545/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001546 * Set a variable with an index: "name[expr]", "name[expr:expr]",
1547 * "name[expr][expr]", etc. Only works if "name" is an existing List.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 * "ip" points to the first '['.
1549 * Returns a pointer to just after the last used ']'; NULL for error.
1550 */
1551 static char_u *
1552set_var_idx(name, ip, rettv, copy, endchars)
1553 char_u *name;
1554 char_u *ip;
1555 typeval *rettv;
1556 int copy;
1557 char_u *endchars;
1558{
1559 VAR v;
1560 int c1;
1561 char_u *p;
1562 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001563 typeval var2;
1564 int range = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 typeval *tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001566 long n1 = 0, n2 = 0;
1567 int empty1, empty2 = FALSE;
1568 listitem *item = NULL;
1569 listitem *ni;
1570 listitem *ri;
1571 listvar *l = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572
1573 c1 = *ip;
1574 *ip = NUL;
1575 v = find_var(name, TRUE);
1576 if (v == NULL)
1577 EMSG2(_(e_undefvar), name);
1578 *ip = c1;
1579 if (v == NULL)
1580 return NULL;
1581
1582 tv = &v->tv;
1583 for (p = ip; *p == '['; p = skipwhite(p + 1))
1584 {
1585 if (tv->v_type != VAR_LIST || tv->vval.v_list == NULL)
1586 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001587 EMSG(_("E689: Can only index a List"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 p = NULL;
1589 break;
1590 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001591 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001593 EMSG(_("E708: [:] must come last"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 p = NULL;
1595 break;
1596 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001597
1598 /* Get the index [expr] or the first index [expr: ]. */
1599 p = skipwhite(p + 1);
1600 if (*p == ':')
1601 empty1 = TRUE;
1602 else
1603 {
1604 empty1 = FALSE;
1605 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
1606 {
1607 p = NULL;
1608 break;
1609 }
1610 }
1611
1612 /* Optionally get the second index [ :expr]. */
1613 if (*p == ':')
1614 {
1615 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL)
1616 {
1617 EMSG(_("E709: [:] requires a List value"));
1618 p = NULL;
1619 if (!empty1)
1620 clear_tv(&var1);
1621 break;
1622 }
1623 p = skipwhite(p + 1);
1624 if (*p == ']')
1625 empty2 = TRUE;
1626 else
1627 {
1628 empty2 = FALSE;
1629 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1630 {
1631 p = NULL;
1632 if (!empty1)
1633 clear_tv(&var1);
1634 break;
1635 }
1636 }
1637 range = TRUE;
1638 }
1639 else
1640 range = FALSE;
1641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 if (*p != ']')
1643 {
1644 EMSG(_(e_missbrac));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001645 if (!empty1)
1646 clear_tv(&var1);
1647 if (range && !empty2)
1648 clear_tv(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 p = NULL;
1650 break;
1651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001652
1653 /*
1654 * Get the number and item for the only or first index.
1655 */
1656 if (empty1)
1657 n1 = 0;
1658 else
1659 {
1660 n1 = get_tv_number(&var1);
1661 clear_tv(&var1);
1662 }
1663 l = tv->vval.v_list;
1664 item = list_find(l, n1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 if (item == NULL)
1666 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001667 EMSGN(_(e_listidx), n1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 p = NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001669 if (range && !empty2)
1670 clear_tv(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 break;
1672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001673
1674 /*
1675 * May need to find the item or absolute index for the second index of
1676 * a range.
1677 * When no index given: "empty2" is TRUE.
1678 * Otherwise "n2" is set to the second index.
1679 */
1680 if (range && !empty2)
1681 {
1682 n2 = get_tv_number(&var2);
1683 clear_tv(&var2);
1684 if (n2 < 0)
1685 {
1686 ni = list_find(l, n2);
1687 if (ni == NULL)
1688 {
1689 EMSGN(_(e_listidx), n2);
1690 p = NULL;
1691 break;
1692 }
1693 n2 = list_idx_of_item(l, ni);
1694 }
1695
1696 /* Check that n2 isn't before n1. */
1697 if (n1 < 0)
1698 n1 = list_idx_of_item(l, item);
1699 if (n2 < n1)
1700 {
1701 EMSGN(_(e_listidx), n2);
1702 p = NULL;
1703 break;
1704 }
1705 }
1706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001707 tv = &item->li_tv;
1708 }
1709
1710 if (p != NULL)
1711 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 {
1714 EMSG(_(e_letunexp));
1715 p = NULL;
1716 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001717 else if (range)
1718 {
1719 /*
1720 * Assign the List values to the list items.
1721 */
1722 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1723 {
1724 clear_tv(&item->li_tv);
1725 copy_tv(&ri->li_tv, &item->li_tv);
1726 ri = ri->li_next;
1727 if (ri == NULL || (!empty2 && n2 == n1))
1728 break;
1729 if (item->li_next == NULL)
1730 {
1731 /* Need to add an empty item. */
1732 ni = listitem_alloc();
1733 if (ni == NULL)
1734 {
1735 ri = NULL;
1736 break;
1737 }
1738 ni->li_tv.v_type = VAR_NUMBER;
1739 ni->li_tv.vval.v_number = 0;
1740 list_append(l, ni);
1741 }
1742 item = item->li_next;
1743 ++n1;
1744 }
1745 if (ri != NULL)
1746 EMSG(_("E710: List value has more items than target"));
1747 else if (empty2 ? item != NULL && item->li_next != NULL : n1 != n2)
1748 EMSG(_("E711: List value has not enough items"));
1749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 else
1751 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001752 /*
1753 * Assign the value to the variable or list item.
1754 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 clear_tv(tv);
1756 if (copy)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001757 copy_tv(rettv, tv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 else
1759 {
1760 *tv = *rettv;
1761 init_tv(rettv);
1762 }
1763 }
1764 }
1765 return p;
1766}
1767
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768/*
1769 * Add a watcher to a list.
1770 */
1771 static void
1772list_add_watch(l, lw)
1773 listvar *l;
1774 listwatch *lw;
1775{
1776 lw->lw_next = l->lv_watch;
1777 l->lv_watch = lw;
1778}
1779
1780/*
1781 * Remove a watches from a list.
1782 * No warning when it isn't found...
1783 */
1784 static void
1785list_rem_watch(l, lwrem)
1786 listvar *l;
1787 listwatch *lwrem;
1788{
1789 listwatch *lw, **lwp;
1790
1791 lwp = &l->lv_watch;
1792 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1793 {
1794 if (lw == lwrem)
1795 {
1796 *lwp = lw->lw_next;
1797 break;
1798 }
1799 lwp = &lw->lw_next;
1800 }
1801}
1802
1803/*
1804 * Just before removing an item from a list: advance watchers to the next
1805 * item.
1806 */
1807 static void
1808list_fix_watch(l, item)
1809 listvar *l;
1810 listitem *item;
1811{
1812 listwatch *lw;
1813
1814 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1815 if (lw->lw_item == item)
1816 lw->lw_item = item->li_next;
1817}
1818
1819/*
1820 * Evaluate the expression used in a ":for var in expr" command.
1821 * "arg" points to "var".
1822 * Set "*errp" to TRUE for an error, FALSE otherwise;
1823 * Return a pointer that holds the info. Null when there is an error.
1824 */
1825 void *
1826eval_for_line(arg, errp, nextcmdp, skip)
1827 char_u *arg;
1828 int *errp;
1829 char_u **nextcmdp;
1830 int skip;
1831{
1832 forinfo *fi;
1833 char_u *expr;
1834 typeval tv;
1835 listvar *l;
1836
1837 *errp = TRUE; /* default: there is an error */
1838
1839 fi = (forinfo *)alloc_clear(sizeof(forinfo));
1840 if (fi == NULL)
1841 return NULL;
1842
1843 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1844 if (expr == NULL)
1845 return fi;
1846
1847 expr = skipwhite(expr);
1848 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
1849 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001850 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 return fi;
1852 }
1853
1854 if (skip)
1855 ++emsg_skip;
1856 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1857 {
1858 *errp = FALSE;
1859 if (!skip)
1860 {
1861 l = tv.vval.v_list;
1862 if (tv.v_type != VAR_LIST || l == NULL)
1863 EMSG(_(e_listreq));
1864 else
1865 {
1866 fi->fi_list = l;
1867 list_add_watch(l, &fi->fi_lw);
1868 fi->fi_lw.lw_item = l->lv_first;
1869 }
1870 }
1871 }
1872 if (skip)
1873 --emsg_skip;
1874
1875 return fi;
1876}
1877
1878/*
1879 * Use the first item in a ":for" list. Advance to the next.
1880 * Assign the values to the variable (list). "arg" points to the first one.
1881 * Return TRUE when a valid item was found, FALSE when at end of list or
1882 * something wrong.
1883 */
1884 int
1885next_for_item(fi_void, arg)
1886 void *fi_void;
1887 char_u *arg;
1888{
1889 forinfo *fi = (forinfo *)fi_void;
1890 int result;
1891 listitem *item;
1892
1893 item = fi->fi_lw.lw_item;
1894 if (item == NULL)
1895 result = FALSE;
1896 else
1897 {
1898 fi->fi_lw.lw_item = item->li_next;
1899 result = (ex_let_vars(arg, &item->li_tv, TRUE,
1900 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
1901 }
1902 return result;
1903}
1904
1905/*
1906 * Free the structure used to store info used by ":for".
1907 */
1908 void
1909free_for_info(fi_void)
1910 void *fi_void;
1911{
1912 forinfo *fi = (forinfo *)fi_void;
1913
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001914 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 list_rem_watch(fi->fi_list, &fi->fi_lw);
1916 vim_free(fi);
1917}
1918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1920
1921 void
1922set_context_for_expression(xp, arg, cmdidx)
1923 expand_T *xp;
1924 char_u *arg;
1925 cmdidx_T cmdidx;
1926{
1927 int got_eq = FALSE;
1928 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 if (cmdidx == CMD_let)
1932 {
1933 xp->xp_context = EXPAND_USER_VARS;
1934 if (vim_strchr(arg, '=') == NULL)
1935 {
1936 /* ":let var1 var2 ...": find last space. */
1937 for (p = arg + STRLEN(arg); p > arg; )
1938 {
1939 xp->xp_pattern = p;
1940 p = mb_ptr_back(arg, p);
1941 if (vim_iswhite(*p))
1942 break;
1943 }
1944 return;
1945 }
1946 }
1947 else
1948 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1949 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 while ((xp->xp_pattern = vim_strpbrk(arg,
1951 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1952 {
1953 c = *xp->xp_pattern;
1954 if (c == '&')
1955 {
1956 c = xp->xp_pattern[1];
1957 if (c == '&')
1958 {
1959 ++xp->xp_pattern;
1960 xp->xp_context = cmdidx != CMD_let || got_eq
1961 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1962 }
1963 else if (c != ' ')
1964 xp->xp_context = EXPAND_SETTINGS;
1965 }
1966 else if (c == '$')
1967 {
1968 /* environment variable */
1969 xp->xp_context = EXPAND_ENV_VARS;
1970 }
1971 else if (c == '=')
1972 {
1973 got_eq = TRUE;
1974 xp->xp_context = EXPAND_EXPRESSION;
1975 }
1976 else if (c == '<'
1977 && xp->xp_context == EXPAND_FUNCTIONS
1978 && vim_strchr(xp->xp_pattern, '(') == NULL)
1979 {
1980 /* Function name can start with "<SNR>" */
1981 break;
1982 }
1983 else if (cmdidx != CMD_let || got_eq)
1984 {
1985 if (c == '"') /* string */
1986 {
1987 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1988 if (c == '\\' && xp->xp_pattern[1] != NUL)
1989 ++xp->xp_pattern;
1990 xp->xp_context = EXPAND_NOTHING;
1991 }
1992 else if (c == '\'') /* literal string */
1993 {
1994 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1995 /* skip */ ;
1996 xp->xp_context = EXPAND_NOTHING;
1997 }
1998 else if (c == '|')
1999 {
2000 if (xp->xp_pattern[1] == '|')
2001 {
2002 ++xp->xp_pattern;
2003 xp->xp_context = EXPAND_EXPRESSION;
2004 }
2005 else
2006 xp->xp_context = EXPAND_COMMANDS;
2007 }
2008 else
2009 xp->xp_context = EXPAND_EXPRESSION;
2010 }
2011 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 /* Doesn't look like something valid, expand as an expression
2013 * anyway. */
2014 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 arg = xp->xp_pattern;
2016 if (*arg != NUL)
2017 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2018 /* skip */ ;
2019 }
2020 xp->xp_pattern = arg;
2021}
2022
2023#endif /* FEAT_CMDL_COMPL */
2024
2025/*
2026 * ":1,25call func(arg1, arg2)" function call.
2027 */
2028 void
2029ex_call(eap)
2030 exarg_T *eap;
2031{
2032 char_u *arg = eap->arg;
2033 char_u *startarg;
2034 char_u *alias;
2035 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 int len;
2038 linenr_T lnum;
2039 int doesrange;
2040 int failed = FALSE;
2041
2042 name = arg;
2043 len = get_func_len(&arg, &alias, !eap->skip);
2044 if (len == 0)
2045 goto end;
2046 if (alias != NULL)
2047 name = alias;
2048
2049 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 if (*startarg != '(')
2053 {
2054 EMSG2(_("E107: Missing braces: %s"), name);
2055 goto end;
2056 }
2057
2058 /*
2059 * When skipping, evaluate the function once, to find the end of the
2060 * arguments.
2061 * When the function takes a range, this is discovered after the first
2062 * call, and the loop is broken.
2063 */
2064 if (eap->skip)
2065 {
2066 ++emsg_skip;
2067 lnum = eap->line2; /* do it once, also with an invalid range */
2068 }
2069 else
2070 lnum = eap->line1;
2071 for ( ; lnum <= eap->line2; ++lnum)
2072 {
2073 if (!eap->skip && eap->addr_count > 0)
2074 {
2075 curwin->w_cursor.lnum = lnum;
2076 curwin->w_cursor.col = 0;
2077 }
2078 arg = startarg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 if (get_func_tv(name, len, &rettv, &arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
2081 {
2082 failed = TRUE;
2083 break;
2084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (doesrange || eap->skip)
2087 break;
2088 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002089 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002091 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 if (aborting())
2093 break;
2094 }
2095 if (eap->skip)
2096 --emsg_skip;
2097
2098 if (!failed)
2099 {
2100 /* Check for trailing illegal characters and a following command. */
2101 if (!ends_excmd(*arg))
2102 {
2103 emsg_severe = TRUE;
2104 EMSG(_(e_trailing));
2105 }
2106 else
2107 eap->nextcmd = check_nextcmd(arg);
2108 }
2109
2110end:
2111 if (alias != NULL)
2112 vim_free(alias);
2113}
2114
2115/*
2116 * ":unlet[!] var1 ... " command.
2117 */
2118 void
2119ex_unlet(eap)
2120 exarg_T *eap;
2121{
2122 char_u *arg = eap->arg;
2123 char_u *name_end;
2124 char_u cc;
2125 char_u *expr_start;
2126 char_u *expr_end;
2127 int error = FALSE;
2128
2129 do
2130 {
2131 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
2135 {
2136 emsg_severe = TRUE;
2137 EMSG(_(e_trailing));
2138 break;
2139 }
2140
2141 if (!error && !eap->skip)
2142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (expr_start != NULL)
2144 {
2145 char_u *temp_string;
2146
2147 temp_string = make_expanded_name(arg, expr_start,
2148 expr_end, name_end);
2149 if (temp_string == NULL)
2150 {
2151 /*
2152 * Report an invalid expression in braces, unless the
2153 * expression evaluation has been cancelled due to an
2154 * aborting error, an interrupt, or an exception.
2155 */
2156 if (!aborting())
2157 {
2158 emsg_severe = TRUE;
2159 EMSG2(_(e_invarg2), arg);
2160 break;
2161 }
2162 error = TRUE;
2163 }
2164 else
2165 {
2166 if (do_unlet(temp_string) == FAIL && !eap->forceit)
2167 {
2168 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
2169 error = TRUE;
2170 }
2171 vim_free(temp_string);
2172 }
2173 }
2174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
2176 cc = *name_end;
2177 *name_end = NUL;
2178
2179 if (do_unlet(arg) == FAIL && !eap->forceit)
2180 {
2181 EMSG2(_("E108: No such variable: \"%s\""), arg);
2182 error = TRUE;
2183 }
2184
2185 *name_end = cc;
2186 }
2187 }
2188 arg = skipwhite(name_end);
2189 } while (!ends_excmd(*arg));
2190
2191 eap->nextcmd = check_nextcmd(arg);
2192}
2193
2194/*
2195 * "unlet" a variable. Return OK if it existed, FAIL if not.
2196 */
2197 int
2198do_unlet(name)
2199 char_u *name;
2200{
2201 VAR v;
2202
2203 v = find_var(name, TRUE);
2204 if (v != NULL)
2205 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002206 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 return OK;
2208 }
2209 return FAIL;
2210}
2211
2212#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2213/*
2214 * Delete all "menutrans_" variables.
2215 */
2216 void
2217del_menutrans_vars()
2218{
2219 int i;
2220
2221 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002222 if (VAR_ENTRY(i).v_name != NULL
2223 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2224 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225}
2226#endif
2227
2228#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2229
2230/*
2231 * Local string buffer for the next two functions to store a variable name
2232 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2233 * get_user_var_name().
2234 */
2235
2236static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2237
2238static char_u *varnamebuf = NULL;
2239static int varnamebuflen = 0;
2240
2241/*
2242 * Function to concatenate a prefix and a variable name.
2243 */
2244 static char_u *
2245cat_prefix_varname(prefix, name)
2246 int prefix;
2247 char_u *name;
2248{
2249 int len;
2250
2251 len = (int)STRLEN(name) + 3;
2252 if (len > varnamebuflen)
2253 {
2254 vim_free(varnamebuf);
2255 len += 10; /* some additional space */
2256 varnamebuf = alloc(len);
2257 if (varnamebuf == NULL)
2258 {
2259 varnamebuflen = 0;
2260 return NULL;
2261 }
2262 varnamebuflen = len;
2263 }
2264 *varnamebuf = prefix;
2265 varnamebuf[1] = ':';
2266 STRCPY(varnamebuf + 2, name);
2267 return varnamebuf;
2268}
2269
2270/*
2271 * Function given to ExpandGeneric() to obtain the list of user defined
2272 * (global/buffer/window/built-in) variable names.
2273 */
2274/*ARGSUSED*/
2275 char_u *
2276get_user_var_name(xp, idx)
2277 expand_T *xp;
2278 int idx;
2279{
2280 static int gidx;
2281 static int bidx;
2282 static int widx;
2283 static int vidx;
2284 char_u *name;
2285
2286 if (idx == 0)
2287 gidx = bidx = widx = vidx = 0;
2288 if (gidx < variables.ga_len) /* Global variables */
2289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002290 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 && gidx < variables.ga_len)
2292 /* skip */;
2293 if (name != NULL)
2294 {
2295 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2296 return cat_prefix_varname('g', name);
2297 else
2298 return name;
2299 }
2300 }
2301 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002303 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 && bidx < curbuf->b_vars.ga_len)
2305 /* skip */;
2306 if (name != NULL)
2307 return cat_prefix_varname('b', name);
2308 }
2309 if (bidx == curbuf->b_vars.ga_len)
2310 {
2311 ++bidx;
2312 return (char_u *)"b:changedtick";
2313 }
2314 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2315 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002316 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 && widx < curwin->w_vars.ga_len)
2318 /* skip */;
2319 if (name != NULL)
2320 return cat_prefix_varname('w', name);
2321 }
2322 if (vidx < VV_LEN) /* Built-in variables */
2323 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2324
2325 vim_free(varnamebuf);
2326 varnamebuf = NULL;
2327 varnamebuflen = 0;
2328 return NULL;
2329}
2330
2331#endif /* FEAT_CMDL_COMPL */
2332
2333/*
2334 * types for expressions.
2335 */
2336typedef enum
2337{
2338 TYPE_UNKNOWN = 0
2339 , TYPE_EQUAL /* == */
2340 , TYPE_NEQUAL /* != */
2341 , TYPE_GREATER /* > */
2342 , TYPE_GEQUAL /* >= */
2343 , TYPE_SMALLER /* < */
2344 , TYPE_SEQUAL /* <= */
2345 , TYPE_MATCH /* =~ */
2346 , TYPE_NOMATCH /* !~ */
2347} exptype_T;
2348
2349/*
2350 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2353 */
2354
2355/*
2356 * Handle zero level expression.
2357 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 * Return OK or FAIL.
2360 */
2361 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 char_u **nextcmd;
2366 int evaluate;
2367{
2368 int ret;
2369 char_u *p;
2370
2371 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (ret == FAIL || !ends_excmd(*p))
2374 {
2375 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /*
2378 * Report the invalid expression unless the expression evaluation has
2379 * been cancelled due to an aborting error, an interrupt, or an
2380 * exception.
2381 */
2382 if (!aborting())
2383 EMSG2(_(e_invexpr2), arg);
2384 ret = FAIL;
2385 }
2386 if (nextcmd != NULL)
2387 *nextcmd = check_nextcmd(p);
2388
2389 return ret;
2390}
2391
2392/*
2393 * Handle top level expression:
2394 * expr1 ? expr0 : expr0
2395 *
2396 * "arg" must point to the first non-white of the expression.
2397 * "arg" is advanced to the next non-white after the recognized expression.
2398 *
2399 * Return OK or FAIL.
2400 */
2401 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 int evaluate;
2406{
2407 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002408 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 /*
2411 * Get the first variable.
2412 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return FAIL;
2415
2416 if ((*arg)[0] == '?')
2417 {
2418 result = FALSE;
2419 if (evaluate)
2420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
2426 /*
2427 * Get the second variable.
2428 */
2429 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 return FAIL;
2432
2433 /*
2434 * Check for the ":".
2435 */
2436 if ((*arg)[0] != ':')
2437 {
2438 EMSG(_("E109: Missing ':' after '?'"));
2439 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 return FAIL;
2442 }
2443
2444 /*
2445 * Get the third variable.
2446 */
2447 *arg = skipwhite(*arg + 1);
2448 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2449 {
2450 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return FAIL;
2453 }
2454 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457
2458 return OK;
2459}
2460
2461/*
2462 * Handle first level expression:
2463 * expr2 || expr2 || expr2 logical OR
2464 *
2465 * "arg" must point to the first non-white of the expression.
2466 * "arg" is advanced to the next non-white after the recognized expression.
2467 *
2468 * Return OK or FAIL.
2469 */
2470 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 int evaluate;
2475{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002476 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 long result;
2478 int first;
2479
2480 /*
2481 * Get the first variable.
2482 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 return FAIL;
2485
2486 /*
2487 * Repeat until there is no following "||".
2488 */
2489 first = TRUE;
2490 result = FALSE;
2491 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2492 {
2493 if (evaluate && first)
2494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 first = FALSE;
2499 }
2500
2501 /*
2502 * Get the second variable.
2503 */
2504 *arg = skipwhite(*arg + 2);
2505 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2506 return FAIL;
2507
2508 /*
2509 * Compute the result.
2510 */
2511 if (evaluate && !result)
2512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 if (evaluate)
2518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 rettv->v_type = VAR_NUMBER;
2520 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
2522 }
2523
2524 return OK;
2525}
2526
2527/*
2528 * Handle second level expression:
2529 * expr3 && expr3 && expr3 logical AND
2530 *
2531 * "arg" must point to the first non-white of the expression.
2532 * "arg" is advanced to the next non-white after the recognized expression.
2533 *
2534 * Return OK or FAIL.
2535 */
2536 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int evaluate;
2541{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002542 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 long result;
2544 int first;
2545
2546 /*
2547 * Get the first variable.
2548 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return FAIL;
2551
2552 /*
2553 * Repeat until there is no following "&&".
2554 */
2555 first = TRUE;
2556 result = TRUE;
2557 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2558 {
2559 if (evaluate && first)
2560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 first = FALSE;
2565 }
2566
2567 /*
2568 * Get the second variable.
2569 */
2570 *arg = skipwhite(*arg + 2);
2571 if (eval4(arg, &var2, evaluate && result) == FAIL)
2572 return FAIL;
2573
2574 /*
2575 * Compute the result.
2576 */
2577 if (evaluate && result)
2578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 if (evaluate)
2584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 rettv->v_type = VAR_NUMBER;
2586 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 }
2589
2590 return OK;
2591}
2592
2593/*
2594 * Handle third level expression:
2595 * var1 == var2
2596 * var1 =~ var2
2597 * var1 != var2
2598 * var1 !~ var2
2599 * var1 > var2
2600 * var1 >= var2
2601 * var1 < var2
2602 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002603 * var1 is var2
2604 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 *
2606 * "arg" must point to the first non-white of the expression.
2607 * "arg" is advanced to the next non-white after the recognized expression.
2608 *
2609 * Return OK or FAIL.
2610 */
2611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 int evaluate;
2616{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002617 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 char_u *p;
2619 int i;
2620 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002621 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 int len = 2;
2623 long n1, n2;
2624 char_u *s1, *s2;
2625 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2626 regmatch_T regmatch;
2627 int ic;
2628 char_u *save_cpo;
2629
2630 /*
2631 * Get the first variable.
2632 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 return FAIL;
2635
2636 p = *arg;
2637 switch (p[0])
2638 {
2639 case '=': if (p[1] == '=')
2640 type = TYPE_EQUAL;
2641 else if (p[1] == '~')
2642 type = TYPE_MATCH;
2643 break;
2644 case '!': if (p[1] == '=')
2645 type = TYPE_NEQUAL;
2646 else if (p[1] == '~')
2647 type = TYPE_NOMATCH;
2648 break;
2649 case '>': if (p[1] != '=')
2650 {
2651 type = TYPE_GREATER;
2652 len = 1;
2653 }
2654 else
2655 type = TYPE_GEQUAL;
2656 break;
2657 case '<': if (p[1] != '=')
2658 {
2659 type = TYPE_SMALLER;
2660 len = 1;
2661 }
2662 else
2663 type = TYPE_SEQUAL;
2664 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002665 case 'i': if (p[1] == 's')
2666 {
2667 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2668 len = 5;
2669 if (!vim_isIDc(p[len]))
2670 {
2671 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2672 type_is = TRUE;
2673 }
2674 }
2675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677
2678 /*
2679 * If there is a comparitive operator, use it.
2680 */
2681 if (type != TYPE_UNKNOWN)
2682 {
2683 /* extra question mark appended: ignore case */
2684 if (p[len] == '?')
2685 {
2686 ic = TRUE;
2687 ++len;
2688 }
2689 /* extra '#' appended: match case */
2690 else if (p[len] == '#')
2691 {
2692 ic = FALSE;
2693 ++len;
2694 }
2695 /* nothing appened: use 'ignorecase' */
2696 else
2697 ic = p_ic;
2698
2699 /*
2700 * Get the second variable.
2701 */
2702 *arg = skipwhite(p + len);
2703 if (eval5(arg, &var2, evaluate) == FAIL)
2704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
2707 }
2708
2709 if (evaluate)
2710 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002711 if (type_is && rettv->v_type != var2.v_type)
2712 {
2713 /* For "is" a different type always means FALSE, for "notis"
2714 * it means TRUE. */
2715 n1 = (type == TYPE_NEQUAL);
2716 }
2717 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
2718 {
2719 if (type_is)
2720 {
2721 n1 = (rettv->v_type == var2.v_type
2722 && rettv->vval.v_list == var2.vval.v_list);
2723 if (type == TYPE_NEQUAL)
2724 n1 = !n1;
2725 }
2726 else if (rettv->v_type != var2.v_type
2727 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2728 {
2729 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002730 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002731 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002732 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002733 clear_tv(rettv);
2734 clear_tv(&var2);
2735 return FAIL;
2736 }
2737 else
2738 {
2739 /* Compare two Lists for being equal or unequal. */
2740 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
2741 if (type == TYPE_NEQUAL)
2742 n1 = !n1;
2743 }
2744 }
2745
2746 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
2747 {
2748 if (rettv->v_type != var2.v_type
2749 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2750 {
2751 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002752 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002753 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002754 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002755 clear_tv(rettv);
2756 clear_tv(&var2);
2757 return FAIL;
2758 }
2759 else
2760 {
2761 /* Compare two Funcrefs for being equal or unequal. */
2762 if (rettv->vval.v_string == NULL
2763 || var2.vval.v_string == NULL)
2764 n1 = FALSE;
2765 else
2766 n1 = STRCMP(rettv->vval.v_string,
2767 var2.vval.v_string) == 0;
2768 if (type == TYPE_NEQUAL)
2769 n1 = !n1;
2770 }
2771 }
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 /*
2774 * If one of the two variables is a number, compare as a number.
2775 * When using "=~" or "!~", always compare as string.
2776 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002777 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002780 n1 = get_tv_number(rettv);
2781 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 switch (type)
2783 {
2784 case TYPE_EQUAL: n1 = (n1 == n2); break;
2785 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2786 case TYPE_GREATER: n1 = (n1 > n2); break;
2787 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2788 case TYPE_SMALLER: n1 = (n1 < n2); break;
2789 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2790 case TYPE_UNKNOWN:
2791 case TYPE_MATCH:
2792 case TYPE_NOMATCH: break; /* avoid gcc warning */
2793 }
2794 }
2795 else
2796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002797 s1 = get_tv_string_buf(rettv, buf1);
2798 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2800 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2801 else
2802 i = 0;
2803 n1 = FALSE;
2804 switch (type)
2805 {
2806 case TYPE_EQUAL: n1 = (i == 0); break;
2807 case TYPE_NEQUAL: n1 = (i != 0); break;
2808 case TYPE_GREATER: n1 = (i > 0); break;
2809 case TYPE_GEQUAL: n1 = (i >= 0); break;
2810 case TYPE_SMALLER: n1 = (i < 0); break;
2811 case TYPE_SEQUAL: n1 = (i <= 0); break;
2812
2813 case TYPE_MATCH:
2814 case TYPE_NOMATCH:
2815 /* avoid 'l' flag in 'cpoptions' */
2816 save_cpo = p_cpo;
2817 p_cpo = (char_u *)"";
2818 regmatch.regprog = vim_regcomp(s2,
2819 RE_MAGIC + RE_STRING);
2820 regmatch.rm_ic = ic;
2821 if (regmatch.regprog != NULL)
2822 {
2823 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2824 vim_free(regmatch.regprog);
2825 if (type == TYPE_NOMATCH)
2826 n1 = !n1;
2827 }
2828 p_cpo = save_cpo;
2829 break;
2830
2831 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2832 }
2833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002834 clear_tv(rettv);
2835 clear_tv(&var2);
2836 rettv->v_type = VAR_NUMBER;
2837 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 }
2840
2841 return OK;
2842}
2843
2844/*
2845 * Handle fourth level expression:
2846 * + number addition
2847 * - number subtraction
2848 * . string concatenation
2849 *
2850 * "arg" must point to the first non-white of the expression.
2851 * "arg" is advanced to the next non-white after the recognized expression.
2852 *
2853 * Return OK or FAIL.
2854 */
2855 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 int evaluate;
2860{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002861 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002862 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 int op;
2864 long n1, n2;
2865 char_u *s1, *s2;
2866 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2867 char_u *p;
2868
2869 /*
2870 * Get the first variable.
2871 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 return FAIL;
2874
2875 /*
2876 * Repeat computing, until no '+', '-' or '.' is following.
2877 */
2878 for (;;)
2879 {
2880 op = **arg;
2881 if (op != '+' && op != '-' && op != '.')
2882 break;
2883
2884 /*
2885 * Get the second variable.
2886 */
2887 *arg = skipwhite(*arg + 1);
2888 if (eval6(arg, &var2, evaluate) == FAIL)
2889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 return FAIL;
2892 }
2893
2894 if (evaluate)
2895 {
2896 /*
2897 * Compute the result.
2898 */
2899 if (op == '.')
2900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 s1 = get_tv_string_buf(rettv, buf1);
2902 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 op = (int)STRLEN(s1);
2904 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2905 if (p != NULL)
2906 {
2907 STRCPY(p, s1);
2908 STRCPY(p + op, s2);
2909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 clear_tv(rettv);
2911 rettv->v_type = VAR_STRING;
2912 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002914 else if (rettv->v_type == VAR_LIST && var2.v_type == VAR_LIST)
2915 {
2916 /* concatenate Lists */
2917 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2918 &var3) == FAIL)
2919 {
2920 clear_tv(rettv);
2921 clear_tv(&var2);
2922 return FAIL;
2923 }
2924 clear_tv(rettv);
2925 *rettv = var3;
2926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else
2928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 n1 = get_tv_number(rettv);
2930 n2 = get_tv_number(&var2);
2931 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (op == '+')
2933 n1 = n1 + n2;
2934 else
2935 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 rettv->v_type = VAR_NUMBER;
2937 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 }
2942 return OK;
2943}
2944
2945/*
2946 * Handle fifth level expression:
2947 * * number multiplication
2948 * / number division
2949 * % number modulo
2950 *
2951 * "arg" must point to the first non-white of the expression.
2952 * "arg" is advanced to the next non-white after the recognized expression.
2953 *
2954 * Return OK or FAIL.
2955 */
2956 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int evaluate;
2961{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002962 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 int op;
2964 long n1, n2;
2965
2966 /*
2967 * Get the first variable.
2968 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 return FAIL;
2971
2972 /*
2973 * Repeat computing, until no '*', '/' or '%' is following.
2974 */
2975 for (;;)
2976 {
2977 op = **arg;
2978 if (op != '*' && op != '/' && op != '%')
2979 break;
2980
2981 if (evaluate)
2982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 n1 = get_tv_number(rettv);
2984 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
2986 else
2987 n1 = 0;
2988
2989 /*
2990 * Get the second variable.
2991 */
2992 *arg = skipwhite(*arg + 1);
2993 if (eval7(arg, &var2, evaluate) == FAIL)
2994 return FAIL;
2995
2996 if (evaluate)
2997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998 n2 = get_tv_number(&var2);
2999 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 /*
3002 * Compute the result.
3003 */
3004 if (op == '*')
3005 n1 = n1 * n2;
3006 else if (op == '/')
3007 {
3008 if (n2 == 0) /* give an error message? */
3009 n1 = 0x7fffffffL;
3010 else
3011 n1 = n1 / n2;
3012 }
3013 else
3014 {
3015 if (n2 == 0) /* give an error message? */
3016 n1 = 0;
3017 else
3018 n1 = n1 % n2;
3019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003020 rettv->v_type = VAR_NUMBER;
3021 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
3023 }
3024
3025 return OK;
3026}
3027
3028/*
3029 * Handle sixth level expression:
3030 * number number constant
3031 * "string" string contstant
3032 * 'string' literal string contstant
3033 * &option-name option value
3034 * @r register contents
3035 * identifier variable value
3036 * function() function call
3037 * $VAR environment variable
3038 * (expression) nested expression
3039 *
3040 * Also handle:
3041 * ! in front logical NOT
3042 * - in front unary minus
3043 * + in front unary plus (ignored)
3044 * trailing [] subscript in String
3045 *
3046 * "arg" must point to the first non-white of the expression.
3047 * "arg" is advanced to the next non-white after the recognized expression.
3048 *
3049 * Return OK or FAIL.
3050 */
3051 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 int evaluate;
3056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 long n;
3058 int len;
3059 char_u *s;
3060 int val;
3061 char_u *start_leader, *end_leader;
3062 int ret = OK;
3063 char_u *alias;
3064
3065 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003067 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071 /*
3072 * Skip '!' and '-' characters. They are handled later.
3073 */
3074 start_leader = *arg;
3075 while (**arg == '!' || **arg == '-' || **arg == '+')
3076 *arg = skipwhite(*arg + 1);
3077 end_leader = *arg;
3078
3079 switch (**arg)
3080 {
3081 /*
3082 * Number constant.
3083 */
3084 case '0':
3085 case '1':
3086 case '2':
3087 case '3':
3088 case '4':
3089 case '5':
3090 case '6':
3091 case '7':
3092 case '8':
3093 case '9':
3094 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3095 *arg += len;
3096 if (evaluate)
3097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 rettv->v_type = VAR_NUMBER;
3099 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 break;
3102
3103 /*
3104 * String constant: "string".
3105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 break;
3108
3109 /*
3110 * Literal string constant: 'string'.
3111 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003112 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003113 break;
3114
3115 /*
3116 * List: [expr, expr]
3117 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003118 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 break;
3120
3121 /*
3122 * Option value: &name
3123 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003124 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 break;
3126
3127 /*
3128 * Environment variable: $VAR.
3129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003130 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 break;
3132
3133 /*
3134 * Register contents: @r.
3135 */
3136 case '@': ++*arg;
3137 if (evaluate)
3138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003139 rettv->v_type = VAR_STRING;
3140 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
3142 if (**arg != NUL)
3143 ++*arg;
3144 break;
3145
3146 /*
3147 * nested expression: (expression).
3148 */
3149 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003150 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (**arg == ')')
3152 ++*arg;
3153 else if (ret == OK)
3154 {
3155 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003156 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 ret = FAIL;
3158 }
3159 break;
3160
3161 /*
3162 * Must be a variable or function name then.
3163 */
3164 default: s = *arg;
3165 len = get_func_len(arg, &alias, evaluate);
3166 if (alias != NULL)
3167 s = alias;
3168
3169 if (len == 0)
3170 ret = FAIL;
3171 else
3172 {
3173 if (**arg == '(') /* recursive! */
3174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003175 /* If "s" is the name of a variable of type VAR_FUNC
3176 * use its contents. */
3177 s = deref_func_name(s, &len);
3178
3179 /* Invoke the function. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 ret = get_func_tv(s, len, rettv, arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3182 &len, evaluate);
3183 /* Stop the expression evaluation when immediately
3184 * aborting on error, or when an interrupt occurred or
3185 * an exception was thrown but not caught. */
3186 if (aborting())
3187 {
3188 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 ret = FAIL;
3191 }
3192 }
3193 else if (evaluate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 ret = get_var_tv(s, len, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196
3197 if (alias != NULL)
3198 vim_free(alias);
3199
3200 break;
3201 }
3202 *arg = skipwhite(*arg);
3203
3204 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003205 * Handle expr[expr] and expr[expr:expr] subscript.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003207 while (**arg == '[' && ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003209 if (eval_index(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 return FAIL;
3213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
3215
3216 /*
3217 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3218 */
3219 if (ret == OK && evaluate && end_leader > start_leader)
3220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003221 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 while (end_leader > start_leader)
3223 {
3224 --end_leader;
3225 if (*end_leader == '!')
3226 val = !val;
3227 else if (*end_leader == '-')
3228 val = -val;
3229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003230 clear_tv(rettv);
3231 rettv->v_type = VAR_NUMBER;
3232 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234
3235 return ret;
3236}
3237
3238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003239 * Evaluate an "[expr]" or "[expr:expr]" index.
3240 * "*arg" points to the '['.
3241 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3242 */
3243 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003244eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003245 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003246 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003247 int evaluate;
3248{
3249 int empty1 = FALSE, empty2 = FALSE;
3250 typeval var1, var2;
3251 long n1, n2 = 0;
3252 long len;
3253 int range;
3254 char_u *s;
3255
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003256 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003258 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003259 return FAIL;
3260 }
3261
3262 /*
3263 * Get the (first) variable from inside the [].
3264 */
3265 *arg = skipwhite(*arg + 1);
3266 if (**arg == ':')
3267 empty1 = TRUE;
3268 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3269 return FAIL;
3270
3271 /*
3272 * Get the second variable from inside the [:].
3273 */
3274 if (**arg == ':')
3275 {
3276 range = TRUE;
3277 *arg = skipwhite(*arg + 1);
3278 if (**arg == ']')
3279 empty2 = TRUE;
3280 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003283 return FAIL;
3284 }
3285 }
3286 else
3287 range = FALSE;
3288
3289 /* Check for the ']'. */
3290 if (**arg != ']')
3291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003292 EMSG(_(e_missbrac));
3293 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003294 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003295 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003296 return FAIL;
3297 }
3298
3299 if (evaluate)
3300 {
3301 if (empty1)
3302 n1 = 0;
3303 else
3304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003305 n1 = get_tv_number(&var1);
3306 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003307 }
3308 if (range)
3309 {
3310 if (empty2)
3311 n2 = -1;
3312 else
3313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003314 n2 = get_tv_number(&var2);
3315 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003316 }
3317 }
3318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003319 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003320 {
3321 case VAR_NUMBER:
3322 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003323 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003324 len = (long)STRLEN(s);
3325 if (range)
3326 {
3327 /* The resulting variable is a substring. If the indexes
3328 * are out of range the result is empty. */
3329 if (n1 < 0)
3330 {
3331 n1 = len + n1;
3332 if (n1 < 0)
3333 n1 = 0;
3334 }
3335 if (n2 < 0)
3336 n2 = len + n2;
3337 else if (n2 >= len)
3338 n2 = len;
3339 if (n1 >= len || n2 < 0 || n1 > n2)
3340 s = NULL;
3341 else
3342 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3343 }
3344 else
3345 {
3346 /* The resulting variable is a string of a single
3347 * character. If the index is too big or negative the
3348 * result is empty. */
3349 if (n1 >= len || n1 < 0)
3350 s = NULL;
3351 else
3352 s = vim_strnsave(s + n1, 1);
3353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 clear_tv(rettv);
3355 rettv->v_type = VAR_STRING;
3356 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003357 break;
3358
3359 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003361 if (n1 < 0)
3362 n1 = len + n1;
3363 if (!empty1 && (n1 < 0 || n1 >= len))
3364 {
3365 EMSGN(_(e_listidx), n1);
3366 return FAIL;
3367 }
3368 if (range)
3369 {
3370 listvar *l;
3371 listitem *item;
3372
3373 if (n2 < 0)
3374 n2 = len + n2;
3375 if (!empty2 && (n2 < 0 || n2 >= len || n2 < n1))
3376 {
3377 EMSGN(_(e_listidx), n2);
3378 return FAIL;
3379 }
3380 l = list_alloc();
3381 if (l == NULL)
3382 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003384 n1 <= n2; ++n1)
3385 {
3386 if (list_append_tv(l, &item->li_tv) == FAIL)
3387 {
3388 list_free(l);
3389 return FAIL;
3390 }
3391 item = item->li_next;
3392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 clear_tv(rettv);
3394 rettv->v_type = VAR_LIST;
3395 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003396 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003397 }
3398 else
3399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003400 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003401 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 clear_tv(rettv);
3403 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003404 }
3405 break;
3406 }
3407 }
3408
3409 *arg = skipwhite(*arg + 1); /* skip the ']' */
3410 return OK;
3411}
3412
3413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Get an option value.
3415 * "arg" points to the '&' or '+' before the option name.
3416 * "arg" is advanced to character after the option name.
3417 * Return OK or FAIL.
3418 */
3419 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003420get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003422 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 int evaluate;
3424{
3425 char_u *option_end;
3426 long numval;
3427 char_u *stringval;
3428 int opt_type;
3429 int c;
3430 int working = (**arg == '+'); /* has("+option") */
3431 int ret = OK;
3432 int opt_flags;
3433
3434 /*
3435 * Isolate the option name and find its value.
3436 */
3437 option_end = find_option_end(arg, &opt_flags);
3438 if (option_end == NULL)
3439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 EMSG2(_("E112: Option name missing: %s"), *arg);
3442 return FAIL;
3443 }
3444
3445 if (!evaluate)
3446 {
3447 *arg = option_end;
3448 return OK;
3449 }
3450
3451 c = *option_end;
3452 *option_end = NUL;
3453 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003454 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 if (opt_type == -3) /* invalid name */
3457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003458 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 EMSG2(_("E113: Unknown option: %s"), *arg);
3460 ret = FAIL;
3461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 if (opt_type == -2) /* hidden string option */
3465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 rettv->v_type = VAR_STRING;
3467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 else if (opt_type == -1) /* hidden number option */
3470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 rettv->v_type = VAR_NUMBER;
3472 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 else if (opt_type == 1) /* number option */
3475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 rettv->v_type = VAR_NUMBER;
3477 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 else /* string option */
3480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 rettv->v_type = VAR_STRING;
3482 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484 }
3485 else if (working && (opt_type == -2 || opt_type == -1))
3486 ret = FAIL;
3487
3488 *option_end = c; /* put back for error messages */
3489 *arg = option_end;
3490
3491 return ret;
3492}
3493
3494/*
3495 * Allocate a variable for a string constant.
3496 * Return OK or FAIL.
3497 */
3498 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 int evaluate;
3503{
3504 char_u *p;
3505 char_u *name;
3506 int i;
3507 int extra = 0;
3508
3509 /*
3510 * Find the end of the string, skipping backslashed characters.
3511 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003512 for (p = *arg + 1; *p && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 if (*p == '\\' && p[1] != NUL)
3515 {
3516 ++p;
3517 /* A "\<x>" form occupies at least 4 characters, and produces up
3518 * to 6 characters: reserve space for 2 extra */
3519 if (*p == '<')
3520 extra += 2;
3521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523
3524 if (*p != '"')
3525 {
3526 EMSG2(_("E114: Missing quote: %s"), *arg);
3527 return FAIL;
3528 }
3529
3530 /* If only parsing, set *arg and return here */
3531 if (!evaluate)
3532 {
3533 *arg = p + 1;
3534 return OK;
3535 }
3536
3537 /*
3538 * Copy the string into allocated memory, handling backslashed
3539 * characters.
3540 */
3541 name = alloc((unsigned)(p - *arg + extra));
3542 if (name == NULL)
3543 return FAIL;
3544
3545 i = 0;
3546 for (p = *arg + 1; *p && *p != '"'; ++p)
3547 {
3548 if (*p == '\\')
3549 {
3550 switch (*++p)
3551 {
3552 case 'b': name[i++] = BS; break;
3553 case 'e': name[i++] = ESC; break;
3554 case 'f': name[i++] = FF; break;
3555 case 'n': name[i++] = NL; break;
3556 case 'r': name[i++] = CAR; break;
3557 case 't': name[i++] = TAB; break;
3558
3559 case 'X': /* hex: "\x1", "\x12" */
3560 case 'x':
3561 case 'u': /* Unicode: "\u0023" */
3562 case 'U':
3563 if (vim_isxdigit(p[1]))
3564 {
3565 int n, nr;
3566 int c = toupper(*p);
3567
3568 if (c == 'X')
3569 n = 2;
3570 else
3571 n = 4;
3572 nr = 0;
3573 while (--n >= 0 && vim_isxdigit(p[1]))
3574 {
3575 ++p;
3576 nr = (nr << 4) + hex2nr(*p);
3577 }
3578#ifdef FEAT_MBYTE
3579 /* For "\u" store the number according to
3580 * 'encoding'. */
3581 if (c != 'X')
3582 i += (*mb_char2bytes)(nr, name + i);
3583 else
3584#endif
3585 name[i++] = nr;
3586 }
3587 else
3588 name[i++] = *p;
3589 break;
3590
3591 /* octal: "\1", "\12", "\123" */
3592 case '0':
3593 case '1':
3594 case '2':
3595 case '3':
3596 case '4':
3597 case '5':
3598 case '6':
3599 case '7': name[i] = *p - '0';
3600 if (p[1] >= '0' && p[1] <= '7')
3601 {
3602 ++p;
3603 name[i] = (name[i] << 3) + *p - '0';
3604 if (p[1] >= '0' && p[1] <= '7')
3605 {
3606 ++p;
3607 name[i] = (name[i] << 3) + *p - '0';
3608 }
3609 }
3610 ++i;
3611 break;
3612
3613 /* Special key, e.g.: "\<C-W>" */
3614 case '<': extra = trans_special(&p, name + i, TRUE);
3615 if (extra != 0)
3616 {
3617 i += extra;
3618 --p;
3619 break;
3620 }
3621 /* FALLTHROUGH */
3622
3623 default: name[i++] = *p;
3624 break;
3625 }
3626 }
3627 else
3628 name[i++] = *p;
3629
3630#ifdef FEAT_MBYTE
3631 /* For a multi-byte character copy the bytes after the first one. */
3632 if (has_mbyte)
3633 {
3634 int l = (*mb_ptr2len_check)(p);
3635
3636 while (--l > 0)
3637 name[i++] = *++p;
3638 }
3639#endif
3640 }
3641 name[i] = NUL;
3642 *arg = p + 1;
3643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003644 rettv->v_type = VAR_STRING;
3645 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 return OK;
3648}
3649
3650/*
3651 * Allocate a variable for an backtick-string constant.
3652 * Return OK or FAIL.
3653 */
3654 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 int evaluate;
3659{
3660 char_u *p;
3661 char_u *name;
3662
3663 /*
3664 * Find the end of the string.
3665 */
3666 p = vim_strchr(*arg + 1, '\'');
3667 if (p == NULL)
3668 {
3669 EMSG2(_("E115: Missing quote: %s"), *arg);
3670 return FAIL;
3671 }
3672
3673 if (evaluate)
3674 {
3675 /*
3676 * Copy the string into allocated memory.
3677 */
3678 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
3679 if (name == NULL)
3680 return FAIL;
3681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 rettv->v_type = VAR_STRING;
3683 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685
3686 *arg = p + 1;
3687
3688 return OK;
3689}
3690
3691/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003692 * Allocate a variable for a List and fill it from "*arg".
3693 * Return OK or FAIL.
3694 */
3695 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003696get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003697 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003698 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003699 int evaluate;
3700{
3701 listvar *l = NULL;
3702 typeval tv;
3703 listitem *item;
3704
3705 if (evaluate)
3706 {
3707 l = list_alloc();
3708 if (l == NULL)
3709 return FAIL;
3710 }
3711
3712 *arg = skipwhite(*arg + 1);
3713 while (**arg != ']' && **arg != NUL)
3714 {
3715 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
3716 goto failret;
3717 if (evaluate)
3718 {
3719 item = listitem_alloc();
3720 if (item != NULL)
3721 {
3722 item->li_tv = tv;
3723 list_append(l, item);
3724 }
3725 }
3726
3727 if (**arg == ']')
3728 break;
3729 if (**arg != ',')
3730 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003731 EMSG2(_("E696: Missing comma in list: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003732 goto failret;
3733 }
3734 *arg = skipwhite(*arg + 1);
3735 }
3736
3737 if (**arg != ']')
3738 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003739 EMSG2(_("E697: Missing end of list ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003740failret:
3741 if (evaluate)
3742 list_free(l);
3743 return FAIL;
3744 }
3745
3746 *arg = skipwhite(*arg + 1);
3747 if (evaluate)
3748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003749 rettv->v_type = VAR_LIST;
3750 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003751 ++l->lv_refcount;
3752 }
3753
3754 return OK;
3755}
3756
3757/*
3758 * Allocate an empty header for a list.
3759 */
3760 static listvar *
3761list_alloc()
3762{
3763 return (listvar *)alloc_clear(sizeof(listvar));
3764}
3765
3766/*
3767 * Unreference a list: decrement the reference count and free it when it
3768 * becomes zero.
3769 */
3770 static void
3771list_unref(l)
3772 listvar *l;
3773{
3774 if (l != NULL && --l->lv_refcount <= 0)
3775 list_free(l);
3776}
3777
3778/*
3779 * Free a list, including all items it points to.
3780 * Ignores the reference count.
3781 */
3782 static void
3783list_free(l)
3784 listvar *l;
3785{
3786 listitem *item;
3787 listitem *next;
3788
3789 for (item = l->lv_first; item != NULL; item = next)
3790 {
3791 next = item->li_next;
3792 listitem_free(item);
3793 }
3794 vim_free(l);
3795}
3796
3797/*
3798 * Allocate a list item.
3799 */
3800 static listitem *
3801listitem_alloc()
3802{
3803 return (listitem *)alloc(sizeof(listitem));
3804}
3805
3806/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003807 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003808 */
3809 static void
3810listitem_free(item)
3811 listitem *item;
3812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003814 vim_free(item);
3815}
3816
3817/*
3818 * Get the number of items in a list.
3819 */
3820 static long
3821list_len(l)
3822 listvar *l;
3823{
3824 listitem *item;
3825 long len = 0;
3826
3827 if (l == NULL)
3828 return 0L;
3829 for (item = l->lv_first; item != NULL; item = item->li_next)
3830 ++len;
3831 return len;
3832}
3833
3834/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003835 * Return TRUE when two lists have exactly the same values.
3836 */
3837 static int
3838list_equal(l1, l2, ic)
3839 listvar *l1;
3840 listvar *l2;
3841 int ic; /* ignore case for strings */
3842{
3843 listitem *item1, *item2;
3844
3845 for (item1 = l1->lv_first, item2 = l2->lv_first;
3846 item1 != NULL && item2 != NULL;
3847 item1 = item1->li_next, item2 = item2->li_next)
3848 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
3849 return FALSE;
3850 return item1 == NULL && item2 == NULL;
3851}
3852
3853/*
3854 * Return TRUE if "tv1" and "tv2" have the same value.
3855 * Compares the items just like "==" would compare them.
3856 */
3857 static int
3858tv_equal(tv1, tv2, ic)
3859 typeval *tv1;
3860 typeval *tv2;
3861 int ic; /* ignore case */
3862{
3863 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3864
3865 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
3866 {
3867 /* recursive! */
3868 if (tv1->v_type != tv2->v_type
3869 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
3870 return FALSE;
3871 }
3872 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
3873 {
3874 if (tv1->v_type != tv2->v_type
3875 || tv1->vval.v_string == NULL
3876 || tv2->vval.v_string == NULL
3877 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
3878 return FALSE;
3879 }
3880 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
3881 {
3882 if (get_tv_number(tv1) != get_tv_number(tv2))
3883 return FALSE;
3884 }
3885 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
3886 get_tv_string_buf(tv2, buf2)) != 0)
3887 return FALSE;
3888 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
3889 get_tv_string_buf(tv2, buf2)) != 0)
3890 return FALSE;
3891 return TRUE;
3892}
3893
3894/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003895 * Locate item with index "n" in list "l" and return it.
3896 * A negative index is counted from the end; -1 is the last item.
3897 * Returns NULL when "n" is out of range.
3898 */
3899 static listitem *
3900list_find(l, n)
3901 listvar *l;
3902 long n;
3903{
3904 listitem *item;
3905 long idx;
3906
3907 if (l == NULL)
3908 return NULL;
3909 if (n < 0)
3910 {
3911 idx = -1; /* search from the end */
3912 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
3913 --idx;
3914 }
3915 else
3916 {
3917 idx = 0; /* search from the start */
3918 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
3919 ++idx;
3920 }
3921 if (idx != n)
3922 return NULL;
3923 return item;
3924}
3925
3926/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003927 * Locate "item" list "l" and return its index.
3928 * Returns -1 when "item" is not in the list.
3929 */
3930 static long
3931list_idx_of_item(l, item)
3932 listvar *l;
3933 listitem *item;
3934{
3935 long idx = 0;
3936 listitem *li;
3937
3938 if (l == NULL)
3939 return -1;
3940 idx = 0;
3941 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
3942 ++idx;
3943 if (li == NULL)
3944 return -1;
3945 return idx;;
3946}
3947
3948/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003949 * Like list_find(), but also find an item just past the end.
3950 * "*ip" is the item to find.
3951 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
3952 * Returns NULL when item not found or item is just past the end.
3953 */
3954 static listitem *
3955list_find_ext(l, ip)
3956 listvar *l;
3957 long *ip;
3958{
3959 long n;
3960 listitem *item;
3961
3962 if (*ip < 0)
3963 {
3964 /* Count from the end: -1 is before last item. */
3965 item = l->lv_last;
3966 for (n = *ip + 1; n < 0 && item != NULL; ++n)
3967 item = item->li_prev;
3968 if (item == NULL)
3969 n = 1; /* error! */
3970 }
3971 else
3972 {
3973 item = l->lv_first;
3974 for (n = *ip; n > 0 && item != NULL; --n)
3975 item = item->li_next;
3976 }
3977 *ip = n;
3978 return item;
3979}
3980
3981/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982 * Append item "item" to the end of list "l".
3983 */
3984 static void
3985list_append(l, item)
3986 listvar *l;
3987 listitem *item;
3988{
3989 if (l->lv_last == NULL)
3990 {
3991 /* empty list */
3992 l->lv_first = item;
3993 l->lv_last = item;
3994 item->li_prev = NULL;
3995 }
3996 else
3997 {
3998 l->lv_last->li_next = item;
3999 item->li_prev = l->lv_last;
4000 l->lv_last = item;
4001 }
4002 item->li_next = NULL;
4003}
4004
4005/*
4006 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004007 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 */
4009 static int
4010list_append_tv(l, tv)
4011 listvar *l;
4012 typeval *tv;
4013{
4014 listitem *ni = listitem_alloc();
4015
4016 if (ni == NULL)
4017 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004019 list_append(l, ni);
4020 return OK;
4021}
4022
4023/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004024 * Insert typeval "tv" in list "l" before "item".
4025 * If "item" is NULL append at the end.
4026 * Return FAIL when out of memory.
4027 */
4028 static int
4029list_insert_tv(l, tv, item)
4030 listvar *l;
4031 typeval *tv;
4032 listitem *item;
4033{
4034 listitem *ni = listitem_alloc();
4035
4036 if (ni == NULL)
4037 return FAIL;
4038 copy_tv(tv, &ni->li_tv);
4039 if (item == NULL)
4040 /* Append new item at end of list. */
4041 list_append(l, ni);
4042 else
4043 {
4044 /* Insert new item before existing item. */
4045 ni->li_prev = item->li_prev;
4046 ni->li_next = item;
4047 if (item->li_prev == NULL)
4048 l->lv_first = ni;
4049 else
4050 item->li_prev->li_next = ni;
4051 item->li_prev = ni;
4052 }
4053 return OK;
4054}
4055
4056/*
4057 * Extend "l1" with "l2".
4058 * If "bef" is NULL append at the end, otherwise insert before this item.
4059 * Returns FAIL when out of memory.
4060 */
4061 static int
4062list_extend(l1, l2, bef)
4063 listvar *l1;
4064 listvar *l2;
4065 listitem *bef;
4066{
4067 listitem *item;
4068
4069 for (item = l2->lv_first; item != NULL; item = item->li_next)
4070 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4071 return FAIL;
4072 return OK;
4073}
4074
4075/*
4076 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4077 * Return FAIL when out of memory.
4078 */
4079 static int
4080list_concat(l1, l2, tv)
4081 listvar *l1;
4082 listvar *l2;
4083 typeval *tv;
4084{
4085 listvar *l;
4086
4087 /* make a copy of the first list. */
4088 l = list_copy(l1, FALSE);
4089 if (l == NULL)
4090 return FAIL;
4091 tv->v_type = VAR_LIST;
4092 tv->vval.v_list = l;
4093
4094 /* append all items from the second list */
4095 return list_extend(l, l2, NULL);
4096}
4097
4098/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004099 * Make a copy of list "l". Shallow if "deep" is FALSE.
4100 * The refcount of the new list is set to 1.
4101 * Returns NULL when out of memory.
4102 */
4103 static listvar *
4104list_copy(orig, deep)
4105 listvar *orig;
4106 int deep;
4107{
4108 listvar *copy;
4109 listitem *item;
4110 listitem *ni;
4111 static int recurse = 0;
4112
4113 if (orig == NULL)
4114 return NULL;
4115 if (recurse >= VAR_LIST_MAXNEST)
4116 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004117 EMSG(_("E698: List nested too deep for making a copy"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118 return NULL;
4119 }
4120 ++recurse;
4121
4122 copy = list_alloc();
4123 if (copy != NULL)
4124 {
4125 for (item = orig->lv_first; item != NULL; item = item->li_next)
4126 {
4127 ni = listitem_alloc();
4128 if (ni == NULL)
4129 break;
4130 if (deep && item->li_tv.v_type == VAR_LIST)
4131 {
4132 ni->li_tv.v_type = VAR_LIST;
4133 ni->li_tv.vval.v_list = list_copy(item->li_tv.vval.v_list,
4134 TRUE);
4135 if (ni->li_tv.vval.v_list == NULL)
4136 {
4137 vim_free(ni);
4138 break;
4139 }
4140 }
4141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004143 list_append(copy, ni);
4144 }
4145 ++copy->lv_refcount;
4146 }
4147
4148 --recurse;
4149 return copy;
4150}
4151
4152/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004153 * Remove items "item" to "item2" from list "l".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004154 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004155 static void
4156list_getrem(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004157 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004158 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004159 listitem *item2;
4160{
4161 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004162
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004163 /* notify watchers */
4164 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004166 list_fix_watch(l, ip);
4167 if (ip == item2)
4168 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004169 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004170
4171 if (item2->li_next == NULL)
4172 l->lv_last = item->li_prev;
4173 else
4174 item2->li_next->li_prev = item->li_prev;
4175 if (item->li_prev == NULL)
4176 l->lv_first = item2->li_next;
4177 else
4178 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004179}
4180
4181/*
4182 * Return an allocated string with the string representation of a list.
4183 * May return NULL.
4184 */
4185 static char_u *
4186list2string(tv)
4187 typeval *tv;
4188{
4189 garray_T ga;
4190 listitem *item;
4191 int first = TRUE;
4192 char_u *tofree;
4193 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004194 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195
4196 if (tv->vval.v_list == NULL)
4197 return NULL;
4198 ga_init2(&ga, (int)sizeof(char), 80);
4199 ga_append(&ga, '[');
4200
4201 for (item = tv->vval.v_list->lv_first; item != NULL; item = item->li_next)
4202 {
4203 if (first)
4204 first = FALSE;
4205 else
4206 ga_concat(&ga, (char_u *)", ");
4207
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004208 s = tv2string(&item->li_tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 if (s != NULL)
4210 ga_concat(&ga, s);
4211 vim_free(tofree);
4212 }
4213
4214 ga_append(&ga, ']');
4215 ga_append(&ga, NUL);
4216 return (char_u *)ga.ga_data;
4217}
4218
4219/*
4220 * Return a string with the string representation of a variable.
4221 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 * "numbuf" is used for a number.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004223 * May return NULL;
4224 */
4225 static char_u *
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004226tv2string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004227 typeval *tv;
4228 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004229 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004230{
4231 switch (tv->v_type)
4232 {
4233 case VAR_FUNC:
4234 *tofree = NULL;
4235 return tv->vval.v_string;
4236 case VAR_LIST:
4237 *tofree = list2string(tv);
4238 return *tofree;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 case VAR_STRING:
4240 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 default:
4243 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244 }
4245 *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004246 return get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004247}
4248
4249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * Get the value of an environment variable.
4251 * "arg" is pointing to the '$'. It is advanced to after the name.
4252 * If the environment variable was not set, silently assume it is empty.
4253 * Always return OK.
4254 */
4255 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 int evaluate;
4260{
4261 char_u *string = NULL;
4262 int len;
4263 int cc;
4264 char_u *name;
4265
4266 ++*arg;
4267 name = *arg;
4268 len = get_env_len(arg);
4269 if (evaluate)
4270 {
4271 if (len != 0)
4272 {
4273 cc = name[len];
4274 name[len] = NUL;
4275 /* first try mch_getenv(), fast for normal environment vars */
4276 string = mch_getenv(name);
4277 if (string != NULL && *string != NUL)
4278 string = vim_strsave(string);
4279 else
4280 {
4281 /* next try expanding things like $VIM and ${HOME} */
4282 string = expand_env_save(name - 1);
4283 if (string != NULL && *string == '$')
4284 {
4285 vim_free(string);
4286 string = NULL;
4287 }
4288 }
4289 name[len] = cc;
4290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 rettv->v_type = VAR_STRING;
4292 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294
4295 return OK;
4296}
4297
4298/*
4299 * Array with names and number of arguments of all internal functions
4300 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
4301 */
4302static struct fst
4303{
4304 char *f_name; /* function name */
4305 char f_min_argc; /* minimal number of arguments */
4306 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004307 void (*f_func) __ARGS((typeval *args, typeval *rvar));
4308 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309} functions[] =
4310{
Bram Moolenaar0d660222005-01-07 21:51:51 +00004311 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {"append", 2, 2, f_append},
4313 {"argc", 0, 0, f_argc},
4314 {"argidx", 0, 0, f_argidx},
4315 {"argv", 1, 1, f_argv},
4316 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004317 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {"bufexists", 1, 1, f_bufexists},
4319 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
4320 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
4321 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
4322 {"buflisted", 1, 1, f_buflisted},
4323 {"bufloaded", 1, 1, f_bufloaded},
4324 {"bufname", 1, 1, f_bufname},
4325 {"bufnr", 1, 1, f_bufnr},
4326 {"bufwinnr", 1, 1, f_bufwinnr},
4327 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004328 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004329 {"call", 2, 2, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {"char2nr", 1, 1, f_char2nr},
4331 {"cindent", 1, 1, f_cindent},
4332 {"col", 1, 1, f_col},
4333 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004334 {"copy", 1, 1, f_copy},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004335 {"count", 2, 3, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {"cscope_connection",0,3, f_cscope_connection},
4337 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004338 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {"delete", 1, 1, f_delete},
4340 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00004341 {"diff_filler", 1, 1, f_diff_filler},
4342 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004343 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {"escape", 2, 2, f_escape},
4345 {"eventhandler", 0, 0, f_eventhandler},
4346 {"executable", 1, 1, f_executable},
4347 {"exists", 1, 1, f_exists},
4348 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004349 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
4351 {"filereadable", 1, 1, f_filereadable},
4352 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004353 {"finddir", 1, 3, f_finddir},
4354 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {"fnamemodify", 2, 2, f_fnamemodify},
4356 {"foldclosed", 1, 1, f_foldclosed},
4357 {"foldclosedend", 1, 1, f_foldclosedend},
4358 {"foldlevel", 1, 1, f_foldlevel},
4359 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004360 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004362 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004363 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {"getbufvar", 2, 2, f_getbufvar},
4365 {"getchar", 0, 1, f_getchar},
4366 {"getcharmod", 0, 0, f_getcharmod},
4367 {"getcmdline", 0, 0, f_getcmdline},
4368 {"getcmdpos", 0, 0, f_getcmdpos},
4369 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004370 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004371 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {"getfsize", 1, 1, f_getfsize},
4373 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004374 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004375 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {"getreg", 0, 1, f_getreg},
4377 {"getregtype", 0, 1, f_getregtype},
4378 {"getwinposx", 0, 0, f_getwinposx},
4379 {"getwinposy", 0, 0, f_getwinposy},
4380 {"getwinvar", 2, 2, f_getwinvar},
4381 {"glob", 1, 1, f_glob},
4382 {"globpath", 2, 2, f_globpath},
4383 {"has", 1, 1, f_has},
4384 {"hasmapto", 1, 2, f_hasmapto},
4385 {"highlightID", 1, 1, f_hlID}, /* obsolete */
4386 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
4387 {"histadd", 2, 2, f_histadd},
4388 {"histdel", 1, 2, f_histdel},
4389 {"histget", 1, 2, f_histget},
4390 {"histnr", 1, 1, f_histnr},
4391 {"hlID", 1, 1, f_hlID},
4392 {"hlexists", 1, 1, f_hlexists},
4393 {"hostname", 0, 0, f_hostname},
4394 {"iconv", 3, 3, f_iconv},
4395 {"indent", 1, 1, f_indent},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 {"index", 2, 3, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {"input", 1, 2, f_input},
4398 {"inputdialog", 1, 3, f_inputdialog},
4399 {"inputrestore", 0, 0, f_inputrestore},
4400 {"inputsave", 0, 0, f_inputsave},
4401 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004402 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {"isdirectory", 1, 1, f_isdirectory},
4404 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004405 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {"libcall", 3, 3, f_libcall},
4407 {"libcallnr", 3, 3, f_libcallnr},
4408 {"line", 1, 1, f_line},
4409 {"line2byte", 1, 1, f_line2byte},
4410 {"lispindent", 1, 1, f_lispindent},
4411 {"localtime", 0, 0, f_localtime},
4412 {"maparg", 1, 2, f_maparg},
4413 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004414 {"match", 2, 4, f_match},
4415 {"matchend", 2, 4, f_matchend},
4416 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004417 {"max", 1, 1, f_max},
4418 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {"mode", 0, 0, f_mode},
4420 {"nextnonblank", 1, 1, f_nextnonblank},
4421 {"nr2char", 1, 1, f_nr2char},
4422 {"prevnonblank", 1, 1, f_prevnonblank},
4423 {"remote_expr", 2, 3, f_remote_expr},
4424 {"remote_foreground", 1, 1, f_remote_foreground},
4425 {"remote_peek", 1, 2, f_remote_peek},
4426 {"remote_read", 1, 1, f_remote_read},
4427 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004430 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004432 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {"search", 1, 2, f_search},
4434 {"searchpair", 3, 5, f_searchpair},
4435 {"server2client", 2, 2, f_server2client},
4436 {"serverlist", 0, 0, f_serverlist},
4437 {"setbufvar", 3, 3, f_setbufvar},
4438 {"setcmdpos", 1, 1, f_setcmdpos},
4439 {"setline", 2, 2, f_setline},
4440 {"setreg", 2, 3, f_setreg},
4441 {"setwinvar", 3, 3, f_setwinvar},
4442 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004443 {"sort", 1, 2, f_sort},
4444 {"str2list", 1, 2, f_str2list},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445#ifdef HAVE_STRFTIME
4446 {"strftime", 1, 2, f_strftime},
4447#endif
4448 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {"strlen", 1, 1, f_strlen},
4451 {"strpart", 2, 3, f_strpart},
4452 {"strridx", 2, 2, f_strridx},
4453 {"strtrans", 1, 1, f_strtrans},
4454 {"submatch", 1, 1, f_submatch},
4455 {"substitute", 4, 4, f_substitute},
4456 {"synID", 3, 3, f_synID},
4457 {"synIDattr", 2, 3, f_synIDattr},
4458 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004459 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {"tempname", 0, 0, f_tempname},
4461 {"tolower", 1, 1, f_tolower},
4462 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00004463 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {"type", 1, 1, f_type},
4465 {"virtcol", 1, 1, f_virtcol},
4466 {"visualmode", 0, 1, f_visualmode},
4467 {"winbufnr", 1, 1, f_winbufnr},
4468 {"wincol", 0, 0, f_wincol},
4469 {"winheight", 1, 1, f_winheight},
4470 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004471 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {"winrestcmd", 0, 0, f_winrestcmd},
4473 {"winwidth", 1, 1, f_winwidth},
4474};
4475
4476#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4477
4478/*
4479 * Function given to ExpandGeneric() to obtain the list of internal
4480 * or user defined function names.
4481 */
4482 char_u *
4483get_function_name(xp, idx)
4484 expand_T *xp;
4485 int idx;
4486{
4487 static int intidx = -1;
4488 char_u *name;
4489
4490 if (idx == 0)
4491 intidx = -1;
4492 if (intidx < 0)
4493 {
4494 name = get_user_func_name(xp, idx);
4495 if (name != NULL)
4496 return name;
4497 }
4498 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
4499 {
4500 STRCPY(IObuff, functions[intidx].f_name);
4501 STRCAT(IObuff, "(");
4502 if (functions[intidx].f_max_argc == 0)
4503 STRCAT(IObuff, ")");
4504 return IObuff;
4505 }
4506
4507 return NULL;
4508}
4509
4510/*
4511 * Function given to ExpandGeneric() to obtain the list of internal or
4512 * user defined variable or function names.
4513 */
4514/*ARGSUSED*/
4515 char_u *
4516get_expr_name(xp, idx)
4517 expand_T *xp;
4518 int idx;
4519{
4520 static int intidx = -1;
4521 char_u *name;
4522
4523 if (idx == 0)
4524 intidx = -1;
4525 if (intidx < 0)
4526 {
4527 name = get_function_name(xp, idx);
4528 if (name != NULL)
4529 return name;
4530 }
4531 return get_user_var_name(xp, ++intidx);
4532}
4533
4534#endif /* FEAT_CMDL_COMPL */
4535
4536/*
4537 * Find internal function in table above.
4538 * Return index, or -1 if not found
4539 */
4540 static int
4541find_internal_func(name)
4542 char_u *name; /* name of the function */
4543{
4544 int first = 0;
4545 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
4546 int cmp;
4547 int x;
4548
4549 /*
4550 * Find the function name in the table. Binary search.
4551 */
4552 while (first <= last)
4553 {
4554 x = first + ((unsigned)(last - first) >> 1);
4555 cmp = STRCMP(name, functions[x].f_name);
4556 if (cmp < 0)
4557 last = x - 1;
4558 else if (cmp > 0)
4559 first = x + 1;
4560 else
4561 return x;
4562 }
4563 return -1;
4564}
4565
4566/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
4568 * name it contains, otherwise return "name".
4569 */
4570 static char_u *
4571deref_func_name(name, lenp)
4572 char_u *name;
4573 int *lenp;
4574{
4575 VAR v;
4576 int cc;
4577
4578 cc = name[*lenp];
4579 name[*lenp] = NUL;
4580 v = find_var(name, FALSE);
4581 name[*lenp] = cc;
4582 if (v != NULL && v->tv.v_type == VAR_FUNC)
4583 {
4584 if (v->tv.vval.v_string == NULL)
4585 {
4586 *lenp = 0;
4587 return (char_u *)""; /* just in case */
4588 }
4589 *lenp = STRLEN(v->tv.vval.v_string);
4590 return v->tv.vval.v_string;
4591 }
4592
4593 return name;
4594}
4595
4596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 * Allocate a variable for the result of a function.
4598 * Return OK or FAIL.
4599 */
4600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 char_u *name; /* name of the function */
4603 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 char_u **arg; /* argument, pointing to the '(' */
4606 linenr_T firstline; /* first line of range */
4607 linenr_T lastline; /* last line of range */
4608 int *doesrange; /* return: function handled range */
4609 int evaluate;
4610{
4611 char_u *argp;
4612 int ret = OK;
4613#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 int argcount = 0; /* number of arguments found */
4616
4617 /*
4618 * Get the arguments.
4619 */
4620 argp = *arg;
4621 while (argcount < MAX_FUNC_ARGS)
4622 {
4623 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
4624 if (*argp == ')' || *argp == ',' || *argp == NUL)
4625 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
4627 {
4628 ret = FAIL;
4629 break;
4630 }
4631 ++argcount;
4632 if (*argp != ',')
4633 break;
4634 }
4635 if (*argp == ')')
4636 ++argp;
4637 else
4638 ret = FAIL;
4639
4640 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 firstline, lastline, doesrange, evaluate);
4643 else if (!aborting())
4644 EMSG2(_("E116: Invalid arguments for function %s"), name);
4645
4646 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 *arg = skipwhite(argp);
4650 return ret;
4651}
4652
4653
4654/*
4655 * Call a function with its resolved parameters
4656 * Return OK or FAIL.
4657 */
4658 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 doesrange, evaluate)
4661 char_u *name; /* name of the function */
4662 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 linenr_T firstline; /* first line of range */
4667 linenr_T lastline; /* last line of range */
4668 int *doesrange; /* return: function handled range */
4669 int evaluate;
4670{
4671 int ret = FAIL;
4672 static char *errors[] =
4673 {N_("E117: Unknown function: %s"),
4674 N_("E118: Too many arguments for function: %s"),
4675 N_("E119: Not enough arguments for function: %s"),
4676 N_("E120: Using <SID> not in a script context: %s"),
4677 };
4678#define ERROR_UNKNOWN 0
4679#define ERROR_TOOMANY 1
4680#define ERROR_TOOFEW 2
4681#define ERROR_SCRIPT 3
4682#define ERROR_NONE 4
4683#define ERROR_OTHER 5
4684 int error = ERROR_NONE;
4685 int i;
4686 int llen;
4687 ufunc_T *fp;
4688 int cc;
4689#define FLEN_FIXED 40
4690 char_u fname_buf[FLEN_FIXED + 1];
4691 char_u *fname;
4692
4693 /*
4694 * In a script change <SID>name() and s:name() to K_SNR 123_name().
4695 * Change <SNR>123_name() to K_SNR 123_name().
4696 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
4697 */
4698 cc = name[len];
4699 name[len] = NUL;
4700 llen = eval_fname_script(name);
4701 if (llen > 0)
4702 {
4703 fname_buf[0] = K_SPECIAL;
4704 fname_buf[1] = KS_EXTRA;
4705 fname_buf[2] = (int)KE_SNR;
4706 i = 3;
4707 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
4708 {
4709 if (current_SID <= 0)
4710 error = ERROR_SCRIPT;
4711 else
4712 {
4713 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
4714 i = (int)STRLEN(fname_buf);
4715 }
4716 }
4717 if (i + STRLEN(name + llen) < FLEN_FIXED)
4718 {
4719 STRCPY(fname_buf + i, name + llen);
4720 fname = fname_buf;
4721 }
4722 else
4723 {
4724 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
4725 if (fname == NULL)
4726 error = ERROR_OTHER;
4727 else
4728 {
4729 mch_memmove(fname, fname_buf, (size_t)i);
4730 STRCPY(fname + i, name + llen);
4731 }
4732 }
4733 }
4734 else
4735 fname = name;
4736
4737 *doesrange = FALSE;
4738
4739
4740 /* execute the function if no errors detected and executing */
4741 if (evaluate && error == ERROR_NONE)
4742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 error = ERROR_UNKNOWN;
4745
4746 if (!ASCII_ISLOWER(fname[0]))
4747 {
4748 /*
4749 * User defined function.
4750 */
4751 fp = find_func(fname);
4752#ifdef FEAT_AUTOCMD
4753 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
4754 fname, fname, TRUE, NULL)
4755#ifdef FEAT_EVAL
4756 && !aborting()
4757#endif
4758 )
4759 {
4760 /* executed an autocommand, search for function again */
4761 fp = find_func(fname);
4762 }
4763#endif
4764 if (fp != NULL)
4765 {
4766 if (fp->flags & FC_RANGE)
4767 *doesrange = TRUE;
4768 if (argcount < fp->args.ga_len)
4769 error = ERROR_TOOFEW;
4770 else if (!fp->varargs && argcount > fp->args.ga_len)
4771 error = ERROR_TOOMANY;
4772 else
4773 {
4774 /*
4775 * Call the user function.
4776 * Save and restore search patterns, script variables and
4777 * redo buffer.
4778 */
4779 save_search_patterns();
4780 saveRedobuff();
4781 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 firstline, lastline);
4784 --fp->calls;
4785 restoreRedobuff();
4786 restore_search_patterns();
4787 error = ERROR_NONE;
4788 }
4789 }
4790 }
4791 else
4792 {
4793 /*
4794 * Find the function name in the table, call its implementation.
4795 */
4796 i = find_internal_func(fname);
4797 if (i >= 0)
4798 {
4799 if (argcount < functions[i].f_min_argc)
4800 error = ERROR_TOOFEW;
4801 else if (argcount > functions[i].f_max_argc)
4802 error = ERROR_TOOMANY;
4803 else
4804 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 error = ERROR_NONE;
4808 }
4809 }
4810 }
4811 /*
4812 * The function call (or "FuncUndefined" autocommand sequence) might
4813 * have been aborted by an error, an interrupt, or an explicitly thrown
4814 * exception that has not been caught so far. This situation can be
4815 * tested for by calling aborting(). For an error in an internal
4816 * function or for the "E132" error in call_user_func(), however, the
4817 * throw point at which the "force_abort" flag (temporarily reset by
4818 * emsg()) is normally updated has not been reached yet. We need to
4819 * update that flag first to make aborting() reliable.
4820 */
4821 update_force_abort();
4822 }
4823 if (error == ERROR_NONE)
4824 ret = OK;
4825
4826 /*
4827 * Report an error unless the argument evaluation or function call has been
4828 * cancelled due to an aborting error, an interrupt, or an exception.
4829 */
4830 if (error < ERROR_NONE && !aborting())
4831 EMSG2((char_u *)_(errors[error]), name);
4832
4833 name[len] = cc;
4834 if (fname != name && fname != fname_buf)
4835 vim_free(fname);
4836
4837 return ret;
4838}
4839
4840/*********************************************
4841 * Implementation of the built-in functions
4842 */
4843
4844/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00004845 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
4847 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00004848f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004852 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004857 l = argvars[0].vval.v_list;
4858 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 }
4861 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00004862 EMSG(_(e_listreq));
4863}
4864
4865/*
4866 * "append(lnum, string/list)" function
4867 */
4868 static void
4869f_append(argvars, rettv)
4870 typeval *argvars;
4871 typeval *rettv;
4872{
4873 long lnum;
4874 listvar *l = NULL;
4875 listitem *li = NULL;
4876 typeval *tv;
4877 long added = 0;
4878
4879 rettv->vval.v_number = 1; /* Default: Failed */
4880 lnum = get_tv_lnum(argvars);
4881 if (lnum >= 0
4882 && lnum <= curbuf->b_ml.ml_line_count
4883 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00004885 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00004887 l = argvars[1].vval.v_list;
4888 if (l == NULL)
4889 return;
4890 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00004892 for (;;)
4893 {
4894 if (l == NULL)
4895 tv = &argvars[1]; /* append a string */
4896 else if (li == NULL)
4897 break; /* end of list */
4898 else
4899 tv = &li->li_tv; /* append item from list */
4900 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
4901 ++added;
4902 if (l == NULL)
4903 break;
4904 li = li->li_next;
4905 }
4906
4907 appended_lines_mark(lnum, added);
4908 if (curwin->w_cursor.lnum > lnum)
4909 curwin->w_cursor.lnum += added;
4910 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912}
4913
4914/*
4915 * "argc()" function
4916 */
4917/* ARGSUSED */
4918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004920 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924}
4925
4926/*
4927 * "argidx()" function
4928 */
4929/* ARGSUSED */
4930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004935 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
4937
4938/*
4939 * "argv(nr)" function
4940 */
4941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 int idx;
4947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004948 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004952 rettv->vval.v_string = NULL;
4953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
4956/*
4957 * "browse(save, title, initdir, default)" function
4958 */
4959/* ARGSUSED */
4960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004962 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004963 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965#ifdef FEAT_BROWSE
4966 int save;
4967 char_u *title;
4968 char_u *initdir;
4969 char_u *defname;
4970 char_u buf[NUMBUFLEN];
4971 char_u buf2[NUMBUFLEN];
4972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 save = get_tv_number(&argvars[0]);
4974 title = get_tv_string(&argvars[1]);
4975 initdir = get_tv_string_buf(&argvars[2], buf);
4976 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004979 do_browse(save ? BROWSE_SAVE : 0,
4980 title, defname, NULL, initdir, NULL, curbuf);
4981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004985}
4986
4987/*
4988 * "browsedir(title, initdir)" function
4989 */
4990/* ARGSUSED */
4991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004992f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004995{
4996#ifdef FEAT_BROWSE
4997 char_u *title;
4998 char_u *initdir;
4999 char_u buf[NUMBUFLEN];
5000
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 title = get_tv_string(&argvars[0]);
5002 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005005 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010}
5011
Bram Moolenaar0d660222005-01-07 21:51:51 +00005012static buf_T *find_buffer __ARGS((typeval *avar));
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014/*
5015 * Find a buffer by number or exact name.
5016 */
5017 static buf_T *
5018find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005019 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005023 if (avar->v_type == VAR_NUMBER)
5024 buf = buflist_findnr((int)avar->vval.v_number);
5025 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005027 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005028 if (buf == NULL)
5029 {
5030 /* No full path name match, try a match with a URL or a "nofile"
5031 * buffer, these don't use the full path. */
5032 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5033 if (buf->b_fname != NULL
5034 && (path_with_url(buf->b_fname)
5035#ifdef FEAT_QUICKFIX
5036 || bt_nofile(buf)
5037#endif
5038 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005039 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005040 break;
5041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043 return buf;
5044}
5045
5046/*
5047 * "bufexists(expr)" function
5048 */
5049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005050f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005051 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
5057/*
5058 * "buflisted(expr)" function
5059 */
5060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 buf_T *buf;
5066
5067 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069}
5070
5071/*
5072 * "bufloaded(expr)" function
5073 */
5074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 buf_T *buf;
5080
5081 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083}
5084
Bram Moolenaar0d660222005-01-07 21:51:51 +00005085static buf_T *get_buf_tv __ARGS((typeval *tv));
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087/*
5088 * Get buffer by number or pattern.
5089 */
5090 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091get_buf_tv(tv)
5092 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 int save_magic;
5096 char_u *save_cpo;
5097 buf_T *buf;
5098
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 if (tv->v_type == VAR_NUMBER)
5100 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (name == NULL || *name == NUL)
5102 return curbuf;
5103 if (name[0] == '$' && name[1] == NUL)
5104 return lastbuf;
5105
5106 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
5107 save_magic = p_magic;
5108 p_magic = TRUE;
5109 save_cpo = p_cpo;
5110 p_cpo = (char_u *)"";
5111
5112 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
5113 TRUE, FALSE));
5114
5115 p_magic = save_magic;
5116 p_cpo = save_cpo;
5117
5118 /* If not found, try expanding the name, like done for bufexists(). */
5119 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122 return buf;
5123}
5124
5125/*
5126 * "bufname(expr)" function
5127 */
5128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005129f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005131 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 buf_T *buf;
5134
5135 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136 buf = get_buf_tv(&argvars[0]);
5137 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 --emsg_off;
5143}
5144
5145/*
5146 * "bufnr(expr)" function
5147 */
5148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 buf_T *buf;
5154
5155 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 --emsg_off;
5162}
5163
5164/*
5165 * "bufwinnr(nr)" function
5166 */
5167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172#ifdef FEAT_WINDOWS
5173 win_T *wp;
5174 int winnr = 0;
5175#endif
5176 buf_T *buf;
5177
5178 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_WINDOWS
5181 for (wp = firstwin; wp; wp = wp->w_next)
5182 {
5183 ++winnr;
5184 if (wp->w_buffer == buf)
5185 break;
5186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190#endif
5191 --emsg_off;
5192}
5193
5194/*
5195 * "byte2line(byte)" function
5196 */
5197/*ARGSUSED*/
5198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#else
5206 long boff = 0;
5207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 (linenr_T)0, &boff);
5214#endif
5215}
5216
5217/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005218 * "byteidx()" function
5219 */
5220/*ARGSUSED*/
5221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005225{
5226#ifdef FEAT_MBYTE
5227 char_u *t;
5228#endif
5229 char_u *str;
5230 long idx;
5231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005232 str = get_tv_string(&argvars[0]);
5233 idx = get_tv_number(&argvars[1]);
5234 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005235 if (idx < 0)
5236 return;
5237
5238#ifdef FEAT_MBYTE
5239 t = str;
5240 for ( ; idx > 0; idx--)
5241 {
5242 if (*t == NUL) /* EOL reached */
5243 return;
5244 t += mb_ptr2len_check(t);
5245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005246 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005247#else
5248 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005250#endif
5251}
5252
5253/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005254 * "call(func, arglist)" function
5255 */
5256 static void
5257f_call(argvars, rettv)
5258 typeval *argvars;
5259 typeval *rettv;
5260{
5261 char_u *func;
5262 typeval argv[MAX_FUNC_ARGS];
5263 int argc = 0;
5264 listitem *item;
5265 int dummy;
5266
5267 rettv->vval.v_number = 0;
5268 if (argvars[1].v_type != VAR_LIST)
5269 {
5270 EMSG(_(e_listreq));
5271 return;
5272 }
5273 if (argvars[1].vval.v_list == NULL)
5274 return;
5275
5276 if (argvars[0].v_type == VAR_FUNC)
5277 func = argvars[0].vval.v_string;
5278 else
5279 func = get_tv_string(&argvars[0]);
5280
5281 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
5282 item = item->li_next)
5283 {
5284 if (argc == MAX_FUNC_ARGS)
5285 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005286 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005287 break;
5288 }
5289 /* Make a copy of each argument (is this really needed?) */
5290 copy_tv(&item->li_tv, &argv[argc++]);
5291 }
5292
5293 if (item == NULL)
5294 (void)call_func(func, STRLEN(func), rettv, argc, argv,
5295 curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, TRUE);
5296
5297 /* Free the arguments. */
5298 while (argc > 0)
5299 clear_tv(&argv[--argc]);
5300}
5301
5302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 * "char2nr(string)" function
5304 */
5305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005306f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310#ifdef FEAT_MBYTE
5311 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005312 rettv->vval.v_number =
5313 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else
5315#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005316 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317}
5318
5319/*
5320 * "cindent(lnum)" function
5321 */
5322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005325 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
5327#ifdef FEAT_CINDENT
5328 pos_T pos;
5329 linenr_T lnum;
5330
5331 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5334 {
5335 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 curwin->w_cursor = pos;
5338 }
5339 else
5340#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342}
5343
5344/*
5345 * "col(string)" function
5346 */
5347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005350 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 colnr_T col = 0;
5353 pos_T *fp;
5354
5355 fp = var2fpos(&argvars[0], FALSE);
5356 if (fp != NULL)
5357 {
5358 if (fp->col == MAXCOL)
5359 {
5360 /* '> can be MAXCOL, get the length of the line then */
5361 if (fp->lnum <= curbuf->b_ml.ml_line_count)
5362 col = STRLEN(ml_get(fp->lnum)) + 1;
5363 else
5364 col = MAXCOL;
5365 }
5366 else
5367 {
5368 col = fp->col + 1;
5369#ifdef FEAT_VIRTUALEDIT
5370 /* col(".") when the cursor is on the NUL at the end of the line
5371 * because of "coladd" can be seen as an extra column. */
5372 if (virtual_active() && fp == &curwin->w_cursor)
5373 {
5374 char_u *p = ml_get_cursor();
5375
5376 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
5377 curwin->w_virtcol - curwin->w_cursor.coladd))
5378 {
5379# ifdef FEAT_MBYTE
5380 int l;
5381
5382 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
5383 col += l;
5384# else
5385 if (*p != NUL && p[1] == NUL)
5386 ++col;
5387# endif
5388 }
5389 }
5390#endif
5391 }
5392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
5395
5396/*
5397 * "confirm(message, buttons[, default [, type]])" function
5398 */
5399/*ARGSUSED*/
5400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5406 char_u *message;
5407 char_u *buttons = NULL;
5408 char_u buf[NUMBUFLEN];
5409 char_u buf2[NUMBUFLEN];
5410 int def = 1;
5411 int type = VIM_GENERIC;
5412 int c;
5413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 switch (TOUPPER_ASC(c))
5426 {
5427 case 'E': type = VIM_ERROR; break;
5428 case 'Q': type = VIM_QUESTION; break;
5429 case 'I': type = VIM_INFO; break;
5430 case 'W': type = VIM_WARNING; break;
5431 case 'G': type = VIM_GENERIC; break;
5432 }
5433 }
5434 }
5435 }
5436
5437 if (buttons == NULL || *buttons == NUL)
5438 buttons = (char_u *)_("&Ok");
5439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 def, NULL);
5442#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005443 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#endif
5445}
5446
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447/*
5448 * "copy()" function
5449 */
5450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454{
5455 if (argvars[0].v_type == VAR_LIST)
5456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 rettv->v_type = VAR_LIST;
5458 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005465 * "count()" function
5466 */
5467 static void
5468f_count(argvars, rettv)
5469 typeval *argvars;
5470 typeval *rettv;
5471{
5472 listitem *li;
5473 long n = 0;
5474 int ic = FALSE;
5475
5476 if (argvars[0].v_type != VAR_LIST)
5477 EMSG(_(e_listreq));
5478 else if (argvars[0].vval.v_list != NULL)
5479 {
5480 if (argvars[2].v_type != VAR_UNKNOWN)
5481 ic = get_tv_number(&argvars[2]);
5482
5483 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
5484 li = li->li_next)
5485 if (tv_equal(&li->li_tv, &argvars[1], ic))
5486 ++n;
5487 }
5488 rettv->vval.v_number = n;
5489}
5490
5491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
5493 *
5494 * Checks the existence of a cscope connection.
5495 */
5496/*ARGSUSED*/
5497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502#ifdef FEAT_CSCOPE
5503 int num = 0;
5504 char_u *dbpath = NULL;
5505 char_u *prepend = NULL;
5506 char_u buf[NUMBUFLEN];
5507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 if (argvars[0].v_type != VAR_UNKNOWN
5509 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 num = (int)get_tv_number(&argvars[0]);
5512 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520#endif
5521}
5522
5523/*
5524 * "cursor(lnum, col)" function
5525 *
5526 * Moves the cursor to the specified line and column
5527 */
5528/*ARGSUSED*/
5529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 long line, col;
5535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (line > 0)
5538 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (col > 0)
5541 curwin->w_cursor.col = col - 1;
5542#ifdef FEAT_VIRTUALEDIT
5543 curwin->w_cursor.coladd = 0;
5544#endif
5545
5546 /* Make sure the cursor is in a valid position. */
5547 check_cursor();
5548#ifdef FEAT_MBYTE
5549 /* Correct cursor for multi-byte character. */
5550 if (has_mbyte)
5551 mb_adjust_cursor();
5552#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005553
5554 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555}
5556
5557/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 */
5560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_LIST;
5568 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 copy_tv(&argvars[0], rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572}
5573
5574/*
5575 * "delete()" function
5576 */
5577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
5587
5588/*
5589 * "did_filetype()" function
5590 */
5591/*ARGSUSED*/
5592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601#endif
5602}
5603
5604/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00005605 * "diff_filler()" function
5606 */
5607/*ARGSUSED*/
5608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005612{
5613#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00005615#endif
5616}
5617
5618/*
5619 * "diff_hlID()" function
5620 */
5621/*ARGSUSED*/
5622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005626{
5627#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00005629 static linenr_T prev_lnum = 0;
5630 static int changedtick = 0;
5631 static int fnum = 0;
5632 static int change_start = 0;
5633 static int change_end = 0;
5634 static enum hlf_value hlID = 0;
5635 int filler_lines;
5636 int col;
5637
5638 if (lnum != prev_lnum
5639 || changedtick != curbuf->b_changedtick
5640 || fnum != curbuf->b_fnum)
5641 {
5642 /* New line, buffer, change: need to get the values. */
5643 filler_lines = diff_check(curwin, lnum);
5644 if (filler_lines < 0)
5645 {
5646 if (filler_lines == -1)
5647 {
5648 change_start = MAXCOL;
5649 change_end = -1;
5650 if (diff_find_change(curwin, lnum, &change_start, &change_end))
5651 hlID = HLF_ADD; /* added line */
5652 else
5653 hlID = HLF_CHD; /* changed line */
5654 }
5655 else
5656 hlID = HLF_ADD; /* added line */
5657 }
5658 else
5659 hlID = (enum hlf_value)0;
5660 prev_lnum = lnum;
5661 changedtick = curbuf->b_changedtick;
5662 fnum = curbuf->b_fnum;
5663 }
5664
5665 if (hlID == HLF_CHD || hlID == HLF_TXD)
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005668 if (col >= change_start && col <= change_end)
5669 hlID = HLF_TXD; /* changed text */
5670 else
5671 hlID = HLF_CHD; /* changed line */
5672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005674#endif
5675}
5676
5677/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005678 * "empty({expr})" function
5679 */
5680 static void
5681f_empty(argvars, rettv)
5682 typeval *argvars;
5683 typeval *rettv;
5684{
5685 int n;
5686
5687 switch (argvars[0].v_type)
5688 {
5689 case VAR_STRING:
5690 case VAR_FUNC:
5691 n = argvars[0].vval.v_string == NULL
5692 || *argvars[0].vval.v_string == NUL;
5693 break;
5694 case VAR_NUMBER:
5695 n = argvars[0].vval.v_number == 0;
5696 break;
5697 case VAR_LIST:
5698 n = argvars[0].vval.v_list == NULL
5699 || argvars[0].vval.v_list->lv_first == NULL;
5700 break;
5701 default:
5702 EMSG2(_(e_intern2), "f_empty()");
5703 n = 0;
5704 }
5705
5706 rettv->vval.v_number = n;
5707}
5708
5709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 * "escape({string}, {chars})" function
5711 */
5712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 char_u buf[NUMBUFLEN];
5718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv->vval.v_string =
5720 vim_strsave_escaped(get_tv_string(&argvars[0]),
5721 get_tv_string_buf(&argvars[1], buf));
5722 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723}
5724
5725/*
5726 * "eventhandler()" function
5727 */
5728/*ARGSUSED*/
5729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735}
5736
5737/*
5738 * "executable()" function
5739 */
5740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005741f_executable(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746}
5747
5748/*
5749 * "exists()" function
5750 */
5751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 char_u *p;
5757 char_u *name;
5758 int n = FALSE;
5759 int len = 0;
5760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (*p == '$') /* environment variable */
5763 {
5764 /* first try "normal" environment variables (fast) */
5765 if (mch_getenv(p + 1) != NULL)
5766 n = TRUE;
5767 else
5768 {
5769 /* try expanding things like $VIM and ${HOME} */
5770 p = expand_env_save(p);
5771 if (p != NULL && *p != '$')
5772 n = TRUE;
5773 vim_free(p);
5774 }
5775 }
5776 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 else if (*p == '*') /* internal or user defined function */
5779 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782 else if (*p == ':')
5783 {
5784 n = cmd_exists(p + 1);
5785 }
5786 else if (*p == '#')
5787 {
5788#ifdef FEAT_AUTOCMD
5789 name = p + 1;
5790 p = vim_strchr(name, '#');
5791 if (p != NULL)
5792 n = au_exists(name, p, p + 1);
5793 else
5794 n = au_exists(name, name + STRLEN(name), NULL);
5795#endif
5796 }
5797 else /* internal variable */
5798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 char_u *expr_start;
5800 char_u *expr_end;
5801 char_u *temp_string = NULL;
5802 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 name = p;
5804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005806 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 if (expr_start != NULL)
5808 {
5809 temp_string = make_expanded_name(name, expr_start, expr_end, s);
5810 if (temp_string != NULL)
5811 {
5812 len = STRLEN(temp_string);
5813 name = temp_string;
5814 }
5815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (len == 0)
5817 len = get_id_len(&p);
5818 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005819 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
5823
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005824 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826
5827/*
5828 * "expand()" function
5829 */
5830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005833 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
5835 char_u *s;
5836 int len;
5837 char_u *errormsg;
5838 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
5839 expand_T xpc;
5840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005841 rettv->v_type = VAR_STRING;
5842 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 if (*s == '%' || *s == '#' || *s == '<')
5844 {
5845 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005846 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 --emsg_off;
5848 }
5849 else
5850 {
5851 /* When the optional second argument is non-zero, don't remove matches
5852 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005853 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 flags |= WILD_KEEP_ALL;
5855 ExpandInit(&xpc);
5856 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005857 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 ExpandCleanup(&xpc);
5859 }
5860}
5861
5862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863 * "extend(list, list [, idx])" function
5864 */
5865 static void
5866f_extend(argvars, rettv)
5867 typeval *argvars;
5868 typeval *rettv;
5869{
5870 long before;
5871 long n;
5872 listitem *item;
5873 listvar *l1, *l2;
5874
5875 rettv->vval.v_number = 0;
5876 if (argvars[0].v_type != VAR_LIST || argvars[1].v_type != VAR_LIST)
5877 {
5878 EMSG(_(e_listreq));
5879 return;
5880 }
5881 l1 = argvars[0].vval.v_list;
5882 l2 = argvars[1].vval.v_list;
5883 if (l1 != NULL && l2 != NULL)
5884 {
5885 if (argvars[2].v_type != VAR_UNKNOWN)
5886 {
5887 n = before = get_tv_number(&argvars[2]);
5888 item = list_find_ext(l1, &n);
5889 if (n != 0)
5890 {
5891 EMSGN(_(e_listidx), before);
5892 return;
5893 }
5894 }
5895 else
5896 item = NULL;
5897 list_extend(l1, l2, item);
5898
5899 ++l1->lv_refcount;
5900 copy_tv(&argvars[0], rettv);
5901 }
5902}
5903
5904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 * "filereadable()" function
5906 */
5907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 FILE *fd;
5913 char_u *p;
5914 int n;
5915
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
5918 {
5919 n = TRUE;
5920 fclose(fd);
5921 }
5922 else
5923 n = FALSE;
5924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005925 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926}
5927
5928/*
5929 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
5930 * rights to write into.
5931 */
5932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005933f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 char_u *p;
5938 int retval = 0;
5939#if defined(UNIX) || defined(VMS)
5940 int perm = 0;
5941#endif
5942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005943 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944#if defined(UNIX) || defined(VMS)
5945 perm = mch_getperm(p);
5946#endif
5947#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
5948 if (
5949# ifdef WIN3264
5950 mch_writable(p) &&
5951# else
5952# if defined(UNIX) || defined(VMS)
5953 (perm & 0222) &&
5954# endif
5955# endif
5956 mch_access((char *)p, W_OK) == 0
5957 )
5958#endif
5959 {
5960 ++retval;
5961 if (mch_isdir(p))
5962 ++retval;
5963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965}
5966
Bram Moolenaar0d660222005-01-07 21:51:51 +00005967static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005968
5969 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005970findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005972 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005973 int dir;
5974{
5975#ifdef FEAT_SEARCHPATH
5976 char_u *fname;
5977 char_u *fresult = NULL;
5978 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
5979 char_u *p;
5980 char_u pathbuf[NUMBUFLEN];
5981 int count = 1;
5982 int first = TRUE;
5983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005984 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005988 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005989 if (*p != NUL)
5990 path = p;
5991
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005993 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005994 }
5995
5996 do
5997 {
5998 vim_free(fresult);
5999 fresult = find_file_in_path_option(first ? fname : NULL,
6000 first ? (int)STRLEN(fname) : 0,
6001 0, first, path, dir, NULL);
6002 first = FALSE;
6003 } while (--count > 0 && fresult != NULL);
6004
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006005 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006006#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006007 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006008#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006009 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006010}
6011
6012/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006013 * "finddir({fname}[, {path}[, {count}]])" function
6014 */
6015 static void
6016f_finddir(argvars, rettv)
6017 typeval *argvars;
6018 typeval *rettv;
6019{
6020 findfilendir(argvars, rettv, TRUE);
6021}
6022
6023/*
6024 * "findfile({fname}[, {path}[, {count}]])" function
6025 */
6026 static void
6027f_findfile(argvars, rettv)
6028 typeval *argvars;
6029 typeval *rettv;
6030{
6031 findfilendir(argvars, rettv, FALSE);
6032}
6033
6034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 * "fnamemodify({fname}, {mods})" function
6036 */
6037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006038f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 char_u *fname;
6043 char_u *mods;
6044 int usedlen = 0;
6045 int len;
6046 char_u *fbuf = NULL;
6047 char_u buf[NUMBUFLEN];
6048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006049 fname = get_tv_string(&argvars[0]);
6050 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 len = (int)STRLEN(fname);
6052
6053 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
6054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006057 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006059 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 vim_free(fbuf);
6061}
6062
Bram Moolenaar0d660222005-01-07 21:51:51 +00006063static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
6065/*
6066 * "foldclosed()" function
6067 */
6068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006071 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 int end;
6073{
6074#ifdef FEAT_FOLDING
6075 linenr_T lnum;
6076 linenr_T first, last;
6077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006078 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6080 {
6081 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
6082 {
6083 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006084 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006086 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 return;
6088 }
6089 }
6090#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006091 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092}
6093
6094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006095 * "foldclosed()" function
6096 */
6097 static void
6098f_foldclosed(argvars, rettv)
6099 typeval *argvars;
6100 typeval *rettv;
6101{
6102 foldclosed_both(argvars, rettv, FALSE);
6103}
6104
6105/*
6106 * "foldclosedend()" function
6107 */
6108 static void
6109f_foldclosedend(argvars, rettv)
6110 typeval *argvars;
6111 typeval *rettv;
6112{
6113 foldclosed_both(argvars, rettv, TRUE);
6114}
6115
6116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 * "foldlevel()" function
6118 */
6119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006120f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006122 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124#ifdef FEAT_FOLDING
6125 linenr_T lnum;
6126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006127 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006129 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 else
6131#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006132 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133}
6134
6135/*
6136 * "foldtext()" function
6137 */
6138/*ARGSUSED*/
6139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006140f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006142 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144#ifdef FEAT_FOLDING
6145 linenr_T lnum;
6146 char_u *s;
6147 char_u *r;
6148 int len;
6149 char *txt;
6150#endif
6151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006152 rettv->v_type = VAR_STRING;
6153 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154#ifdef FEAT_FOLDING
6155 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
6156 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
6157 && vimvars[VV_FOLDDASHES].val != NULL)
6158 {
6159 /* Find first non-empty line in the fold. */
6160 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
6161 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
6162 {
6163 if (!linewhite(lnum))
6164 break;
6165 ++lnum;
6166 }
6167
6168 /* Find interesting text in this line. */
6169 s = skipwhite(ml_get(lnum));
6170 /* skip C comment-start */
6171 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006174 if (*skipwhite(s) == NUL
6175 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
6176 {
6177 s = skipwhite(ml_get(lnum + 1));
6178 if (*s == '*')
6179 s = skipwhite(s + 1);
6180 }
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 txt = _("+-%s%3ld lines: ");
6183 r = alloc((unsigned)(STRLEN(txt)
6184 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
6185 + 20 /* for %3ld */
6186 + STRLEN(s))); /* concatenated */
6187 if (r != NULL)
6188 {
6189 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
6190 (long)((linenr_T)vimvars[VV_FOLDEND].val
6191 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
6192 len = (int)STRLEN(r);
6193 STRCAT(r, s);
6194 /* remove 'foldmarker' and 'commentstring' */
6195 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006196 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198 }
6199#endif
6200}
6201
6202/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006203 * "foldtextresult(lnum)" function
6204 */
6205/*ARGSUSED*/
6206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006207f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006210{
6211#ifdef FEAT_FOLDING
6212 linenr_T lnum;
6213 char_u *text;
6214 char_u buf[51];
6215 foldinfo_T foldinfo;
6216 int fold_count;
6217#endif
6218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006219 rettv->v_type = VAR_STRING;
6220 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006221#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006222 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006223 fold_count = foldedCount(curwin, lnum, &foldinfo);
6224 if (fold_count > 0)
6225 {
6226 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
6227 &foldinfo, buf);
6228 if (text == buf)
6229 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006230 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006231 }
6232#endif
6233}
6234
6235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 * "foreground()" function
6237 */
6238/*ARGSUSED*/
6239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006240f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006242 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245#ifdef FEAT_GUI
6246 if (gui.in_use)
6247 gui_mch_set_foreground();
6248#else
6249# ifdef WIN32
6250 win32_set_foreground();
6251# endif
6252#endif
6253}
6254
6255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * "function()" function
6257 */
6258/*ARGSUSED*/
6259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006260f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006262 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263{
6264 char_u *s;
6265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006266 s = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 if (s == NULL || *s == NUL || isdigit(*s))
6268 EMSG2(_(e_invarg2), s);
6269 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006270 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 else
6272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006273 rettv->vval.v_string = vim_strsave(s);
6274 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 }
6276}
6277
6278/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006279 * "get()" function
6280 */
6281 static void
6282f_get(argvars, rettv)
6283 typeval *argvars;
6284 typeval *rettv;
6285{
6286 listitem *item;
6287 listvar *l;
6288
6289 if (argvars[0].v_type != VAR_LIST)
6290 EMSG2(_(e_listarg), "get()");
6291 else if ((l = argvars[0].vval.v_list) != NULL)
6292 {
6293 item = list_find(l, get_tv_number(&argvars[1]));
6294 if (item == NULL)
6295 {
6296 if (argvars[2].v_type == VAR_UNKNOWN)
6297 rettv->vval.v_number = 0;
6298 else
6299 copy_tv(&argvars[2], rettv);
6300 }
6301 else
6302 copy_tv(&item->li_tv, rettv);
6303 }
6304}
6305
6306/*
6307 * "getbufvar()" function
6308 */
6309 static void
6310f_getbufvar(argvars, rettv)
6311 typeval *argvars;
6312 typeval *rettv;
6313{
6314 buf_T *buf;
6315 buf_T *save_curbuf;
6316 char_u *varname;
6317 VAR v;
6318
6319 ++emsg_off;
6320 buf = get_buf_tv(&argvars[0]);
6321 varname = get_tv_string(&argvars[1]);
6322
6323 rettv->v_type = VAR_STRING;
6324 rettv->vval.v_string = NULL;
6325
6326 if (buf != NULL && varname != NULL)
6327 {
6328 if (*varname == '&') /* buffer-local-option */
6329 {
6330 /* set curbuf to be our buf, temporarily */
6331 save_curbuf = curbuf;
6332 curbuf = buf;
6333
6334 get_option_tv(&varname, rettv, TRUE);
6335
6336 /* restore previous notion of curbuf */
6337 curbuf = save_curbuf;
6338 }
6339 else
6340 {
6341 /* look up the variable */
6342 v = find_var_in_ga(&buf->b_vars, varname);
6343 if (v != NULL)
6344 copy_tv(&v->tv, rettv);
6345 }
6346 }
6347
6348 --emsg_off;
6349}
6350
6351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 * "getchar()" function
6353 */
6354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006355f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006357 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 varnumber_T n;
6360
6361 ++no_mapping;
6362 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 /* getchar(): blocking wait. */
6365 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006366 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 /* getchar(1): only check if char avail */
6368 n = vpeekc();
6369 else if (vpeekc() == NUL)
6370 /* getchar(0) and no char avail: return zero */
6371 n = 0;
6372 else
6373 /* getchar(0) and char avail: return char */
6374 n = safe_vgetc();
6375 --no_mapping;
6376 --allow_keys;
6377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006378 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 if (IS_SPECIAL(n) || mod_mask != 0)
6380 {
6381 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
6382 int i = 0;
6383
6384 /* Turn a special key into three bytes, plus modifier. */
6385 if (mod_mask != 0)
6386 {
6387 temp[i++] = K_SPECIAL;
6388 temp[i++] = KS_MODIFIER;
6389 temp[i++] = mod_mask;
6390 }
6391 if (IS_SPECIAL(n))
6392 {
6393 temp[i++] = K_SPECIAL;
6394 temp[i++] = K_SECOND(n);
6395 temp[i++] = K_THIRD(n);
6396 }
6397#ifdef FEAT_MBYTE
6398 else if (has_mbyte)
6399 i += (*mb_char2bytes)(n, temp + i);
6400#endif
6401 else
6402 temp[i++] = n;
6403 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006404 rettv->v_type = VAR_STRING;
6405 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407}
6408
6409/*
6410 * "getcharmod()" function
6411 */
6412/*ARGSUSED*/
6413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006416 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419}
6420
6421/*
6422 * "getcmdline()" function
6423 */
6424/*ARGSUSED*/
6425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006426f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006428 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006430 rettv->v_type = VAR_STRING;
6431 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433
6434/*
6435 * "getcmdpos()" function
6436 */
6437/*ARGSUSED*/
6438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006439f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006441 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006443 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444}
6445
6446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 * "getcwd()" function
6448 */
6449/*ARGSUSED*/
6450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006451f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006453 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 char_u cwd[MAXPATHL];
6456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 else
6461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006464 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#endif
6466 }
6467}
6468
6469/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006470 * "getfontname()" function
6471 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006472/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006478 rettv->v_type = VAR_STRING;
6479 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006480#ifdef FEAT_GUI
6481 if (gui.in_use)
6482 {
6483 GuiFont font;
6484 char_u *name = NULL;
6485
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006487 {
6488 /* Get the "Normal" font. Either the name saved by
6489 * hl_set_font_name() or from the font ID. */
6490 font = gui.norm_font;
6491 name = hl_get_font_name();
6492 }
6493 else
6494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006496 if (STRCMP(name, "*") == 0) /* don't use font dialog */
6497 return;
6498 font = gui_mch_get_font(name, FALSE);
6499 if (font == NOFONT)
6500 return; /* Invalid font name, return empty string. */
6501 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006502 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006504 gui_mch_free_font(font);
6505 }
6506#endif
6507}
6508
6509/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006510 * "getfperm({fname})" function
6511 */
6512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006513f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006515 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006516{
6517 char_u *fname;
6518 struct stat st;
6519 char_u *perm = NULL;
6520 char_u flags[] = "rwx";
6521 int i;
6522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006523 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006526 if (mch_stat((char *)fname, &st) >= 0)
6527 {
6528 perm = vim_strsave((char_u *)"---------");
6529 if (perm != NULL)
6530 {
6531 for (i = 0; i < 9; i++)
6532 {
6533 if (st.st_mode & (1 << (8 - i)))
6534 perm[i] = flags[i % 3];
6535 }
6536 }
6537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006539}
6540
6541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 * "getfsize({fname})" function
6543 */
6544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006545f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 char_u *fname;
6550 struct stat st;
6551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006552 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006554 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 if (mch_stat((char *)fname, &st) >= 0)
6557 {
6558 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006559 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006561 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
6566
6567/*
6568 * "getftime({fname})" function
6569 */
6570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006571f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006573 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 char_u *fname;
6576 struct stat st;
6577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006581 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
6585
6586/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006587 * "getftype({fname})" function
6588 */
6589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006592 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006593{
6594 char_u *fname;
6595 struct stat st;
6596 char_u *type = NULL;
6597 char *t;
6598
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006600
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006601 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006602 if (mch_lstat((char *)fname, &st) >= 0)
6603 {
6604#ifdef S_ISREG
6605 if (S_ISREG(st.st_mode))
6606 t = "file";
6607 else if (S_ISDIR(st.st_mode))
6608 t = "dir";
6609# ifdef S_ISLNK
6610 else if (S_ISLNK(st.st_mode))
6611 t = "link";
6612# endif
6613# ifdef S_ISBLK
6614 else if (S_ISBLK(st.st_mode))
6615 t = "bdev";
6616# endif
6617# ifdef S_ISCHR
6618 else if (S_ISCHR(st.st_mode))
6619 t = "cdev";
6620# endif
6621# ifdef S_ISFIFO
6622 else if (S_ISFIFO(st.st_mode))
6623 t = "fifo";
6624# endif
6625# ifdef S_ISSOCK
6626 else if (S_ISSOCK(st.st_mode))
6627 t = "fifo";
6628# endif
6629 else
6630 t = "other";
6631#else
6632# ifdef S_IFMT
6633 switch (st.st_mode & S_IFMT)
6634 {
6635 case S_IFREG: t = "file"; break;
6636 case S_IFDIR: t = "dir"; break;
6637# ifdef S_IFLNK
6638 case S_IFLNK: t = "link"; break;
6639# endif
6640# ifdef S_IFBLK
6641 case S_IFBLK: t = "bdev"; break;
6642# endif
6643# ifdef S_IFCHR
6644 case S_IFCHR: t = "cdev"; break;
6645# endif
6646# ifdef S_IFIFO
6647 case S_IFIFO: t = "fifo"; break;
6648# endif
6649# ifdef S_IFSOCK
6650 case S_IFSOCK: t = "socket"; break;
6651# endif
6652 default: t = "other";
6653 }
6654# else
6655 if (mch_isdir(fname))
6656 t = "dir";
6657 else
6658 t = "file";
6659# endif
6660#endif
6661 type = vim_strsave((char_u *)t);
6662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006663 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006664}
6665
6666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006667 * "getline(lnum)" function
6668 */
6669 static void
6670f_getline(argvars, rettv)
6671 typeval *argvars;
6672 typeval *rettv;
6673{
6674 linenr_T lnum;
6675 linenr_T end;
6676 char_u *p;
6677 listvar *l;
6678 listitem *li;
6679
6680 lnum = get_tv_lnum(argvars);
6681
6682 if (argvars[1].v_type == VAR_UNKNOWN)
6683 {
6684 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6685 p = ml_get(lnum);
6686 else
6687 p = (char_u *)"";
6688
6689 rettv->v_type = VAR_STRING;
6690 rettv->vval.v_string = vim_strsave(p);
6691 }
6692 else
6693 {
6694 end = get_tv_lnum(&argvars[1]);
6695 if (end < lnum)
6696 {
6697 EMSG(_(e_invrange));
6698 rettv->vval.v_number = 0;
6699 }
6700 else
6701 {
6702 l = list_alloc();
6703 if (l != NULL)
6704 {
6705 if (lnum < 1)
6706 lnum = 1;
6707 if (end > curbuf->b_ml.ml_line_count)
6708 end = curbuf->b_ml.ml_line_count;
6709 while (lnum <= end)
6710 {
6711 li = listitem_alloc();
6712 if (li == NULL)
6713 break;
6714 list_append(l, li);
6715 li->li_tv.v_type = VAR_STRING;
6716 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
6717 }
6718 rettv->vval.v_list = l;
6719 rettv->v_type = VAR_LIST;
6720 ++l->lv_refcount;
6721 }
6722 }
6723 }
6724}
6725
6726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 * "getreg()" function
6728 */
6729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006730f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 char_u *strregname;
6735 int regname;
6736
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006737 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 else
6740 strregname = vimvars[VV_REG].val;
6741 regname = (strregname == NULL ? '"' : *strregname);
6742 if (regname == 0)
6743 regname = '"';
6744
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006745 rettv->v_type = VAR_STRING;
6746 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747}
6748
6749/*
6750 * "getregtype()" function
6751 */
6752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006753f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006755 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 char_u *strregname;
6758 int regname;
6759 char_u buf[NUMBUFLEN + 2];
6760 long reglen = 0;
6761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006763 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 else
6765 /* Default to v:register */
6766 strregname = vimvars[VV_REG].val;
6767
6768 regname = (strregname == NULL ? '"' : *strregname);
6769 if (regname == 0)
6770 regname = '"';
6771
6772 buf[0] = NUL;
6773 buf[1] = NUL;
6774 switch (get_reg_type(regname, &reglen))
6775 {
6776 case MLINE: buf[0] = 'V'; break;
6777 case MCHAR: buf[0] = 'v'; break;
6778#ifdef FEAT_VISUAL
6779 case MBLOCK:
6780 buf[0] = Ctrl_V;
6781 sprintf((char *)buf + 1, "%ld", reglen + 1);
6782 break;
6783#endif
6784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006785 rettv->v_type = VAR_STRING;
6786 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787}
6788
6789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 * "getwinposx()" function
6791 */
6792/*ARGSUSED*/
6793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006796 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#ifdef FEAT_GUI
6800 if (gui.in_use)
6801 {
6802 int x, y;
6803
6804 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
6807#endif
6808}
6809
6810/*
6811 * "getwinposy()" function
6812 */
6813/*ARGSUSED*/
6814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006816 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006819 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820#ifdef FEAT_GUI
6821 if (gui.in_use)
6822 {
6823 int x, y;
6824
6825 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
6828#endif
6829}
6830
6831/*
6832 * "getwinvar()" function
6833 */
6834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 win_T *win, *oldcurwin;
6840 char_u *varname;
6841 VAR v;
6842
6843 ++emsg_off;
6844 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006845 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006847 rettv->v_type = VAR_STRING;
6848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 if (win != NULL && varname != NULL)
6851 {
6852 if (*varname == '&') /* window-local-option */
6853 {
6854 /* set curwin to be our win, temporarily */
6855 oldcurwin = curwin;
6856 curwin = win;
6857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
6860 /* restore previous notion of curwin */
6861 curwin = oldcurwin;
6862 }
6863 else
6864 {
6865 /* look up the variable */
6866 v = find_var_in_ga(&win->w_vars, varname);
6867 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006868 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870 }
6871
6872 --emsg_off;
6873}
6874
6875/*
6876 * "glob()" function
6877 */
6878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879f_glob(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 expand_T xpc;
6884
6885 ExpandInit(&xpc);
6886 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887 rettv->v_type = VAR_STRING;
6888 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
6890 ExpandCleanup(&xpc);
6891}
6892
6893/*
6894 * "globpath()" function
6895 */
6896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006897f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
6901 char_u buf1[NUMBUFLEN];
6902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006903 rettv->v_type = VAR_STRING;
6904 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
6905 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906}
6907
6908/*
6909 * "has()" function
6910 */
6911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006912f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006913 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 int i;
6917 char_u *name;
6918 int n = FALSE;
6919 static char *(has_list[]) =
6920 {
6921#ifdef AMIGA
6922 "amiga",
6923# ifdef FEAT_ARP
6924 "arp",
6925# endif
6926#endif
6927#ifdef __BEOS__
6928 "beos",
6929#endif
6930#ifdef MSDOS
6931# ifdef DJGPP
6932 "dos32",
6933# else
6934 "dos16",
6935# endif
6936#endif
6937#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
6938 "mac",
6939#endif
6940#if defined(MACOS_X_UNIX)
6941 "macunix",
6942#endif
6943#ifdef OS2
6944 "os2",
6945#endif
6946#ifdef __QNX__
6947 "qnx",
6948#endif
6949#ifdef RISCOS
6950 "riscos",
6951#endif
6952#ifdef UNIX
6953 "unix",
6954#endif
6955#ifdef VMS
6956 "vms",
6957#endif
6958#ifdef WIN16
6959 "win16",
6960#endif
6961#ifdef WIN32
6962 "win32",
6963#endif
6964#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6965 "win32unix",
6966#endif
6967#ifdef WIN64
6968 "win64",
6969#endif
6970#ifdef EBCDIC
6971 "ebcdic",
6972#endif
6973#ifndef CASE_INSENSITIVE_FILENAME
6974 "fname_case",
6975#endif
6976#ifdef FEAT_ARABIC
6977 "arabic",
6978#endif
6979#ifdef FEAT_AUTOCMD
6980 "autocmd",
6981#endif
6982#ifdef FEAT_BEVAL
6983 "balloon_eval",
6984#endif
6985#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6986 "builtin_terms",
6987# ifdef ALL_BUILTIN_TCAPS
6988 "all_builtin_terms",
6989# endif
6990#endif
6991#ifdef FEAT_BYTEOFF
6992 "byte_offset",
6993#endif
6994#ifdef FEAT_CINDENT
6995 "cindent",
6996#endif
6997#ifdef FEAT_CLIENTSERVER
6998 "clientserver",
6999#endif
7000#ifdef FEAT_CLIPBOARD
7001 "clipboard",
7002#endif
7003#ifdef FEAT_CMDL_COMPL
7004 "cmdline_compl",
7005#endif
7006#ifdef FEAT_CMDHIST
7007 "cmdline_hist",
7008#endif
7009#ifdef FEAT_COMMENTS
7010 "comments",
7011#endif
7012#ifdef FEAT_CRYPT
7013 "cryptv",
7014#endif
7015#ifdef FEAT_CSCOPE
7016 "cscope",
7017#endif
7018#ifdef DEBUG
7019 "debug",
7020#endif
7021#ifdef FEAT_CON_DIALOG
7022 "dialog_con",
7023#endif
7024#ifdef FEAT_GUI_DIALOG
7025 "dialog_gui",
7026#endif
7027#ifdef FEAT_DIFF
7028 "diff",
7029#endif
7030#ifdef FEAT_DIGRAPHS
7031 "digraphs",
7032#endif
7033#ifdef FEAT_DND
7034 "dnd",
7035#endif
7036#ifdef FEAT_EMACS_TAGS
7037 "emacs_tags",
7038#endif
7039 "eval", /* always present, of course! */
7040#ifdef FEAT_EX_EXTRA
7041 "ex_extra",
7042#endif
7043#ifdef FEAT_SEARCH_EXTRA
7044 "extra_search",
7045#endif
7046#ifdef FEAT_FKMAP
7047 "farsi",
7048#endif
7049#ifdef FEAT_SEARCHPATH
7050 "file_in_path",
7051#endif
7052#ifdef FEAT_FIND_ID
7053 "find_in_path",
7054#endif
7055#ifdef FEAT_FOLDING
7056 "folding",
7057#endif
7058#ifdef FEAT_FOOTER
7059 "footer",
7060#endif
7061#if !defined(USE_SYSTEM) && defined(UNIX)
7062 "fork",
7063#endif
7064#ifdef FEAT_GETTEXT
7065 "gettext",
7066#endif
7067#ifdef FEAT_GUI
7068 "gui",
7069#endif
7070#ifdef FEAT_GUI_ATHENA
7071# ifdef FEAT_GUI_NEXTAW
7072 "gui_neXtaw",
7073# else
7074 "gui_athena",
7075# endif
7076#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007077#ifdef FEAT_GUI_KDE
7078 "gui_kde",
7079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#ifdef FEAT_GUI_GTK
7081 "gui_gtk",
7082# ifdef HAVE_GTK2
7083 "gui_gtk2",
7084# endif
7085#endif
7086#ifdef FEAT_GUI_MAC
7087 "gui_mac",
7088#endif
7089#ifdef FEAT_GUI_MOTIF
7090 "gui_motif",
7091#endif
7092#ifdef FEAT_GUI_PHOTON
7093 "gui_photon",
7094#endif
7095#ifdef FEAT_GUI_W16
7096 "gui_win16",
7097#endif
7098#ifdef FEAT_GUI_W32
7099 "gui_win32",
7100#endif
7101#ifdef FEAT_HANGULIN
7102 "hangul_input",
7103#endif
7104#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
7105 "iconv",
7106#endif
7107#ifdef FEAT_INS_EXPAND
7108 "insert_expand",
7109#endif
7110#ifdef FEAT_JUMPLIST
7111 "jumplist",
7112#endif
7113#ifdef FEAT_KEYMAP
7114 "keymap",
7115#endif
7116#ifdef FEAT_LANGMAP
7117 "langmap",
7118#endif
7119#ifdef FEAT_LIBCALL
7120 "libcall",
7121#endif
7122#ifdef FEAT_LINEBREAK
7123 "linebreak",
7124#endif
7125#ifdef FEAT_LISP
7126 "lispindent",
7127#endif
7128#ifdef FEAT_LISTCMDS
7129 "listcmds",
7130#endif
7131#ifdef FEAT_LOCALMAP
7132 "localmap",
7133#endif
7134#ifdef FEAT_MENU
7135 "menu",
7136#endif
7137#ifdef FEAT_SESSION
7138 "mksession",
7139#endif
7140#ifdef FEAT_MODIFY_FNAME
7141 "modify_fname",
7142#endif
7143#ifdef FEAT_MOUSE
7144 "mouse",
7145#endif
7146#ifdef FEAT_MOUSESHAPE
7147 "mouseshape",
7148#endif
7149#if defined(UNIX) || defined(VMS)
7150# ifdef FEAT_MOUSE_DEC
7151 "mouse_dec",
7152# endif
7153# ifdef FEAT_MOUSE_GPM
7154 "mouse_gpm",
7155# endif
7156# ifdef FEAT_MOUSE_JSB
7157 "mouse_jsbterm",
7158# endif
7159# ifdef FEAT_MOUSE_NET
7160 "mouse_netterm",
7161# endif
7162# ifdef FEAT_MOUSE_PTERM
7163 "mouse_pterm",
7164# endif
7165# ifdef FEAT_MOUSE_XTERM
7166 "mouse_xterm",
7167# endif
7168#endif
7169#ifdef FEAT_MBYTE
7170 "multi_byte",
7171#endif
7172#ifdef FEAT_MBYTE_IME
7173 "multi_byte_ime",
7174#endif
7175#ifdef FEAT_MULTI_LANG
7176 "multi_lang",
7177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007178#ifdef FEAT_MZSCHEME
7179 "mzscheme",
7180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181#ifdef FEAT_OLE
7182 "ole",
7183#endif
7184#ifdef FEAT_OSFILETYPE
7185 "osfiletype",
7186#endif
7187#ifdef FEAT_PATH_EXTRA
7188 "path_extra",
7189#endif
7190#ifdef FEAT_PERL
7191#ifndef DYNAMIC_PERL
7192 "perl",
7193#endif
7194#endif
7195#ifdef FEAT_PYTHON
7196#ifndef DYNAMIC_PYTHON
7197 "python",
7198#endif
7199#endif
7200#ifdef FEAT_POSTSCRIPT
7201 "postscript",
7202#endif
7203#ifdef FEAT_PRINTER
7204 "printer",
7205#endif
7206#ifdef FEAT_QUICKFIX
7207 "quickfix",
7208#endif
7209#ifdef FEAT_RIGHTLEFT
7210 "rightleft",
7211#endif
7212#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
7213 "ruby",
7214#endif
7215#ifdef FEAT_SCROLLBIND
7216 "scrollbind",
7217#endif
7218#ifdef FEAT_CMDL_INFO
7219 "showcmd",
7220 "cmdline_info",
7221#endif
7222#ifdef FEAT_SIGNS
7223 "signs",
7224#endif
7225#ifdef FEAT_SMARTINDENT
7226 "smartindent",
7227#endif
7228#ifdef FEAT_SNIFF
7229 "sniff",
7230#endif
7231#ifdef FEAT_STL_OPT
7232 "statusline",
7233#endif
7234#ifdef FEAT_SUN_WORKSHOP
7235 "sun_workshop",
7236#endif
7237#ifdef FEAT_NETBEANS_INTG
7238 "netbeans_intg",
7239#endif
7240#ifdef FEAT_SYN_HL
7241 "syntax",
7242#endif
7243#if defined(USE_SYSTEM) || !defined(UNIX)
7244 "system",
7245#endif
7246#ifdef FEAT_TAG_BINS
7247 "tag_binary",
7248#endif
7249#ifdef FEAT_TAG_OLDSTATIC
7250 "tag_old_static",
7251#endif
7252#ifdef FEAT_TAG_ANYWHITE
7253 "tag_any_white",
7254#endif
7255#ifdef FEAT_TCL
7256# ifndef DYNAMIC_TCL
7257 "tcl",
7258# endif
7259#endif
7260#ifdef TERMINFO
7261 "terminfo",
7262#endif
7263#ifdef FEAT_TERMRESPONSE
7264 "termresponse",
7265#endif
7266#ifdef FEAT_TEXTOBJ
7267 "textobjects",
7268#endif
7269#ifdef HAVE_TGETENT
7270 "tgetent",
7271#endif
7272#ifdef FEAT_TITLE
7273 "title",
7274#endif
7275#ifdef FEAT_TOOLBAR
7276 "toolbar",
7277#endif
7278#ifdef FEAT_USR_CMDS
7279 "user-commands", /* was accidentally included in 5.4 */
7280 "user_commands",
7281#endif
7282#ifdef FEAT_VIMINFO
7283 "viminfo",
7284#endif
7285#ifdef FEAT_VERTSPLIT
7286 "vertsplit",
7287#endif
7288#ifdef FEAT_VIRTUALEDIT
7289 "virtualedit",
7290#endif
7291#ifdef FEAT_VISUAL
7292 "visual",
7293#endif
7294#ifdef FEAT_VISUALEXTRA
7295 "visualextra",
7296#endif
7297#ifdef FEAT_VREPLACE
7298 "vreplace",
7299#endif
7300#ifdef FEAT_WILDIGN
7301 "wildignore",
7302#endif
7303#ifdef FEAT_WILDMENU
7304 "wildmenu",
7305#endif
7306#ifdef FEAT_WINDOWS
7307 "windows",
7308#endif
7309#ifdef FEAT_WAK
7310 "winaltkeys",
7311#endif
7312#ifdef FEAT_WRITEBACKUP
7313 "writebackup",
7314#endif
7315#ifdef FEAT_XIM
7316 "xim",
7317#endif
7318#ifdef FEAT_XFONTSET
7319 "xfontset",
7320#endif
7321#ifdef USE_XSMP
7322 "xsmp",
7323#endif
7324#ifdef USE_XSMP_INTERACT
7325 "xsmp_interact",
7326#endif
7327#ifdef FEAT_XCLIPBOARD
7328 "xterm_clipboard",
7329#endif
7330#ifdef FEAT_XTERM_SAVE
7331 "xterm_save",
7332#endif
7333#if defined(UNIX) && defined(FEAT_X11)
7334 "X11",
7335#endif
7336 NULL
7337 };
7338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007339 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 for (i = 0; has_list[i] != NULL; ++i)
7341 if (STRICMP(name, has_list[i]) == 0)
7342 {
7343 n = TRUE;
7344 break;
7345 }
7346
7347 if (n == FALSE)
7348 {
7349 if (STRNICMP(name, "patch", 5) == 0)
7350 n = has_patch(atoi((char *)name + 5));
7351 else if (STRICMP(name, "vim_starting") == 0)
7352 n = (starting != 0);
7353#ifdef DYNAMIC_TCL
7354 else if (STRICMP(name, "tcl") == 0)
7355 n = tcl_enabled(FALSE);
7356#endif
7357#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
7358 else if (STRICMP(name, "iconv") == 0)
7359 n = iconv_enabled(FALSE);
7360#endif
7361#ifdef DYNAMIC_RUBY
7362 else if (STRICMP(name, "ruby") == 0)
7363 n = ruby_enabled(FALSE);
7364#endif
7365#ifdef DYNAMIC_PYTHON
7366 else if (STRICMP(name, "python") == 0)
7367 n = python_enabled(FALSE);
7368#endif
7369#ifdef DYNAMIC_PERL
7370 else if (STRICMP(name, "perl") == 0)
7371 n = perl_enabled(FALSE);
7372#endif
7373#ifdef FEAT_GUI
7374 else if (STRICMP(name, "gui_running") == 0)
7375 n = (gui.in_use || gui.starting);
7376# ifdef FEAT_GUI_W32
7377 else if (STRICMP(name, "gui_win32s") == 0)
7378 n = gui_is_win32s();
7379# endif
7380# ifdef FEAT_BROWSE
7381 else if (STRICMP(name, "browse") == 0)
7382 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7383# endif
7384#endif
7385#ifdef FEAT_SYN_HL
7386 else if (STRICMP(name, "syntax_items") == 0)
7387 n = syntax_present(curbuf);
7388#endif
7389#if defined(WIN3264)
7390 else if (STRICMP(name, "win95") == 0)
7391 n = mch_windows95();
7392#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00007393#ifdef FEAT_NETBEANS_INTG
7394 else if (STRICMP(name, "netbeans_enabled") == 0)
7395 n = usingNetbeans;
7396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
7398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400}
7401
7402/*
7403 * "hasmapto()" function
7404 */
7405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007406f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007407 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007408 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 char_u *name;
7411 char_u *mode;
7412 char_u buf[NUMBUFLEN];
7413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007414 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 mode = (char_u *)"nvo";
7417 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
7420 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007421 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426/*
7427 * "histadd()" function
7428 */
7429/*ARGSUSED*/
7430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007433 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
7435#ifdef FEAT_CMDHIST
7436 int histype;
7437 char_u *str;
7438 char_u buf[NUMBUFLEN];
7439#endif
7440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (check_restricted() || check_secure())
7443 return;
7444#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 if (histype >= 0)
7447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007448 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (*str != NUL)
7450 {
7451 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 return;
7454 }
7455 }
7456#endif
7457}
7458
7459/*
7460 * "histdel()" function
7461 */
7462/*ARGSUSED*/
7463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007465 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007466 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468#ifdef FEAT_CMDHIST
7469 int n;
7470 char_u buf[NUMBUFLEN];
7471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007475 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007477 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
7478 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 else
7480 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
7482 get_tv_string_buf(&argvars[1], buf));
7483 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007485 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486#endif
7487}
7488
7489/*
7490 * "histget()" function
7491 */
7492/*ARGSUSED*/
7493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007495 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498#ifdef FEAT_CMDHIST
7499 int type;
7500 int idx;
7501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 idx = get_history_idx(type);
7505 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 idx = (int)get_tv_number(&argvars[1]);
7507 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513
7514/*
7515 * "histnr()" function
7516 */
7517/*ARGSUSED*/
7518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007520 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007521 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 int i;
7524
7525#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 if (i >= HIST_CMD && i < HIST_COUNT)
7528 i = get_history_idx(i);
7529 else
7530#endif
7531 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533}
7534
7535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 * "highlightID(name)" function
7537 */
7538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007543 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544}
7545
7546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007547 * "highlight_exists()" function
7548 */
7549 static void
7550f_hlexists(argvars, rettv)
7551 typeval *argvars;
7552 typeval *rettv;
7553{
7554 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
7555}
7556
7557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 * "hostname()" function
7559 */
7560/*ARGSUSED*/
7561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
7566 char_u hostname[256];
7567
7568 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007569 rettv->v_type = VAR_STRING;
7570 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571}
7572
7573/*
7574 * iconv() function
7575 */
7576/*ARGSUSED*/
7577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007578f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582#ifdef FEAT_MBYTE
7583 char_u buf1[NUMBUFLEN];
7584 char_u buf2[NUMBUFLEN];
7585 char_u *from, *to, *str;
7586 vimconv_T vimconv;
7587#endif
7588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007589 rettv->v_type = VAR_STRING;
7590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
7592#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 str = get_tv_string(&argvars[0]);
7594 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
7595 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 vimconv.vc_type = CONV_NONE;
7597 convert_setup(&vimconv, from, to);
7598
7599 /* If the encodings are equal, no conversion needed. */
7600 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
7605 convert_setup(&vimconv, NULL, NULL);
7606 vim_free(from);
7607 vim_free(to);
7608#endif
7609}
7610
7611/*
7612 * "indent()" function
7613 */
7614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007615f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
7619 linenr_T lnum;
7620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626}
7627
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007628/*
7629 * "index()" function
7630 */
7631 static void
7632f_index(argvars, rettv)
7633 typeval *argvars;
7634 typeval *rettv;
7635{
7636 listvar *l;
7637 listitem *item;
7638 long idx = 0;
7639 int ic = FALSE;
7640
7641 rettv->vval.v_number = -1;
7642 if (argvars[0].v_type != VAR_LIST)
7643 {
7644 EMSG(_(e_listreq));
7645 return;
7646 }
7647 l = argvars[0].vval.v_list;
7648 if (l != NULL)
7649 {
7650 if (argvars[2].v_type != VAR_UNKNOWN)
7651 ic = get_tv_number(&argvars[2]);
7652
7653 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
7654 if (tv_equal(&item->li_tv, &argvars[1], ic))
7655 {
7656 rettv->vval.v_number = idx;
7657 break;
7658 }
7659 }
7660}
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662static int inputsecret_flag = 0;
7663
7664/*
7665 * "input()" function
7666 * Also handles inputsecret() when inputsecret is set.
7667 */
7668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 char_u *p = NULL;
7675 int c;
7676 char_u buf[NUMBUFLEN];
7677 int cmd_silent_save = cmd_silent;
7678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681#ifdef NO_CONSOLE_INPUT
7682 /* While starting up, there is no place to enter text. */
7683 if (no_console_input())
7684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 return;
7687 }
7688#endif
7689
7690 cmd_silent = FALSE; /* Want to see the prompt. */
7691 if (prompt != NULL)
7692 {
7693 /* Only the part of the message after the last NL is considered as
7694 * prompt for the command line */
7695 p = vim_strrchr(prompt, '\n');
7696 if (p == NULL)
7697 p = prompt;
7698 else
7699 {
7700 ++p;
7701 c = *p;
7702 *p = NUL;
7703 msg_start();
7704 msg_clr_eos();
7705 msg_puts_attr(prompt, echo_attr);
7706 msg_didout = FALSE;
7707 msg_starthere();
7708 *p = c;
7709 }
7710 cmdline_row = msg_row;
7711 }
7712
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
7718
7719 /* since the user typed this, no need to wait for return */
7720 need_wait_return = FALSE;
7721 msg_didout = FALSE;
7722 cmd_silent = cmd_silent_save;
7723}
7724
7725/*
7726 * "inputdialog()" function
7727 */
7728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007729f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733#if defined(FEAT_GUI_TEXTDIALOG)
7734 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7735 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7736 {
7737 char_u *message;
7738 char_u buf[NUMBUFLEN];
7739
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007741 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 IObuff[IOSIZE - 1] = NUL;
7745 }
7746 else
7747 IObuff[0] = NUL;
7748 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
7749 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 else
7752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753 if (argvars[1].v_type != VAR_UNKNOWN
7754 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_string = vim_strsave(
7756 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762 else
7763#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766
7767static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7768
7769/*
7770 * "inputrestore()" function
7771 */
7772/*ARGSUSED*/
7773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 if (ga_userinput.ga_len > 0)
7779 {
7780 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7782 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785 else if (p_verbose > 1)
7786 {
7787 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790}
7791
7792/*
7793 * "inputsave()" function
7794 */
7795/*ARGSUSED*/
7796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 /* Add an entry to the stack of typehead storage. */
7802 if (ga_grow(&ga_userinput, 1) == OK)
7803 {
7804 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7805 + ga_userinput.ga_len);
7806 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811}
7812
7813/*
7814 * "inputsecret()" function
7815 */
7816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007817f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007819 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 ++cmdline_star;
7822 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 --cmdline_star;
7825 --inputsecret_flag;
7826}
7827
7828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 * "insert()" function
7830 */
7831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835{
7836 long before = 0;
7837 long n;
7838 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 listvar *l;
7840
7841 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007842 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 else if ((l = argvars[0].vval.v_list) != NULL)
7844 {
7845 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007848 n = before;
7849 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007850 if (n > 0)
7851 EMSGN(_(e_listidx), before);
7852 else
7853 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007854 list_insert_tv(l, &argvars[1], item);
7855 ++l->lv_refcount;
7856 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 }
7858 }
7859}
7860
7861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 * "isdirectory()" function
7863 */
7864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007866 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870}
7871
7872/*
7873 * "last_buffer_nr()" function.
7874 */
7875/*ARGSUSED*/
7876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 int n = 0;
7882 buf_T *buf;
7883
7884 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7885 if (n < buf->b_fnum)
7886 n = buf->b_fnum;
7887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007888 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889}
7890
7891/*
7892 * "len()" function
7893 */
7894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898{
7899 switch (argvars[0].v_type)
7900 {
7901 case VAR_STRING:
7902 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->vval.v_number = (varnumber_T)STRLEN(
7904 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007905 break;
7906 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007907 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 break;
7909 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007910 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 break;
7912 }
7913}
7914
Bram Moolenaar0d660222005-01-07 21:51:51 +00007915static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916
7917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921 int type;
7922{
7923#ifdef FEAT_LIBCALL
7924 char_u *string_in;
7925 char_u **string_result;
7926 int nr_result;
7927#endif
7928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007930 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007931 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007932 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007934
7935 if (check_restricted() || check_secure())
7936 return;
7937
7938#ifdef FEAT_LIBCALL
7939 /* The first two args must be strings, otherwise its meaningless */
7940 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7941 {
7942 if (argvars[2].v_type == VAR_NUMBER)
7943 string_in = NULL;
7944 else
7945 string_in = argvars[2].vval.v_string;
7946 if (type == VAR_NUMBER)
7947 string_result = NULL;
7948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007950 if (mch_libcall(argvars[0].vval.v_string,
7951 argvars[1].vval.v_string,
7952 string_in,
7953 argvars[2].vval.v_number,
7954 string_result,
7955 &nr_result) == OK
7956 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 }
7959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960}
7961
7962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007963 * "libcall()" function
7964 */
7965 static void
7966f_libcall(argvars, rettv)
7967 typeval *argvars;
7968 typeval *rettv;
7969{
7970 libcall_common(argvars, rettv, VAR_STRING);
7971}
7972
7973/*
7974 * "libcallnr()" function
7975 */
7976 static void
7977f_libcallnr(argvars, rettv)
7978 typeval *argvars;
7979 typeval *rettv;
7980{
7981 libcall_common(argvars, rettv, VAR_NUMBER);
7982}
7983
7984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 * "line(string)" function
7986 */
7987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 linenr_T lnum = 0;
7993 pos_T *fp;
7994
7995 fp = var2fpos(&argvars[0], TRUE);
7996 if (fp != NULL)
7997 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007998 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999}
8000
8001/*
8002 * "line2byte(lnum)" function
8003 */
8004/*ARGSUSED*/
8005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012#else
8013 linenr_T lnum;
8014
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8020 if (rettv->vval.v_number >= 0)
8021 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022#endif
8023}
8024
8025/*
8026 * "lispindent(lnum)" function
8027 */
8028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032{
8033#ifdef FEAT_LISP
8034 pos_T pos;
8035 linenr_T lnum;
8036
8037 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8040 {
8041 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 curwin->w_cursor = pos;
8044 }
8045 else
8046#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048}
8049
8050/*
8051 * "localtime()" function
8052 */
8053/*ARGSUSED*/
8054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008056 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008057 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
Bram Moolenaar0d660222005-01-07 21:51:51 +00008062static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008066 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008067 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 int exact;
8069{
8070 char_u *keys;
8071 char_u *which;
8072 char_u buf[NUMBUFLEN];
8073 char_u *keys_buf = NULL;
8074 char_u *rhs;
8075 int mode;
8076 garray_T ga;
8077
8078 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008079 rettv->v_type = VAR_STRING;
8080 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (*keys == NUL)
8084 return;
8085
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 else
8089 which = (char_u *)"";
8090 mode = get_map_mode(&which, 0);
8091
8092 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
8093 rhs = check_map(keys, mode, exact);
8094 vim_free(keys_buf);
8095 if (rhs != NULL)
8096 {
8097 ga_init(&ga);
8098 ga.ga_itemsize = 1;
8099 ga.ga_growsize = 40;
8100
8101 while (*rhs != NUL)
8102 ga_concat(&ga, str2special(&rhs, FALSE));
8103
8104 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 }
8107}
8108
8109/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008110 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 */
8112 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008113f_maparg(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{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008117 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118}
8119
8120/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008121 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 */
8123 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008124f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008126 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008128 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129}
8130
Bram Moolenaar0d660222005-01-07 21:51:51 +00008131static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132
8133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 int type;
8138{
8139 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008140 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 char_u *pat;
8142 regmatch_T regmatch;
8143 char_u patbuf[NUMBUFLEN];
8144 char_u *save_cpo;
8145 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008146 long nth = 1;
8147 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
8149 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8150 save_cpo = p_cpo;
8151 p_cpo = (char_u *)"";
8152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008153 expr = str = get_tv_string(&argvars[0]);
8154 pat = get_tv_string_buf(&argvars[1], patbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
8156 if (type == 2)
8157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 rettv->v_type = VAR_STRING;
8159 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 }
8161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008164 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 start = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 if (start < 0)
8168 start = 0;
8169 if (start > (long)STRLEN(str))
8170 goto theend;
8171 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008172
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 }
8176
8177 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8178 if (regmatch.regprog != NULL)
8179 {
8180 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008181
8182 while (1)
8183 {
8184 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
8185 if (!match || --nth <= 0)
8186 break;
8187 /* Advance to just after the match. */
8188#ifdef FEAT_MBYTE
8189 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
8190#else
8191 str = regmatch.startp[0] + 1;
8192#endif
8193 }
8194
8195 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {
8197 if (type == 2)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 (int)(regmatch.endp[0] - regmatch.startp[0]));
8200 else
8201 {
8202 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 (varnumber_T)(regmatch.startp[0] - str);
8205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008206 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 }
8210 }
8211 vim_free(regmatch.regprog);
8212 }
8213
8214theend:
8215 p_cpo = save_cpo;
8216}
8217
8218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008219 * "match()" function
8220 */
8221 static void
8222f_match(argvars, rettv)
8223 typeval *argvars;
8224 typeval *rettv;
8225{
8226 find_some_match(argvars, rettv, 1);
8227}
8228
8229/*
8230 * "matchend()" function
8231 */
8232 static void
8233f_matchend(argvars, rettv)
8234 typeval *argvars;
8235 typeval *rettv;
8236{
8237 find_some_match(argvars, rettv, 0);
8238}
8239
8240/*
8241 * "matchstr()" function
8242 */
8243 static void
8244f_matchstr(argvars, rettv)
8245 typeval *argvars;
8246 typeval *rettv;
8247{
8248 find_some_match(argvars, rettv, 2);
8249}
8250
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008251static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
8252
8253 static void
8254max_min(argvars, rettv, domax)
8255 typeval *argvars;
8256 typeval *rettv;
8257 int domax;
8258{
8259 listvar *l;
8260 listitem *li;
8261 long n = 0;
8262 long i;
8263
8264 if (argvars[0].v_type == VAR_LIST)
8265 {
8266 l = argvars[0].vval.v_list;
8267 if (l != NULL)
8268 {
8269 li = l->lv_first;
8270 if (li != NULL)
8271 {
8272 n = get_tv_number(&li->li_tv);
8273 while (1)
8274 {
8275 li = li->li_next;
8276 if (li == NULL)
8277 break;
8278 i = get_tv_number(&li->li_tv);
8279 if (domax ? i > n : i < n)
8280 n = i;
8281 }
8282 }
8283 }
8284 }
8285 else
8286 EMSG(_(e_listreq));
8287 rettv->vval.v_number = n;
8288}
8289
8290/*
8291 * "max()" function
8292 */
8293 static void
8294f_max(argvars, rettv)
8295 typeval *argvars;
8296 typeval *rettv;
8297{
8298 max_min(argvars, rettv, TRUE);
8299}
8300
8301/*
8302 * "min()" function
8303 */
8304 static void
8305f_min(argvars, rettv)
8306 typeval *argvars;
8307 typeval *rettv;
8308{
8309 max_min(argvars, rettv, FALSE);
8310}
8311
Bram Moolenaar0d660222005-01-07 21:51:51 +00008312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 * "mode()" function
8314 */
8315/*ARGSUSED*/
8316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 char_u buf[2];
8322
8323#ifdef FEAT_VISUAL
8324 if (VIsual_active)
8325 {
8326 if (VIsual_select)
8327 buf[0] = VIsual_mode + 's' - 'v';
8328 else
8329 buf[0] = VIsual_mode;
8330 }
8331 else
8332#endif
8333 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
8334 buf[0] = 'r';
8335 else if (State & INSERT)
8336 {
8337 if (State & REPLACE_FLAG)
8338 buf[0] = 'R';
8339 else
8340 buf[0] = 'i';
8341 }
8342 else if (State & CMDLINE)
8343 buf[0] = 'c';
8344 else
8345 buf[0] = 'n';
8346
8347 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348 rettv->vval.v_string = vim_strsave(buf);
8349 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350}
8351
8352/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008353 * "nextnonblank()" function
8354 */
8355 static void
8356f_nextnonblank(argvars, rettv)
8357 typeval *argvars;
8358 typeval *rettv;
8359{
8360 linenr_T lnum;
8361
8362 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8363 {
8364 if (lnum > curbuf->b_ml.ml_line_count)
8365 {
8366 lnum = 0;
8367 break;
8368 }
8369 if (*skipwhite(ml_get(lnum)) != NUL)
8370 break;
8371 }
8372 rettv->vval.v_number = lnum;
8373}
8374
8375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * "nr2char()" function
8377 */
8378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008380 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008381 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382{
8383 char_u buf[NUMBUFLEN];
8384
8385#ifdef FEAT_MBYTE
8386 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 else
8389#endif
8390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 buf[1] = NUL;
8393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008394 rettv->v_type = VAR_STRING;
8395 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008396}
8397
8398/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008399 * "prevnonblank()" function
8400 */
8401 static void
8402f_prevnonblank(argvars, rettv)
8403 typeval *argvars;
8404 typeval *rettv;
8405{
8406 linenr_T lnum;
8407
8408 lnum = get_tv_lnum(argvars);
8409 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8410 lnum = 0;
8411 else
8412 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8413 --lnum;
8414 rettv->vval.v_number = lnum;
8415}
8416
8417#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8418static void make_connection __ARGS((void));
8419static int check_connection __ARGS((void));
8420
8421 static void
8422make_connection()
8423{
8424 if (X_DISPLAY == NULL
8425# ifdef FEAT_GUI
8426 && !gui.in_use
8427# endif
8428 )
8429 {
8430 x_force_connect = TRUE;
8431 setup_term_clip();
8432 x_force_connect = FALSE;
8433 }
8434}
8435
8436 static int
8437check_connection()
8438{
8439 make_connection();
8440 if (X_DISPLAY == NULL)
8441 {
8442 EMSG(_("E240: No connection to Vim server"));
8443 return FAIL;
8444 }
8445 return OK;
8446}
8447#endif
8448
8449#ifdef FEAT_CLIENTSERVER
8450static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
8451
8452 static void
8453remote_common(argvars, rettv, expr)
8454 typeval *argvars;
8455 typeval *rettv;
8456 int expr;
8457{
8458 char_u *server_name;
8459 char_u *keys;
8460 char_u *r = NULL;
8461 char_u buf[NUMBUFLEN];
8462# ifdef WIN32
8463 HWND w;
8464# else
8465 Window w;
8466# endif
8467
8468 if (check_restricted() || check_secure())
8469 return;
8470
8471# ifdef FEAT_X11
8472 if (check_connection() == FAIL)
8473 return;
8474# endif
8475
8476 server_name = get_tv_string(&argvars[0]);
8477 keys = get_tv_string_buf(&argvars[1], buf);
8478# ifdef WIN32
8479 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8480# else
8481 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8482 < 0)
8483# endif
8484 {
8485 if (r != NULL)
8486 EMSG(r); /* sending worked but evaluation failed */
8487 else
8488 EMSG2(_("E241: Unable to send to %s"), server_name);
8489 return;
8490 }
8491
8492 rettv->vval.v_string = r;
8493
8494 if (argvars[2].v_type != VAR_UNKNOWN)
8495 {
8496 var v;
8497 char_u str[30];
8498
8499 sprintf((char *)str, "0x%x", (unsigned int)w);
8500 v.tv.v_type = VAR_STRING;
8501 v.tv.vval.v_string = vim_strsave(str);
8502 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
8503 vim_free(v.tv.vval.v_string);
8504 }
8505}
8506#endif
8507
8508/*
8509 * "remote_expr()" function
8510 */
8511/*ARGSUSED*/
8512 static void
8513f_remote_expr(argvars, rettv)
8514 typeval *argvars;
8515 typeval *rettv;
8516{
8517 rettv->v_type = VAR_STRING;
8518 rettv->vval.v_string = NULL;
8519#ifdef FEAT_CLIENTSERVER
8520 remote_common(argvars, rettv, TRUE);
8521#endif
8522}
8523
8524/*
8525 * "remote_foreground()" function
8526 */
8527/*ARGSUSED*/
8528 static void
8529f_remote_foreground(argvars, rettv)
8530 typeval *argvars;
8531 typeval *rettv;
8532{
8533 rettv->vval.v_number = 0;
8534#ifdef FEAT_CLIENTSERVER
8535# ifdef WIN32
8536 /* On Win32 it's done in this application. */
8537 serverForeground(get_tv_string(&argvars[0]));
8538# else
8539 /* Send a foreground() expression to the server. */
8540 argvars[1].v_type = VAR_STRING;
8541 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8542 argvars[2].v_type = VAR_UNKNOWN;
8543 remote_common(argvars, rettv, TRUE);
8544 vim_free(argvars[1].vval.v_string);
8545# endif
8546#endif
8547}
8548
8549/*ARGSUSED*/
8550 static void
8551f_remote_peek(argvars, rettv)
8552 typeval *argvars;
8553 typeval *rettv;
8554{
8555#ifdef FEAT_CLIENTSERVER
8556 var v;
8557 char_u *s = NULL;
8558# ifdef WIN32
8559 int n = 0;
8560# endif
8561
8562 if (check_restricted() || check_secure())
8563 {
8564 rettv->vval.v_number = -1;
8565 return;
8566 }
8567# ifdef WIN32
8568 sscanf(get_tv_string(&argvars[0]), "%x", &n);
8569 if (n == 0)
8570 rettv->vval.v_number = -1;
8571 else
8572 {
8573 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8574 rettv->vval.v_number = (s != NULL);
8575 }
8576# else
8577 rettv->vval.v_number = 0;
8578 if (check_connection() == FAIL)
8579 return;
8580
8581 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8582 serverStrToWin(get_tv_string(&argvars[0])), &s);
8583# endif
8584
8585 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8586 {
8587 v.tv.v_type = VAR_STRING;
8588 v.tv.vval.v_string = vim_strsave(s);
8589 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
8590 vim_free(v.tv.vval.v_string);
8591 }
8592#else
8593 rettv->vval.v_number = -1;
8594#endif
8595}
8596
8597/*ARGSUSED*/
8598 static void
8599f_remote_read(argvars, rettv)
8600 typeval *argvars;
8601 typeval *rettv;
8602{
8603 char_u *r = NULL;
8604
8605#ifdef FEAT_CLIENTSERVER
8606 if (!check_restricted() && !check_secure())
8607 {
8608# ifdef WIN32
8609 /* The server's HWND is encoded in the 'id' parameter */
8610 int n = 0;
8611
8612 sscanf(get_tv_string(&argvars[0]), "%x", &n);
8613 if (n != 0)
8614 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8615 if (r == NULL)
8616# else
8617 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8618 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
8619# endif
8620 EMSG(_("E277: Unable to read a server reply"));
8621 }
8622#endif
8623 rettv->v_type = VAR_STRING;
8624 rettv->vval.v_string = r;
8625}
8626
8627/*
8628 * "remote_send()" function
8629 */
8630/*ARGSUSED*/
8631 static void
8632f_remote_send(argvars, rettv)
8633 typeval *argvars;
8634 typeval *rettv;
8635{
8636 rettv->v_type = VAR_STRING;
8637 rettv->vval.v_string = NULL;
8638#ifdef FEAT_CLIENTSERVER
8639 remote_common(argvars, rettv, FALSE);
8640#endif
8641}
8642
8643/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008644 * "remove({list}, {idx} [, {end}])" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008645 */
8646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650{
8651 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008652 listitem *item, *item2;
8653 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008655 long end;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008657 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008658 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008659 EMSG2(_(e_listarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 else if ((l = argvars[0].vval.v_list) != NULL)
8661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008663 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008664 if (item == NULL)
8665 EMSGN(_(e_listidx), idx);
8666 else
8667 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008668 if (argvars[2].v_type == VAR_UNKNOWN)
8669 {
8670 /* Remove one item, return its value. */
8671 list_getrem(l, item, item);
8672 *rettv = item->li_tv;
8673 vim_free(item);
8674 }
8675 else
8676 {
8677 /* Remove range of items, return list with values. */
8678 end = get_tv_number(&argvars[2]);
8679 item2 = list_find(l, end);
8680 if (item2 == NULL)
8681 EMSGN(_(e_listidx), end);
8682 else
8683 {
8684 for (li = item; li != item2 && li != NULL; li = li->li_next)
8685 ;
8686 if (li == NULL) /* didn't find "item2" after "item" */
8687 EMSG(_(e_invrange));
8688 else
8689 {
8690 list_getrem(l, item, item2);
8691 l = list_alloc();
8692 if (l != NULL)
8693 {
8694 rettv->v_type = VAR_LIST;
8695 rettv->vval.v_list = l;
8696 l->lv_first = item;
8697 l->lv_last = item2;
8698 l->lv_refcount = 1;
8699 item->li_prev = NULL;
8700 item2->li_next = NULL;
8701 }
8702 }
8703 }
8704 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008705 }
8706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707}
8708
8709/*
8710 * "rename({from}, {to})" function
8711 */
8712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
8717 char_u buf[NUMBUFLEN];
8718
8719 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8723 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
8726/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008727 * "repeat()" function
8728 */
8729/*ARGSUSED*/
8730 static void
8731f_repeat(argvars, rettv)
8732 typeval *argvars;
8733 typeval *rettv;
8734{
8735 char_u *p;
8736 int n;
8737 int slen;
8738 int len;
8739 char_u *r;
8740 int i;
8741 listvar *l;
8742
8743 n = get_tv_number(&argvars[1]);
8744 if (argvars[0].v_type == VAR_LIST)
8745 {
8746 l = list_alloc();
8747 if (l != NULL && argvars[0].vval.v_list != NULL)
8748 {
8749 l->lv_refcount = 1;
8750 while (n-- > 0)
8751 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
8752 break;
8753 }
8754 rettv->v_type = VAR_LIST;
8755 rettv->vval.v_list = l;
8756 }
8757 else
8758 {
8759 p = get_tv_string(&argvars[0]);
8760 rettv->v_type = VAR_STRING;
8761 rettv->vval.v_string = NULL;
8762
8763 slen = (int)STRLEN(p);
8764 len = slen * n;
8765 if (len <= 0)
8766 return;
8767
8768 r = alloc(len + 1);
8769 if (r != NULL)
8770 {
8771 for (i = 0; i < n; i++)
8772 mch_memmove(r + i * slen, p, (size_t)slen);
8773 r[len] = NUL;
8774 }
8775
8776 rettv->vval.v_string = r;
8777 }
8778}
8779
8780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 * "resolve()" function
8782 */
8783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008785 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 char_u *p;
8789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#ifdef FEAT_SHORTCUT
8792 {
8793 char_u *v = NULL;
8794
8795 v = mch_resolve_shortcut(p);
8796 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 }
8801#else
8802# ifdef HAVE_READLINK
8803 {
8804 char_u buf[MAXPATHL + 1];
8805 char_u *cpy;
8806 int len;
8807 char_u *remain = NULL;
8808 char_u *q;
8809 int is_relative_to_current = FALSE;
8810 int has_trailing_pathsep = FALSE;
8811 int limit = 100;
8812
8813 p = vim_strsave(p);
8814
8815 if (p[0] == '.' && (vim_ispathsep(p[1])
8816 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8817 is_relative_to_current = TRUE;
8818
8819 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008820 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 has_trailing_pathsep = TRUE;
8822
8823 q = getnextcomp(p);
8824 if (*q != NUL)
8825 {
8826 /* Separate the first path component in "p", and keep the
8827 * remainder (beginning with the path separator). */
8828 remain = vim_strsave(q - 1);
8829 q[-1] = NUL;
8830 }
8831
8832 for (;;)
8833 {
8834 for (;;)
8835 {
8836 len = readlink((char *)p, (char *)buf, MAXPATHL);
8837 if (len <= 0)
8838 break;
8839 buf[len] = NUL;
8840
8841 if (limit-- == 0)
8842 {
8843 vim_free(p);
8844 vim_free(remain);
8845 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 goto fail;
8848 }
8849
8850 /* Ensure that the result will have a trailing path separator
8851 * if the argument has one. */
8852 if (remain == NULL && has_trailing_pathsep)
8853 add_pathsep(buf);
8854
8855 /* Separate the first path component in the link value and
8856 * concatenate the remainders. */
8857 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8858 if (*q != NUL)
8859 {
8860 if (remain == NULL)
8861 remain = vim_strsave(q - 1);
8862 else
8863 {
8864 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
8865 if (cpy != NULL)
8866 {
8867 STRCAT(cpy, remain);
8868 vim_free(remain);
8869 remain = cpy;
8870 }
8871 }
8872 q[-1] = NUL;
8873 }
8874
8875 q = gettail(p);
8876 if (q > p && *q == NUL)
8877 {
8878 /* Ignore trailing path separator. */
8879 q[-1] = NUL;
8880 q = gettail(p);
8881 }
8882 if (q > p && !mch_isFullName(buf))
8883 {
8884 /* symlink is relative to directory of argument */
8885 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8886 if (cpy != NULL)
8887 {
8888 STRCPY(cpy, p);
8889 STRCPY(gettail(cpy), buf);
8890 vim_free(p);
8891 p = cpy;
8892 }
8893 }
8894 else
8895 {
8896 vim_free(p);
8897 p = vim_strsave(buf);
8898 }
8899 }
8900
8901 if (remain == NULL)
8902 break;
8903
8904 /* Append the first path component of "remain" to "p". */
8905 q = getnextcomp(remain + 1);
8906 len = q - remain - (*q != NUL);
8907 cpy = vim_strnsave(p, STRLEN(p) + len);
8908 if (cpy != NULL)
8909 {
8910 STRNCAT(cpy, remain, len);
8911 vim_free(p);
8912 p = cpy;
8913 }
8914 /* Shorten "remain". */
8915 if (*q != NUL)
8916 STRCPY(remain, q - 1);
8917 else
8918 {
8919 vim_free(remain);
8920 remain = NULL;
8921 }
8922 }
8923
8924 /* If the result is a relative path name, make it explicitly relative to
8925 * the current directory if and only if the argument had this form. */
8926 if (!vim_ispathsep(*p))
8927 {
8928 if (is_relative_to_current
8929 && *p != NUL
8930 && !(p[0] == '.'
8931 && (p[1] == NUL
8932 || vim_ispathsep(p[1])
8933 || (p[1] == '.'
8934 && (p[2] == NUL
8935 || vim_ispathsep(p[2]))))))
8936 {
8937 /* Prepend "./". */
8938 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
8939 if (cpy != NULL)
8940 {
8941 STRCAT(cpy, p);
8942 vim_free(p);
8943 p = cpy;
8944 }
8945 }
8946 else if (!is_relative_to_current)
8947 {
8948 /* Strip leading "./". */
8949 q = p;
8950 while (q[0] == '.' && vim_ispathsep(q[1]))
8951 q += 2;
8952 if (q > p)
8953 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
8954 }
8955 }
8956
8957 /* Ensure that the result will have no trailing path separator
8958 * if the argument had none. But keep "/" or "//". */
8959 if (!has_trailing_pathsep)
8960 {
8961 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008962 if (after_pathsep(p, q))
8963 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970# endif
8971#endif
8972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975#ifdef HAVE_READLINK
8976fail:
8977#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008982 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 */
8984 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 listvar *l;
8990 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992 rettv->vval.v_number = 0;
8993 if (argvars[0].v_type != VAR_LIST)
8994 EMSG2(_(e_listarg), "reverse()");
8995 else if ((l = argvars[0].vval.v_list) != NULL)
8996 {
8997 li = l->lv_last;
8998 l->lv_first = l->lv_last = li;
8999 while (li != NULL)
9000 {
9001 ni = li->li_prev;
9002 list_append(l, li);
9003 li = ni;
9004 }
9005 rettv->vval.v_list = l;
9006 rettv->v_type = VAR_LIST;
9007 ++l->lv_refcount;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009}
9010
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009011#define SP_NOMOVE 1 /* don't move cursor */
9012#define SP_REPEAT 2 /* repeat to find outer pair */
9013#define SP_RETCOUNT 4 /* return matchcount */
9014
Bram Moolenaar0d660222005-01-07 21:51:51 +00009015static int get_search_arg __ARGS((typeval *varp, int *flagsp));
9016
9017/*
9018 * Get flags for a search function.
9019 * Possibly sets "p_ws".
9020 * Returns BACKWARD, FORWARD or zero (for an error).
9021 */
9022 static int
9023get_search_arg(varp, flagsp)
9024 typeval *varp;
9025 int *flagsp;
9026{
9027 int dir = FORWARD;
9028 char_u *flags;
9029 char_u nbuf[NUMBUFLEN];
9030 int mask;
9031
9032 if (varp->v_type != VAR_UNKNOWN)
9033 {
9034 flags = get_tv_string_buf(varp, nbuf);
9035 while (*flags != NUL)
9036 {
9037 switch (*flags)
9038 {
9039 case 'b': dir = BACKWARD; break;
9040 case 'w': p_ws = TRUE; break;
9041 case 'W': p_ws = FALSE; break;
9042 default: mask = 0;
9043 if (flagsp != NULL)
9044 switch (*flags)
9045 {
9046 case 'n': mask = SP_NOMOVE; break;
9047 case 'r': mask = SP_REPEAT; break;
9048 case 'm': mask = SP_RETCOUNT; break;
9049 }
9050 if (mask == 0)
9051 {
9052 EMSG2(_(e_invarg2), flags);
9053 dir = 0;
9054 }
9055 else
9056 *flagsp |= mask;
9057 }
9058 if (dir == 0)
9059 break;
9060 ++flags;
9061 }
9062 }
9063 return dir;
9064}
9065
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066/*
9067 * "search()" function
9068 */
9069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009071 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 char_u *pat;
9075 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009076 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 int save_p_ws = p_ws;
9078 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009079 int flags = 0;
9080
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009084 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
9085 if (dir == 0)
9086 goto theend;
9087 if ((flags & ~SP_NOMOVE) != 0)
9088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009090 goto theend;
9091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009093 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
9095 SEARCH_KEEP, RE_SEARCH) != FAIL)
9096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 curwin->w_cursor = pos;
9099 /* "/$" will put the cursor after the end of the line, may need to
9100 * correct that here */
9101 check_cursor();
9102 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009103
9104 /* If 'n' flag is used: restore cursor position. */
9105 if (flags & SP_NOMOVE)
9106 curwin->w_cursor = save_cursor;
9107theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 p_ws = save_p_ws;
9109}
9110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111/*
9112 * "searchpair()" function
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009116 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 char_u *spat, *mpat, *epat;
9120 char_u *skip;
9121 char_u *pat, *pat2, *pat3;
9122 pos_T pos;
9123 pos_T firstpos;
9124 pos_T save_cursor;
9125 pos_T save_pos;
9126 int save_p_ws = p_ws;
9127 char_u *save_cpo;
9128 int dir;
9129 int flags = 0;
9130 char_u nbuf1[NUMBUFLEN];
9131 char_u nbuf2[NUMBUFLEN];
9132 char_u nbuf3[NUMBUFLEN];
9133 int n;
9134 int r;
9135 int nest = 1;
9136 int err;
9137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139
9140 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9141 save_cpo = p_cpo;
9142 p_cpo = (char_u *)"";
9143
9144 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 spat = get_tv_string(&argvars[0]);
9146 mpat = get_tv_string_buf(&argvars[1], nbuf1);
9147 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148
9149 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9150 * start/middle/end (pat3, for the top pair). */
9151 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
9152 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
9153 if (pat2 == NULL || pat3 == NULL)
9154 goto theend;
9155 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
9156 if (*mpat == NUL)
9157 STRCPY(pat3, pat2);
9158 else
9159 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
9160 spat, epat, mpat);
9161
9162 /* Handle the optional fourth argument: flags */
9163 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009164 if (dir == 0)
9165 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166
9167 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009168 if (argvars[3].v_type == VAR_UNKNOWN
9169 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 skip = (char_u *)"";
9171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173
9174 save_cursor = curwin->w_cursor;
9175 pos = curwin->w_cursor;
9176 firstpos.lnum = 0;
9177 pat = pat3;
9178 for (;;)
9179 {
9180 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9181 SEARCH_KEEP, RE_SEARCH);
9182 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9183 /* didn't find it or found the first match again: FAIL */
9184 break;
9185
9186 if (firstpos.lnum == 0)
9187 firstpos = pos;
9188
9189 /* If the skip pattern matches, ignore this match. */
9190 if (*skip != NUL)
9191 {
9192 save_pos = curwin->w_cursor;
9193 curwin->w_cursor = pos;
9194 r = eval_to_bool(skip, &err, NULL, FALSE);
9195 curwin->w_cursor = save_pos;
9196 if (err)
9197 {
9198 /* Evaluating {skip} caused an error, break here. */
9199 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 break;
9202 }
9203 if (r)
9204 continue;
9205 }
9206
9207 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9208 {
9209 /* Found end when searching backwards or start when searching
9210 * forward: nested pair. */
9211 ++nest;
9212 pat = pat2; /* nested, don't search for middle */
9213 }
9214 else
9215 {
9216 /* Found end when searching forward or start when searching
9217 * backward: end of (nested) pair; or found middle in outer pair. */
9218 if (--nest == 1)
9219 pat = pat3; /* outer level, search for middle */
9220 }
9221
9222 if (nest == 0)
9223 {
9224 /* Found the match: return matchcount or line number. */
9225 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009228 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 curwin->w_cursor = pos;
9230 if (!(flags & SP_REPEAT))
9231 break;
9232 nest = 1; /* search for next unmatched */
9233 }
9234 }
9235
9236 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 curwin->w_cursor = save_cursor;
9239
9240theend:
9241 vim_free(pat2);
9242 vim_free(pat3);
9243 p_ws = save_p_ws;
9244 p_cpo = save_cpo;
9245}
9246
Bram Moolenaar0d660222005-01-07 21:51:51 +00009247/*ARGSUSED*/
9248 static void
9249f_server2client(argvars, rettv)
9250 typeval *argvars;
9251 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009253#ifdef FEAT_CLIENTSERVER
9254 char_u buf[NUMBUFLEN];
9255 char_u *server = get_tv_string(&argvars[0]);
9256 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
Bram Moolenaar0d660222005-01-07 21:51:51 +00009258 rettv->vval.v_number = -1;
9259 if (check_restricted() || check_secure())
9260 return;
9261# ifdef FEAT_X11
9262 if (check_connection() == FAIL)
9263 return;
9264# endif
9265
9266 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009268 EMSG(_("E258: Unable to send to client"));
9269 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009271 rettv->vval.v_number = 0;
9272#else
9273 rettv->vval.v_number = -1;
9274#endif
9275}
9276
9277/*ARGSUSED*/
9278 static void
9279f_serverlist(argvars, rettv)
9280 typeval *argvars;
9281 typeval *rettv;
9282{
9283 char_u *r = NULL;
9284
9285#ifdef FEAT_CLIENTSERVER
9286# ifdef WIN32
9287 r = serverGetVimNames();
9288# else
9289 make_connection();
9290 if (X_DISPLAY != NULL)
9291 r = serverGetVimNames(X_DISPLAY);
9292# endif
9293#endif
9294 rettv->v_type = VAR_STRING;
9295 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296}
9297
9298/*
9299 * "setbufvar()" function
9300 */
9301/*ARGSUSED*/
9302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009304 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307 buf_T *buf;
9308#ifdef FEAT_AUTOCMD
9309 aco_save_T aco;
9310#else
9311 buf_T *save_curbuf;
9312#endif
9313 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009314 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 char_u nbuf[NUMBUFLEN];
9316
9317 if (check_restricted() || check_secure())
9318 return;
9319 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 buf = get_buf_tv(&argvars[0]);
9321 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 varp = &argvars[2];
9323
9324 if (buf != NULL && varname != NULL && varp != NULL)
9325 {
9326 /* set curbuf to be our buf, temporarily */
9327#ifdef FEAT_AUTOCMD
9328 aucmd_prepbuf(&aco, buf);
9329#else
9330 save_curbuf = curbuf;
9331 curbuf = buf;
9332#endif
9333
9334 if (*varname == '&')
9335 {
9336 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337 set_option_value(varname, get_tv_number(varp),
9338 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
9340 else
9341 {
9342 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9343 if (bufvarname != NULL)
9344 {
9345 STRCPY(bufvarname, "b:");
9346 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009347 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 vim_free(bufvarname);
9349 }
9350 }
9351
9352 /* reset notion of buffer */
9353#ifdef FEAT_AUTOCMD
9354 aucmd_restbuf(&aco);
9355#else
9356 curbuf = save_curbuf;
9357#endif
9358 }
9359 --emsg_off;
9360}
9361
9362/*
9363 * "setcmdpos()" function
9364 */
9365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009367 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = set_cmdline_pos(
9371 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372}
9373
9374/*
9375 * "setline()" function
9376 */
9377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009379 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 linenr_T lnum;
9383 char_u *line;
9384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 lnum = get_tv_lnum(argvars);
9386 line = get_tv_string(&argvars[1]);
9387 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
9389 if (lnum >= 1
9390 && lnum <= curbuf->b_ml.ml_line_count
9391 && u_savesub(lnum) == OK
9392 && ml_replace(lnum, line, TRUE) == OK)
9393 {
9394 changed_bytes(lnum, 0);
9395 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 }
9398}
9399
9400/*
9401 * "setreg()" function
9402 */
9403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009405 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 int regname;
9409 char_u *strregname;
9410 char_u *stropt;
9411 int append;
9412 char_u yank_type;
9413 long block_len;
9414
9415 block_len = -1;
9416 yank_type = MAUTO;
9417 append = FALSE;
9418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 strregname = get_tv_string(argvars);
9420 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421
9422 regname = (strregname == NULL ? '"' : *strregname);
9423 if (regname == 0 || regname == '@')
9424 regname = '"';
9425 else if (regname == '=')
9426 return;
9427
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 switch (*stropt)
9432 {
9433 case 'a': case 'A': /* append */
9434 append = TRUE;
9435 break;
9436 case 'v': case 'c': /* character-wise selection */
9437 yank_type = MCHAR;
9438 break;
9439 case 'V': case 'l': /* line-wise selection */
9440 yank_type = MLINE;
9441 break;
9442#ifdef FEAT_VISUAL
9443 case 'b': case Ctrl_V: /* block-wise selection */
9444 yank_type = MBLOCK;
9445 if (VIM_ISDIGIT(stropt[1]))
9446 {
9447 ++stropt;
9448 block_len = getdigits(&stropt) - 1;
9449 --stropt;
9450 }
9451 break;
9452#endif
9453 }
9454 }
9455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459}
9460
9461
9462/*
9463 * "setwinvar(expr)" function
9464 */
9465/*ARGSUSED*/
9466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009468 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 win_T *win;
9472#ifdef FEAT_WINDOWS
9473 win_T *save_curwin;
9474#endif
9475 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009476 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 char_u nbuf[NUMBUFLEN];
9478
9479 if (check_restricted() || check_secure())
9480 return;
9481 ++emsg_off;
9482 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 varp = &argvars[2];
9485
9486 if (win != NULL && varname != NULL && varp != NULL)
9487 {
9488#ifdef FEAT_WINDOWS
9489 /* set curwin to be our win, temporarily */
9490 save_curwin = curwin;
9491 curwin = win;
9492 curbuf = curwin->w_buffer;
9493#endif
9494
9495 if (*varname == '&')
9496 {
9497 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 set_option_value(varname, get_tv_number(varp),
9499 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
9501 else
9502 {
9503 winvarname = alloc((unsigned)STRLEN(varname) + 3);
9504 if (winvarname != NULL)
9505 {
9506 STRCPY(winvarname, "w:");
9507 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009508 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 vim_free(winvarname);
9510 }
9511 }
9512
9513#ifdef FEAT_WINDOWS
9514 /* Restore current window, if it's still valid (autocomands can make
9515 * it invalid). */
9516 if (win_valid(save_curwin))
9517 {
9518 curwin = save_curwin;
9519 curbuf = curwin->w_buffer;
9520 }
9521#endif
9522 }
9523 --emsg_off;
9524}
9525
9526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009527 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 */
9529 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009530f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009531 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009534 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536 p = get_tv_string(&argvars[0]);
9537 rettv->vval.v_string = vim_strsave(p);
9538 simplify_filename(rettv->vval.v_string); /* simplify in place */
9539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541
Bram Moolenaar0d660222005-01-07 21:51:51 +00009542static int
9543#ifdef __BORLANDC__
9544 _RTLENTRYF
9545#endif
9546 item_compare __ARGS((const void *s1, const void *s2));
9547static int
9548#ifdef __BORLANDC__
9549 _RTLENTRYF
9550#endif
9551 item_compare2 __ARGS((const void *s1, const void *s2));
9552
9553static int item_compare_ic;
9554static char_u *item_compare_func;
9555#define ITEM_COMPARE_FAIL 999
9556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009558 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 */
Bram Moolenaar0d660222005-01-07 21:51:51 +00009560 static int
9561#ifdef __BORLANDC__
9562_RTLENTRYF
9563#endif
9564item_compare(s1, s2)
9565 const void *s1;
9566 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568 char_u *p1, *p2;
9569 char_u *tofree1, *tofree2;
9570 int res;
9571 char_u numbuf1[NUMBUFLEN];
9572 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
Bram Moolenaar0d660222005-01-07 21:51:51 +00009574 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
9575 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
9576 if (item_compare_ic)
9577 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009579 res = STRCMP(p1, p2);
9580 vim_free(tofree1);
9581 vim_free(tofree2);
9582 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583}
9584
9585 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +00009586#ifdef __BORLANDC__
9587_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00009589item_compare2(s1, s2)
9590 const void *s1;
9591 const void *s2;
9592{
9593 int res;
9594 typeval rettv;
9595 typeval argv[2];
9596 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598 /* copy the values (is this really needed?) */
9599 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
9600 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
9601
9602 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
9603 res = call_func(item_compare_func, STRLEN(item_compare_func),
9604 &rettv, 2, argv, 0L, 0L, &dummy, TRUE);
9605 clear_tv(&argv[0]);
9606 clear_tv(&argv[1]);
9607
9608 if (res == FAIL)
9609 res = ITEM_COMPARE_FAIL;
9610 else
9611 res = get_tv_number(&rettv);
9612 clear_tv(&rettv);
9613 return res;
9614}
9615
9616/*
9617 * "sort({list})" function
9618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009620f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009624 listvar *l;
9625 listitem *li;
9626 listitem **ptrs;
9627 long len;
9628 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
Bram Moolenaar0d660222005-01-07 21:51:51 +00009630 rettv->vval.v_number = 0;
9631 if (argvars[0].v_type != VAR_LIST)
9632 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 else
9634 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009635 l = argvars[0].vval.v_list;
9636 if (l == NULL)
9637 return;
9638 rettv->vval.v_list = l;
9639 rettv->v_type = VAR_LIST;
9640 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642 len = list_len(l);
9643 if (len <= 1)
9644 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645
Bram Moolenaar0d660222005-01-07 21:51:51 +00009646 item_compare_ic = FALSE;
9647 item_compare_func = NULL;
9648 if (argvars[1].v_type != VAR_UNKNOWN)
9649 {
9650 if (argvars[1].v_type == VAR_FUNC)
9651 item_compare_func = argvars[0].vval.v_string;
9652 else
9653 {
9654 i = get_tv_number(&argvars[1]);
9655 if (i == 1)
9656 item_compare_ic = TRUE;
9657 else
9658 item_compare_func = get_tv_string(&argvars[1]);
9659 }
9660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
Bram Moolenaar0d660222005-01-07 21:51:51 +00009662 /* Make an array with each entry pointing to an item in the List. */
9663 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
9664 if (ptrs == NULL)
9665 return;
9666 i = 0;
9667 for (li = l->lv_first; li != NULL; li = li->li_next)
9668 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669
Bram Moolenaar0d660222005-01-07 21:51:51 +00009670 /* test the compare function */
9671 if (item_compare_func != NULL
9672 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
9673 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009674 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009676 {
9677 /* Sort the array with item pointers. */
9678 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
9679 item_compare_func == NULL ? item_compare : item_compare2);
9680
9681 /* Clear the List and append the items in the sorted order. */
9682 l->lv_first = l->lv_last = NULL;
9683 for (i = 0; i < len; ++i)
9684 list_append(l, ptrs[i]);
9685 }
9686
9687 vim_free(ptrs);
9688 }
9689}
9690
9691 static void
9692f_str2list(argvars, rettv)
9693 typeval *argvars;
9694 typeval *rettv;
9695{
9696 char_u *str;
9697 char_u *end;
9698 char_u *pat;
9699 regmatch_T regmatch;
9700 char_u patbuf[NUMBUFLEN];
9701 char_u *save_cpo;
9702 int match;
9703 listitem *ni;
9704 listvar *l;
9705 colnr_T col = 0;
9706
9707 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9708 save_cpo = p_cpo;
9709 p_cpo = (char_u *)"";
9710
9711 str = get_tv_string(&argvars[0]);
9712 if (argvars[1].v_type == VAR_UNKNOWN)
9713 pat = (char_u *)"[\\x01- ]\\+";
9714 else
9715 pat = get_tv_string_buf(&argvars[1], patbuf);
9716
9717 l = list_alloc();
9718 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009720 rettv->v_type = VAR_LIST;
9721 rettv->vval.v_list = l;
9722 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723
Bram Moolenaar0d660222005-01-07 21:51:51 +00009724 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9725 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009727 regmatch.rm_ic = FALSE;
9728 while (*str != NUL)
9729 {
9730 match = vim_regexec_nl(&regmatch, str, col);
9731 if (match)
9732 end = regmatch.startp[0];
9733 else
9734 end = str + STRLEN(str);
9735 if (end > str)
9736 {
9737 ni = listitem_alloc();
9738 if (ni == NULL)
9739 break;
9740 ni->li_tv.v_type = VAR_STRING;
9741 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
9742 list_append(l, ni);
9743 }
9744 if (!match)
9745 break;
9746 /* Advance to just after the match. */
9747 if (regmatch.endp[0] > str)
9748 col = 0;
9749 else
9750 {
9751 /* Don't get stuck at the same match. */
9752#ifdef FEAT_MBYTE
9753 col = mb_ptr2len_check(regmatch.endp[0]);
9754#else
9755 col = 1;
9756#endif
9757 }
9758 str = regmatch.endp[0];
9759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
Bram Moolenaar0d660222005-01-07 21:51:51 +00009761 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
Bram Moolenaar0d660222005-01-07 21:51:51 +00009764 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765}
9766
9767#ifdef HAVE_STRFTIME
9768/*
9769 * "strftime({format}[, {time}])" function
9770 */
9771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009773 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776 char_u result_buf[256];
9777 struct tm *curtime;
9778 time_t seconds;
9779 char_u *p;
9780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009784 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 seconds = time(NULL);
9786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 curtime = localtime(&seconds);
9789 /* MSVC returns NULL for an invalid value of seconds. */
9790 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 else
9793 {
9794# ifdef FEAT_MBYTE
9795 vimconv_T conv;
9796 char_u *enc;
9797
9798 conv.vc_type = CONV_NONE;
9799 enc = enc_locale();
9800 convert_setup(&conv, p_enc, enc);
9801 if (conv.vc_type != CONV_NONE)
9802 p = string_convert(&conv, p, NULL);
9803# endif
9804 if (p != NULL)
9805 (void)strftime((char *)result_buf, sizeof(result_buf),
9806 (char *)p, curtime);
9807 else
9808 result_buf[0] = NUL;
9809
9810# ifdef FEAT_MBYTE
9811 if (conv.vc_type != CONV_NONE)
9812 vim_free(p);
9813 convert_setup(&conv, enc, p_enc);
9814 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 else
9817# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819
9820# ifdef FEAT_MBYTE
9821 /* Release conversion descriptors */
9822 convert_setup(&conv, NULL, NULL);
9823 vim_free(enc);
9824# endif
9825 }
9826}
9827#endif
9828
9829/*
9830 * "stridx()" function
9831 */
9832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009834 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 char_u buf[NUMBUFLEN];
9838 char_u *needle;
9839 char_u *haystack;
9840 char_u *pos;
9841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 needle = get_tv_string(&argvars[1]);
9843 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9845
9846 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850}
9851
9852/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009853 * "string()" function
9854 */
9855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009857 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009859{
9860 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009861 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009864 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009865 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
9869/*
9870 * "strlen()" function
9871 */
9872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009874 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = (varnumber_T)(STRLEN(
9878 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879}
9880
9881/*
9882 * "strpart()" function
9883 */
9884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009886 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889 char_u *p;
9890 int n;
9891 int len;
9892 int slen;
9893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 slen = (int)STRLEN(p);
9896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009898 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 else
9901 len = slen - n; /* default len: all bytes that are available. */
9902
9903 /*
9904 * Only return the overlap between the specified part and the actual
9905 * string.
9906 */
9907 if (n < 0)
9908 {
9909 len += n;
9910 n = 0;
9911 }
9912 else if (n > slen)
9913 n = slen;
9914 if (len < 0)
9915 len = 0;
9916 else if (n + len > slen)
9917 len = slen - n;
9918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 rettv->v_type = VAR_STRING;
9920 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921}
9922
9923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009924 * "strridx()" function
9925 */
9926 static void
9927f_strridx(argvars, rettv)
9928 typeval *argvars;
9929 typeval *rettv;
9930{
9931 char_u buf[NUMBUFLEN];
9932 char_u *needle;
9933 char_u *haystack;
9934 char_u *rest;
9935 char_u *lastmatch = NULL;
9936
9937 needle = get_tv_string(&argvars[1]);
9938 haystack = get_tv_string_buf(&argvars[0], buf);
9939 if (*needle == NUL)
9940 /* Empty string matches past the end. */
9941 lastmatch = haystack + STRLEN(haystack);
9942 else
9943 for (rest = haystack; *rest != '\0'; ++rest)
9944 {
9945 rest = (char_u *)strstr((char *)rest, (char *)needle);
9946 if (rest == NULL)
9947 break;
9948 lastmatch = rest;
9949 }
9950
9951 if (lastmatch == NULL)
9952 rettv->vval.v_number = -1;
9953 else
9954 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9955}
9956
9957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 * "strtrans()" function
9959 */
9960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009962 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->v_type = VAR_STRING;
9966 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
9968
9969/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009970 * "submatch()" function
9971 */
9972 static void
9973f_submatch(argvars, rettv)
9974 typeval *argvars;
9975 typeval *rettv;
9976{
9977 rettv->v_type = VAR_STRING;
9978 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
9979}
9980
9981/*
9982 * "substitute()" function
9983 */
9984 static void
9985f_substitute(argvars, rettv)
9986 typeval *argvars;
9987 typeval *rettv;
9988{
9989 char_u patbuf[NUMBUFLEN];
9990 char_u subbuf[NUMBUFLEN];
9991 char_u flagsbuf[NUMBUFLEN];
9992
9993 rettv->v_type = VAR_STRING;
9994 rettv->vval.v_string = do_string_sub(
9995 get_tv_string(&argvars[0]),
9996 get_tv_string_buf(&argvars[1], patbuf),
9997 get_tv_string_buf(&argvars[2], subbuf),
9998 get_tv_string_buf(&argvars[3], flagsbuf));
9999}
10000
10001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 * "synID(line, col, trans)" function
10003 */
10004/*ARGSUSED*/
10005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010007 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010 int id = 0;
10011#ifdef FEAT_SYN_HL
10012 long line;
10013 long col;
10014 int trans;
10015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 line = get_tv_lnum(argvars);
10017 col = get_tv_number(&argvars[1]) - 1;
10018 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019
10020 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
10021 && col >= 0 && col < (long)STRLEN(ml_get(line)))
10022 id = syn_get_id(line, col, trans);
10023#endif
10024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026}
10027
10028/*
10029 * "synIDattr(id, what [, mode])" function
10030 */
10031/*ARGSUSED*/
10032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010034 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 char_u *p = NULL;
10038#ifdef FEAT_SYN_HL
10039 int id;
10040 char_u *what;
10041 char_u *mode;
10042 char_u modebuf[NUMBUFLEN];
10043 int modec;
10044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 id = get_tv_number(&argvars[0]);
10046 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 modec = TOLOWER_ASC(mode[0]);
10051 if (modec != 't' && modec != 'c'
10052#ifdef FEAT_GUI
10053 && modec != 'g'
10054#endif
10055 )
10056 modec = 0; /* replace invalid with current */
10057 }
10058 else
10059 {
10060#ifdef FEAT_GUI
10061 if (gui.in_use)
10062 modec = 'g';
10063 else
10064#endif
10065 if (t_colors > 1)
10066 modec = 'c';
10067 else
10068 modec = 't';
10069 }
10070
10071
10072 switch (TOLOWER_ASC(what[0]))
10073 {
10074 case 'b':
10075 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
10076 p = highlight_color(id, what, modec);
10077 else /* bold */
10078 p = highlight_has_attr(id, HL_BOLD, modec);
10079 break;
10080
10081 case 'f': /* fg[#] */
10082 p = highlight_color(id, what, modec);
10083 break;
10084
10085 case 'i':
10086 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
10087 p = highlight_has_attr(id, HL_INVERSE, modec);
10088 else /* italic */
10089 p = highlight_has_attr(id, HL_ITALIC, modec);
10090 break;
10091
10092 case 'n': /* name */
10093 p = get_highlight_name(NULL, id - 1);
10094 break;
10095
10096 case 'r': /* reverse */
10097 p = highlight_has_attr(id, HL_INVERSE, modec);
10098 break;
10099
10100 case 's': /* standout */
10101 p = highlight_has_attr(id, HL_STANDOUT, modec);
10102 break;
10103
10104 case 'u': /* underline */
10105 p = highlight_has_attr(id, HL_UNDERLINE, modec);
10106 break;
10107 }
10108
10109 if (p != NULL)
10110 p = vim_strsave(p);
10111#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->v_type = VAR_STRING;
10113 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114}
10115
10116/*
10117 * "synIDtrans(id)" function
10118 */
10119/*ARGSUSED*/
10120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010122 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124{
10125 int id;
10126
10127#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
10130 if (id > 0)
10131 id = syn_get_final_id(id);
10132 else
10133#endif
10134 id = 0;
10135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137}
10138
10139/*
10140 * "system()" function
10141 */
10142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010144 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010147 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010149 char_u *infile = NULL;
10150 char_u buf[NUMBUFLEN];
10151 int err = FALSE;
10152 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010154 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010155 {
10156 /*
10157 * Write the string to a temp file, to be used for input of the shell
10158 * command.
10159 */
10160 if ((infile = vim_tempname('i')) == NULL)
10161 {
10162 EMSG(_(e_notmp));
10163 return;
10164 }
10165
10166 fd = mch_fopen((char *)infile, WRITEBIN);
10167 if (fd == NULL)
10168 {
10169 EMSG2(_(e_notopen), infile);
10170 goto done;
10171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010173 if (fwrite(p, STRLEN(p), 1, fd) != 1)
10174 err = TRUE;
10175 if (fclose(fd) != 0)
10176 err = TRUE;
10177 if (err)
10178 {
10179 EMSG(_("E677: Error writing temp file"));
10180 goto done;
10181 }
10182 }
10183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010185
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186#ifdef USE_CR
10187 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010188 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 {
10190 char_u *s;
10191
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010192 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 {
10194 if (*s == CAR)
10195 *s = NL;
10196 }
10197 }
10198#else
10199# ifdef USE_CRNL
10200 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010201 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
10203 char_u *s, *d;
10204
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010205 d = res;
10206 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 {
10208 if (s[0] == CAR && s[1] == NL)
10209 ++s;
10210 *d++ = *s;
10211 }
10212 *d = NUL;
10213 }
10214# endif
10215#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010216
10217done:
10218 if (infile != NULL)
10219 {
10220 mch_remove(infile);
10221 vim_free(infile);
10222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->v_type = VAR_STRING;
10224 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225}
10226
10227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 * "tempname()" function
10229 */
10230/*ARGSUSED*/
10231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010233 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010234 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236 static int x = 'A';
10237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 rettv->v_type = VAR_STRING;
10239 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
10242 * names. Skip 'I' and 'O', they are used for shell redirection. */
10243 do
10244 {
10245 if (x == 'Z')
10246 x = '0';
10247 else if (x == '9')
10248 x = 'A';
10249 else
10250 {
10251#ifdef EBCDIC
10252 if (x == 'I')
10253 x = 'J';
10254 else if (x == 'R')
10255 x = 'S';
10256 else
10257#endif
10258 ++x;
10259 }
10260 } while (x == 'I' || x == 'O');
10261}
10262
10263/*
10264 * "tolower(string)" function
10265 */
10266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010267f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010268 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010269 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271 char_u *p;
10272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010273 p = vim_strsave(get_tv_string(&argvars[0]));
10274 rettv->v_type = VAR_STRING;
10275 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276
10277 if (p != NULL)
10278 while (*p != NUL)
10279 {
10280#ifdef FEAT_MBYTE
10281 int l;
10282
10283 if (enc_utf8)
10284 {
10285 int c, lc;
10286
10287 c = utf_ptr2char(p);
10288 lc = utf_tolower(c);
10289 l = utf_ptr2len_check(p);
10290 /* TODO: reallocate string when byte count changes. */
10291 if (utf_char2len(lc) == l)
10292 utf_char2bytes(lc, p);
10293 p += l;
10294 }
10295 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10296 p += l; /* skip multi-byte character */
10297 else
10298#endif
10299 {
10300 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
10301 ++p;
10302 }
10303 }
10304}
10305
10306/*
10307 * "toupper(string)" function
10308 */
10309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010311 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313{
10314 char_u *p;
10315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 p = vim_strsave(get_tv_string(&argvars[0]));
10317 rettv->v_type = VAR_STRING;
10318 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
10320 if (p != NULL)
10321 while (*p != NUL)
10322 {
10323#ifdef FEAT_MBYTE
10324 int l;
10325
10326 if (enc_utf8)
10327 {
10328 int c, uc;
10329
10330 c = utf_ptr2char(p);
10331 uc = utf_toupper(c);
10332 l = utf_ptr2len_check(p);
10333 /* TODO: reallocate string when byte count changes. */
10334 if (utf_char2len(uc) == l)
10335 utf_char2bytes(uc, p);
10336 p += l;
10337 }
10338 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10339 p += l; /* skip multi-byte character */
10340 else
10341#endif
10342 {
10343 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
10344 p++;
10345 }
10346 }
10347}
10348
10349/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000010350 * "tr(string, fromstr, tostr)" function
10351 */
10352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010353f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010356{
10357 char_u *instr;
10358 char_u *fromstr;
10359 char_u *tostr;
10360 char_u *p;
10361#ifdef FEAT_MBYTE
10362 int inlen;
10363 int fromlen;
10364 int tolen;
10365 int idx;
10366 char_u *cpstr;
10367 int cplen;
10368 int first = TRUE;
10369#endif
10370 char_u buf[NUMBUFLEN];
10371 char_u buf2[NUMBUFLEN];
10372 garray_T ga;
10373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374 instr = get_tv_string(&argvars[0]);
10375 fromstr = get_tv_string_buf(&argvars[1], buf);
10376 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010377
10378 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 rettv->v_type = VAR_STRING;
10380 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010381 ga_init2(&ga, (int)sizeof(char), 80);
10382
10383#ifdef FEAT_MBYTE
10384 if (!has_mbyte)
10385#endif
10386 /* not multi-byte: fromstr and tostr must be the same length */
10387 if (STRLEN(fromstr) != STRLEN(tostr))
10388 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010389#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000010390error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010391#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000010392 EMSG2(_(e_invarg2), fromstr);
10393 ga_clear(&ga);
10394 return;
10395 }
10396
10397 /* fromstr and tostr have to contain the same number of chars */
10398 while (*instr != NUL)
10399 {
10400#ifdef FEAT_MBYTE
10401 if (has_mbyte)
10402 {
10403 inlen = mb_ptr2len_check(instr);
10404 cpstr = instr;
10405 cplen = inlen;
10406 idx = 0;
10407 for (p = fromstr; *p != NUL; p += fromlen)
10408 {
10409 fromlen = mb_ptr2len_check(p);
10410 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
10411 {
10412 for (p = tostr; *p != NUL; p += tolen)
10413 {
10414 tolen = mb_ptr2len_check(p);
10415 if (idx-- == 0)
10416 {
10417 cplen = tolen;
10418 cpstr = p;
10419 break;
10420 }
10421 }
10422 if (*p == NUL) /* tostr is shorter than fromstr */
10423 goto error;
10424 break;
10425 }
10426 ++idx;
10427 }
10428
10429 if (first && cpstr == instr)
10430 {
10431 /* Check that fromstr and tostr have the same number of
10432 * (multi-byte) characters. Done only once when a character
10433 * of instr doesn't appear in fromstr. */
10434 first = FALSE;
10435 for (p = tostr; *p != NUL; p += tolen)
10436 {
10437 tolen = mb_ptr2len_check(p);
10438 --idx;
10439 }
10440 if (idx != 0)
10441 goto error;
10442 }
10443
10444 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000010445 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010446 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010447
10448 instr += inlen;
10449 }
10450 else
10451#endif
10452 {
10453 /* When not using multi-byte chars we can do it faster. */
10454 p = vim_strchr(fromstr, *instr);
10455 if (p != NULL)
10456 ga_append(&ga, tostr[p - fromstr]);
10457 else
10458 ga_append(&ga, *instr);
10459 ++instr;
10460 }
10461 }
10462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010464}
10465
10466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * "type(expr)" function
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010471 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010474 int n;
10475
10476 switch (argvars[0].v_type)
10477 {
10478 case VAR_NUMBER: n = 0; break;
10479 case VAR_STRING: n = 1; break;
10480 case VAR_FUNC: n = 2; break;
10481 case VAR_LIST: n = 3; break;
10482 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
10483 }
10484 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485}
10486
10487/*
10488 * "virtcol(string)" function
10489 */
10490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010492 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 colnr_T vcol = 0;
10496 pos_T *fp;
10497
10498 fp = var2fpos(&argvars[0], FALSE);
10499 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
10500 {
10501 getvvcol(curwin, fp, NULL, NULL, &vcol);
10502 ++vcol;
10503 }
10504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506}
10507
10508/*
10509 * "visualmode()" function
10510 */
10511/*ARGSUSED*/
10512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517#ifdef FEAT_VISUAL
10518 char_u str[2];
10519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 str[0] = curbuf->b_visual_mode_eval;
10522 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524
10525 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010526 if ((argvars[0].v_type == VAR_NUMBER
10527 && argvars[0].vval.v_number != 0)
10528 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 curbuf->b_visual_mode_eval = NUL;
10531#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533#endif
10534}
10535
10536/*
10537 * "winbufnr(nr)" function
10538 */
10539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010541 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543{
10544 win_T *wp;
10545
10546 wp = find_win_by_nr(&argvars[0]);
10547 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551}
10552
10553/*
10554 * "wincol()" function
10555 */
10556/*ARGSUSED*/
10557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010559 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564}
10565
10566/*
10567 * "winheight(nr)" function
10568 */
10569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010571 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
10574 win_T *wp;
10575
10576 wp = find_win_by_nr(&argvars[0]);
10577 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
10583/*
10584 * "winline()" function
10585 */
10586/*ARGSUSED*/
10587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588f_winline(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{
10592 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594}
10595
10596/*
10597 * "winnr()" function
10598 */
10599/* ARGSUSED */
10600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010602 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
10605 int nr = 1;
10606#ifdef FEAT_WINDOWS
10607 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010608 win_T *twin = curwin;
10609 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010611 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010614 if (STRCMP(arg, "$") == 0)
10615 twin = lastwin;
10616 else if (STRCMP(arg, "#") == 0)
10617 {
10618 twin = prevwin;
10619 if (prevwin == NULL)
10620 nr = 0;
10621 }
10622 else
10623 {
10624 EMSG2(_(e_invexpr2), arg);
10625 nr = 0;
10626 }
10627 }
10628
10629 if (nr > 0)
10630 for (wp = firstwin; wp != twin; wp = wp->w_next)
10631 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634}
10635
10636/*
10637 * "winrestcmd()" function
10638 */
10639/* ARGSUSED */
10640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010642 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644{
10645#ifdef FEAT_WINDOWS
10646 win_T *wp;
10647 int winnr = 1;
10648 garray_T ga;
10649 char_u buf[50];
10650
10651 ga_init2(&ga, (int)sizeof(char), 70);
10652 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10653 {
10654 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
10655 ga_concat(&ga, buf);
10656# ifdef FEAT_VERTSPLIT
10657 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
10658 ga_concat(&ga, buf);
10659# endif
10660 ++winnr;
10661 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000010662 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669}
10670
10671/*
10672 * "winwidth(nr)" function
10673 */
10674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010676 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 win_T *wp;
10680
10681 wp = find_win_by_nr(&argvars[0]);
10682 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 else
10685#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689#endif
10690}
10691
10692 static win_T *
10693find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010694 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
10696#ifdef FEAT_WINDOWS
10697 win_T *wp;
10698#endif
10699 int nr;
10700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702
10703#ifdef FEAT_WINDOWS
10704 if (nr == 0)
10705 return curwin;
10706
10707 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10708 if (--nr <= 0)
10709 break;
10710 return wp;
10711#else
10712 if (nr == 0 || nr == 1)
10713 return curwin;
10714 return NULL;
10715#endif
10716}
10717
10718/*
10719 * Translate a String variable into a position.
10720 */
10721 static pos_T *
10722var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010723 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 int lnum; /* TRUE when $ is last line */
10725{
10726 char_u *name;
10727 static pos_T pos;
10728 pos_T *pp;
10729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 if (name[0] == '.') /* cursor */
10732 return &curwin->w_cursor;
10733 if (name[0] == '\'') /* mark */
10734 {
10735 pp = getmark(name[1], FALSE);
10736 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
10737 return NULL;
10738 return pp;
10739 }
10740 if (name[0] == '$') /* last column or line */
10741 {
10742 if (lnum)
10743 {
10744 pos.lnum = curbuf->b_ml.ml_line_count;
10745 pos.col = 0;
10746 }
10747 else
10748 {
10749 pos.lnum = curwin->w_cursor.lnum;
10750 pos.col = (colnr_T)STRLEN(ml_get_curline());
10751 }
10752 return &pos;
10753 }
10754 return NULL;
10755}
10756
10757/*
10758 * Get the length of an environment variable name.
10759 * Advance "arg" to the first character after the name.
10760 * Return 0 for error.
10761 */
10762 static int
10763get_env_len(arg)
10764 char_u **arg;
10765{
10766 char_u *p;
10767 int len;
10768
10769 for (p = *arg; vim_isIDc(*p); ++p)
10770 ;
10771 if (p == *arg) /* no name found */
10772 return 0;
10773
10774 len = (int)(p - *arg);
10775 *arg = p;
10776 return len;
10777}
10778
10779/*
10780 * Get the length of the name of a function or internal variable.
10781 * "arg" is advanced to the first non-white character after the name.
10782 * Return 0 if something is wrong.
10783 */
10784 static int
10785get_id_len(arg)
10786 char_u **arg;
10787{
10788 char_u *p;
10789 int len;
10790
10791 /* Find the end of the name. */
10792 for (p = *arg; eval_isnamec(*p); ++p)
10793 ;
10794 if (p == *arg) /* no name found */
10795 return 0;
10796
10797 len = (int)(p - *arg);
10798 *arg = skipwhite(p);
10799
10800 return len;
10801}
10802
10803/*
10804 * Get the length of the name of a function.
10805 * "arg" is advanced to the first non-white character after the name.
10806 * Return 0 if something is wrong.
10807 * If the name contains 'magic' {}'s, expand them and return the
10808 * expanded name in an allocated string via 'alias' - caller must free.
10809 */
10810 static int
10811get_func_len(arg, alias, evaluate)
10812 char_u **arg;
10813 char_u **alias;
10814 int evaluate;
10815{
10816 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 char_u *p;
10818 char_u *expr_start;
10819 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820
10821 *alias = NULL; /* default to no alias */
10822
10823 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
10824 && (*arg)[2] == (int)KE_SNR)
10825 {
10826 /* hard coded <SNR>, already translated */
10827 *arg += 3;
10828 return get_id_len(arg) + 3;
10829 }
10830 len = eval_fname_script(*arg);
10831 if (len > 0)
10832 {
10833 /* literal "<SID>", "s:" or "<SNR>" */
10834 *arg += len;
10835 }
10836
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 if (expr_start != NULL)
10842 {
10843 char_u *temp_string;
10844
10845 if (!evaluate)
10846 {
10847 len += (int)(p - *arg);
10848 *arg = skipwhite(p);
10849 return len;
10850 }
10851
10852 /*
10853 * Include any <SID> etc in the expanded string:
10854 * Thus the -len here.
10855 */
10856 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
10857 if (temp_string == NULL)
10858 return 0;
10859 *alias = temp_string;
10860 *arg = skipwhite(p);
10861 return (int)STRLEN(temp_string);
10862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
10864 len += get_id_len(arg);
10865 if (len == 0)
10866 EMSG2(_(e_invexpr2), *arg);
10867
10868 return len;
10869}
10870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871/*
10872 * Find the end of a variable or function name, taking care of magic braces.
10873 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
10874 * start and end of the first magic braces item.
10875 * Return a pointer to just after the name. Equal to "arg" if there is no
10876 * valid name.
10877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 char_u *arg;
10881 char_u **expr_start;
10882 char_u **expr_end;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 int incl_br; /* Include [] indexes */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 int mb_nest = 0;
10886 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 char_u *p;
10888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 *expr_start = NULL;
10892 *expr_end = NULL;
10893 }
10894
10895 for (p = arg; *p != NUL
10896 && (eval_isnamec(*p)
10897 || (*p == '[' && incl_br)
10898 || mb_nest != 0
10899 || br_nest != 0); ++p)
10900 {
10901 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903 if (*p == '[')
10904 ++br_nest;
10905 else if (*p == ']')
10906 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 if (*p == '{')
10911 {
10912 mb_nest++;
10913 if (expr_start != NULL && *expr_start == NULL)
10914 *expr_start = p;
10915 }
10916 else if (*p == '}')
10917 {
10918 mb_nest--;
10919 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
10920 *expr_end = p;
10921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 }
10924
10925 return p;
10926}
10927
10928/*
10929 * Return TRUE if character "c" can be used in a variable or function name.
10930 */
10931 static int
10932eval_isnamec(c)
10933 int c;
10934{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == '{' || c == '}');
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936}
10937
10938/*
10939 * Find a v: variable.
10940 * Return it's index, or -1 if not found.
10941 */
10942 static int
10943find_vim_var(name, len)
10944 char_u *name;
10945 int len; /* length of "name" */
10946{
10947 char_u *vname;
10948 int vlen;
10949 int i;
10950
10951 /*
10952 * Ignore "v:" for old built-in variables, require it for new ones.
10953 */
10954 if (name[0] == 'v' && name[1] == ':')
10955 {
10956 vname = name + 2;
10957 vlen = len - 2;
10958 }
10959 else
10960 {
10961 vname = name;
10962 vlen = len;
10963 }
10964 for (i = 0; i < VV_LEN; ++i)
10965 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
10966 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
10967 return i;
10968 return -1;
10969}
10970
10971/*
10972 * Set number v: variable to "val".
10973 */
10974 void
10975set_vim_var_nr(idx, val)
10976 int idx;
10977 long val;
10978{
10979 vimvars[idx].val = (char_u *)val;
10980}
10981
10982/*
10983 * Get number v: variable value;
10984 */
10985 long
10986get_vim_var_nr(idx)
10987 int idx;
10988{
10989 return (long)vimvars[idx].val;
10990}
10991
10992/*
10993 * Set v:count, v:count1 and v:prevcount.
10994 */
10995 void
10996set_vcount(count, count1)
10997 long count;
10998 long count1;
10999{
11000 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
11001 vimvars[VV_COUNT].val = (char_u *)count;
11002 vimvars[VV_COUNT1].val = (char_u *)count1;
11003}
11004
11005/*
11006 * Set string v: variable to a copy of "val".
11007 */
11008 void
11009set_vim_var_string(idx, val, len)
11010 int idx;
11011 char_u *val;
11012 int len; /* length of "val" to use or -1 (whole string) */
11013{
11014 vim_free(vimvars[idx].val);
11015 if (val == NULL)
11016 vimvars[idx].val = NULL;
11017 else if (len == -1)
11018 vimvars[idx].val = vim_strsave(val);
11019 else
11020 vimvars[idx].val = vim_strnsave(val, len);
11021}
11022
11023/*
11024 * Set v:register if needed.
11025 */
11026 void
11027set_reg_var(c)
11028 int c;
11029{
11030 char_u regname;
11031
11032 if (c == 0 || c == ' ')
11033 regname = '"';
11034 else
11035 regname = c;
11036 /* Avoid free/alloc when the value is already right. */
11037 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
11038 set_vim_var_string(VV_REG, &regname, 1);
11039}
11040
11041/*
11042 * Get or set v:exception. If "oldval" == NULL, return the current value.
11043 * Otherwise, restore the value to "oldval" and return NULL.
11044 * Must always be called in pairs to save and restore v:exception! Does not
11045 * take care of memory allocations.
11046 */
11047 char_u *
11048v_exception(oldval)
11049 char_u *oldval;
11050{
11051 if (oldval == NULL)
11052 return vimvars[VV_EXCEPTION].val;
11053
11054 vimvars[VV_EXCEPTION].val = oldval;
11055 return NULL;
11056}
11057
11058/*
11059 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
11060 * Otherwise, restore the value to "oldval" and return NULL.
11061 * Must always be called in pairs to save and restore v:throwpoint! Does not
11062 * take care of memory allocations.
11063 */
11064 char_u *
11065v_throwpoint(oldval)
11066 char_u *oldval;
11067{
11068 if (oldval == NULL)
11069 return vimvars[VV_THROWPOINT].val;
11070
11071 vimvars[VV_THROWPOINT].val = oldval;
11072 return NULL;
11073}
11074
11075#if defined(FEAT_AUTOCMD) || defined(PROTO)
11076/*
11077 * Set v:cmdarg.
11078 * If "eap" != NULL, use "eap" to generate the value and return the old value.
11079 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
11080 * Must always be called in pairs!
11081 */
11082 char_u *
11083set_cmdarg(eap, oldarg)
11084 exarg_T *eap;
11085 char_u *oldarg;
11086{
11087 char_u *oldval;
11088 char_u *newval;
11089 unsigned len;
11090
11091 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000011092 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000011094 vim_free(oldval);
11095 vimvars[VV_CMDARG].val = oldarg;
11096 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 }
11098
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000011099 if (eap->force_bin == FORCE_BIN)
11100 len = 6;
11101 else if (eap->force_bin == FORCE_NOBIN)
11102 len = 8;
11103 else
11104 len = 0;
11105 if (eap->force_ff != 0)
11106 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
11107# ifdef FEAT_MBYTE
11108 if (eap->force_enc != 0)
11109 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
11110# endif
11111
11112 newval = alloc(len + 1);
11113 if (newval == NULL)
11114 return NULL;
11115
11116 if (eap->force_bin == FORCE_BIN)
11117 sprintf((char *)newval, " ++bin");
11118 else if (eap->force_bin == FORCE_NOBIN)
11119 sprintf((char *)newval, " ++nobin");
11120 else
11121 *newval = NUL;
11122 if (eap->force_ff != 0)
11123 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
11124 eap->cmd + eap->force_ff);
11125# ifdef FEAT_MBYTE
11126 if (eap->force_enc != 0)
11127 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
11128 eap->cmd + eap->force_enc);
11129# endif
11130 vimvars[VV_CMDARG].val = newval;
11131 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132}
11133#endif
11134
11135/*
11136 * Get the value of internal variable "name".
11137 * Return OK or FAIL.
11138 */
11139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 char_u *name;
11142 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
11145 int ret = OK;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011146 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 VAR v;
11148 int cc;
11149 int i;
11150
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011151 tv.v_type = VAR_UNKNOWN;
11152
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 /* truncate the name, so that we can use strcmp() */
11154 cc = name[len];
11155 name[len] = NUL;
11156
11157 /*
11158 * Check for "b:changedtick".
11159 */
11160 if (STRCMP(name, "b:changedtick") == 0)
11161 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011162 tv.v_type = VAR_NUMBER;
11163 tv.vval.v_number = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 }
11165
11166 /*
11167 * Check for built-in v: variables.
11168 */
11169 else if ((i = find_vim_var(name, len)) >= 0)
11170 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011171 tv.v_type = vimvars[i].type;
11172 if (tv.v_type == VAR_NUMBER)
11173 tv.vval.v_number = (long)vimvars[i].val;
11174 else
11175 tv.vval.v_string = vimvars[i].val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 }
11177
11178 /*
11179 * Check for user-defined variables.
11180 */
11181 else
11182 {
11183 v = find_var(name, FALSE);
11184 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011185 tv = v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 }
11187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011188 if (tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 if (rettv != NULL)
11191 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 ret = FAIL;
11193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 else if (rettv != NULL)
11195 copy_tv(&tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196
11197 name[len] = cc;
11198
11199 return ret;
11200}
11201
11202/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011203 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
11204 * value).
11205 */
11206 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011208{
11209 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
11210}
11211
11212/*
11213 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 * The string "s" must have been allocated, it is consumed.
11215 * Return NULL for out of memory, the variable otherwise.
11216 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011217 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 char_u *s;
11220{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 rettv = alloc_tv();
11224 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->v_type = VAR_STRING;
11227 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 }
11229 else
11230 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232}
11233
11234/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011235 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 */
11237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011238free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011239 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
11241 if (varp != NULL)
11242 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011243 switch (varp->v_type)
11244 {
11245 case VAR_STRING:
11246 case VAR_FUNC:
11247 vim_free(varp->vval.v_string);
11248 break;
11249 case VAR_LIST:
11250 list_unref(varp->vval.v_list);
11251 break;
11252 default:
11253 break;
11254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 vim_free(varp);
11256 }
11257}
11258
11259/*
11260 * Free the memory for a variable value and set the value to NULL or 0.
11261 */
11262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265{
11266 if (varp != NULL)
11267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011270 case VAR_STRING:
11271 case VAR_FUNC:
11272 vim_free(varp->vval.v_string);
11273 varp->vval.v_string = NULL;
11274 break;
11275 case VAR_LIST:
11276 list_unref(varp->vval.v_list);
11277 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011279 varp->vval.v_number = 0;
11280 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 case VAR_UNKNOWN:
11282 break;
11283 default:
11284 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 }
11287}
11288
11289/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 * Set the value of a variable to NULL without freeing items.
11291 */
11292 static void
11293init_tv(varp)
11294 typeval *varp;
11295{
11296 if (varp != NULL)
11297 vim_memset(varp, 0, sizeof(typeval));
11298}
11299
11300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 * Get the number value of a variable.
11302 * If it is a String variable, uses vim_str2nr().
11303 */
11304 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011306 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011310 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011312 case VAR_NUMBER:
11313 n = (long)(varp->vval.v_number);
11314 break;
11315 case VAR_FUNC:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011316 EMSG(_("E703: Using function reference as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011317 break;
11318 case VAR_STRING:
11319 if (varp->vval.v_string != NULL)
11320 vim_str2nr(varp->vval.v_string, NULL, NULL,
11321 TRUE, TRUE, &n, NULL);
11322 break;
11323 default:
11324 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011326 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327}
11328
11329/*
11330 * Get the lnum from the first argument. Also accepts ".", "$", etc.
11331 */
11332 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011334 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 linenr_T lnum;
11338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 if (lnum == 0) /* no valid number, try using line() */
11341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv.v_type = VAR_NUMBER;
11343 f_line(argvars, &rettv);
11344 lnum = rettv.vval.v_number;
11345 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 }
11347 return lnum;
11348}
11349
11350/*
11351 * Get the string value of a variable.
11352 * If it is a Number variable, the number is converted into a string.
11353 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
11354 * get_var_string_buf() uses a given buffer.
11355 * If the String variable has never been set, return an empty string.
11356 * Never returns NULL;
11357 */
11358 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011360 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362 static char_u mybuf[NUMBUFLEN];
11363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365}
11366
11367 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011369 typeval *varp;
11370 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011372 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011374 case VAR_NUMBER:
11375 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
11376 return buf;
11377 case VAR_FUNC:
11378 EMSG(_("E99: using Funcref as a String"));
11379 break;
11380 case VAR_LIST:
11381 EMSG(_("E99: using List as a String"));
11382 break;
11383 case VAR_STRING:
11384 if (varp->vval.v_string != NULL)
11385 return varp->vval.v_string;
11386 break;
11387 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011391 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392}
11393
11394/*
11395 * Find variable "name" in the list of variables.
11396 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011397 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 */
11399 static VAR
11400find_var(name, writing)
11401 char_u *name;
11402 int writing;
11403{
11404 int i;
11405 char_u *varname;
11406 garray_T *gap;
11407
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 if (name[0] == 'a' && name[1] == ':')
11409 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011410 /* Function arguments "a:".
11411 * NOTE: We use a typecast, because function arguments don't have a
11412 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 if (writing)
11414 {
11415 EMSG2(_(e_readonlyvar), name);
11416 return NULL;
11417 }
11418 name += 2;
11419 if (current_funccal == NULL)
11420 return NULL;
11421 if (VIM_ISDIGIT(*name))
11422 {
11423 i = atol((char *)name);
11424 if (i == 0) /* a:0 */
11425 return &current_funccal->a0_var;
11426 i += current_funccal->func->args.ga_len;
11427 if (i > current_funccal->argcount) /* a:999 */
11428 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011429 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 }
11431 if (STRCMP(name, "firstline") == 0)
11432 return &(current_funccal->firstline);
11433 if (STRCMP(name, "lastline") == 0)
11434 return &(current_funccal->lastline);
11435 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
11436 if (STRCMP(name, ((char_u **)
11437 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011438 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 return NULL;
11440 }
11441
11442 gap = find_var_ga(name, &varname);
11443 if (gap == NULL)
11444 return NULL;
11445 return find_var_in_ga(gap, varname);
11446}
11447
11448 static VAR
11449find_var_in_ga(gap, varname)
11450 garray_T *gap;
11451 char_u *varname;
11452{
11453 int i;
11454
11455 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011456 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
11457 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 break;
11459 if (i < 0)
11460 return NULL;
11461 return &VAR_GAP_ENTRY(i, gap);
11462}
11463
11464/*
11465 * Find the growarray and start of name without ':' for a variable name.
11466 */
11467 static garray_T *
11468find_var_ga(name, varname)
11469 char_u *name;
11470 char_u **varname;
11471{
11472 if (name[1] != ':')
11473 {
11474 /* If not "x:name" there must not be any ":" in the name. */
11475 if (vim_strchr(name, ':') != NULL)
11476 return NULL;
11477 *varname = name;
11478 if (current_funccal == NULL)
11479 return &variables; /* global variable */
11480 return &current_funccal->l_vars; /* local function variable */
11481 }
11482 *varname = name + 2;
11483 if (*name == 'b') /* buffer variable */
11484 return &curbuf->b_vars;
11485 if (*name == 'w') /* window variable */
11486 return &curwin->w_vars;
11487 if (*name == 'g') /* global variable */
11488 return &variables;
11489 if (*name == 'l' && current_funccal != NULL)/* local function variable */
11490 return &current_funccal->l_vars;
11491 if (*name == 's' /* script variable */
11492 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
11493 return &SCRIPT_VARS(current_SID);
11494 return NULL;
11495}
11496
11497/*
11498 * Get the string value of a (global/local) variable.
11499 * Returns NULL when it doesn't exist.
11500 */
11501 char_u *
11502get_var_value(name)
11503 char_u *name;
11504{
11505 VAR v;
11506
11507 v = find_var(name, FALSE);
11508 if (v == NULL)
11509 return NULL;
11510 return get_var_string(v);
11511}
11512
11513/*
11514 * Allocate a new growarry for a sourced script. It will be used while
11515 * sourcing this script and when executing functions defined in the script.
11516 */
11517 void
11518new_script_vars(id)
11519 scid_T id;
11520{
11521 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
11522 {
11523 while (ga_scripts.ga_len < id)
11524 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011525 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 }
11528 }
11529}
11530
11531/*
11532 * Initialize internal variables for use.
11533 */
11534 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011535vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 garray_T *gap;
11537{
11538 ga_init2(gap, (int)sizeof(var), 4);
11539}
11540
11541/*
11542 * Clean up a list of internal variables.
11543 */
11544 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011545vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 garray_T *gap;
11547{
11548 int i;
11549
11550 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011551 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 ga_clear(gap);
11553}
11554
11555 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011556clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 VAR v;
11558{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011559 vim_free(v->v_name);
11560 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562}
11563
11564/*
11565 * List the value of one internal variable.
11566 */
11567 static void
11568list_one_var(v, prefix)
11569 VAR v;
11570 char_u *prefix;
11571{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011572 char_u *tofree;
11573 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011575
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011576 s = tv2string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011577 list_one_var_a(prefix, v->v_name, v->tv.v_type,
11578 s == NULL ? (char_u *)"" : s);
11579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580}
11581
11582/*
11583 * List the value of one "v:" variable.
11584 */
11585 static void
11586list_vim_var(i)
11587 int i; /* index in vimvars[] */
11588{
11589 char_u *p;
11590 char_u numbuf[NUMBUFLEN];
11591
11592 if (vimvars[i].type == VAR_NUMBER)
11593 {
11594 p = numbuf;
11595 sprintf((char *)p, "%ld", (long)vimvars[i].val);
11596 }
11597 else if (vimvars[i].val == NULL)
11598 p = (char_u *)"";
11599 else
11600 p = vimvars[i].val;
11601 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
11602 vimvars[i].type, p);
11603}
11604
11605 static void
11606list_one_var_a(prefix, name, type, string)
11607 char_u *prefix;
11608 char_u *name;
11609 int type;
11610 char_u *string;
11611{
11612 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
11613 if (name != NULL) /* "a:" vars don't have a name stored */
11614 msg_puts(name);
11615 msg_putchar(' ');
11616 msg_advance(22);
11617 if (type == VAR_NUMBER)
11618 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011619 else if (type == VAR_FUNC)
11620 msg_putchar('*');
11621 else if (type == VAR_LIST)
11622 {
11623 msg_putchar('[');
11624 if (*string == '[')
11625 ++string;
11626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 else
11628 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011629
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631
11632 if (type == VAR_FUNC)
11633 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634}
11635
11636/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 * If the variable already exists, the value is updated.
11639 * Otherwise the variable is created.
11640 */
11641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 typeval *tv;
11645 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646{
11647 int i;
11648 VAR v;
11649 char_u *varname;
11650 garray_T *gap;
11651
11652 /*
11653 * Handle setting internal v: variables.
11654 */
11655 i = find_vim_var(name, (int)STRLEN(name));
11656 if (i >= 0)
11657 {
11658 if (vimvars[i].flags & VV_RO)
11659 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011660 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
11661 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 else
11663 {
11664 if (vimvars[i].type == VAR_STRING)
11665 {
11666 vim_free(vimvars[i].val);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 if (copy || tv->v_type != VAR_STRING)
11668 vimvars[i].val = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011669 else
11670 {
11671 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 vimvars[i].val = tv->vval.v_string;
11673 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 }
11676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677 vimvars[i].val = (char_u *)get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 }
11679 return;
11680 }
11681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011683 {
11684 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
11685 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
11686 ? name[2] : name[0]))
11687 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011688 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011689 return;
11690 }
11691 if (function_exists(name))
11692 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011693 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011694 return;
11695 }
11696 }
11697
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 v = find_var(name, TRUE);
11699 if (v != NULL) /* existing variable, only need to free string */
11700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011702 && !((v->tv.v_type == VAR_STRING
11703 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 && (tv->v_type == VAR_STRING
11705 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011706 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011707 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011708 return;
11709 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 }
11712 else /* add a new variable */
11713 {
11714 gap = find_var_ga(name, &varname);
11715 if (gap == NULL) /* illegal name */
11716 {
11717 EMSG2(_("E461: Illegal variable name: %s"), name);
11718 return;
11719 }
11720
11721 /* Try to use an empty entry */
11722 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011723 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 break;
11725 if (i < 0) /* need to allocate more room */
11726 {
11727 if (ga_grow(gap, 1) == FAIL)
11728 return;
11729 i = gap->ga_len;
11730 }
11731 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011732 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 return;
11734 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 if (copy || tv->v_type == VAR_NUMBER)
11738 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011739 else
11740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741 v->tv = *tv;
11742 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744}
11745
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011746/*
11747 * Copy the values from typeval "from" to typeval "to".
11748 * When needed allocates string or increases reference count.
11749 * Does not make a copy of a list!
11750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011753 typeval *from;
11754 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011756 to->v_type = from->v_type;
11757 switch (from->v_type)
11758 {
11759 case VAR_NUMBER:
11760 to->vval.v_number = from->vval.v_number;
11761 break;
11762 case VAR_STRING:
11763 case VAR_FUNC:
11764 if (from->vval.v_string == NULL)
11765 to->vval.v_string = NULL;
11766 else
11767 to->vval.v_string = vim_strsave(from->vval.v_string);
11768 break;
11769 case VAR_LIST:
11770 if (from->vval.v_list == NULL)
11771 to->vval.v_list = NULL;
11772 else
11773 {
11774 to->vval.v_list = from->vval.v_list;
11775 ++to->vval.v_list->lv_refcount;
11776 }
11777 break;
11778 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011780 break;
11781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782}
11783
11784/*
11785 * ":echo expr1 ..." print each argument separated with a space, add a
11786 * newline at the end.
11787 * ":echon expr1 ..." print each argument plain.
11788 */
11789 void
11790ex_echo(eap)
11791 exarg_T *eap;
11792{
11793 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011795 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 char_u *p;
11797 int needclr = TRUE;
11798 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011799 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800
11801 if (eap->skip)
11802 ++emsg_skip;
11803 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
11804 {
11805 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 {
11808 /*
11809 * Report the invalid expression unless the expression evaluation
11810 * has been cancelled due to an aborting error, an interrupt, or an
11811 * exception.
11812 */
11813 if (!aborting())
11814 EMSG2(_(e_invexpr2), p);
11815 break;
11816 }
11817 if (!eap->skip)
11818 {
11819 if (atstart)
11820 {
11821 atstart = FALSE;
11822 /* Call msg_start() after eval1(), evaluating the expression
11823 * may cause a message to appear. */
11824 if (eap->cmdidx == CMD_echo)
11825 msg_start();
11826 }
11827 else if (eap->cmdidx == CMD_echo)
11828 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011829 for (p = tv2string(&rettv, &tofree, numbuf);
11830 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 if (*p == '\n' || *p == '\r' || *p == TAB)
11832 {
11833 if (*p != TAB && needclr)
11834 {
11835 /* remove any text still there from the command */
11836 msg_clr_eos();
11837 needclr = FALSE;
11838 }
11839 msg_putchar_attr(*p, echo_attr);
11840 }
11841 else
11842 {
11843#ifdef FEAT_MBYTE
11844 if (has_mbyte)
11845 {
11846 int i = (*mb_ptr2len_check)(p);
11847
11848 (void)msg_outtrans_len_attr(p, i, echo_attr);
11849 p += i - 1;
11850 }
11851 else
11852#endif
11853 (void)msg_outtrans_len_attr(p, 1, echo_attr);
11854 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 arg = skipwhite(arg);
11859 }
11860 eap->nextcmd = check_nextcmd(arg);
11861
11862 if (eap->skip)
11863 --emsg_skip;
11864 else
11865 {
11866 /* remove text that may still be there from the command */
11867 if (needclr)
11868 msg_clr_eos();
11869 if (eap->cmdidx == CMD_echo)
11870 msg_end();
11871 }
11872}
11873
11874/*
11875 * ":echohl {name}".
11876 */
11877 void
11878ex_echohl(eap)
11879 exarg_T *eap;
11880{
11881 int id;
11882
11883 id = syn_name2id(eap->arg);
11884 if (id == 0)
11885 echo_attr = 0;
11886 else
11887 echo_attr = syn_id2attr(id);
11888}
11889
11890/*
11891 * ":execute expr1 ..." execute the result of an expression.
11892 * ":echomsg expr1 ..." Print a message
11893 * ":echoerr expr1 ..." Print an error
11894 * Each gets spaces around each argument and a newline at the end for
11895 * echo commands
11896 */
11897 void
11898ex_execute(eap)
11899 exarg_T *eap;
11900{
11901 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 int ret = OK;
11904 char_u *p;
11905 garray_T ga;
11906 int len;
11907 int save_did_emsg;
11908
11909 ga_init2(&ga, 1, 80);
11910
11911 if (eap->skip)
11912 ++emsg_skip;
11913 while (*arg != NUL && *arg != '|' && *arg != '\n')
11914 {
11915 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 {
11918 /*
11919 * Report the invalid expression unless the expression evaluation
11920 * has been cancelled due to an aborting error, an interrupt, or an
11921 * exception.
11922 */
11923 if (!aborting())
11924 EMSG2(_(e_invexpr2), p);
11925 ret = FAIL;
11926 break;
11927 }
11928
11929 if (!eap->skip)
11930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 len = (int)STRLEN(p);
11933 if (ga_grow(&ga, len + 2) == FAIL)
11934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 ret = FAIL;
11937 break;
11938 }
11939 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 ga.ga_len += len;
11943 }
11944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 arg = skipwhite(arg);
11947 }
11948
11949 if (ret != FAIL && ga.ga_data != NULL)
11950 {
11951 if (eap->cmdidx == CMD_echomsg)
11952 MSG_ATTR(ga.ga_data, echo_attr);
11953 else if (eap->cmdidx == CMD_echoerr)
11954 {
11955 /* We don't want to abort following commands, restore did_emsg. */
11956 save_did_emsg = did_emsg;
11957 EMSG((char_u *)ga.ga_data);
11958 if (!force_abort)
11959 did_emsg = save_did_emsg;
11960 }
11961 else if (eap->cmdidx == CMD_execute)
11962 do_cmdline((char_u *)ga.ga_data,
11963 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
11964 }
11965
11966 ga_clear(&ga);
11967
11968 if (eap->skip)
11969 --emsg_skip;
11970
11971 eap->nextcmd = check_nextcmd(arg);
11972}
11973
11974/*
11975 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
11976 * "arg" points to the "&" or '+' when called, to "option" when returning.
11977 * Returns NULL when no option name found. Otherwise pointer to the char
11978 * after the option name.
11979 */
11980 static char_u *
11981find_option_end(arg, opt_flags)
11982 char_u **arg;
11983 int *opt_flags;
11984{
11985 char_u *p = *arg;
11986
11987 ++p;
11988 if (*p == 'g' && p[1] == ':')
11989 {
11990 *opt_flags = OPT_GLOBAL;
11991 p += 2;
11992 }
11993 else if (*p == 'l' && p[1] == ':')
11994 {
11995 *opt_flags = OPT_LOCAL;
11996 p += 2;
11997 }
11998 else
11999 *opt_flags = 0;
12000
12001 if (!ASCII_ISALPHA(*p))
12002 return NULL;
12003 *arg = p;
12004
12005 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
12006 p += 4; /* termcap option */
12007 else
12008 while (ASCII_ISALPHA(*p))
12009 ++p;
12010 return p;
12011}
12012
12013/*
12014 * ":function"
12015 */
12016 void
12017ex_function(eap)
12018 exarg_T *eap;
12019{
12020 char_u *theline;
12021 int j;
12022 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 char_u *name = NULL;
12025 char_u *p;
12026 char_u *arg;
12027 garray_T newargs;
12028 garray_T newlines;
12029 int varargs = FALSE;
12030 int mustend = FALSE;
12031 int flags = 0;
12032 ufunc_T *fp;
12033 int indent;
12034 int nesting;
12035 char_u *skip_until = NULL;
12036 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012037 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038
12039 /*
12040 * ":function" without argument: list functions.
12041 */
12042 if (ends_excmd(*eap->arg))
12043 {
12044 if (!eap->skip)
12045 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
12046 list_func_head(fp, FALSE);
12047 eap->nextcmd = check_nextcmd(eap->arg);
12048 return;
12049 }
12050
12051 p = eap->arg;
12052 name = trans_function_name(&p, eap->skip, FALSE);
12053 if (name == NULL && !eap->skip)
12054 {
12055 /*
12056 * Return on an invalid expression in braces, unless the expression
12057 * evaluation has been cancelled due to an aborting error, an
12058 * interrupt, or an exception.
12059 */
12060 if (!aborting())
12061 return;
12062 else
12063 eap->skip = TRUE;
12064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 /* An error in a function call during evaluation of an expression in magic
12066 * braces should not cause the function not to be defined. */
12067 saved_did_emsg = did_emsg;
12068 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069
12070 /*
12071 * ":function func" with only function name: list function.
12072 */
12073 if (vim_strchr(p, '(') == NULL)
12074 {
12075 if (!ends_excmd(*skipwhite(p)))
12076 {
12077 EMSG(_(e_trailing));
12078 goto erret_name;
12079 }
12080 eap->nextcmd = check_nextcmd(p);
12081 if (eap->nextcmd != NULL)
12082 *p = NUL;
12083 if (!eap->skip && !got_int)
12084 {
12085 fp = find_func(name);
12086 if (fp != NULL)
12087 {
12088 list_func_head(fp, TRUE);
12089 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
12090 {
12091 msg_putchar('\n');
12092 msg_outnum((long)(j + 1));
12093 if (j < 9)
12094 msg_putchar(' ');
12095 if (j < 99)
12096 msg_putchar(' ');
12097 msg_prt_line(FUNCLINE(fp, j));
12098 out_flush(); /* show a line at a time */
12099 ui_breakcheck();
12100 }
12101 if (!got_int)
12102 {
12103 msg_putchar('\n');
12104 msg_puts((char_u *)" endfunction");
12105 }
12106 }
12107 else
12108 EMSG2(_("E123: Undefined function: %s"), eap->arg);
12109 }
12110 goto erret_name;
12111 }
12112
12113 /*
12114 * ":function name(arg1, arg2)" Define function.
12115 */
12116 p = skipwhite(p);
12117 if (*p != '(')
12118 {
12119 if (!eap->skip)
12120 {
12121 EMSG2(_("E124: Missing '(': %s"), eap->arg);
12122 goto erret_name;
12123 }
12124 /* attempt to continue by skipping some text */
12125 if (vim_strchr(p, '(') != NULL)
12126 p = vim_strchr(p, '(');
12127 }
12128 p = skipwhite(p + 1);
12129
12130 ga_init2(&newargs, (int)sizeof(char_u *), 3);
12131 ga_init2(&newlines, (int)sizeof(char_u *), 3);
12132
12133 /*
12134 * Isolate the arguments: "arg1, arg2, ...)"
12135 */
12136 while (*p != ')')
12137 {
12138 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
12139 {
12140 varargs = TRUE;
12141 p += 3;
12142 mustend = TRUE;
12143 }
12144 else
12145 {
12146 arg = p;
12147 while (ASCII_ISALNUM(*p) || *p == '_')
12148 ++p;
12149 if (arg == p || isdigit(*arg)
12150 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
12151 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
12152 {
12153 if (!eap->skip)
12154 EMSG2(_("E125: Illegal argument: %s"), arg);
12155 break;
12156 }
12157 if (ga_grow(&newargs, 1) == FAIL)
12158 goto erret;
12159 c = *p;
12160 *p = NUL;
12161 arg = vim_strsave(arg);
12162 if (arg == NULL)
12163 goto erret;
12164 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
12165 *p = c;
12166 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 if (*p == ',')
12168 ++p;
12169 else
12170 mustend = TRUE;
12171 }
12172 p = skipwhite(p);
12173 if (mustend && *p != ')')
12174 {
12175 if (!eap->skip)
12176 EMSG2(_(e_invarg2), eap->arg);
12177 break;
12178 }
12179 }
12180 ++p; /* skip the ')' */
12181
12182 /* find extra arguments "range" and "abort" */
12183 for (;;)
12184 {
12185 p = skipwhite(p);
12186 if (STRNCMP(p, "range", 5) == 0)
12187 {
12188 flags |= FC_RANGE;
12189 p += 5;
12190 }
12191 else if (STRNCMP(p, "abort", 5) == 0)
12192 {
12193 flags |= FC_ABORT;
12194 p += 5;
12195 }
12196 else
12197 break;
12198 }
12199
12200 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
12201 EMSG(_(e_trailing));
12202
12203 /*
12204 * Read the body of the function, until ":endfunction" is found.
12205 */
12206 if (KeyTyped)
12207 {
12208 /* Check if the function already exists, don't let the user type the
12209 * whole function before telling him it doesn't work! For a script we
12210 * need to skip the body to be able to find what follows. */
12211 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
12212 EMSG2(_(e_funcexts), name);
12213
12214 msg_putchar('\n'); /* don't overwrite the function name */
12215 cmdline_row = msg_row;
12216 }
12217
12218 indent = 2;
12219 nesting = 0;
12220 for (;;)
12221 {
12222 msg_scroll = TRUE;
12223 need_wait_return = FALSE;
12224 if (eap->getline == NULL)
12225 theline = getcmdline(':', 0L, indent);
12226 else
12227 theline = eap->getline(':', eap->cookie, indent);
12228 if (KeyTyped)
12229 lines_left = Rows - 1;
12230 if (theline == NULL)
12231 {
12232 EMSG(_("E126: Missing :endfunction"));
12233 goto erret;
12234 }
12235
12236 if (skip_until != NULL)
12237 {
12238 /* between ":append" and "." and between ":python <<EOF" and "EOF"
12239 * don't check for ":endfunc". */
12240 if (STRCMP(theline, skip_until) == 0)
12241 {
12242 vim_free(skip_until);
12243 skip_until = NULL;
12244 }
12245 }
12246 else
12247 {
12248 /* skip ':' and blanks*/
12249 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
12250 ;
12251
12252 /* Check for "endfunction" (should be more strict...). */
12253 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
12254 {
12255 vim_free(theline);
12256 break;
12257 }
12258
12259 /* Increase indent inside "if", "while", and "try", decrease
12260 * at "end". */
12261 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
12262 indent -= 2;
12263 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
12264 || STRNCMP(p, "try", 3) == 0)
12265 indent += 2;
12266
12267 /* Check for defining a function inside this function. */
12268 if (STRNCMP(p, "fu", 2) == 0)
12269 {
12270 p = skipwhite(skiptowhite(p));
12271 p += eval_fname_script(p);
12272 if (ASCII_ISALPHA(*p))
12273 {
12274 vim_free(trans_function_name(&p, TRUE, FALSE));
12275 if (*skipwhite(p) == '(')
12276 {
12277 ++nesting;
12278 indent += 2;
12279 }
12280 }
12281 }
12282
12283 /* Check for ":append" or ":insert". */
12284 p = skip_range(p, NULL);
12285 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
12286 || (p[0] == 'i'
12287 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
12288 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
12289 skip_until = vim_strsave((char_u *)".");
12290
12291 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
12292 arg = skipwhite(skiptowhite(p));
12293 if (arg[0] == '<' && arg[1] =='<'
12294 && ((p[0] == 'p' && p[1] == 'y'
12295 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
12296 || (p[0] == 'p' && p[1] == 'e'
12297 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
12298 || (p[0] == 't' && p[1] == 'c'
12299 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
12300 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
12301 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012302 || (p[0] == 'm' && p[1] == 'z'
12303 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 ))
12305 {
12306 /* ":python <<" continues until a dot, like ":append" */
12307 p = skipwhite(arg + 2);
12308 if (*p == NUL)
12309 skip_until = vim_strsave((char_u *)".");
12310 else
12311 skip_until = vim_strsave(p);
12312 }
12313 }
12314
12315 /* Add the line to the function. */
12316 if (ga_grow(&newlines, 1) == FAIL)
12317 goto erret;
12318 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
12319 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 }
12321
12322 /* Don't define the function when skipping commands or when an error was
12323 * detected. */
12324 if (eap->skip || did_emsg)
12325 goto erret;
12326
12327 /*
12328 * If there are no errors, add the function
12329 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012330 v = find_var(name, FALSE);
12331 if (v != NULL && v->tv.v_type == VAR_FUNC)
12332 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012333 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012334 goto erret;
12335 }
12336
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 fp = find_func(name);
12338 if (fp != NULL)
12339 {
12340 if (!eap->forceit)
12341 {
12342 EMSG2(_(e_funcexts), name);
12343 goto erret;
12344 }
12345 if (fp->calls)
12346 {
12347 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
12348 goto erret;
12349 }
12350 /* redefine existing function */
12351 ga_clear_strings(&(fp->args));
12352 ga_clear_strings(&(fp->lines));
12353 vim_free(name);
12354 }
12355 else
12356 {
12357 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
12358 if (fp == NULL)
12359 goto erret;
12360 /* insert the new function in the function list */
12361 fp->next = firstfunc;
12362 firstfunc = fp;
12363 fp->name = name;
12364 }
12365 fp->args = newargs;
12366 fp->lines = newlines;
12367 fp->varargs = varargs;
12368 fp->flags = flags;
12369 fp->calls = 0;
12370 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 vim_free(skip_until);
12373 return;
12374
12375erret:
12376 vim_free(skip_until);
12377 ga_clear_strings(&newargs);
12378 ga_clear_strings(&newlines);
12379erret_name:
12380 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382}
12383
12384/*
12385 * Get a function name, translating "<SID>" and "<SNR>".
12386 * Returns the function name in allocated memory, or NULL for failure.
12387 * Advances "pp" to just after the function name (if no error).
12388 */
12389 static char_u *
12390trans_function_name(pp, skip, internal)
12391 char_u **pp;
12392 int skip; /* only find the end, don't evaluate */
12393 int internal; /* TRUE if internal function name OK */
12394{
12395 char_u *name;
12396 char_u *start;
12397 char_u *end;
12398 int lead;
12399 char_u sid_buf[20];
12400 char_u *temp_string = NULL;
12401 char_u *expr_start, *expr_end;
12402 int len;
12403
12404 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
12405 start = *pp;
12406 lead = eval_fname_script(start);
12407 if (lead > 0)
12408 start += lead;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 if (end == start)
12411 {
12412 if (!skip)
12413 EMSG(_("E129: Function name required"));
12414 return NULL;
12415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 if (expr_start != NULL && !skip)
12417 {
12418 /* expand magic curlies */
12419 temp_string = make_expanded_name(start, expr_start, expr_end, end);
12420 if (temp_string == NULL)
12421 {
12422 /*
12423 * Report an invalid expression in braces, unless the expression
12424 * evaluation has been cancelled due to an aborting error, an
12425 * interrupt, or an exception.
12426 */
12427 if (!aborting())
12428 EMSG2(_(e_invarg2), start);
12429 else
12430 *pp = end;
12431 return NULL;
12432 }
12433 start = temp_string;
12434 len = (int)STRLEN(temp_string);
12435 }
12436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 len = (int)(end - start);
12438
12439 /*
12440 * Copy the function name to allocated memory.
12441 * Accept <SID>name() inside a script, translate into <SNR>123_name().
12442 * Accept <SNR>123_name() outside a script.
12443 */
12444 if (skip)
12445 lead = 0; /* do nothing */
12446 else if (lead > 0)
12447 {
12448 lead = 3;
12449 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12450 {
12451 if (current_SID <= 0)
12452 {
12453 EMSG(_(e_usingsid));
12454 return NULL;
12455 }
12456 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
12457 lead += (int)STRLEN(sid_buf);
12458 }
12459 }
12460 else if (!internal && !ASCII_ISUPPER(*start))
12461 {
12462 EMSG2(_("E128: Function name must start with a capital: %s"), start);
12463 return NULL;
12464 }
12465 name = alloc((unsigned)(len + lead + 1));
12466 if (name != NULL)
12467 {
12468 if (lead > 0)
12469 {
12470 name[0] = K_SPECIAL;
12471 name[1] = KS_EXTRA;
12472 name[2] = (int)KE_SNR;
12473 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12474 STRCPY(name + 3, sid_buf);
12475 }
12476 mch_memmove(name + lead, start, (size_t)len);
12477 name[len + lead] = NUL;
12478 }
12479 *pp = end;
12480
12481 vim_free(temp_string);
12482 return name;
12483}
12484
12485/*
12486 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
12487 * Return 2 if "p" starts with "s:".
12488 * Return 0 otherwise.
12489 */
12490 static int
12491eval_fname_script(p)
12492 char_u *p;
12493{
12494 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
12495 || STRNICMP(p + 1, "SNR>", 4) == 0))
12496 return 5;
12497 if (p[0] == 's' && p[1] == ':')
12498 return 2;
12499 return 0;
12500}
12501
12502/*
12503 * Return TRUE if "p" starts with "<SID>" or "s:".
12504 * Only works if eval_fname_script() returned non-zero for "p"!
12505 */
12506 static int
12507eval_fname_sid(p)
12508 char_u *p;
12509{
12510 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
12511}
12512
12513/*
12514 * List the head of the function: "name(arg1, arg2)".
12515 */
12516 static void
12517list_func_head(fp, indent)
12518 ufunc_T *fp;
12519 int indent;
12520{
12521 int j;
12522
12523 msg_start();
12524 if (indent)
12525 MSG_PUTS(" ");
12526 MSG_PUTS("function ");
12527 if (fp->name[0] == K_SPECIAL)
12528 {
12529 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
12530 msg_puts(fp->name + 3);
12531 }
12532 else
12533 msg_puts(fp->name);
12534 msg_putchar('(');
12535 for (j = 0; j < fp->args.ga_len; ++j)
12536 {
12537 if (j)
12538 MSG_PUTS(", ");
12539 msg_puts(FUNCARG(fp, j));
12540 }
12541 if (fp->varargs)
12542 {
12543 if (j)
12544 MSG_PUTS(", ");
12545 MSG_PUTS("...");
12546 }
12547 msg_putchar(')');
12548}
12549
12550/*
12551 * Find a function by name, return pointer to it in ufuncs.
12552 * Return NULL for unknown function.
12553 */
12554 static ufunc_T *
12555find_func(name)
12556 char_u *name;
12557{
12558 ufunc_T *fp;
12559
12560 for (fp = firstfunc; fp != NULL; fp = fp->next)
12561 if (STRCMP(name, fp->name) == 0)
12562 break;
12563 return fp;
12564}
12565
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012566/*
12567 * Return TRUE if a function "name" exists.
12568 */
12569 static int
12570function_exists(name)
12571 char_u *name;
12572{
12573 char_u *p = name;
12574 int n = FALSE;
12575
12576 p = trans_function_name(&p, FALSE, TRUE);
12577 if (p != NULL)
12578 {
12579 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
12580 n = (find_func(p) != NULL);
12581 else if (ASCII_ISLOWER(*p))
12582 n = (find_internal_func(p) >= 0);
12583 vim_free(p);
12584 }
12585 return n;
12586}
12587
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12589
12590/*
12591 * Function given to ExpandGeneric() to obtain the list of user defined
12592 * function names.
12593 */
12594 char_u *
12595get_user_func_name(xp, idx)
12596 expand_T *xp;
12597 int idx;
12598{
12599 static ufunc_T *fp = NULL;
12600
12601 if (idx == 0)
12602 fp = firstfunc;
12603 if (fp != NULL)
12604 {
12605 if (STRLEN(fp->name) + 4 >= IOSIZE)
12606 return fp->name; /* prevents overflow */
12607
12608 cat_func_name(IObuff, fp);
12609 if (xp->xp_context != EXPAND_USER_FUNC)
12610 {
12611 STRCAT(IObuff, "(");
12612 if (!fp->varargs && fp->args.ga_len == 0)
12613 STRCAT(IObuff, ")");
12614 }
12615
12616 fp = fp->next;
12617 return IObuff;
12618 }
12619 return NULL;
12620}
12621
12622#endif /* FEAT_CMDL_COMPL */
12623
12624/*
12625 * Copy the function name of "fp" to buffer "buf".
12626 * "buf" must be able to hold the function name plus three bytes.
12627 * Takes care of script-local function names.
12628 */
12629 static void
12630cat_func_name(buf, fp)
12631 char_u *buf;
12632 ufunc_T *fp;
12633{
12634 if (fp->name[0] == K_SPECIAL)
12635 {
12636 STRCPY(buf, "<SNR>");
12637 STRCAT(buf, fp->name + 3);
12638 }
12639 else
12640 STRCPY(buf, fp->name);
12641}
12642
12643/*
12644 * ":delfunction {name}"
12645 */
12646 void
12647ex_delfunction(eap)
12648 exarg_T *eap;
12649{
12650 ufunc_T *fp = NULL, *pfp;
12651 char_u *p;
12652 char_u *name;
12653
12654 p = eap->arg;
12655 name = trans_function_name(&p, eap->skip, FALSE);
12656 if (name == NULL)
12657 return;
12658 if (!ends_excmd(*skipwhite(p)))
12659 {
12660 vim_free(name);
12661 EMSG(_(e_trailing));
12662 return;
12663 }
12664 eap->nextcmd = check_nextcmd(p);
12665 if (eap->nextcmd != NULL)
12666 *p = NUL;
12667
12668 if (!eap->skip)
12669 fp = find_func(name);
12670 vim_free(name);
12671
12672 if (!eap->skip)
12673 {
12674 if (fp == NULL)
12675 {
12676 EMSG2(_("E130: Undefined function: %s"), eap->arg);
12677 return;
12678 }
12679 if (fp->calls)
12680 {
12681 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
12682 return;
12683 }
12684
12685 /* clear this function */
12686 vim_free(fp->name);
12687 ga_clear_strings(&(fp->args));
12688 ga_clear_strings(&(fp->lines));
12689
12690 /* remove the function from the function list */
12691 if (firstfunc == fp)
12692 firstfunc = fp->next;
12693 else
12694 {
12695 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
12696 if (pfp->next == fp)
12697 {
12698 pfp->next = fp->next;
12699 break;
12700 }
12701 }
12702 vim_free(fp);
12703 }
12704}
12705
12706/*
12707 * Call a user function.
12708 */
12709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710call_user_func(fp, argcount, argvars, rettv, firstline, lastline)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 ufunc_T *fp; /* pointer to function */
12712 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012713 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 linenr_T firstline; /* first line of range */
12716 linenr_T lastline; /* last line of range */
12717{
12718 char_u *save_sourcing_name;
12719 linenr_T save_sourcing_lnum;
12720 scid_T save_current_SID;
12721 struct funccall fc;
12722 struct funccall *save_fcp = current_funccal;
12723 int save_did_emsg;
12724 static int depth = 0;
12725
12726 /* If depth of calling is getting too high, don't execute the function */
12727 if (depth >= p_mfd)
12728 {
12729 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->v_type = VAR_NUMBER;
12731 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 return;
12733 }
12734 ++depth;
12735
12736 line_breakcheck(); /* check for CTRL-C hit */
12737
12738 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012739 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 fc.func = fp;
12741 fc.argcount = argcount;
12742 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 fc.rettv = rettv;
12744 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 fc.linenr = 0;
12746 fc.returned = FALSE;
12747 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012748 fc.a0_var.tv.v_type = VAR_NUMBER;
12749 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
12750 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012752 fc.firstline.tv.v_type = VAR_NUMBER;
12753 fc.firstline.tv.vval.v_number = firstline;
12754 fc.firstline.v_name = NULL;
12755 fc.lastline.tv.v_type = VAR_NUMBER;
12756 fc.lastline.tv.vval.v_number = lastline;
12757 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 /* Check if this function has a breakpoint. */
12759 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
12760 fc.dbg_tick = debug_tick;
12761
12762 /* Don't redraw while executing the function. */
12763 ++RedrawingDisabled;
12764 save_sourcing_name = sourcing_name;
12765 save_sourcing_lnum = sourcing_lnum;
12766 sourcing_lnum = 1;
12767 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
12768 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
12769 if (sourcing_name != NULL)
12770 {
12771 if (save_sourcing_name != NULL
12772 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
12773 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
12774 else
12775 STRCPY(sourcing_name, "function ");
12776 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
12777
12778 if (p_verbose >= 12)
12779 {
12780 ++no_wait_return;
12781 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12782 msg_str((char_u *)_("calling %s"), sourcing_name);
12783 if (p_verbose >= 14)
12784 {
12785 int i;
12786 char_u buf[MSG_BUF_LEN];
12787
12788 msg_puts((char_u *)"(");
12789 for (i = 0; i < argcount; ++i)
12790 {
12791 if (i > 0)
12792 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012793 if (argvars[i].v_type == VAR_NUMBER)
12794 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 else
12796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 buf, MSG_BUF_LEN);
12799 msg_puts((char_u *)"\"");
12800 msg_puts(buf);
12801 msg_puts((char_u *)"\"");
12802 }
12803 }
12804 msg_puts((char_u *)")");
12805 }
12806 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12807 cmdline_row = msg_row;
12808 --no_wait_return;
12809 }
12810 }
12811 save_current_SID = current_SID;
12812 current_SID = fp->script_ID;
12813 save_did_emsg = did_emsg;
12814 did_emsg = FALSE;
12815
12816 /* call do_cmdline() to execute the lines */
12817 do_cmdline(NULL, get_func_line, (void *)&fc,
12818 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
12819
12820 --RedrawingDisabled;
12821
12822 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 clear_tv(rettv);
12826 rettv->v_type = VAR_NUMBER;
12827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 }
12829
12830 /* when being verbose, mention the return value */
12831 if (p_verbose >= 12)
12832 {
12833 char_u *sn, *val;
12834
12835 ++no_wait_return;
12836 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12837
12838 /* Make sure the output fits in IObuff. */
12839 sn = sourcing_name;
12840 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
12841 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
12842
12843 if (aborting())
12844 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 (long)fc.rettv->vval.v_number);
12848 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 if (STRLEN(val) > IOSIZE / 2 - 50)
12852 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
12853 smsg((char_u *)_("%s returning \"%s\""), sn, val);
12854 }
12855 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12856 cmdline_row = msg_row;
12857 --no_wait_return;
12858 }
12859
12860 vim_free(sourcing_name);
12861 sourcing_name = save_sourcing_name;
12862 sourcing_lnum = save_sourcing_lnum;
12863 current_SID = save_current_SID;
12864
12865 if (p_verbose >= 12 && sourcing_name != NULL)
12866 {
12867 ++no_wait_return;
12868 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12869 msg_str((char_u *)_("continuing in %s"), sourcing_name);
12870 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12871 cmdline_row = msg_row;
12872 --no_wait_return;
12873 }
12874
12875 did_emsg |= save_did_emsg;
12876 current_funccal = save_fcp;
12877
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012878 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 --depth;
12880}
12881
12882/*
12883 * ":return [expr]"
12884 */
12885 void
12886ex_return(eap)
12887 exarg_T *eap;
12888{
12889 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 int returning = FALSE;
12892
12893 if (current_funccal == NULL)
12894 {
12895 EMSG(_("E133: :return not inside a function"));
12896 return;
12897 }
12898
12899 if (eap->skip)
12900 ++emsg_skip;
12901
12902 eap->nextcmd = NULL;
12903 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 {
12906 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 }
12911 /* It's safer to return also on error. */
12912 else if (!eap->skip)
12913 {
12914 /*
12915 * Return unless the expression evaluation has been cancelled due to an
12916 * aborting error, an interrupt, or an exception.
12917 */
12918 if (!aborting())
12919 returning = do_return(eap, FALSE, TRUE, NULL);
12920 }
12921
12922 /* When skipping or the return gets pending, advance to the next command
12923 * in this line (!returning). Otherwise, ignore the rest of the line.
12924 * Following lines will be ignored by get_func_line(). */
12925 if (returning)
12926 eap->nextcmd = NULL;
12927 else if (eap->nextcmd == NULL) /* no argument */
12928 eap->nextcmd = check_nextcmd(arg);
12929
12930 if (eap->skip)
12931 --emsg_skip;
12932}
12933
12934/*
12935 * Return from a function. Possibly makes the return pending. Also called
12936 * for a pending return at the ":endtry" or after returning from an extra
12937 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 * when called due to a ":return" command. "rettv" may point to a typeval
12939 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 * FALSE when the return gets pending.
12941 */
12942 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 exarg_T *eap;
12945 int reanimate;
12946 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948{
12949 int idx;
12950 struct condstack *cstack = eap->cstack;
12951
12952 if (reanimate)
12953 /* Undo the return. */
12954 current_funccal->returned = FALSE;
12955
12956 /*
12957 * Cleanup (and inactivate) conditionals, but stop when a try conditional
12958 * not in its finally clause (which then is to be executed next) is found.
12959 * In this case, make the ":return" pending for execution at the ":endtry".
12960 * Otherwise, return normally.
12961 */
12962 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
12963 if (idx >= 0)
12964 {
12965 cstack->cs_pending[idx] = CSTP_RETURN;
12966
12967 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 /* A pending return again gets pending. "rettv" points to an
12969 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 else
12973 {
12974 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 {
12981 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
12983 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 else
12985 EMSG(_(e_outofmem));
12986 }
12987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989
12990 if (reanimate)
12991 {
12992 /* The pending return value could be overwritten by a ":return"
12993 * without argument in a finally clause; reset the default
12994 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 current_funccal->rettv->v_type = VAR_NUMBER;
12996 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 }
12998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 }
13001 else
13002 {
13003 current_funccal->returned = TRUE;
13004
13005 /* If the return is carried out now, store the return value. For
13006 * a return immediately after reanimation, the value is already
13007 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013008 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 clear_tv(current_funccal->rettv);
13011 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 }
13015 }
13016
13017 return idx < 0;
13018}
13019
13020/*
13021 * Free the variable with a pending return value.
13022 */
13023 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024discard_pending_return(rettv)
13025 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013027 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028}
13029
13030/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 * is an allocated string. Used by report_pending() for verbose messages.
13033 */
13034 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035get_return_cmd(rettv)
13036 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 char_u *s;
13039 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013040 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 if (rettv == NULL)
13043 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 else
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013045 s = tv2string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046
13047 STRCPY(IObuff, ":return ");
13048 STRNCPY(IObuff + 8, s, IOSIZE - 8);
13049 if (STRLEN(s) + 8 >= IOSIZE)
13050 STRCPY(IObuff + IOSIZE - 4, "...");
13051 vim_free(tofree);
13052 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053}
13054
13055/*
13056 * Get next function line.
13057 * Called by do_cmdline() to get the next line.
13058 * Returns allocated string, or NULL for end of function.
13059 */
13060/* ARGSUSED */
13061 char_u *
13062get_func_line(c, cookie, indent)
13063 int c; /* not used */
13064 void *cookie;
13065 int indent; /* not used */
13066{
13067 struct funccall *fcp = (struct funccall *)cookie;
13068 char_u *retval;
13069 garray_T *gap; /* growarray with function lines */
13070
13071 /* If breakpoints have been added/deleted need to check for it. */
13072 if (fcp->dbg_tick != debug_tick)
13073 {
13074 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
13075 sourcing_lnum);
13076 fcp->dbg_tick = debug_tick;
13077 }
13078
13079 gap = &fcp->func->lines;
13080 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
13081 retval = NULL;
13082 else if (fcp->returned || fcp->linenr >= gap->ga_len)
13083 retval = NULL;
13084 else
13085 {
13086 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
13087 sourcing_lnum = fcp->linenr;
13088 }
13089
13090 /* Did we encounter a breakpoint? */
13091 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
13092 {
13093 dbg_breakpoint(fcp->func->name, sourcing_lnum);
13094 /* Find next breakpoint. */
13095 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
13096 sourcing_lnum);
13097 fcp->dbg_tick = debug_tick;
13098 }
13099
13100 return retval;
13101}
13102
13103/*
13104 * Return TRUE if the currently active function should be ended, because a
13105 * return was encountered or an error occured. Used inside a ":while".
13106 */
13107 int
13108func_has_ended(cookie)
13109 void *cookie;
13110{
13111 struct funccall *fcp = (struct funccall *)cookie;
13112
13113 /* Ignore the "abort" flag if the abortion behavior has been changed due to
13114 * an error inside a try conditional. */
13115 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
13116 || fcp->returned);
13117}
13118
13119/*
13120 * return TRUE if cookie indicates a function which "abort"s on errors.
13121 */
13122 int
13123func_has_abort(cookie)
13124 void *cookie;
13125{
13126 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
13127}
13128
13129#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
13130typedef enum
13131{
13132 VAR_FLAVOUR_DEFAULT,
13133 VAR_FLAVOUR_SESSION,
13134 VAR_FLAVOUR_VIMINFO
13135} var_flavour_T;
13136
13137static var_flavour_T var_flavour __ARGS((char_u *varname));
13138
13139 static var_flavour_T
13140var_flavour(varname)
13141 char_u *varname;
13142{
13143 char_u *p = varname;
13144
13145 if (ASCII_ISUPPER(*p))
13146 {
13147 while (*(++p))
13148 if (ASCII_ISLOWER(*p))
13149 return VAR_FLAVOUR_SESSION;
13150 return VAR_FLAVOUR_VIMINFO;
13151 }
13152 else
13153 return VAR_FLAVOUR_DEFAULT;
13154}
13155#endif
13156
13157#if defined(FEAT_VIMINFO) || defined(PROTO)
13158/*
13159 * Restore global vars that start with a capital from the viminfo file
13160 */
13161 int
13162read_viminfo_varlist(virp, writing)
13163 vir_T *virp;
13164 int writing;
13165{
13166 char_u *tab;
13167 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013168 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169
13170 if (!writing && (find_viminfo_parameter('!') != NULL))
13171 {
13172 tab = vim_strchr(virp->vir_line + 1, '\t');
13173 if (tab != NULL)
13174 {
13175 *tab++ = '\0'; /* isolate the variable name */
13176 if (*tab == 'S') /* string var */
13177 is_string = TRUE;
13178
13179 tab = vim_strchr(tab, '\t');
13180 if (tab != NULL)
13181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 if (is_string)
13183 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013184 tv.v_type = VAR_STRING;
13185 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 }
13188 else
13189 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013190 tv.v_type = VAR_NUMBER;
13191 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013193 set_var(virp->vir_line + 1, &tv, FALSE);
13194 if (is_string)
13195 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 }
13197 }
13198 }
13199
13200 return viminfo_readline(virp);
13201}
13202
13203/*
13204 * Write global vars that start with a capital to the viminfo file
13205 */
13206 void
13207write_viminfo_varlist(fp)
13208 FILE *fp;
13209{
13210 garray_T *gap = &variables; /* global variable */
13211 VAR this_var;
13212 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 char *s;
13214 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013215 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216
13217 if (find_viminfo_parameter('!') == NULL)
13218 return;
13219
13220 fprintf(fp, _("\n# global variables:\n"));
13221 for (i = gap->ga_len; --i >= 0; )
13222 {
13223 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224 if (this_var->v_name != NULL
13225 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 switch (this_var->tv.v_type)
13228 {
13229 case VAR_STRING: s = "STR"; break;
13230 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013231 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 }
13233 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013234 viminfo_writestring(fp, tv2string(&this_var->tv, &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 }
13237 }
13238}
13239#endif
13240
13241#if defined(FEAT_SESSION) || defined(PROTO)
13242 int
13243store_session_globals(fd)
13244 FILE *fd;
13245{
13246 garray_T *gap = &variables; /* global variable */
13247 VAR this_var;
13248 int i;
13249 char_u *p, *t;
13250
13251 for (i = gap->ga_len; --i >= 0; )
13252 {
13253 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013254 if (this_var->v_name != NULL
13255 && (this_var->tv.v_type == VAR_NUMBER
13256 || this_var->tv.v_type == VAR_STRING)
13257 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013259 /* Escape special characters with a backslash. Turn a LF and
13260 * CR into \n and \r. */
13261 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013263 if (p == NULL) /* out of memory */
13264 continue;
13265 for (t = p; *t != NUL; ++t)
13266 if (*t == '\n')
13267 *t = 'n';
13268 else if (*t == '\r')
13269 *t = 'r';
13270 if ((fprintf(fd, "let %s = %c%s%c",
13271 this_var->v_name,
13272 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
13273 p,
13274 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
13275 || put_eol(fd) == FAIL)
13276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013280 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 }
13282 }
13283 return OK;
13284}
13285#endif
13286
13287#endif /* FEAT_EVAL */
13288
13289#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
13290
13291
13292#ifdef WIN3264
13293/*
13294 * Functions for ":8" filename modifier: get 8.3 version of a filename.
13295 */
13296static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13297static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
13298static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13299
13300/*
13301 * Get the short pathname of a file.
13302 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
13303 */
13304 static int
13305get_short_pathname(fnamep, bufp, fnamelen)
13306 char_u **fnamep;
13307 char_u **bufp;
13308 int *fnamelen;
13309{
13310 int l,len;
13311 char_u *newbuf;
13312
13313 len = *fnamelen;
13314
13315 l = GetShortPathName(*fnamep, *fnamep, len);
13316 if (l > len - 1)
13317 {
13318 /* If that doesn't work (not enough space), then save the string
13319 * and try again with a new buffer big enough
13320 */
13321 newbuf = vim_strnsave(*fnamep, l);
13322 if (newbuf == NULL)
13323 return 0;
13324
13325 vim_free(*bufp);
13326 *fnamep = *bufp = newbuf;
13327
13328 l = GetShortPathName(*fnamep,*fnamep,l+1);
13329
13330 /* Really should always succeed, as the buffer is big enough */
13331 }
13332
13333 *fnamelen = l;
13334 return 1;
13335}
13336
13337/*
13338 * Create a short path name. Returns the length of the buffer it needs.
13339 * Doesn't copy over the end of the buffer passed in.
13340 */
13341 static int
13342shortpath_for_invalid_fname(fname, bufp, fnamelen)
13343 char_u **fname;
13344 char_u **bufp;
13345 int *fnamelen;
13346{
13347 char_u *s, *p, *pbuf2, *pbuf3;
13348 char_u ch;
13349 int l,len,len2,plen,slen;
13350
13351 /* Make a copy */
13352 len2 = *fnamelen;
13353 pbuf2 = vim_strnsave(*fname, len2);
13354 pbuf3 = NULL;
13355
13356 s = pbuf2 + len2 - 1; /* Find the end */
13357 slen = 1;
13358 plen = len2;
13359
13360 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013361 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 {
13363 --s;
13364 ++slen;
13365 --plen;
13366 }
13367
13368 do
13369 {
13370 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013371 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 {
13373 --s;
13374 ++slen;
13375 --plen;
13376 }
13377 if (s <= pbuf2)
13378 break;
13379
13380 /* Remeber the character that is about to be blatted */
13381 ch = *s;
13382 *s = 0; /* get_short_pathname requires a null-terminated string */
13383
13384 /* Try it in situ */
13385 p = pbuf2;
13386 if (!get_short_pathname(&p, &pbuf3, &plen))
13387 {
13388 vim_free(pbuf2);
13389 return -1;
13390 }
13391 *s = ch; /* Preserve the string */
13392 } while (plen == 0);
13393
13394 if (plen > 0)
13395 {
13396 /* Remeber the length of the new string. */
13397 *fnamelen = len = plen + slen;
13398 vim_free(*bufp);
13399 if (len > len2)
13400 {
13401 /* If there's not enough space in the currently allocated string,
13402 * then copy it to a buffer big enough.
13403 */
13404 *fname= *bufp = vim_strnsave(p, len);
13405 if (*fname == NULL)
13406 return -1;
13407 }
13408 else
13409 {
13410 /* Transfer pbuf2 to being the main buffer (it's big enough) */
13411 *fname = *bufp = pbuf2;
13412 if (p != pbuf2)
13413 strncpy(*fname, p, plen);
13414 pbuf2 = NULL;
13415 }
13416 /* Concat the next bit */
13417 strncpy(*fname + plen, s, slen);
13418 (*fname)[len] = '\0';
13419 }
13420 vim_free(pbuf3);
13421 vim_free(pbuf2);
13422 return 0;
13423}
13424
13425/*
13426 * Get a pathname for a partial path.
13427 */
13428 static int
13429shortpath_for_partial(fnamep, bufp, fnamelen)
13430 char_u **fnamep;
13431 char_u **bufp;
13432 int *fnamelen;
13433{
13434 int sepcount, len, tflen;
13435 char_u *p;
13436 char_u *pbuf, *tfname;
13437 int hasTilde;
13438
13439 /* Count up the path seperators from the RHS.. so we know which part
13440 * of the path to return.
13441 */
13442 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013443 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 if (vim_ispathsep(*p))
13445 ++sepcount;
13446
13447 /* Need full path first (use expand_env() to remove a "~/") */
13448 hasTilde = (**fnamep == '~');
13449 if (hasTilde)
13450 pbuf = tfname = expand_env_save(*fnamep);
13451 else
13452 pbuf = tfname = FullName_save(*fnamep, FALSE);
13453
13454 len = tflen = STRLEN(tfname);
13455
13456 if (!get_short_pathname(&tfname, &pbuf, &len))
13457 return -1;
13458
13459 if (len == 0)
13460 {
13461 /* Don't have a valid filename, so shorten the rest of the
13462 * path if we can. This CAN give us invalid 8.3 filenames, but
13463 * there's not a lot of point in guessing what it might be.
13464 */
13465 len = tflen;
13466 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
13467 return -1;
13468 }
13469
13470 /* Count the paths backward to find the beginning of the desired string. */
13471 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013472 {
13473#ifdef FEAT_MBYTE
13474 if (has_mbyte)
13475 p -= mb_head_off(tfname, p);
13476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 if (vim_ispathsep(*p))
13478 {
13479 if (sepcount == 0 || (hasTilde && sepcount == 1))
13480 break;
13481 else
13482 sepcount --;
13483 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 if (hasTilde)
13486 {
13487 --p;
13488 if (p >= tfname)
13489 *p = '~';
13490 else
13491 return -1;
13492 }
13493 else
13494 ++p;
13495
13496 /* Copy in the string - p indexes into tfname - allocated at pbuf */
13497 vim_free(*bufp);
13498 *fnamelen = (int)STRLEN(p);
13499 *bufp = pbuf;
13500 *fnamep = p;
13501
13502 return 0;
13503}
13504#endif /* WIN3264 */
13505
13506/*
13507 * Adjust a filename, according to a string of modifiers.
13508 * *fnamep must be NUL terminated when called. When returning, the length is
13509 * determined by *fnamelen.
13510 * Returns valid flags.
13511 * When there is an error, *fnamep is set to NULL.
13512 */
13513 int
13514modify_fname(src, usedlen, fnamep, bufp, fnamelen)
13515 char_u *src; /* string with modifiers */
13516 int *usedlen; /* characters after src that are used */
13517 char_u **fnamep; /* file name so far */
13518 char_u **bufp; /* buffer for allocated file name or NULL */
13519 int *fnamelen; /* length of fnamep */
13520{
13521 int valid = 0;
13522 char_u *tail;
13523 char_u *s, *p, *pbuf;
13524 char_u dirname[MAXPATHL];
13525 int c;
13526 int has_fullname = 0;
13527#ifdef WIN3264
13528 int has_shortname = 0;
13529#endif
13530
13531repeat:
13532 /* ":p" - full path/file_name */
13533 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
13534 {
13535 has_fullname = 1;
13536
13537 valid |= VALID_PATH;
13538 *usedlen += 2;
13539
13540 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
13541 if ((*fnamep)[0] == '~'
13542#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
13543 && ((*fnamep)[1] == '/'
13544# ifdef BACKSLASH_IN_FILENAME
13545 || (*fnamep)[1] == '\\'
13546# endif
13547 || (*fnamep)[1] == NUL)
13548
13549#endif
13550 )
13551 {
13552 *fnamep = expand_env_save(*fnamep);
13553 vim_free(*bufp); /* free any allocated file name */
13554 *bufp = *fnamep;
13555 if (*fnamep == NULL)
13556 return -1;
13557 }
13558
13559 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013560 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 {
13562 if (vim_ispathsep(*p)
13563 && p[1] == '.'
13564 && (p[2] == NUL
13565 || vim_ispathsep(p[2])
13566 || (p[2] == '.'
13567 && (p[3] == NUL || vim_ispathsep(p[3])))))
13568 break;
13569 }
13570
13571 /* FullName_save() is slow, don't use it when not needed. */
13572 if (*p != NUL || !vim_isAbsName(*fnamep))
13573 {
13574 *fnamep = FullName_save(*fnamep, *p != NUL);
13575 vim_free(*bufp); /* free any allocated file name */
13576 *bufp = *fnamep;
13577 if (*fnamep == NULL)
13578 return -1;
13579 }
13580
13581 /* Append a path separator to a directory. */
13582 if (mch_isdir(*fnamep))
13583 {
13584 /* Make room for one or two extra characters. */
13585 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
13586 vim_free(*bufp); /* free any allocated file name */
13587 *bufp = *fnamep;
13588 if (*fnamep == NULL)
13589 return -1;
13590 add_pathsep(*fnamep);
13591 }
13592 }
13593
13594 /* ":." - path relative to the current directory */
13595 /* ":~" - path relative to the home directory */
13596 /* ":8" - shortname path - postponed till after */
13597 while (src[*usedlen] == ':'
13598 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
13599 {
13600 *usedlen += 2;
13601 if (c == '8')
13602 {
13603#ifdef WIN3264
13604 has_shortname = 1; /* Postpone this. */
13605#endif
13606 continue;
13607 }
13608 pbuf = NULL;
13609 /* Need full path first (use expand_env() to remove a "~/") */
13610 if (!has_fullname)
13611 {
13612 if (c == '.' && **fnamep == '~')
13613 p = pbuf = expand_env_save(*fnamep);
13614 else
13615 p = pbuf = FullName_save(*fnamep, FALSE);
13616 }
13617 else
13618 p = *fnamep;
13619
13620 has_fullname = 0;
13621
13622 if (p != NULL)
13623 {
13624 if (c == '.')
13625 {
13626 mch_dirname(dirname, MAXPATHL);
13627 s = shorten_fname(p, dirname);
13628 if (s != NULL)
13629 {
13630 *fnamep = s;
13631 if (pbuf != NULL)
13632 {
13633 vim_free(*bufp); /* free any allocated file name */
13634 *bufp = pbuf;
13635 pbuf = NULL;
13636 }
13637 }
13638 }
13639 else
13640 {
13641 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
13642 /* Only replace it when it starts with '~' */
13643 if (*dirname == '~')
13644 {
13645 s = vim_strsave(dirname);
13646 if (s != NULL)
13647 {
13648 *fnamep = s;
13649 vim_free(*bufp);
13650 *bufp = s;
13651 }
13652 }
13653 }
13654 vim_free(pbuf);
13655 }
13656 }
13657
13658 tail = gettail(*fnamep);
13659 *fnamelen = (int)STRLEN(*fnamep);
13660
13661 /* ":h" - head, remove "/file_name", can be repeated */
13662 /* Don't remove the first "/" or "c:\" */
13663 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
13664 {
13665 valid |= VALID_HEAD;
13666 *usedlen += 2;
13667 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013668 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 --tail;
13670 *fnamelen = (int)(tail - *fnamep);
13671#ifdef VMS
13672 if (*fnamelen > 0)
13673 *fnamelen += 1; /* the path separator is part of the path */
13674#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013675 while (tail > s && !after_pathsep(s, tail))
13676 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 }
13678
13679 /* ":8" - shortname */
13680 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
13681 {
13682 *usedlen += 2;
13683#ifdef WIN3264
13684 has_shortname = 1;
13685#endif
13686 }
13687
13688#ifdef WIN3264
13689 /* Check shortname after we have done 'heads' and before we do 'tails'
13690 */
13691 if (has_shortname)
13692 {
13693 pbuf = NULL;
13694 /* Copy the string if it is shortened by :h */
13695 if (*fnamelen < (int)STRLEN(*fnamep))
13696 {
13697 p = vim_strnsave(*fnamep, *fnamelen);
13698 if (p == 0)
13699 return -1;
13700 vim_free(*bufp);
13701 *bufp = *fnamep = p;
13702 }
13703
13704 /* Split into two implementations - makes it easier. First is where
13705 * there isn't a full name already, second is where there is.
13706 */
13707 if (!has_fullname && !vim_isAbsName(*fnamep))
13708 {
13709 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
13710 return -1;
13711 }
13712 else
13713 {
13714 int l;
13715
13716 /* Simple case, already have the full-name
13717 * Nearly always shorter, so try first time. */
13718 l = *fnamelen;
13719 if (!get_short_pathname(fnamep, bufp, &l))
13720 return -1;
13721
13722 if (l == 0)
13723 {
13724 /* Couldn't find the filename.. search the paths.
13725 */
13726 l = *fnamelen;
13727 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
13728 return -1;
13729 }
13730 *fnamelen = l;
13731 }
13732 }
13733#endif /* WIN3264 */
13734
13735 /* ":t" - tail, just the basename */
13736 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
13737 {
13738 *usedlen += 2;
13739 *fnamelen -= (int)(tail - *fnamep);
13740 *fnamep = tail;
13741 }
13742
13743 /* ":e" - extension, can be repeated */
13744 /* ":r" - root, without extension, can be repeated */
13745 while (src[*usedlen] == ':'
13746 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
13747 {
13748 /* find a '.' in the tail:
13749 * - for second :e: before the current fname
13750 * - otherwise: The last '.'
13751 */
13752 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
13753 s = *fnamep - 2;
13754 else
13755 s = *fnamep + *fnamelen - 1;
13756 for ( ; s > tail; --s)
13757 if (s[0] == '.')
13758 break;
13759 if (src[*usedlen + 1] == 'e') /* :e */
13760 {
13761 if (s > tail)
13762 {
13763 *fnamelen += (int)(*fnamep - (s + 1));
13764 *fnamep = s + 1;
13765#ifdef VMS
13766 /* cut version from the extension */
13767 s = *fnamep + *fnamelen - 1;
13768 for ( ; s > *fnamep; --s)
13769 if (s[0] == ';')
13770 break;
13771 if (s > *fnamep)
13772 *fnamelen = s - *fnamep;
13773#endif
13774 }
13775 else if (*fnamep <= tail)
13776 *fnamelen = 0;
13777 }
13778 else /* :r */
13779 {
13780 if (s > tail) /* remove one extension */
13781 *fnamelen = (int)(s - *fnamep);
13782 }
13783 *usedlen += 2;
13784 }
13785
13786 /* ":s?pat?foo?" - substitute */
13787 /* ":gs?pat?foo?" - global substitute */
13788 if (src[*usedlen] == ':'
13789 && (src[*usedlen + 1] == 's'
13790 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
13791 {
13792 char_u *str;
13793 char_u *pat;
13794 char_u *sub;
13795 int sep;
13796 char_u *flags;
13797 int didit = FALSE;
13798
13799 flags = (char_u *)"";
13800 s = src + *usedlen + 2;
13801 if (src[*usedlen + 1] == 'g')
13802 {
13803 flags = (char_u *)"g";
13804 ++s;
13805 }
13806
13807 sep = *s++;
13808 if (sep)
13809 {
13810 /* find end of pattern */
13811 p = vim_strchr(s, sep);
13812 if (p != NULL)
13813 {
13814 pat = vim_strnsave(s, (int)(p - s));
13815 if (pat != NULL)
13816 {
13817 s = p + 1;
13818 /* find end of substitution */
13819 p = vim_strchr(s, sep);
13820 if (p != NULL)
13821 {
13822 sub = vim_strnsave(s, (int)(p - s));
13823 str = vim_strnsave(*fnamep, *fnamelen);
13824 if (sub != NULL && str != NULL)
13825 {
13826 *usedlen = (int)(p + 1 - src);
13827 s = do_string_sub(str, pat, sub, flags);
13828 if (s != NULL)
13829 {
13830 *fnamep = s;
13831 *fnamelen = (int)STRLEN(s);
13832 vim_free(*bufp);
13833 *bufp = s;
13834 didit = TRUE;
13835 }
13836 }
13837 vim_free(sub);
13838 vim_free(str);
13839 }
13840 vim_free(pat);
13841 }
13842 }
13843 /* after using ":s", repeat all the modifiers */
13844 if (didit)
13845 goto repeat;
13846 }
13847 }
13848
13849 return valid;
13850}
13851
13852/*
13853 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
13854 * "flags" can be "g" to do a global substitute.
13855 * Returns an allocated string, NULL for error.
13856 */
13857 char_u *
13858do_string_sub(str, pat, sub, flags)
13859 char_u *str;
13860 char_u *pat;
13861 char_u *sub;
13862 char_u *flags;
13863{
13864 int sublen;
13865 regmatch_T regmatch;
13866 int i;
13867 int do_all;
13868 char_u *tail;
13869 garray_T ga;
13870 char_u *ret;
13871 char_u *save_cpo;
13872
13873 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
13874 save_cpo = p_cpo;
13875 p_cpo = (char_u *)"";
13876
13877 ga_init2(&ga, 1, 200);
13878
13879 do_all = (flags[0] == 'g');
13880
13881 regmatch.rm_ic = p_ic;
13882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13883 if (regmatch.regprog != NULL)
13884 {
13885 tail = str;
13886 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
13887 {
13888 /*
13889 * Get some space for a temporary buffer to do the substitution
13890 * into. It will contain:
13891 * - The text up to where the match is.
13892 * - The substituted text.
13893 * - The text after the match.
13894 */
13895 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
13896 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
13897 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
13898 {
13899 ga_clear(&ga);
13900 break;
13901 }
13902
13903 /* copy the text up to where the match is */
13904 i = (int)(regmatch.startp[0] - tail);
13905 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
13906 /* add the substituted text */
13907 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
13908 + ga.ga_len + i, TRUE, TRUE, FALSE);
13909 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 /* avoid getting stuck on a match with an empty string */
13911 if (tail == regmatch.endp[0])
13912 {
13913 if (*tail == NUL)
13914 break;
13915 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
13916 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 }
13918 else
13919 {
13920 tail = regmatch.endp[0];
13921 if (*tail == NUL)
13922 break;
13923 }
13924 if (!do_all)
13925 break;
13926 }
13927
13928 if (ga.ga_data != NULL)
13929 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
13930
13931 vim_free(regmatch.regprog);
13932 }
13933
13934 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
13935 ga_clear(&ga);
13936 p_cpo = save_cpo;
13937
13938 return ret;
13939}
13940
13941#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */