blob: ba4fad4ea6ba37b980da129c9ba0335cfe0a89a0 [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 Moolenaar8a283e52005-01-06 23:28:25 +0000327static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000328static void list_append __ARGS((listvar *l, listitem *item));
329static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000330static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
331static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
332static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000333static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000334static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000335static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000336static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000337static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000339static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340static int get_func_tv __ARGS((char_u *name, int len, typeval *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
341static int call_func __ARGS((char_u *name, int len, typeval *rettv, int argcount, typeval *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000342
343static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000344static void f_append __ARGS((typeval *argvars, typeval *rettv));
345static void f_argc __ARGS((typeval *argvars, typeval *rettv));
346static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
347static void f_argv __ARGS((typeval *argvars, typeval *rettv));
348static void f_browse __ARGS((typeval *argvars, typeval *rettv));
349static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000350static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
351static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
352static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
354static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
355static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
356static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
357static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000358static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000359static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
360static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
361static void f_col __ARGS((typeval *argvars, typeval *rettv));
362static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
363static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000364static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000365static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
366static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
367static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
368static void f_delete __ARGS((typeval *argvars, typeval *rettv));
369static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
370static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
371static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000372static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000373static void f_escape __ARGS((typeval *argvars, typeval *rettv));
374static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
375static void f_executable __ARGS((typeval *argvars, typeval *rettv));
376static void f_exists __ARGS((typeval *argvars, typeval *rettv));
377static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000378static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000379static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
380static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
381static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
382static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000383static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
384static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
385static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
387static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
388static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
389static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
390static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000391static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000392static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
393static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
394static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
395static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
396static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
397static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
398static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
399static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
400static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
401static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
402static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
403static void f_getline __ARGS((typeval *argvars, typeval *rettv));
404static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
405static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
406static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
407static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
408static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
409static void f_glob __ARGS((typeval *argvars, typeval *rettv));
410static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
411static void f_has __ARGS((typeval *argvars, typeval *rettv));
412static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
413static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
414static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
415static void f_histget __ARGS((typeval *argvars, typeval *rettv));
416static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000418static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000419static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
420static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
421static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000422static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000423static void f_input __ARGS((typeval *argvars, typeval *rettv));
424static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
425static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
426static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
427static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000428static void f_insert __ARGS((typeval *argvars, typeval *rettv));
429static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000430static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
431static void f_len __ARGS((typeval *argvars, typeval *rettv));
432static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
433static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000434static void f_line __ARGS((typeval *argvars, typeval *rettv));
435static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
436static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
437static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
438static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
439static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000440static void f_match __ARGS((typeval *argvars, typeval *rettv));
441static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
442static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
443static void f_mode __ARGS((typeval *argvars, typeval *rettv));
444static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
445static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
446static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000447static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
448static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
449static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
450static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
451static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000452static void f_remove __ARGS((typeval *argvars, typeval *rettv));
453static void f_rename __ARGS((typeval *argvars, typeval *rettv));
454static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
455static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
456static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
457static void f_search __ARGS((typeval *argvars, typeval *rettv));
458static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000459static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
460static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000461static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
462static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000463static void f_setline __ARGS((typeval *argvars, typeval *rettv));
464static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000465static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000466static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000467static void f_sort __ARGS((typeval *argvars, typeval *rettv));
468static void f_str2list __ARGS((typeval *argvars, typeval *rettv));
469#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000470static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000471#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000472static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
473static void f_string __ARGS((typeval *argvars, typeval *rettv));
474static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
475static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
476static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
477static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000478static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
479static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000480static void f_synID __ARGS((typeval *argvars, typeval *rettv));
481static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
482static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
483static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000484static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
485static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
486static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
487static void f_tr __ARGS((typeval *argvars, typeval *rettv));
488static void f_type __ARGS((typeval *argvars, typeval *rettv));
489static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
490static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
491static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
492static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
493static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
494static void f_winline __ARGS((typeval *argvars, typeval *rettv));
495static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
496static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
497static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000498
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000499static win_T *find_win_by_nr __ARGS((typeval *vp));
500static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501static int get_env_len __ARGS((char_u **arg));
502static int get_id_len __ARGS((char_u **arg));
503static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000504static 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 +0000505static int eval_isnamec __ARGS((int c));
506static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000507static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
508static typeval *alloc_tv __ARGS((void));
509static typeval *alloc_string_tv __ARGS((char_u *string));
510static void free_tv __ARGS((typeval *varp));
511static void clear_tv __ARGS((typeval *varp));
512static void init_tv __ARGS((typeval *varp));
513static long get_tv_number __ARGS((typeval *varp));
514static linenr_T get_tv_lnum __ARGS((typeval *argvars));
515static char_u *get_tv_string __ARGS((typeval *varp));
516static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517static VAR find_var __ARGS((char_u *name, int writing));
518static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
519static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000520static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521static void list_one_var __ARGS((VAR v, char_u *prefix));
522static void list_vim_var __ARGS((int i));
523static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000524static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000525static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
527static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
528static int eval_fname_script __ARGS((char_u *p));
529static int eval_fname_sid __ARGS((char_u *p));
530static void list_func_head __ARGS((ufunc_T *fp, int indent));
531static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
532static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000533static int function_exists __ARGS((char_u *name));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000534static 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 +0000535
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000536#define get_var_string(p) get_tv_string(&(p)->tv)
537#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
538#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540static 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 +0000541
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000542static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
543static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
544static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000545static void list_all_vars __ARGS((void));
546static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
547static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
548static 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 +0000549static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551/*
552 * Set an internal variable to a string value. Creates the variable if it does
553 * not already exist.
554 */
555 void
556set_internal_string_var(name, value)
557 char_u *name;
558 char_u *value;
559{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000560 char_u *val;
561 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
563 val = vim_strsave(value);
564 if (val != NULL)
565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000566 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000567 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000569 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000570 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
572 }
573}
574
575# if defined(FEAT_MBYTE) || defined(PROTO)
576 int
577eval_charconvert(enc_from, enc_to, fname_from, fname_to)
578 char_u *enc_from;
579 char_u *enc_to;
580 char_u *fname_from;
581 char_u *fname_to;
582{
583 int err = FALSE;
584
585 set_vim_var_string(VV_CC_FROM, enc_from, -1);
586 set_vim_var_string(VV_CC_TO, enc_to, -1);
587 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
588 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
589 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
590 err = TRUE;
591 set_vim_var_string(VV_CC_FROM, NULL, -1);
592 set_vim_var_string(VV_CC_TO, NULL, -1);
593 set_vim_var_string(VV_FNAME_IN, NULL, -1);
594 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
595
596 if (err)
597 return FAIL;
598 return OK;
599}
600# endif
601
602# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
603 int
604eval_printexpr(fname, args)
605 char_u *fname;
606 char_u *args;
607{
608 int err = FALSE;
609
610 set_vim_var_string(VV_FNAME_IN, fname, -1);
611 set_vim_var_string(VV_CMDARG, args, -1);
612 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
613 err = TRUE;
614 set_vim_var_string(VV_FNAME_IN, NULL, -1);
615 set_vim_var_string(VV_CMDARG, NULL, -1);
616
617 if (err)
618 {
619 mch_remove(fname);
620 return FAIL;
621 }
622 return OK;
623}
624# endif
625
626# if defined(FEAT_DIFF) || defined(PROTO)
627 void
628eval_diff(origfile, newfile, outfile)
629 char_u *origfile;
630 char_u *newfile;
631 char_u *outfile;
632{
633 int err = FALSE;
634
635 set_vim_var_string(VV_FNAME_IN, origfile, -1);
636 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
637 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
638 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642}
643
644 void
645eval_patch(origfile, difffile, outfile)
646 char_u *origfile;
647 char_u *difffile;
648 char_u *outfile;
649{
650 int err;
651
652 set_vim_var_string(VV_FNAME_IN, origfile, -1);
653 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
654 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
655 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
656 set_vim_var_string(VV_FNAME_IN, NULL, -1);
657 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
658 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
659}
660# endif
661
662/*
663 * Top level evaluation function, returning a boolean.
664 * Sets "error" to TRUE if there was an error.
665 * Return TRUE or FALSE.
666 */
667 int
668eval_to_bool(arg, error, nextcmd, skip)
669 char_u *arg;
670 int *error;
671 char_u **nextcmd;
672 int skip; /* only parse, don't execute */
673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000674 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int retval = FALSE;
676
677 if (skip)
678 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000679 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 else
682 {
683 *error = FALSE;
684 if (!skip)
685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000686 retval = (get_tv_number(&tv) != 0);
687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 }
690 if (skip)
691 --emsg_skip;
692
693 return retval;
694}
695
696/*
697 * Top level evaluation function, returning a string. If "skip" is TRUE,
698 * only parsing to "nextcmd" is done, without reporting errors. Return
699 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
700 */
701 char_u *
702eval_to_string_skip(arg, nextcmd, skip)
703 char_u *arg;
704 char_u **nextcmd;
705 int skip; /* only parse, don't execute */
706{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000707 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 char_u *retval;
709
710 if (skip)
711 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 retval = NULL;
714 else
715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 retval = vim_strsave(get_tv_string(&tv));
717 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 if (skip)
720 --emsg_skip;
721
722 return retval;
723}
724
725/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000726 * Skip over an expression at "*pp".
727 * Return FAIL for an error, OK otherwise.
728 */
729 int
730skip_expr(pp)
731 char_u **pp;
732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000733 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000734
735 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000736 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000737}
738
739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * Top level evaluation function, returning a string.
741 * Return pointer to allocated memory, or NULL for failure.
742 */
743 char_u *
744eval_to_string(arg, nextcmd)
745 char_u *arg;
746 char_u **nextcmd;
747{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000748 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 char_u *retval;
750
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000751 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 retval = NULL;
753 else
754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000755 retval = vim_strsave(get_tv_string(&tv));
756 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
758
759 return retval;
760}
761
762/*
763 * Call eval_to_string() with "sandbox" set and not using local variables.
764 */
765 char_u *
766eval_to_string_safe(arg, nextcmd)
767 char_u *arg;
768 char_u **nextcmd;
769{
770 char_u *retval;
771 void *save_funccalp;
772
773 save_funccalp = save_funccal();
774 ++sandbox;
775 retval = eval_to_string(arg, nextcmd);
776 --sandbox;
777 restore_funccal(save_funccalp);
778 return retval;
779}
780
781#if 0 /* not used */
782/*
783 * Top level evaluation function, returning a string.
784 * Advances "arg" to the first non-blank after the evaluated expression.
785 * Return pointer to allocated memory, or NULL for failure.
786 * Doesn't give error messages.
787 */
788 char_u *
789eval_arg_to_string(arg)
790 char_u **arg;
791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000792 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 char_u *retval;
794 int ret;
795
796 ++emsg_off;
797
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000798 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (ret == FAIL)
800 retval = NULL;
801 else
802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 retval = vim_strsave(get_tv_string(&rettv));
804 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806
807 --emsg_off;
808
809 return retval;
810}
811#endif
812
813/*
814 * Top level evaluation function, returning a number.
815 * Evaluates "expr" silently.
816 * Returns -1 for an error.
817 */
818 int
819eval_to_number(expr)
820 char_u *expr;
821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 int retval;
824 char_u *p = expr;
825
826 ++emsg_off;
827
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000828 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 retval = -1;
830 else
831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 retval = get_tv_number(&rettv);
833 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835 --emsg_off;
836
837 return retval;
838}
839
840#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
841/*
842 * Call some vimL function and return the result as a string
843 * Uses argv[argc] for the function arguments.
844 */
845 char_u *
846call_vim_function(func, argc, argv, safe)
847 char_u *func;
848 int argc;
849 char_u **argv;
850 int safe; /* use the sandbox */
851{
852 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000854 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 long n;
856 int len;
857 int i;
858 int doesrange;
859 void *save_funccalp = NULL;
860
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000861 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (argvars == NULL)
863 return NULL;
864
865 for (i = 0; i < argc; i++)
866 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000867 /* Pass a NULL or empty argument as an empty string */
868 if (argv[i] == NULL || *argv[i] == NUL)
869 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000870 argvars[i].v_type = VAR_STRING;
871 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000872 continue;
873 }
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 /* Recognize a number argument, the others must be strings. */
876 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
877 if (len != 0 && len == (int)STRLEN(argv[i]))
878 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000879 argvars[i].v_type = VAR_NUMBER;
880 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
882 else
883 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000884 argvars[i].v_type = VAR_STRING;
885 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 }
888
889 if (safe)
890 {
891 save_funccalp = save_funccal();
892 ++sandbox;
893 }
894
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
896 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
898 &doesrange, TRUE) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 vim_free(argvars);
903
904 if (safe)
905 {
906 --sandbox;
907 restore_funccal(save_funccalp);
908 }
909 return retval;
910}
911#endif
912
913/*
914 * Save the current function call pointer, and set it to NULL.
915 * Used when executing autocommands and for ":source".
916 */
917 void *
918save_funccal()
919{
920 struct funccall *fc;
921
922 fc = current_funccal;
923 current_funccal = NULL;
924 return (void *)fc;
925}
926
927 void
928restore_funccal(fc)
929 void *fc;
930{
931 current_funccal = (struct funccall *)fc;
932}
933
934#ifdef FEAT_FOLDING
935/*
936 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
937 * it in "*cp". Doesn't give error messages.
938 */
939 int
940eval_foldexpr(arg, cp)
941 char_u *arg;
942 int *cp;
943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 int retval;
946 char_u *s;
947
948 ++emsg_off;
949 ++sandbox;
950 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 retval = 0;
953 else
954 {
955 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000956 if (tv.v_type == VAR_NUMBER)
957 retval = tv.vval.v_number;
958 else if (tv.v_type == VAR_UNKNOWN
959 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 retval = 0;
961 else
962 {
963 /* If the result is a string, check if there is a non-digit before
964 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (!VIM_ISDIGIT(*s) && *s != '-')
967 *cp = *s++;
968 retval = atol((char *)s);
969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 --emsg_off;
973 --sandbox;
974
975 return retval;
976}
977#endif
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979/*
980 * Expands out the 'magic' {}'s in a variable/function name.
981 * Note that this can call itself recursively, to deal with
982 * constructs like foo{bar}{baz}{bam}
983 * The four pointer arguments point to "foo{expre}ss{ion}bar"
984 * "in_start" ^
985 * "expr_start" ^
986 * "expr_end" ^
987 * "in_end" ^
988 *
989 * Returns a new allocated string, which the caller must free.
990 * Returns NULL for failure.
991 */
992 static char_u *
993make_expanded_name(in_start, expr_start, expr_end, in_end)
994 char_u *in_start;
995 char_u *expr_start;
996 char_u *expr_end;
997 char_u *in_end;
998{
999 char_u c1;
1000 char_u *retval = NULL;
1001 char_u *temp_result;
1002 char_u *nextcmd = NULL;
1003
1004 if (expr_end == NULL || in_end == NULL)
1005 return NULL;
1006 *expr_start = NUL;
1007 *expr_end = NUL;
1008 c1 = *in_end;
1009 *in_end = NUL;
1010
1011 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1012 if (temp_result != NULL && nextcmd == NULL)
1013 {
1014 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1015 + (in_end - expr_end) + 1));
1016
1017 if (retval != NULL)
1018 {
1019 STRCPY(retval, in_start);
1020 STRCAT(retval, temp_result);
1021 STRCAT(retval, expr_end + 1);
1022 }
1023 }
1024 vim_free(temp_result);
1025
1026 *in_end = c1; /* put char back for error messages */
1027 *expr_start = '{';
1028 *expr_end = '}';
1029
1030 if (retval != NULL)
1031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001032 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (expr_start != NULL)
1034 {
1035 /* Further expansion! */
1036 temp_result = make_expanded_name(retval, expr_start,
1037 expr_end, temp_result);
1038 vim_free(retval);
1039 retval = temp_result;
1040 }
1041 }
1042
1043 return retval;
1044
1045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001048 * ":let" list all variable values
1049 * ":let var1 var2" list variable values
1050 * ":let var = expr" assignment command.
1051 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
1053 void
1054ex_let(eap)
1055 exarg_T *eap;
1056{
1057 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001058 char_u *expr = NULL;
1059 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001061 int var_count = 0;
1062 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001064 expr = skip_var_list(arg, &var_count, &semicolon);
1065 if (expr == NULL)
1066 return;
1067 expr = vim_strchr(expr, '=');
1068 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001070 if (*arg == '[')
1071 EMSG(_(e_invarg));
1072 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001073 /* ":let var1 var2" */
1074 arg = list_arg_vars(eap, arg);
1075 else if (!eap->skip)
1076 /* ":let" */
1077 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 eap->nextcmd = check_nextcmd(arg);
1079 }
1080 else
1081 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001082 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (eap->skip)
1085 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001086 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (eap->skip)
1088 {
1089 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 --emsg_skip;
1092 }
1093 else if (i != FAIL)
1094 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001095 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1096 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100}
1101
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001102/*
1103 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1104 * Handles both "var" with any type and "[var, var; var]" with a list type.
1105 * Returns OK or FAIL;
1106 */
1107 static int
1108ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1109 char_u *arg_start;
1110 typeval *tv;
1111 int copy; /* copy values from "tv", don't move */
1112 int semicolon; /* from skip_var_list() */
1113 int var_count; /* from skip_var_list() */
1114 char_u *nextchars; /* characters that must follow or NULL */
1115{
1116 char_u *arg = arg_start;
1117 listvar *l;
1118 int i;
1119 listitem *item;
1120 typeval ltv;
1121
1122 if (*arg != '[')
1123 {
1124 /*
1125 * ":let var = expr" or ":for var in list"
1126 */
1127 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1128 return FAIL;
1129 return OK;
1130 }
1131
1132 /*
1133 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1134 */
1135 l = tv->vval.v_list;
1136 if (tv->v_type != VAR_LIST || l == NULL)
1137 {
1138 EMSG(_(e_listreq));
1139 return FAIL;
1140 }
1141
1142 i = list_len(l);
1143 if (semicolon == 0 && var_count < i)
1144 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001145 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001146 return FAIL;
1147 }
1148 if (var_count - semicolon > i)
1149 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001150 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001151 return FAIL;
1152 }
1153
1154 item = l->lv_first;
1155 while (*arg != ']')
1156 {
1157 arg = skipwhite(arg + 1);
1158 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1159 item = item->li_next;
1160 if (arg == NULL)
1161 return FAIL;
1162
1163 arg = skipwhite(arg);
1164 if (*arg == ';')
1165 {
1166 /* Put the rest of the list (may be empty) in the var after ';'.
1167 * Create a new list for this. */
1168 l = list_alloc();
1169 if (l == NULL)
1170 return FAIL;
1171 while (item != NULL)
1172 {
1173 list_append_tv(l, &item->li_tv);
1174 item = item->li_next;
1175 }
1176
1177 ltv.v_type = VAR_LIST;
1178 ltv.vval.v_list = l;
1179 l->lv_refcount = 1;
1180
1181 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1182 clear_tv(&ltv);
1183 if (arg == NULL)
1184 return FAIL;
1185 break;
1186 }
1187 else if (*arg != ',' && *arg != ']')
1188 {
1189 EMSG2(_(e_intern2), "ex_let_vars()");
1190 return FAIL;
1191 }
1192 }
1193
1194 return OK;
1195}
1196
1197/*
1198 * Skip over assignable variable "var" or list of variables "[var, var]".
1199 * Used for ":let varvar = expr" and ":for varvar in expr".
1200 * For "[var, var]" increment "*var_count" for each variable.
1201 * for "[var, var; var]" set "semicolon".
1202 * Return NULL for an error.
1203 */
1204 static char_u *
1205skip_var_list(arg, var_count, semicolon)
1206 char_u *arg;
1207 int *var_count;
1208 int *semicolon;
1209{
1210 char_u *p, *s;
1211
1212 if (*arg == '[')
1213 {
1214 /* "[var, var]": find the matching ']'. */
1215 p = arg;
1216 while (1)
1217 {
1218 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1219 s = skip_var_one(p);
1220 if (s == p)
1221 {
1222 EMSG2(_(e_invarg2), p);
1223 return NULL;
1224 }
1225 ++*var_count;
1226
1227 p = skipwhite(s);
1228 if (*p == ']')
1229 break;
1230 else if (*p == ';')
1231 {
1232 if (*semicolon == 1)
1233 {
1234 EMSG(_("Double ; in list of variables"));
1235 return NULL;
1236 }
1237 *semicolon = 1;
1238 }
1239 else if (*p != ',')
1240 {
1241 EMSG2(_(e_invarg2), p);
1242 return NULL;
1243 }
1244 }
1245 return p + 1;
1246 }
1247 else
1248 return skip_var_one(arg);
1249}
1250
1251 static char_u *
1252skip_var_one(arg)
1253 char_u *arg;
1254{
1255 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1256 ++arg;
1257 return find_name_end(arg, NULL, NULL, TRUE);
1258}
1259
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260 static void
1261list_all_vars()
1262{
1263 int i;
1264
1265 /*
1266 * List all variables.
1267 */
1268 for (i = 0; i < variables.ga_len && !got_int; ++i)
1269 if (VAR_ENTRY(i).v_name != NULL)
1270 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1271 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1272 if (BVAR_ENTRY(i).v_name != NULL)
1273 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1274 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1275 if (WVAR_ENTRY(i).v_name != NULL)
1276 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1277 for (i = 0; i < VV_LEN && !got_int; ++i)
1278 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
1279 list_vim_var(i);
1280}
1281
1282/*
1283 * List variables in "arg".
1284 */
1285 static char_u *
1286list_arg_vars(eap, arg)
1287 exarg_T *eap;
1288 char_u *arg;
1289{
1290 int error = FALSE;
1291 char_u *temp_string = NULL;
1292 int arg_len;
1293 char_u *expr_start;
1294 char_u *expr_end;
1295 char_u *name_end;
1296 int c1 = 0, c2;
1297 int i;
1298 VAR varp;
1299 char_u *name;
1300
1301 while (!ends_excmd(*arg) && !got_int)
1302 {
1303 /* Find the end of the name. */
1304 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1305
1306 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1307 {
1308 emsg_severe = TRUE;
1309 EMSG(_(e_trailing));
1310 break;
1311 }
1312 if (!error && !eap->skip)
1313 {
1314 if (expr_start != NULL)
1315 {
1316 temp_string = make_expanded_name(arg, expr_start,
1317 expr_end, name_end);
1318 if (temp_string == NULL)
1319 {
1320 /*
1321 * Report an invalid expression in braces, unless
1322 * the expression evaluation has been cancelled due
1323 * to an aborting error, an interrupt, or an
1324 * exception.
1325 */
1326 if (!aborting())
1327 {
1328 emsg_severe = TRUE;
1329 EMSG2(_(e_invarg2), arg);
1330 break;
1331 }
1332 error = TRUE;
1333 arg = skipwhite(name_end);
1334 continue;
1335 }
1336 arg = temp_string;
1337 arg_len = STRLEN(temp_string);
1338 }
1339 else
1340 {
1341 c1 = *name_end;
1342 *name_end = NUL;
1343 arg_len = (int)(name_end - arg);
1344 }
1345 i = find_vim_var(arg, arg_len);
1346 if (i >= 0)
1347 list_vim_var(i);
1348 else if (STRCMP("b:changedtick", arg) == 0)
1349 {
1350 char_u numbuf[NUMBUFLEN];
1351
1352 sprintf((char *)numbuf, "%ld",
1353 (long)curbuf->b_changedtick);
1354 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1355 VAR_NUMBER, numbuf);
1356 }
1357 else
1358 {
1359 varp = find_var(arg, FALSE);
1360 if (varp == NULL)
1361 {
1362 /* Skip further arguments but do continue to
1363 * search for a trailing command. */
1364 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1365 error = TRUE;
1366 }
1367 else
1368 {
1369 name = vim_strchr(arg, ':');
1370 if (name != NULL)
1371 {
1372 /* "a:" vars have no name stored, use whole arg */
1373 if (arg[0] == 'a' && arg[1] == ':')
1374 c2 = NUL;
1375 else
1376 {
1377 c2 = *++name;
1378 *name = NUL;
1379 }
1380 list_one_var(varp, arg);
1381 if (c2 != NUL)
1382 *name = c2;
1383 }
1384 else
1385 list_one_var(varp, (char_u *)"");
1386 }
1387 }
1388 if (expr_start != NULL)
1389 vim_free(temp_string);
1390 else
1391 *name_end = c1;
1392 }
1393 arg = skipwhite(name_end);
1394 }
1395
1396 return arg;
1397}
1398
1399/*
1400 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1401 * Returns a pointer to the char just after the var name.
1402 * Returns NULL if there is an error.
1403 */
1404 static char_u *
1405ex_let_one(arg, tv, copy, endchars)
1406 char_u *arg; /* points to variable name */
1407 typeval *tv; /* value to assign to variable */
1408 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001409 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001410{
1411 int c1;
1412 char_u *name;
1413 char_u *p;
1414 char_u *arg_end = NULL;
1415 int len;
1416 int opt_flags;
1417
1418 /*
1419 * ":let $VAR = expr": Set environment variable.
1420 */
1421 if (*arg == '$')
1422 {
1423 /* Find the end of the name. */
1424 ++arg;
1425 name = arg;
1426 len = get_env_len(&arg);
1427 if (len == 0)
1428 EMSG2(_(e_invarg2), name - 1);
1429 else
1430 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 if (endchars != NULL
1432 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 EMSG(_(e_letunexp));
1434 else
1435 {
1436 c1 = name[len];
1437 name[len] = NUL;
1438 p = get_tv_string(tv);
1439 vim_setenv(name, p);
1440 if (STRICMP(name, "HOME") == 0)
1441 init_homedir();
1442 else if (didset_vim && STRICMP(name, "VIM") == 0)
1443 didset_vim = FALSE;
1444 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1445 didset_vimruntime = FALSE;
1446 name[len] = c1;
1447 arg_end = arg;
1448 }
1449 }
1450 }
1451
1452 /*
1453 * ":let &option = expr": Set option value.
1454 * ":let &l:option = expr": Set local option value.
1455 * ":let &g:option = expr": Set global option value.
1456 */
1457 else if (*arg == '&')
1458 {
1459 /* Find the end of the name. */
1460 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001461 if (p == NULL || (endchars != NULL
1462 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001463 EMSG(_(e_letunexp));
1464 else
1465 {
1466 c1 = *p;
1467 *p = NUL;
1468 set_option_value(arg, get_tv_number(tv),
1469 get_tv_string(tv), opt_flags);
1470 *p = c1;
1471 arg_end = p;
1472 }
1473 }
1474
1475 /*
1476 * ":let @r = expr": Set register contents.
1477 */
1478 else if (*arg == '@')
1479 {
1480 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 if (endchars != NULL
1482 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 EMSG(_(e_letunexp));
1484 else
1485 {
1486 write_reg_contents(*arg == '@' ? '"' : *arg,
1487 get_tv_string(tv), -1, FALSE);
1488 arg_end = arg + 1;
1489 }
1490 }
1491
1492 /*
1493 * ":let var = expr": Set internal variable.
1494 */
1495 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1496 {
1497 char_u *exp_name = NULL;
1498 char_u *expr_start, *expr_end;
1499
1500 /* Find the end of the name. */
1501 p = find_name_end(arg, &expr_start, &expr_end, FALSE);
1502 if (expr_start != NULL)
1503 {
1504 exp_name = make_expanded_name(arg, expr_start, expr_end, p);
1505 arg = exp_name;
1506 }
1507
1508 if (arg == NULL)
1509 {
1510 /* Report an invalid expression in braces, unless the
1511 * expression evaluation has been cancelled due to an
1512 * aborting error, an interrupt, or an exception. */
1513 if (!aborting())
1514 EMSG2(_(e_invarg2), arg);
1515 }
1516 else if (*p == '[')
1517 arg_end = set_var_idx(arg, p, tv, copy, endchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001518 else if (endchars != NULL
1519 && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 EMSG(_(e_letunexp));
1521 else if (STRNCMP(arg, "b:changedtick", 13) == 0
1522 && !eval_isnamec(arg[13]))
1523 EMSG2(_(e_readonlyvar), arg);
1524 else
1525 {
1526 c1 = *p;
1527 *p = NUL;
1528 set_var(arg, tv, copy);
1529 *p = c1;
1530 arg_end = p;
1531 }
1532
1533 vim_free(exp_name);
1534 }
1535
1536 else
1537 EMSG2(_(e_invarg2), arg);
1538
1539 return arg_end;
1540}
1541
1542/*
1543 * Set a variable with an index: "name[expr]", "name[expr][expr]", etc.
1544 * Only works if "name" is an existing List.
1545 * "ip" points to the first '['.
1546 * Returns a pointer to just after the last used ']'; NULL for error.
1547 */
1548 static char_u *
1549set_var_idx(name, ip, rettv, copy, endchars)
1550 char_u *name;
1551 char_u *ip;
1552 typeval *rettv;
1553 int copy;
1554 char_u *endchars;
1555{
1556 VAR v;
1557 int c1;
1558 char_u *p;
1559 typeval var1;
1560 typeval *tv;
1561 long n;
1562 listitem *item;
1563
1564 c1 = *ip;
1565 *ip = NUL;
1566 v = find_var(name, TRUE);
1567 if (v == NULL)
1568 EMSG2(_(e_undefvar), name);
1569 *ip = c1;
1570 if (v == NULL)
1571 return NULL;
1572
1573 tv = &v->tv;
1574 for (p = ip; *p == '['; p = skipwhite(p + 1))
1575 {
1576 if (tv->v_type != VAR_LIST || tv->vval.v_list == NULL)
1577 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001578 EMSG(_("E689: Can only index a List"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001579 p = NULL;
1580 break;
1581 }
1582 p = skipwhite(p + 1);
1583 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
1584 {
1585 p = NULL;
1586 break;
1587 }
1588 if (*p != ']')
1589 {
1590 EMSG(_(e_missbrac));
1591 clear_tv(&var1);
1592 p = NULL;
1593 break;
1594 }
1595 n = get_tv_number(&var1);
1596 clear_tv(&var1);
1597 item = list_find(tv->vval.v_list, n);
1598 if (item == NULL)
1599 {
1600 EMSGN(_(e_listidx), n);
1601 p = NULL;
1602 break;
1603 }
1604 tv = &item->li_tv;
1605 }
1606
1607 if (p != NULL)
1608 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 {
1611 EMSG(_(e_letunexp));
1612 p = NULL;
1613 }
1614 else
1615 {
1616 clear_tv(tv);
1617 if (copy)
1618 copy_tv(tv, rettv);
1619 else
1620 {
1621 *tv = *rettv;
1622 init_tv(rettv);
1623 }
1624 }
1625 }
1626 return p;
1627}
1628
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629/*
1630 * Add a watcher to a list.
1631 */
1632 static void
1633list_add_watch(l, lw)
1634 listvar *l;
1635 listwatch *lw;
1636{
1637 lw->lw_next = l->lv_watch;
1638 l->lv_watch = lw;
1639}
1640
1641/*
1642 * Remove a watches from a list.
1643 * No warning when it isn't found...
1644 */
1645 static void
1646list_rem_watch(l, lwrem)
1647 listvar *l;
1648 listwatch *lwrem;
1649{
1650 listwatch *lw, **lwp;
1651
1652 lwp = &l->lv_watch;
1653 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1654 {
1655 if (lw == lwrem)
1656 {
1657 *lwp = lw->lw_next;
1658 break;
1659 }
1660 lwp = &lw->lw_next;
1661 }
1662}
1663
1664/*
1665 * Just before removing an item from a list: advance watchers to the next
1666 * item.
1667 */
1668 static void
1669list_fix_watch(l, item)
1670 listvar *l;
1671 listitem *item;
1672{
1673 listwatch *lw;
1674
1675 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1676 if (lw->lw_item == item)
1677 lw->lw_item = item->li_next;
1678}
1679
1680/*
1681 * Evaluate the expression used in a ":for var in expr" command.
1682 * "arg" points to "var".
1683 * Set "*errp" to TRUE for an error, FALSE otherwise;
1684 * Return a pointer that holds the info. Null when there is an error.
1685 */
1686 void *
1687eval_for_line(arg, errp, nextcmdp, skip)
1688 char_u *arg;
1689 int *errp;
1690 char_u **nextcmdp;
1691 int skip;
1692{
1693 forinfo *fi;
1694 char_u *expr;
1695 typeval tv;
1696 listvar *l;
1697
1698 *errp = TRUE; /* default: there is an error */
1699
1700 fi = (forinfo *)alloc_clear(sizeof(forinfo));
1701 if (fi == NULL)
1702 return NULL;
1703
1704 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1705 if (expr == NULL)
1706 return fi;
1707
1708 expr = skipwhite(expr);
1709 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
1710 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001711 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 return fi;
1713 }
1714
1715 if (skip)
1716 ++emsg_skip;
1717 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1718 {
1719 *errp = FALSE;
1720 if (!skip)
1721 {
1722 l = tv.vval.v_list;
1723 if (tv.v_type != VAR_LIST || l == NULL)
1724 EMSG(_(e_listreq));
1725 else
1726 {
1727 fi->fi_list = l;
1728 list_add_watch(l, &fi->fi_lw);
1729 fi->fi_lw.lw_item = l->lv_first;
1730 }
1731 }
1732 }
1733 if (skip)
1734 --emsg_skip;
1735
1736 return fi;
1737}
1738
1739/*
1740 * Use the first item in a ":for" list. Advance to the next.
1741 * Assign the values to the variable (list). "arg" points to the first one.
1742 * Return TRUE when a valid item was found, FALSE when at end of list or
1743 * something wrong.
1744 */
1745 int
1746next_for_item(fi_void, arg)
1747 void *fi_void;
1748 char_u *arg;
1749{
1750 forinfo *fi = (forinfo *)fi_void;
1751 int result;
1752 listitem *item;
1753
1754 item = fi->fi_lw.lw_item;
1755 if (item == NULL)
1756 result = FALSE;
1757 else
1758 {
1759 fi->fi_lw.lw_item = item->li_next;
1760 result = (ex_let_vars(arg, &item->li_tv, TRUE,
1761 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
1762 }
1763 return result;
1764}
1765
1766/*
1767 * Free the structure used to store info used by ":for".
1768 */
1769 void
1770free_for_info(fi_void)
1771 void *fi_void;
1772{
1773 forinfo *fi = (forinfo *)fi_void;
1774
1775 if (fi->fi_list != NULL)
1776 list_rem_watch(fi->fi_list, &fi->fi_lw);
1777 vim_free(fi);
1778}
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1781
1782 void
1783set_context_for_expression(xp, arg, cmdidx)
1784 expand_T *xp;
1785 char_u *arg;
1786 cmdidx_T cmdidx;
1787{
1788 int got_eq = FALSE;
1789 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 if (cmdidx == CMD_let)
1793 {
1794 xp->xp_context = EXPAND_USER_VARS;
1795 if (vim_strchr(arg, '=') == NULL)
1796 {
1797 /* ":let var1 var2 ...": find last space. */
1798 for (p = arg + STRLEN(arg); p > arg; )
1799 {
1800 xp->xp_pattern = p;
1801 p = mb_ptr_back(arg, p);
1802 if (vim_iswhite(*p))
1803 break;
1804 }
1805 return;
1806 }
1807 }
1808 else
1809 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1810 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 while ((xp->xp_pattern = vim_strpbrk(arg,
1812 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1813 {
1814 c = *xp->xp_pattern;
1815 if (c == '&')
1816 {
1817 c = xp->xp_pattern[1];
1818 if (c == '&')
1819 {
1820 ++xp->xp_pattern;
1821 xp->xp_context = cmdidx != CMD_let || got_eq
1822 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1823 }
1824 else if (c != ' ')
1825 xp->xp_context = EXPAND_SETTINGS;
1826 }
1827 else if (c == '$')
1828 {
1829 /* environment variable */
1830 xp->xp_context = EXPAND_ENV_VARS;
1831 }
1832 else if (c == '=')
1833 {
1834 got_eq = TRUE;
1835 xp->xp_context = EXPAND_EXPRESSION;
1836 }
1837 else if (c == '<'
1838 && xp->xp_context == EXPAND_FUNCTIONS
1839 && vim_strchr(xp->xp_pattern, '(') == NULL)
1840 {
1841 /* Function name can start with "<SNR>" */
1842 break;
1843 }
1844 else if (cmdidx != CMD_let || got_eq)
1845 {
1846 if (c == '"') /* string */
1847 {
1848 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1849 if (c == '\\' && xp->xp_pattern[1] != NUL)
1850 ++xp->xp_pattern;
1851 xp->xp_context = EXPAND_NOTHING;
1852 }
1853 else if (c == '\'') /* literal string */
1854 {
1855 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1856 /* skip */ ;
1857 xp->xp_context = EXPAND_NOTHING;
1858 }
1859 else if (c == '|')
1860 {
1861 if (xp->xp_pattern[1] == '|')
1862 {
1863 ++xp->xp_pattern;
1864 xp->xp_context = EXPAND_EXPRESSION;
1865 }
1866 else
1867 xp->xp_context = EXPAND_COMMANDS;
1868 }
1869 else
1870 xp->xp_context = EXPAND_EXPRESSION;
1871 }
1872 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 /* Doesn't look like something valid, expand as an expression
1874 * anyway. */
1875 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 arg = xp->xp_pattern;
1877 if (*arg != NUL)
1878 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1879 /* skip */ ;
1880 }
1881 xp->xp_pattern = arg;
1882}
1883
1884#endif /* FEAT_CMDL_COMPL */
1885
1886/*
1887 * ":1,25call func(arg1, arg2)" function call.
1888 */
1889 void
1890ex_call(eap)
1891 exarg_T *eap;
1892{
1893 char_u *arg = eap->arg;
1894 char_u *startarg;
1895 char_u *alias;
1896 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int len;
1899 linenr_T lnum;
1900 int doesrange;
1901 int failed = FALSE;
1902
1903 name = arg;
1904 len = get_func_len(&arg, &alias, !eap->skip);
1905 if (len == 0)
1906 goto end;
1907 if (alias != NULL)
1908 name = alias;
1909
1910 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 if (*startarg != '(')
1914 {
1915 EMSG2(_("E107: Missing braces: %s"), name);
1916 goto end;
1917 }
1918
1919 /*
1920 * When skipping, evaluate the function once, to find the end of the
1921 * arguments.
1922 * When the function takes a range, this is discovered after the first
1923 * call, and the loop is broken.
1924 */
1925 if (eap->skip)
1926 {
1927 ++emsg_skip;
1928 lnum = eap->line2; /* do it once, also with an invalid range */
1929 }
1930 else
1931 lnum = eap->line1;
1932 for ( ; lnum <= eap->line2; ++lnum)
1933 {
1934 if (!eap->skip && eap->addr_count > 0)
1935 {
1936 curwin->w_cursor.lnum = lnum;
1937 curwin->w_cursor.col = 0;
1938 }
1939 arg = startarg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 if (get_func_tv(name, len, &rettv, &arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
1942 {
1943 failed = TRUE;
1944 break;
1945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (doesrange || eap->skip)
1948 break;
1949 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001950 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001952 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (aborting())
1954 break;
1955 }
1956 if (eap->skip)
1957 --emsg_skip;
1958
1959 if (!failed)
1960 {
1961 /* Check for trailing illegal characters and a following command. */
1962 if (!ends_excmd(*arg))
1963 {
1964 emsg_severe = TRUE;
1965 EMSG(_(e_trailing));
1966 }
1967 else
1968 eap->nextcmd = check_nextcmd(arg);
1969 }
1970
1971end:
1972 if (alias != NULL)
1973 vim_free(alias);
1974}
1975
1976/*
1977 * ":unlet[!] var1 ... " command.
1978 */
1979 void
1980ex_unlet(eap)
1981 exarg_T *eap;
1982{
1983 char_u *arg = eap->arg;
1984 char_u *name_end;
1985 char_u cc;
1986 char_u *expr_start;
1987 char_u *expr_end;
1988 int error = FALSE;
1989
1990 do
1991 {
1992 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1996 {
1997 emsg_severe = TRUE;
1998 EMSG(_(e_trailing));
1999 break;
2000 }
2001
2002 if (!error && !eap->skip)
2003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (expr_start != NULL)
2005 {
2006 char_u *temp_string;
2007
2008 temp_string = make_expanded_name(arg, expr_start,
2009 expr_end, name_end);
2010 if (temp_string == NULL)
2011 {
2012 /*
2013 * Report an invalid expression in braces, unless the
2014 * expression evaluation has been cancelled due to an
2015 * aborting error, an interrupt, or an exception.
2016 */
2017 if (!aborting())
2018 {
2019 emsg_severe = TRUE;
2020 EMSG2(_(e_invarg2), arg);
2021 break;
2022 }
2023 error = TRUE;
2024 }
2025 else
2026 {
2027 if (do_unlet(temp_string) == FAIL && !eap->forceit)
2028 {
2029 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
2030 error = TRUE;
2031 }
2032 vim_free(temp_string);
2033 }
2034 }
2035 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 cc = *name_end;
2038 *name_end = NUL;
2039
2040 if (do_unlet(arg) == FAIL && !eap->forceit)
2041 {
2042 EMSG2(_("E108: No such variable: \"%s\""), arg);
2043 error = TRUE;
2044 }
2045
2046 *name_end = cc;
2047 }
2048 }
2049 arg = skipwhite(name_end);
2050 } while (!ends_excmd(*arg));
2051
2052 eap->nextcmd = check_nextcmd(arg);
2053}
2054
2055/*
2056 * "unlet" a variable. Return OK if it existed, FAIL if not.
2057 */
2058 int
2059do_unlet(name)
2060 char_u *name;
2061{
2062 VAR v;
2063
2064 v = find_var(name, TRUE);
2065 if (v != NULL)
2066 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002067 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return OK;
2069 }
2070 return FAIL;
2071}
2072
2073#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2074/*
2075 * Delete all "menutrans_" variables.
2076 */
2077 void
2078del_menutrans_vars()
2079{
2080 int i;
2081
2082 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002083 if (VAR_ENTRY(i).v_name != NULL
2084 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2085 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086}
2087#endif
2088
2089#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2090
2091/*
2092 * Local string buffer for the next two functions to store a variable name
2093 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2094 * get_user_var_name().
2095 */
2096
2097static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2098
2099static char_u *varnamebuf = NULL;
2100static int varnamebuflen = 0;
2101
2102/*
2103 * Function to concatenate a prefix and a variable name.
2104 */
2105 static char_u *
2106cat_prefix_varname(prefix, name)
2107 int prefix;
2108 char_u *name;
2109{
2110 int len;
2111
2112 len = (int)STRLEN(name) + 3;
2113 if (len > varnamebuflen)
2114 {
2115 vim_free(varnamebuf);
2116 len += 10; /* some additional space */
2117 varnamebuf = alloc(len);
2118 if (varnamebuf == NULL)
2119 {
2120 varnamebuflen = 0;
2121 return NULL;
2122 }
2123 varnamebuflen = len;
2124 }
2125 *varnamebuf = prefix;
2126 varnamebuf[1] = ':';
2127 STRCPY(varnamebuf + 2, name);
2128 return varnamebuf;
2129}
2130
2131/*
2132 * Function given to ExpandGeneric() to obtain the list of user defined
2133 * (global/buffer/window/built-in) variable names.
2134 */
2135/*ARGSUSED*/
2136 char_u *
2137get_user_var_name(xp, idx)
2138 expand_T *xp;
2139 int idx;
2140{
2141 static int gidx;
2142 static int bidx;
2143 static int widx;
2144 static int vidx;
2145 char_u *name;
2146
2147 if (idx == 0)
2148 gidx = bidx = widx = vidx = 0;
2149 if (gidx < variables.ga_len) /* Global variables */
2150 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002151 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 && gidx < variables.ga_len)
2153 /* skip */;
2154 if (name != NULL)
2155 {
2156 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2157 return cat_prefix_varname('g', name);
2158 else
2159 return name;
2160 }
2161 }
2162 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2163 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002164 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 && bidx < curbuf->b_vars.ga_len)
2166 /* skip */;
2167 if (name != NULL)
2168 return cat_prefix_varname('b', name);
2169 }
2170 if (bidx == curbuf->b_vars.ga_len)
2171 {
2172 ++bidx;
2173 return (char_u *)"b:changedtick";
2174 }
2175 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2176 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002177 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 && widx < curwin->w_vars.ga_len)
2179 /* skip */;
2180 if (name != NULL)
2181 return cat_prefix_varname('w', name);
2182 }
2183 if (vidx < VV_LEN) /* Built-in variables */
2184 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2185
2186 vim_free(varnamebuf);
2187 varnamebuf = NULL;
2188 varnamebuflen = 0;
2189 return NULL;
2190}
2191
2192#endif /* FEAT_CMDL_COMPL */
2193
2194/*
2195 * types for expressions.
2196 */
2197typedef enum
2198{
2199 TYPE_UNKNOWN = 0
2200 , TYPE_EQUAL /* == */
2201 , TYPE_NEQUAL /* != */
2202 , TYPE_GREATER /* > */
2203 , TYPE_GEQUAL /* >= */
2204 , TYPE_SMALLER /* < */
2205 , TYPE_SEQUAL /* <= */
2206 , TYPE_MATCH /* =~ */
2207 , TYPE_NOMATCH /* !~ */
2208} exptype_T;
2209
2210/*
2211 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2214 */
2215
2216/*
2217 * Handle zero level expression.
2218 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * Return OK or FAIL.
2221 */
2222 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 char_u **nextcmd;
2227 int evaluate;
2228{
2229 int ret;
2230 char_u *p;
2231
2232 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (ret == FAIL || !ends_excmd(*p))
2235 {
2236 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 /*
2239 * Report the invalid expression unless the expression evaluation has
2240 * been cancelled due to an aborting error, an interrupt, or an
2241 * exception.
2242 */
2243 if (!aborting())
2244 EMSG2(_(e_invexpr2), arg);
2245 ret = FAIL;
2246 }
2247 if (nextcmd != NULL)
2248 *nextcmd = check_nextcmd(p);
2249
2250 return ret;
2251}
2252
2253/*
2254 * Handle top level expression:
2255 * expr1 ? expr0 : expr0
2256 *
2257 * "arg" must point to the first non-white of the expression.
2258 * "arg" is advanced to the next non-white after the recognized expression.
2259 *
2260 * Return OK or FAIL.
2261 */
2262 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 int evaluate;
2267{
2268 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002269 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
2271 /*
2272 * Get the first variable.
2273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 return FAIL;
2276
2277 if ((*arg)[0] == '?')
2278 {
2279 result = FALSE;
2280 if (evaluate)
2281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286
2287 /*
2288 * Get the second variable.
2289 */
2290 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return FAIL;
2293
2294 /*
2295 * Check for the ":".
2296 */
2297 if ((*arg)[0] != ':')
2298 {
2299 EMSG(_("E109: Missing ':' after '?'"));
2300 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 return FAIL;
2303 }
2304
2305 /*
2306 * Get the third variable.
2307 */
2308 *arg = skipwhite(*arg + 1);
2309 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2310 {
2311 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 return FAIL;
2314 }
2315 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318
2319 return OK;
2320}
2321
2322/*
2323 * Handle first level expression:
2324 * expr2 || expr2 || expr2 logical OR
2325 *
2326 * "arg" must point to the first non-white of the expression.
2327 * "arg" is advanced to the next non-white after the recognized expression.
2328 *
2329 * Return OK or FAIL.
2330 */
2331 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 int evaluate;
2336{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002337 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 long result;
2339 int first;
2340
2341 /*
2342 * Get the first variable.
2343 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 return FAIL;
2346
2347 /*
2348 * Repeat until there is no following "||".
2349 */
2350 first = TRUE;
2351 result = FALSE;
2352 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2353 {
2354 if (evaluate && first)
2355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 first = FALSE;
2360 }
2361
2362 /*
2363 * Get the second variable.
2364 */
2365 *arg = skipwhite(*arg + 2);
2366 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2367 return FAIL;
2368
2369 /*
2370 * Compute the result.
2371 */
2372 if (evaluate && !result)
2373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 if (evaluate)
2379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 rettv->v_type = VAR_NUMBER;
2381 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
2383 }
2384
2385 return OK;
2386}
2387
2388/*
2389 * Handle second level expression:
2390 * expr3 && expr3 && expr3 logical AND
2391 *
2392 * "arg" must point to the first non-white of the expression.
2393 * "arg" is advanced to the next non-white after the recognized expression.
2394 *
2395 * Return OK or FAIL.
2396 */
2397 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 int evaluate;
2402{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002403 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 long result;
2405 int first;
2406
2407 /*
2408 * Get the first variable.
2409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 return FAIL;
2412
2413 /*
2414 * Repeat until there is no following "&&".
2415 */
2416 first = TRUE;
2417 result = TRUE;
2418 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2419 {
2420 if (evaluate && first)
2421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 first = FALSE;
2426 }
2427
2428 /*
2429 * Get the second variable.
2430 */
2431 *arg = skipwhite(*arg + 2);
2432 if (eval4(arg, &var2, evaluate && result) == FAIL)
2433 return FAIL;
2434
2435 /*
2436 * Compute the result.
2437 */
2438 if (evaluate && result)
2439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444 if (evaluate)
2445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 rettv->v_type = VAR_NUMBER;
2447 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449 }
2450
2451 return OK;
2452}
2453
2454/*
2455 * Handle third level expression:
2456 * var1 == var2
2457 * var1 =~ var2
2458 * var1 != var2
2459 * var1 !~ var2
2460 * var1 > var2
2461 * var1 >= var2
2462 * var1 < var2
2463 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002464 * var1 is var2
2465 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 *
2467 * "arg" must point to the first non-white of the expression.
2468 * "arg" is advanced to the next non-white after the recognized expression.
2469 *
2470 * Return OK or FAIL.
2471 */
2472 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 int evaluate;
2477{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002478 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 char_u *p;
2480 int i;
2481 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002482 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 int len = 2;
2484 long n1, n2;
2485 char_u *s1, *s2;
2486 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2487 regmatch_T regmatch;
2488 int ic;
2489 char_u *save_cpo;
2490
2491 /*
2492 * Get the first variable.
2493 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return FAIL;
2496
2497 p = *arg;
2498 switch (p[0])
2499 {
2500 case '=': if (p[1] == '=')
2501 type = TYPE_EQUAL;
2502 else if (p[1] == '~')
2503 type = TYPE_MATCH;
2504 break;
2505 case '!': if (p[1] == '=')
2506 type = TYPE_NEQUAL;
2507 else if (p[1] == '~')
2508 type = TYPE_NOMATCH;
2509 break;
2510 case '>': if (p[1] != '=')
2511 {
2512 type = TYPE_GREATER;
2513 len = 1;
2514 }
2515 else
2516 type = TYPE_GEQUAL;
2517 break;
2518 case '<': if (p[1] != '=')
2519 {
2520 type = TYPE_SMALLER;
2521 len = 1;
2522 }
2523 else
2524 type = TYPE_SEQUAL;
2525 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002526 case 'i': if (p[1] == 's')
2527 {
2528 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2529 len = 5;
2530 if (!vim_isIDc(p[len]))
2531 {
2532 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2533 type_is = TRUE;
2534 }
2535 }
2536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538
2539 /*
2540 * If there is a comparitive operator, use it.
2541 */
2542 if (type != TYPE_UNKNOWN)
2543 {
2544 /* extra question mark appended: ignore case */
2545 if (p[len] == '?')
2546 {
2547 ic = TRUE;
2548 ++len;
2549 }
2550 /* extra '#' appended: match case */
2551 else if (p[len] == '#')
2552 {
2553 ic = FALSE;
2554 ++len;
2555 }
2556 /* nothing appened: use 'ignorecase' */
2557 else
2558 ic = p_ic;
2559
2560 /*
2561 * Get the second variable.
2562 */
2563 *arg = skipwhite(p + len);
2564 if (eval5(arg, &var2, evaluate) == FAIL)
2565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return FAIL;
2568 }
2569
2570 if (evaluate)
2571 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002572 if (type_is && rettv->v_type != var2.v_type)
2573 {
2574 /* For "is" a different type always means FALSE, for "notis"
2575 * it means TRUE. */
2576 n1 = (type == TYPE_NEQUAL);
2577 }
2578 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
2579 {
2580 if (type_is)
2581 {
2582 n1 = (rettv->v_type == var2.v_type
2583 && rettv->vval.v_list == var2.vval.v_list);
2584 if (type == TYPE_NEQUAL)
2585 n1 = !n1;
2586 }
2587 else if (rettv->v_type != var2.v_type
2588 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2589 {
2590 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002591 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002592 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002593 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002594 clear_tv(rettv);
2595 clear_tv(&var2);
2596 return FAIL;
2597 }
2598 else
2599 {
2600 /* Compare two Lists for being equal or unequal. */
2601 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
2602 if (type == TYPE_NEQUAL)
2603 n1 = !n1;
2604 }
2605 }
2606
2607 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
2608 {
2609 if (rettv->v_type != var2.v_type
2610 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2611 {
2612 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002613 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002614 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002615 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002616 clear_tv(rettv);
2617 clear_tv(&var2);
2618 return FAIL;
2619 }
2620 else
2621 {
2622 /* Compare two Funcrefs for being equal or unequal. */
2623 if (rettv->vval.v_string == NULL
2624 || var2.vval.v_string == NULL)
2625 n1 = FALSE;
2626 else
2627 n1 = STRCMP(rettv->vval.v_string,
2628 var2.vval.v_string) == 0;
2629 if (type == TYPE_NEQUAL)
2630 n1 = !n1;
2631 }
2632 }
2633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 /*
2635 * If one of the two variables is a number, compare as a number.
2636 * When using "=~" or "!~", always compare as string.
2637 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002638 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 n1 = get_tv_number(rettv);
2642 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 switch (type)
2644 {
2645 case TYPE_EQUAL: n1 = (n1 == n2); break;
2646 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2647 case TYPE_GREATER: n1 = (n1 > n2); break;
2648 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2649 case TYPE_SMALLER: n1 = (n1 < n2); break;
2650 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2651 case TYPE_UNKNOWN:
2652 case TYPE_MATCH:
2653 case TYPE_NOMATCH: break; /* avoid gcc warning */
2654 }
2655 }
2656 else
2657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 s1 = get_tv_string_buf(rettv, buf1);
2659 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2661 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2662 else
2663 i = 0;
2664 n1 = FALSE;
2665 switch (type)
2666 {
2667 case TYPE_EQUAL: n1 = (i == 0); break;
2668 case TYPE_NEQUAL: n1 = (i != 0); break;
2669 case TYPE_GREATER: n1 = (i > 0); break;
2670 case TYPE_GEQUAL: n1 = (i >= 0); break;
2671 case TYPE_SMALLER: n1 = (i < 0); break;
2672 case TYPE_SEQUAL: n1 = (i <= 0); break;
2673
2674 case TYPE_MATCH:
2675 case TYPE_NOMATCH:
2676 /* avoid 'l' flag in 'cpoptions' */
2677 save_cpo = p_cpo;
2678 p_cpo = (char_u *)"";
2679 regmatch.regprog = vim_regcomp(s2,
2680 RE_MAGIC + RE_STRING);
2681 regmatch.rm_ic = ic;
2682 if (regmatch.regprog != NULL)
2683 {
2684 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2685 vim_free(regmatch.regprog);
2686 if (type == TYPE_NOMATCH)
2687 n1 = !n1;
2688 }
2689 p_cpo = save_cpo;
2690 break;
2691
2692 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2693 }
2694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 clear_tv(rettv);
2696 clear_tv(&var2);
2697 rettv->v_type = VAR_NUMBER;
2698 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 }
2701
2702 return OK;
2703}
2704
2705/*
2706 * Handle fourth level expression:
2707 * + number addition
2708 * - number subtraction
2709 * . string concatenation
2710 *
2711 * "arg" must point to the first non-white of the expression.
2712 * "arg" is advanced to the next non-white after the recognized expression.
2713 *
2714 * Return OK or FAIL.
2715 */
2716 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002719 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 int evaluate;
2721{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002722 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002723 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int op;
2725 long n1, n2;
2726 char_u *s1, *s2;
2727 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2728 char_u *p;
2729
2730 /*
2731 * Get the first variable.
2732 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002733 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 return FAIL;
2735
2736 /*
2737 * Repeat computing, until no '+', '-' or '.' is following.
2738 */
2739 for (;;)
2740 {
2741 op = **arg;
2742 if (op != '+' && op != '-' && op != '.')
2743 break;
2744
2745 /*
2746 * Get the second variable.
2747 */
2748 *arg = skipwhite(*arg + 1);
2749 if (eval6(arg, &var2, evaluate) == FAIL)
2750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 return FAIL;
2753 }
2754
2755 if (evaluate)
2756 {
2757 /*
2758 * Compute the result.
2759 */
2760 if (op == '.')
2761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002762 s1 = get_tv_string_buf(rettv, buf1);
2763 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 op = (int)STRLEN(s1);
2765 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2766 if (p != NULL)
2767 {
2768 STRCPY(p, s1);
2769 STRCPY(p + op, s2);
2770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002771 clear_tv(rettv);
2772 rettv->v_type = VAR_STRING;
2773 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002775 else if (rettv->v_type == VAR_LIST && var2.v_type == VAR_LIST)
2776 {
2777 /* concatenate Lists */
2778 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2779 &var3) == FAIL)
2780 {
2781 clear_tv(rettv);
2782 clear_tv(&var2);
2783 return FAIL;
2784 }
2785 clear_tv(rettv);
2786 *rettv = var3;
2787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
2789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002790 n1 = get_tv_number(rettv);
2791 n2 = get_tv_number(&var2);
2792 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (op == '+')
2794 n1 = n1 + n2;
2795 else
2796 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002797 rettv->v_type = VAR_NUMBER;
2798 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802 }
2803 return OK;
2804}
2805
2806/*
2807 * Handle fifth level expression:
2808 * * number multiplication
2809 * / number division
2810 * % number modulo
2811 *
2812 * "arg" must point to the first non-white of the expression.
2813 * "arg" is advanced to the next non-white after the recognized expression.
2814 *
2815 * Return OK or FAIL.
2816 */
2817 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002818eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002820 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 int evaluate;
2822{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002823 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 int op;
2825 long n1, n2;
2826
2827 /*
2828 * Get the first variable.
2829 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 return FAIL;
2832
2833 /*
2834 * Repeat computing, until no '*', '/' or '%' is following.
2835 */
2836 for (;;)
2837 {
2838 op = **arg;
2839 if (op != '*' && op != '/' && op != '%')
2840 break;
2841
2842 if (evaluate)
2843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002844 n1 = get_tv_number(rettv);
2845 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847 else
2848 n1 = 0;
2849
2850 /*
2851 * Get the second variable.
2852 */
2853 *arg = skipwhite(*arg + 1);
2854 if (eval7(arg, &var2, evaluate) == FAIL)
2855 return FAIL;
2856
2857 if (evaluate)
2858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 n2 = get_tv_number(&var2);
2860 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 /*
2863 * Compute the result.
2864 */
2865 if (op == '*')
2866 n1 = n1 * n2;
2867 else if (op == '/')
2868 {
2869 if (n2 == 0) /* give an error message? */
2870 n1 = 0x7fffffffL;
2871 else
2872 n1 = n1 / n2;
2873 }
2874 else
2875 {
2876 if (n2 == 0) /* give an error message? */
2877 n1 = 0;
2878 else
2879 n1 = n1 % n2;
2880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002881 rettv->v_type = VAR_NUMBER;
2882 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 }
2885
2886 return OK;
2887}
2888
2889/*
2890 * Handle sixth level expression:
2891 * number number constant
2892 * "string" string contstant
2893 * 'string' literal string contstant
2894 * &option-name option value
2895 * @r register contents
2896 * identifier variable value
2897 * function() function call
2898 * $VAR environment variable
2899 * (expression) nested expression
2900 *
2901 * Also handle:
2902 * ! in front logical NOT
2903 * - in front unary minus
2904 * + in front unary plus (ignored)
2905 * trailing [] subscript in String
2906 *
2907 * "arg" must point to the first non-white of the expression.
2908 * "arg" is advanced to the next non-white after the recognized expression.
2909 *
2910 * Return OK or FAIL.
2911 */
2912 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002913eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int evaluate;
2917{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 long n;
2919 int len;
2920 char_u *s;
2921 int val;
2922 char_u *start_leader, *end_leader;
2923 int ret = OK;
2924 char_u *alias;
2925
2926 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002928 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 /*
2933 * Skip '!' and '-' characters. They are handled later.
2934 */
2935 start_leader = *arg;
2936 while (**arg == '!' || **arg == '-' || **arg == '+')
2937 *arg = skipwhite(*arg + 1);
2938 end_leader = *arg;
2939
2940 switch (**arg)
2941 {
2942 /*
2943 * Number constant.
2944 */
2945 case '0':
2946 case '1':
2947 case '2':
2948 case '3':
2949 case '4':
2950 case '5':
2951 case '6':
2952 case '7':
2953 case '8':
2954 case '9':
2955 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
2956 *arg += len;
2957 if (evaluate)
2958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 rettv->v_type = VAR_NUMBER;
2960 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
2962 break;
2963
2964 /*
2965 * String constant: "string".
2966 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 break;
2969
2970 /*
2971 * Literal string constant: 'string'.
2972 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002974 break;
2975
2976 /*
2977 * List: [expr, expr]
2978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 break;
2981
2982 /*
2983 * Option value: &name
2984 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002985 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 break;
2987
2988 /*
2989 * Environment variable: $VAR.
2990 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002991 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 break;
2993
2994 /*
2995 * Register contents: @r.
2996 */
2997 case '@': ++*arg;
2998 if (evaluate)
2999 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003000 rettv->v_type = VAR_STRING;
3001 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003 if (**arg != NUL)
3004 ++*arg;
3005 break;
3006
3007 /*
3008 * nested expression: (expression).
3009 */
3010 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003011 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (**arg == ')')
3013 ++*arg;
3014 else if (ret == OK)
3015 {
3016 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 ret = FAIL;
3019 }
3020 break;
3021
3022 /*
3023 * Must be a variable or function name then.
3024 */
3025 default: s = *arg;
3026 len = get_func_len(arg, &alias, evaluate);
3027 if (alias != NULL)
3028 s = alias;
3029
3030 if (len == 0)
3031 ret = FAIL;
3032 else
3033 {
3034 if (**arg == '(') /* recursive! */
3035 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003036 /* If "s" is the name of a variable of type VAR_FUNC
3037 * use its contents. */
3038 s = deref_func_name(s, &len);
3039
3040 /* Invoke the function. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 ret = get_func_tv(s, len, rettv, arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3043 &len, evaluate);
3044 /* Stop the expression evaluation when immediately
3045 * aborting on error, or when an interrupt occurred or
3046 * an exception was thrown but not caught. */
3047 if (aborting())
3048 {
3049 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003050 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 ret = FAIL;
3052 }
3053 }
3054 else if (evaluate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 ret = get_var_tv(s, len, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 }
3057
3058 if (alias != NULL)
3059 vim_free(alias);
3060
3061 break;
3062 }
3063 *arg = skipwhite(*arg);
3064
3065 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003066 * Handle expr[expr] and expr[expr:expr] subscript.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003068 while (**arg == '[' && ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 if (eval_index(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 return FAIL;
3074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076
3077 /*
3078 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3079 */
3080 if (ret == OK && evaluate && end_leader > start_leader)
3081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 while (end_leader > start_leader)
3084 {
3085 --end_leader;
3086 if (*end_leader == '!')
3087 val = !val;
3088 else if (*end_leader == '-')
3089 val = -val;
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 clear_tv(rettv);
3092 rettv->v_type = VAR_NUMBER;
3093 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 }
3095
3096 return ret;
3097}
3098
3099/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003100 * Evaluate an "[expr]" or "[expr:expr]" index.
3101 * "*arg" points to the '['.
3102 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3103 */
3104 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003106 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003108 int evaluate;
3109{
3110 int empty1 = FALSE, empty2 = FALSE;
3111 typeval var1, var2;
3112 long n1, n2 = 0;
3113 long len;
3114 int range;
3115 char_u *s;
3116
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003117 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003118 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003119 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003120 return FAIL;
3121 }
3122
3123 /*
3124 * Get the (first) variable from inside the [].
3125 */
3126 *arg = skipwhite(*arg + 1);
3127 if (**arg == ':')
3128 empty1 = TRUE;
3129 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3130 return FAIL;
3131
3132 /*
3133 * Get the second variable from inside the [:].
3134 */
3135 if (**arg == ':')
3136 {
3137 range = TRUE;
3138 *arg = skipwhite(*arg + 1);
3139 if (**arg == ']')
3140 empty2 = TRUE;
3141 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003143 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003144 return FAIL;
3145 }
3146 }
3147 else
3148 range = FALSE;
3149
3150 /* Check for the ']'. */
3151 if (**arg != ']')
3152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003153 EMSG(_(e_missbrac));
3154 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003155 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003156 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003157 return FAIL;
3158 }
3159
3160 if (evaluate)
3161 {
3162 if (empty1)
3163 n1 = 0;
3164 else
3165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003166 n1 = get_tv_number(&var1);
3167 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003168 }
3169 if (range)
3170 {
3171 if (empty2)
3172 n2 = -1;
3173 else
3174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003175 n2 = get_tv_number(&var2);
3176 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003177 }
3178 }
3179
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003181 {
3182 case VAR_NUMBER:
3183 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003184 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003185 len = (long)STRLEN(s);
3186 if (range)
3187 {
3188 /* The resulting variable is a substring. If the indexes
3189 * are out of range the result is empty. */
3190 if (n1 < 0)
3191 {
3192 n1 = len + n1;
3193 if (n1 < 0)
3194 n1 = 0;
3195 }
3196 if (n2 < 0)
3197 n2 = len + n2;
3198 else if (n2 >= len)
3199 n2 = len;
3200 if (n1 >= len || n2 < 0 || n1 > n2)
3201 s = NULL;
3202 else
3203 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3204 }
3205 else
3206 {
3207 /* The resulting variable is a string of a single
3208 * character. If the index is too big or negative the
3209 * result is empty. */
3210 if (n1 >= len || n1 < 0)
3211 s = NULL;
3212 else
3213 s = vim_strnsave(s + n1, 1);
3214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003215 clear_tv(rettv);
3216 rettv->v_type = VAR_STRING;
3217 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003218 break;
3219
3220 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003221 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003222 if (n1 < 0)
3223 n1 = len + n1;
3224 if (!empty1 && (n1 < 0 || n1 >= len))
3225 {
3226 EMSGN(_(e_listidx), n1);
3227 return FAIL;
3228 }
3229 if (range)
3230 {
3231 listvar *l;
3232 listitem *item;
3233
3234 if (n2 < 0)
3235 n2 = len + n2;
3236 if (!empty2 && (n2 < 0 || n2 >= len || n2 < n1))
3237 {
3238 EMSGN(_(e_listidx), n2);
3239 return FAIL;
3240 }
3241 l = list_alloc();
3242 if (l == NULL)
3243 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003244 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003245 n1 <= n2; ++n1)
3246 {
3247 if (list_append_tv(l, &item->li_tv) == FAIL)
3248 {
3249 list_free(l);
3250 return FAIL;
3251 }
3252 item = item->li_next;
3253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003254 clear_tv(rettv);
3255 rettv->v_type = VAR_LIST;
3256 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003257 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003258 }
3259 else
3260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003261 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003262 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003263 clear_tv(rettv);
3264 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003265 }
3266 break;
3267 }
3268 }
3269
3270 *arg = skipwhite(*arg + 1); /* skip the ']' */
3271 return OK;
3272}
3273
3274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 * Get an option value.
3276 * "arg" points to the '&' or '+' before the option name.
3277 * "arg" is advanced to character after the option name.
3278 * Return OK or FAIL.
3279 */
3280 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003281get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 int evaluate;
3285{
3286 char_u *option_end;
3287 long numval;
3288 char_u *stringval;
3289 int opt_type;
3290 int c;
3291 int working = (**arg == '+'); /* has("+option") */
3292 int ret = OK;
3293 int opt_flags;
3294
3295 /*
3296 * Isolate the option name and find its value.
3297 */
3298 option_end = find_option_end(arg, &opt_flags);
3299 if (option_end == NULL)
3300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003301 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 EMSG2(_("E112: Option name missing: %s"), *arg);
3303 return FAIL;
3304 }
3305
3306 if (!evaluate)
3307 {
3308 *arg = option_end;
3309 return OK;
3310 }
3311
3312 c = *option_end;
3313 *option_end = NUL;
3314 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003315 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
3317 if (opt_type == -3) /* invalid name */
3318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003319 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 EMSG2(_("E113: Unknown option: %s"), *arg);
3321 ret = FAIL;
3322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003323 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 if (opt_type == -2) /* hidden string option */
3326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 rettv->v_type = VAR_STRING;
3328 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330 else if (opt_type == -1) /* hidden number option */
3331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 rettv->v_type = VAR_NUMBER;
3333 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335 else if (opt_type == 1) /* number option */
3336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003337 rettv->v_type = VAR_NUMBER;
3338 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340 else /* string option */
3341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 rettv->v_type = VAR_STRING;
3343 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345 }
3346 else if (working && (opt_type == -2 || opt_type == -1))
3347 ret = FAIL;
3348
3349 *option_end = c; /* put back for error messages */
3350 *arg = option_end;
3351
3352 return ret;
3353}
3354
3355/*
3356 * Allocate a variable for a string constant.
3357 * Return OK or FAIL.
3358 */
3359 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 int evaluate;
3364{
3365 char_u *p;
3366 char_u *name;
3367 int i;
3368 int extra = 0;
3369
3370 /*
3371 * Find the end of the string, skipping backslashed characters.
3372 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003373 for (p = *arg + 1; *p && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
3375 if (*p == '\\' && p[1] != NUL)
3376 {
3377 ++p;
3378 /* A "\<x>" form occupies at least 4 characters, and produces up
3379 * to 6 characters: reserve space for 2 extra */
3380 if (*p == '<')
3381 extra += 2;
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384
3385 if (*p != '"')
3386 {
3387 EMSG2(_("E114: Missing quote: %s"), *arg);
3388 return FAIL;
3389 }
3390
3391 /* If only parsing, set *arg and return here */
3392 if (!evaluate)
3393 {
3394 *arg = p + 1;
3395 return OK;
3396 }
3397
3398 /*
3399 * Copy the string into allocated memory, handling backslashed
3400 * characters.
3401 */
3402 name = alloc((unsigned)(p - *arg + extra));
3403 if (name == NULL)
3404 return FAIL;
3405
3406 i = 0;
3407 for (p = *arg + 1; *p && *p != '"'; ++p)
3408 {
3409 if (*p == '\\')
3410 {
3411 switch (*++p)
3412 {
3413 case 'b': name[i++] = BS; break;
3414 case 'e': name[i++] = ESC; break;
3415 case 'f': name[i++] = FF; break;
3416 case 'n': name[i++] = NL; break;
3417 case 'r': name[i++] = CAR; break;
3418 case 't': name[i++] = TAB; break;
3419
3420 case 'X': /* hex: "\x1", "\x12" */
3421 case 'x':
3422 case 'u': /* Unicode: "\u0023" */
3423 case 'U':
3424 if (vim_isxdigit(p[1]))
3425 {
3426 int n, nr;
3427 int c = toupper(*p);
3428
3429 if (c == 'X')
3430 n = 2;
3431 else
3432 n = 4;
3433 nr = 0;
3434 while (--n >= 0 && vim_isxdigit(p[1]))
3435 {
3436 ++p;
3437 nr = (nr << 4) + hex2nr(*p);
3438 }
3439#ifdef FEAT_MBYTE
3440 /* For "\u" store the number according to
3441 * 'encoding'. */
3442 if (c != 'X')
3443 i += (*mb_char2bytes)(nr, name + i);
3444 else
3445#endif
3446 name[i++] = nr;
3447 }
3448 else
3449 name[i++] = *p;
3450 break;
3451
3452 /* octal: "\1", "\12", "\123" */
3453 case '0':
3454 case '1':
3455 case '2':
3456 case '3':
3457 case '4':
3458 case '5':
3459 case '6':
3460 case '7': name[i] = *p - '0';
3461 if (p[1] >= '0' && p[1] <= '7')
3462 {
3463 ++p;
3464 name[i] = (name[i] << 3) + *p - '0';
3465 if (p[1] >= '0' && p[1] <= '7')
3466 {
3467 ++p;
3468 name[i] = (name[i] << 3) + *p - '0';
3469 }
3470 }
3471 ++i;
3472 break;
3473
3474 /* Special key, e.g.: "\<C-W>" */
3475 case '<': extra = trans_special(&p, name + i, TRUE);
3476 if (extra != 0)
3477 {
3478 i += extra;
3479 --p;
3480 break;
3481 }
3482 /* FALLTHROUGH */
3483
3484 default: name[i++] = *p;
3485 break;
3486 }
3487 }
3488 else
3489 name[i++] = *p;
3490
3491#ifdef FEAT_MBYTE
3492 /* For a multi-byte character copy the bytes after the first one. */
3493 if (has_mbyte)
3494 {
3495 int l = (*mb_ptr2len_check)(p);
3496
3497 while (--l > 0)
3498 name[i++] = *++p;
3499 }
3500#endif
3501 }
3502 name[i] = NUL;
3503 *arg = p + 1;
3504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003505 rettv->v_type = VAR_STRING;
3506 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 return OK;
3509}
3510
3511/*
3512 * Allocate a variable for an backtick-string constant.
3513 * Return OK or FAIL.
3514 */
3515 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003516get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 int evaluate;
3520{
3521 char_u *p;
3522 char_u *name;
3523
3524 /*
3525 * Find the end of the string.
3526 */
3527 p = vim_strchr(*arg + 1, '\'');
3528 if (p == NULL)
3529 {
3530 EMSG2(_("E115: Missing quote: %s"), *arg);
3531 return FAIL;
3532 }
3533
3534 if (evaluate)
3535 {
3536 /*
3537 * Copy the string into allocated memory.
3538 */
3539 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
3540 if (name == NULL)
3541 return FAIL;
3542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 rettv->v_type = VAR_STRING;
3544 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
3547 *arg = p + 1;
3548
3549 return OK;
3550}
3551
3552/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003553 * Allocate a variable for a List and fill it from "*arg".
3554 * Return OK or FAIL.
3555 */
3556 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 int evaluate;
3561{
3562 listvar *l = NULL;
3563 typeval tv;
3564 listitem *item;
3565
3566 if (evaluate)
3567 {
3568 l = list_alloc();
3569 if (l == NULL)
3570 return FAIL;
3571 }
3572
3573 *arg = skipwhite(*arg + 1);
3574 while (**arg != ']' && **arg != NUL)
3575 {
3576 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
3577 goto failret;
3578 if (evaluate)
3579 {
3580 item = listitem_alloc();
3581 if (item != NULL)
3582 {
3583 item->li_tv = tv;
3584 list_append(l, item);
3585 }
3586 }
3587
3588 if (**arg == ']')
3589 break;
3590 if (**arg != ',')
3591 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003592 EMSG2(_("E696: Missing comma in list: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003593 goto failret;
3594 }
3595 *arg = skipwhite(*arg + 1);
3596 }
3597
3598 if (**arg != ']')
3599 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003600 EMSG2(_("E697: Missing end of list ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601failret:
3602 if (evaluate)
3603 list_free(l);
3604 return FAIL;
3605 }
3606
3607 *arg = skipwhite(*arg + 1);
3608 if (evaluate)
3609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 rettv->v_type = VAR_LIST;
3611 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003612 ++l->lv_refcount;
3613 }
3614
3615 return OK;
3616}
3617
3618/*
3619 * Allocate an empty header for a list.
3620 */
3621 static listvar *
3622list_alloc()
3623{
3624 return (listvar *)alloc_clear(sizeof(listvar));
3625}
3626
3627/*
3628 * Unreference a list: decrement the reference count and free it when it
3629 * becomes zero.
3630 */
3631 static void
3632list_unref(l)
3633 listvar *l;
3634{
3635 if (l != NULL && --l->lv_refcount <= 0)
3636 list_free(l);
3637}
3638
3639/*
3640 * Free a list, including all items it points to.
3641 * Ignores the reference count.
3642 */
3643 static void
3644list_free(l)
3645 listvar *l;
3646{
3647 listitem *item;
3648 listitem *next;
3649
3650 for (item = l->lv_first; item != NULL; item = next)
3651 {
3652 next = item->li_next;
3653 listitem_free(item);
3654 }
3655 vim_free(l);
3656}
3657
3658/*
3659 * Allocate a list item.
3660 */
3661 static listitem *
3662listitem_alloc()
3663{
3664 return (listitem *)alloc(sizeof(listitem));
3665}
3666
3667/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003668 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003669 */
3670 static void
3671listitem_free(item)
3672 listitem *item;
3673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003675 vim_free(item);
3676}
3677
3678/*
3679 * Get the number of items in a list.
3680 */
3681 static long
3682list_len(l)
3683 listvar *l;
3684{
3685 listitem *item;
3686 long len = 0;
3687
3688 if (l == NULL)
3689 return 0L;
3690 for (item = l->lv_first; item != NULL; item = item->li_next)
3691 ++len;
3692 return len;
3693}
3694
3695/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003696 * Return TRUE when two lists have exactly the same values.
3697 */
3698 static int
3699list_equal(l1, l2, ic)
3700 listvar *l1;
3701 listvar *l2;
3702 int ic; /* ignore case for strings */
3703{
3704 listitem *item1, *item2;
3705
3706 for (item1 = l1->lv_first, item2 = l2->lv_first;
3707 item1 != NULL && item2 != NULL;
3708 item1 = item1->li_next, item2 = item2->li_next)
3709 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
3710 return FALSE;
3711 return item1 == NULL && item2 == NULL;
3712}
3713
3714/*
3715 * Return TRUE if "tv1" and "tv2" have the same value.
3716 * Compares the items just like "==" would compare them.
3717 */
3718 static int
3719tv_equal(tv1, tv2, ic)
3720 typeval *tv1;
3721 typeval *tv2;
3722 int ic; /* ignore case */
3723{
3724 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3725
3726 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
3727 {
3728 /* recursive! */
3729 if (tv1->v_type != tv2->v_type
3730 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
3731 return FALSE;
3732 }
3733 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
3734 {
3735 if (tv1->v_type != tv2->v_type
3736 || tv1->vval.v_string == NULL
3737 || tv2->vval.v_string == NULL
3738 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
3739 return FALSE;
3740 }
3741 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
3742 {
3743 if (get_tv_number(tv1) != get_tv_number(tv2))
3744 return FALSE;
3745 }
3746 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
3747 get_tv_string_buf(tv2, buf2)) != 0)
3748 return FALSE;
3749 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
3750 get_tv_string_buf(tv2, buf2)) != 0)
3751 return FALSE;
3752 return TRUE;
3753}
3754
3755/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003756 * Locate item with index "n" in list "l" and return it.
3757 * A negative index is counted from the end; -1 is the last item.
3758 * Returns NULL when "n" is out of range.
3759 */
3760 static listitem *
3761list_find(l, n)
3762 listvar *l;
3763 long n;
3764{
3765 listitem *item;
3766 long idx;
3767
3768 if (l == NULL)
3769 return NULL;
3770 if (n < 0)
3771 {
3772 idx = -1; /* search from the end */
3773 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
3774 --idx;
3775 }
3776 else
3777 {
3778 idx = 0; /* search from the start */
3779 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
3780 ++idx;
3781 }
3782 if (idx != n)
3783 return NULL;
3784 return item;
3785}
3786
3787/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003788 * Like list_find(), but also find an item just past the end.
3789 * "*ip" is the item to find.
3790 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
3791 * Returns NULL when item not found or item is just past the end.
3792 */
3793 static listitem *
3794list_find_ext(l, ip)
3795 listvar *l;
3796 long *ip;
3797{
3798 long n;
3799 listitem *item;
3800
3801 if (*ip < 0)
3802 {
3803 /* Count from the end: -1 is before last item. */
3804 item = l->lv_last;
3805 for (n = *ip + 1; n < 0 && item != NULL; ++n)
3806 item = item->li_prev;
3807 if (item == NULL)
3808 n = 1; /* error! */
3809 }
3810 else
3811 {
3812 item = l->lv_first;
3813 for (n = *ip; n > 0 && item != NULL; --n)
3814 item = item->li_next;
3815 }
3816 *ip = n;
3817 return item;
3818}
3819
3820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003821 * Append item "item" to the end of list "l".
3822 */
3823 static void
3824list_append(l, item)
3825 listvar *l;
3826 listitem *item;
3827{
3828 if (l->lv_last == NULL)
3829 {
3830 /* empty list */
3831 l->lv_first = item;
3832 l->lv_last = item;
3833 item->li_prev = NULL;
3834 }
3835 else
3836 {
3837 l->lv_last->li_next = item;
3838 item->li_prev = l->lv_last;
3839 l->lv_last = item;
3840 }
3841 item->li_next = NULL;
3842}
3843
3844/*
3845 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003846 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847 */
3848 static int
3849list_append_tv(l, tv)
3850 listvar *l;
3851 typeval *tv;
3852{
3853 listitem *ni = listitem_alloc();
3854
3855 if (ni == NULL)
3856 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003858 list_append(l, ni);
3859 return OK;
3860}
3861
3862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003863 * Insert typeval "tv" in list "l" before "item".
3864 * If "item" is NULL append at the end.
3865 * Return FAIL when out of memory.
3866 */
3867 static int
3868list_insert_tv(l, tv, item)
3869 listvar *l;
3870 typeval *tv;
3871 listitem *item;
3872{
3873 listitem *ni = listitem_alloc();
3874
3875 if (ni == NULL)
3876 return FAIL;
3877 copy_tv(tv, &ni->li_tv);
3878 if (item == NULL)
3879 /* Append new item at end of list. */
3880 list_append(l, ni);
3881 else
3882 {
3883 /* Insert new item before existing item. */
3884 ni->li_prev = item->li_prev;
3885 ni->li_next = item;
3886 if (item->li_prev == NULL)
3887 l->lv_first = ni;
3888 else
3889 item->li_prev->li_next = ni;
3890 item->li_prev = ni;
3891 }
3892 return OK;
3893}
3894
3895/*
3896 * Extend "l1" with "l2".
3897 * If "bef" is NULL append at the end, otherwise insert before this item.
3898 * Returns FAIL when out of memory.
3899 */
3900 static int
3901list_extend(l1, l2, bef)
3902 listvar *l1;
3903 listvar *l2;
3904 listitem *bef;
3905{
3906 listitem *item;
3907
3908 for (item = l2->lv_first; item != NULL; item = item->li_next)
3909 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
3910 return FAIL;
3911 return OK;
3912}
3913
3914/*
3915 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
3916 * Return FAIL when out of memory.
3917 */
3918 static int
3919list_concat(l1, l2, tv)
3920 listvar *l1;
3921 listvar *l2;
3922 typeval *tv;
3923{
3924 listvar *l;
3925
3926 /* make a copy of the first list. */
3927 l = list_copy(l1, FALSE);
3928 if (l == NULL)
3929 return FAIL;
3930 tv->v_type = VAR_LIST;
3931 tv->vval.v_list = l;
3932
3933 /* append all items from the second list */
3934 return list_extend(l, l2, NULL);
3935}
3936
3937/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003938 * Make a copy of list "l". Shallow if "deep" is FALSE.
3939 * The refcount of the new list is set to 1.
3940 * Returns NULL when out of memory.
3941 */
3942 static listvar *
3943list_copy(orig, deep)
3944 listvar *orig;
3945 int deep;
3946{
3947 listvar *copy;
3948 listitem *item;
3949 listitem *ni;
3950 static int recurse = 0;
3951
3952 if (orig == NULL)
3953 return NULL;
3954 if (recurse >= VAR_LIST_MAXNEST)
3955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003956 EMSG(_("E698: List nested too deep for making a copy"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003957 return NULL;
3958 }
3959 ++recurse;
3960
3961 copy = list_alloc();
3962 if (copy != NULL)
3963 {
3964 for (item = orig->lv_first; item != NULL; item = item->li_next)
3965 {
3966 ni = listitem_alloc();
3967 if (ni == NULL)
3968 break;
3969 if (deep && item->li_tv.v_type == VAR_LIST)
3970 {
3971 ni->li_tv.v_type = VAR_LIST;
3972 ni->li_tv.vval.v_list = list_copy(item->li_tv.vval.v_list,
3973 TRUE);
3974 if (ni->li_tv.vval.v_list == NULL)
3975 {
3976 vim_free(ni);
3977 break;
3978 }
3979 }
3980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982 list_append(copy, ni);
3983 }
3984 ++copy->lv_refcount;
3985 }
3986
3987 --recurse;
3988 return copy;
3989}
3990
3991/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003992 * Remove items "item" to "item2" from list "l".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003993 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003994 static void
3995list_getrem(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003996 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003997 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003998 listitem *item2;
3999{
4000 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 /* notify watchers */
4003 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004004 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004005 list_fix_watch(l, ip);
4006 if (ip == item2)
4007 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004009
4010 if (item2->li_next == NULL)
4011 l->lv_last = item->li_prev;
4012 else
4013 item2->li_next->li_prev = item->li_prev;
4014 if (item->li_prev == NULL)
4015 l->lv_first = item2->li_next;
4016 else
4017 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004018}
4019
4020/*
4021 * Return an allocated string with the string representation of a list.
4022 * May return NULL.
4023 */
4024 static char_u *
4025list2string(tv)
4026 typeval *tv;
4027{
4028 garray_T ga;
4029 listitem *item;
4030 int first = TRUE;
4031 char_u *tofree;
4032 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004033 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004034
4035 if (tv->vval.v_list == NULL)
4036 return NULL;
4037 ga_init2(&ga, (int)sizeof(char), 80);
4038 ga_append(&ga, '[');
4039
4040 for (item = tv->vval.v_list->lv_first; item != NULL; item = item->li_next)
4041 {
4042 if (first)
4043 first = FALSE;
4044 else
4045 ga_concat(&ga, (char_u *)", ");
4046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004047 s = tv2string(&item->li_tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004048 if (s != NULL)
4049 ga_concat(&ga, s);
4050 vim_free(tofree);
4051 }
4052
4053 ga_append(&ga, ']');
4054 ga_append(&ga, NUL);
4055 return (char_u *)ga.ga_data;
4056}
4057
4058/*
4059 * Return a string with the string representation of a variable.
4060 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 * "numbuf" is used for a number.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 * May return NULL;
4063 */
4064 static char_u *
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004065tv2string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004066 typeval *tv;
4067 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004068 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004069{
4070 switch (tv->v_type)
4071 {
4072 case VAR_FUNC:
4073 *tofree = NULL;
4074 return tv->vval.v_string;
4075 case VAR_LIST:
4076 *tofree = list2string(tv);
4077 return *tofree;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 case VAR_STRING:
4079 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004080 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 default:
4082 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004083 }
4084 *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004085 return get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004086}
4087
4088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 * Get the value of an environment variable.
4090 * "arg" is pointing to the '$'. It is advanced to after the name.
4091 * If the environment variable was not set, silently assume it is empty.
4092 * Always return OK.
4093 */
4094 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 int evaluate;
4099{
4100 char_u *string = NULL;
4101 int len;
4102 int cc;
4103 char_u *name;
4104
4105 ++*arg;
4106 name = *arg;
4107 len = get_env_len(arg);
4108 if (evaluate)
4109 {
4110 if (len != 0)
4111 {
4112 cc = name[len];
4113 name[len] = NUL;
4114 /* first try mch_getenv(), fast for normal environment vars */
4115 string = mch_getenv(name);
4116 if (string != NULL && *string != NUL)
4117 string = vim_strsave(string);
4118 else
4119 {
4120 /* next try expanding things like $VIM and ${HOME} */
4121 string = expand_env_save(name - 1);
4122 if (string != NULL && *string == '$')
4123 {
4124 vim_free(string);
4125 string = NULL;
4126 }
4127 }
4128 name[len] = cc;
4129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 rettv->v_type = VAR_STRING;
4131 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133
4134 return OK;
4135}
4136
4137/*
4138 * Array with names and number of arguments of all internal functions
4139 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
4140 */
4141static struct fst
4142{
4143 char *f_name; /* function name */
4144 char f_min_argc; /* minimal number of arguments */
4145 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004146 void (*f_func) __ARGS((typeval *args, typeval *rvar));
4147 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148} functions[] =
4149{
Bram Moolenaar0d660222005-01-07 21:51:51 +00004150 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {"append", 2, 2, f_append},
4152 {"argc", 0, 0, f_argc},
4153 {"argidx", 0, 0, f_argidx},
4154 {"argv", 1, 1, f_argv},
4155 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004156 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {"bufexists", 1, 1, f_bufexists},
4158 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
4159 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
4160 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
4161 {"buflisted", 1, 1, f_buflisted},
4162 {"bufloaded", 1, 1, f_bufloaded},
4163 {"bufname", 1, 1, f_bufname},
4164 {"bufnr", 1, 1, f_bufnr},
4165 {"bufwinnr", 1, 1, f_bufwinnr},
4166 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004167 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 {"call", 2, 2, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {"char2nr", 1, 1, f_char2nr},
4170 {"cindent", 1, 1, f_cindent},
4171 {"col", 1, 1, f_col},
4172 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004173 {"copy", 1, 1, f_copy},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004174 {"count", 2, 3, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {"cscope_connection",0,3, f_cscope_connection},
4176 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004177 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {"delete", 1, 1, f_delete},
4179 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00004180 {"diff_filler", 1, 1, f_diff_filler},
4181 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004182 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {"escape", 2, 2, f_escape},
4184 {"eventhandler", 0, 0, f_eventhandler},
4185 {"executable", 1, 1, f_executable},
4186 {"exists", 1, 1, f_exists},
4187 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004188 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
4190 {"filereadable", 1, 1, f_filereadable},
4191 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004192 {"finddir", 1, 3, f_finddir},
4193 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {"fnamemodify", 2, 2, f_fnamemodify},
4195 {"foldclosed", 1, 1, f_foldclosed},
4196 {"foldclosedend", 1, 1, f_foldclosedend},
4197 {"foldlevel", 1, 1, f_foldlevel},
4198 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004199 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004201 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004202 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {"getbufvar", 2, 2, f_getbufvar},
4204 {"getchar", 0, 1, f_getchar},
4205 {"getcharmod", 0, 0, f_getcharmod},
4206 {"getcmdline", 0, 0, f_getcmdline},
4207 {"getcmdpos", 0, 0, f_getcmdpos},
4208 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004209 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004210 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {"getfsize", 1, 1, f_getfsize},
4212 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004213 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004214 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {"getreg", 0, 1, f_getreg},
4216 {"getregtype", 0, 1, f_getregtype},
4217 {"getwinposx", 0, 0, f_getwinposx},
4218 {"getwinposy", 0, 0, f_getwinposy},
4219 {"getwinvar", 2, 2, f_getwinvar},
4220 {"glob", 1, 1, f_glob},
4221 {"globpath", 2, 2, f_globpath},
4222 {"has", 1, 1, f_has},
4223 {"hasmapto", 1, 2, f_hasmapto},
4224 {"highlightID", 1, 1, f_hlID}, /* obsolete */
4225 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
4226 {"histadd", 2, 2, f_histadd},
4227 {"histdel", 1, 2, f_histdel},
4228 {"histget", 1, 2, f_histget},
4229 {"histnr", 1, 1, f_histnr},
4230 {"hlID", 1, 1, f_hlID},
4231 {"hlexists", 1, 1, f_hlexists},
4232 {"hostname", 0, 0, f_hostname},
4233 {"iconv", 3, 3, f_iconv},
4234 {"indent", 1, 1, f_indent},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004235 {"index", 2, 3, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {"input", 1, 2, f_input},
4237 {"inputdialog", 1, 3, f_inputdialog},
4238 {"inputrestore", 0, 0, f_inputrestore},
4239 {"inputsave", 0, 0, f_inputsave},
4240 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {"isdirectory", 1, 1, f_isdirectory},
4243 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {"libcall", 3, 3, f_libcall},
4246 {"libcallnr", 3, 3, f_libcallnr},
4247 {"line", 1, 1, f_line},
4248 {"line2byte", 1, 1, f_line2byte},
4249 {"lispindent", 1, 1, f_lispindent},
4250 {"localtime", 0, 0, f_localtime},
4251 {"maparg", 1, 2, f_maparg},
4252 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004253 {"match", 2, 4, f_match},
4254 {"matchend", 2, 4, f_matchend},
4255 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {"mode", 0, 0, f_mode},
4257 {"nextnonblank", 1, 1, f_nextnonblank},
4258 {"nr2char", 1, 1, f_nr2char},
4259 {"prevnonblank", 1, 1, f_prevnonblank},
4260 {"remote_expr", 2, 3, f_remote_expr},
4261 {"remote_foreground", 1, 1, f_remote_foreground},
4262 {"remote_peek", 1, 2, f_remote_peek},
4263 {"remote_read", 1, 1, f_remote_read},
4264 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004265 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004267 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004269 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {"search", 1, 2, f_search},
4271 {"searchpair", 3, 5, f_searchpair},
4272 {"server2client", 2, 2, f_server2client},
4273 {"serverlist", 0, 0, f_serverlist},
4274 {"setbufvar", 3, 3, f_setbufvar},
4275 {"setcmdpos", 1, 1, f_setcmdpos},
4276 {"setline", 2, 2, f_setline},
4277 {"setreg", 2, 3, f_setreg},
4278 {"setwinvar", 3, 3, f_setwinvar},
4279 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004280 {"sort", 1, 2, f_sort},
4281 {"str2list", 1, 2, f_str2list},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#ifdef HAVE_STRFTIME
4283 {"strftime", 1, 2, f_strftime},
4284#endif
4285 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004286 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {"strlen", 1, 1, f_strlen},
4288 {"strpart", 2, 3, f_strpart},
4289 {"strridx", 2, 2, f_strridx},
4290 {"strtrans", 1, 1, f_strtrans},
4291 {"submatch", 1, 1, f_submatch},
4292 {"substitute", 4, 4, f_substitute},
4293 {"synID", 3, 3, f_synID},
4294 {"synIDattr", 2, 3, f_synIDattr},
4295 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004296 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {"tempname", 0, 0, f_tempname},
4298 {"tolower", 1, 1, f_tolower},
4299 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00004300 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {"type", 1, 1, f_type},
4302 {"virtcol", 1, 1, f_virtcol},
4303 {"visualmode", 0, 1, f_visualmode},
4304 {"winbufnr", 1, 1, f_winbufnr},
4305 {"wincol", 0, 0, f_wincol},
4306 {"winheight", 1, 1, f_winheight},
4307 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004308 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {"winrestcmd", 0, 0, f_winrestcmd},
4310 {"winwidth", 1, 1, f_winwidth},
4311};
4312
4313#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4314
4315/*
4316 * Function given to ExpandGeneric() to obtain the list of internal
4317 * or user defined function names.
4318 */
4319 char_u *
4320get_function_name(xp, idx)
4321 expand_T *xp;
4322 int idx;
4323{
4324 static int intidx = -1;
4325 char_u *name;
4326
4327 if (idx == 0)
4328 intidx = -1;
4329 if (intidx < 0)
4330 {
4331 name = get_user_func_name(xp, idx);
4332 if (name != NULL)
4333 return name;
4334 }
4335 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
4336 {
4337 STRCPY(IObuff, functions[intidx].f_name);
4338 STRCAT(IObuff, "(");
4339 if (functions[intidx].f_max_argc == 0)
4340 STRCAT(IObuff, ")");
4341 return IObuff;
4342 }
4343
4344 return NULL;
4345}
4346
4347/*
4348 * Function given to ExpandGeneric() to obtain the list of internal or
4349 * user defined variable or function names.
4350 */
4351/*ARGSUSED*/
4352 char_u *
4353get_expr_name(xp, idx)
4354 expand_T *xp;
4355 int idx;
4356{
4357 static int intidx = -1;
4358 char_u *name;
4359
4360 if (idx == 0)
4361 intidx = -1;
4362 if (intidx < 0)
4363 {
4364 name = get_function_name(xp, idx);
4365 if (name != NULL)
4366 return name;
4367 }
4368 return get_user_var_name(xp, ++intidx);
4369}
4370
4371#endif /* FEAT_CMDL_COMPL */
4372
4373/*
4374 * Find internal function in table above.
4375 * Return index, or -1 if not found
4376 */
4377 static int
4378find_internal_func(name)
4379 char_u *name; /* name of the function */
4380{
4381 int first = 0;
4382 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
4383 int cmp;
4384 int x;
4385
4386 /*
4387 * Find the function name in the table. Binary search.
4388 */
4389 while (first <= last)
4390 {
4391 x = first + ((unsigned)(last - first) >> 1);
4392 cmp = STRCMP(name, functions[x].f_name);
4393 if (cmp < 0)
4394 last = x - 1;
4395 else if (cmp > 0)
4396 first = x + 1;
4397 else
4398 return x;
4399 }
4400 return -1;
4401}
4402
4403/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
4405 * name it contains, otherwise return "name".
4406 */
4407 static char_u *
4408deref_func_name(name, lenp)
4409 char_u *name;
4410 int *lenp;
4411{
4412 VAR v;
4413 int cc;
4414
4415 cc = name[*lenp];
4416 name[*lenp] = NUL;
4417 v = find_var(name, FALSE);
4418 name[*lenp] = cc;
4419 if (v != NULL && v->tv.v_type == VAR_FUNC)
4420 {
4421 if (v->tv.vval.v_string == NULL)
4422 {
4423 *lenp = 0;
4424 return (char_u *)""; /* just in case */
4425 }
4426 *lenp = STRLEN(v->tv.vval.v_string);
4427 return v->tv.vval.v_string;
4428 }
4429
4430 return name;
4431}
4432
4433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 * Allocate a variable for the result of a function.
4435 * Return OK or FAIL.
4436 */
4437 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 char_u *name; /* name of the function */
4440 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 char_u **arg; /* argument, pointing to the '(' */
4443 linenr_T firstline; /* first line of range */
4444 linenr_T lastline; /* last line of range */
4445 int *doesrange; /* return: function handled range */
4446 int evaluate;
4447{
4448 char_u *argp;
4449 int ret = OK;
4450#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 int argcount = 0; /* number of arguments found */
4453
4454 /*
4455 * Get the arguments.
4456 */
4457 argp = *arg;
4458 while (argcount < MAX_FUNC_ARGS)
4459 {
4460 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
4461 if (*argp == ')' || *argp == ',' || *argp == NUL)
4462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
4464 {
4465 ret = FAIL;
4466 break;
4467 }
4468 ++argcount;
4469 if (*argp != ',')
4470 break;
4471 }
4472 if (*argp == ')')
4473 ++argp;
4474 else
4475 ret = FAIL;
4476
4477 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 firstline, lastline, doesrange, evaluate);
4480 else if (!aborting())
4481 EMSG2(_("E116: Invalid arguments for function %s"), name);
4482
4483 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 *arg = skipwhite(argp);
4487 return ret;
4488}
4489
4490
4491/*
4492 * Call a function with its resolved parameters
4493 * Return OK or FAIL.
4494 */
4495 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 doesrange, evaluate)
4498 char_u *name; /* name of the function */
4499 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 linenr_T firstline; /* first line of range */
4504 linenr_T lastline; /* last line of range */
4505 int *doesrange; /* return: function handled range */
4506 int evaluate;
4507{
4508 int ret = FAIL;
4509 static char *errors[] =
4510 {N_("E117: Unknown function: %s"),
4511 N_("E118: Too many arguments for function: %s"),
4512 N_("E119: Not enough arguments for function: %s"),
4513 N_("E120: Using <SID> not in a script context: %s"),
4514 };
4515#define ERROR_UNKNOWN 0
4516#define ERROR_TOOMANY 1
4517#define ERROR_TOOFEW 2
4518#define ERROR_SCRIPT 3
4519#define ERROR_NONE 4
4520#define ERROR_OTHER 5
4521 int error = ERROR_NONE;
4522 int i;
4523 int llen;
4524 ufunc_T *fp;
4525 int cc;
4526#define FLEN_FIXED 40
4527 char_u fname_buf[FLEN_FIXED + 1];
4528 char_u *fname;
4529
4530 /*
4531 * In a script change <SID>name() and s:name() to K_SNR 123_name().
4532 * Change <SNR>123_name() to K_SNR 123_name().
4533 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
4534 */
4535 cc = name[len];
4536 name[len] = NUL;
4537 llen = eval_fname_script(name);
4538 if (llen > 0)
4539 {
4540 fname_buf[0] = K_SPECIAL;
4541 fname_buf[1] = KS_EXTRA;
4542 fname_buf[2] = (int)KE_SNR;
4543 i = 3;
4544 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
4545 {
4546 if (current_SID <= 0)
4547 error = ERROR_SCRIPT;
4548 else
4549 {
4550 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
4551 i = (int)STRLEN(fname_buf);
4552 }
4553 }
4554 if (i + STRLEN(name + llen) < FLEN_FIXED)
4555 {
4556 STRCPY(fname_buf + i, name + llen);
4557 fname = fname_buf;
4558 }
4559 else
4560 {
4561 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
4562 if (fname == NULL)
4563 error = ERROR_OTHER;
4564 else
4565 {
4566 mch_memmove(fname, fname_buf, (size_t)i);
4567 STRCPY(fname + i, name + llen);
4568 }
4569 }
4570 }
4571 else
4572 fname = name;
4573
4574 *doesrange = FALSE;
4575
4576
4577 /* execute the function if no errors detected and executing */
4578 if (evaluate && error == ERROR_NONE)
4579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 error = ERROR_UNKNOWN;
4582
4583 if (!ASCII_ISLOWER(fname[0]))
4584 {
4585 /*
4586 * User defined function.
4587 */
4588 fp = find_func(fname);
4589#ifdef FEAT_AUTOCMD
4590 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
4591 fname, fname, TRUE, NULL)
4592#ifdef FEAT_EVAL
4593 && !aborting()
4594#endif
4595 )
4596 {
4597 /* executed an autocommand, search for function again */
4598 fp = find_func(fname);
4599 }
4600#endif
4601 if (fp != NULL)
4602 {
4603 if (fp->flags & FC_RANGE)
4604 *doesrange = TRUE;
4605 if (argcount < fp->args.ga_len)
4606 error = ERROR_TOOFEW;
4607 else if (!fp->varargs && argcount > fp->args.ga_len)
4608 error = ERROR_TOOMANY;
4609 else
4610 {
4611 /*
4612 * Call the user function.
4613 * Save and restore search patterns, script variables and
4614 * redo buffer.
4615 */
4616 save_search_patterns();
4617 saveRedobuff();
4618 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 firstline, lastline);
4621 --fp->calls;
4622 restoreRedobuff();
4623 restore_search_patterns();
4624 error = ERROR_NONE;
4625 }
4626 }
4627 }
4628 else
4629 {
4630 /*
4631 * Find the function name in the table, call its implementation.
4632 */
4633 i = find_internal_func(fname);
4634 if (i >= 0)
4635 {
4636 if (argcount < functions[i].f_min_argc)
4637 error = ERROR_TOOFEW;
4638 else if (argcount > functions[i].f_max_argc)
4639 error = ERROR_TOOMANY;
4640 else
4641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 error = ERROR_NONE;
4645 }
4646 }
4647 }
4648 /*
4649 * The function call (or "FuncUndefined" autocommand sequence) might
4650 * have been aborted by an error, an interrupt, or an explicitly thrown
4651 * exception that has not been caught so far. This situation can be
4652 * tested for by calling aborting(). For an error in an internal
4653 * function or for the "E132" error in call_user_func(), however, the
4654 * throw point at which the "force_abort" flag (temporarily reset by
4655 * emsg()) is normally updated has not been reached yet. We need to
4656 * update that flag first to make aborting() reliable.
4657 */
4658 update_force_abort();
4659 }
4660 if (error == ERROR_NONE)
4661 ret = OK;
4662
4663 /*
4664 * Report an error unless the argument evaluation or function call has been
4665 * cancelled due to an aborting error, an interrupt, or an exception.
4666 */
4667 if (error < ERROR_NONE && !aborting())
4668 EMSG2((char_u *)_(errors[error]), name);
4669
4670 name[len] = cc;
4671 if (fname != name && fname != fname_buf)
4672 vim_free(fname);
4673
4674 return ret;
4675}
4676
4677/*********************************************
4678 * Implementation of the built-in functions
4679 */
4680
4681/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00004682 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 */
4684 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00004685f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004689 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004694 l = argvars[0].vval.v_list;
4695 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 }
4698 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00004699 EMSG(_(e_listreq));
4700}
4701
4702/*
4703 * "append(lnum, string/list)" function
4704 */
4705 static void
4706f_append(argvars, rettv)
4707 typeval *argvars;
4708 typeval *rettv;
4709{
4710 long lnum;
4711 listvar *l = NULL;
4712 listitem *li = NULL;
4713 typeval *tv;
4714 long added = 0;
4715
4716 rettv->vval.v_number = 1; /* Default: Failed */
4717 lnum = get_tv_lnum(argvars);
4718 if (lnum >= 0
4719 && lnum <= curbuf->b_ml.ml_line_count
4720 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00004722 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00004724 l = argvars[1].vval.v_list;
4725 if (l == NULL)
4726 return;
4727 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00004729 for (;;)
4730 {
4731 if (l == NULL)
4732 tv = &argvars[1]; /* append a string */
4733 else if (li == NULL)
4734 break; /* end of list */
4735 else
4736 tv = &li->li_tv; /* append item from list */
4737 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
4738 ++added;
4739 if (l == NULL)
4740 break;
4741 li = li->li_next;
4742 }
4743
4744 appended_lines_mark(lnum, added);
4745 if (curwin->w_cursor.lnum > lnum)
4746 curwin->w_cursor.lnum += added;
4747 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749}
4750
4751/*
4752 * "argc()" function
4753 */
4754/* ARGSUSED */
4755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761}
4762
4763/*
4764 * "argidx()" function
4765 */
4766/* ARGSUSED */
4767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773}
4774
4775/*
4776 * "argv(nr)" function
4777 */
4778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 int idx;
4784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 rettv->vval.v_string = NULL;
4790 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791}
4792
4793/*
4794 * "browse(save, title, initdir, default)" function
4795 */
4796/* ARGSUSED */
4797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802#ifdef FEAT_BROWSE
4803 int save;
4804 char_u *title;
4805 char_u *initdir;
4806 char_u *defname;
4807 char_u buf[NUMBUFLEN];
4808 char_u buf2[NUMBUFLEN];
4809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004810 save = get_tv_number(&argvars[0]);
4811 title = get_tv_string(&argvars[1]);
4812 initdir = get_tv_string_buf(&argvars[2], buf);
4813 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004816 do_browse(save ? BROWSE_SAVE : 0,
4817 title, defname, NULL, initdir, NULL, curbuf);
4818#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004820#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004822}
4823
4824/*
4825 * "browsedir(title, initdir)" function
4826 */
4827/* ARGSUSED */
4828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004832{
4833#ifdef FEAT_BROWSE
4834 char_u *title;
4835 char_u *initdir;
4836 char_u buf[NUMBUFLEN];
4837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 title = get_tv_string(&argvars[0]);
4839 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004842 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847}
4848
Bram Moolenaar0d660222005-01-07 21:51:51 +00004849static buf_T *find_buffer __ARGS((typeval *avar));
4850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851/*
4852 * Find a buffer by number or exact name.
4853 */
4854 static buf_T *
4855find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 if (avar->v_type == VAR_NUMBER)
4861 buf = buflist_findnr((int)avar->vval.v_number);
4862 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004865 if (buf == NULL)
4866 {
4867 /* No full path name match, try a match with a URL or a "nofile"
4868 * buffer, these don't use the full path. */
4869 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4870 if (buf->b_fname != NULL
4871 && (path_with_url(buf->b_fname)
4872#ifdef FEAT_QUICKFIX
4873 || bt_nofile(buf)
4874#endif
4875 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004876 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004877 break;
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 return buf;
4881}
4882
4883/*
4884 * "bufexists(expr)" function
4885 */
4886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892}
4893
4894/*
4895 * "buflisted(expr)" function
4896 */
4897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 buf_T *buf;
4903
4904 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906}
4907
4908/*
4909 * "bufloaded(expr)" function
4910 */
4911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 buf_T *buf;
4917
4918 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920}
4921
Bram Moolenaar0d660222005-01-07 21:51:51 +00004922static buf_T *get_buf_tv __ARGS((typeval *tv));
4923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924/*
4925 * Get buffer by number or pattern.
4926 */
4927 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004928get_buf_tv(tv)
4929 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 int save_magic;
4933 char_u *save_cpo;
4934 buf_T *buf;
4935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004936 if (tv->v_type == VAR_NUMBER)
4937 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (name == NULL || *name == NUL)
4939 return curbuf;
4940 if (name[0] == '$' && name[1] == NUL)
4941 return lastbuf;
4942
4943 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
4944 save_magic = p_magic;
4945 p_magic = TRUE;
4946 save_cpo = p_cpo;
4947 p_cpo = (char_u *)"";
4948
4949 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
4950 TRUE, FALSE));
4951
4952 p_magic = save_magic;
4953 p_cpo = save_cpo;
4954
4955 /* If not found, try expanding the name, like done for bufexists(). */
4956 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
4959 return buf;
4960}
4961
4962/*
4963 * "bufname(expr)" function
4964 */
4965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 buf_T *buf;
4971
4972 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 buf = get_buf_tv(&argvars[0]);
4974 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 --emsg_off;
4980}
4981
4982/*
4983 * "bufnr(expr)" function
4984 */
4985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 buf_T *buf;
4991
4992 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 --emsg_off;
4999}
5000
5001/*
5002 * "bufwinnr(nr)" function
5003 */
5004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005006 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009#ifdef FEAT_WINDOWS
5010 win_T *wp;
5011 int winnr = 0;
5012#endif
5013 buf_T *buf;
5014
5015 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017#ifdef FEAT_WINDOWS
5018 for (wp = firstwin; wp; wp = wp->w_next)
5019 {
5020 ++winnr;
5021 if (wp->w_buffer == buf)
5022 break;
5023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005024 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005026 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#endif
5028 --emsg_off;
5029}
5030
5031/*
5032 * "byte2line(byte)" function
5033 */
5034/*ARGSUSED*/
5035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039{
5040#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#else
5043 long boff = 0;
5044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005049 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 (linenr_T)0, &boff);
5051#endif
5052}
5053
5054/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005055 * "byteidx()" function
5056 */
5057/*ARGSUSED*/
5058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005062{
5063#ifdef FEAT_MBYTE
5064 char_u *t;
5065#endif
5066 char_u *str;
5067 long idx;
5068
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 str = get_tv_string(&argvars[0]);
5070 idx = get_tv_number(&argvars[1]);
5071 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005072 if (idx < 0)
5073 return;
5074
5075#ifdef FEAT_MBYTE
5076 t = str;
5077 for ( ; idx > 0; idx--)
5078 {
5079 if (*t == NUL) /* EOL reached */
5080 return;
5081 t += mb_ptr2len_check(t);
5082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005084#else
5085 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005087#endif
5088}
5089
5090/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005091 * "call(func, arglist)" function
5092 */
5093 static void
5094f_call(argvars, rettv)
5095 typeval *argvars;
5096 typeval *rettv;
5097{
5098 char_u *func;
5099 typeval argv[MAX_FUNC_ARGS];
5100 int argc = 0;
5101 listitem *item;
5102 int dummy;
5103
5104 rettv->vval.v_number = 0;
5105 if (argvars[1].v_type != VAR_LIST)
5106 {
5107 EMSG(_(e_listreq));
5108 return;
5109 }
5110 if (argvars[1].vval.v_list == NULL)
5111 return;
5112
5113 if (argvars[0].v_type == VAR_FUNC)
5114 func = argvars[0].vval.v_string;
5115 else
5116 func = get_tv_string(&argvars[0]);
5117
5118 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
5119 item = item->li_next)
5120 {
5121 if (argc == MAX_FUNC_ARGS)
5122 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005123 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005124 break;
5125 }
5126 /* Make a copy of each argument (is this really needed?) */
5127 copy_tv(&item->li_tv, &argv[argc++]);
5128 }
5129
5130 if (item == NULL)
5131 (void)call_func(func, STRLEN(func), rettv, argc, argv,
5132 curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, TRUE);
5133
5134 /* Free the arguments. */
5135 while (argc > 0)
5136 clear_tv(&argv[--argc]);
5137}
5138
5139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 * "char2nr(string)" function
5141 */
5142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
5147#ifdef FEAT_MBYTE
5148 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 rettv->vval.v_number =
5150 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 else
5152#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154}
5155
5156/*
5157 * "cindent(lnum)" function
5158 */
5159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164#ifdef FEAT_CINDENT
5165 pos_T pos;
5166 linenr_T lnum;
5167
5168 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5171 {
5172 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 curwin->w_cursor = pos;
5175 }
5176 else
5177#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179}
5180
5181/*
5182 * "col(string)" function
5183 */
5184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
5189 colnr_T col = 0;
5190 pos_T *fp;
5191
5192 fp = var2fpos(&argvars[0], FALSE);
5193 if (fp != NULL)
5194 {
5195 if (fp->col == MAXCOL)
5196 {
5197 /* '> can be MAXCOL, get the length of the line then */
5198 if (fp->lnum <= curbuf->b_ml.ml_line_count)
5199 col = STRLEN(ml_get(fp->lnum)) + 1;
5200 else
5201 col = MAXCOL;
5202 }
5203 else
5204 {
5205 col = fp->col + 1;
5206#ifdef FEAT_VIRTUALEDIT
5207 /* col(".") when the cursor is on the NUL at the end of the line
5208 * because of "coladd" can be seen as an extra column. */
5209 if (virtual_active() && fp == &curwin->w_cursor)
5210 {
5211 char_u *p = ml_get_cursor();
5212
5213 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
5214 curwin->w_virtcol - curwin->w_cursor.coladd))
5215 {
5216# ifdef FEAT_MBYTE
5217 int l;
5218
5219 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
5220 col += l;
5221# else
5222 if (*p != NUL && p[1] == NUL)
5223 ++col;
5224# endif
5225 }
5226 }
5227#endif
5228 }
5229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005230 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231}
5232
5233/*
5234 * "confirm(message, buttons[, default [, type]])" function
5235 */
5236/*ARGSUSED*/
5237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5243 char_u *message;
5244 char_u *buttons = NULL;
5245 char_u buf[NUMBUFLEN];
5246 char_u buf2[NUMBUFLEN];
5247 int def = 1;
5248 int type = VIM_GENERIC;
5249 int c;
5250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005257 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 switch (TOUPPER_ASC(c))
5263 {
5264 case 'E': type = VIM_ERROR; break;
5265 case 'Q': type = VIM_QUESTION; break;
5266 case 'I': type = VIM_INFO; break;
5267 case 'W': type = VIM_WARNING; break;
5268 case 'G': type = VIM_GENERIC; break;
5269 }
5270 }
5271 }
5272 }
5273
5274 if (buttons == NULL || *buttons == NUL)
5275 buttons = (char_u *)_("&Ok");
5276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 def, NULL);
5279#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#endif
5282}
5283
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284/*
5285 * "copy()" function
5286 */
5287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005288f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291{
5292 if (argvars[0].v_type == VAR_LIST)
5293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 rettv->v_type = VAR_LIST;
5295 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 }
5297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005302 * "count()" function
5303 */
5304 static void
5305f_count(argvars, rettv)
5306 typeval *argvars;
5307 typeval *rettv;
5308{
5309 listitem *li;
5310 long n = 0;
5311 int ic = FALSE;
5312
5313 if (argvars[0].v_type != VAR_LIST)
5314 EMSG(_(e_listreq));
5315 else if (argvars[0].vval.v_list != NULL)
5316 {
5317 if (argvars[2].v_type != VAR_UNKNOWN)
5318 ic = get_tv_number(&argvars[2]);
5319
5320 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
5321 li = li->li_next)
5322 if (tv_equal(&li->li_tv, &argvars[1], ic))
5323 ++n;
5324 }
5325 rettv->vval.v_number = n;
5326}
5327
5328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
5330 *
5331 * Checks the existence of a cscope connection.
5332 */
5333/*ARGSUSED*/
5334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005337 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339#ifdef FEAT_CSCOPE
5340 int num = 0;
5341 char_u *dbpath = NULL;
5342 char_u *prepend = NULL;
5343 char_u buf[NUMBUFLEN];
5344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 if (argvars[0].v_type != VAR_UNKNOWN
5346 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 num = (int)get_tv_number(&argvars[0]);
5349 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005351 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357#endif
5358}
5359
5360/*
5361 * "cursor(lnum, col)" function
5362 *
5363 * Moves the cursor to the specified line and column
5364 */
5365/*ARGSUSED*/
5366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005369 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 long line, col;
5372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (line > 0)
5375 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (col > 0)
5378 curwin->w_cursor.col = col - 1;
5379#ifdef FEAT_VIRTUALEDIT
5380 curwin->w_cursor.coladd = 0;
5381#endif
5382
5383 /* Make sure the cursor is in a valid position. */
5384 check_cursor();
5385#ifdef FEAT_MBYTE
5386 /* Correct cursor for multi-byte character. */
5387 if (has_mbyte)
5388 mb_adjust_cursor();
5389#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005390
5391 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392}
5393
5394/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 */
5397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005398f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005404 rettv->v_type = VAR_LIST;
5405 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 copy_tv(&argvars[0], rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409}
5410
5411/*
5412 * "delete()" function
5413 */
5414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005422 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423}
5424
5425/*
5426 * "did_filetype()" function
5427 */
5428/*ARGSUSED*/
5429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438#endif
5439}
5440
5441/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00005442 * "diff_filler()" function
5443 */
5444/*ARGSUSED*/
5445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005449{
5450#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00005452#endif
5453}
5454
5455/*
5456 * "diff_hlID()" function
5457 */
5458/*ARGSUSED*/
5459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005463{
5464#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00005466 static linenr_T prev_lnum = 0;
5467 static int changedtick = 0;
5468 static int fnum = 0;
5469 static int change_start = 0;
5470 static int change_end = 0;
5471 static enum hlf_value hlID = 0;
5472 int filler_lines;
5473 int col;
5474
5475 if (lnum != prev_lnum
5476 || changedtick != curbuf->b_changedtick
5477 || fnum != curbuf->b_fnum)
5478 {
5479 /* New line, buffer, change: need to get the values. */
5480 filler_lines = diff_check(curwin, lnum);
5481 if (filler_lines < 0)
5482 {
5483 if (filler_lines == -1)
5484 {
5485 change_start = MAXCOL;
5486 change_end = -1;
5487 if (diff_find_change(curwin, lnum, &change_start, &change_end))
5488 hlID = HLF_ADD; /* added line */
5489 else
5490 hlID = HLF_CHD; /* changed line */
5491 }
5492 else
5493 hlID = HLF_ADD; /* added line */
5494 }
5495 else
5496 hlID = (enum hlf_value)0;
5497 prev_lnum = lnum;
5498 changedtick = curbuf->b_changedtick;
5499 fnum = curbuf->b_fnum;
5500 }
5501
5502 if (hlID == HLF_CHD || hlID == HLF_TXD)
5503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005505 if (col >= change_start && col <= change_end)
5506 hlID = HLF_TXD; /* changed text */
5507 else
5508 hlID = HLF_CHD; /* changed line */
5509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005511#endif
5512}
5513
5514/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005515 * "empty({expr})" function
5516 */
5517 static void
5518f_empty(argvars, rettv)
5519 typeval *argvars;
5520 typeval *rettv;
5521{
5522 int n;
5523
5524 switch (argvars[0].v_type)
5525 {
5526 case VAR_STRING:
5527 case VAR_FUNC:
5528 n = argvars[0].vval.v_string == NULL
5529 || *argvars[0].vval.v_string == NUL;
5530 break;
5531 case VAR_NUMBER:
5532 n = argvars[0].vval.v_number == 0;
5533 break;
5534 case VAR_LIST:
5535 n = argvars[0].vval.v_list == NULL
5536 || argvars[0].vval.v_list->lv_first == NULL;
5537 break;
5538 default:
5539 EMSG2(_(e_intern2), "f_empty()");
5540 n = 0;
5541 }
5542
5543 rettv->vval.v_number = n;
5544}
5545
5546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * "escape({string}, {chars})" function
5548 */
5549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 char_u buf[NUMBUFLEN];
5555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 rettv->vval.v_string =
5557 vim_strsave_escaped(get_tv_string(&argvars[0]),
5558 get_tv_string_buf(&argvars[1], buf));
5559 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560}
5561
5562/*
5563 * "eventhandler()" function
5564 */
5565/*ARGSUSED*/
5566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572}
5573
5574/*
5575 * "executable()" function
5576 */
5577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578f_executable(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
5585/*
5586 * "exists()" function
5587 */
5588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 char_u *p;
5594 char_u *name;
5595 int n = FALSE;
5596 int len = 0;
5597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (*p == '$') /* environment variable */
5600 {
5601 /* first try "normal" environment variables (fast) */
5602 if (mch_getenv(p + 1) != NULL)
5603 n = TRUE;
5604 else
5605 {
5606 /* try expanding things like $VIM and ${HOME} */
5607 p = expand_env_save(p);
5608 if (p != NULL && *p != '$')
5609 n = TRUE;
5610 vim_free(p);
5611 }
5612 }
5613 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 else if (*p == '*') /* internal or user defined function */
5616 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 else if (*p == ':')
5620 {
5621 n = cmd_exists(p + 1);
5622 }
5623 else if (*p == '#')
5624 {
5625#ifdef FEAT_AUTOCMD
5626 name = p + 1;
5627 p = vim_strchr(name, '#');
5628 if (p != NULL)
5629 n = au_exists(name, p, p + 1);
5630 else
5631 n = au_exists(name, name + STRLEN(name), NULL);
5632#endif
5633 }
5634 else /* internal variable */
5635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 char_u *expr_start;
5637 char_u *expr_end;
5638 char_u *temp_string = NULL;
5639 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 name = p;
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (expr_start != NULL)
5645 {
5646 temp_string = make_expanded_name(name, expr_start, expr_end, s);
5647 if (temp_string != NULL)
5648 {
5649 len = STRLEN(temp_string);
5650 name = temp_string;
5651 }
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (len == 0)
5654 len = get_id_len(&p);
5655 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662}
5663
5664/*
5665 * "expand()" function
5666 */
5667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 char_u *s;
5673 int len;
5674 char_u *errormsg;
5675 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
5676 expand_T xpc;
5677
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_STRING;
5679 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (*s == '%' || *s == '#' || *s == '<')
5681 {
5682 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 --emsg_off;
5685 }
5686 else
5687 {
5688 /* When the optional second argument is non-zero, don't remove matches
5689 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 flags |= WILD_KEEP_ALL;
5692 ExpandInit(&xpc);
5693 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 ExpandCleanup(&xpc);
5696 }
5697}
5698
5699/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005700 * "extend(list, list [, idx])" function
5701 */
5702 static void
5703f_extend(argvars, rettv)
5704 typeval *argvars;
5705 typeval *rettv;
5706{
5707 long before;
5708 long n;
5709 listitem *item;
5710 listvar *l1, *l2;
5711
5712 rettv->vval.v_number = 0;
5713 if (argvars[0].v_type != VAR_LIST || argvars[1].v_type != VAR_LIST)
5714 {
5715 EMSG(_(e_listreq));
5716 return;
5717 }
5718 l1 = argvars[0].vval.v_list;
5719 l2 = argvars[1].vval.v_list;
5720 if (l1 != NULL && l2 != NULL)
5721 {
5722 if (argvars[2].v_type != VAR_UNKNOWN)
5723 {
5724 n = before = get_tv_number(&argvars[2]);
5725 item = list_find_ext(l1, &n);
5726 if (n != 0)
5727 {
5728 EMSGN(_(e_listidx), before);
5729 return;
5730 }
5731 }
5732 else
5733 item = NULL;
5734 list_extend(l1, l2, item);
5735
5736 ++l1->lv_refcount;
5737 copy_tv(&argvars[0], rettv);
5738 }
5739}
5740
5741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 * "filereadable()" function
5743 */
5744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005747 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 FILE *fd;
5750 char_u *p;
5751 int n;
5752
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005753 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
5755 {
5756 n = TRUE;
5757 fclose(fd);
5758 }
5759 else
5760 n = FALSE;
5761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005762 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763}
5764
5765/*
5766 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
5767 * rights to write into.
5768 */
5769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005772 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 char_u *p;
5775 int retval = 0;
5776#if defined(UNIX) || defined(VMS)
5777 int perm = 0;
5778#endif
5779
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005780 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#if defined(UNIX) || defined(VMS)
5782 perm = mch_getperm(p);
5783#endif
5784#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
5785 if (
5786# ifdef WIN3264
5787 mch_writable(p) &&
5788# else
5789# if defined(UNIX) || defined(VMS)
5790 (perm & 0222) &&
5791# endif
5792# endif
5793 mch_access((char *)p, W_OK) == 0
5794 )
5795#endif
5796 {
5797 ++retval;
5798 if (mch_isdir(p))
5799 ++retval;
5800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005801 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802}
5803
Bram Moolenaar0d660222005-01-07 21:51:51 +00005804static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005805
5806 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005807findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005809 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005810 int dir;
5811{
5812#ifdef FEAT_SEARCHPATH
5813 char_u *fname;
5814 char_u *fresult = NULL;
5815 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
5816 char_u *p;
5817 char_u pathbuf[NUMBUFLEN];
5818 int count = 1;
5819 int first = TRUE;
5820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005821 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005826 if (*p != NUL)
5827 path = p;
5828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005830 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005831 }
5832
5833 do
5834 {
5835 vim_free(fresult);
5836 fresult = find_file_in_path_option(first ? fname : NULL,
5837 first ? (int)STRLEN(fname) : 0,
5838 0, first, path, dir, NULL);
5839 first = FALSE;
5840 } while (--count > 0 && fresult != NULL);
5841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005842 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005843#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005844 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005845#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005846 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005847}
5848
5849/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005850 * "finddir({fname}[, {path}[, {count}]])" function
5851 */
5852 static void
5853f_finddir(argvars, rettv)
5854 typeval *argvars;
5855 typeval *rettv;
5856{
5857 findfilendir(argvars, rettv, TRUE);
5858}
5859
5860/*
5861 * "findfile({fname}[, {path}[, {count}]])" function
5862 */
5863 static void
5864f_findfile(argvars, rettv)
5865 typeval *argvars;
5866 typeval *rettv;
5867{
5868 findfilendir(argvars, rettv, FALSE);
5869}
5870
5871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 * "fnamemodify({fname}, {mods})" function
5873 */
5874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005875f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005877 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 char_u *fname;
5880 char_u *mods;
5881 int usedlen = 0;
5882 int len;
5883 char_u *fbuf = NULL;
5884 char_u buf[NUMBUFLEN];
5885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005886 fname = get_tv_string(&argvars[0]);
5887 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 len = (int)STRLEN(fname);
5889
5890 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
5891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005894 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005896 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 vim_free(fbuf);
5898}
5899
Bram Moolenaar0d660222005-01-07 21:51:51 +00005900static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902/*
5903 * "foldclosed()" function
5904 */
5905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005906foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 int end;
5910{
5911#ifdef FEAT_FOLDING
5912 linenr_T lnum;
5913 linenr_T first, last;
5914
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005915 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5917 {
5918 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
5919 {
5920 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005921 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005923 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 return;
5925 }
5926 }
5927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005928 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929}
5930
5931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005932 * "foldclosed()" function
5933 */
5934 static void
5935f_foldclosed(argvars, rettv)
5936 typeval *argvars;
5937 typeval *rettv;
5938{
5939 foldclosed_both(argvars, rettv, FALSE);
5940}
5941
5942/*
5943 * "foldclosedend()" function
5944 */
5945 static void
5946f_foldclosedend(argvars, rettv)
5947 typeval *argvars;
5948 typeval *rettv;
5949{
5950 foldclosed_both(argvars, rettv, TRUE);
5951}
5952
5953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 * "foldlevel()" function
5955 */
5956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005957f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005959 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961#ifdef FEAT_FOLDING
5962 linenr_T lnum;
5963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005966 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 else
5968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970}
5971
5972/*
5973 * "foldtext()" function
5974 */
5975/*ARGSUSED*/
5976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005977f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981#ifdef FEAT_FOLDING
5982 linenr_T lnum;
5983 char_u *s;
5984 char_u *r;
5985 int len;
5986 char *txt;
5987#endif
5988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 rettv->v_type = VAR_STRING;
5990 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#ifdef FEAT_FOLDING
5992 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
5993 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
5994 && vimvars[VV_FOLDDASHES].val != NULL)
5995 {
5996 /* Find first non-empty line in the fold. */
5997 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
5998 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
5999 {
6000 if (!linewhite(lnum))
6001 break;
6002 ++lnum;
6003 }
6004
6005 /* Find interesting text in this line. */
6006 s = skipwhite(ml_get(lnum));
6007 /* skip C comment-start */
6008 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006011 if (*skipwhite(s) == NUL
6012 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
6013 {
6014 s = skipwhite(ml_get(lnum + 1));
6015 if (*s == '*')
6016 s = skipwhite(s + 1);
6017 }
6018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 txt = _("+-%s%3ld lines: ");
6020 r = alloc((unsigned)(STRLEN(txt)
6021 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
6022 + 20 /* for %3ld */
6023 + STRLEN(s))); /* concatenated */
6024 if (r != NULL)
6025 {
6026 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
6027 (long)((linenr_T)vimvars[VV_FOLDEND].val
6028 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
6029 len = (int)STRLEN(r);
6030 STRCAT(r, s);
6031 /* remove 'foldmarker' and 'commentstring' */
6032 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006033 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035 }
6036#endif
6037}
6038
6039/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006040 * "foldtextresult(lnum)" function
6041 */
6042/*ARGSUSED*/
6043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006044f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006046 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006047{
6048#ifdef FEAT_FOLDING
6049 linenr_T lnum;
6050 char_u *text;
6051 char_u buf[51];
6052 foldinfo_T foldinfo;
6053 int fold_count;
6054#endif
6055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006056 rettv->v_type = VAR_STRING;
6057 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006058#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006059 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006060 fold_count = foldedCount(curwin, lnum, &foldinfo);
6061 if (fold_count > 0)
6062 {
6063 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
6064 &foldinfo, buf);
6065 if (text == buf)
6066 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006067 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006068 }
6069#endif
6070}
6071
6072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 * "foreground()" function
6074 */
6075/*ARGSUSED*/
6076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006077f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006079 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006081 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#ifdef FEAT_GUI
6083 if (gui.in_use)
6084 gui_mch_set_foreground();
6085#else
6086# ifdef WIN32
6087 win32_set_foreground();
6088# endif
6089#endif
6090}
6091
6092/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 * "function()" function
6094 */
6095/*ARGSUSED*/
6096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006097f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006099 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100{
6101 char_u *s;
6102
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006103 s = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006104 if (s == NULL || *s == NUL || isdigit(*s))
6105 EMSG2(_(e_invarg2), s);
6106 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006107 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108 else
6109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006110 rettv->vval.v_string = vim_strsave(s);
6111 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112 }
6113}
6114
6115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006116 * "get()" function
6117 */
6118 static void
6119f_get(argvars, rettv)
6120 typeval *argvars;
6121 typeval *rettv;
6122{
6123 listitem *item;
6124 listvar *l;
6125
6126 if (argvars[0].v_type != VAR_LIST)
6127 EMSG2(_(e_listarg), "get()");
6128 else if ((l = argvars[0].vval.v_list) != NULL)
6129 {
6130 item = list_find(l, get_tv_number(&argvars[1]));
6131 if (item == NULL)
6132 {
6133 if (argvars[2].v_type == VAR_UNKNOWN)
6134 rettv->vval.v_number = 0;
6135 else
6136 copy_tv(&argvars[2], rettv);
6137 }
6138 else
6139 copy_tv(&item->li_tv, rettv);
6140 }
6141}
6142
6143/*
6144 * "getbufvar()" function
6145 */
6146 static void
6147f_getbufvar(argvars, rettv)
6148 typeval *argvars;
6149 typeval *rettv;
6150{
6151 buf_T *buf;
6152 buf_T *save_curbuf;
6153 char_u *varname;
6154 VAR v;
6155
6156 ++emsg_off;
6157 buf = get_buf_tv(&argvars[0]);
6158 varname = get_tv_string(&argvars[1]);
6159
6160 rettv->v_type = VAR_STRING;
6161 rettv->vval.v_string = NULL;
6162
6163 if (buf != NULL && varname != NULL)
6164 {
6165 if (*varname == '&') /* buffer-local-option */
6166 {
6167 /* set curbuf to be our buf, temporarily */
6168 save_curbuf = curbuf;
6169 curbuf = buf;
6170
6171 get_option_tv(&varname, rettv, TRUE);
6172
6173 /* restore previous notion of curbuf */
6174 curbuf = save_curbuf;
6175 }
6176 else
6177 {
6178 /* look up the variable */
6179 v = find_var_in_ga(&buf->b_vars, varname);
6180 if (v != NULL)
6181 copy_tv(&v->tv, rettv);
6182 }
6183 }
6184
6185 --emsg_off;
6186}
6187
6188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 * "getchar()" function
6190 */
6191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006194 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 varnumber_T n;
6197
6198 ++no_mapping;
6199 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 /* getchar(): blocking wait. */
6202 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006203 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 /* getchar(1): only check if char avail */
6205 n = vpeekc();
6206 else if (vpeekc() == NUL)
6207 /* getchar(0) and no char avail: return zero */
6208 n = 0;
6209 else
6210 /* getchar(0) and char avail: return char */
6211 n = safe_vgetc();
6212 --no_mapping;
6213 --allow_keys;
6214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006215 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (IS_SPECIAL(n) || mod_mask != 0)
6217 {
6218 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
6219 int i = 0;
6220
6221 /* Turn a special key into three bytes, plus modifier. */
6222 if (mod_mask != 0)
6223 {
6224 temp[i++] = K_SPECIAL;
6225 temp[i++] = KS_MODIFIER;
6226 temp[i++] = mod_mask;
6227 }
6228 if (IS_SPECIAL(n))
6229 {
6230 temp[i++] = K_SPECIAL;
6231 temp[i++] = K_SECOND(n);
6232 temp[i++] = K_THIRD(n);
6233 }
6234#ifdef FEAT_MBYTE
6235 else if (has_mbyte)
6236 i += (*mb_char2bytes)(n, temp + i);
6237#endif
6238 else
6239 temp[i++] = n;
6240 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006241 rettv->v_type = VAR_STRING;
6242 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244}
6245
6246/*
6247 * "getcharmod()" function
6248 */
6249/*ARGSUSED*/
6250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006251f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006253 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006255 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256}
6257
6258/*
6259 * "getcmdline()" function
6260 */
6261/*ARGSUSED*/
6262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006263f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006265 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 rettv->v_type = VAR_STRING;
6268 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269}
6270
6271/*
6272 * "getcmdpos()" function
6273 */
6274/*ARGSUSED*/
6275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006276f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006278 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006280 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281}
6282
6283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 * "getcwd()" function
6285 */
6286/*ARGSUSED*/
6287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006288f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006290 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 char_u cwd[MAXPATHL];
6293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006296 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 else
6298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006299 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006301 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302#endif
6303 }
6304}
6305
6306/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006307 * "getfontname()" function
6308 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006309/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006311f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006313 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006315 rettv->v_type = VAR_STRING;
6316 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006317#ifdef FEAT_GUI
6318 if (gui.in_use)
6319 {
6320 GuiFont font;
6321 char_u *name = NULL;
6322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006324 {
6325 /* Get the "Normal" font. Either the name saved by
6326 * hl_set_font_name() or from the font ID. */
6327 font = gui.norm_font;
6328 name = hl_get_font_name();
6329 }
6330 else
6331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006332 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006333 if (STRCMP(name, "*") == 0) /* don't use font dialog */
6334 return;
6335 font = gui_mch_get_font(name, FALSE);
6336 if (font == NOFONT)
6337 return; /* Invalid font name, return empty string. */
6338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006339 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006341 gui_mch_free_font(font);
6342 }
6343#endif
6344}
6345
6346/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006347 * "getfperm({fname})" function
6348 */
6349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006352 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006353{
6354 char_u *fname;
6355 struct stat st;
6356 char_u *perm = NULL;
6357 char_u flags[] = "rwx";
6358 int i;
6359
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006360 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006361
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006363 if (mch_stat((char *)fname, &st) >= 0)
6364 {
6365 perm = vim_strsave((char_u *)"---------");
6366 if (perm != NULL)
6367 {
6368 for (i = 0; i < 9; i++)
6369 {
6370 if (st.st_mode & (1 << (8 - i)))
6371 perm[i] = flags[i % 3];
6372 }
6373 }
6374 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006375 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006376}
6377
6378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 * "getfsize({fname})" function
6380 */
6381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006382f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 char_u *fname;
6387 struct stat st;
6388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006389 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393 if (mch_stat((char *)fname, &st) >= 0)
6394 {
6395 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006396 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006398 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006401 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402}
6403
6404/*
6405 * "getftime({fname})" function
6406 */
6407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 char_u *fname;
6413 struct stat st;
6414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006415 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421}
6422
6423/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006424 * "getftype({fname})" function
6425 */
6426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006429 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006430{
6431 char_u *fname;
6432 struct stat st;
6433 char_u *type = NULL;
6434 char *t;
6435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006436 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006438 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006439 if (mch_lstat((char *)fname, &st) >= 0)
6440 {
6441#ifdef S_ISREG
6442 if (S_ISREG(st.st_mode))
6443 t = "file";
6444 else if (S_ISDIR(st.st_mode))
6445 t = "dir";
6446# ifdef S_ISLNK
6447 else if (S_ISLNK(st.st_mode))
6448 t = "link";
6449# endif
6450# ifdef S_ISBLK
6451 else if (S_ISBLK(st.st_mode))
6452 t = "bdev";
6453# endif
6454# ifdef S_ISCHR
6455 else if (S_ISCHR(st.st_mode))
6456 t = "cdev";
6457# endif
6458# ifdef S_ISFIFO
6459 else if (S_ISFIFO(st.st_mode))
6460 t = "fifo";
6461# endif
6462# ifdef S_ISSOCK
6463 else if (S_ISSOCK(st.st_mode))
6464 t = "fifo";
6465# endif
6466 else
6467 t = "other";
6468#else
6469# ifdef S_IFMT
6470 switch (st.st_mode & S_IFMT)
6471 {
6472 case S_IFREG: t = "file"; break;
6473 case S_IFDIR: t = "dir"; break;
6474# ifdef S_IFLNK
6475 case S_IFLNK: t = "link"; break;
6476# endif
6477# ifdef S_IFBLK
6478 case S_IFBLK: t = "bdev"; break;
6479# endif
6480# ifdef S_IFCHR
6481 case S_IFCHR: t = "cdev"; break;
6482# endif
6483# ifdef S_IFIFO
6484 case S_IFIFO: t = "fifo"; break;
6485# endif
6486# ifdef S_IFSOCK
6487 case S_IFSOCK: t = "socket"; break;
6488# endif
6489 default: t = "other";
6490 }
6491# else
6492 if (mch_isdir(fname))
6493 t = "dir";
6494 else
6495 t = "file";
6496# endif
6497#endif
6498 type = vim_strsave((char_u *)t);
6499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006500 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006501}
6502
6503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006504 * "getline(lnum)" function
6505 */
6506 static void
6507f_getline(argvars, rettv)
6508 typeval *argvars;
6509 typeval *rettv;
6510{
6511 linenr_T lnum;
6512 linenr_T end;
6513 char_u *p;
6514 listvar *l;
6515 listitem *li;
6516
6517 lnum = get_tv_lnum(argvars);
6518
6519 if (argvars[1].v_type == VAR_UNKNOWN)
6520 {
6521 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6522 p = ml_get(lnum);
6523 else
6524 p = (char_u *)"";
6525
6526 rettv->v_type = VAR_STRING;
6527 rettv->vval.v_string = vim_strsave(p);
6528 }
6529 else
6530 {
6531 end = get_tv_lnum(&argvars[1]);
6532 if (end < lnum)
6533 {
6534 EMSG(_(e_invrange));
6535 rettv->vval.v_number = 0;
6536 }
6537 else
6538 {
6539 l = list_alloc();
6540 if (l != NULL)
6541 {
6542 if (lnum < 1)
6543 lnum = 1;
6544 if (end > curbuf->b_ml.ml_line_count)
6545 end = curbuf->b_ml.ml_line_count;
6546 while (lnum <= end)
6547 {
6548 li = listitem_alloc();
6549 if (li == NULL)
6550 break;
6551 list_append(l, li);
6552 li->li_tv.v_type = VAR_STRING;
6553 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
6554 }
6555 rettv->vval.v_list = l;
6556 rettv->v_type = VAR_LIST;
6557 ++l->lv_refcount;
6558 }
6559 }
6560 }
6561}
6562
6563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 * "getreg()" function
6565 */
6566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006567f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571 char_u *strregname;
6572 int regname;
6573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 else
6577 strregname = vimvars[VV_REG].val;
6578 regname = (strregname == NULL ? '"' : *strregname);
6579 if (regname == 0)
6580 regname = '"';
6581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006582 rettv->v_type = VAR_STRING;
6583 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
6585
6586/*
6587 * "getregtype()" function
6588 */
6589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006592 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 char_u *strregname;
6595 int regname;
6596 char_u buf[NUMBUFLEN + 2];
6597 long reglen = 0;
6598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 else
6602 /* Default to v:register */
6603 strregname = vimvars[VV_REG].val;
6604
6605 regname = (strregname == NULL ? '"' : *strregname);
6606 if (regname == 0)
6607 regname = '"';
6608
6609 buf[0] = NUL;
6610 buf[1] = NUL;
6611 switch (get_reg_type(regname, &reglen))
6612 {
6613 case MLINE: buf[0] = 'V'; break;
6614 case MCHAR: buf[0] = 'v'; break;
6615#ifdef FEAT_VISUAL
6616 case MBLOCK:
6617 buf[0] = Ctrl_V;
6618 sprintf((char *)buf + 1, "%ld", reglen + 1);
6619 break;
6620#endif
6621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006622 rettv->v_type = VAR_STRING;
6623 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624}
6625
6626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 * "getwinposx()" function
6628 */
6629/*ARGSUSED*/
6630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006631f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006633 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#ifdef FEAT_GUI
6637 if (gui.in_use)
6638 {
6639 int x, y;
6640
6641 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006642 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644#endif
6645}
6646
6647/*
6648 * "getwinposy()" function
6649 */
6650/*ARGSUSED*/
6651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006652f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006654 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006656 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657#ifdef FEAT_GUI
6658 if (gui.in_use)
6659 {
6660 int x, y;
6661
6662 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006663 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 }
6665#endif
6666}
6667
6668/*
6669 * "getwinvar()" function
6670 */
6671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 win_T *win, *oldcurwin;
6677 char_u *varname;
6678 VAR v;
6679
6680 ++emsg_off;
6681 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006682 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006684 rettv->v_type = VAR_STRING;
6685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687 if (win != NULL && varname != NULL)
6688 {
6689 if (*varname == '&') /* window-local-option */
6690 {
6691 /* set curwin to be our win, temporarily */
6692 oldcurwin = curwin;
6693 curwin = win;
6694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006695 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 /* restore previous notion of curwin */
6698 curwin = oldcurwin;
6699 }
6700 else
6701 {
6702 /* look up the variable */
6703 v = find_var_in_ga(&win->w_vars, varname);
6704 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006705 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
6707 }
6708
6709 --emsg_off;
6710}
6711
6712/*
6713 * "glob()" function
6714 */
6715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006716f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006717 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006718 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 expand_T xpc;
6721
6722 ExpandInit(&xpc);
6723 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006724 rettv->v_type = VAR_STRING;
6725 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
6727 ExpandCleanup(&xpc);
6728}
6729
6730/*
6731 * "globpath()" function
6732 */
6733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 char_u buf1[NUMBUFLEN];
6739
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006740 rettv->v_type = VAR_STRING;
6741 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
6742 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743}
6744
6745/*
6746 * "has()" function
6747 */
6748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006749f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006751 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int i;
6754 char_u *name;
6755 int n = FALSE;
6756 static char *(has_list[]) =
6757 {
6758#ifdef AMIGA
6759 "amiga",
6760# ifdef FEAT_ARP
6761 "arp",
6762# endif
6763#endif
6764#ifdef __BEOS__
6765 "beos",
6766#endif
6767#ifdef MSDOS
6768# ifdef DJGPP
6769 "dos32",
6770# else
6771 "dos16",
6772# endif
6773#endif
6774#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
6775 "mac",
6776#endif
6777#if defined(MACOS_X_UNIX)
6778 "macunix",
6779#endif
6780#ifdef OS2
6781 "os2",
6782#endif
6783#ifdef __QNX__
6784 "qnx",
6785#endif
6786#ifdef RISCOS
6787 "riscos",
6788#endif
6789#ifdef UNIX
6790 "unix",
6791#endif
6792#ifdef VMS
6793 "vms",
6794#endif
6795#ifdef WIN16
6796 "win16",
6797#endif
6798#ifdef WIN32
6799 "win32",
6800#endif
6801#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6802 "win32unix",
6803#endif
6804#ifdef WIN64
6805 "win64",
6806#endif
6807#ifdef EBCDIC
6808 "ebcdic",
6809#endif
6810#ifndef CASE_INSENSITIVE_FILENAME
6811 "fname_case",
6812#endif
6813#ifdef FEAT_ARABIC
6814 "arabic",
6815#endif
6816#ifdef FEAT_AUTOCMD
6817 "autocmd",
6818#endif
6819#ifdef FEAT_BEVAL
6820 "balloon_eval",
6821#endif
6822#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6823 "builtin_terms",
6824# ifdef ALL_BUILTIN_TCAPS
6825 "all_builtin_terms",
6826# endif
6827#endif
6828#ifdef FEAT_BYTEOFF
6829 "byte_offset",
6830#endif
6831#ifdef FEAT_CINDENT
6832 "cindent",
6833#endif
6834#ifdef FEAT_CLIENTSERVER
6835 "clientserver",
6836#endif
6837#ifdef FEAT_CLIPBOARD
6838 "clipboard",
6839#endif
6840#ifdef FEAT_CMDL_COMPL
6841 "cmdline_compl",
6842#endif
6843#ifdef FEAT_CMDHIST
6844 "cmdline_hist",
6845#endif
6846#ifdef FEAT_COMMENTS
6847 "comments",
6848#endif
6849#ifdef FEAT_CRYPT
6850 "cryptv",
6851#endif
6852#ifdef FEAT_CSCOPE
6853 "cscope",
6854#endif
6855#ifdef DEBUG
6856 "debug",
6857#endif
6858#ifdef FEAT_CON_DIALOG
6859 "dialog_con",
6860#endif
6861#ifdef FEAT_GUI_DIALOG
6862 "dialog_gui",
6863#endif
6864#ifdef FEAT_DIFF
6865 "diff",
6866#endif
6867#ifdef FEAT_DIGRAPHS
6868 "digraphs",
6869#endif
6870#ifdef FEAT_DND
6871 "dnd",
6872#endif
6873#ifdef FEAT_EMACS_TAGS
6874 "emacs_tags",
6875#endif
6876 "eval", /* always present, of course! */
6877#ifdef FEAT_EX_EXTRA
6878 "ex_extra",
6879#endif
6880#ifdef FEAT_SEARCH_EXTRA
6881 "extra_search",
6882#endif
6883#ifdef FEAT_FKMAP
6884 "farsi",
6885#endif
6886#ifdef FEAT_SEARCHPATH
6887 "file_in_path",
6888#endif
6889#ifdef FEAT_FIND_ID
6890 "find_in_path",
6891#endif
6892#ifdef FEAT_FOLDING
6893 "folding",
6894#endif
6895#ifdef FEAT_FOOTER
6896 "footer",
6897#endif
6898#if !defined(USE_SYSTEM) && defined(UNIX)
6899 "fork",
6900#endif
6901#ifdef FEAT_GETTEXT
6902 "gettext",
6903#endif
6904#ifdef FEAT_GUI
6905 "gui",
6906#endif
6907#ifdef FEAT_GUI_ATHENA
6908# ifdef FEAT_GUI_NEXTAW
6909 "gui_neXtaw",
6910# else
6911 "gui_athena",
6912# endif
6913#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00006914#ifdef FEAT_GUI_KDE
6915 "gui_kde",
6916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917#ifdef FEAT_GUI_GTK
6918 "gui_gtk",
6919# ifdef HAVE_GTK2
6920 "gui_gtk2",
6921# endif
6922#endif
6923#ifdef FEAT_GUI_MAC
6924 "gui_mac",
6925#endif
6926#ifdef FEAT_GUI_MOTIF
6927 "gui_motif",
6928#endif
6929#ifdef FEAT_GUI_PHOTON
6930 "gui_photon",
6931#endif
6932#ifdef FEAT_GUI_W16
6933 "gui_win16",
6934#endif
6935#ifdef FEAT_GUI_W32
6936 "gui_win32",
6937#endif
6938#ifdef FEAT_HANGULIN
6939 "hangul_input",
6940#endif
6941#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6942 "iconv",
6943#endif
6944#ifdef FEAT_INS_EXPAND
6945 "insert_expand",
6946#endif
6947#ifdef FEAT_JUMPLIST
6948 "jumplist",
6949#endif
6950#ifdef FEAT_KEYMAP
6951 "keymap",
6952#endif
6953#ifdef FEAT_LANGMAP
6954 "langmap",
6955#endif
6956#ifdef FEAT_LIBCALL
6957 "libcall",
6958#endif
6959#ifdef FEAT_LINEBREAK
6960 "linebreak",
6961#endif
6962#ifdef FEAT_LISP
6963 "lispindent",
6964#endif
6965#ifdef FEAT_LISTCMDS
6966 "listcmds",
6967#endif
6968#ifdef FEAT_LOCALMAP
6969 "localmap",
6970#endif
6971#ifdef FEAT_MENU
6972 "menu",
6973#endif
6974#ifdef FEAT_SESSION
6975 "mksession",
6976#endif
6977#ifdef FEAT_MODIFY_FNAME
6978 "modify_fname",
6979#endif
6980#ifdef FEAT_MOUSE
6981 "mouse",
6982#endif
6983#ifdef FEAT_MOUSESHAPE
6984 "mouseshape",
6985#endif
6986#if defined(UNIX) || defined(VMS)
6987# ifdef FEAT_MOUSE_DEC
6988 "mouse_dec",
6989# endif
6990# ifdef FEAT_MOUSE_GPM
6991 "mouse_gpm",
6992# endif
6993# ifdef FEAT_MOUSE_JSB
6994 "mouse_jsbterm",
6995# endif
6996# ifdef FEAT_MOUSE_NET
6997 "mouse_netterm",
6998# endif
6999# ifdef FEAT_MOUSE_PTERM
7000 "mouse_pterm",
7001# endif
7002# ifdef FEAT_MOUSE_XTERM
7003 "mouse_xterm",
7004# endif
7005#endif
7006#ifdef FEAT_MBYTE
7007 "multi_byte",
7008#endif
7009#ifdef FEAT_MBYTE_IME
7010 "multi_byte_ime",
7011#endif
7012#ifdef FEAT_MULTI_LANG
7013 "multi_lang",
7014#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007015#ifdef FEAT_MZSCHEME
7016 "mzscheme",
7017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#ifdef FEAT_OLE
7019 "ole",
7020#endif
7021#ifdef FEAT_OSFILETYPE
7022 "osfiletype",
7023#endif
7024#ifdef FEAT_PATH_EXTRA
7025 "path_extra",
7026#endif
7027#ifdef FEAT_PERL
7028#ifndef DYNAMIC_PERL
7029 "perl",
7030#endif
7031#endif
7032#ifdef FEAT_PYTHON
7033#ifndef DYNAMIC_PYTHON
7034 "python",
7035#endif
7036#endif
7037#ifdef FEAT_POSTSCRIPT
7038 "postscript",
7039#endif
7040#ifdef FEAT_PRINTER
7041 "printer",
7042#endif
7043#ifdef FEAT_QUICKFIX
7044 "quickfix",
7045#endif
7046#ifdef FEAT_RIGHTLEFT
7047 "rightleft",
7048#endif
7049#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
7050 "ruby",
7051#endif
7052#ifdef FEAT_SCROLLBIND
7053 "scrollbind",
7054#endif
7055#ifdef FEAT_CMDL_INFO
7056 "showcmd",
7057 "cmdline_info",
7058#endif
7059#ifdef FEAT_SIGNS
7060 "signs",
7061#endif
7062#ifdef FEAT_SMARTINDENT
7063 "smartindent",
7064#endif
7065#ifdef FEAT_SNIFF
7066 "sniff",
7067#endif
7068#ifdef FEAT_STL_OPT
7069 "statusline",
7070#endif
7071#ifdef FEAT_SUN_WORKSHOP
7072 "sun_workshop",
7073#endif
7074#ifdef FEAT_NETBEANS_INTG
7075 "netbeans_intg",
7076#endif
7077#ifdef FEAT_SYN_HL
7078 "syntax",
7079#endif
7080#if defined(USE_SYSTEM) || !defined(UNIX)
7081 "system",
7082#endif
7083#ifdef FEAT_TAG_BINS
7084 "tag_binary",
7085#endif
7086#ifdef FEAT_TAG_OLDSTATIC
7087 "tag_old_static",
7088#endif
7089#ifdef FEAT_TAG_ANYWHITE
7090 "tag_any_white",
7091#endif
7092#ifdef FEAT_TCL
7093# ifndef DYNAMIC_TCL
7094 "tcl",
7095# endif
7096#endif
7097#ifdef TERMINFO
7098 "terminfo",
7099#endif
7100#ifdef FEAT_TERMRESPONSE
7101 "termresponse",
7102#endif
7103#ifdef FEAT_TEXTOBJ
7104 "textobjects",
7105#endif
7106#ifdef HAVE_TGETENT
7107 "tgetent",
7108#endif
7109#ifdef FEAT_TITLE
7110 "title",
7111#endif
7112#ifdef FEAT_TOOLBAR
7113 "toolbar",
7114#endif
7115#ifdef FEAT_USR_CMDS
7116 "user-commands", /* was accidentally included in 5.4 */
7117 "user_commands",
7118#endif
7119#ifdef FEAT_VIMINFO
7120 "viminfo",
7121#endif
7122#ifdef FEAT_VERTSPLIT
7123 "vertsplit",
7124#endif
7125#ifdef FEAT_VIRTUALEDIT
7126 "virtualedit",
7127#endif
7128#ifdef FEAT_VISUAL
7129 "visual",
7130#endif
7131#ifdef FEAT_VISUALEXTRA
7132 "visualextra",
7133#endif
7134#ifdef FEAT_VREPLACE
7135 "vreplace",
7136#endif
7137#ifdef FEAT_WILDIGN
7138 "wildignore",
7139#endif
7140#ifdef FEAT_WILDMENU
7141 "wildmenu",
7142#endif
7143#ifdef FEAT_WINDOWS
7144 "windows",
7145#endif
7146#ifdef FEAT_WAK
7147 "winaltkeys",
7148#endif
7149#ifdef FEAT_WRITEBACKUP
7150 "writebackup",
7151#endif
7152#ifdef FEAT_XIM
7153 "xim",
7154#endif
7155#ifdef FEAT_XFONTSET
7156 "xfontset",
7157#endif
7158#ifdef USE_XSMP
7159 "xsmp",
7160#endif
7161#ifdef USE_XSMP_INTERACT
7162 "xsmp_interact",
7163#endif
7164#ifdef FEAT_XCLIPBOARD
7165 "xterm_clipboard",
7166#endif
7167#ifdef FEAT_XTERM_SAVE
7168 "xterm_save",
7169#endif
7170#if defined(UNIX) && defined(FEAT_X11)
7171 "X11",
7172#endif
7173 NULL
7174 };
7175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007176 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 for (i = 0; has_list[i] != NULL; ++i)
7178 if (STRICMP(name, has_list[i]) == 0)
7179 {
7180 n = TRUE;
7181 break;
7182 }
7183
7184 if (n == FALSE)
7185 {
7186 if (STRNICMP(name, "patch", 5) == 0)
7187 n = has_patch(atoi((char *)name + 5));
7188 else if (STRICMP(name, "vim_starting") == 0)
7189 n = (starting != 0);
7190#ifdef DYNAMIC_TCL
7191 else if (STRICMP(name, "tcl") == 0)
7192 n = tcl_enabled(FALSE);
7193#endif
7194#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
7195 else if (STRICMP(name, "iconv") == 0)
7196 n = iconv_enabled(FALSE);
7197#endif
7198#ifdef DYNAMIC_RUBY
7199 else if (STRICMP(name, "ruby") == 0)
7200 n = ruby_enabled(FALSE);
7201#endif
7202#ifdef DYNAMIC_PYTHON
7203 else if (STRICMP(name, "python") == 0)
7204 n = python_enabled(FALSE);
7205#endif
7206#ifdef DYNAMIC_PERL
7207 else if (STRICMP(name, "perl") == 0)
7208 n = perl_enabled(FALSE);
7209#endif
7210#ifdef FEAT_GUI
7211 else if (STRICMP(name, "gui_running") == 0)
7212 n = (gui.in_use || gui.starting);
7213# ifdef FEAT_GUI_W32
7214 else if (STRICMP(name, "gui_win32s") == 0)
7215 n = gui_is_win32s();
7216# endif
7217# ifdef FEAT_BROWSE
7218 else if (STRICMP(name, "browse") == 0)
7219 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7220# endif
7221#endif
7222#ifdef FEAT_SYN_HL
7223 else if (STRICMP(name, "syntax_items") == 0)
7224 n = syntax_present(curbuf);
7225#endif
7226#if defined(WIN3264)
7227 else if (STRICMP(name, "win95") == 0)
7228 n = mch_windows95();
7229#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00007230#ifdef FEAT_NETBEANS_INTG
7231 else if (STRICMP(name, "netbeans_enabled") == 0)
7232 n = usingNetbeans;
7233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237}
7238
7239/*
7240 * "hasmapto()" function
7241 */
7242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007245 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
7247 char_u *name;
7248 char_u *mode;
7249 char_u buf[NUMBUFLEN];
7250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007251 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007252 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 mode = (char_u *)"nvo";
7254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007255 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007258 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007260 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261}
7262
7263/*
7264 * "histadd()" function
7265 */
7266/*ARGSUSED*/
7267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007268f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007269 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007270 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272#ifdef FEAT_CMDHIST
7273 int histype;
7274 char_u *str;
7275 char_u buf[NUMBUFLEN];
7276#endif
7277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 if (check_restricted() || check_secure())
7280 return;
7281#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007282 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (histype >= 0)
7284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007285 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 if (*str != NUL)
7287 {
7288 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007289 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 return;
7291 }
7292 }
7293#endif
7294}
7295
7296/*
7297 * "histdel()" function
7298 */
7299/*ARGSUSED*/
7300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007301f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007303 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
7305#ifdef FEAT_CMDHIST
7306 int n;
7307 char_u buf[NUMBUFLEN];
7308
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007309 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007311 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007314 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
7315 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 else
7317 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007318 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
7319 get_tv_string_buf(&argvars[1], buf));
7320 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007322 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323#endif
7324}
7325
7326/*
7327 * "histget()" function
7328 */
7329/*ARGSUSED*/
7330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007331f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007332 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007333 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335#ifdef FEAT_CMDHIST
7336 int type;
7337 int idx;
7338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007339 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 idx = get_history_idx(type);
7342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007343 idx = (int)get_tv_number(&argvars[1]);
7344 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007348 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349}
7350
7351/*
7352 * "histnr()" function
7353 */
7354/*ARGSUSED*/
7355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007356f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007357 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
7360 int i;
7361
7362#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007363 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (i >= HIST_CMD && i < HIST_COUNT)
7365 i = get_history_idx(i);
7366 else
7367#endif
7368 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007369 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370}
7371
7372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 * "highlightID(name)" function
7374 */
7375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007377 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007380 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007384 * "highlight_exists()" function
7385 */
7386 static void
7387f_hlexists(argvars, rettv)
7388 typeval *argvars;
7389 typeval *rettv;
7390{
7391 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
7392}
7393
7394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 * "hostname()" function
7396 */
7397/*ARGSUSED*/
7398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007400 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007401 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 char_u hostname[256];
7404
7405 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007406 rettv->v_type = VAR_STRING;
7407 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408}
7409
7410/*
7411 * iconv() function
7412 */
7413/*ARGSUSED*/
7414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
7419#ifdef FEAT_MBYTE
7420 char_u buf1[NUMBUFLEN];
7421 char_u buf2[NUMBUFLEN];
7422 char_u *from, *to, *str;
7423 vimconv_T vimconv;
7424#endif
7425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 rettv->v_type = VAR_STRING;
7427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007430 str = get_tv_string(&argvars[0]);
7431 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
7432 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 vimconv.vc_type = CONV_NONE;
7434 convert_setup(&vimconv, from, to);
7435
7436 /* If the encodings are equal, no conversion needed. */
7437 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007438 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
7442 convert_setup(&vimconv, NULL, NULL);
7443 vim_free(from);
7444 vim_free(to);
7445#endif
7446}
7447
7448/*
7449 * "indent()" function
7450 */
7451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007453 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
7456 linenr_T lnum;
7457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007458 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007465/*
7466 * "index()" function
7467 */
7468 static void
7469f_index(argvars, rettv)
7470 typeval *argvars;
7471 typeval *rettv;
7472{
7473 listvar *l;
7474 listitem *item;
7475 long idx = 0;
7476 int ic = FALSE;
7477
7478 rettv->vval.v_number = -1;
7479 if (argvars[0].v_type != VAR_LIST)
7480 {
7481 EMSG(_(e_listreq));
7482 return;
7483 }
7484 l = argvars[0].vval.v_list;
7485 if (l != NULL)
7486 {
7487 if (argvars[2].v_type != VAR_UNKNOWN)
7488 ic = get_tv_number(&argvars[2]);
7489
7490 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
7491 if (tv_equal(&item->li_tv, &argvars[1], ic))
7492 {
7493 rettv->vval.v_number = idx;
7494 break;
7495 }
7496 }
7497}
7498
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499static int inputsecret_flag = 0;
7500
7501/*
7502 * "input()" function
7503 * Also handles inputsecret() when inputsecret is set.
7504 */
7505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007507 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 char_u *p = NULL;
7512 int c;
7513 char_u buf[NUMBUFLEN];
7514 int cmd_silent_save = cmd_silent;
7515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517
7518#ifdef NO_CONSOLE_INPUT
7519 /* While starting up, there is no place to enter text. */
7520 if (no_console_input())
7521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 return;
7524 }
7525#endif
7526
7527 cmd_silent = FALSE; /* Want to see the prompt. */
7528 if (prompt != NULL)
7529 {
7530 /* Only the part of the message after the last NL is considered as
7531 * prompt for the command line */
7532 p = vim_strrchr(prompt, '\n');
7533 if (p == NULL)
7534 p = prompt;
7535 else
7536 {
7537 ++p;
7538 c = *p;
7539 *p = NUL;
7540 msg_start();
7541 msg_clr_eos();
7542 msg_puts_attr(prompt, echo_attr);
7543 msg_didout = FALSE;
7544 msg_starthere();
7545 *p = c;
7546 }
7547 cmdline_row = msg_row;
7548 }
7549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
7555
7556 /* since the user typed this, no need to wait for return */
7557 need_wait_return = FALSE;
7558 msg_didout = FALSE;
7559 cmd_silent = cmd_silent_save;
7560}
7561
7562/*
7563 * "inputdialog()" function
7564 */
7565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007566f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007568 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570#if defined(FEAT_GUI_TEXTDIALOG)
7571 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7572 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7573 {
7574 char_u *message;
7575 char_u buf[NUMBUFLEN];
7576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 IObuff[IOSIZE - 1] = NUL;
7582 }
7583 else
7584 IObuff[0] = NUL;
7585 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
7586 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007590 if (argvars[1].v_type != VAR_UNKNOWN
7591 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592 rettv->vval.v_string = vim_strsave(
7593 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599 else
7600#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
7604static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7605
7606/*
7607 * "inputrestore()" function
7608 */
7609/*ARGSUSED*/
7610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614{
7615 if (ga_userinput.ga_len > 0)
7616 {
7617 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7619 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 else if (p_verbose > 1)
7623 {
7624 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
7627}
7628
7629/*
7630 * "inputsave()" function
7631 */
7632/*ARGSUSED*/
7633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637{
7638 /* Add an entry to the stack of typehead storage. */
7639 if (ga_grow(&ga_userinput, 1) == OK)
7640 {
7641 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7642 + ga_userinput.ga_len);
7643 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
7651 * "inputsecret()" function
7652 */
7653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007655 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 ++cmdline_star;
7659 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 --cmdline_star;
7662 --inputsecret_flag;
7663}
7664
7665/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007666 * "insert()" function
7667 */
7668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672{
7673 long before = 0;
7674 long n;
7675 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676 listvar *l;
7677
7678 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007679 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 else if ((l = argvars[0].vval.v_list) != NULL)
7681 {
7682 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007684
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007685 n = before;
7686 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 if (n > 0)
7688 EMSGN(_(e_listidx), before);
7689 else
7690 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007691 list_insert_tv(l, &argvars[1], item);
7692 ++l->lv_refcount;
7693 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007694 }
7695 }
7696}
7697
7698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 * "isdirectory()" function
7700 */
7701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007703 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007704 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707}
7708
7709/*
7710 * "last_buffer_nr()" function.
7711 */
7712/*ARGSUSED*/
7713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
7718 int n = 0;
7719 buf_T *buf;
7720
7721 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7722 if (n < buf->b_fnum)
7723 n = buf->b_fnum;
7724
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726}
7727
7728/*
7729 * "len()" function
7730 */
7731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007733 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007735{
7736 switch (argvars[0].v_type)
7737 {
7738 case VAR_STRING:
7739 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740 rettv->vval.v_number = (varnumber_T)STRLEN(
7741 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007742 break;
7743 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 break;
7746 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007747 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 break;
7749 }
7750}
7751
Bram Moolenaar0d660222005-01-07 21:51:51 +00007752static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753
7754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007758 int type;
7759{
7760#ifdef FEAT_LIBCALL
7761 char_u *string_in;
7762 char_u **string_result;
7763 int nr_result;
7764#endif
7765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007771
7772 if (check_restricted() || check_secure())
7773 return;
7774
7775#ifdef FEAT_LIBCALL
7776 /* The first two args must be strings, otherwise its meaningless */
7777 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7778 {
7779 if (argvars[2].v_type == VAR_NUMBER)
7780 string_in = NULL;
7781 else
7782 string_in = argvars[2].vval.v_string;
7783 if (type == VAR_NUMBER)
7784 string_result = NULL;
7785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007787 if (mch_libcall(argvars[0].vval.v_string,
7788 argvars[1].vval.v_string,
7789 string_in,
7790 argvars[2].vval.v_number,
7791 string_result,
7792 &nr_result) == OK
7793 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 }
7796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
7799/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007800 * "libcall()" function
7801 */
7802 static void
7803f_libcall(argvars, rettv)
7804 typeval *argvars;
7805 typeval *rettv;
7806{
7807 libcall_common(argvars, rettv, VAR_STRING);
7808}
7809
7810/*
7811 * "libcallnr()" function
7812 */
7813 static void
7814f_libcallnr(argvars, rettv)
7815 typeval *argvars;
7816 typeval *rettv;
7817{
7818 libcall_common(argvars, rettv, VAR_NUMBER);
7819}
7820
7821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 * "line(string)" function
7823 */
7824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 linenr_T lnum = 0;
7830 pos_T *fp;
7831
7832 fp = var2fpos(&argvars[0], TRUE);
7833 if (fp != NULL)
7834 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836}
7837
7838/*
7839 * "line2byte(lnum)" function
7840 */
7841/*ARGSUSED*/
7842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007844 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849#else
7850 linenr_T lnum;
7851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007854 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7857 if (rettv->vval.v_number >= 0)
7858 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#endif
7860}
7861
7862/*
7863 * "lispindent(lnum)" function
7864 */
7865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
7870#ifdef FEAT_LISP
7871 pos_T pos;
7872 linenr_T lnum;
7873
7874 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7877 {
7878 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 curwin->w_cursor = pos;
7881 }
7882 else
7883#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885}
7886
7887/*
7888 * "localtime()" function
7889 */
7890/*ARGSUSED*/
7891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
Bram Moolenaar0d660222005-01-07 21:51:51 +00007899static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
7901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007904 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 int exact;
7906{
7907 char_u *keys;
7908 char_u *which;
7909 char_u buf[NUMBUFLEN];
7910 char_u *keys_buf = NULL;
7911 char_u *rhs;
7912 int mode;
7913 garray_T ga;
7914
7915 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 rettv->v_type = VAR_STRING;
7917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007919 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 if (*keys == NUL)
7921 return;
7922
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007923 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 else
7926 which = (char_u *)"";
7927 mode = get_map_mode(&which, 0);
7928
7929 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
7930 rhs = check_map(keys, mode, exact);
7931 vim_free(keys_buf);
7932 if (rhs != NULL)
7933 {
7934 ga_init(&ga);
7935 ga.ga_itemsize = 1;
7936 ga.ga_growsize = 40;
7937
7938 while (*rhs != NUL)
7939 ga_concat(&ga, str2special(&rhs, FALSE));
7940
7941 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
7944}
7945
7946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007947 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 */
7949 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007950f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007954 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
7957/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007958 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 */
7960 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007961f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007965 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
Bram Moolenaar0d660222005-01-07 21:51:51 +00007968static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 int type;
7975{
7976 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007977 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 char_u *pat;
7979 regmatch_T regmatch;
7980 char_u patbuf[NUMBUFLEN];
7981 char_u *save_cpo;
7982 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007983 long nth = 1;
7984 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7987 save_cpo = p_cpo;
7988 p_cpo = (char_u *)"";
7989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990 expr = str = get_tv_string(&argvars[0]);
7991 pat = get_tv_string_buf(&argvars[1], patbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
7993 if (type == 2)
7994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->v_type = VAR_STRING;
7996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
7998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003 start = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 if (start < 0)
8005 start = 0;
8006 if (start > (long)STRLEN(str))
8007 goto theend;
8008 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008010 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 }
8013
8014 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8015 if (regmatch.regprog != NULL)
8016 {
8017 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008018
8019 while (1)
8020 {
8021 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
8022 if (!match || --nth <= 0)
8023 break;
8024 /* Advance to just after the match. */
8025#ifdef FEAT_MBYTE
8026 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
8027#else
8028 str = regmatch.startp[0] + 1;
8029#endif
8030 }
8031
8032 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {
8034 if (type == 2)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 (int)(regmatch.endp[0] - regmatch.startp[0]));
8037 else
8038 {
8039 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 (varnumber_T)(regmatch.startp[0] - str);
8042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047 }
8048 vim_free(regmatch.regprog);
8049 }
8050
8051theend:
8052 p_cpo = save_cpo;
8053}
8054
8055/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008056 * "match()" function
8057 */
8058 static void
8059f_match(argvars, rettv)
8060 typeval *argvars;
8061 typeval *rettv;
8062{
8063 find_some_match(argvars, rettv, 1);
8064}
8065
8066/*
8067 * "matchend()" function
8068 */
8069 static void
8070f_matchend(argvars, rettv)
8071 typeval *argvars;
8072 typeval *rettv;
8073{
8074 find_some_match(argvars, rettv, 0);
8075}
8076
8077/*
8078 * "matchstr()" function
8079 */
8080 static void
8081f_matchstr(argvars, rettv)
8082 typeval *argvars;
8083 typeval *rettv;
8084{
8085 find_some_match(argvars, rettv, 2);
8086}
8087
8088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 * "mode()" function
8090 */
8091/*ARGSUSED*/
8092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008095 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 char_u buf[2];
8098
8099#ifdef FEAT_VISUAL
8100 if (VIsual_active)
8101 {
8102 if (VIsual_select)
8103 buf[0] = VIsual_mode + 's' - 'v';
8104 else
8105 buf[0] = VIsual_mode;
8106 }
8107 else
8108#endif
8109 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
8110 buf[0] = 'r';
8111 else if (State & INSERT)
8112 {
8113 if (State & REPLACE_FLAG)
8114 buf[0] = 'R';
8115 else
8116 buf[0] = 'i';
8117 }
8118 else if (State & CMDLINE)
8119 buf[0] = 'c';
8120 else
8121 buf[0] = 'n';
8122
8123 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 rettv->vval.v_string = vim_strsave(buf);
8125 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126}
8127
8128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008129 * "nextnonblank()" function
8130 */
8131 static void
8132f_nextnonblank(argvars, rettv)
8133 typeval *argvars;
8134 typeval *rettv;
8135{
8136 linenr_T lnum;
8137
8138 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8139 {
8140 if (lnum > curbuf->b_ml.ml_line_count)
8141 {
8142 lnum = 0;
8143 break;
8144 }
8145 if (*skipwhite(ml_get(lnum)) != NUL)
8146 break;
8147 }
8148 rettv->vval.v_number = lnum;
8149}
8150
8151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 * "nr2char()" function
8153 */
8154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 char_u buf[NUMBUFLEN];
8160
8161#ifdef FEAT_MBYTE
8162 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008163 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 else
8165#endif
8166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 buf[1] = NUL;
8169 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008170 rettv->v_type = VAR_STRING;
8171 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008172}
8173
8174/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008175 * "prevnonblank()" function
8176 */
8177 static void
8178f_prevnonblank(argvars, rettv)
8179 typeval *argvars;
8180 typeval *rettv;
8181{
8182 linenr_T lnum;
8183
8184 lnum = get_tv_lnum(argvars);
8185 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8186 lnum = 0;
8187 else
8188 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8189 --lnum;
8190 rettv->vval.v_number = lnum;
8191}
8192
8193#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8194static void make_connection __ARGS((void));
8195static int check_connection __ARGS((void));
8196
8197 static void
8198make_connection()
8199{
8200 if (X_DISPLAY == NULL
8201# ifdef FEAT_GUI
8202 && !gui.in_use
8203# endif
8204 )
8205 {
8206 x_force_connect = TRUE;
8207 setup_term_clip();
8208 x_force_connect = FALSE;
8209 }
8210}
8211
8212 static int
8213check_connection()
8214{
8215 make_connection();
8216 if (X_DISPLAY == NULL)
8217 {
8218 EMSG(_("E240: No connection to Vim server"));
8219 return FAIL;
8220 }
8221 return OK;
8222}
8223#endif
8224
8225#ifdef FEAT_CLIENTSERVER
8226static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
8227
8228 static void
8229remote_common(argvars, rettv, expr)
8230 typeval *argvars;
8231 typeval *rettv;
8232 int expr;
8233{
8234 char_u *server_name;
8235 char_u *keys;
8236 char_u *r = NULL;
8237 char_u buf[NUMBUFLEN];
8238# ifdef WIN32
8239 HWND w;
8240# else
8241 Window w;
8242# endif
8243
8244 if (check_restricted() || check_secure())
8245 return;
8246
8247# ifdef FEAT_X11
8248 if (check_connection() == FAIL)
8249 return;
8250# endif
8251
8252 server_name = get_tv_string(&argvars[0]);
8253 keys = get_tv_string_buf(&argvars[1], buf);
8254# ifdef WIN32
8255 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8256# else
8257 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8258 < 0)
8259# endif
8260 {
8261 if (r != NULL)
8262 EMSG(r); /* sending worked but evaluation failed */
8263 else
8264 EMSG2(_("E241: Unable to send to %s"), server_name);
8265 return;
8266 }
8267
8268 rettv->vval.v_string = r;
8269
8270 if (argvars[2].v_type != VAR_UNKNOWN)
8271 {
8272 var v;
8273 char_u str[30];
8274
8275 sprintf((char *)str, "0x%x", (unsigned int)w);
8276 v.tv.v_type = VAR_STRING;
8277 v.tv.vval.v_string = vim_strsave(str);
8278 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
8279 vim_free(v.tv.vval.v_string);
8280 }
8281}
8282#endif
8283
8284/*
8285 * "remote_expr()" function
8286 */
8287/*ARGSUSED*/
8288 static void
8289f_remote_expr(argvars, rettv)
8290 typeval *argvars;
8291 typeval *rettv;
8292{
8293 rettv->v_type = VAR_STRING;
8294 rettv->vval.v_string = NULL;
8295#ifdef FEAT_CLIENTSERVER
8296 remote_common(argvars, rettv, TRUE);
8297#endif
8298}
8299
8300/*
8301 * "remote_foreground()" function
8302 */
8303/*ARGSUSED*/
8304 static void
8305f_remote_foreground(argvars, rettv)
8306 typeval *argvars;
8307 typeval *rettv;
8308{
8309 rettv->vval.v_number = 0;
8310#ifdef FEAT_CLIENTSERVER
8311# ifdef WIN32
8312 /* On Win32 it's done in this application. */
8313 serverForeground(get_tv_string(&argvars[0]));
8314# else
8315 /* Send a foreground() expression to the server. */
8316 argvars[1].v_type = VAR_STRING;
8317 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8318 argvars[2].v_type = VAR_UNKNOWN;
8319 remote_common(argvars, rettv, TRUE);
8320 vim_free(argvars[1].vval.v_string);
8321# endif
8322#endif
8323}
8324
8325/*ARGSUSED*/
8326 static void
8327f_remote_peek(argvars, rettv)
8328 typeval *argvars;
8329 typeval *rettv;
8330{
8331#ifdef FEAT_CLIENTSERVER
8332 var v;
8333 char_u *s = NULL;
8334# ifdef WIN32
8335 int n = 0;
8336# endif
8337
8338 if (check_restricted() || check_secure())
8339 {
8340 rettv->vval.v_number = -1;
8341 return;
8342 }
8343# ifdef WIN32
8344 sscanf(get_tv_string(&argvars[0]), "%x", &n);
8345 if (n == 0)
8346 rettv->vval.v_number = -1;
8347 else
8348 {
8349 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8350 rettv->vval.v_number = (s != NULL);
8351 }
8352# else
8353 rettv->vval.v_number = 0;
8354 if (check_connection() == FAIL)
8355 return;
8356
8357 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8358 serverStrToWin(get_tv_string(&argvars[0])), &s);
8359# endif
8360
8361 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8362 {
8363 v.tv.v_type = VAR_STRING;
8364 v.tv.vval.v_string = vim_strsave(s);
8365 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
8366 vim_free(v.tv.vval.v_string);
8367 }
8368#else
8369 rettv->vval.v_number = -1;
8370#endif
8371}
8372
8373/*ARGSUSED*/
8374 static void
8375f_remote_read(argvars, rettv)
8376 typeval *argvars;
8377 typeval *rettv;
8378{
8379 char_u *r = NULL;
8380
8381#ifdef FEAT_CLIENTSERVER
8382 if (!check_restricted() && !check_secure())
8383 {
8384# ifdef WIN32
8385 /* The server's HWND is encoded in the 'id' parameter */
8386 int n = 0;
8387
8388 sscanf(get_tv_string(&argvars[0]), "%x", &n);
8389 if (n != 0)
8390 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8391 if (r == NULL)
8392# else
8393 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8394 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
8395# endif
8396 EMSG(_("E277: Unable to read a server reply"));
8397 }
8398#endif
8399 rettv->v_type = VAR_STRING;
8400 rettv->vval.v_string = r;
8401}
8402
8403/*
8404 * "remote_send()" function
8405 */
8406/*ARGSUSED*/
8407 static void
8408f_remote_send(argvars, rettv)
8409 typeval *argvars;
8410 typeval *rettv;
8411{
8412 rettv->v_type = VAR_STRING;
8413 rettv->vval.v_string = NULL;
8414#ifdef FEAT_CLIENTSERVER
8415 remote_common(argvars, rettv, FALSE);
8416#endif
8417}
8418
8419/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008420 * "remove({list}, {idx} [, {end}])" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008421 */
8422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008426{
8427 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 listitem *item, *item2;
8429 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008431 long end;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008433 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008435 EMSG2(_(e_listarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 else if ((l = argvars[0].vval.v_list) != NULL)
8437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008439 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440 if (item == NULL)
8441 EMSGN(_(e_listidx), idx);
8442 else
8443 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008444 if (argvars[2].v_type == VAR_UNKNOWN)
8445 {
8446 /* Remove one item, return its value. */
8447 list_getrem(l, item, item);
8448 *rettv = item->li_tv;
8449 vim_free(item);
8450 }
8451 else
8452 {
8453 /* Remove range of items, return list with values. */
8454 end = get_tv_number(&argvars[2]);
8455 item2 = list_find(l, end);
8456 if (item2 == NULL)
8457 EMSGN(_(e_listidx), end);
8458 else
8459 {
8460 for (li = item; li != item2 && li != NULL; li = li->li_next)
8461 ;
8462 if (li == NULL) /* didn't find "item2" after "item" */
8463 EMSG(_(e_invrange));
8464 else
8465 {
8466 list_getrem(l, item, item2);
8467 l = list_alloc();
8468 if (l != NULL)
8469 {
8470 rettv->v_type = VAR_LIST;
8471 rettv->vval.v_list = l;
8472 l->lv_first = item;
8473 l->lv_last = item2;
8474 l->lv_refcount = 1;
8475 item->li_prev = NULL;
8476 item2->li_next = NULL;
8477 }
8478 }
8479 }
8480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 }
8482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484
8485/*
8486 * "rename({from}, {to})" function
8487 */
8488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008490 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 char_u buf[NUMBUFLEN];
8494
8495 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8499 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500}
8501
8502/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008503 * "repeat()" function
8504 */
8505/*ARGSUSED*/
8506 static void
8507f_repeat(argvars, rettv)
8508 typeval *argvars;
8509 typeval *rettv;
8510{
8511 char_u *p;
8512 int n;
8513 int slen;
8514 int len;
8515 char_u *r;
8516 int i;
8517 listvar *l;
8518
8519 n = get_tv_number(&argvars[1]);
8520 if (argvars[0].v_type == VAR_LIST)
8521 {
8522 l = list_alloc();
8523 if (l != NULL && argvars[0].vval.v_list != NULL)
8524 {
8525 l->lv_refcount = 1;
8526 while (n-- > 0)
8527 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
8528 break;
8529 }
8530 rettv->v_type = VAR_LIST;
8531 rettv->vval.v_list = l;
8532 }
8533 else
8534 {
8535 p = get_tv_string(&argvars[0]);
8536 rettv->v_type = VAR_STRING;
8537 rettv->vval.v_string = NULL;
8538
8539 slen = (int)STRLEN(p);
8540 len = slen * n;
8541 if (len <= 0)
8542 return;
8543
8544 r = alloc(len + 1);
8545 if (r != NULL)
8546 {
8547 for (i = 0; i < n; i++)
8548 mch_memmove(r + i * slen, p, (size_t)slen);
8549 r[len] = NUL;
8550 }
8551
8552 rettv->vval.v_string = r;
8553 }
8554}
8555
8556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 * "resolve()" function
8558 */
8559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 char_u *p;
8565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#ifdef FEAT_SHORTCUT
8568 {
8569 char_u *v = NULL;
8570
8571 v = mch_resolve_shortcut(p);
8572 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577#else
8578# ifdef HAVE_READLINK
8579 {
8580 char_u buf[MAXPATHL + 1];
8581 char_u *cpy;
8582 int len;
8583 char_u *remain = NULL;
8584 char_u *q;
8585 int is_relative_to_current = FALSE;
8586 int has_trailing_pathsep = FALSE;
8587 int limit = 100;
8588
8589 p = vim_strsave(p);
8590
8591 if (p[0] == '.' && (vim_ispathsep(p[1])
8592 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8593 is_relative_to_current = TRUE;
8594
8595 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008596 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 has_trailing_pathsep = TRUE;
8598
8599 q = getnextcomp(p);
8600 if (*q != NUL)
8601 {
8602 /* Separate the first path component in "p", and keep the
8603 * remainder (beginning with the path separator). */
8604 remain = vim_strsave(q - 1);
8605 q[-1] = NUL;
8606 }
8607
8608 for (;;)
8609 {
8610 for (;;)
8611 {
8612 len = readlink((char *)p, (char *)buf, MAXPATHL);
8613 if (len <= 0)
8614 break;
8615 buf[len] = NUL;
8616
8617 if (limit-- == 0)
8618 {
8619 vim_free(p);
8620 vim_free(remain);
8621 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 goto fail;
8624 }
8625
8626 /* Ensure that the result will have a trailing path separator
8627 * if the argument has one. */
8628 if (remain == NULL && has_trailing_pathsep)
8629 add_pathsep(buf);
8630
8631 /* Separate the first path component in the link value and
8632 * concatenate the remainders. */
8633 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8634 if (*q != NUL)
8635 {
8636 if (remain == NULL)
8637 remain = vim_strsave(q - 1);
8638 else
8639 {
8640 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
8641 if (cpy != NULL)
8642 {
8643 STRCAT(cpy, remain);
8644 vim_free(remain);
8645 remain = cpy;
8646 }
8647 }
8648 q[-1] = NUL;
8649 }
8650
8651 q = gettail(p);
8652 if (q > p && *q == NUL)
8653 {
8654 /* Ignore trailing path separator. */
8655 q[-1] = NUL;
8656 q = gettail(p);
8657 }
8658 if (q > p && !mch_isFullName(buf))
8659 {
8660 /* symlink is relative to directory of argument */
8661 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8662 if (cpy != NULL)
8663 {
8664 STRCPY(cpy, p);
8665 STRCPY(gettail(cpy), buf);
8666 vim_free(p);
8667 p = cpy;
8668 }
8669 }
8670 else
8671 {
8672 vim_free(p);
8673 p = vim_strsave(buf);
8674 }
8675 }
8676
8677 if (remain == NULL)
8678 break;
8679
8680 /* Append the first path component of "remain" to "p". */
8681 q = getnextcomp(remain + 1);
8682 len = q - remain - (*q != NUL);
8683 cpy = vim_strnsave(p, STRLEN(p) + len);
8684 if (cpy != NULL)
8685 {
8686 STRNCAT(cpy, remain, len);
8687 vim_free(p);
8688 p = cpy;
8689 }
8690 /* Shorten "remain". */
8691 if (*q != NUL)
8692 STRCPY(remain, q - 1);
8693 else
8694 {
8695 vim_free(remain);
8696 remain = NULL;
8697 }
8698 }
8699
8700 /* If the result is a relative path name, make it explicitly relative to
8701 * the current directory if and only if the argument had this form. */
8702 if (!vim_ispathsep(*p))
8703 {
8704 if (is_relative_to_current
8705 && *p != NUL
8706 && !(p[0] == '.'
8707 && (p[1] == NUL
8708 || vim_ispathsep(p[1])
8709 || (p[1] == '.'
8710 && (p[2] == NUL
8711 || vim_ispathsep(p[2]))))))
8712 {
8713 /* Prepend "./". */
8714 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
8715 if (cpy != NULL)
8716 {
8717 STRCAT(cpy, p);
8718 vim_free(p);
8719 p = cpy;
8720 }
8721 }
8722 else if (!is_relative_to_current)
8723 {
8724 /* Strip leading "./". */
8725 q = p;
8726 while (q[0] == '.' && vim_ispathsep(q[1]))
8727 q += 2;
8728 if (q > p)
8729 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
8730 }
8731 }
8732
8733 /* Ensure that the result will have no trailing path separator
8734 * if the argument had none. But keep "/" or "//". */
8735 if (!has_trailing_pathsep)
8736 {
8737 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008738 if (after_pathsep(p, q))
8739 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 }
8741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 }
8744# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746# endif
8747#endif
8748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
8751#ifdef HAVE_READLINK
8752fail:
8753#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008758 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 */
8760 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008762 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 listvar *l;
8766 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
Bram Moolenaar0d660222005-01-07 21:51:51 +00008768 rettv->vval.v_number = 0;
8769 if (argvars[0].v_type != VAR_LIST)
8770 EMSG2(_(e_listarg), "reverse()");
8771 else if ((l = argvars[0].vval.v_list) != NULL)
8772 {
8773 li = l->lv_last;
8774 l->lv_first = l->lv_last = li;
8775 while (li != NULL)
8776 {
8777 ni = li->li_prev;
8778 list_append(l, li);
8779 li = ni;
8780 }
8781 rettv->vval.v_list = l;
8782 rettv->v_type = VAR_LIST;
8783 ++l->lv_refcount;
8784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008787#define SP_NOMOVE 1 /* don't move cursor */
8788#define SP_REPEAT 2 /* repeat to find outer pair */
8789#define SP_RETCOUNT 4 /* return matchcount */
8790
Bram Moolenaar0d660222005-01-07 21:51:51 +00008791static int get_search_arg __ARGS((typeval *varp, int *flagsp));
8792
8793/*
8794 * Get flags for a search function.
8795 * Possibly sets "p_ws".
8796 * Returns BACKWARD, FORWARD or zero (for an error).
8797 */
8798 static int
8799get_search_arg(varp, flagsp)
8800 typeval *varp;
8801 int *flagsp;
8802{
8803 int dir = FORWARD;
8804 char_u *flags;
8805 char_u nbuf[NUMBUFLEN];
8806 int mask;
8807
8808 if (varp->v_type != VAR_UNKNOWN)
8809 {
8810 flags = get_tv_string_buf(varp, nbuf);
8811 while (*flags != NUL)
8812 {
8813 switch (*flags)
8814 {
8815 case 'b': dir = BACKWARD; break;
8816 case 'w': p_ws = TRUE; break;
8817 case 'W': p_ws = FALSE; break;
8818 default: mask = 0;
8819 if (flagsp != NULL)
8820 switch (*flags)
8821 {
8822 case 'n': mask = SP_NOMOVE; break;
8823 case 'r': mask = SP_REPEAT; break;
8824 case 'm': mask = SP_RETCOUNT; break;
8825 }
8826 if (mask == 0)
8827 {
8828 EMSG2(_(e_invarg2), flags);
8829 dir = 0;
8830 }
8831 else
8832 *flagsp |= mask;
8833 }
8834 if (dir == 0)
8835 break;
8836 ++flags;
8837 }
8838 }
8839 return dir;
8840}
8841
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842/*
8843 * "search()" function
8844 */
8845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008847 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 char_u *pat;
8851 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008852 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 int save_p_ws = p_ws;
8854 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008855 int flags = 0;
8856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008860 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
8861 if (dir == 0)
8862 goto theend;
8863 if ((flags & ~SP_NOMOVE) != 0)
8864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008866 goto theend;
8867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008869 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
8871 SEARCH_KEEP, RE_SEARCH) != FAIL)
8872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 curwin->w_cursor = pos;
8875 /* "/$" will put the cursor after the end of the line, may need to
8876 * correct that here */
8877 check_cursor();
8878 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008879
8880 /* If 'n' flag is used: restore cursor position. */
8881 if (flags & SP_NOMOVE)
8882 curwin->w_cursor = save_cursor;
8883theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 p_ws = save_p_ws;
8885}
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * "searchpair()" function
8889 */
8890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008892 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 char_u *spat, *mpat, *epat;
8896 char_u *skip;
8897 char_u *pat, *pat2, *pat3;
8898 pos_T pos;
8899 pos_T firstpos;
8900 pos_T save_cursor;
8901 pos_T save_pos;
8902 int save_p_ws = p_ws;
8903 char_u *save_cpo;
8904 int dir;
8905 int flags = 0;
8906 char_u nbuf1[NUMBUFLEN];
8907 char_u nbuf2[NUMBUFLEN];
8908 char_u nbuf3[NUMBUFLEN];
8909 int n;
8910 int r;
8911 int nest = 1;
8912 int err;
8913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
8916 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8917 save_cpo = p_cpo;
8918 p_cpo = (char_u *)"";
8919
8920 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 spat = get_tv_string(&argvars[0]);
8922 mpat = get_tv_string_buf(&argvars[1], nbuf1);
8923 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
8925 /* Make two search patterns: start/end (pat2, for in nested pairs) and
8926 * start/middle/end (pat3, for the top pair). */
8927 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
8928 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
8929 if (pat2 == NULL || pat3 == NULL)
8930 goto theend;
8931 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
8932 if (*mpat == NUL)
8933 STRCPY(pat3, pat2);
8934 else
8935 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
8936 spat, epat, mpat);
8937
8938 /* Handle the optional fourth argument: flags */
8939 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008940 if (dir == 0)
8941 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
8943 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008944 if (argvars[3].v_type == VAR_UNKNOWN
8945 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 skip = (char_u *)"";
8947 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
8950 save_cursor = curwin->w_cursor;
8951 pos = curwin->w_cursor;
8952 firstpos.lnum = 0;
8953 pat = pat3;
8954 for (;;)
8955 {
8956 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
8957 SEARCH_KEEP, RE_SEARCH);
8958 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
8959 /* didn't find it or found the first match again: FAIL */
8960 break;
8961
8962 if (firstpos.lnum == 0)
8963 firstpos = pos;
8964
8965 /* If the skip pattern matches, ignore this match. */
8966 if (*skip != NUL)
8967 {
8968 save_pos = curwin->w_cursor;
8969 curwin->w_cursor = pos;
8970 r = eval_to_bool(skip, &err, NULL, FALSE);
8971 curwin->w_cursor = save_pos;
8972 if (err)
8973 {
8974 /* Evaluating {skip} caused an error, break here. */
8975 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 break;
8978 }
8979 if (r)
8980 continue;
8981 }
8982
8983 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8984 {
8985 /* Found end when searching backwards or start when searching
8986 * forward: nested pair. */
8987 ++nest;
8988 pat = pat2; /* nested, don't search for middle */
8989 }
8990 else
8991 {
8992 /* Found end when searching forward or start when searching
8993 * backward: end of (nested) pair; or found middle in outer pair. */
8994 if (--nest == 1)
8995 pat = pat3; /* outer level, search for middle */
8996 }
8997
8998 if (nest == 0)
8999 {
9000 /* Found the match: return matchcount or line number. */
9001 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 curwin->w_cursor = pos;
9006 if (!(flags & SP_REPEAT))
9007 break;
9008 nest = 1; /* search for next unmatched */
9009 }
9010 }
9011
9012 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 curwin->w_cursor = save_cursor;
9015
9016theend:
9017 vim_free(pat2);
9018 vim_free(pat3);
9019 p_ws = save_p_ws;
9020 p_cpo = save_cpo;
9021}
9022
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023/*ARGSUSED*/
9024 static void
9025f_server2client(argvars, rettv)
9026 typeval *argvars;
9027 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009029#ifdef FEAT_CLIENTSERVER
9030 char_u buf[NUMBUFLEN];
9031 char_u *server = get_tv_string(&argvars[0]);
9032 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
Bram Moolenaar0d660222005-01-07 21:51:51 +00009034 rettv->vval.v_number = -1;
9035 if (check_restricted() || check_secure())
9036 return;
9037# ifdef FEAT_X11
9038 if (check_connection() == FAIL)
9039 return;
9040# endif
9041
9042 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009044 EMSG(_("E258: Unable to send to client"));
9045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009047 rettv->vval.v_number = 0;
9048#else
9049 rettv->vval.v_number = -1;
9050#endif
9051}
9052
9053/*ARGSUSED*/
9054 static void
9055f_serverlist(argvars, rettv)
9056 typeval *argvars;
9057 typeval *rettv;
9058{
9059 char_u *r = NULL;
9060
9061#ifdef FEAT_CLIENTSERVER
9062# ifdef WIN32
9063 r = serverGetVimNames();
9064# else
9065 make_connection();
9066 if (X_DISPLAY != NULL)
9067 r = serverGetVimNames(X_DISPLAY);
9068# endif
9069#endif
9070 rettv->v_type = VAR_STRING;
9071 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
9075 * "setbufvar()" function
9076 */
9077/*ARGSUSED*/
9078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009080 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 buf_T *buf;
9084#ifdef FEAT_AUTOCMD
9085 aco_save_T aco;
9086#else
9087 buf_T *save_curbuf;
9088#endif
9089 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009090 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 char_u nbuf[NUMBUFLEN];
9092
9093 if (check_restricted() || check_secure())
9094 return;
9095 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 buf = get_buf_tv(&argvars[0]);
9097 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 varp = &argvars[2];
9099
9100 if (buf != NULL && varname != NULL && varp != NULL)
9101 {
9102 /* set curbuf to be our buf, temporarily */
9103#ifdef FEAT_AUTOCMD
9104 aucmd_prepbuf(&aco, buf);
9105#else
9106 save_curbuf = curbuf;
9107 curbuf = buf;
9108#endif
9109
9110 if (*varname == '&')
9111 {
9112 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 set_option_value(varname, get_tv_number(varp),
9114 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
9116 else
9117 {
9118 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9119 if (bufvarname != NULL)
9120 {
9121 STRCPY(bufvarname, "b:");
9122 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009123 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 vim_free(bufvarname);
9125 }
9126 }
9127
9128 /* reset notion of buffer */
9129#ifdef FEAT_AUTOCMD
9130 aucmd_restbuf(&aco);
9131#else
9132 curbuf = save_curbuf;
9133#endif
9134 }
9135 --emsg_off;
9136}
9137
9138/*
9139 * "setcmdpos()" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009143 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = set_cmdline_pos(
9147 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148}
9149
9150/*
9151 * "setline()" function
9152 */
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009155 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 linenr_T lnum;
9159 char_u *line;
9160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 lnum = get_tv_lnum(argvars);
9162 line = get_tv_string(&argvars[1]);
9163 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165 if (lnum >= 1
9166 && lnum <= curbuf->b_ml.ml_line_count
9167 && u_savesub(lnum) == OK
9168 && ml_replace(lnum, line, TRUE) == OK)
9169 {
9170 changed_bytes(lnum, 0);
9171 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
9174}
9175
9176/*
9177 * "setreg()" function
9178 */
9179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009181 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
9184 int regname;
9185 char_u *strregname;
9186 char_u *stropt;
9187 int append;
9188 char_u yank_type;
9189 long block_len;
9190
9191 block_len = -1;
9192 yank_type = MAUTO;
9193 append = FALSE;
9194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 strregname = get_tv_string(argvars);
9196 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
9198 regname = (strregname == NULL ? '"' : *strregname);
9199 if (regname == 0 || regname == '@')
9200 regname = '"';
9201 else if (regname == '=')
9202 return;
9203
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 switch (*stropt)
9208 {
9209 case 'a': case 'A': /* append */
9210 append = TRUE;
9211 break;
9212 case 'v': case 'c': /* character-wise selection */
9213 yank_type = MCHAR;
9214 break;
9215 case 'V': case 'l': /* line-wise selection */
9216 yank_type = MLINE;
9217 break;
9218#ifdef FEAT_VISUAL
9219 case 'b': case Ctrl_V: /* block-wise selection */
9220 yank_type = MBLOCK;
9221 if (VIM_ISDIGIT(stropt[1]))
9222 {
9223 ++stropt;
9224 block_len = getdigits(&stropt) - 1;
9225 --stropt;
9226 }
9227 break;
9228#endif
9229 }
9230 }
9231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235}
9236
9237
9238/*
9239 * "setwinvar(expr)" function
9240 */
9241/*ARGSUSED*/
9242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009244 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009245 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
9247 win_T *win;
9248#ifdef FEAT_WINDOWS
9249 win_T *save_curwin;
9250#endif
9251 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009252 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 char_u nbuf[NUMBUFLEN];
9254
9255 if (check_restricted() || check_secure())
9256 return;
9257 ++emsg_off;
9258 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 varp = &argvars[2];
9261
9262 if (win != NULL && varname != NULL && varp != NULL)
9263 {
9264#ifdef FEAT_WINDOWS
9265 /* set curwin to be our win, temporarily */
9266 save_curwin = curwin;
9267 curwin = win;
9268 curbuf = curwin->w_buffer;
9269#endif
9270
9271 if (*varname == '&')
9272 {
9273 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 set_option_value(varname, get_tv_number(varp),
9275 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 }
9277 else
9278 {
9279 winvarname = alloc((unsigned)STRLEN(varname) + 3);
9280 if (winvarname != NULL)
9281 {
9282 STRCPY(winvarname, "w:");
9283 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009284 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 vim_free(winvarname);
9286 }
9287 }
9288
9289#ifdef FEAT_WINDOWS
9290 /* Restore current window, if it's still valid (autocomands can make
9291 * it invalid). */
9292 if (win_valid(save_curwin))
9293 {
9294 curwin = save_curwin;
9295 curbuf = curwin->w_buffer;
9296 }
9297#endif
9298 }
9299 --emsg_off;
9300}
9301
9302/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009303 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 */
9305 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009306f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009307 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312 p = get_tv_string(&argvars[0]);
9313 rettv->vval.v_string = vim_strsave(p);
9314 simplify_filename(rettv->vval.v_string); /* simplify in place */
9315 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316}
9317
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318static int
9319#ifdef __BORLANDC__
9320 _RTLENTRYF
9321#endif
9322 item_compare __ARGS((const void *s1, const void *s2));
9323static int
9324#ifdef __BORLANDC__
9325 _RTLENTRYF
9326#endif
9327 item_compare2 __ARGS((const void *s1, const void *s2));
9328
9329static int item_compare_ic;
9330static char_u *item_compare_func;
9331#define ITEM_COMPARE_FAIL 999
9332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 */
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 static int
9337#ifdef __BORLANDC__
9338_RTLENTRYF
9339#endif
9340item_compare(s1, s2)
9341 const void *s1;
9342 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009344 char_u *p1, *p2;
9345 char_u *tofree1, *tofree2;
9346 int res;
9347 char_u numbuf1[NUMBUFLEN];
9348 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
Bram Moolenaar0d660222005-01-07 21:51:51 +00009350 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
9351 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
9352 if (item_compare_ic)
9353 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009355 res = STRCMP(p1, p2);
9356 vim_free(tofree1);
9357 vim_free(tofree2);
9358 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359}
9360
9361 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362#ifdef __BORLANDC__
9363_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00009365item_compare2(s1, s2)
9366 const void *s1;
9367 const void *s2;
9368{
9369 int res;
9370 typeval rettv;
9371 typeval argv[2];
9372 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
Bram Moolenaar0d660222005-01-07 21:51:51 +00009374 /* copy the values (is this really needed?) */
9375 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
9376 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
9377
9378 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
9379 res = call_func(item_compare_func, STRLEN(item_compare_func),
9380 &rettv, 2, argv, 0L, 0L, &dummy, TRUE);
9381 clear_tv(&argv[0]);
9382 clear_tv(&argv[1]);
9383
9384 if (res == FAIL)
9385 res = ITEM_COMPARE_FAIL;
9386 else
9387 res = get_tv_number(&rettv);
9388 clear_tv(&rettv);
9389 return res;
9390}
9391
9392/*
9393 * "sort({list})" function
9394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009396f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009397 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400 listvar *l;
9401 listitem *li;
9402 listitem **ptrs;
9403 long len;
9404 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405
Bram Moolenaar0d660222005-01-07 21:51:51 +00009406 rettv->vval.v_number = 0;
9407 if (argvars[0].v_type != VAR_LIST)
9408 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 else
9410 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009411 l = argvars[0].vval.v_list;
9412 if (l == NULL)
9413 return;
9414 rettv->vval.v_list = l;
9415 rettv->v_type = VAR_LIST;
9416 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417
Bram Moolenaar0d660222005-01-07 21:51:51 +00009418 len = list_len(l);
9419 if (len <= 1)
9420 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 item_compare_ic = FALSE;
9423 item_compare_func = NULL;
9424 if (argvars[1].v_type != VAR_UNKNOWN)
9425 {
9426 if (argvars[1].v_type == VAR_FUNC)
9427 item_compare_func = argvars[0].vval.v_string;
9428 else
9429 {
9430 i = get_tv_number(&argvars[1]);
9431 if (i == 1)
9432 item_compare_ic = TRUE;
9433 else
9434 item_compare_func = get_tv_string(&argvars[1]);
9435 }
9436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438 /* Make an array with each entry pointing to an item in the List. */
9439 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
9440 if (ptrs == NULL)
9441 return;
9442 i = 0;
9443 for (li = l->lv_first; li != NULL; li = li->li_next)
9444 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 /* test the compare function */
9447 if (item_compare_func != NULL
9448 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
9449 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009450 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009452 {
9453 /* Sort the array with item pointers. */
9454 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
9455 item_compare_func == NULL ? item_compare : item_compare2);
9456
9457 /* Clear the List and append the items in the sorted order. */
9458 l->lv_first = l->lv_last = NULL;
9459 for (i = 0; i < len; ++i)
9460 list_append(l, ptrs[i]);
9461 }
9462
9463 vim_free(ptrs);
9464 }
9465}
9466
9467 static void
9468f_str2list(argvars, rettv)
9469 typeval *argvars;
9470 typeval *rettv;
9471{
9472 char_u *str;
9473 char_u *end;
9474 char_u *pat;
9475 regmatch_T regmatch;
9476 char_u patbuf[NUMBUFLEN];
9477 char_u *save_cpo;
9478 int match;
9479 listitem *ni;
9480 listvar *l;
9481 colnr_T col = 0;
9482
9483 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9484 save_cpo = p_cpo;
9485 p_cpo = (char_u *)"";
9486
9487 str = get_tv_string(&argvars[0]);
9488 if (argvars[1].v_type == VAR_UNKNOWN)
9489 pat = (char_u *)"[\\x01- ]\\+";
9490 else
9491 pat = get_tv_string_buf(&argvars[1], patbuf);
9492
9493 l = list_alloc();
9494 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009496 rettv->v_type = VAR_LIST;
9497 rettv->vval.v_list = l;
9498 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
Bram Moolenaar0d660222005-01-07 21:51:51 +00009500 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9501 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009503 regmatch.rm_ic = FALSE;
9504 while (*str != NUL)
9505 {
9506 match = vim_regexec_nl(&regmatch, str, col);
9507 if (match)
9508 end = regmatch.startp[0];
9509 else
9510 end = str + STRLEN(str);
9511 if (end > str)
9512 {
9513 ni = listitem_alloc();
9514 if (ni == NULL)
9515 break;
9516 ni->li_tv.v_type = VAR_STRING;
9517 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
9518 list_append(l, ni);
9519 }
9520 if (!match)
9521 break;
9522 /* Advance to just after the match. */
9523 if (regmatch.endp[0] > str)
9524 col = 0;
9525 else
9526 {
9527 /* Don't get stuck at the same match. */
9528#ifdef FEAT_MBYTE
9529 col = mb_ptr2len_check(regmatch.endp[0]);
9530#else
9531 col = 1;
9532#endif
9533 }
9534 str = regmatch.endp[0];
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
Bram Moolenaar0d660222005-01-07 21:51:51 +00009537 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539
Bram Moolenaar0d660222005-01-07 21:51:51 +00009540 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541}
9542
9543#ifdef HAVE_STRFTIME
9544/*
9545 * "strftime({format}[, {time}])" function
9546 */
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 char_u result_buf[256];
9553 struct tm *curtime;
9554 time_t seconds;
9555 char_u *p;
9556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 seconds = time(NULL);
9562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 curtime = localtime(&seconds);
9565 /* MSVC returns NULL for an invalid value of seconds. */
9566 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 else
9569 {
9570# ifdef FEAT_MBYTE
9571 vimconv_T conv;
9572 char_u *enc;
9573
9574 conv.vc_type = CONV_NONE;
9575 enc = enc_locale();
9576 convert_setup(&conv, p_enc, enc);
9577 if (conv.vc_type != CONV_NONE)
9578 p = string_convert(&conv, p, NULL);
9579# endif
9580 if (p != NULL)
9581 (void)strftime((char *)result_buf, sizeof(result_buf),
9582 (char *)p, curtime);
9583 else
9584 result_buf[0] = NUL;
9585
9586# ifdef FEAT_MBYTE
9587 if (conv.vc_type != CONV_NONE)
9588 vim_free(p);
9589 convert_setup(&conv, enc, p_enc);
9590 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 else
9593# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
9596# ifdef FEAT_MBYTE
9597 /* Release conversion descriptors */
9598 convert_setup(&conv, NULL, NULL);
9599 vim_free(enc);
9600# endif
9601 }
9602}
9603#endif
9604
9605/*
9606 * "stridx()" function
9607 */
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009610 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 char_u buf[NUMBUFLEN];
9614 char_u *needle;
9615 char_u *haystack;
9616 char_u *pos;
9617
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 needle = get_tv_string(&argvars[1]);
9619 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9621
9622 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
9627
9628/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009629 * "string()" function
9630 */
9631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009633 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009635{
9636 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009637 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009640 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009641 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643}
9644
9645/*
9646 * "strlen()" function
9647 */
9648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009650 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_number = (varnumber_T)(STRLEN(
9654 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655}
9656
9657/*
9658 * "strpart()" function
9659 */
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009662 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 char_u *p;
9666 int n;
9667 int len;
9668 int slen;
9669
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 slen = (int)STRLEN(p);
9672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009674 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 else
9677 len = slen - n; /* default len: all bytes that are available. */
9678
9679 /*
9680 * Only return the overlap between the specified part and the actual
9681 * string.
9682 */
9683 if (n < 0)
9684 {
9685 len += n;
9686 n = 0;
9687 }
9688 else if (n > slen)
9689 n = slen;
9690 if (len < 0)
9691 len = 0;
9692 else if (n + len > slen)
9693 len = slen - n;
9694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->v_type = VAR_STRING;
9696 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697}
9698
9699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009700 * "strridx()" function
9701 */
9702 static void
9703f_strridx(argvars, rettv)
9704 typeval *argvars;
9705 typeval *rettv;
9706{
9707 char_u buf[NUMBUFLEN];
9708 char_u *needle;
9709 char_u *haystack;
9710 char_u *rest;
9711 char_u *lastmatch = NULL;
9712
9713 needle = get_tv_string(&argvars[1]);
9714 haystack = get_tv_string_buf(&argvars[0], buf);
9715 if (*needle == NUL)
9716 /* Empty string matches past the end. */
9717 lastmatch = haystack + STRLEN(haystack);
9718 else
9719 for (rest = haystack; *rest != '\0'; ++rest)
9720 {
9721 rest = (char_u *)strstr((char *)rest, (char *)needle);
9722 if (rest == NULL)
9723 break;
9724 lastmatch = rest;
9725 }
9726
9727 if (lastmatch == NULL)
9728 rettv->vval.v_number = -1;
9729 else
9730 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9731}
9732
9733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 * "strtrans()" function
9735 */
9736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009738 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->v_type = VAR_STRING;
9742 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
9745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009746 * "submatch()" function
9747 */
9748 static void
9749f_submatch(argvars, rettv)
9750 typeval *argvars;
9751 typeval *rettv;
9752{
9753 rettv->v_type = VAR_STRING;
9754 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
9755}
9756
9757/*
9758 * "substitute()" function
9759 */
9760 static void
9761f_substitute(argvars, rettv)
9762 typeval *argvars;
9763 typeval *rettv;
9764{
9765 char_u patbuf[NUMBUFLEN];
9766 char_u subbuf[NUMBUFLEN];
9767 char_u flagsbuf[NUMBUFLEN];
9768
9769 rettv->v_type = VAR_STRING;
9770 rettv->vval.v_string = do_string_sub(
9771 get_tv_string(&argvars[0]),
9772 get_tv_string_buf(&argvars[1], patbuf),
9773 get_tv_string_buf(&argvars[2], subbuf),
9774 get_tv_string_buf(&argvars[3], flagsbuf));
9775}
9776
9777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 * "synID(line, col, trans)" function
9779 */
9780/*ARGSUSED*/
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009783 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786 int id = 0;
9787#ifdef FEAT_SYN_HL
9788 long line;
9789 long col;
9790 int trans;
9791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 line = get_tv_lnum(argvars);
9793 col = get_tv_number(&argvars[1]) - 1;
9794 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795
9796 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
9797 && col >= 0 && col < (long)STRLEN(ml_get(line)))
9798 id = syn_get_id(line, col, trans);
9799#endif
9800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802}
9803
9804/*
9805 * "synIDattr(id, what [, mode])" function
9806 */
9807/*ARGSUSED*/
9808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009810 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813 char_u *p = NULL;
9814#ifdef FEAT_SYN_HL
9815 int id;
9816 char_u *what;
9817 char_u *mode;
9818 char_u modebuf[NUMBUFLEN];
9819 int modec;
9820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 id = get_tv_number(&argvars[0]);
9822 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 modec = TOLOWER_ASC(mode[0]);
9827 if (modec != 't' && modec != 'c'
9828#ifdef FEAT_GUI
9829 && modec != 'g'
9830#endif
9831 )
9832 modec = 0; /* replace invalid with current */
9833 }
9834 else
9835 {
9836#ifdef FEAT_GUI
9837 if (gui.in_use)
9838 modec = 'g';
9839 else
9840#endif
9841 if (t_colors > 1)
9842 modec = 'c';
9843 else
9844 modec = 't';
9845 }
9846
9847
9848 switch (TOLOWER_ASC(what[0]))
9849 {
9850 case 'b':
9851 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
9852 p = highlight_color(id, what, modec);
9853 else /* bold */
9854 p = highlight_has_attr(id, HL_BOLD, modec);
9855 break;
9856
9857 case 'f': /* fg[#] */
9858 p = highlight_color(id, what, modec);
9859 break;
9860
9861 case 'i':
9862 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
9863 p = highlight_has_attr(id, HL_INVERSE, modec);
9864 else /* italic */
9865 p = highlight_has_attr(id, HL_ITALIC, modec);
9866 break;
9867
9868 case 'n': /* name */
9869 p = get_highlight_name(NULL, id - 1);
9870 break;
9871
9872 case 'r': /* reverse */
9873 p = highlight_has_attr(id, HL_INVERSE, modec);
9874 break;
9875
9876 case 's': /* standout */
9877 p = highlight_has_attr(id, HL_STANDOUT, modec);
9878 break;
9879
9880 case 'u': /* underline */
9881 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9882 break;
9883 }
9884
9885 if (p != NULL)
9886 p = vim_strsave(p);
9887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->v_type = VAR_STRING;
9889 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892/*
9893 * "synIDtrans(id)" function
9894 */
9895/*ARGSUSED*/
9896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009898 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
9901 int id;
9902
9903#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905
9906 if (id > 0)
9907 id = syn_get_final_id(id);
9908 else
9909#endif
9910 id = 0;
9911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913}
9914
9915/*
9916 * "system()" function
9917 */
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009920 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009923 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009925 char_u *infile = NULL;
9926 char_u buf[NUMBUFLEN];
9927 int err = FALSE;
9928 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009930 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009931 {
9932 /*
9933 * Write the string to a temp file, to be used for input of the shell
9934 * command.
9935 */
9936 if ((infile = vim_tempname('i')) == NULL)
9937 {
9938 EMSG(_(e_notmp));
9939 return;
9940 }
9941
9942 fd = mch_fopen((char *)infile, WRITEBIN);
9943 if (fd == NULL)
9944 {
9945 EMSG2(_(e_notopen), infile);
9946 goto done;
9947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009949 if (fwrite(p, STRLEN(p), 1, fd) != 1)
9950 err = TRUE;
9951 if (fclose(fd) != 0)
9952 err = TRUE;
9953 if (err)
9954 {
9955 EMSG(_("E677: Error writing temp file"));
9956 goto done;
9957 }
9958 }
9959
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009961
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962#ifdef USE_CR
9963 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009964 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 {
9966 char_u *s;
9967
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009968 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 {
9970 if (*s == CAR)
9971 *s = NL;
9972 }
9973 }
9974#else
9975# ifdef USE_CRNL
9976 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009977 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 {
9979 char_u *s, *d;
9980
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009981 d = res;
9982 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 {
9984 if (s[0] == CAR && s[1] == NL)
9985 ++s;
9986 *d++ = *s;
9987 }
9988 *d = NUL;
9989 }
9990# endif
9991#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009992
9993done:
9994 if (infile != NULL)
9995 {
9996 mch_remove(infile);
9997 vim_free(infile);
9998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 rettv->v_type = VAR_STRING;
10000 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
10002
10003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 * "tempname()" function
10005 */
10006/*ARGSUSED*/
10007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010009 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011{
10012 static int x = 'A';
10013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 rettv->v_type = VAR_STRING;
10015 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
10017 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
10018 * names. Skip 'I' and 'O', they are used for shell redirection. */
10019 do
10020 {
10021 if (x == 'Z')
10022 x = '0';
10023 else if (x == '9')
10024 x = 'A';
10025 else
10026 {
10027#ifdef EBCDIC
10028 if (x == 'I')
10029 x = 'J';
10030 else if (x == 'R')
10031 x = 'S';
10032 else
10033#endif
10034 ++x;
10035 }
10036 } while (x == 'I' || x == 'O');
10037}
10038
10039/*
10040 * "tolower(string)" function
10041 */
10042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010044 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046{
10047 char_u *p;
10048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 p = vim_strsave(get_tv_string(&argvars[0]));
10050 rettv->v_type = VAR_STRING;
10051 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
10053 if (p != NULL)
10054 while (*p != NUL)
10055 {
10056#ifdef FEAT_MBYTE
10057 int l;
10058
10059 if (enc_utf8)
10060 {
10061 int c, lc;
10062
10063 c = utf_ptr2char(p);
10064 lc = utf_tolower(c);
10065 l = utf_ptr2len_check(p);
10066 /* TODO: reallocate string when byte count changes. */
10067 if (utf_char2len(lc) == l)
10068 utf_char2bytes(lc, p);
10069 p += l;
10070 }
10071 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10072 p += l; /* skip multi-byte character */
10073 else
10074#endif
10075 {
10076 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
10077 ++p;
10078 }
10079 }
10080}
10081
10082/*
10083 * "toupper(string)" function
10084 */
10085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010087 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089{
10090 char_u *p;
10091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010092 p = vim_strsave(get_tv_string(&argvars[0]));
10093 rettv->v_type = VAR_STRING;
10094 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
10096 if (p != NULL)
10097 while (*p != NUL)
10098 {
10099#ifdef FEAT_MBYTE
10100 int l;
10101
10102 if (enc_utf8)
10103 {
10104 int c, uc;
10105
10106 c = utf_ptr2char(p);
10107 uc = utf_toupper(c);
10108 l = utf_ptr2len_check(p);
10109 /* TODO: reallocate string when byte count changes. */
10110 if (utf_char2len(uc) == l)
10111 utf_char2bytes(uc, p);
10112 p += l;
10113 }
10114 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10115 p += l; /* skip multi-byte character */
10116 else
10117#endif
10118 {
10119 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
10120 p++;
10121 }
10122 }
10123}
10124
10125/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000010126 * "tr(string, fromstr, tostr)" function
10127 */
10128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010130 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010132{
10133 char_u *instr;
10134 char_u *fromstr;
10135 char_u *tostr;
10136 char_u *p;
10137#ifdef FEAT_MBYTE
10138 int inlen;
10139 int fromlen;
10140 int tolen;
10141 int idx;
10142 char_u *cpstr;
10143 int cplen;
10144 int first = TRUE;
10145#endif
10146 char_u buf[NUMBUFLEN];
10147 char_u buf2[NUMBUFLEN];
10148 garray_T ga;
10149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 instr = get_tv_string(&argvars[0]);
10151 fromstr = get_tv_string_buf(&argvars[1], buf);
10152 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010153
10154 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155 rettv->v_type = VAR_STRING;
10156 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010157 ga_init2(&ga, (int)sizeof(char), 80);
10158
10159#ifdef FEAT_MBYTE
10160 if (!has_mbyte)
10161#endif
10162 /* not multi-byte: fromstr and tostr must be the same length */
10163 if (STRLEN(fromstr) != STRLEN(tostr))
10164 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010165#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000010166error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010167#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000010168 EMSG2(_(e_invarg2), fromstr);
10169 ga_clear(&ga);
10170 return;
10171 }
10172
10173 /* fromstr and tostr have to contain the same number of chars */
10174 while (*instr != NUL)
10175 {
10176#ifdef FEAT_MBYTE
10177 if (has_mbyte)
10178 {
10179 inlen = mb_ptr2len_check(instr);
10180 cpstr = instr;
10181 cplen = inlen;
10182 idx = 0;
10183 for (p = fromstr; *p != NUL; p += fromlen)
10184 {
10185 fromlen = mb_ptr2len_check(p);
10186 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
10187 {
10188 for (p = tostr; *p != NUL; p += tolen)
10189 {
10190 tolen = mb_ptr2len_check(p);
10191 if (idx-- == 0)
10192 {
10193 cplen = tolen;
10194 cpstr = p;
10195 break;
10196 }
10197 }
10198 if (*p == NUL) /* tostr is shorter than fromstr */
10199 goto error;
10200 break;
10201 }
10202 ++idx;
10203 }
10204
10205 if (first && cpstr == instr)
10206 {
10207 /* Check that fromstr and tostr have the same number of
10208 * (multi-byte) characters. Done only once when a character
10209 * of instr doesn't appear in fromstr. */
10210 first = FALSE;
10211 for (p = tostr; *p != NUL; p += tolen)
10212 {
10213 tolen = mb_ptr2len_check(p);
10214 --idx;
10215 }
10216 if (idx != 0)
10217 goto error;
10218 }
10219
10220 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000010221 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010222 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010223
10224 instr += inlen;
10225 }
10226 else
10227#endif
10228 {
10229 /* When not using multi-byte chars we can do it faster. */
10230 p = vim_strchr(fromstr, *instr);
10231 if (p != NULL)
10232 ga_append(&ga, tostr[p - fromstr]);
10233 else
10234 ga_append(&ga, *instr);
10235 ++instr;
10236 }
10237 }
10238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010240}
10241
10242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 * "type(expr)" function
10244 */
10245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010247 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010250 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253 rettv->vval.v_number = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254}
10255
10256/*
10257 * "virtcol(string)" function
10258 */
10259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010261 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010262 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
10264 colnr_T vcol = 0;
10265 pos_T *fp;
10266
10267 fp = var2fpos(&argvars[0], FALSE);
10268 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
10269 {
10270 getvvcol(curwin, fp, NULL, NULL, &vcol);
10271 ++vcol;
10272 }
10273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275}
10276
10277/*
10278 * "visualmode()" function
10279 */
10280/*ARGSUSED*/
10281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010283 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286#ifdef FEAT_VISUAL
10287 char_u str[2];
10288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 str[0] = curbuf->b_visual_mode_eval;
10291 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293
10294 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010295 if ((argvars[0].v_type == VAR_NUMBER
10296 && argvars[0].vval.v_number != 0)
10297 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 curbuf->b_visual_mode_eval = NUL;
10300#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302#endif
10303}
10304
10305/*
10306 * "winbufnr(nr)" function
10307 */
10308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010310 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312{
10313 win_T *wp;
10314
10315 wp = find_win_by_nr(&argvars[0]);
10316 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010319 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320}
10321
10322/*
10323 * "wincol()" function
10324 */
10325/*ARGSUSED*/
10326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010327f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010328 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330{
10331 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
10334
10335/*
10336 * "winheight(nr)" function
10337 */
10338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
10343 win_T *wp;
10344
10345 wp = find_win_by_nr(&argvars[0]);
10346 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350}
10351
10352/*
10353 * "winline()" function
10354 */
10355/*ARGSUSED*/
10356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010358 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
10361 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363}
10364
10365/*
10366 * "winnr()" function
10367 */
10368/* ARGSUSED */
10369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373{
10374 int nr = 1;
10375#ifdef FEAT_WINDOWS
10376 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010377 win_T *twin = curwin;
10378 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010380 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010383 if (STRCMP(arg, "$") == 0)
10384 twin = lastwin;
10385 else if (STRCMP(arg, "#") == 0)
10386 {
10387 twin = prevwin;
10388 if (prevwin == NULL)
10389 nr = 0;
10390 }
10391 else
10392 {
10393 EMSG2(_(e_invexpr2), arg);
10394 nr = 0;
10395 }
10396 }
10397
10398 if (nr > 0)
10399 for (wp = firstwin; wp != twin; wp = wp->w_next)
10400 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010402 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403}
10404
10405/*
10406 * "winrestcmd()" function
10407 */
10408/* ARGSUSED */
10409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010411 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413{
10414#ifdef FEAT_WINDOWS
10415 win_T *wp;
10416 int winnr = 1;
10417 garray_T ga;
10418 char_u buf[50];
10419
10420 ga_init2(&ga, (int)sizeof(char), 70);
10421 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10422 {
10423 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
10424 ga_concat(&ga, buf);
10425# ifdef FEAT_VERTSPLIT
10426 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
10427 ga_concat(&ga, buf);
10428# endif
10429 ++winnr;
10430 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000010431 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438}
10439
10440/*
10441 * "winwidth(nr)" function
10442 */
10443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010445 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
10448 win_T *wp;
10449
10450 wp = find_win_by_nr(&argvars[0]);
10451 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 else
10454#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458#endif
10459}
10460
10461 static win_T *
10462find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010463 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
10465#ifdef FEAT_WINDOWS
10466 win_T *wp;
10467#endif
10468 int nr;
10469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471
10472#ifdef FEAT_WINDOWS
10473 if (nr == 0)
10474 return curwin;
10475
10476 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10477 if (--nr <= 0)
10478 break;
10479 return wp;
10480#else
10481 if (nr == 0 || nr == 1)
10482 return curwin;
10483 return NULL;
10484#endif
10485}
10486
10487/*
10488 * Translate a String variable into a position.
10489 */
10490 static pos_T *
10491var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010492 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 int lnum; /* TRUE when $ is last line */
10494{
10495 char_u *name;
10496 static pos_T pos;
10497 pos_T *pp;
10498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 if (name[0] == '.') /* cursor */
10501 return &curwin->w_cursor;
10502 if (name[0] == '\'') /* mark */
10503 {
10504 pp = getmark(name[1], FALSE);
10505 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
10506 return NULL;
10507 return pp;
10508 }
10509 if (name[0] == '$') /* last column or line */
10510 {
10511 if (lnum)
10512 {
10513 pos.lnum = curbuf->b_ml.ml_line_count;
10514 pos.col = 0;
10515 }
10516 else
10517 {
10518 pos.lnum = curwin->w_cursor.lnum;
10519 pos.col = (colnr_T)STRLEN(ml_get_curline());
10520 }
10521 return &pos;
10522 }
10523 return NULL;
10524}
10525
10526/*
10527 * Get the length of an environment variable name.
10528 * Advance "arg" to the first character after the name.
10529 * Return 0 for error.
10530 */
10531 static int
10532get_env_len(arg)
10533 char_u **arg;
10534{
10535 char_u *p;
10536 int len;
10537
10538 for (p = *arg; vim_isIDc(*p); ++p)
10539 ;
10540 if (p == *arg) /* no name found */
10541 return 0;
10542
10543 len = (int)(p - *arg);
10544 *arg = p;
10545 return len;
10546}
10547
10548/*
10549 * Get the length of the name of a function or internal variable.
10550 * "arg" is advanced to the first non-white character after the name.
10551 * Return 0 if something is wrong.
10552 */
10553 static int
10554get_id_len(arg)
10555 char_u **arg;
10556{
10557 char_u *p;
10558 int len;
10559
10560 /* Find the end of the name. */
10561 for (p = *arg; eval_isnamec(*p); ++p)
10562 ;
10563 if (p == *arg) /* no name found */
10564 return 0;
10565
10566 len = (int)(p - *arg);
10567 *arg = skipwhite(p);
10568
10569 return len;
10570}
10571
10572/*
10573 * Get the length of the name of a function.
10574 * "arg" is advanced to the first non-white character after the name.
10575 * Return 0 if something is wrong.
10576 * If the name contains 'magic' {}'s, expand them and return the
10577 * expanded name in an allocated string via 'alias' - caller must free.
10578 */
10579 static int
10580get_func_len(arg, alias, evaluate)
10581 char_u **arg;
10582 char_u **alias;
10583 int evaluate;
10584{
10585 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 char_u *p;
10587 char_u *expr_start;
10588 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589
10590 *alias = NULL; /* default to no alias */
10591
10592 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
10593 && (*arg)[2] == (int)KE_SNR)
10594 {
10595 /* hard coded <SNR>, already translated */
10596 *arg += 3;
10597 return get_id_len(arg) + 3;
10598 }
10599 len = eval_fname_script(*arg);
10600 if (len > 0)
10601 {
10602 /* literal "<SID>", "s:" or "<SNR>" */
10603 *arg += len;
10604 }
10605
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 if (expr_start != NULL)
10611 {
10612 char_u *temp_string;
10613
10614 if (!evaluate)
10615 {
10616 len += (int)(p - *arg);
10617 *arg = skipwhite(p);
10618 return len;
10619 }
10620
10621 /*
10622 * Include any <SID> etc in the expanded string:
10623 * Thus the -len here.
10624 */
10625 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
10626 if (temp_string == NULL)
10627 return 0;
10628 *alias = temp_string;
10629 *arg = skipwhite(p);
10630 return (int)STRLEN(temp_string);
10631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632
10633 len += get_id_len(arg);
10634 if (len == 0)
10635 EMSG2(_(e_invexpr2), *arg);
10636
10637 return len;
10638}
10639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640/*
10641 * Find the end of a variable or function name, taking care of magic braces.
10642 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
10643 * start and end of the first magic braces item.
10644 * Return a pointer to just after the name. Equal to "arg" if there is no
10645 * valid name.
10646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 char_u *arg;
10650 char_u **expr_start;
10651 char_u **expr_end;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652 int incl_br; /* Include [] indexes */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 int mb_nest = 0;
10655 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 char_u *p;
10657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 *expr_start = NULL;
10661 *expr_end = NULL;
10662 }
10663
10664 for (p = arg; *p != NUL
10665 && (eval_isnamec(*p)
10666 || (*p == '[' && incl_br)
10667 || mb_nest != 0
10668 || br_nest != 0); ++p)
10669 {
10670 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010672 if (*p == '[')
10673 ++br_nest;
10674 else if (*p == ']')
10675 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 if (*p == '{')
10680 {
10681 mb_nest++;
10682 if (expr_start != NULL && *expr_start == NULL)
10683 *expr_start = p;
10684 }
10685 else if (*p == '}')
10686 {
10687 mb_nest--;
10688 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
10689 *expr_end = p;
10690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 }
10693
10694 return p;
10695}
10696
10697/*
10698 * Return TRUE if character "c" can be used in a variable or function name.
10699 */
10700 static int
10701eval_isnamec(c)
10702 int c;
10703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == '{' || c == '}');
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
10708 * Find a v: variable.
10709 * Return it's index, or -1 if not found.
10710 */
10711 static int
10712find_vim_var(name, len)
10713 char_u *name;
10714 int len; /* length of "name" */
10715{
10716 char_u *vname;
10717 int vlen;
10718 int i;
10719
10720 /*
10721 * Ignore "v:" for old built-in variables, require it for new ones.
10722 */
10723 if (name[0] == 'v' && name[1] == ':')
10724 {
10725 vname = name + 2;
10726 vlen = len - 2;
10727 }
10728 else
10729 {
10730 vname = name;
10731 vlen = len;
10732 }
10733 for (i = 0; i < VV_LEN; ++i)
10734 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
10735 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
10736 return i;
10737 return -1;
10738}
10739
10740/*
10741 * Set number v: variable to "val".
10742 */
10743 void
10744set_vim_var_nr(idx, val)
10745 int idx;
10746 long val;
10747{
10748 vimvars[idx].val = (char_u *)val;
10749}
10750
10751/*
10752 * Get number v: variable value;
10753 */
10754 long
10755get_vim_var_nr(idx)
10756 int idx;
10757{
10758 return (long)vimvars[idx].val;
10759}
10760
10761/*
10762 * Set v:count, v:count1 and v:prevcount.
10763 */
10764 void
10765set_vcount(count, count1)
10766 long count;
10767 long count1;
10768{
10769 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
10770 vimvars[VV_COUNT].val = (char_u *)count;
10771 vimvars[VV_COUNT1].val = (char_u *)count1;
10772}
10773
10774/*
10775 * Set string v: variable to a copy of "val".
10776 */
10777 void
10778set_vim_var_string(idx, val, len)
10779 int idx;
10780 char_u *val;
10781 int len; /* length of "val" to use or -1 (whole string) */
10782{
10783 vim_free(vimvars[idx].val);
10784 if (val == NULL)
10785 vimvars[idx].val = NULL;
10786 else if (len == -1)
10787 vimvars[idx].val = vim_strsave(val);
10788 else
10789 vimvars[idx].val = vim_strnsave(val, len);
10790}
10791
10792/*
10793 * Set v:register if needed.
10794 */
10795 void
10796set_reg_var(c)
10797 int c;
10798{
10799 char_u regname;
10800
10801 if (c == 0 || c == ' ')
10802 regname = '"';
10803 else
10804 regname = c;
10805 /* Avoid free/alloc when the value is already right. */
10806 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
10807 set_vim_var_string(VV_REG, &regname, 1);
10808}
10809
10810/*
10811 * Get or set v:exception. If "oldval" == NULL, return the current value.
10812 * Otherwise, restore the value to "oldval" and return NULL.
10813 * Must always be called in pairs to save and restore v:exception! Does not
10814 * take care of memory allocations.
10815 */
10816 char_u *
10817v_exception(oldval)
10818 char_u *oldval;
10819{
10820 if (oldval == NULL)
10821 return vimvars[VV_EXCEPTION].val;
10822
10823 vimvars[VV_EXCEPTION].val = oldval;
10824 return NULL;
10825}
10826
10827/*
10828 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
10829 * Otherwise, restore the value to "oldval" and return NULL.
10830 * Must always be called in pairs to save and restore v:throwpoint! Does not
10831 * take care of memory allocations.
10832 */
10833 char_u *
10834v_throwpoint(oldval)
10835 char_u *oldval;
10836{
10837 if (oldval == NULL)
10838 return vimvars[VV_THROWPOINT].val;
10839
10840 vimvars[VV_THROWPOINT].val = oldval;
10841 return NULL;
10842}
10843
10844#if defined(FEAT_AUTOCMD) || defined(PROTO)
10845/*
10846 * Set v:cmdarg.
10847 * If "eap" != NULL, use "eap" to generate the value and return the old value.
10848 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
10849 * Must always be called in pairs!
10850 */
10851 char_u *
10852set_cmdarg(eap, oldarg)
10853 exarg_T *eap;
10854 char_u *oldarg;
10855{
10856 char_u *oldval;
10857 char_u *newval;
10858 unsigned len;
10859
10860 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000010861 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000010863 vim_free(oldval);
10864 vimvars[VV_CMDARG].val = oldarg;
10865 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 }
10867
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000010868 if (eap->force_bin == FORCE_BIN)
10869 len = 6;
10870 else if (eap->force_bin == FORCE_NOBIN)
10871 len = 8;
10872 else
10873 len = 0;
10874 if (eap->force_ff != 0)
10875 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
10876# ifdef FEAT_MBYTE
10877 if (eap->force_enc != 0)
10878 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
10879# endif
10880
10881 newval = alloc(len + 1);
10882 if (newval == NULL)
10883 return NULL;
10884
10885 if (eap->force_bin == FORCE_BIN)
10886 sprintf((char *)newval, " ++bin");
10887 else if (eap->force_bin == FORCE_NOBIN)
10888 sprintf((char *)newval, " ++nobin");
10889 else
10890 *newval = NUL;
10891 if (eap->force_ff != 0)
10892 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
10893 eap->cmd + eap->force_ff);
10894# ifdef FEAT_MBYTE
10895 if (eap->force_enc != 0)
10896 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
10897 eap->cmd + eap->force_enc);
10898# endif
10899 vimvars[VV_CMDARG].val = newval;
10900 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901}
10902#endif
10903
10904/*
10905 * Get the value of internal variable "name".
10906 * Return OK or FAIL.
10907 */
10908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 char_u *name;
10911 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
10914 int ret = OK;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010915 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 VAR v;
10917 int cc;
10918 int i;
10919
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010920 tv.v_type = VAR_UNKNOWN;
10921
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 /* truncate the name, so that we can use strcmp() */
10923 cc = name[len];
10924 name[len] = NUL;
10925
10926 /*
10927 * Check for "b:changedtick".
10928 */
10929 if (STRCMP(name, "b:changedtick") == 0)
10930 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010931 tv.v_type = VAR_NUMBER;
10932 tv.vval.v_number = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 }
10934
10935 /*
10936 * Check for built-in v: variables.
10937 */
10938 else if ((i = find_vim_var(name, len)) >= 0)
10939 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940 tv.v_type = vimvars[i].type;
10941 if (tv.v_type == VAR_NUMBER)
10942 tv.vval.v_number = (long)vimvars[i].val;
10943 else
10944 tv.vval.v_string = vimvars[i].val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 }
10946
10947 /*
10948 * Check for user-defined variables.
10949 */
10950 else
10951 {
10952 v = find_var(name, FALSE);
10953 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010954 tv = v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 }
10956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 if (tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 if (rettv != NULL)
10960 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 ret = FAIL;
10962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 else if (rettv != NULL)
10964 copy_tv(&tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966 name[len] = cc;
10967
10968 return ret;
10969}
10970
10971/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010972 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
10973 * value).
10974 */
10975 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010977{
10978 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
10979}
10980
10981/*
10982 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 * The string "s" must have been allocated, it is consumed.
10984 * Return NULL for out of memory, the variable otherwise.
10985 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010986 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 char_u *s;
10989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 rettv = alloc_tv();
10993 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995 rettv->v_type = VAR_STRING;
10996 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 }
10998 else
10999 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001}
11002
11003/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 */
11006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011008 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
11010 if (varp != NULL)
11011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011012 switch (varp->v_type)
11013 {
11014 case VAR_STRING:
11015 case VAR_FUNC:
11016 vim_free(varp->vval.v_string);
11017 break;
11018 case VAR_LIST:
11019 list_unref(varp->vval.v_list);
11020 break;
11021 default:
11022 break;
11023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 vim_free(varp);
11025 }
11026}
11027
11028/*
11029 * Free the memory for a variable value and set the value to NULL or 0.
11030 */
11031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011033 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
11035 if (varp != NULL)
11036 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011037 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011039 case VAR_STRING:
11040 case VAR_FUNC:
11041 vim_free(varp->vval.v_string);
11042 varp->vval.v_string = NULL;
11043 break;
11044 case VAR_LIST:
11045 list_unref(varp->vval.v_list);
11046 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048 varp->vval.v_number = 0;
11049 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 case VAR_UNKNOWN:
11051 break;
11052 default:
11053 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
11056}
11057
11058/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 * Set the value of a variable to NULL without freeing items.
11060 */
11061 static void
11062init_tv(varp)
11063 typeval *varp;
11064{
11065 if (varp != NULL)
11066 vim_memset(varp, 0, sizeof(typeval));
11067}
11068
11069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 * Get the number value of a variable.
11071 * If it is a String variable, uses vim_str2nr().
11072 */
11073 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011077 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011079 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011081 case VAR_NUMBER:
11082 n = (long)(varp->vval.v_number);
11083 break;
11084 case VAR_FUNC:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011085 EMSG(_("E703: Using function reference as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086 break;
11087 case VAR_STRING:
11088 if (varp->vval.v_string != NULL)
11089 vim_str2nr(varp->vval.v_string, NULL, NULL,
11090 TRUE, TRUE, &n, NULL);
11091 break;
11092 default:
11093 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011095 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096}
11097
11098/*
11099 * Get the lnum from the first argument. Also accepts ".", "$", etc.
11100 */
11101 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011103 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 linenr_T lnum;
11107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 if (lnum == 0) /* no valid number, try using line() */
11110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 rettv.v_type = VAR_NUMBER;
11112 f_line(argvars, &rettv);
11113 lnum = rettv.vval.v_number;
11114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 }
11116 return lnum;
11117}
11118
11119/*
11120 * Get the string value of a variable.
11121 * If it is a Number variable, the number is converted into a string.
11122 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
11123 * get_var_string_buf() uses a given buffer.
11124 * If the String variable has never been set, return an empty string.
11125 * Never returns NULL;
11126 */
11127 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011129 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
11131 static char_u mybuf[NUMBUFLEN];
11132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134}
11135
11136 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011138 typeval *varp;
11139 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011141 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011143 case VAR_NUMBER:
11144 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
11145 return buf;
11146 case VAR_FUNC:
11147 EMSG(_("E99: using Funcref as a String"));
11148 break;
11149 case VAR_LIST:
11150 EMSG(_("E99: using List as a String"));
11151 break;
11152 case VAR_STRING:
11153 if (varp->vval.v_string != NULL)
11154 return varp->vval.v_string;
11155 break;
11156 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011160 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161}
11162
11163/*
11164 * Find variable "name" in the list of variables.
11165 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011166 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 */
11168 static VAR
11169find_var(name, writing)
11170 char_u *name;
11171 int writing;
11172{
11173 int i;
11174 char_u *varname;
11175 garray_T *gap;
11176
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 if (name[0] == 'a' && name[1] == ':')
11178 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011179 /* Function arguments "a:".
11180 * NOTE: We use a typecast, because function arguments don't have a
11181 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 if (writing)
11183 {
11184 EMSG2(_(e_readonlyvar), name);
11185 return NULL;
11186 }
11187 name += 2;
11188 if (current_funccal == NULL)
11189 return NULL;
11190 if (VIM_ISDIGIT(*name))
11191 {
11192 i = atol((char *)name);
11193 if (i == 0) /* a:0 */
11194 return &current_funccal->a0_var;
11195 i += current_funccal->func->args.ga_len;
11196 if (i > current_funccal->argcount) /* a:999 */
11197 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011198 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 }
11200 if (STRCMP(name, "firstline") == 0)
11201 return &(current_funccal->firstline);
11202 if (STRCMP(name, "lastline") == 0)
11203 return &(current_funccal->lastline);
11204 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
11205 if (STRCMP(name, ((char_u **)
11206 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011207 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 return NULL;
11209 }
11210
11211 gap = find_var_ga(name, &varname);
11212 if (gap == NULL)
11213 return NULL;
11214 return find_var_in_ga(gap, varname);
11215}
11216
11217 static VAR
11218find_var_in_ga(gap, varname)
11219 garray_T *gap;
11220 char_u *varname;
11221{
11222 int i;
11223
11224 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011225 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
11226 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 break;
11228 if (i < 0)
11229 return NULL;
11230 return &VAR_GAP_ENTRY(i, gap);
11231}
11232
11233/*
11234 * Find the growarray and start of name without ':' for a variable name.
11235 */
11236 static garray_T *
11237find_var_ga(name, varname)
11238 char_u *name;
11239 char_u **varname;
11240{
11241 if (name[1] != ':')
11242 {
11243 /* If not "x:name" there must not be any ":" in the name. */
11244 if (vim_strchr(name, ':') != NULL)
11245 return NULL;
11246 *varname = name;
11247 if (current_funccal == NULL)
11248 return &variables; /* global variable */
11249 return &current_funccal->l_vars; /* local function variable */
11250 }
11251 *varname = name + 2;
11252 if (*name == 'b') /* buffer variable */
11253 return &curbuf->b_vars;
11254 if (*name == 'w') /* window variable */
11255 return &curwin->w_vars;
11256 if (*name == 'g') /* global variable */
11257 return &variables;
11258 if (*name == 'l' && current_funccal != NULL)/* local function variable */
11259 return &current_funccal->l_vars;
11260 if (*name == 's' /* script variable */
11261 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
11262 return &SCRIPT_VARS(current_SID);
11263 return NULL;
11264}
11265
11266/*
11267 * Get the string value of a (global/local) variable.
11268 * Returns NULL when it doesn't exist.
11269 */
11270 char_u *
11271get_var_value(name)
11272 char_u *name;
11273{
11274 VAR v;
11275
11276 v = find_var(name, FALSE);
11277 if (v == NULL)
11278 return NULL;
11279 return get_var_string(v);
11280}
11281
11282/*
11283 * Allocate a new growarry for a sourced script. It will be used while
11284 * sourcing this script and when executing functions defined in the script.
11285 */
11286 void
11287new_script_vars(id)
11288 scid_T id;
11289{
11290 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
11291 {
11292 while (ga_scripts.ga_len < id)
11293 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011294 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 }
11297 }
11298}
11299
11300/*
11301 * Initialize internal variables for use.
11302 */
11303 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011304vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 garray_T *gap;
11306{
11307 ga_init2(gap, (int)sizeof(var), 4);
11308}
11309
11310/*
11311 * Clean up a list of internal variables.
11312 */
11313 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011314vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 garray_T *gap;
11316{
11317 int i;
11318
11319 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011320 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 ga_clear(gap);
11322}
11323
11324 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011325clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 VAR v;
11327{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011328 vim_free(v->v_name);
11329 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331}
11332
11333/*
11334 * List the value of one internal variable.
11335 */
11336 static void
11337list_one_var(v, prefix)
11338 VAR v;
11339 char_u *prefix;
11340{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011341 char_u *tofree;
11342 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011343 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011344
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011345 s = tv2string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011346 list_one_var_a(prefix, v->v_name, v->tv.v_type,
11347 s == NULL ? (char_u *)"" : s);
11348 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349}
11350
11351/*
11352 * List the value of one "v:" variable.
11353 */
11354 static void
11355list_vim_var(i)
11356 int i; /* index in vimvars[] */
11357{
11358 char_u *p;
11359 char_u numbuf[NUMBUFLEN];
11360
11361 if (vimvars[i].type == VAR_NUMBER)
11362 {
11363 p = numbuf;
11364 sprintf((char *)p, "%ld", (long)vimvars[i].val);
11365 }
11366 else if (vimvars[i].val == NULL)
11367 p = (char_u *)"";
11368 else
11369 p = vimvars[i].val;
11370 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
11371 vimvars[i].type, p);
11372}
11373
11374 static void
11375list_one_var_a(prefix, name, type, string)
11376 char_u *prefix;
11377 char_u *name;
11378 int type;
11379 char_u *string;
11380{
11381 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
11382 if (name != NULL) /* "a:" vars don't have a name stored */
11383 msg_puts(name);
11384 msg_putchar(' ');
11385 msg_advance(22);
11386 if (type == VAR_NUMBER)
11387 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011388 else if (type == VAR_FUNC)
11389 msg_putchar('*');
11390 else if (type == VAR_LIST)
11391 {
11392 msg_putchar('[');
11393 if (*string == '[')
11394 ++string;
11395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 else
11397 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011398
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011400
11401 if (type == VAR_FUNC)
11402 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403}
11404
11405/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 * If the variable already exists, the value is updated.
11408 * Otherwise the variable is created.
11409 */
11410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 typeval *tv;
11414 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
11416 int i;
11417 VAR v;
11418 char_u *varname;
11419 garray_T *gap;
11420
11421 /*
11422 * Handle setting internal v: variables.
11423 */
11424 i = find_vim_var(name, (int)STRLEN(name));
11425 if (i >= 0)
11426 {
11427 if (vimvars[i].flags & VV_RO)
11428 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011429 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
11430 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 else
11432 {
11433 if (vimvars[i].type == VAR_STRING)
11434 {
11435 vim_free(vimvars[i].val);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 if (copy || tv->v_type != VAR_STRING)
11437 vimvars[i].val = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011438 else
11439 {
11440 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 vimvars[i].val = tv->vval.v_string;
11442 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 }
11445 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 vimvars[i].val = (char_u *)get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 }
11448 return;
11449 }
11450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011452 {
11453 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
11454 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
11455 ? name[2] : name[0]))
11456 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011457 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011458 return;
11459 }
11460 if (function_exists(name))
11461 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011462 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011463 return;
11464 }
11465 }
11466
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 v = find_var(name, TRUE);
11468 if (v != NULL) /* existing variable, only need to free string */
11469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011471 && !((v->tv.v_type == VAR_STRING
11472 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 && (tv->v_type == VAR_STRING
11474 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011476 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011477 return;
11478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 }
11481 else /* add a new variable */
11482 {
11483 gap = find_var_ga(name, &varname);
11484 if (gap == NULL) /* illegal name */
11485 {
11486 EMSG2(_("E461: Illegal variable name: %s"), name);
11487 return;
11488 }
11489
11490 /* Try to use an empty entry */
11491 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011492 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 break;
11494 if (i < 0) /* need to allocate more room */
11495 {
11496 if (ga_grow(gap, 1) == FAIL)
11497 return;
11498 i = gap->ga_len;
11499 }
11500 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011501 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 return;
11503 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 if (copy || tv->v_type == VAR_NUMBER)
11507 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011508 else
11509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 v->tv = *tv;
11511 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513}
11514
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011515/*
11516 * Copy the values from typeval "from" to typeval "to".
11517 * When needed allocates string or increases reference count.
11518 * Does not make a copy of a list!
11519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011522 typeval *from;
11523 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011525 to->v_type = from->v_type;
11526 switch (from->v_type)
11527 {
11528 case VAR_NUMBER:
11529 to->vval.v_number = from->vval.v_number;
11530 break;
11531 case VAR_STRING:
11532 case VAR_FUNC:
11533 if (from->vval.v_string == NULL)
11534 to->vval.v_string = NULL;
11535 else
11536 to->vval.v_string = vim_strsave(from->vval.v_string);
11537 break;
11538 case VAR_LIST:
11539 if (from->vval.v_list == NULL)
11540 to->vval.v_list = NULL;
11541 else
11542 {
11543 to->vval.v_list = from->vval.v_list;
11544 ++to->vval.v_list->lv_refcount;
11545 }
11546 break;
11547 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011549 break;
11550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551}
11552
11553/*
11554 * ":echo expr1 ..." print each argument separated with a space, add a
11555 * newline at the end.
11556 * ":echon expr1 ..." print each argument plain.
11557 */
11558 void
11559ex_echo(eap)
11560 exarg_T *eap;
11561{
11562 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011564 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 char_u *p;
11566 int needclr = TRUE;
11567 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011568 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569
11570 if (eap->skip)
11571 ++emsg_skip;
11572 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
11573 {
11574 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 {
11577 /*
11578 * Report the invalid expression unless the expression evaluation
11579 * has been cancelled due to an aborting error, an interrupt, or an
11580 * exception.
11581 */
11582 if (!aborting())
11583 EMSG2(_(e_invexpr2), p);
11584 break;
11585 }
11586 if (!eap->skip)
11587 {
11588 if (atstart)
11589 {
11590 atstart = FALSE;
11591 /* Call msg_start() after eval1(), evaluating the expression
11592 * may cause a message to appear. */
11593 if (eap->cmdidx == CMD_echo)
11594 msg_start();
11595 }
11596 else if (eap->cmdidx == CMD_echo)
11597 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011598 for (p = tv2string(&rettv, &tofree, numbuf);
11599 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 if (*p == '\n' || *p == '\r' || *p == TAB)
11601 {
11602 if (*p != TAB && needclr)
11603 {
11604 /* remove any text still there from the command */
11605 msg_clr_eos();
11606 needclr = FALSE;
11607 }
11608 msg_putchar_attr(*p, echo_attr);
11609 }
11610 else
11611 {
11612#ifdef FEAT_MBYTE
11613 if (has_mbyte)
11614 {
11615 int i = (*mb_ptr2len_check)(p);
11616
11617 (void)msg_outtrans_len_attr(p, i, echo_attr);
11618 p += i - 1;
11619 }
11620 else
11621#endif
11622 (void)msg_outtrans_len_attr(p, 1, echo_attr);
11623 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011624 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 arg = skipwhite(arg);
11628 }
11629 eap->nextcmd = check_nextcmd(arg);
11630
11631 if (eap->skip)
11632 --emsg_skip;
11633 else
11634 {
11635 /* remove text that may still be there from the command */
11636 if (needclr)
11637 msg_clr_eos();
11638 if (eap->cmdidx == CMD_echo)
11639 msg_end();
11640 }
11641}
11642
11643/*
11644 * ":echohl {name}".
11645 */
11646 void
11647ex_echohl(eap)
11648 exarg_T *eap;
11649{
11650 int id;
11651
11652 id = syn_name2id(eap->arg);
11653 if (id == 0)
11654 echo_attr = 0;
11655 else
11656 echo_attr = syn_id2attr(id);
11657}
11658
11659/*
11660 * ":execute expr1 ..." execute the result of an expression.
11661 * ":echomsg expr1 ..." Print a message
11662 * ":echoerr expr1 ..." Print an error
11663 * Each gets spaces around each argument and a newline at the end for
11664 * echo commands
11665 */
11666 void
11667ex_execute(eap)
11668 exarg_T *eap;
11669{
11670 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 int ret = OK;
11673 char_u *p;
11674 garray_T ga;
11675 int len;
11676 int save_did_emsg;
11677
11678 ga_init2(&ga, 1, 80);
11679
11680 if (eap->skip)
11681 ++emsg_skip;
11682 while (*arg != NUL && *arg != '|' && *arg != '\n')
11683 {
11684 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 {
11687 /*
11688 * Report the invalid expression unless the expression evaluation
11689 * has been cancelled due to an aborting error, an interrupt, or an
11690 * exception.
11691 */
11692 if (!aborting())
11693 EMSG2(_(e_invexpr2), p);
11694 ret = FAIL;
11695 break;
11696 }
11697
11698 if (!eap->skip)
11699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011700 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 len = (int)STRLEN(p);
11702 if (ga_grow(&ga, len + 2) == FAIL)
11703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 ret = FAIL;
11706 break;
11707 }
11708 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 ga.ga_len += len;
11712 }
11713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 arg = skipwhite(arg);
11716 }
11717
11718 if (ret != FAIL && ga.ga_data != NULL)
11719 {
11720 if (eap->cmdidx == CMD_echomsg)
11721 MSG_ATTR(ga.ga_data, echo_attr);
11722 else if (eap->cmdidx == CMD_echoerr)
11723 {
11724 /* We don't want to abort following commands, restore did_emsg. */
11725 save_did_emsg = did_emsg;
11726 EMSG((char_u *)ga.ga_data);
11727 if (!force_abort)
11728 did_emsg = save_did_emsg;
11729 }
11730 else if (eap->cmdidx == CMD_execute)
11731 do_cmdline((char_u *)ga.ga_data,
11732 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
11733 }
11734
11735 ga_clear(&ga);
11736
11737 if (eap->skip)
11738 --emsg_skip;
11739
11740 eap->nextcmd = check_nextcmd(arg);
11741}
11742
11743/*
11744 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
11745 * "arg" points to the "&" or '+' when called, to "option" when returning.
11746 * Returns NULL when no option name found. Otherwise pointer to the char
11747 * after the option name.
11748 */
11749 static char_u *
11750find_option_end(arg, opt_flags)
11751 char_u **arg;
11752 int *opt_flags;
11753{
11754 char_u *p = *arg;
11755
11756 ++p;
11757 if (*p == 'g' && p[1] == ':')
11758 {
11759 *opt_flags = OPT_GLOBAL;
11760 p += 2;
11761 }
11762 else if (*p == 'l' && p[1] == ':')
11763 {
11764 *opt_flags = OPT_LOCAL;
11765 p += 2;
11766 }
11767 else
11768 *opt_flags = 0;
11769
11770 if (!ASCII_ISALPHA(*p))
11771 return NULL;
11772 *arg = p;
11773
11774 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
11775 p += 4; /* termcap option */
11776 else
11777 while (ASCII_ISALPHA(*p))
11778 ++p;
11779 return p;
11780}
11781
11782/*
11783 * ":function"
11784 */
11785 void
11786ex_function(eap)
11787 exarg_T *eap;
11788{
11789 char_u *theline;
11790 int j;
11791 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 char_u *name = NULL;
11794 char_u *p;
11795 char_u *arg;
11796 garray_T newargs;
11797 garray_T newlines;
11798 int varargs = FALSE;
11799 int mustend = FALSE;
11800 int flags = 0;
11801 ufunc_T *fp;
11802 int indent;
11803 int nesting;
11804 char_u *skip_until = NULL;
11805 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011806 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807
11808 /*
11809 * ":function" without argument: list functions.
11810 */
11811 if (ends_excmd(*eap->arg))
11812 {
11813 if (!eap->skip)
11814 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
11815 list_func_head(fp, FALSE);
11816 eap->nextcmd = check_nextcmd(eap->arg);
11817 return;
11818 }
11819
11820 p = eap->arg;
11821 name = trans_function_name(&p, eap->skip, FALSE);
11822 if (name == NULL && !eap->skip)
11823 {
11824 /*
11825 * Return on an invalid expression in braces, unless the expression
11826 * evaluation has been cancelled due to an aborting error, an
11827 * interrupt, or an exception.
11828 */
11829 if (!aborting())
11830 return;
11831 else
11832 eap->skip = TRUE;
11833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 /* An error in a function call during evaluation of an expression in magic
11835 * braces should not cause the function not to be defined. */
11836 saved_did_emsg = did_emsg;
11837 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838
11839 /*
11840 * ":function func" with only function name: list function.
11841 */
11842 if (vim_strchr(p, '(') == NULL)
11843 {
11844 if (!ends_excmd(*skipwhite(p)))
11845 {
11846 EMSG(_(e_trailing));
11847 goto erret_name;
11848 }
11849 eap->nextcmd = check_nextcmd(p);
11850 if (eap->nextcmd != NULL)
11851 *p = NUL;
11852 if (!eap->skip && !got_int)
11853 {
11854 fp = find_func(name);
11855 if (fp != NULL)
11856 {
11857 list_func_head(fp, TRUE);
11858 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
11859 {
11860 msg_putchar('\n');
11861 msg_outnum((long)(j + 1));
11862 if (j < 9)
11863 msg_putchar(' ');
11864 if (j < 99)
11865 msg_putchar(' ');
11866 msg_prt_line(FUNCLINE(fp, j));
11867 out_flush(); /* show a line at a time */
11868 ui_breakcheck();
11869 }
11870 if (!got_int)
11871 {
11872 msg_putchar('\n');
11873 msg_puts((char_u *)" endfunction");
11874 }
11875 }
11876 else
11877 EMSG2(_("E123: Undefined function: %s"), eap->arg);
11878 }
11879 goto erret_name;
11880 }
11881
11882 /*
11883 * ":function name(arg1, arg2)" Define function.
11884 */
11885 p = skipwhite(p);
11886 if (*p != '(')
11887 {
11888 if (!eap->skip)
11889 {
11890 EMSG2(_("E124: Missing '(': %s"), eap->arg);
11891 goto erret_name;
11892 }
11893 /* attempt to continue by skipping some text */
11894 if (vim_strchr(p, '(') != NULL)
11895 p = vim_strchr(p, '(');
11896 }
11897 p = skipwhite(p + 1);
11898
11899 ga_init2(&newargs, (int)sizeof(char_u *), 3);
11900 ga_init2(&newlines, (int)sizeof(char_u *), 3);
11901
11902 /*
11903 * Isolate the arguments: "arg1, arg2, ...)"
11904 */
11905 while (*p != ')')
11906 {
11907 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
11908 {
11909 varargs = TRUE;
11910 p += 3;
11911 mustend = TRUE;
11912 }
11913 else
11914 {
11915 arg = p;
11916 while (ASCII_ISALNUM(*p) || *p == '_')
11917 ++p;
11918 if (arg == p || isdigit(*arg)
11919 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
11920 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
11921 {
11922 if (!eap->skip)
11923 EMSG2(_("E125: Illegal argument: %s"), arg);
11924 break;
11925 }
11926 if (ga_grow(&newargs, 1) == FAIL)
11927 goto erret;
11928 c = *p;
11929 *p = NUL;
11930 arg = vim_strsave(arg);
11931 if (arg == NULL)
11932 goto erret;
11933 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
11934 *p = c;
11935 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 if (*p == ',')
11937 ++p;
11938 else
11939 mustend = TRUE;
11940 }
11941 p = skipwhite(p);
11942 if (mustend && *p != ')')
11943 {
11944 if (!eap->skip)
11945 EMSG2(_(e_invarg2), eap->arg);
11946 break;
11947 }
11948 }
11949 ++p; /* skip the ')' */
11950
11951 /* find extra arguments "range" and "abort" */
11952 for (;;)
11953 {
11954 p = skipwhite(p);
11955 if (STRNCMP(p, "range", 5) == 0)
11956 {
11957 flags |= FC_RANGE;
11958 p += 5;
11959 }
11960 else if (STRNCMP(p, "abort", 5) == 0)
11961 {
11962 flags |= FC_ABORT;
11963 p += 5;
11964 }
11965 else
11966 break;
11967 }
11968
11969 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
11970 EMSG(_(e_trailing));
11971
11972 /*
11973 * Read the body of the function, until ":endfunction" is found.
11974 */
11975 if (KeyTyped)
11976 {
11977 /* Check if the function already exists, don't let the user type the
11978 * whole function before telling him it doesn't work! For a script we
11979 * need to skip the body to be able to find what follows. */
11980 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
11981 EMSG2(_(e_funcexts), name);
11982
11983 msg_putchar('\n'); /* don't overwrite the function name */
11984 cmdline_row = msg_row;
11985 }
11986
11987 indent = 2;
11988 nesting = 0;
11989 for (;;)
11990 {
11991 msg_scroll = TRUE;
11992 need_wait_return = FALSE;
11993 if (eap->getline == NULL)
11994 theline = getcmdline(':', 0L, indent);
11995 else
11996 theline = eap->getline(':', eap->cookie, indent);
11997 if (KeyTyped)
11998 lines_left = Rows - 1;
11999 if (theline == NULL)
12000 {
12001 EMSG(_("E126: Missing :endfunction"));
12002 goto erret;
12003 }
12004
12005 if (skip_until != NULL)
12006 {
12007 /* between ":append" and "." and between ":python <<EOF" and "EOF"
12008 * don't check for ":endfunc". */
12009 if (STRCMP(theline, skip_until) == 0)
12010 {
12011 vim_free(skip_until);
12012 skip_until = NULL;
12013 }
12014 }
12015 else
12016 {
12017 /* skip ':' and blanks*/
12018 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
12019 ;
12020
12021 /* Check for "endfunction" (should be more strict...). */
12022 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
12023 {
12024 vim_free(theline);
12025 break;
12026 }
12027
12028 /* Increase indent inside "if", "while", and "try", decrease
12029 * at "end". */
12030 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
12031 indent -= 2;
12032 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
12033 || STRNCMP(p, "try", 3) == 0)
12034 indent += 2;
12035
12036 /* Check for defining a function inside this function. */
12037 if (STRNCMP(p, "fu", 2) == 0)
12038 {
12039 p = skipwhite(skiptowhite(p));
12040 p += eval_fname_script(p);
12041 if (ASCII_ISALPHA(*p))
12042 {
12043 vim_free(trans_function_name(&p, TRUE, FALSE));
12044 if (*skipwhite(p) == '(')
12045 {
12046 ++nesting;
12047 indent += 2;
12048 }
12049 }
12050 }
12051
12052 /* Check for ":append" or ":insert". */
12053 p = skip_range(p, NULL);
12054 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
12055 || (p[0] == 'i'
12056 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
12057 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
12058 skip_until = vim_strsave((char_u *)".");
12059
12060 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
12061 arg = skipwhite(skiptowhite(p));
12062 if (arg[0] == '<' && arg[1] =='<'
12063 && ((p[0] == 'p' && p[1] == 'y'
12064 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
12065 || (p[0] == 'p' && p[1] == 'e'
12066 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
12067 || (p[0] == 't' && p[1] == 'c'
12068 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
12069 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
12070 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012071 || (p[0] == 'm' && p[1] == 'z'
12072 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 ))
12074 {
12075 /* ":python <<" continues until a dot, like ":append" */
12076 p = skipwhite(arg + 2);
12077 if (*p == NUL)
12078 skip_until = vim_strsave((char_u *)".");
12079 else
12080 skip_until = vim_strsave(p);
12081 }
12082 }
12083
12084 /* Add the line to the function. */
12085 if (ga_grow(&newlines, 1) == FAIL)
12086 goto erret;
12087 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
12088 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 }
12090
12091 /* Don't define the function when skipping commands or when an error was
12092 * detected. */
12093 if (eap->skip || did_emsg)
12094 goto erret;
12095
12096 /*
12097 * If there are no errors, add the function
12098 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 v = find_var(name, FALSE);
12100 if (v != NULL && v->tv.v_type == VAR_FUNC)
12101 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012102 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012103 goto erret;
12104 }
12105
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 fp = find_func(name);
12107 if (fp != NULL)
12108 {
12109 if (!eap->forceit)
12110 {
12111 EMSG2(_(e_funcexts), name);
12112 goto erret;
12113 }
12114 if (fp->calls)
12115 {
12116 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
12117 goto erret;
12118 }
12119 /* redefine existing function */
12120 ga_clear_strings(&(fp->args));
12121 ga_clear_strings(&(fp->lines));
12122 vim_free(name);
12123 }
12124 else
12125 {
12126 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
12127 if (fp == NULL)
12128 goto erret;
12129 /* insert the new function in the function list */
12130 fp->next = firstfunc;
12131 firstfunc = fp;
12132 fp->name = name;
12133 }
12134 fp->args = newargs;
12135 fp->lines = newlines;
12136 fp->varargs = varargs;
12137 fp->flags = flags;
12138 fp->calls = 0;
12139 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 vim_free(skip_until);
12142 return;
12143
12144erret:
12145 vim_free(skip_until);
12146 ga_clear_strings(&newargs);
12147 ga_clear_strings(&newlines);
12148erret_name:
12149 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151}
12152
12153/*
12154 * Get a function name, translating "<SID>" and "<SNR>".
12155 * Returns the function name in allocated memory, or NULL for failure.
12156 * Advances "pp" to just after the function name (if no error).
12157 */
12158 static char_u *
12159trans_function_name(pp, skip, internal)
12160 char_u **pp;
12161 int skip; /* only find the end, don't evaluate */
12162 int internal; /* TRUE if internal function name OK */
12163{
12164 char_u *name;
12165 char_u *start;
12166 char_u *end;
12167 int lead;
12168 char_u sid_buf[20];
12169 char_u *temp_string = NULL;
12170 char_u *expr_start, *expr_end;
12171 int len;
12172
12173 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
12174 start = *pp;
12175 lead = eval_fname_script(start);
12176 if (lead > 0)
12177 start += lead;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 if (end == start)
12180 {
12181 if (!skip)
12182 EMSG(_("E129: Function name required"));
12183 return NULL;
12184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 if (expr_start != NULL && !skip)
12186 {
12187 /* expand magic curlies */
12188 temp_string = make_expanded_name(start, expr_start, expr_end, end);
12189 if (temp_string == NULL)
12190 {
12191 /*
12192 * Report an invalid expression in braces, unless the expression
12193 * evaluation has been cancelled due to an aborting error, an
12194 * interrupt, or an exception.
12195 */
12196 if (!aborting())
12197 EMSG2(_(e_invarg2), start);
12198 else
12199 *pp = end;
12200 return NULL;
12201 }
12202 start = temp_string;
12203 len = (int)STRLEN(temp_string);
12204 }
12205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 len = (int)(end - start);
12207
12208 /*
12209 * Copy the function name to allocated memory.
12210 * Accept <SID>name() inside a script, translate into <SNR>123_name().
12211 * Accept <SNR>123_name() outside a script.
12212 */
12213 if (skip)
12214 lead = 0; /* do nothing */
12215 else if (lead > 0)
12216 {
12217 lead = 3;
12218 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12219 {
12220 if (current_SID <= 0)
12221 {
12222 EMSG(_(e_usingsid));
12223 return NULL;
12224 }
12225 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
12226 lead += (int)STRLEN(sid_buf);
12227 }
12228 }
12229 else if (!internal && !ASCII_ISUPPER(*start))
12230 {
12231 EMSG2(_("E128: Function name must start with a capital: %s"), start);
12232 return NULL;
12233 }
12234 name = alloc((unsigned)(len + lead + 1));
12235 if (name != NULL)
12236 {
12237 if (lead > 0)
12238 {
12239 name[0] = K_SPECIAL;
12240 name[1] = KS_EXTRA;
12241 name[2] = (int)KE_SNR;
12242 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12243 STRCPY(name + 3, sid_buf);
12244 }
12245 mch_memmove(name + lead, start, (size_t)len);
12246 name[len + lead] = NUL;
12247 }
12248 *pp = end;
12249
12250 vim_free(temp_string);
12251 return name;
12252}
12253
12254/*
12255 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
12256 * Return 2 if "p" starts with "s:".
12257 * Return 0 otherwise.
12258 */
12259 static int
12260eval_fname_script(p)
12261 char_u *p;
12262{
12263 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
12264 || STRNICMP(p + 1, "SNR>", 4) == 0))
12265 return 5;
12266 if (p[0] == 's' && p[1] == ':')
12267 return 2;
12268 return 0;
12269}
12270
12271/*
12272 * Return TRUE if "p" starts with "<SID>" or "s:".
12273 * Only works if eval_fname_script() returned non-zero for "p"!
12274 */
12275 static int
12276eval_fname_sid(p)
12277 char_u *p;
12278{
12279 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
12280}
12281
12282/*
12283 * List the head of the function: "name(arg1, arg2)".
12284 */
12285 static void
12286list_func_head(fp, indent)
12287 ufunc_T *fp;
12288 int indent;
12289{
12290 int j;
12291
12292 msg_start();
12293 if (indent)
12294 MSG_PUTS(" ");
12295 MSG_PUTS("function ");
12296 if (fp->name[0] == K_SPECIAL)
12297 {
12298 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
12299 msg_puts(fp->name + 3);
12300 }
12301 else
12302 msg_puts(fp->name);
12303 msg_putchar('(');
12304 for (j = 0; j < fp->args.ga_len; ++j)
12305 {
12306 if (j)
12307 MSG_PUTS(", ");
12308 msg_puts(FUNCARG(fp, j));
12309 }
12310 if (fp->varargs)
12311 {
12312 if (j)
12313 MSG_PUTS(", ");
12314 MSG_PUTS("...");
12315 }
12316 msg_putchar(')');
12317}
12318
12319/*
12320 * Find a function by name, return pointer to it in ufuncs.
12321 * Return NULL for unknown function.
12322 */
12323 static ufunc_T *
12324find_func(name)
12325 char_u *name;
12326{
12327 ufunc_T *fp;
12328
12329 for (fp = firstfunc; fp != NULL; fp = fp->next)
12330 if (STRCMP(name, fp->name) == 0)
12331 break;
12332 return fp;
12333}
12334
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012335/*
12336 * Return TRUE if a function "name" exists.
12337 */
12338 static int
12339function_exists(name)
12340 char_u *name;
12341{
12342 char_u *p = name;
12343 int n = FALSE;
12344
12345 p = trans_function_name(&p, FALSE, TRUE);
12346 if (p != NULL)
12347 {
12348 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
12349 n = (find_func(p) != NULL);
12350 else if (ASCII_ISLOWER(*p))
12351 n = (find_internal_func(p) >= 0);
12352 vim_free(p);
12353 }
12354 return n;
12355}
12356
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12358
12359/*
12360 * Function given to ExpandGeneric() to obtain the list of user defined
12361 * function names.
12362 */
12363 char_u *
12364get_user_func_name(xp, idx)
12365 expand_T *xp;
12366 int idx;
12367{
12368 static ufunc_T *fp = NULL;
12369
12370 if (idx == 0)
12371 fp = firstfunc;
12372 if (fp != NULL)
12373 {
12374 if (STRLEN(fp->name) + 4 >= IOSIZE)
12375 return fp->name; /* prevents overflow */
12376
12377 cat_func_name(IObuff, fp);
12378 if (xp->xp_context != EXPAND_USER_FUNC)
12379 {
12380 STRCAT(IObuff, "(");
12381 if (!fp->varargs && fp->args.ga_len == 0)
12382 STRCAT(IObuff, ")");
12383 }
12384
12385 fp = fp->next;
12386 return IObuff;
12387 }
12388 return NULL;
12389}
12390
12391#endif /* FEAT_CMDL_COMPL */
12392
12393/*
12394 * Copy the function name of "fp" to buffer "buf".
12395 * "buf" must be able to hold the function name plus three bytes.
12396 * Takes care of script-local function names.
12397 */
12398 static void
12399cat_func_name(buf, fp)
12400 char_u *buf;
12401 ufunc_T *fp;
12402{
12403 if (fp->name[0] == K_SPECIAL)
12404 {
12405 STRCPY(buf, "<SNR>");
12406 STRCAT(buf, fp->name + 3);
12407 }
12408 else
12409 STRCPY(buf, fp->name);
12410}
12411
12412/*
12413 * ":delfunction {name}"
12414 */
12415 void
12416ex_delfunction(eap)
12417 exarg_T *eap;
12418{
12419 ufunc_T *fp = NULL, *pfp;
12420 char_u *p;
12421 char_u *name;
12422
12423 p = eap->arg;
12424 name = trans_function_name(&p, eap->skip, FALSE);
12425 if (name == NULL)
12426 return;
12427 if (!ends_excmd(*skipwhite(p)))
12428 {
12429 vim_free(name);
12430 EMSG(_(e_trailing));
12431 return;
12432 }
12433 eap->nextcmd = check_nextcmd(p);
12434 if (eap->nextcmd != NULL)
12435 *p = NUL;
12436
12437 if (!eap->skip)
12438 fp = find_func(name);
12439 vim_free(name);
12440
12441 if (!eap->skip)
12442 {
12443 if (fp == NULL)
12444 {
12445 EMSG2(_("E130: Undefined function: %s"), eap->arg);
12446 return;
12447 }
12448 if (fp->calls)
12449 {
12450 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
12451 return;
12452 }
12453
12454 /* clear this function */
12455 vim_free(fp->name);
12456 ga_clear_strings(&(fp->args));
12457 ga_clear_strings(&(fp->lines));
12458
12459 /* remove the function from the function list */
12460 if (firstfunc == fp)
12461 firstfunc = fp->next;
12462 else
12463 {
12464 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
12465 if (pfp->next == fp)
12466 {
12467 pfp->next = fp->next;
12468 break;
12469 }
12470 }
12471 vim_free(fp);
12472 }
12473}
12474
12475/*
12476 * Call a user function.
12477 */
12478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479call_user_func(fp, argcount, argvars, rettv, firstline, lastline)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 ufunc_T *fp; /* pointer to function */
12481 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012482 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 linenr_T firstline; /* first line of range */
12485 linenr_T lastline; /* last line of range */
12486{
12487 char_u *save_sourcing_name;
12488 linenr_T save_sourcing_lnum;
12489 scid_T save_current_SID;
12490 struct funccall fc;
12491 struct funccall *save_fcp = current_funccal;
12492 int save_did_emsg;
12493 static int depth = 0;
12494
12495 /* If depth of calling is getting too high, don't execute the function */
12496 if (depth >= p_mfd)
12497 {
12498 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->v_type = VAR_NUMBER;
12500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 return;
12502 }
12503 ++depth;
12504
12505 line_breakcheck(); /* check for CTRL-C hit */
12506
12507 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012508 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 fc.func = fp;
12510 fc.argcount = argcount;
12511 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 fc.rettv = rettv;
12513 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 fc.linenr = 0;
12515 fc.returned = FALSE;
12516 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012517 fc.a0_var.tv.v_type = VAR_NUMBER;
12518 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
12519 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012521 fc.firstline.tv.v_type = VAR_NUMBER;
12522 fc.firstline.tv.vval.v_number = firstline;
12523 fc.firstline.v_name = NULL;
12524 fc.lastline.tv.v_type = VAR_NUMBER;
12525 fc.lastline.tv.vval.v_number = lastline;
12526 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 /* Check if this function has a breakpoint. */
12528 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
12529 fc.dbg_tick = debug_tick;
12530
12531 /* Don't redraw while executing the function. */
12532 ++RedrawingDisabled;
12533 save_sourcing_name = sourcing_name;
12534 save_sourcing_lnum = sourcing_lnum;
12535 sourcing_lnum = 1;
12536 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
12537 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
12538 if (sourcing_name != NULL)
12539 {
12540 if (save_sourcing_name != NULL
12541 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
12542 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
12543 else
12544 STRCPY(sourcing_name, "function ");
12545 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
12546
12547 if (p_verbose >= 12)
12548 {
12549 ++no_wait_return;
12550 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12551 msg_str((char_u *)_("calling %s"), sourcing_name);
12552 if (p_verbose >= 14)
12553 {
12554 int i;
12555 char_u buf[MSG_BUF_LEN];
12556
12557 msg_puts((char_u *)"(");
12558 for (i = 0; i < argcount; ++i)
12559 {
12560 if (i > 0)
12561 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012562 if (argvars[i].v_type == VAR_NUMBER)
12563 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564 else
12565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 buf, MSG_BUF_LEN);
12568 msg_puts((char_u *)"\"");
12569 msg_puts(buf);
12570 msg_puts((char_u *)"\"");
12571 }
12572 }
12573 msg_puts((char_u *)")");
12574 }
12575 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12576 cmdline_row = msg_row;
12577 --no_wait_return;
12578 }
12579 }
12580 save_current_SID = current_SID;
12581 current_SID = fp->script_ID;
12582 save_did_emsg = did_emsg;
12583 did_emsg = FALSE;
12584
12585 /* call do_cmdline() to execute the lines */
12586 do_cmdline(NULL, get_func_line, (void *)&fc,
12587 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
12588
12589 --RedrawingDisabled;
12590
12591 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 clear_tv(rettv);
12595 rettv->v_type = VAR_NUMBER;
12596 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 }
12598
12599 /* when being verbose, mention the return value */
12600 if (p_verbose >= 12)
12601 {
12602 char_u *sn, *val;
12603
12604 ++no_wait_return;
12605 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12606
12607 /* Make sure the output fits in IObuff. */
12608 sn = sourcing_name;
12609 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
12610 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
12611
12612 if (aborting())
12613 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 (long)fc.rettv->vval.v_number);
12617 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 if (STRLEN(val) > IOSIZE / 2 - 50)
12621 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
12622 smsg((char_u *)_("%s returning \"%s\""), sn, val);
12623 }
12624 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12625 cmdline_row = msg_row;
12626 --no_wait_return;
12627 }
12628
12629 vim_free(sourcing_name);
12630 sourcing_name = save_sourcing_name;
12631 sourcing_lnum = save_sourcing_lnum;
12632 current_SID = save_current_SID;
12633
12634 if (p_verbose >= 12 && sourcing_name != NULL)
12635 {
12636 ++no_wait_return;
12637 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12638 msg_str((char_u *)_("continuing in %s"), sourcing_name);
12639 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12640 cmdline_row = msg_row;
12641 --no_wait_return;
12642 }
12643
12644 did_emsg |= save_did_emsg;
12645 current_funccal = save_fcp;
12646
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012647 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 --depth;
12649}
12650
12651/*
12652 * ":return [expr]"
12653 */
12654 void
12655ex_return(eap)
12656 exarg_T *eap;
12657{
12658 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 int returning = FALSE;
12661
12662 if (current_funccal == NULL)
12663 {
12664 EMSG(_("E133: :return not inside a function"));
12665 return;
12666 }
12667
12668 if (eap->skip)
12669 ++emsg_skip;
12670
12671 eap->nextcmd = NULL;
12672 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 {
12675 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 }
12680 /* It's safer to return also on error. */
12681 else if (!eap->skip)
12682 {
12683 /*
12684 * Return unless the expression evaluation has been cancelled due to an
12685 * aborting error, an interrupt, or an exception.
12686 */
12687 if (!aborting())
12688 returning = do_return(eap, FALSE, TRUE, NULL);
12689 }
12690
12691 /* When skipping or the return gets pending, advance to the next command
12692 * in this line (!returning). Otherwise, ignore the rest of the line.
12693 * Following lines will be ignored by get_func_line(). */
12694 if (returning)
12695 eap->nextcmd = NULL;
12696 else if (eap->nextcmd == NULL) /* no argument */
12697 eap->nextcmd = check_nextcmd(arg);
12698
12699 if (eap->skip)
12700 --emsg_skip;
12701}
12702
12703/*
12704 * Return from a function. Possibly makes the return pending. Also called
12705 * for a pending return at the ":endtry" or after returning from an extra
12706 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 * when called due to a ":return" command. "rettv" may point to a typeval
12708 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 * FALSE when the return gets pending.
12710 */
12711 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 exarg_T *eap;
12714 int reanimate;
12715 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
12718 int idx;
12719 struct condstack *cstack = eap->cstack;
12720
12721 if (reanimate)
12722 /* Undo the return. */
12723 current_funccal->returned = FALSE;
12724
12725 /*
12726 * Cleanup (and inactivate) conditionals, but stop when a try conditional
12727 * not in its finally clause (which then is to be executed next) is found.
12728 * In this case, make the ":return" pending for execution at the ":endtry".
12729 * Otherwise, return normally.
12730 */
12731 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
12732 if (idx >= 0)
12733 {
12734 cstack->cs_pending[idx] = CSTP_RETURN;
12735
12736 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 /* A pending return again gets pending. "rettv" points to an
12738 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 else
12742 {
12743 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 {
12750 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
12752 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 else
12754 EMSG(_(e_outofmem));
12755 }
12756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758
12759 if (reanimate)
12760 {
12761 /* The pending return value could be overwritten by a ":return"
12762 * without argument in a finally clause; reset the default
12763 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 current_funccal->rettv->v_type = VAR_NUMBER;
12765 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 }
12767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 }
12770 else
12771 {
12772 current_funccal->returned = TRUE;
12773
12774 /* If the return is carried out now, store the return value. For
12775 * a return immediately after reanimation, the value is already
12776 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779 clear_tv(current_funccal->rettv);
12780 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 }
12784 }
12785
12786 return idx < 0;
12787}
12788
12789/*
12790 * Free the variable with a pending return value.
12791 */
12792 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793discard_pending_return(rettv)
12794 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797}
12798
12799/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 * is an allocated string. Used by report_pending() for verbose messages.
12802 */
12803 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804get_return_cmd(rettv)
12805 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 char_u *s;
12808 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012809 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 if (rettv == NULL)
12812 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 else
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012814 s = tv2string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815
12816 STRCPY(IObuff, ":return ");
12817 STRNCPY(IObuff + 8, s, IOSIZE - 8);
12818 if (STRLEN(s) + 8 >= IOSIZE)
12819 STRCPY(IObuff + IOSIZE - 4, "...");
12820 vim_free(tofree);
12821 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822}
12823
12824/*
12825 * Get next function line.
12826 * Called by do_cmdline() to get the next line.
12827 * Returns allocated string, or NULL for end of function.
12828 */
12829/* ARGSUSED */
12830 char_u *
12831get_func_line(c, cookie, indent)
12832 int c; /* not used */
12833 void *cookie;
12834 int indent; /* not used */
12835{
12836 struct funccall *fcp = (struct funccall *)cookie;
12837 char_u *retval;
12838 garray_T *gap; /* growarray with function lines */
12839
12840 /* If breakpoints have been added/deleted need to check for it. */
12841 if (fcp->dbg_tick != debug_tick)
12842 {
12843 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
12844 sourcing_lnum);
12845 fcp->dbg_tick = debug_tick;
12846 }
12847
12848 gap = &fcp->func->lines;
12849 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
12850 retval = NULL;
12851 else if (fcp->returned || fcp->linenr >= gap->ga_len)
12852 retval = NULL;
12853 else
12854 {
12855 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
12856 sourcing_lnum = fcp->linenr;
12857 }
12858
12859 /* Did we encounter a breakpoint? */
12860 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
12861 {
12862 dbg_breakpoint(fcp->func->name, sourcing_lnum);
12863 /* Find next breakpoint. */
12864 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
12865 sourcing_lnum);
12866 fcp->dbg_tick = debug_tick;
12867 }
12868
12869 return retval;
12870}
12871
12872/*
12873 * Return TRUE if the currently active function should be ended, because a
12874 * return was encountered or an error occured. Used inside a ":while".
12875 */
12876 int
12877func_has_ended(cookie)
12878 void *cookie;
12879{
12880 struct funccall *fcp = (struct funccall *)cookie;
12881
12882 /* Ignore the "abort" flag if the abortion behavior has been changed due to
12883 * an error inside a try conditional. */
12884 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
12885 || fcp->returned);
12886}
12887
12888/*
12889 * return TRUE if cookie indicates a function which "abort"s on errors.
12890 */
12891 int
12892func_has_abort(cookie)
12893 void *cookie;
12894{
12895 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
12896}
12897
12898#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
12899typedef enum
12900{
12901 VAR_FLAVOUR_DEFAULT,
12902 VAR_FLAVOUR_SESSION,
12903 VAR_FLAVOUR_VIMINFO
12904} var_flavour_T;
12905
12906static var_flavour_T var_flavour __ARGS((char_u *varname));
12907
12908 static var_flavour_T
12909var_flavour(varname)
12910 char_u *varname;
12911{
12912 char_u *p = varname;
12913
12914 if (ASCII_ISUPPER(*p))
12915 {
12916 while (*(++p))
12917 if (ASCII_ISLOWER(*p))
12918 return VAR_FLAVOUR_SESSION;
12919 return VAR_FLAVOUR_VIMINFO;
12920 }
12921 else
12922 return VAR_FLAVOUR_DEFAULT;
12923}
12924#endif
12925
12926#if defined(FEAT_VIMINFO) || defined(PROTO)
12927/*
12928 * Restore global vars that start with a capital from the viminfo file
12929 */
12930 int
12931read_viminfo_varlist(virp, writing)
12932 vir_T *virp;
12933 int writing;
12934{
12935 char_u *tab;
12936 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012937 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938
12939 if (!writing && (find_viminfo_parameter('!') != NULL))
12940 {
12941 tab = vim_strchr(virp->vir_line + 1, '\t');
12942 if (tab != NULL)
12943 {
12944 *tab++ = '\0'; /* isolate the variable name */
12945 if (*tab == 'S') /* string var */
12946 is_string = TRUE;
12947
12948 tab = vim_strchr(tab, '\t');
12949 if (tab != NULL)
12950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 if (is_string)
12952 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012953 tv.v_type = VAR_STRING;
12954 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 }
12957 else
12958 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012959 tv.v_type = VAR_NUMBER;
12960 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012962 set_var(virp->vir_line + 1, &tv, FALSE);
12963 if (is_string)
12964 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 }
12966 }
12967 }
12968
12969 return viminfo_readline(virp);
12970}
12971
12972/*
12973 * Write global vars that start with a capital to the viminfo file
12974 */
12975 void
12976write_viminfo_varlist(fp)
12977 FILE *fp;
12978{
12979 garray_T *gap = &variables; /* global variable */
12980 VAR this_var;
12981 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 char *s;
12983 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012984 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985
12986 if (find_viminfo_parameter('!') == NULL)
12987 return;
12988
12989 fprintf(fp, _("\n# global variables:\n"));
12990 for (i = gap->ga_len; --i >= 0; )
12991 {
12992 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012993 if (this_var->v_name != NULL
12994 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 switch (this_var->tv.v_type)
12997 {
12998 case VAR_STRING: s = "STR"; break;
12999 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013000 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 }
13002 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013003 viminfo_writestring(fp, tv2string(&this_var->tv, &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013004 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 }
13006 }
13007}
13008#endif
13009
13010#if defined(FEAT_SESSION) || defined(PROTO)
13011 int
13012store_session_globals(fd)
13013 FILE *fd;
13014{
13015 garray_T *gap = &variables; /* global variable */
13016 VAR this_var;
13017 int i;
13018 char_u *p, *t;
13019
13020 for (i = gap->ga_len; --i >= 0; )
13021 {
13022 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013023 if (this_var->v_name != NULL
13024 && (this_var->tv.v_type == VAR_NUMBER
13025 || this_var->tv.v_type == VAR_STRING)
13026 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013028 /* Escape special characters with a backslash. Turn a LF and
13029 * CR into \n and \r. */
13030 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013032 if (p == NULL) /* out of memory */
13033 continue;
13034 for (t = p; *t != NUL; ++t)
13035 if (*t == '\n')
13036 *t = 'n';
13037 else if (*t == '\r')
13038 *t = 'r';
13039 if ((fprintf(fd, "let %s = %c%s%c",
13040 this_var->v_name,
13041 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
13042 p,
13043 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
13044 || put_eol(fd) == FAIL)
13045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013047 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013049 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 }
13051 }
13052 return OK;
13053}
13054#endif
13055
13056#endif /* FEAT_EVAL */
13057
13058#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
13059
13060
13061#ifdef WIN3264
13062/*
13063 * Functions for ":8" filename modifier: get 8.3 version of a filename.
13064 */
13065static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13066static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
13067static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13068
13069/*
13070 * Get the short pathname of a file.
13071 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
13072 */
13073 static int
13074get_short_pathname(fnamep, bufp, fnamelen)
13075 char_u **fnamep;
13076 char_u **bufp;
13077 int *fnamelen;
13078{
13079 int l,len;
13080 char_u *newbuf;
13081
13082 len = *fnamelen;
13083
13084 l = GetShortPathName(*fnamep, *fnamep, len);
13085 if (l > len - 1)
13086 {
13087 /* If that doesn't work (not enough space), then save the string
13088 * and try again with a new buffer big enough
13089 */
13090 newbuf = vim_strnsave(*fnamep, l);
13091 if (newbuf == NULL)
13092 return 0;
13093
13094 vim_free(*bufp);
13095 *fnamep = *bufp = newbuf;
13096
13097 l = GetShortPathName(*fnamep,*fnamep,l+1);
13098
13099 /* Really should always succeed, as the buffer is big enough */
13100 }
13101
13102 *fnamelen = l;
13103 return 1;
13104}
13105
13106/*
13107 * Create a short path name. Returns the length of the buffer it needs.
13108 * Doesn't copy over the end of the buffer passed in.
13109 */
13110 static int
13111shortpath_for_invalid_fname(fname, bufp, fnamelen)
13112 char_u **fname;
13113 char_u **bufp;
13114 int *fnamelen;
13115{
13116 char_u *s, *p, *pbuf2, *pbuf3;
13117 char_u ch;
13118 int l,len,len2,plen,slen;
13119
13120 /* Make a copy */
13121 len2 = *fnamelen;
13122 pbuf2 = vim_strnsave(*fname, len2);
13123 pbuf3 = NULL;
13124
13125 s = pbuf2 + len2 - 1; /* Find the end */
13126 slen = 1;
13127 plen = len2;
13128
13129 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013130 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 {
13132 --s;
13133 ++slen;
13134 --plen;
13135 }
13136
13137 do
13138 {
13139 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013140 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 {
13142 --s;
13143 ++slen;
13144 --plen;
13145 }
13146 if (s <= pbuf2)
13147 break;
13148
13149 /* Remeber the character that is about to be blatted */
13150 ch = *s;
13151 *s = 0; /* get_short_pathname requires a null-terminated string */
13152
13153 /* Try it in situ */
13154 p = pbuf2;
13155 if (!get_short_pathname(&p, &pbuf3, &plen))
13156 {
13157 vim_free(pbuf2);
13158 return -1;
13159 }
13160 *s = ch; /* Preserve the string */
13161 } while (plen == 0);
13162
13163 if (plen > 0)
13164 {
13165 /* Remeber the length of the new string. */
13166 *fnamelen = len = plen + slen;
13167 vim_free(*bufp);
13168 if (len > len2)
13169 {
13170 /* If there's not enough space in the currently allocated string,
13171 * then copy it to a buffer big enough.
13172 */
13173 *fname= *bufp = vim_strnsave(p, len);
13174 if (*fname == NULL)
13175 return -1;
13176 }
13177 else
13178 {
13179 /* Transfer pbuf2 to being the main buffer (it's big enough) */
13180 *fname = *bufp = pbuf2;
13181 if (p != pbuf2)
13182 strncpy(*fname, p, plen);
13183 pbuf2 = NULL;
13184 }
13185 /* Concat the next bit */
13186 strncpy(*fname + plen, s, slen);
13187 (*fname)[len] = '\0';
13188 }
13189 vim_free(pbuf3);
13190 vim_free(pbuf2);
13191 return 0;
13192}
13193
13194/*
13195 * Get a pathname for a partial path.
13196 */
13197 static int
13198shortpath_for_partial(fnamep, bufp, fnamelen)
13199 char_u **fnamep;
13200 char_u **bufp;
13201 int *fnamelen;
13202{
13203 int sepcount, len, tflen;
13204 char_u *p;
13205 char_u *pbuf, *tfname;
13206 int hasTilde;
13207
13208 /* Count up the path seperators from the RHS.. so we know which part
13209 * of the path to return.
13210 */
13211 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013212 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 if (vim_ispathsep(*p))
13214 ++sepcount;
13215
13216 /* Need full path first (use expand_env() to remove a "~/") */
13217 hasTilde = (**fnamep == '~');
13218 if (hasTilde)
13219 pbuf = tfname = expand_env_save(*fnamep);
13220 else
13221 pbuf = tfname = FullName_save(*fnamep, FALSE);
13222
13223 len = tflen = STRLEN(tfname);
13224
13225 if (!get_short_pathname(&tfname, &pbuf, &len))
13226 return -1;
13227
13228 if (len == 0)
13229 {
13230 /* Don't have a valid filename, so shorten the rest of the
13231 * path if we can. This CAN give us invalid 8.3 filenames, but
13232 * there's not a lot of point in guessing what it might be.
13233 */
13234 len = tflen;
13235 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
13236 return -1;
13237 }
13238
13239 /* Count the paths backward to find the beginning of the desired string. */
13240 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013241 {
13242#ifdef FEAT_MBYTE
13243 if (has_mbyte)
13244 p -= mb_head_off(tfname, p);
13245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 if (vim_ispathsep(*p))
13247 {
13248 if (sepcount == 0 || (hasTilde && sepcount == 1))
13249 break;
13250 else
13251 sepcount --;
13252 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 if (hasTilde)
13255 {
13256 --p;
13257 if (p >= tfname)
13258 *p = '~';
13259 else
13260 return -1;
13261 }
13262 else
13263 ++p;
13264
13265 /* Copy in the string - p indexes into tfname - allocated at pbuf */
13266 vim_free(*bufp);
13267 *fnamelen = (int)STRLEN(p);
13268 *bufp = pbuf;
13269 *fnamep = p;
13270
13271 return 0;
13272}
13273#endif /* WIN3264 */
13274
13275/*
13276 * Adjust a filename, according to a string of modifiers.
13277 * *fnamep must be NUL terminated when called. When returning, the length is
13278 * determined by *fnamelen.
13279 * Returns valid flags.
13280 * When there is an error, *fnamep is set to NULL.
13281 */
13282 int
13283modify_fname(src, usedlen, fnamep, bufp, fnamelen)
13284 char_u *src; /* string with modifiers */
13285 int *usedlen; /* characters after src that are used */
13286 char_u **fnamep; /* file name so far */
13287 char_u **bufp; /* buffer for allocated file name or NULL */
13288 int *fnamelen; /* length of fnamep */
13289{
13290 int valid = 0;
13291 char_u *tail;
13292 char_u *s, *p, *pbuf;
13293 char_u dirname[MAXPATHL];
13294 int c;
13295 int has_fullname = 0;
13296#ifdef WIN3264
13297 int has_shortname = 0;
13298#endif
13299
13300repeat:
13301 /* ":p" - full path/file_name */
13302 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
13303 {
13304 has_fullname = 1;
13305
13306 valid |= VALID_PATH;
13307 *usedlen += 2;
13308
13309 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
13310 if ((*fnamep)[0] == '~'
13311#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
13312 && ((*fnamep)[1] == '/'
13313# ifdef BACKSLASH_IN_FILENAME
13314 || (*fnamep)[1] == '\\'
13315# endif
13316 || (*fnamep)[1] == NUL)
13317
13318#endif
13319 )
13320 {
13321 *fnamep = expand_env_save(*fnamep);
13322 vim_free(*bufp); /* free any allocated file name */
13323 *bufp = *fnamep;
13324 if (*fnamep == NULL)
13325 return -1;
13326 }
13327
13328 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013329 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 {
13331 if (vim_ispathsep(*p)
13332 && p[1] == '.'
13333 && (p[2] == NUL
13334 || vim_ispathsep(p[2])
13335 || (p[2] == '.'
13336 && (p[3] == NUL || vim_ispathsep(p[3])))))
13337 break;
13338 }
13339
13340 /* FullName_save() is slow, don't use it when not needed. */
13341 if (*p != NUL || !vim_isAbsName(*fnamep))
13342 {
13343 *fnamep = FullName_save(*fnamep, *p != NUL);
13344 vim_free(*bufp); /* free any allocated file name */
13345 *bufp = *fnamep;
13346 if (*fnamep == NULL)
13347 return -1;
13348 }
13349
13350 /* Append a path separator to a directory. */
13351 if (mch_isdir(*fnamep))
13352 {
13353 /* Make room for one or two extra characters. */
13354 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
13355 vim_free(*bufp); /* free any allocated file name */
13356 *bufp = *fnamep;
13357 if (*fnamep == NULL)
13358 return -1;
13359 add_pathsep(*fnamep);
13360 }
13361 }
13362
13363 /* ":." - path relative to the current directory */
13364 /* ":~" - path relative to the home directory */
13365 /* ":8" - shortname path - postponed till after */
13366 while (src[*usedlen] == ':'
13367 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
13368 {
13369 *usedlen += 2;
13370 if (c == '8')
13371 {
13372#ifdef WIN3264
13373 has_shortname = 1; /* Postpone this. */
13374#endif
13375 continue;
13376 }
13377 pbuf = NULL;
13378 /* Need full path first (use expand_env() to remove a "~/") */
13379 if (!has_fullname)
13380 {
13381 if (c == '.' && **fnamep == '~')
13382 p = pbuf = expand_env_save(*fnamep);
13383 else
13384 p = pbuf = FullName_save(*fnamep, FALSE);
13385 }
13386 else
13387 p = *fnamep;
13388
13389 has_fullname = 0;
13390
13391 if (p != NULL)
13392 {
13393 if (c == '.')
13394 {
13395 mch_dirname(dirname, MAXPATHL);
13396 s = shorten_fname(p, dirname);
13397 if (s != NULL)
13398 {
13399 *fnamep = s;
13400 if (pbuf != NULL)
13401 {
13402 vim_free(*bufp); /* free any allocated file name */
13403 *bufp = pbuf;
13404 pbuf = NULL;
13405 }
13406 }
13407 }
13408 else
13409 {
13410 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
13411 /* Only replace it when it starts with '~' */
13412 if (*dirname == '~')
13413 {
13414 s = vim_strsave(dirname);
13415 if (s != NULL)
13416 {
13417 *fnamep = s;
13418 vim_free(*bufp);
13419 *bufp = s;
13420 }
13421 }
13422 }
13423 vim_free(pbuf);
13424 }
13425 }
13426
13427 tail = gettail(*fnamep);
13428 *fnamelen = (int)STRLEN(*fnamep);
13429
13430 /* ":h" - head, remove "/file_name", can be repeated */
13431 /* Don't remove the first "/" or "c:\" */
13432 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
13433 {
13434 valid |= VALID_HEAD;
13435 *usedlen += 2;
13436 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013437 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 --tail;
13439 *fnamelen = (int)(tail - *fnamep);
13440#ifdef VMS
13441 if (*fnamelen > 0)
13442 *fnamelen += 1; /* the path separator is part of the path */
13443#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013444 while (tail > s && !after_pathsep(s, tail))
13445 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 }
13447
13448 /* ":8" - shortname */
13449 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
13450 {
13451 *usedlen += 2;
13452#ifdef WIN3264
13453 has_shortname = 1;
13454#endif
13455 }
13456
13457#ifdef WIN3264
13458 /* Check shortname after we have done 'heads' and before we do 'tails'
13459 */
13460 if (has_shortname)
13461 {
13462 pbuf = NULL;
13463 /* Copy the string if it is shortened by :h */
13464 if (*fnamelen < (int)STRLEN(*fnamep))
13465 {
13466 p = vim_strnsave(*fnamep, *fnamelen);
13467 if (p == 0)
13468 return -1;
13469 vim_free(*bufp);
13470 *bufp = *fnamep = p;
13471 }
13472
13473 /* Split into two implementations - makes it easier. First is where
13474 * there isn't a full name already, second is where there is.
13475 */
13476 if (!has_fullname && !vim_isAbsName(*fnamep))
13477 {
13478 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
13479 return -1;
13480 }
13481 else
13482 {
13483 int l;
13484
13485 /* Simple case, already have the full-name
13486 * Nearly always shorter, so try first time. */
13487 l = *fnamelen;
13488 if (!get_short_pathname(fnamep, bufp, &l))
13489 return -1;
13490
13491 if (l == 0)
13492 {
13493 /* Couldn't find the filename.. search the paths.
13494 */
13495 l = *fnamelen;
13496 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
13497 return -1;
13498 }
13499 *fnamelen = l;
13500 }
13501 }
13502#endif /* WIN3264 */
13503
13504 /* ":t" - tail, just the basename */
13505 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
13506 {
13507 *usedlen += 2;
13508 *fnamelen -= (int)(tail - *fnamep);
13509 *fnamep = tail;
13510 }
13511
13512 /* ":e" - extension, can be repeated */
13513 /* ":r" - root, without extension, can be repeated */
13514 while (src[*usedlen] == ':'
13515 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
13516 {
13517 /* find a '.' in the tail:
13518 * - for second :e: before the current fname
13519 * - otherwise: The last '.'
13520 */
13521 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
13522 s = *fnamep - 2;
13523 else
13524 s = *fnamep + *fnamelen - 1;
13525 for ( ; s > tail; --s)
13526 if (s[0] == '.')
13527 break;
13528 if (src[*usedlen + 1] == 'e') /* :e */
13529 {
13530 if (s > tail)
13531 {
13532 *fnamelen += (int)(*fnamep - (s + 1));
13533 *fnamep = s + 1;
13534#ifdef VMS
13535 /* cut version from the extension */
13536 s = *fnamep + *fnamelen - 1;
13537 for ( ; s > *fnamep; --s)
13538 if (s[0] == ';')
13539 break;
13540 if (s > *fnamep)
13541 *fnamelen = s - *fnamep;
13542#endif
13543 }
13544 else if (*fnamep <= tail)
13545 *fnamelen = 0;
13546 }
13547 else /* :r */
13548 {
13549 if (s > tail) /* remove one extension */
13550 *fnamelen = (int)(s - *fnamep);
13551 }
13552 *usedlen += 2;
13553 }
13554
13555 /* ":s?pat?foo?" - substitute */
13556 /* ":gs?pat?foo?" - global substitute */
13557 if (src[*usedlen] == ':'
13558 && (src[*usedlen + 1] == 's'
13559 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
13560 {
13561 char_u *str;
13562 char_u *pat;
13563 char_u *sub;
13564 int sep;
13565 char_u *flags;
13566 int didit = FALSE;
13567
13568 flags = (char_u *)"";
13569 s = src + *usedlen + 2;
13570 if (src[*usedlen + 1] == 'g')
13571 {
13572 flags = (char_u *)"g";
13573 ++s;
13574 }
13575
13576 sep = *s++;
13577 if (sep)
13578 {
13579 /* find end of pattern */
13580 p = vim_strchr(s, sep);
13581 if (p != NULL)
13582 {
13583 pat = vim_strnsave(s, (int)(p - s));
13584 if (pat != NULL)
13585 {
13586 s = p + 1;
13587 /* find end of substitution */
13588 p = vim_strchr(s, sep);
13589 if (p != NULL)
13590 {
13591 sub = vim_strnsave(s, (int)(p - s));
13592 str = vim_strnsave(*fnamep, *fnamelen);
13593 if (sub != NULL && str != NULL)
13594 {
13595 *usedlen = (int)(p + 1 - src);
13596 s = do_string_sub(str, pat, sub, flags);
13597 if (s != NULL)
13598 {
13599 *fnamep = s;
13600 *fnamelen = (int)STRLEN(s);
13601 vim_free(*bufp);
13602 *bufp = s;
13603 didit = TRUE;
13604 }
13605 }
13606 vim_free(sub);
13607 vim_free(str);
13608 }
13609 vim_free(pat);
13610 }
13611 }
13612 /* after using ":s", repeat all the modifiers */
13613 if (didit)
13614 goto repeat;
13615 }
13616 }
13617
13618 return valid;
13619}
13620
13621/*
13622 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
13623 * "flags" can be "g" to do a global substitute.
13624 * Returns an allocated string, NULL for error.
13625 */
13626 char_u *
13627do_string_sub(str, pat, sub, flags)
13628 char_u *str;
13629 char_u *pat;
13630 char_u *sub;
13631 char_u *flags;
13632{
13633 int sublen;
13634 regmatch_T regmatch;
13635 int i;
13636 int do_all;
13637 char_u *tail;
13638 garray_T ga;
13639 char_u *ret;
13640 char_u *save_cpo;
13641
13642 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
13643 save_cpo = p_cpo;
13644 p_cpo = (char_u *)"";
13645
13646 ga_init2(&ga, 1, 200);
13647
13648 do_all = (flags[0] == 'g');
13649
13650 regmatch.rm_ic = p_ic;
13651 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13652 if (regmatch.regprog != NULL)
13653 {
13654 tail = str;
13655 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
13656 {
13657 /*
13658 * Get some space for a temporary buffer to do the substitution
13659 * into. It will contain:
13660 * - The text up to where the match is.
13661 * - The substituted text.
13662 * - The text after the match.
13663 */
13664 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
13665 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
13666 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
13667 {
13668 ga_clear(&ga);
13669 break;
13670 }
13671
13672 /* copy the text up to where the match is */
13673 i = (int)(regmatch.startp[0] - tail);
13674 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
13675 /* add the substituted text */
13676 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
13677 + ga.ga_len + i, TRUE, TRUE, FALSE);
13678 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 /* avoid getting stuck on a match with an empty string */
13680 if (tail == regmatch.endp[0])
13681 {
13682 if (*tail == NUL)
13683 break;
13684 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
13685 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 }
13687 else
13688 {
13689 tail = regmatch.endp[0];
13690 if (*tail == NUL)
13691 break;
13692 }
13693 if (!do_all)
13694 break;
13695 }
13696
13697 if (ga.ga_data != NULL)
13698 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
13699
13700 vim_free(regmatch.regprog);
13701 }
13702
13703 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
13704 ga_clear(&ga);
13705 p_cpo = save_cpo;
13706
13707 return ret;
13708}
13709
13710#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */