blob: 82d1c69c5272c2592f33f02fb0c6450d48b91c8a [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/*
85 * Structure to hold the info about a list.
86 */
87struct listvar_S
88{
89 int lv_refcount; /* reference count */
90 listitem *lv_first; /* first item, NULL if none */
91 listitem *lv_last; /* last item, NULL if none */
92};
93
94typedef struct listvar_S listvar;
95
96#define VAR_LIST_MAXNEST 100 /* maximum nesting of lists */
97static char_u *e_listidx = (char_u *)N_("E999: list index out of range: %ld");
98
99/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 * All user-defined global variables are stored in "variables".
101 */
102garray_T variables = {0, 0, sizeof(var), 4, NULL};
103
104/*
105 * Array to hold an array with variables local to each sourced script.
106 */
107static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
108#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
109
110
111#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
112#define VAR_GAP_ENTRY(idx, gap) (((VAR)(gap->ga_data))[idx])
113#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
114#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
115
116static int echo_attr = 0; /* attributes used for ":echo" */
117
118/*
119 * Structure to hold info for a user function.
120 */
121typedef struct ufunc ufunc_T;
122
123struct ufunc
124{
125 ufunc_T *next; /* next function in list */
126 char_u *name; /* name of function; can start with <SNR>123_
127 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
128 int varargs; /* variable nr of arguments */
129 int flags;
130 int calls; /* nr of active calls */
131 garray_T args; /* arguments */
132 garray_T lines; /* function lines */
133 scid_T script_ID; /* ID of script where function was defined,
134 used for s: variables */
135};
136
137/* function flags */
138#define FC_ABORT 1 /* abort function on error */
139#define FC_RANGE 2 /* function accepts range */
140
141/*
142 * All user-defined functions are found in the forward-linked function list.
143 * The first function is pointed at by firstfunc.
144 */
145ufunc_T *firstfunc = NULL;
146
147#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
148#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
149
150/* structure to hold info for a function that is currently being executed. */
151struct funccall
152{
153 ufunc_T *func; /* function being called */
154 int linenr; /* next line to be executed */
155 int returned; /* ":return" used */
156 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000157 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 var a0_var; /* "a:0" variable */
159 var firstline; /* "a:firstline" variable */
160 var lastline; /* "a:lastline" variable */
161 garray_T l_vars; /* local function variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000162 typeval *retvar; /* return value variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 linenr_T breakpoint; /* next line with breakpoint or zero */
164 int dbg_tick; /* debug_tick when breakpoint was set */
165 int level; /* top nesting level of executed function */
166};
167
168/*
169 * Return the name of the executed function.
170 */
171 char_u *
172func_name(cookie)
173 void *cookie;
174{
175 return ((struct funccall *)cookie)->func->name;
176}
177
178/*
179 * Return the address holding the next breakpoint line for a funccall cookie.
180 */
181 linenr_T *
182func_breakpoint(cookie)
183 void *cookie;
184{
185 return &((struct funccall *)cookie)->breakpoint;
186}
187
188/*
189 * Return the address holding the debug tick for a funccall cookie.
190 */
191 int *
192func_dbg_tick(cookie)
193 void *cookie;
194{
195 return &((struct funccall *)cookie)->dbg_tick;
196}
197
198/*
199 * Return the nesting level for a funccall cookie.
200 */
201 int
202func_level(cookie)
203 void *cookie;
204{
205 return ((struct funccall *)cookie)->level;
206}
207
208/* pointer to funccal for currently active function */
209struct funccall *current_funccal = NULL;
210
211/*
212 * Return TRUE when a function was ended by a ":return" command.
213 */
214 int
215current_func_returned()
216{
217 return current_funccal->returned;
218}
219
220
221/*
222 * Array to hold the value of v: variables.
223 */
224#include "version.h"
225
226/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000227#define VV_COMPAT 1 /* compatible, also used without "v:" */
228#define VV_RO 2 /* read-only */
229#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231struct vimvar
232{
233 char *name; /* name of variable, without v: */
234 int len; /* length of name */
235 char_u *val; /* current value (can also be a number!) */
236 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000237 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238} vimvars[VV_LEN] =
239{ /* The order here must match the VV_ defines in vim.h! */
240 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
241 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
242 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
243 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
244 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
245 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
246 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
247 VV_COMPAT+VV_RO},
248 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
249 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
250 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000251 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
253 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
254 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
255 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
256 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
257 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
258 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
259 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
260 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
261 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
262 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
263 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000264 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
265 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
266 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
267 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
269 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
270 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
271 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
272 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
273 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
274 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000275 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276};
277
278static int eval0 __ARGS((char_u *arg, VAR retvar, char_u **nextcmd, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000279static int eval1 __ARGS((char_u **arg, typeval *retvar, int evaluate));
280static int eval2 __ARGS((char_u **arg, typeval *retvar, int evaluate));
281static int eval3 __ARGS((char_u **arg, typeval *retvar, int evaluate));
282static int eval4 __ARGS((char_u **arg, typeval *retvar, int evaluate));
283static int eval5 __ARGS((char_u **arg, typeval *retvar, int evaluate));
284static int eval6 __ARGS((char_u **arg, typeval *retvar, int evaluate));
285static int eval7 __ARGS((char_u **arg, typeval *retvar, int evaluate));
286static int eval_index __ARGS((char_u **arg, typeval *retvar, int evaluate));
287static int get_option_vartv __ARGS((char_u **arg, typeval *retvar, int evaluate));
288static int get_string_vartv __ARGS((char_u **arg, typeval *retvar, int evaluate));
289static int get_lit_string_vartv __ARGS((char_u **arg, typeval *retvar, int evaluate));
290static int get_list_vartv __ARGS((char_u **arg, typeval *retvar, int evaluate));
291static listvar *list_alloc __ARGS((void));
292static void list_unref __ARGS((listvar *l));
293static void list_free __ARGS((listvar *l));
294static listitem *listitem_alloc __ARGS((void));
295static void listitem_free __ARGS((listitem *item));
296static long list_len __ARGS((listvar *l));
297static listitem *list_find __ARGS((listvar *l, long n));
298static void list_append __ARGS((listvar *l, listitem *item));
299static int list_append_tv __ARGS((listvar *l, typeval *tv));
300static listvar *list_copy __ARGS((listvar *orig, int deep));
301static listitem *list_getrem __ARGS((listvar *l, long n));
302static char_u *list2string __ARGS((typeval *tv));
303static char_u *tv2string __ARGS((typeval *tv, char_u **tofree));
304static int get_env_vartv __ARGS((char_u **arg, typeval *retvar, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000306static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
307static int get_func_vartv __ARGS((char_u *name, int len, typeval *retvar, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
308static int call_func __ARGS((char_u *name, int len, typeval *retvar, int argcount, typeval *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
309static void f_append __ARGS((typeval *argvars, typeval *retvar));
310static void f_argc __ARGS((typeval *argvars, typeval *retvar));
311static void f_argidx __ARGS((typeval *argvars, typeval *retvar));
312static void f_argv __ARGS((typeval *argvars, typeval *retvar));
313static void f_browse __ARGS((typeval *argvars, typeval *retvar));
314static void f_browsedir __ARGS((typeval *argvars, typeval *retvar));
315static buf_T *find_buffer __ARGS((typeval *avar));
316static void f_bufexists __ARGS((typeval *argvars, typeval *retvar));
317static void f_buflisted __ARGS((typeval *argvars, typeval *retvar));
318static void f_bufloaded __ARGS((typeval *argvars, typeval *retvar));
319static buf_T *get_buf_vartv __ARGS((typeval *avar));
320static void f_bufname __ARGS((typeval *argvars, typeval *retvar));
321static void f_bufnr __ARGS((typeval *argvars, typeval *retvar));
322static void f_bufwinnr __ARGS((typeval *argvars, typeval *retvar));
323static void f_byte2line __ARGS((typeval *argvars, typeval *retvar));
324static void f_byteidx __ARGS((typeval *argvars, typeval *retvar));
325static void f_char2nr __ARGS((typeval *argvars, typeval *retvar));
326static void f_cindent __ARGS((typeval *argvars, typeval *retvar));
327static void f_col __ARGS((typeval *argvars, typeval *retvar));
328static void f_confirm __ARGS((typeval *argvars, typeval *retvar));
329static void f_copy __ARGS((typeval *argvars, typeval *retvar));
330static void f_cscope_connection __ARGS((typeval *argvars, typeval *retvar));
331static void f_cursor __ARGS((typeval *argsvars, typeval *retvar));
332static void f_deepcopy __ARGS((typeval *argvars, typeval *retvar));
333static void f_delete __ARGS((typeval *argvars, typeval *retvar));
334static void f_did_filetype __ARGS((typeval *argvars, typeval *retvar));
335static void f_diff_filler __ARGS((typeval *argvars, typeval *retvar));
336static void f_diff_hlID __ARGS((typeval *argvars, typeval *retvar));
337static void f_escape __ARGS((typeval *argvars, typeval *retvar));
338static void f_eventhandler __ARGS((typeval *argvars, typeval *retvar));
339static void f_executable __ARGS((typeval *argvars, typeval *retvar));
340static void f_exists __ARGS((typeval *argvars, typeval *retvar));
341static void f_expand __ARGS((typeval *argvars, typeval *retvar));
342static void f_filereadable __ARGS((typeval *argvars, typeval *retvar));
343static void f_filewritable __ARGS((typeval *argvars, typeval *retvar));
344static void f_finddir __ARGS((typeval *argvars, typeval *retvar));
345static void f_findfile __ARGS((typeval *argvars, typeval *retvar));
346static void f_findfilendir __ARGS((typeval *argvars, typeval *retvar, int dir));
347static void f_fnamemodify __ARGS((typeval *argvars, typeval *retvar));
348static void f_foldclosed __ARGS((typeval *argvars, typeval *retvar));
349static void f_foldclosedend __ARGS((typeval *argvars, typeval *retvar));
350static void foldclosed_both __ARGS((typeval *argvars, typeval *retvar, int end));
351static void f_foldlevel __ARGS((typeval *argvars, typeval *retvar));
352static void f_foldtext __ARGS((typeval *argvars, typeval *retvar));
353static void f_foldtextresult __ARGS((typeval *argvars, typeval *retvar));
354static void f_foreground __ARGS((typeval *argvars, typeval *retvar));
355static void f_function __ARGS((typeval *argvars, typeval *retvar));
356static void f_getbufvar __ARGS((typeval *argvars, typeval *retvar));
357static void f_getchar __ARGS((typeval *argvars, typeval *retvar));
358static void f_getcharmod __ARGS((typeval *argvars, typeval *retvar));
359static void f_getcmdline __ARGS((typeval *argvars, typeval *retvar));
360static void f_getcmdpos __ARGS((typeval *argvars, typeval *retvar));
361static void f_getcwd __ARGS((typeval *argvars, typeval *retvar));
362static void f_getfontname __ARGS((typeval *argvars, typeval *retvar));
363static void f_getfperm __ARGS((typeval *argvars, typeval *retvar));
364static void f_getfsize __ARGS((typeval *argvars, typeval *retvar));
365static void f_getftime __ARGS((typeval *argvars, typeval *retvar));
366static void f_getftype __ARGS((typeval *argvars, typeval *retvar));
367static void f_getline __ARGS((typeval *argvars, typeval *retvar));
368static void f_getreg __ARGS((typeval *argvars, typeval *retvar));
369static void f_getregtype __ARGS((typeval *argvars, typeval *retvar));
370static void f_getwinposx __ARGS((typeval *argvars, typeval *retvar));
371static void f_getwinposy __ARGS((typeval *argvars, typeval *retvar));
372static void f_getwinvar __ARGS((typeval *argvars, typeval *retvar));
373static void f_glob __ARGS((typeval *argvars, typeval *retvar));
374static void f_globpath __ARGS((typeval *argvars, typeval *retvar));
375static void f_has __ARGS((typeval *argvars, typeval *retvar));
376static void f_hasmapto __ARGS((typeval *argvars, typeval *retvar));
377static void f_histadd __ARGS((typeval *argvars, typeval *retvar));
378static void f_histdel __ARGS((typeval *argvars, typeval *retvar));
379static void f_histget __ARGS((typeval *argvars, typeval *retvar));
380static void f_histnr __ARGS((typeval *argvars, typeval *retvar));
381static void f_hlexists __ARGS((typeval *argvars, typeval *retvar));
382static void f_hlID __ARGS((typeval *argvars, typeval *retvar));
383static void f_hostname __ARGS((typeval *argvars, typeval *retvar));
384static void f_iconv __ARGS((typeval *argvars, typeval *retvar));
385static void f_indent __ARGS((typeval *argvars, typeval *retvar));
386static void f_insert __ARGS((typeval *argvars, typeval *retvar));
387static void f_isdirectory __ARGS((typeval *argvars, typeval *retvar));
388static void f_input __ARGS((typeval *argvars, typeval *retvar));
389static void f_inputdialog __ARGS((typeval *argvars, typeval *retvar));
390static void f_inputrestore __ARGS((typeval *argvars, typeval *retvar));
391static void f_inputsave __ARGS((typeval *argvars, typeval *retvar));
392static void f_inputsecret __ARGS((typeval *argvars, typeval *retvar));
393static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *retvar));
394static void f_len __ARGS((typeval *argvars, typeval *retvar));
395static void f_libcall __ARGS((typeval *argvars, typeval *retvar));
396static void f_libcallnr __ARGS((typeval *argvars, typeval *retvar));
397static void libcall_common __ARGS((typeval *argvars, typeval *retvar, int type));
398static void f_line __ARGS((typeval *argvars, typeval *retvar));
399static void f_line2byte __ARGS((typeval *argvars, typeval *retvar));
400static void f_lispindent __ARGS((typeval *argvars, typeval *retvar));
401static void f_localtime __ARGS((typeval *argvars, typeval *retvar));
402static void f_maparg __ARGS((typeval *argvars, typeval *retvar));
403static void f_mapcheck __ARGS((typeval *argvars, typeval *retvar));
404static void get_maparg __ARGS((typeval *argvars, typeval *retvar, int exact));
405static void f_match __ARGS((typeval *argvars, typeval *retvar));
406static void f_matchend __ARGS((typeval *argvars, typeval *retvar));
407static void f_matchstr __ARGS((typeval *argvars, typeval *retvar));
408static void f_mode __ARGS((typeval *argvars, typeval *retvar));
409static void f_nextnonblank __ARGS((typeval *argvars, typeval *retvar));
410static void f_nr2char __ARGS((typeval *argvars, typeval *retvar));
411static void f_prevnonblank __ARGS((typeval *argvars, typeval *retvar));
412static void f_setbufvar __ARGS((typeval *argvars, typeval *retvar));
413static void f_setcmdpos __ARGS((typeval *argvars, typeval *retvar));
414static void f_setwinvar __ARGS((typeval *argvars, typeval *retvar));
415static void f_remove __ARGS((typeval *argvars, typeval *retvar));
416static void f_rename __ARGS((typeval *argvars, typeval *retvar));
417static void f_resolve __ARGS((typeval *argvars, typeval *retvar));
418static void f_search __ARGS((typeval *argvars, typeval *retvar));
419static void f_searchpair __ARGS((typeval *argvars, typeval *retvar));
420static int get_search_arg __ARGS((typeval *varp, int *flagsp));
421static void f_remote_expr __ARGS((typeval *argvars, typeval *retvar));
422static void f_remote_foreground __ARGS((typeval *argvars, typeval *retvar));
423static void f_remote_peek __ARGS((typeval *argvars, typeval *retvar));
424static void f_remote_read __ARGS((typeval *argvars, typeval *retvar));
425static void f_remote_send __ARGS((typeval *argvars, typeval *retvar));
426static void f_repeat __ARGS((typeval *argvars, typeval *retvar));
427static void f_server2client __ARGS((typeval *argvars, typeval *retvar));
428static void f_serverlist __ARGS((typeval *argvars, typeval *retvar));
429static void f_setline __ARGS((typeval *argvars, typeval *retvar));
430static void f_setreg __ARGS((typeval *argvars, typeval *retvar));
431static void f_simplify __ARGS((typeval *argvars, typeval *retvar));
432static void find_some_match __ARGS((typeval *argvars, typeval *retvar, int start));
433static void f_strftime __ARGS((typeval *argvars, typeval *retvar));
434static void f_stridx __ARGS((typeval *argvars, typeval *retvar));
435static void f_string __ARGS((typeval *argvars, typeval *retvar));
436static void f_strlen __ARGS((typeval *argvars, typeval *retvar));
437static void f_strpart __ARGS((typeval *argvars, typeval *retvar));
438static void f_strridx __ARGS((typeval *argvars, typeval *retvar));
439static void f_strtrans __ARGS((typeval *argvars, typeval *retvar));
440static void f_synID __ARGS((typeval *argvars, typeval *retvar));
441static void f_synIDattr __ARGS((typeval *argvars, typeval *retvar));
442static void f_synIDtrans __ARGS((typeval *argvars, typeval *retvar));
443static void f_system __ARGS((typeval *argvars, typeval *retvar));
444static void f_submatch __ARGS((typeval *argvars, typeval *retvar));
445static void f_substitute __ARGS((typeval *argvars, typeval *retvar));
446static void f_tempname __ARGS((typeval *argvars, typeval *retvar));
447static void f_tolower __ARGS((typeval *argvars, typeval *retvar));
448static void f_toupper __ARGS((typeval *argvars, typeval *retvar));
449static void f_tr __ARGS((typeval *argvars, typeval *retvar));
450static void f_type __ARGS((typeval *argvars, typeval *retvar));
451static void f_virtcol __ARGS((typeval *argvars, typeval *retvar));
452static void f_visualmode __ARGS((typeval *argvars, typeval *retvar));
453static void f_winbufnr __ARGS((typeval *argvars, typeval *retvar));
454static void f_wincol __ARGS((typeval *argvars, typeval *retvar));
455static void f_winheight __ARGS((typeval *argvars, typeval *retvar));
456static void f_winline __ARGS((typeval *argvars, typeval *retvar));
457static void f_winnr __ARGS((typeval *argvars, typeval *retvar));
458static void f_winrestcmd __ARGS((typeval *argvars, typeval *retvar));
459static void f_winwidth __ARGS((typeval *argvars, typeval *retvar));
460static win_T *find_win_by_nr __ARGS((typeval *vp));
461static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462static int get_env_len __ARGS((char_u **arg));
463static int get_id_len __ARGS((char_u **arg));
464static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
465static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end));
466static int eval_isnamec __ARGS((int c));
467static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000468static int get_var_vartv __ARGS((char_u *name, int len, typeval *retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469static VAR alloc_var __ARGS((void));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000470static typeval *alloc_vartv __ARGS((void));
471static typeval *alloc_string_vartv __ARGS((char_u *string));
472static void free_vartv __ARGS((typeval *varp));
473static void clear_vartv __ARGS((typeval *varp));
474static long get_vartv_number __ARGS((typeval *varp));
475static linenr_T get_vartv_lnum __ARGS((typeval *argvars));
476static char_u *get_vartv_string __ARGS((typeval *varp));
477static char_u *get_vartv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478static VAR find_var __ARGS((char_u *name, int writing));
479static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
480static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000481static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482static void list_one_var __ARGS((VAR v, char_u *prefix));
483static void list_vim_var __ARGS((int i));
484static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000485static void set_var __ARGS((char_u *name, typeval *varp, int copy));
486static void copy_vartv __ARGS((typeval *from, typeval *to));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
488static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
489static int eval_fname_script __ARGS((char_u *p));
490static int eval_fname_sid __ARGS((char_u *p));
491static void list_func_head __ARGS((ufunc_T *fp, int indent));
492static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
493static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000494static int function_exists __ARGS((char_u *name));
495static void call_user_func __ARGS((ufunc_T *fp, int argcount, typeval *argvars, typeval *retvar, linenr_T firstline, linenr_T lastline));
496
497#define get_var_string(p) get_vartv_string(&(p)->tv)
498#define get_var_string_buf(p, b) get_vartv_string_buf(&(p)->tv, (b))
499#define get_var_number(p) get_vartv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501/* Magic braces are always enabled, otherwise Vim scripts would not be
502 * portable. */
503#define FEAT_MAGIC_BRACES
504
505#ifdef FEAT_MAGIC_BRACES
506static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
507#endif
508
509/*
510 * Set an internal variable to a string value. Creates the variable if it does
511 * not already exist.
512 */
513 void
514set_internal_string_var(name, value)
515 char_u *name;
516 char_u *value;
517{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000518 char_u *val;
519 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
521 val = vim_strsave(value);
522 if (val != NULL)
523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000524 tvp = alloc_string_vartv(val);
525 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000527 set_var(name, tvp, FALSE);
528 free_vartv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530 }
531}
532
533# if defined(FEAT_MBYTE) || defined(PROTO)
534 int
535eval_charconvert(enc_from, enc_to, fname_from, fname_to)
536 char_u *enc_from;
537 char_u *enc_to;
538 char_u *fname_from;
539 char_u *fname_to;
540{
541 int err = FALSE;
542
543 set_vim_var_string(VV_CC_FROM, enc_from, -1);
544 set_vim_var_string(VV_CC_TO, enc_to, -1);
545 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
546 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
547 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
548 err = TRUE;
549 set_vim_var_string(VV_CC_FROM, NULL, -1);
550 set_vim_var_string(VV_CC_TO, NULL, -1);
551 set_vim_var_string(VV_FNAME_IN, NULL, -1);
552 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
553
554 if (err)
555 return FAIL;
556 return OK;
557}
558# endif
559
560# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
561 int
562eval_printexpr(fname, args)
563 char_u *fname;
564 char_u *args;
565{
566 int err = FALSE;
567
568 set_vim_var_string(VV_FNAME_IN, fname, -1);
569 set_vim_var_string(VV_CMDARG, args, -1);
570 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
571 err = TRUE;
572 set_vim_var_string(VV_FNAME_IN, NULL, -1);
573 set_vim_var_string(VV_CMDARG, NULL, -1);
574
575 if (err)
576 {
577 mch_remove(fname);
578 return FAIL;
579 }
580 return OK;
581}
582# endif
583
584# if defined(FEAT_DIFF) || defined(PROTO)
585 void
586eval_diff(origfile, newfile, outfile)
587 char_u *origfile;
588 char_u *newfile;
589 char_u *outfile;
590{
591 int err = FALSE;
592
593 set_vim_var_string(VV_FNAME_IN, origfile, -1);
594 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
595 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
596 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
597 set_vim_var_string(VV_FNAME_IN, NULL, -1);
598 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600}
601
602 void
603eval_patch(origfile, difffile, outfile)
604 char_u *origfile;
605 char_u *difffile;
606 char_u *outfile;
607{
608 int err;
609
610 set_vim_var_string(VV_FNAME_IN, origfile, -1);
611 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
612 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
613 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
614 set_vim_var_string(VV_FNAME_IN, NULL, -1);
615 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
616 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
617}
618# endif
619
620/*
621 * Top level evaluation function, returning a boolean.
622 * Sets "error" to TRUE if there was an error.
623 * Return TRUE or FALSE.
624 */
625 int
626eval_to_bool(arg, error, nextcmd, skip)
627 char_u *arg;
628 int *error;
629 char_u **nextcmd;
630 int skip; /* only parse, don't execute */
631{
632 var retvar;
633 int retval = FALSE;
634
635 if (skip)
636 ++emsg_skip;
637 if (eval0(arg, &retvar, nextcmd, !skip) == FAIL)
638 {
639 *error = TRUE;
640 }
641 else
642 {
643 *error = FALSE;
644 if (!skip)
645 {
646 retval = (get_var_number(&retvar) != 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000647 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 if (skip)
651 --emsg_skip;
652
653 return retval;
654}
655
656/*
657 * Top level evaluation function, returning a string. If "skip" is TRUE,
658 * only parsing to "nextcmd" is done, without reporting errors. Return
659 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
660 */
661 char_u *
662eval_to_string_skip(arg, nextcmd, skip)
663 char_u *arg;
664 char_u **nextcmd;
665 int skip; /* only parse, don't execute */
666{
667 var retvar;
668 char_u *retval;
669
670 if (skip)
671 ++emsg_skip;
672 if (eval0(arg, &retvar, nextcmd, !skip) == FAIL || skip)
673 retval = NULL;
674 else
675 {
676 retval = vim_strsave(get_var_string(&retvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000677 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 if (skip)
680 --emsg_skip;
681
682 return retval;
683}
684
685/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000686 * Skip over an expression at "*pp".
687 * Return FAIL for an error, OK otherwise.
688 */
689 int
690skip_expr(pp)
691 char_u **pp;
692{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000693 typeval retvar;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000694
695 *pp = skipwhite(*pp);
696 return eval1(pp, &retvar, FALSE);
697}
698
699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 * Top level evaluation function, returning a string.
701 * Return pointer to allocated memory, or NULL for failure.
702 */
703 char_u *
704eval_to_string(arg, nextcmd)
705 char_u *arg;
706 char_u **nextcmd;
707{
708 var retvar;
709 char_u *retval;
710
711 if (eval0(arg, &retvar, nextcmd, TRUE) == FAIL)
712 retval = NULL;
713 else
714 {
715 retval = vim_strsave(get_var_string(&retvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000716 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718
719 return retval;
720}
721
722/*
723 * Call eval_to_string() with "sandbox" set and not using local variables.
724 */
725 char_u *
726eval_to_string_safe(arg, nextcmd)
727 char_u *arg;
728 char_u **nextcmd;
729{
730 char_u *retval;
731 void *save_funccalp;
732
733 save_funccalp = save_funccal();
734 ++sandbox;
735 retval = eval_to_string(arg, nextcmd);
736 --sandbox;
737 restore_funccal(save_funccalp);
738 return retval;
739}
740
741#if 0 /* not used */
742/*
743 * Top level evaluation function, returning a string.
744 * Advances "arg" to the first non-blank after the evaluated expression.
745 * Return pointer to allocated memory, or NULL for failure.
746 * Doesn't give error messages.
747 */
748 char_u *
749eval_arg_to_string(arg)
750 char_u **arg;
751{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000752 typeval retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *retval;
754 int ret;
755
756 ++emsg_off;
757
758 ret = eval1(arg, &retvar, TRUE);
759 if (ret == FAIL)
760 retval = NULL;
761 else
762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000763 retval = vim_strsave(get_vartv_string(&retvar));
764 clear_vartv(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766
767 --emsg_off;
768
769 return retval;
770}
771#endif
772
773/*
774 * Top level evaluation function, returning a number.
775 * Evaluates "expr" silently.
776 * Returns -1 for an error.
777 */
778 int
779eval_to_number(expr)
780 char_u *expr;
781{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000782 typeval retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 int retval;
784 char_u *p = expr;
785
786 ++emsg_off;
787
788 if (eval1(&p, &retvar, TRUE) == FAIL)
789 retval = -1;
790 else
791 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000792 retval = get_vartv_number(&retvar);
793 clear_vartv(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795 --emsg_off;
796
797 return retval;
798}
799
800#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
801/*
802 * Call some vimL function and return the result as a string
803 * Uses argv[argc] for the function arguments.
804 */
805 char_u *
806call_vim_function(func, argc, argv, safe)
807 char_u *func;
808 int argc;
809 char_u **argv;
810 int safe; /* use the sandbox */
811{
812 char_u *retval = NULL;
813 var retvar;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000814 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 long n;
816 int len;
817 int i;
818 int doesrange;
819 void *save_funccalp = NULL;
820
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000821 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (argvars == NULL)
823 return NULL;
824
825 for (i = 0; i < argc; i++)
826 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000827 /* Pass a NULL or empty argument as an empty string */
828 if (argv[i] == NULL || *argv[i] == NUL)
829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000830 argvars[i].v_type = VAR_STRING;
831 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000832 continue;
833 }
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 /* Recognize a number argument, the others must be strings. */
836 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
837 if (len != 0 && len == (int)STRLEN(argv[i]))
838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 argvars[i].v_type = VAR_NUMBER;
840 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 else
843 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000844 argvars[i].v_type = VAR_STRING;
845 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847 }
848
849 if (safe)
850 {
851 save_funccalp = save_funccal();
852 ++sandbox;
853 }
854
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000855 retvar.tv.v_type = VAR_UNKNOWN; /* clear_vartv() uses this */
856 if (call_func(func, (int)STRLEN(func), &retvar.tv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
858 &doesrange, TRUE) == OK)
859 retval = vim_strsave(get_var_string(&retvar));
860
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000861 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 vim_free(argvars);
863
864 if (safe)
865 {
866 --sandbox;
867 restore_funccal(save_funccalp);
868 }
869 return retval;
870}
871#endif
872
873/*
874 * Save the current function call pointer, and set it to NULL.
875 * Used when executing autocommands and for ":source".
876 */
877 void *
878save_funccal()
879{
880 struct funccall *fc;
881
882 fc = current_funccal;
883 current_funccal = NULL;
884 return (void *)fc;
885}
886
887 void
888restore_funccal(fc)
889 void *fc;
890{
891 current_funccal = (struct funccall *)fc;
892}
893
894#ifdef FEAT_FOLDING
895/*
896 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
897 * it in "*cp". Doesn't give error messages.
898 */
899 int
900eval_foldexpr(arg, cp)
901 char_u *arg;
902 int *cp;
903{
904 var retvar;
905 int retval;
906 char_u *s;
907
908 ++emsg_off;
909 ++sandbox;
910 *cp = NUL;
911 if (eval0(arg, &retvar, NULL, TRUE) == FAIL)
912 retval = 0;
913 else
914 {
915 /* If the result is a number, just return the number. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000916 if (retvar.tv.v_type == VAR_NUMBER)
917 retval = retvar.tv.vval.v_number;
918 else if (retvar.tv.v_type == VAR_UNKNOWN
919 || retvar.tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = 0;
921 else
922 {
923 /* If the result is a string, check if there is a non-digit before
924 * the number. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000925 s = retvar.tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (!VIM_ISDIGIT(*s) && *s != '-')
927 *cp = *s++;
928 retval = atol((char *)s);
929 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000930 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
932 --emsg_off;
933 --sandbox;
934
935 return retval;
936}
937#endif
938
939#ifdef FEAT_MAGIC_BRACES
940/*
941 * Expands out the 'magic' {}'s in a variable/function name.
942 * Note that this can call itself recursively, to deal with
943 * constructs like foo{bar}{baz}{bam}
944 * The four pointer arguments point to "foo{expre}ss{ion}bar"
945 * "in_start" ^
946 * "expr_start" ^
947 * "expr_end" ^
948 * "in_end" ^
949 *
950 * Returns a new allocated string, which the caller must free.
951 * Returns NULL for failure.
952 */
953 static char_u *
954make_expanded_name(in_start, expr_start, expr_end, in_end)
955 char_u *in_start;
956 char_u *expr_start;
957 char_u *expr_end;
958 char_u *in_end;
959{
960 char_u c1;
961 char_u *retval = NULL;
962 char_u *temp_result;
963 char_u *nextcmd = NULL;
964
965 if (expr_end == NULL || in_end == NULL)
966 return NULL;
967 *expr_start = NUL;
968 *expr_end = NUL;
969 c1 = *in_end;
970 *in_end = NUL;
971
972 temp_result = eval_to_string(expr_start + 1, &nextcmd);
973 if (temp_result != NULL && nextcmd == NULL)
974 {
975 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
976 + (in_end - expr_end) + 1));
977
978 if (retval != NULL)
979 {
980 STRCPY(retval, in_start);
981 STRCAT(retval, temp_result);
982 STRCAT(retval, expr_end + 1);
983 }
984 }
985 vim_free(temp_result);
986
987 *in_end = c1; /* put char back for error messages */
988 *expr_start = '{';
989 *expr_end = '}';
990
991 if (retval != NULL)
992 {
993 temp_result = find_name_end(retval, &expr_start, &expr_end);
994 if (expr_start != NULL)
995 {
996 /* Further expansion! */
997 temp_result = make_expanded_name(retval, expr_start,
998 expr_end, temp_result);
999 vim_free(retval);
1000 retval = temp_result;
1001 }
1002 }
1003
1004 return retval;
1005
1006}
1007#endif /* FEAT_MAGIC_BRACES */
1008
1009/*
1010 * ":let var = expr" assignment command.
1011 * ":let var" list one variable value
1012 * ":let" list all variable values
1013 */
1014 void
1015ex_let(eap)
1016 exarg_T *eap;
1017{
1018 char_u *arg = eap->arg;
1019 char_u *expr;
1020 char_u *name;
1021 VAR varp;
1022 var retvar;
1023 char_u *p;
1024 int c1 = 0, c2;
1025 int i;
1026 char_u *expr_start;
1027 char_u *expr_end;
1028 char_u *name_end;
1029
1030 name_end = find_name_end(arg, &expr_start, &expr_end);
1031 expr = vim_strchr(name_end, '=');
1032 if (expr == NULL)
1033 {
1034 if (ends_excmd(*arg))
1035 {
1036 if (!eap->skip)
1037 {
1038 /*
1039 * List all variables.
1040 */
1041 for (i = 0; i < variables.ga_len && !got_int; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001042 if (VAR_ENTRY(i).v_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1044 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001045 if (BVAR_ENTRY(i).v_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1047 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001048 if (WVAR_ENTRY(i).v_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1050 for (i = 0; i < VV_LEN && !got_int; ++i)
1051 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
1052 list_vim_var(i);
1053 }
1054 }
1055 else
1056 {
1057 int error = FALSE;
1058
1059 /*
1060 * List variables.
1061 */
1062 while (!ends_excmd(*arg) && !got_int)
1063 {
1064 char_u *temp_string = NULL;
1065 int arg_len;
1066
1067 /* Find the end of the name. */
1068 name_end = find_name_end(arg, &expr_start, &expr_end);
1069
1070 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1071 {
1072 emsg_severe = TRUE;
1073 EMSG(_(e_trailing));
1074 break;
1075 }
1076 if (!error && !eap->skip)
1077 {
1078#ifdef FEAT_MAGIC_BRACES
1079 if (expr_start != NULL)
1080 {
1081 temp_string = make_expanded_name(arg, expr_start,
1082 expr_end, name_end);
1083 if (temp_string == NULL)
1084 {
1085 /*
1086 * Report an invalid expression in braces, unless
1087 * the expression evaluation has been cancelled due
1088 * to an aborting error, an interrupt, or an
1089 * exception.
1090 */
1091 if (!aborting())
1092 {
1093 emsg_severe = TRUE;
1094 EMSG2(_(e_invarg2), arg);
1095 break;
1096 }
1097 error = TRUE;
1098 arg = skipwhite(name_end);
1099 continue;
1100 }
1101 arg = temp_string;
1102 arg_len = STRLEN(temp_string);
1103 }
1104 else
1105#endif
1106 {
1107 c1 = *name_end;
1108 *name_end = NUL;
1109 arg_len = (int)(name_end - arg);
1110 }
1111 i = find_vim_var(arg, arg_len);
1112 if (i >= 0)
1113 list_vim_var(i);
1114 else if (STRCMP("b:changedtick", arg) == 0)
1115 {
1116 char_u numbuf[NUMBUFLEN];
1117
1118 sprintf((char *)numbuf, "%ld",
1119 (long)curbuf->b_changedtick);
1120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1121 VAR_NUMBER, numbuf);
1122 }
1123 else
1124 {
1125 varp = find_var(arg, FALSE);
1126 if (varp == NULL)
1127 {
1128 /* Skip further arguments but do continue to
1129 * search for a trailing command. */
1130 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1131 error = TRUE;
1132 }
1133 else
1134 {
1135 name = vim_strchr(arg, ':');
1136 if (name != NULL)
1137 {
1138 /* "a:" vars have no name stored, use whole
1139 * arg */
1140 if (arg[0] == 'a' && arg[1] == ':')
1141 c2 = NUL;
1142 else
1143 {
1144 c2 = *++name;
1145 *name = NUL;
1146 }
1147 list_one_var(varp, arg);
1148 if (c2 != NUL)
1149 *name = c2;
1150 }
1151 else
1152 list_one_var(varp, (char_u *)"");
1153 }
1154 }
1155#ifdef FEAT_MAGIC_BRACES
1156 if (expr_start != NULL)
1157 vim_free(temp_string);
1158 else
1159#endif
1160 *name_end = c1;
1161 }
1162 arg = skipwhite(name_end);
1163 }
1164 }
1165 eap->nextcmd = check_nextcmd(arg);
1166 }
1167 else
1168 {
1169 if (eap->skip)
1170 ++emsg_skip;
1171 i = eval0(expr + 1, &retvar, &eap->nextcmd, !eap->skip);
1172 if (eap->skip)
1173 {
1174 if (i != FAIL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001175 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 --emsg_skip;
1177 }
1178 else if (i != FAIL)
1179 {
1180 /*
1181 * ":let $VAR = expr": Set environment variable.
1182 */
1183 if (*arg == '$')
1184 {
1185 int len;
1186 int cc;
1187
1188 /* Find the end of the name. */
1189 ++arg;
1190 name = arg;
1191 len = get_env_len(&arg);
1192 if (len == 0)
1193 EMSG2(_(e_invarg2), name - 1);
1194 else
1195 {
1196 if (*skipwhite(arg) != '=')
1197 EMSG(_(e_letunexp));
1198 else
1199 {
1200 cc = name[len];
1201 name[len] = NUL;
1202 p = get_var_string(&retvar);
1203 vim_setenv(name, p);
1204 if (STRICMP(name, "HOME") == 0)
1205 init_homedir();
1206 else if (didset_vim && STRICMP(name, "VIM") == 0)
1207 didset_vim = FALSE;
1208 else if (didset_vimruntime
1209 && STRICMP(name, "VIMRUNTIME") == 0)
1210 didset_vimruntime = FALSE;
1211 name[len] = cc;
1212 }
1213 }
1214 }
1215
1216 /*
1217 * ":let &option = expr": Set option value.
1218 * ":let &l:option = expr": Set local option value.
1219 * ":let &g:option = expr": Set global option value.
1220 */
1221 else if (*arg == '&')
1222 {
1223 int opt_flags;
1224
1225 /*
1226 * Find the end of the name;
1227 */
1228 p = find_option_end(&arg, &opt_flags);
1229 if (p == NULL || *skipwhite(p) != '=')
1230 EMSG(_(e_letunexp));
1231 else
1232 {
1233 c1 = *p;
1234 *p = NUL;
1235 set_option_value(arg, get_var_number(&retvar),
1236 get_var_string(&retvar), opt_flags);
1237 *p = c1; /* put back for error messages */
1238 }
1239 }
1240
1241 /*
1242 * ":let @r = expr": Set register contents.
1243 */
1244 else if (*arg == '@')
1245 {
1246 ++arg;
1247 if (*skipwhite(arg + 1) != '=')
1248 EMSG(_(e_letunexp));
1249 else
1250 write_reg_contents(*arg == '@' ? '"' : *arg,
1251 get_var_string(&retvar), -1, FALSE);
1252 }
1253
1254 /*
1255 * ":let var = expr": Set internal variable.
1256 */
1257 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1258 {
1259 /* Find the end of the name. */
1260 p = find_name_end(arg, &expr_start, &expr_end);
1261
1262 if (*skipwhite(p) != '=')
1263 EMSG(_(e_letunexp));
1264 else if (p - arg == 13
1265 && STRNCMP(arg, "b:changedtick", 13) == 0)
1266 EMSG2(_(e_readonlyvar), arg);
1267#ifdef FEAT_MAGIC_BRACES
1268 else if (expr_start != NULL)
1269 {
1270 char_u *temp_string;
1271
1272 temp_string = make_expanded_name(arg, expr_start,
1273 expr_end, p);
1274 if (temp_string == NULL)
1275 {
1276 /*
1277 * Report an invalid expression in braces, unless the
1278 * expression evaluation has been cancelled due to an
1279 * aborting error, an interrupt, or an exception.
1280 */
1281 if (!aborting())
1282 EMSG2(_(e_invarg2), arg);
1283 }
1284 else
1285 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001286 set_var(temp_string, &retvar.tv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 vim_free(temp_string);
1288 }
1289 }
1290#endif
1291 else
1292 {
1293 c1 = *p;
1294 *p = NUL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001295 set_var(arg, &retvar.tv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 *p = c1; /* put char back for error messages */
1297 }
1298 }
1299
1300 else
1301 {
1302 EMSG2(_(e_invarg2), arg);
1303 }
1304
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001305 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 }
1308}
1309
1310#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1311
1312 void
1313set_context_for_expression(xp, arg, cmdidx)
1314 expand_T *xp;
1315 char_u *arg;
1316 cmdidx_T cmdidx;
1317{
1318 int got_eq = FALSE;
1319 int c;
1320
1321 xp->xp_context = cmdidx == CMD_let ? EXPAND_USER_VARS
1322 : cmdidx == CMD_call ? EXPAND_FUNCTIONS
1323 : EXPAND_EXPRESSION;
1324 while ((xp->xp_pattern = vim_strpbrk(arg,
1325 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1326 {
1327 c = *xp->xp_pattern;
1328 if (c == '&')
1329 {
1330 c = xp->xp_pattern[1];
1331 if (c == '&')
1332 {
1333 ++xp->xp_pattern;
1334 xp->xp_context = cmdidx != CMD_let || got_eq
1335 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1336 }
1337 else if (c != ' ')
1338 xp->xp_context = EXPAND_SETTINGS;
1339 }
1340 else if (c == '$')
1341 {
1342 /* environment variable */
1343 xp->xp_context = EXPAND_ENV_VARS;
1344 }
1345 else if (c == '=')
1346 {
1347 got_eq = TRUE;
1348 xp->xp_context = EXPAND_EXPRESSION;
1349 }
1350 else if (c == '<'
1351 && xp->xp_context == EXPAND_FUNCTIONS
1352 && vim_strchr(xp->xp_pattern, '(') == NULL)
1353 {
1354 /* Function name can start with "<SNR>" */
1355 break;
1356 }
1357 else if (cmdidx != CMD_let || got_eq)
1358 {
1359 if (c == '"') /* string */
1360 {
1361 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1362 if (c == '\\' && xp->xp_pattern[1] != NUL)
1363 ++xp->xp_pattern;
1364 xp->xp_context = EXPAND_NOTHING;
1365 }
1366 else if (c == '\'') /* literal string */
1367 {
1368 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1369 /* skip */ ;
1370 xp->xp_context = EXPAND_NOTHING;
1371 }
1372 else if (c == '|')
1373 {
1374 if (xp->xp_pattern[1] == '|')
1375 {
1376 ++xp->xp_pattern;
1377 xp->xp_context = EXPAND_EXPRESSION;
1378 }
1379 else
1380 xp->xp_context = EXPAND_COMMANDS;
1381 }
1382 else
1383 xp->xp_context = EXPAND_EXPRESSION;
1384 }
1385 else
1386 xp->xp_context = EXPAND_NOTHING;
1387 arg = xp->xp_pattern;
1388 if (*arg != NUL)
1389 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1390 /* skip */ ;
1391 }
1392 xp->xp_pattern = arg;
1393}
1394
1395#endif /* FEAT_CMDL_COMPL */
1396
1397/*
1398 * ":1,25call func(arg1, arg2)" function call.
1399 */
1400 void
1401ex_call(eap)
1402 exarg_T *eap;
1403{
1404 char_u *arg = eap->arg;
1405 char_u *startarg;
1406 char_u *alias;
1407 char_u *name;
1408 var retvar;
1409 int len;
1410 linenr_T lnum;
1411 int doesrange;
1412 int failed = FALSE;
1413
1414 name = arg;
1415 len = get_func_len(&arg, &alias, !eap->skip);
1416 if (len == 0)
1417 goto end;
1418 if (alias != NULL)
1419 name = alias;
1420
1421 startarg = arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001422 retvar.tv.v_type = VAR_UNKNOWN; /* clear_vartv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 if (*startarg != '(')
1425 {
1426 EMSG2(_("E107: Missing braces: %s"), name);
1427 goto end;
1428 }
1429
1430 /*
1431 * When skipping, evaluate the function once, to find the end of the
1432 * arguments.
1433 * When the function takes a range, this is discovered after the first
1434 * call, and the loop is broken.
1435 */
1436 if (eap->skip)
1437 {
1438 ++emsg_skip;
1439 lnum = eap->line2; /* do it once, also with an invalid range */
1440 }
1441 else
1442 lnum = eap->line1;
1443 for ( ; lnum <= eap->line2; ++lnum)
1444 {
1445 if (!eap->skip && eap->addr_count > 0)
1446 {
1447 curwin->w_cursor.lnum = lnum;
1448 curwin->w_cursor.col = 0;
1449 }
1450 arg = startarg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001451 if (get_func_vartv(name, len, &retvar.tv, &arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
1453 {
1454 failed = TRUE;
1455 break;
1456 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001457 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (doesrange || eap->skip)
1459 break;
1460 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001461 * occurred or an exception was thrown but not caught.
1462 * get_func_vartv() returned OK, so that the check for trailing
1463 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (aborting())
1465 break;
1466 }
1467 if (eap->skip)
1468 --emsg_skip;
1469
1470 if (!failed)
1471 {
1472 /* Check for trailing illegal characters and a following command. */
1473 if (!ends_excmd(*arg))
1474 {
1475 emsg_severe = TRUE;
1476 EMSG(_(e_trailing));
1477 }
1478 else
1479 eap->nextcmd = check_nextcmd(arg);
1480 }
1481
1482end:
1483 if (alias != NULL)
1484 vim_free(alias);
1485}
1486
1487/*
1488 * ":unlet[!] var1 ... " command.
1489 */
1490 void
1491ex_unlet(eap)
1492 exarg_T *eap;
1493{
1494 char_u *arg = eap->arg;
1495 char_u *name_end;
1496 char_u cc;
1497 char_u *expr_start;
1498 char_u *expr_end;
1499 int error = FALSE;
1500
1501 do
1502 {
1503 /* Find the end of the name. */
1504 name_end = find_name_end(arg, &expr_start, &expr_end);
1505
1506 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1507 {
1508 emsg_severe = TRUE;
1509 EMSG(_(e_trailing));
1510 break;
1511 }
1512
1513 if (!error && !eap->skip)
1514 {
1515#ifdef FEAT_MAGIC_BRACES
1516 if (expr_start != NULL)
1517 {
1518 char_u *temp_string;
1519
1520 temp_string = make_expanded_name(arg, expr_start,
1521 expr_end, name_end);
1522 if (temp_string == NULL)
1523 {
1524 /*
1525 * Report an invalid expression in braces, unless the
1526 * expression evaluation has been cancelled due to an
1527 * aborting error, an interrupt, or an exception.
1528 */
1529 if (!aborting())
1530 {
1531 emsg_severe = TRUE;
1532 EMSG2(_(e_invarg2), arg);
1533 break;
1534 }
1535 error = TRUE;
1536 }
1537 else
1538 {
1539 if (do_unlet(temp_string) == FAIL && !eap->forceit)
1540 {
1541 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
1542 error = TRUE;
1543 }
1544 vim_free(temp_string);
1545 }
1546 }
1547 else
1548#endif
1549 {
1550 cc = *name_end;
1551 *name_end = NUL;
1552
1553 if (do_unlet(arg) == FAIL && !eap->forceit)
1554 {
1555 EMSG2(_("E108: No such variable: \"%s\""), arg);
1556 error = TRUE;
1557 }
1558
1559 *name_end = cc;
1560 }
1561 }
1562 arg = skipwhite(name_end);
1563 } while (!ends_excmd(*arg));
1564
1565 eap->nextcmd = check_nextcmd(arg);
1566}
1567
1568/*
1569 * "unlet" a variable. Return OK if it existed, FAIL if not.
1570 */
1571 int
1572do_unlet(name)
1573 char_u *name;
1574{
1575 VAR v;
1576
1577 v = find_var(name, TRUE);
1578 if (v != NULL)
1579 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001580 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return OK;
1582 }
1583 return FAIL;
1584}
1585
1586#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1587/*
1588 * Delete all "menutrans_" variables.
1589 */
1590 void
1591del_menutrans_vars()
1592{
1593 int i;
1594
1595 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 if (VAR_ENTRY(i).v_name != NULL
1597 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
1598 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599}
1600#endif
1601
1602#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1603
1604/*
1605 * Local string buffer for the next two functions to store a variable name
1606 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1607 * get_user_var_name().
1608 */
1609
1610static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
1611
1612static char_u *varnamebuf = NULL;
1613static int varnamebuflen = 0;
1614
1615/*
1616 * Function to concatenate a prefix and a variable name.
1617 */
1618 static char_u *
1619cat_prefix_varname(prefix, name)
1620 int prefix;
1621 char_u *name;
1622{
1623 int len;
1624
1625 len = (int)STRLEN(name) + 3;
1626 if (len > varnamebuflen)
1627 {
1628 vim_free(varnamebuf);
1629 len += 10; /* some additional space */
1630 varnamebuf = alloc(len);
1631 if (varnamebuf == NULL)
1632 {
1633 varnamebuflen = 0;
1634 return NULL;
1635 }
1636 varnamebuflen = len;
1637 }
1638 *varnamebuf = prefix;
1639 varnamebuf[1] = ':';
1640 STRCPY(varnamebuf + 2, name);
1641 return varnamebuf;
1642}
1643
1644/*
1645 * Function given to ExpandGeneric() to obtain the list of user defined
1646 * (global/buffer/window/built-in) variable names.
1647 */
1648/*ARGSUSED*/
1649 char_u *
1650get_user_var_name(xp, idx)
1651 expand_T *xp;
1652 int idx;
1653{
1654 static int gidx;
1655 static int bidx;
1656 static int widx;
1657 static int vidx;
1658 char_u *name;
1659
1660 if (idx == 0)
1661 gidx = bidx = widx = vidx = 0;
1662 if (gidx < variables.ga_len) /* Global variables */
1663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001664 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 && gidx < variables.ga_len)
1666 /* skip */;
1667 if (name != NULL)
1668 {
1669 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1670 return cat_prefix_varname('g', name);
1671 else
1672 return name;
1673 }
1674 }
1675 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
1676 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001677 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 && bidx < curbuf->b_vars.ga_len)
1679 /* skip */;
1680 if (name != NULL)
1681 return cat_prefix_varname('b', name);
1682 }
1683 if (bidx == curbuf->b_vars.ga_len)
1684 {
1685 ++bidx;
1686 return (char_u *)"b:changedtick";
1687 }
1688 if (widx < curwin->w_vars.ga_len) /* Current window variables */
1689 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001690 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 && widx < curwin->w_vars.ga_len)
1692 /* skip */;
1693 if (name != NULL)
1694 return cat_prefix_varname('w', name);
1695 }
1696 if (vidx < VV_LEN) /* Built-in variables */
1697 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
1698
1699 vim_free(varnamebuf);
1700 varnamebuf = NULL;
1701 varnamebuflen = 0;
1702 return NULL;
1703}
1704
1705#endif /* FEAT_CMDL_COMPL */
1706
1707/*
1708 * types for expressions.
1709 */
1710typedef enum
1711{
1712 TYPE_UNKNOWN = 0
1713 , TYPE_EQUAL /* == */
1714 , TYPE_NEQUAL /* != */
1715 , TYPE_GREATER /* > */
1716 , TYPE_GEQUAL /* >= */
1717 , TYPE_SMALLER /* < */
1718 , TYPE_SEQUAL /* <= */
1719 , TYPE_MATCH /* =~ */
1720 , TYPE_NOMATCH /* !~ */
1721} exptype_T;
1722
1723/*
1724 * The "evaluate" argument: When FALSE, the argument is only parsed but not
1725 * executed. The function may return OK, but the retvar will be of type
1726 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1727 */
1728
1729/*
1730 * Handle zero level expression.
1731 * This calls eval1() and handles error message and nextcmd.
1732 * Return OK or FAIL.
1733 */
1734 static int
1735eval0(arg, retvar, nextcmd, evaluate)
1736 char_u *arg;
1737 VAR retvar;
1738 char_u **nextcmd;
1739 int evaluate;
1740{
1741 int ret;
1742 char_u *p;
1743
1744 p = skipwhite(arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001745 ret = eval1(&p, &retvar->tv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (ret == FAIL || !ends_excmd(*p))
1747 {
1748 if (ret != FAIL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001749 clear_vartv(&retvar->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /*
1751 * Report the invalid expression unless the expression evaluation has
1752 * been cancelled due to an aborting error, an interrupt, or an
1753 * exception.
1754 */
1755 if (!aborting())
1756 EMSG2(_(e_invexpr2), arg);
1757 ret = FAIL;
1758 }
1759 if (nextcmd != NULL)
1760 *nextcmd = check_nextcmd(p);
1761
1762 return ret;
1763}
1764
1765/*
1766 * Handle top level expression:
1767 * expr1 ? expr0 : expr0
1768 *
1769 * "arg" must point to the first non-white of the expression.
1770 * "arg" is advanced to the next non-white after the recognized expression.
1771 *
1772 * Return OK or FAIL.
1773 */
1774 static int
1775eval1(arg, retvar, evaluate)
1776 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001777 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int evaluate;
1779{
1780 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001781 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 /*
1784 * Get the first variable.
1785 */
1786 if (eval2(arg, retvar, evaluate) == FAIL)
1787 return FAIL;
1788
1789 if ((*arg)[0] == '?')
1790 {
1791 result = FALSE;
1792 if (evaluate)
1793 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001794 if (get_vartv_number(retvar) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 result = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001796 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798
1799 /*
1800 * Get the second variable.
1801 */
1802 *arg = skipwhite(*arg + 1);
1803 if (eval1(arg, retvar, evaluate && result) == FAIL) /* recursive! */
1804 return FAIL;
1805
1806 /*
1807 * Check for the ":".
1808 */
1809 if ((*arg)[0] != ':')
1810 {
1811 EMSG(_("E109: Missing ':' after '?'"));
1812 if (evaluate && result)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001813 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 return FAIL;
1815 }
1816
1817 /*
1818 * Get the third variable.
1819 */
1820 *arg = skipwhite(*arg + 1);
1821 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
1822 {
1823 if (evaluate && result)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001824 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 return FAIL;
1826 }
1827 if (evaluate && !result)
1828 *retvar = var2;
1829 }
1830
1831 return OK;
1832}
1833
1834/*
1835 * Handle first level expression:
1836 * expr2 || expr2 || expr2 logical OR
1837 *
1838 * "arg" must point to the first non-white of the expression.
1839 * "arg" is advanced to the next non-white after the recognized expression.
1840 *
1841 * Return OK or FAIL.
1842 */
1843 static int
1844eval2(arg, retvar, evaluate)
1845 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001846 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 int evaluate;
1848{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001849 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 long result;
1851 int first;
1852
1853 /*
1854 * Get the first variable.
1855 */
1856 if (eval3(arg, retvar, evaluate) == FAIL)
1857 return FAIL;
1858
1859 /*
1860 * Repeat until there is no following "||".
1861 */
1862 first = TRUE;
1863 result = FALSE;
1864 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1865 {
1866 if (evaluate && first)
1867 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001868 if (get_vartv_number(retvar) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 result = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001870 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 first = FALSE;
1872 }
1873
1874 /*
1875 * Get the second variable.
1876 */
1877 *arg = skipwhite(*arg + 2);
1878 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1879 return FAIL;
1880
1881 /*
1882 * Compute the result.
1883 */
1884 if (evaluate && !result)
1885 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001886 if (get_vartv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 result = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001888 clear_vartv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 if (evaluate)
1891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001892 retvar->v_type = VAR_NUMBER;
1893 retvar->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895 }
1896
1897 return OK;
1898}
1899
1900/*
1901 * Handle second level expression:
1902 * expr3 && expr3 && expr3 logical AND
1903 *
1904 * "arg" must point to the first non-white of the expression.
1905 * "arg" is advanced to the next non-white after the recognized expression.
1906 *
1907 * Return OK or FAIL.
1908 */
1909 static int
1910eval3(arg, retvar, evaluate)
1911 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001912 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int evaluate;
1914{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001915 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 long result;
1917 int first;
1918
1919 /*
1920 * Get the first variable.
1921 */
1922 if (eval4(arg, retvar, evaluate) == FAIL)
1923 return FAIL;
1924
1925 /*
1926 * Repeat until there is no following "&&".
1927 */
1928 first = TRUE;
1929 result = TRUE;
1930 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1931 {
1932 if (evaluate && first)
1933 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001934 if (get_vartv_number(retvar) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 result = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001936 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 first = FALSE;
1938 }
1939
1940 /*
1941 * Get the second variable.
1942 */
1943 *arg = skipwhite(*arg + 2);
1944 if (eval4(arg, &var2, evaluate && result) == FAIL)
1945 return FAIL;
1946
1947 /*
1948 * Compute the result.
1949 */
1950 if (evaluate && result)
1951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001952 if (get_vartv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 result = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001954 clear_vartv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 if (evaluate)
1957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001958 retvar->v_type = VAR_NUMBER;
1959 retvar->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 }
1962
1963 return OK;
1964}
1965
1966/*
1967 * Handle third level expression:
1968 * var1 == var2
1969 * var1 =~ var2
1970 * var1 != var2
1971 * var1 !~ var2
1972 * var1 > var2
1973 * var1 >= var2
1974 * var1 < var2
1975 * var1 <= var2
1976 *
1977 * "arg" must point to the first non-white of the expression.
1978 * "arg" is advanced to the next non-white after the recognized expression.
1979 *
1980 * Return OK or FAIL.
1981 */
1982 static int
1983eval4(arg, retvar, evaluate)
1984 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001985 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 int evaluate;
1987{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001988 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 char_u *p;
1990 int i;
1991 exptype_T type = TYPE_UNKNOWN;
1992 int len = 2;
1993 long n1, n2;
1994 char_u *s1, *s2;
1995 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1996 regmatch_T regmatch;
1997 int ic;
1998 char_u *save_cpo;
1999
2000 /*
2001 * Get the first variable.
2002 */
2003 if (eval5(arg, retvar, evaluate) == FAIL)
2004 return FAIL;
2005
2006 p = *arg;
2007 switch (p[0])
2008 {
2009 case '=': if (p[1] == '=')
2010 type = TYPE_EQUAL;
2011 else if (p[1] == '~')
2012 type = TYPE_MATCH;
2013 break;
2014 case '!': if (p[1] == '=')
2015 type = TYPE_NEQUAL;
2016 else if (p[1] == '~')
2017 type = TYPE_NOMATCH;
2018 break;
2019 case '>': if (p[1] != '=')
2020 {
2021 type = TYPE_GREATER;
2022 len = 1;
2023 }
2024 else
2025 type = TYPE_GEQUAL;
2026 break;
2027 case '<': if (p[1] != '=')
2028 {
2029 type = TYPE_SMALLER;
2030 len = 1;
2031 }
2032 else
2033 type = TYPE_SEQUAL;
2034 break;
2035 }
2036
2037 /*
2038 * If there is a comparitive operator, use it.
2039 */
2040 if (type != TYPE_UNKNOWN)
2041 {
2042 /* extra question mark appended: ignore case */
2043 if (p[len] == '?')
2044 {
2045 ic = TRUE;
2046 ++len;
2047 }
2048 /* extra '#' appended: match case */
2049 else if (p[len] == '#')
2050 {
2051 ic = FALSE;
2052 ++len;
2053 }
2054 /* nothing appened: use 'ignorecase' */
2055 else
2056 ic = p_ic;
2057
2058 /*
2059 * Get the second variable.
2060 */
2061 *arg = skipwhite(p + len);
2062 if (eval5(arg, &var2, evaluate) == FAIL)
2063 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002064 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 return FAIL;
2066 }
2067
2068 if (evaluate)
2069 {
2070 /*
2071 * If one of the two variables is a number, compare as a number.
2072 * When using "=~" or "!~", always compare as string.
2073 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002074 if ((retvar->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2076 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002077 n1 = get_vartv_number(retvar);
2078 n2 = get_vartv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 switch (type)
2080 {
2081 case TYPE_EQUAL: n1 = (n1 == n2); break;
2082 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2083 case TYPE_GREATER: n1 = (n1 > n2); break;
2084 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2085 case TYPE_SMALLER: n1 = (n1 < n2); break;
2086 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2087 case TYPE_UNKNOWN:
2088 case TYPE_MATCH:
2089 case TYPE_NOMATCH: break; /* avoid gcc warning */
2090 }
2091 }
2092 else
2093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002094 s1 = get_vartv_string_buf(retvar, buf1);
2095 s2 = get_vartv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2097 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2098 else
2099 i = 0;
2100 n1 = FALSE;
2101 switch (type)
2102 {
2103 case TYPE_EQUAL: n1 = (i == 0); break;
2104 case TYPE_NEQUAL: n1 = (i != 0); break;
2105 case TYPE_GREATER: n1 = (i > 0); break;
2106 case TYPE_GEQUAL: n1 = (i >= 0); break;
2107 case TYPE_SMALLER: n1 = (i < 0); break;
2108 case TYPE_SEQUAL: n1 = (i <= 0); break;
2109
2110 case TYPE_MATCH:
2111 case TYPE_NOMATCH:
2112 /* avoid 'l' flag in 'cpoptions' */
2113 save_cpo = p_cpo;
2114 p_cpo = (char_u *)"";
2115 regmatch.regprog = vim_regcomp(s2,
2116 RE_MAGIC + RE_STRING);
2117 regmatch.rm_ic = ic;
2118 if (regmatch.regprog != NULL)
2119 {
2120 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2121 vim_free(regmatch.regprog);
2122 if (type == TYPE_NOMATCH)
2123 n1 = !n1;
2124 }
2125 p_cpo = save_cpo;
2126 break;
2127
2128 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2129 }
2130 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002131 clear_vartv(retvar);
2132 clear_vartv(&var2);
2133 retvar->v_type = VAR_NUMBER;
2134 retvar->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 }
2137
2138 return OK;
2139}
2140
2141/*
2142 * Handle fourth level expression:
2143 * + number addition
2144 * - number subtraction
2145 * . string concatenation
2146 *
2147 * "arg" must point to the first non-white of the expression.
2148 * "arg" is advanced to the next non-white after the recognized expression.
2149 *
2150 * Return OK or FAIL.
2151 */
2152 static int
2153eval5(arg, retvar, evaluate)
2154 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002155 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 int evaluate;
2157{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002158 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 int op;
2160 long n1, n2;
2161 char_u *s1, *s2;
2162 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2163 char_u *p;
2164
2165 /*
2166 * Get the first variable.
2167 */
2168 if (eval6(arg, retvar, evaluate) == FAIL)
2169 return FAIL;
2170
2171 /*
2172 * Repeat computing, until no '+', '-' or '.' is following.
2173 */
2174 for (;;)
2175 {
2176 op = **arg;
2177 if (op != '+' && op != '-' && op != '.')
2178 break;
2179
2180 /*
2181 * Get the second variable.
2182 */
2183 *arg = skipwhite(*arg + 1);
2184 if (eval6(arg, &var2, evaluate) == FAIL)
2185 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002186 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188 }
2189
2190 if (evaluate)
2191 {
2192 /*
2193 * Compute the result.
2194 */
2195 if (op == '.')
2196 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002197 s1 = get_vartv_string_buf(retvar, buf1);
2198 s2 = get_vartv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 op = (int)STRLEN(s1);
2200 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2201 if (p != NULL)
2202 {
2203 STRCPY(p, s1);
2204 STRCPY(p + op, s2);
2205 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002206 clear_vartv(retvar);
2207 retvar->v_type = VAR_STRING;
2208 retvar->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210 else
2211 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002212 n1 = get_vartv_number(retvar);
2213 n2 = get_vartv_number(&var2);
2214 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (op == '+')
2216 n1 = n1 + n2;
2217 else
2218 n1 = n1 - n2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002219 retvar->v_type = VAR_NUMBER;
2220 retvar->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002222 clear_vartv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 }
2225 return OK;
2226}
2227
2228/*
2229 * Handle fifth level expression:
2230 * * number multiplication
2231 * / number division
2232 * % number modulo
2233 *
2234 * "arg" must point to the first non-white of the expression.
2235 * "arg" is advanced to the next non-white after the recognized expression.
2236 *
2237 * Return OK or FAIL.
2238 */
2239 static int
2240eval6(arg, retvar, evaluate)
2241 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002242 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 int evaluate;
2244{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002245 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 int op;
2247 long n1, n2;
2248
2249 /*
2250 * Get the first variable.
2251 */
2252 if (eval7(arg, retvar, evaluate) == FAIL)
2253 return FAIL;
2254
2255 /*
2256 * Repeat computing, until no '*', '/' or '%' is following.
2257 */
2258 for (;;)
2259 {
2260 op = **arg;
2261 if (op != '*' && op != '/' && op != '%')
2262 break;
2263
2264 if (evaluate)
2265 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002266 n1 = get_vartv_number(retvar);
2267 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269 else
2270 n1 = 0;
2271
2272 /*
2273 * Get the second variable.
2274 */
2275 *arg = skipwhite(*arg + 1);
2276 if (eval7(arg, &var2, evaluate) == FAIL)
2277 return FAIL;
2278
2279 if (evaluate)
2280 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002281 n2 = get_vartv_number(&var2);
2282 clear_vartv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 /*
2285 * Compute the result.
2286 */
2287 if (op == '*')
2288 n1 = n1 * n2;
2289 else if (op == '/')
2290 {
2291 if (n2 == 0) /* give an error message? */
2292 n1 = 0x7fffffffL;
2293 else
2294 n1 = n1 / n2;
2295 }
2296 else
2297 {
2298 if (n2 == 0) /* give an error message? */
2299 n1 = 0;
2300 else
2301 n1 = n1 % n2;
2302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002303 retvar->v_type = VAR_NUMBER;
2304 retvar->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306 }
2307
2308 return OK;
2309}
2310
2311/*
2312 * Handle sixth level expression:
2313 * number number constant
2314 * "string" string contstant
2315 * 'string' literal string contstant
2316 * &option-name option value
2317 * @r register contents
2318 * identifier variable value
2319 * function() function call
2320 * $VAR environment variable
2321 * (expression) nested expression
2322 *
2323 * Also handle:
2324 * ! in front logical NOT
2325 * - in front unary minus
2326 * + in front unary plus (ignored)
2327 * trailing [] subscript in String
2328 *
2329 * "arg" must point to the first non-white of the expression.
2330 * "arg" is advanced to the next non-white after the recognized expression.
2331 *
2332 * Return OK or FAIL.
2333 */
2334 static int
2335eval7(arg, retvar, evaluate)
2336 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002337 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 int evaluate;
2339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 long n;
2341 int len;
2342 char_u *s;
2343 int val;
2344 char_u *start_leader, *end_leader;
2345 int ret = OK;
2346 char_u *alias;
2347
2348 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002349 * Initialise variable so that clear_vartv() can't mistake this for a
2350 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002352 retvar->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354 /*
2355 * Skip '!' and '-' characters. They are handled later.
2356 */
2357 start_leader = *arg;
2358 while (**arg == '!' || **arg == '-' || **arg == '+')
2359 *arg = skipwhite(*arg + 1);
2360 end_leader = *arg;
2361
2362 switch (**arg)
2363 {
2364 /*
2365 * Number constant.
2366 */
2367 case '0':
2368 case '1':
2369 case '2':
2370 case '3':
2371 case '4':
2372 case '5':
2373 case '6':
2374 case '7':
2375 case '8':
2376 case '9':
2377 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
2378 *arg += len;
2379 if (evaluate)
2380 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002381 retvar->v_type = VAR_NUMBER;
2382 retvar->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384 break;
2385
2386 /*
2387 * String constant: "string".
2388 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002389 case '"': ret = get_string_vartv(arg, retvar, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 break;
2391
2392 /*
2393 * Literal string constant: 'string'.
2394 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002395 case '\'': ret = get_lit_string_vartv(arg, retvar, evaluate);
2396 break;
2397
2398 /*
2399 * List: [expr, expr]
2400 */
2401 case '[': ret = get_list_vartv(arg, retvar, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 break;
2403
2404 /*
2405 * Option value: &name
2406 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002407 case '&': ret = get_option_vartv(arg, retvar, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 break;
2409
2410 /*
2411 * Environment variable: $VAR.
2412 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002413 case '$': ret = get_env_vartv(arg, retvar, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 break;
2415
2416 /*
2417 * Register contents: @r.
2418 */
2419 case '@': ++*arg;
2420 if (evaluate)
2421 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002422 retvar->v_type = VAR_STRING;
2423 retvar->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 if (**arg != NUL)
2426 ++*arg;
2427 break;
2428
2429 /*
2430 * nested expression: (expression).
2431 */
2432 case '(': *arg = skipwhite(*arg + 1);
2433 ret = eval1(arg, retvar, evaluate); /* recursive! */
2434 if (**arg == ')')
2435 ++*arg;
2436 else if (ret == OK)
2437 {
2438 EMSG(_("E110: Missing ')'"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002439 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 ret = FAIL;
2441 }
2442 break;
2443
2444 /*
2445 * Must be a variable or function name then.
2446 */
2447 default: s = *arg;
2448 len = get_func_len(arg, &alias, evaluate);
2449 if (alias != NULL)
2450 s = alias;
2451
2452 if (len == 0)
2453 ret = FAIL;
2454 else
2455 {
2456 if (**arg == '(') /* recursive! */
2457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002458 /* If "s" is the name of a variable of type VAR_FUNC
2459 * use its contents. */
2460 s = deref_func_name(s, &len);
2461
2462 /* Invoke the function. */
2463 ret = get_func_vartv(s, len, retvar, arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
2465 &len, evaluate);
2466 /* Stop the expression evaluation when immediately
2467 * aborting on error, or when an interrupt occurred or
2468 * an exception was thrown but not caught. */
2469 if (aborting())
2470 {
2471 if (ret == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002472 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 ret = FAIL;
2474 }
2475 }
2476 else if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002477 ret = get_var_vartv(s, len, retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479
2480 if (alias != NULL)
2481 vim_free(alias);
2482
2483 break;
2484 }
2485 *arg = skipwhite(*arg);
2486
2487 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002488 * Handle expr[expr] and expr[expr:expr] subscript.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002490 while (**arg == '[' && ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002492 if (eval_index(arg, retvar, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002494 clear_vartv(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return FAIL;
2496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498
2499 /*
2500 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2501 */
2502 if (ret == OK && evaluate && end_leader > start_leader)
2503 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002504 val = get_vartv_number(retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 while (end_leader > start_leader)
2506 {
2507 --end_leader;
2508 if (*end_leader == '!')
2509 val = !val;
2510 else if (*end_leader == '-')
2511 val = -val;
2512 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002513 clear_vartv(retvar);
2514 retvar->v_type = VAR_NUMBER;
2515 retvar->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517
2518 return ret;
2519}
2520
2521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002522 * Evaluate an "[expr]" or "[expr:expr]" index.
2523 * "*arg" points to the '['.
2524 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2525 */
2526 static int
2527eval_index(arg, retvar, evaluate)
2528 char_u **arg;
2529 typeval *retvar;
2530 int evaluate;
2531{
2532 int empty1 = FALSE, empty2 = FALSE;
2533 typeval var1, var2;
2534 long n1, n2 = 0;
2535 long len;
2536 int range;
2537 char_u *s;
2538
2539 if (retvar->v_type == VAR_FUNC)
2540 {
2541 EMSG(_("E999: Cannot index a Funcref"));
2542 return FAIL;
2543 }
2544
2545 /*
2546 * Get the (first) variable from inside the [].
2547 */
2548 *arg = skipwhite(*arg + 1);
2549 if (**arg == ':')
2550 empty1 = TRUE;
2551 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
2552 return FAIL;
2553
2554 /*
2555 * Get the second variable from inside the [:].
2556 */
2557 if (**arg == ':')
2558 {
2559 range = TRUE;
2560 *arg = skipwhite(*arg + 1);
2561 if (**arg == ']')
2562 empty2 = TRUE;
2563 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
2564 {
2565 clear_vartv(&var1);
2566 return FAIL;
2567 }
2568 }
2569 else
2570 range = FALSE;
2571
2572 /* Check for the ']'. */
2573 if (**arg != ']')
2574 {
2575 EMSG(_("E111: Missing ']'"));
2576 clear_vartv(&var1);
2577 if (range)
2578 clear_vartv(&var2);
2579 return FAIL;
2580 }
2581
2582 if (evaluate)
2583 {
2584 if (empty1)
2585 n1 = 0;
2586 else
2587 {
2588 n1 = get_vartv_number(&var1);
2589 clear_vartv(&var1);
2590 }
2591 if (range)
2592 {
2593 if (empty2)
2594 n2 = -1;
2595 else
2596 {
2597 n2 = get_vartv_number(&var2);
2598 clear_vartv(&var2);
2599 }
2600 }
2601
2602 switch (retvar->v_type)
2603 {
2604 case VAR_NUMBER:
2605 case VAR_STRING:
2606 s = get_vartv_string(retvar);
2607 len = (long)STRLEN(s);
2608 if (range)
2609 {
2610 /* The resulting variable is a substring. If the indexes
2611 * are out of range the result is empty. */
2612 if (n1 < 0)
2613 {
2614 n1 = len + n1;
2615 if (n1 < 0)
2616 n1 = 0;
2617 }
2618 if (n2 < 0)
2619 n2 = len + n2;
2620 else if (n2 >= len)
2621 n2 = len;
2622 if (n1 >= len || n2 < 0 || n1 > n2)
2623 s = NULL;
2624 else
2625 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
2626 }
2627 else
2628 {
2629 /* The resulting variable is a string of a single
2630 * character. If the index is too big or negative the
2631 * result is empty. */
2632 if (n1 >= len || n1 < 0)
2633 s = NULL;
2634 else
2635 s = vim_strnsave(s + n1, 1);
2636 }
2637 clear_vartv(retvar);
2638 retvar->v_type = VAR_STRING;
2639 retvar->vval.v_string = s;
2640 break;
2641
2642 case VAR_LIST:
2643 len = list_len(retvar->vval.v_list);
2644 if (n1 < 0)
2645 n1 = len + n1;
2646 if (!empty1 && (n1 < 0 || n1 >= len))
2647 {
2648 EMSGN(_(e_listidx), n1);
2649 return FAIL;
2650 }
2651 if (range)
2652 {
2653 listvar *l;
2654 listitem *item;
2655
2656 if (n2 < 0)
2657 n2 = len + n2;
2658 if (!empty2 && (n2 < 0 || n2 >= len || n2 < n1))
2659 {
2660 EMSGN(_(e_listidx), n2);
2661 return FAIL;
2662 }
2663 l = list_alloc();
2664 if (l == NULL)
2665 return FAIL;
2666 for (item = list_find(retvar->vval.v_list, n1);
2667 n1 <= n2; ++n1)
2668 {
2669 if (list_append_tv(l, &item->li_tv) == FAIL)
2670 {
2671 list_free(l);
2672 return FAIL;
2673 }
2674 item = item->li_next;
2675 }
2676 clear_vartv(retvar);
2677 retvar->v_type = VAR_LIST;
2678 retvar->vval.v_list = l;
2679 }
2680 else
2681 {
2682 copy_vartv(&list_find(retvar->vval.v_list, n1)->li_tv,
2683 &var1);
2684 clear_vartv(retvar);
2685 *retvar = var1;
2686 }
2687 break;
2688 }
2689 }
2690
2691 *arg = skipwhite(*arg + 1); /* skip the ']' */
2692 return OK;
2693}
2694
2695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 * Get an option value.
2697 * "arg" points to the '&' or '+' before the option name.
2698 * "arg" is advanced to character after the option name.
2699 * Return OK or FAIL.
2700 */
2701 static int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002702get_option_vartv(arg, retvar, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002704 typeval *retvar; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 int evaluate;
2706{
2707 char_u *option_end;
2708 long numval;
2709 char_u *stringval;
2710 int opt_type;
2711 int c;
2712 int working = (**arg == '+'); /* has("+option") */
2713 int ret = OK;
2714 int opt_flags;
2715
2716 /*
2717 * Isolate the option name and find its value.
2718 */
2719 option_end = find_option_end(arg, &opt_flags);
2720 if (option_end == NULL)
2721 {
2722 if (retvar != NULL)
2723 EMSG2(_("E112: Option name missing: %s"), *arg);
2724 return FAIL;
2725 }
2726
2727 if (!evaluate)
2728 {
2729 *arg = option_end;
2730 return OK;
2731 }
2732
2733 c = *option_end;
2734 *option_end = NUL;
2735 opt_type = get_option_value(*arg, &numval,
2736 retvar == NULL ? NULL : &stringval, opt_flags);
2737
2738 if (opt_type == -3) /* invalid name */
2739 {
2740 if (retvar != NULL)
2741 EMSG2(_("E113: Unknown option: %s"), *arg);
2742 ret = FAIL;
2743 }
2744 else if (retvar != NULL)
2745 {
2746 if (opt_type == -2) /* hidden string option */
2747 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002748 retvar->v_type = VAR_STRING;
2749 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
2751 else if (opt_type == -1) /* hidden number option */
2752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002753 retvar->v_type = VAR_NUMBER;
2754 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756 else if (opt_type == 1) /* number option */
2757 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002758 retvar->v_type = VAR_NUMBER;
2759 retvar->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 else /* string option */
2762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002763 retvar->v_type = VAR_STRING;
2764 retvar->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766 }
2767 else if (working && (opt_type == -2 || opt_type == -1))
2768 ret = FAIL;
2769
2770 *option_end = c; /* put back for error messages */
2771 *arg = option_end;
2772
2773 return ret;
2774}
2775
2776/*
2777 * Allocate a variable for a string constant.
2778 * Return OK or FAIL.
2779 */
2780 static int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002781get_string_vartv(arg, retvar, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002783 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 int evaluate;
2785{
2786 char_u *p;
2787 char_u *name;
2788 int i;
2789 int extra = 0;
2790
2791 /*
2792 * Find the end of the string, skipping backslashed characters.
2793 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002794 for (p = *arg + 1; *p && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
2796 if (*p == '\\' && p[1] != NUL)
2797 {
2798 ++p;
2799 /* A "\<x>" form occupies at least 4 characters, and produces up
2800 * to 6 characters: reserve space for 2 extra */
2801 if (*p == '<')
2802 extra += 2;
2803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805
2806 if (*p != '"')
2807 {
2808 EMSG2(_("E114: Missing quote: %s"), *arg);
2809 return FAIL;
2810 }
2811
2812 /* If only parsing, set *arg and return here */
2813 if (!evaluate)
2814 {
2815 *arg = p + 1;
2816 return OK;
2817 }
2818
2819 /*
2820 * Copy the string into allocated memory, handling backslashed
2821 * characters.
2822 */
2823 name = alloc((unsigned)(p - *arg + extra));
2824 if (name == NULL)
2825 return FAIL;
2826
2827 i = 0;
2828 for (p = *arg + 1; *p && *p != '"'; ++p)
2829 {
2830 if (*p == '\\')
2831 {
2832 switch (*++p)
2833 {
2834 case 'b': name[i++] = BS; break;
2835 case 'e': name[i++] = ESC; break;
2836 case 'f': name[i++] = FF; break;
2837 case 'n': name[i++] = NL; break;
2838 case 'r': name[i++] = CAR; break;
2839 case 't': name[i++] = TAB; break;
2840
2841 case 'X': /* hex: "\x1", "\x12" */
2842 case 'x':
2843 case 'u': /* Unicode: "\u0023" */
2844 case 'U':
2845 if (vim_isxdigit(p[1]))
2846 {
2847 int n, nr;
2848 int c = toupper(*p);
2849
2850 if (c == 'X')
2851 n = 2;
2852 else
2853 n = 4;
2854 nr = 0;
2855 while (--n >= 0 && vim_isxdigit(p[1]))
2856 {
2857 ++p;
2858 nr = (nr << 4) + hex2nr(*p);
2859 }
2860#ifdef FEAT_MBYTE
2861 /* For "\u" store the number according to
2862 * 'encoding'. */
2863 if (c != 'X')
2864 i += (*mb_char2bytes)(nr, name + i);
2865 else
2866#endif
2867 name[i++] = nr;
2868 }
2869 else
2870 name[i++] = *p;
2871 break;
2872
2873 /* octal: "\1", "\12", "\123" */
2874 case '0':
2875 case '1':
2876 case '2':
2877 case '3':
2878 case '4':
2879 case '5':
2880 case '6':
2881 case '7': name[i] = *p - '0';
2882 if (p[1] >= '0' && p[1] <= '7')
2883 {
2884 ++p;
2885 name[i] = (name[i] << 3) + *p - '0';
2886 if (p[1] >= '0' && p[1] <= '7')
2887 {
2888 ++p;
2889 name[i] = (name[i] << 3) + *p - '0';
2890 }
2891 }
2892 ++i;
2893 break;
2894
2895 /* Special key, e.g.: "\<C-W>" */
2896 case '<': extra = trans_special(&p, name + i, TRUE);
2897 if (extra != 0)
2898 {
2899 i += extra;
2900 --p;
2901 break;
2902 }
2903 /* FALLTHROUGH */
2904
2905 default: name[i++] = *p;
2906 break;
2907 }
2908 }
2909 else
2910 name[i++] = *p;
2911
2912#ifdef FEAT_MBYTE
2913 /* For a multi-byte character copy the bytes after the first one. */
2914 if (has_mbyte)
2915 {
2916 int l = (*mb_ptr2len_check)(p);
2917
2918 while (--l > 0)
2919 name[i++] = *++p;
2920 }
2921#endif
2922 }
2923 name[i] = NUL;
2924 *arg = p + 1;
2925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002926 retvar->v_type = VAR_STRING;
2927 retvar->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 return OK;
2930}
2931
2932/*
2933 * Allocate a variable for an backtick-string constant.
2934 * Return OK or FAIL.
2935 */
2936 static int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002937get_lit_string_vartv(arg, retvar, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002939 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 int evaluate;
2941{
2942 char_u *p;
2943 char_u *name;
2944
2945 /*
2946 * Find the end of the string.
2947 */
2948 p = vim_strchr(*arg + 1, '\'');
2949 if (p == NULL)
2950 {
2951 EMSG2(_("E115: Missing quote: %s"), *arg);
2952 return FAIL;
2953 }
2954
2955 if (evaluate)
2956 {
2957 /*
2958 * Copy the string into allocated memory.
2959 */
2960 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
2961 if (name == NULL)
2962 return FAIL;
2963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002964 retvar->v_type = VAR_STRING;
2965 retvar->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
2967
2968 *arg = p + 1;
2969
2970 return OK;
2971}
2972
2973/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002974 * Allocate a variable for a List and fill it from "*arg".
2975 * Return OK or FAIL.
2976 */
2977 static int
2978get_list_vartv(arg, retvar, evaluate)
2979 char_u **arg;
2980 typeval *retvar;
2981 int evaluate;
2982{
2983 listvar *l = NULL;
2984 typeval tv;
2985 listitem *item;
2986
2987 if (evaluate)
2988 {
2989 l = list_alloc();
2990 if (l == NULL)
2991 return FAIL;
2992 }
2993
2994 *arg = skipwhite(*arg + 1);
2995 while (**arg != ']' && **arg != NUL)
2996 {
2997 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
2998 goto failret;
2999 if (evaluate)
3000 {
3001 item = listitem_alloc();
3002 if (item != NULL)
3003 {
3004 item->li_tv = tv;
3005 list_append(l, item);
3006 }
3007 }
3008
3009 if (**arg == ']')
3010 break;
3011 if (**arg != ',')
3012 {
3013 EMSG2(_("E999: Missing comma in list: %s"), *arg);
3014 goto failret;
3015 }
3016 *arg = skipwhite(*arg + 1);
3017 }
3018
3019 if (**arg != ']')
3020 {
3021 EMSG2(_("E999: Missing end of list ']': %s"), *arg);
3022failret:
3023 if (evaluate)
3024 list_free(l);
3025 return FAIL;
3026 }
3027
3028 *arg = skipwhite(*arg + 1);
3029 if (evaluate)
3030 {
3031 retvar->v_type = VAR_LIST;
3032 retvar->vval.v_list = l;
3033 ++l->lv_refcount;
3034 }
3035
3036 return OK;
3037}
3038
3039/*
3040 * Allocate an empty header for a list.
3041 */
3042 static listvar *
3043list_alloc()
3044{
3045 return (listvar *)alloc_clear(sizeof(listvar));
3046}
3047
3048/*
3049 * Unreference a list: decrement the reference count and free it when it
3050 * becomes zero.
3051 */
3052 static void
3053list_unref(l)
3054 listvar *l;
3055{
3056 if (l != NULL && --l->lv_refcount <= 0)
3057 list_free(l);
3058}
3059
3060/*
3061 * Free a list, including all items it points to.
3062 * Ignores the reference count.
3063 */
3064 static void
3065list_free(l)
3066 listvar *l;
3067{
3068 listitem *item;
3069 listitem *next;
3070
3071 for (item = l->lv_first; item != NULL; item = next)
3072 {
3073 next = item->li_next;
3074 listitem_free(item);
3075 }
3076 vim_free(l);
3077}
3078
3079/*
3080 * Allocate a list item.
3081 */
3082 static listitem *
3083listitem_alloc()
3084{
3085 return (listitem *)alloc(sizeof(listitem));
3086}
3087
3088/*
3089 * Free a list item. Also clears the value;
3090 */
3091 static void
3092listitem_free(item)
3093 listitem *item;
3094{
3095 clear_vartv(&item->li_tv);
3096 vim_free(item);
3097}
3098
3099/*
3100 * Get the number of items in a list.
3101 */
3102 static long
3103list_len(l)
3104 listvar *l;
3105{
3106 listitem *item;
3107 long len = 0;
3108
3109 if (l == NULL)
3110 return 0L;
3111 for (item = l->lv_first; item != NULL; item = item->li_next)
3112 ++len;
3113 return len;
3114}
3115
3116/*
3117 * Locate item with index "n" in list "l" and return it.
3118 * A negative index is counted from the end; -1 is the last item.
3119 * Returns NULL when "n" is out of range.
3120 */
3121 static listitem *
3122list_find(l, n)
3123 listvar *l;
3124 long n;
3125{
3126 listitem *item;
3127 long idx;
3128
3129 if (l == NULL)
3130 return NULL;
3131 if (n < 0)
3132 {
3133 idx = -1; /* search from the end */
3134 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
3135 --idx;
3136 }
3137 else
3138 {
3139 idx = 0; /* search from the start */
3140 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
3141 ++idx;
3142 }
3143 if (idx != n)
3144 return NULL;
3145 return item;
3146}
3147
3148/*
3149 * Append item "item" to the end of list "l".
3150 */
3151 static void
3152list_append(l, item)
3153 listvar *l;
3154 listitem *item;
3155{
3156 if (l->lv_last == NULL)
3157 {
3158 /* empty list */
3159 l->lv_first = item;
3160 l->lv_last = item;
3161 item->li_prev = NULL;
3162 }
3163 else
3164 {
3165 l->lv_last->li_next = item;
3166 item->li_prev = l->lv_last;
3167 l->lv_last = item;
3168 }
3169 item->li_next = NULL;
3170}
3171
3172/*
3173 * Append typeval "tv" to the end of list "l".
3174 */
3175 static int
3176list_append_tv(l, tv)
3177 listvar *l;
3178 typeval *tv;
3179{
3180 listitem *ni = listitem_alloc();
3181
3182 if (ni == NULL)
3183 return FAIL;
3184 copy_vartv(tv, &ni->li_tv);
3185 list_append(l, ni);
3186 return OK;
3187}
3188
3189/*
3190 * Make a copy of list "l". Shallow if "deep" is FALSE.
3191 * The refcount of the new list is set to 1.
3192 * Returns NULL when out of memory.
3193 */
3194 static listvar *
3195list_copy(orig, deep)
3196 listvar *orig;
3197 int deep;
3198{
3199 listvar *copy;
3200 listitem *item;
3201 listitem *ni;
3202 static int recurse = 0;
3203
3204 if (orig == NULL)
3205 return NULL;
3206 if (recurse >= VAR_LIST_MAXNEST)
3207 {
3208 EMSG(_("E999: List nested too deep for making a copy"));
3209 return NULL;
3210 }
3211 ++recurse;
3212
3213 copy = list_alloc();
3214 if (copy != NULL)
3215 {
3216 for (item = orig->lv_first; item != NULL; item = item->li_next)
3217 {
3218 ni = listitem_alloc();
3219 if (ni == NULL)
3220 break;
3221 if (deep && item->li_tv.v_type == VAR_LIST)
3222 {
3223 ni->li_tv.v_type = VAR_LIST;
3224 ni->li_tv.vval.v_list = list_copy(item->li_tv.vval.v_list,
3225 TRUE);
3226 if (ni->li_tv.vval.v_list == NULL)
3227 {
3228 vim_free(ni);
3229 break;
3230 }
3231 }
3232 else
3233 copy_vartv(&item->li_tv, &ni->li_tv);
3234 list_append(copy, ni);
3235 }
3236 ++copy->lv_refcount;
3237 }
3238
3239 --recurse;
3240 return copy;
3241}
3242
3243/*
3244 * Remove item with index "n" from list "l" and return it.
3245 * Returns NULL when "n" is out of range.
3246 */
3247 static listitem *
3248list_getrem(l, n)
3249 listvar *l;
3250 long n;
3251{
3252 listitem *item;
3253
3254 item = list_find(l, n);
3255 if (item != NULL)
3256 {
3257 if (item->li_next == NULL)
3258 l->lv_last = item->li_prev;
3259 else
3260 item->li_next->li_prev = item->li_prev;
3261 if (item->li_prev == NULL)
3262 l->lv_first = item->li_next;
3263 else
3264 item->li_prev->li_next = item->li_next;
3265 }
3266 return item;
3267}
3268
3269/*
3270 * Return an allocated string with the string representation of a list.
3271 * May return NULL.
3272 */
3273 static char_u *
3274list2string(tv)
3275 typeval *tv;
3276{
3277 garray_T ga;
3278 listitem *item;
3279 int first = TRUE;
3280 char_u *tofree;
3281 char_u *s;
3282
3283 if (tv->vval.v_list == NULL)
3284 return NULL;
3285 ga_init2(&ga, (int)sizeof(char), 80);
3286 ga_append(&ga, '[');
3287
3288 for (item = tv->vval.v_list->lv_first; item != NULL; item = item->li_next)
3289 {
3290 if (first)
3291 first = FALSE;
3292 else
3293 ga_concat(&ga, (char_u *)", ");
3294
3295 s = tv2string(&item->li_tv, &tofree);
3296 if (s != NULL)
3297 ga_concat(&ga, s);
3298 vim_free(tofree);
3299 }
3300
3301 ga_append(&ga, ']');
3302 ga_append(&ga, NUL);
3303 return (char_u *)ga.ga_data;
3304}
3305
3306/*
3307 * Return a string with the string representation of a variable.
3308 * If the memory is allocated "tofree" is set to it, otherwise NULL.
3309 * Can only be used once before the value is used, it may call
3310 * get_var_string().
3311 * May return NULL;
3312 */
3313 static char_u *
3314tv2string(tv, tofree)
3315 typeval *tv;
3316 char_u **tofree;
3317{
3318 switch (tv->v_type)
3319 {
3320 case VAR_FUNC:
3321 *tofree = NULL;
3322 return tv->vval.v_string;
3323 case VAR_LIST:
3324 *tofree = list2string(tv);
3325 return *tofree;
3326 default:
3327 break;
3328 }
3329 *tofree = NULL;
3330 return get_vartv_string(tv);
3331}
3332
3333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * Get the value of an environment variable.
3335 * "arg" is pointing to the '$'. It is advanced to after the name.
3336 * If the environment variable was not set, silently assume it is empty.
3337 * Always return OK.
3338 */
3339 static int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003340get_env_vartv(arg, retvar, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 char_u **arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003342 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 int evaluate;
3344{
3345 char_u *string = NULL;
3346 int len;
3347 int cc;
3348 char_u *name;
3349
3350 ++*arg;
3351 name = *arg;
3352 len = get_env_len(arg);
3353 if (evaluate)
3354 {
3355 if (len != 0)
3356 {
3357 cc = name[len];
3358 name[len] = NUL;
3359 /* first try mch_getenv(), fast for normal environment vars */
3360 string = mch_getenv(name);
3361 if (string != NULL && *string != NUL)
3362 string = vim_strsave(string);
3363 else
3364 {
3365 /* next try expanding things like $VIM and ${HOME} */
3366 string = expand_env_save(name - 1);
3367 if (string != NULL && *string == '$')
3368 {
3369 vim_free(string);
3370 string = NULL;
3371 }
3372 }
3373 name[len] = cc;
3374 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003375 retvar->v_type = VAR_STRING;
3376 retvar->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378
3379 return OK;
3380}
3381
3382/*
3383 * Array with names and number of arguments of all internal functions
3384 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
3385 */
3386static struct fst
3387{
3388 char *f_name; /* function name */
3389 char f_min_argc; /* minimal number of arguments */
3390 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003391 void (*f_func) __ARGS((typeval *args, typeval *rvar));
3392 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393} functions[] =
3394{
3395 {"append", 2, 2, f_append},
3396 {"argc", 0, 0, f_argc},
3397 {"argidx", 0, 0, f_argidx},
3398 {"argv", 1, 1, f_argv},
3399 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003400 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {"bufexists", 1, 1, f_bufexists},
3402 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
3403 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
3404 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
3405 {"buflisted", 1, 1, f_buflisted},
3406 {"bufloaded", 1, 1, f_bufloaded},
3407 {"bufname", 1, 1, f_bufname},
3408 {"bufnr", 1, 1, f_bufnr},
3409 {"bufwinnr", 1, 1, f_bufwinnr},
3410 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003411 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {"char2nr", 1, 1, f_char2nr},
3413 {"cindent", 1, 1, f_cindent},
3414 {"col", 1, 1, f_col},
3415 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003416 {"copy", 1, 1, f_copy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {"cscope_connection",0,3, f_cscope_connection},
3418 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003419 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {"delete", 1, 1, f_delete},
3421 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00003422 {"diff_filler", 1, 1, f_diff_filler},
3423 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {"escape", 2, 2, f_escape},
3425 {"eventhandler", 0, 0, f_eventhandler},
3426 {"executable", 1, 1, f_executable},
3427 {"exists", 1, 1, f_exists},
3428 {"expand", 1, 2, f_expand},
3429 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
3430 {"filereadable", 1, 1, f_filereadable},
3431 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00003432 {"finddir", 1, 3, f_finddir},
3433 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {"fnamemodify", 2, 2, f_fnamemodify},
3435 {"foldclosed", 1, 1, f_foldclosed},
3436 {"foldclosedend", 1, 1, f_foldclosedend},
3437 {"foldlevel", 1, 1, f_foldlevel},
3438 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003439 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003441 {"function", 1, 1, f_function},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {"getbufvar", 2, 2, f_getbufvar},
3443 {"getchar", 0, 1, f_getchar},
3444 {"getcharmod", 0, 0, f_getcharmod},
3445 {"getcmdline", 0, 0, f_getcmdline},
3446 {"getcmdpos", 0, 0, f_getcmdpos},
3447 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00003448 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003449 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {"getfsize", 1, 1, f_getfsize},
3451 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003452 {"getftype", 1, 1, f_getftype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {"getline", 1, 1, f_getline},
3454 {"getreg", 0, 1, f_getreg},
3455 {"getregtype", 0, 1, f_getregtype},
3456 {"getwinposx", 0, 0, f_getwinposx},
3457 {"getwinposy", 0, 0, f_getwinposy},
3458 {"getwinvar", 2, 2, f_getwinvar},
3459 {"glob", 1, 1, f_glob},
3460 {"globpath", 2, 2, f_globpath},
3461 {"has", 1, 1, f_has},
3462 {"hasmapto", 1, 2, f_hasmapto},
3463 {"highlightID", 1, 1, f_hlID}, /* obsolete */
3464 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
3465 {"histadd", 2, 2, f_histadd},
3466 {"histdel", 1, 2, f_histdel},
3467 {"histget", 1, 2, f_histget},
3468 {"histnr", 1, 1, f_histnr},
3469 {"hlID", 1, 1, f_hlID},
3470 {"hlexists", 1, 1, f_hlexists},
3471 {"hostname", 0, 0, f_hostname},
3472 {"iconv", 3, 3, f_iconv},
3473 {"indent", 1, 1, f_indent},
3474 {"input", 1, 2, f_input},
3475 {"inputdialog", 1, 3, f_inputdialog},
3476 {"inputrestore", 0, 0, f_inputrestore},
3477 {"inputsave", 0, 0, f_inputsave},
3478 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {"isdirectory", 1, 1, f_isdirectory},
3481 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {"libcall", 3, 3, f_libcall},
3484 {"libcallnr", 3, 3, f_libcallnr},
3485 {"line", 1, 1, f_line},
3486 {"line2byte", 1, 1, f_line2byte},
3487 {"lispindent", 1, 1, f_lispindent},
3488 {"localtime", 0, 0, f_localtime},
3489 {"maparg", 1, 2, f_maparg},
3490 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00003491 {"match", 2, 4, f_match},
3492 {"matchend", 2, 4, f_matchend},
3493 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {"mode", 0, 0, f_mode},
3495 {"nextnonblank", 1, 1, f_nextnonblank},
3496 {"nr2char", 1, 1, f_nr2char},
3497 {"prevnonblank", 1, 1, f_prevnonblank},
3498 {"remote_expr", 2, 3, f_remote_expr},
3499 {"remote_foreground", 1, 1, f_remote_foreground},
3500 {"remote_peek", 1, 2, f_remote_peek},
3501 {"remote_read", 1, 1, f_remote_read},
3502 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 {"remove", 2, 2, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003505 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {"resolve", 1, 1, f_resolve},
3507 {"search", 1, 2, f_search},
3508 {"searchpair", 3, 5, f_searchpair},
3509 {"server2client", 2, 2, f_server2client},
3510 {"serverlist", 0, 0, f_serverlist},
3511 {"setbufvar", 3, 3, f_setbufvar},
3512 {"setcmdpos", 1, 1, f_setcmdpos},
3513 {"setline", 2, 2, f_setline},
3514 {"setreg", 2, 3, f_setreg},
3515 {"setwinvar", 3, 3, f_setwinvar},
3516 {"simplify", 1, 1, f_simplify},
3517#ifdef HAVE_STRFTIME
3518 {"strftime", 1, 2, f_strftime},
3519#endif
3520 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003521 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {"strlen", 1, 1, f_strlen},
3523 {"strpart", 2, 3, f_strpart},
3524 {"strridx", 2, 2, f_strridx},
3525 {"strtrans", 1, 1, f_strtrans},
3526 {"submatch", 1, 1, f_submatch},
3527 {"substitute", 4, 4, f_substitute},
3528 {"synID", 3, 3, f_synID},
3529 {"synIDattr", 2, 3, f_synIDattr},
3530 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003531 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {"tempname", 0, 0, f_tempname},
3533 {"tolower", 1, 1, f_tolower},
3534 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {"type", 1, 1, f_type},
3537 {"virtcol", 1, 1, f_virtcol},
3538 {"visualmode", 0, 1, f_visualmode},
3539 {"winbufnr", 1, 1, f_winbufnr},
3540 {"wincol", 0, 0, f_wincol},
3541 {"winheight", 1, 1, f_winheight},
3542 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003543 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {"winrestcmd", 0, 0, f_winrestcmd},
3545 {"winwidth", 1, 1, f_winwidth},
3546};
3547
3548#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3549
3550/*
3551 * Function given to ExpandGeneric() to obtain the list of internal
3552 * or user defined function names.
3553 */
3554 char_u *
3555get_function_name(xp, idx)
3556 expand_T *xp;
3557 int idx;
3558{
3559 static int intidx = -1;
3560 char_u *name;
3561
3562 if (idx == 0)
3563 intidx = -1;
3564 if (intidx < 0)
3565 {
3566 name = get_user_func_name(xp, idx);
3567 if (name != NULL)
3568 return name;
3569 }
3570 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
3571 {
3572 STRCPY(IObuff, functions[intidx].f_name);
3573 STRCAT(IObuff, "(");
3574 if (functions[intidx].f_max_argc == 0)
3575 STRCAT(IObuff, ")");
3576 return IObuff;
3577 }
3578
3579 return NULL;
3580}
3581
3582/*
3583 * Function given to ExpandGeneric() to obtain the list of internal or
3584 * user defined variable or function names.
3585 */
3586/*ARGSUSED*/
3587 char_u *
3588get_expr_name(xp, idx)
3589 expand_T *xp;
3590 int idx;
3591{
3592 static int intidx = -1;
3593 char_u *name;
3594
3595 if (idx == 0)
3596 intidx = -1;
3597 if (intidx < 0)
3598 {
3599 name = get_function_name(xp, idx);
3600 if (name != NULL)
3601 return name;
3602 }
3603 return get_user_var_name(xp, ++intidx);
3604}
3605
3606#endif /* FEAT_CMDL_COMPL */
3607
3608/*
3609 * Find internal function in table above.
3610 * Return index, or -1 if not found
3611 */
3612 static int
3613find_internal_func(name)
3614 char_u *name; /* name of the function */
3615{
3616 int first = 0;
3617 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
3618 int cmp;
3619 int x;
3620
3621 /*
3622 * Find the function name in the table. Binary search.
3623 */
3624 while (first <= last)
3625 {
3626 x = first + ((unsigned)(last - first) >> 1);
3627 cmp = STRCMP(name, functions[x].f_name);
3628 if (cmp < 0)
3629 last = x - 1;
3630 else if (cmp > 0)
3631 first = x + 1;
3632 else
3633 return x;
3634 }
3635 return -1;
3636}
3637
3638/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003639 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
3640 * name it contains, otherwise return "name".
3641 */
3642 static char_u *
3643deref_func_name(name, lenp)
3644 char_u *name;
3645 int *lenp;
3646{
3647 VAR v;
3648 int cc;
3649
3650 cc = name[*lenp];
3651 name[*lenp] = NUL;
3652 v = find_var(name, FALSE);
3653 name[*lenp] = cc;
3654 if (v != NULL && v->tv.v_type == VAR_FUNC)
3655 {
3656 if (v->tv.vval.v_string == NULL)
3657 {
3658 *lenp = 0;
3659 return (char_u *)""; /* just in case */
3660 }
3661 *lenp = STRLEN(v->tv.vval.v_string);
3662 return v->tv.vval.v_string;
3663 }
3664
3665 return name;
3666}
3667
3668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 * Allocate a variable for the result of a function.
3670 * Return OK or FAIL.
3671 */
3672 static int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003673get_func_vartv(name, len, retvar, arg, firstline, lastline, doesrange, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 char_u *name; /* name of the function */
3675 int len; /* length of "name" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003676 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 char_u **arg; /* argument, pointing to the '(' */
3678 linenr_T firstline; /* first line of range */
3679 linenr_T lastline; /* last line of range */
3680 int *doesrange; /* return: function handled range */
3681 int evaluate;
3682{
3683 char_u *argp;
3684 int ret = OK;
3685#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003686 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 int argcount = 0; /* number of arguments found */
3688
3689 /*
3690 * Get the arguments.
3691 */
3692 argp = *arg;
3693 while (argcount < MAX_FUNC_ARGS)
3694 {
3695 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
3696 if (*argp == ')' || *argp == ',' || *argp == NUL)
3697 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
3699 {
3700 ret = FAIL;
3701 break;
3702 }
3703 ++argcount;
3704 if (*argp != ',')
3705 break;
3706 }
3707 if (*argp == ')')
3708 ++argp;
3709 else
3710 ret = FAIL;
3711
3712 if (ret == OK)
3713 ret = call_func(name, len, retvar, argcount, argvars,
3714 firstline, lastline, doesrange, evaluate);
3715 else if (!aborting())
3716 EMSG2(_("E116: Invalid arguments for function %s"), name);
3717
3718 while (--argcount >= 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003719 clear_vartv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 *arg = skipwhite(argp);
3722 return ret;
3723}
3724
3725
3726/*
3727 * Call a function with its resolved parameters
3728 * Return OK or FAIL.
3729 */
3730 static int
3731call_func(name, len, retvar, argcount, argvars, firstline, lastline,
3732 doesrange, evaluate)
3733 char_u *name; /* name of the function */
3734 int len; /* length of "name" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003735 typeval *retvar; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003737 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 linenr_T firstline; /* first line of range */
3739 linenr_T lastline; /* last line of range */
3740 int *doesrange; /* return: function handled range */
3741 int evaluate;
3742{
3743 int ret = FAIL;
3744 static char *errors[] =
3745 {N_("E117: Unknown function: %s"),
3746 N_("E118: Too many arguments for function: %s"),
3747 N_("E119: Not enough arguments for function: %s"),
3748 N_("E120: Using <SID> not in a script context: %s"),
3749 };
3750#define ERROR_UNKNOWN 0
3751#define ERROR_TOOMANY 1
3752#define ERROR_TOOFEW 2
3753#define ERROR_SCRIPT 3
3754#define ERROR_NONE 4
3755#define ERROR_OTHER 5
3756 int error = ERROR_NONE;
3757 int i;
3758 int llen;
3759 ufunc_T *fp;
3760 int cc;
3761#define FLEN_FIXED 40
3762 char_u fname_buf[FLEN_FIXED + 1];
3763 char_u *fname;
3764
3765 /*
3766 * In a script change <SID>name() and s:name() to K_SNR 123_name().
3767 * Change <SNR>123_name() to K_SNR 123_name().
3768 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
3769 */
3770 cc = name[len];
3771 name[len] = NUL;
3772 llen = eval_fname_script(name);
3773 if (llen > 0)
3774 {
3775 fname_buf[0] = K_SPECIAL;
3776 fname_buf[1] = KS_EXTRA;
3777 fname_buf[2] = (int)KE_SNR;
3778 i = 3;
3779 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
3780 {
3781 if (current_SID <= 0)
3782 error = ERROR_SCRIPT;
3783 else
3784 {
3785 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
3786 i = (int)STRLEN(fname_buf);
3787 }
3788 }
3789 if (i + STRLEN(name + llen) < FLEN_FIXED)
3790 {
3791 STRCPY(fname_buf + i, name + llen);
3792 fname = fname_buf;
3793 }
3794 else
3795 {
3796 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
3797 if (fname == NULL)
3798 error = ERROR_OTHER;
3799 else
3800 {
3801 mch_memmove(fname, fname_buf, (size_t)i);
3802 STRCPY(fname + i, name + llen);
3803 }
3804 }
3805 }
3806 else
3807 fname = name;
3808
3809 *doesrange = FALSE;
3810
3811
3812 /* execute the function if no errors detected and executing */
3813 if (evaluate && error == ERROR_NONE)
3814 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003815 retvar->v_type = VAR_NUMBER; /* default is number retvar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 error = ERROR_UNKNOWN;
3817
3818 if (!ASCII_ISLOWER(fname[0]))
3819 {
3820 /*
3821 * User defined function.
3822 */
3823 fp = find_func(fname);
3824#ifdef FEAT_AUTOCMD
3825 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
3826 fname, fname, TRUE, NULL)
3827#ifdef FEAT_EVAL
3828 && !aborting()
3829#endif
3830 )
3831 {
3832 /* executed an autocommand, search for function again */
3833 fp = find_func(fname);
3834 }
3835#endif
3836 if (fp != NULL)
3837 {
3838 if (fp->flags & FC_RANGE)
3839 *doesrange = TRUE;
3840 if (argcount < fp->args.ga_len)
3841 error = ERROR_TOOFEW;
3842 else if (!fp->varargs && argcount > fp->args.ga_len)
3843 error = ERROR_TOOMANY;
3844 else
3845 {
3846 /*
3847 * Call the user function.
3848 * Save and restore search patterns, script variables and
3849 * redo buffer.
3850 */
3851 save_search_patterns();
3852 saveRedobuff();
3853 ++fp->calls;
3854 call_user_func(fp, argcount, argvars, retvar,
3855 firstline, lastline);
3856 --fp->calls;
3857 restoreRedobuff();
3858 restore_search_patterns();
3859 error = ERROR_NONE;
3860 }
3861 }
3862 }
3863 else
3864 {
3865 /*
3866 * Find the function name in the table, call its implementation.
3867 */
3868 i = find_internal_func(fname);
3869 if (i >= 0)
3870 {
3871 if (argcount < functions[i].f_min_argc)
3872 error = ERROR_TOOFEW;
3873 else if (argcount > functions[i].f_max_argc)
3874 error = ERROR_TOOMANY;
3875 else
3876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003877 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 functions[i].f_func(argvars, retvar);
3879 error = ERROR_NONE;
3880 }
3881 }
3882 }
3883 /*
3884 * The function call (or "FuncUndefined" autocommand sequence) might
3885 * have been aborted by an error, an interrupt, or an explicitly thrown
3886 * exception that has not been caught so far. This situation can be
3887 * tested for by calling aborting(). For an error in an internal
3888 * function or for the "E132" error in call_user_func(), however, the
3889 * throw point at which the "force_abort" flag (temporarily reset by
3890 * emsg()) is normally updated has not been reached yet. We need to
3891 * update that flag first to make aborting() reliable.
3892 */
3893 update_force_abort();
3894 }
3895 if (error == ERROR_NONE)
3896 ret = OK;
3897
3898 /*
3899 * Report an error unless the argument evaluation or function call has been
3900 * cancelled due to an aborting error, an interrupt, or an exception.
3901 */
3902 if (error < ERROR_NONE && !aborting())
3903 EMSG2((char_u *)_(errors[error]), name);
3904
3905 name[len] = cc;
3906 if (fname != name && fname != fname_buf)
3907 vim_free(fname);
3908
3909 return ret;
3910}
3911
3912/*********************************************
3913 * Implementation of the built-in functions
3914 */
3915
3916/*
3917 * "append(lnum, string)" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003918 * or "append(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 */
3920 static void
3921f_append(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003922 typeval *argvars;
3923 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 long lnum;
3926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003927 retvar->vval.v_number = 1; /* Default: Failed */
3928 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003930 if (argvars[0].vval.v_list != NULL
3931 && list_append_tv(argvars[0].vval.v_list, &argvars[1]) == OK)
3932 copy_vartv(&argvars[0], retvar);
3933 }
3934 else
3935 {
3936 lnum = get_vartv_lnum(argvars);
3937 if (lnum >= 0
3938 && lnum <= curbuf->b_ml.ml_line_count
3939 && u_save(lnum, lnum + 1) == OK)
3940 {
3941 ml_append(lnum, get_vartv_string(&argvars[1]), (colnr_T)0, FALSE);
3942 if (curwin->w_cursor.lnum > lnum)
3943 ++curwin->w_cursor.lnum;
3944 appended_lines_mark(lnum, 1L);
3945 retvar->vval.v_number = 0;
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948}
3949
3950/*
3951 * "argc()" function
3952 */
3953/* ARGSUSED */
3954 static void
3955f_argc(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003956 typeval *argvars;
3957 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003959 retvar->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960}
3961
3962/*
3963 * "argidx()" function
3964 */
3965/* ARGSUSED */
3966 static void
3967f_argidx(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 typeval *argvars;
3969 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003971 retvar->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972}
3973
3974/*
3975 * "argv(nr)" function
3976 */
3977 static void
3978f_argv(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979 typeval *argvars;
3980 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 int idx;
3983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984 idx = get_vartv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003986 retvar->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003988 retvar->vval.v_string = NULL;
3989 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
3991
3992/*
3993 * "browse(save, title, initdir, default)" function
3994 */
3995/* ARGSUSED */
3996 static void
3997f_browse(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003998 typeval *argvars;
3999 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001#ifdef FEAT_BROWSE
4002 int save;
4003 char_u *title;
4004 char_u *initdir;
4005 char_u *defname;
4006 char_u buf[NUMBUFLEN];
4007 char_u buf2[NUMBUFLEN];
4008
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004009 save = get_vartv_number(&argvars[0]);
4010 title = get_vartv_string(&argvars[1]);
4011 initdir = get_vartv_string_buf(&argvars[2], buf);
4012 defname = get_vartv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004014 retvar->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004015 do_browse(save ? BROWSE_SAVE : 0,
4016 title, defname, NULL, initdir, NULL, curbuf);
4017#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004018 retvar->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004019#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004020 retvar->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004021}
4022
4023/*
4024 * "browsedir(title, initdir)" function
4025 */
4026/* ARGSUSED */
4027 static void
4028f_browsedir(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004029 typeval *argvars;
4030 typeval *retvar;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004031{
4032#ifdef FEAT_BROWSE
4033 char_u *title;
4034 char_u *initdir;
4035 char_u buf[NUMBUFLEN];
4036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004037 title = get_vartv_string(&argvars[0]);
4038 initdir = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004039
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004040 retvar->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004041 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004043 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004045 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046}
4047
4048/*
4049 * Find a buffer by number or exact name.
4050 */
4051 static buf_T *
4052find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004053 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
4055 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004057 if (avar->v_type == VAR_NUMBER)
4058 buf = buflist_findnr((int)avar->vval.v_number);
4059 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004061 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004062 if (buf == NULL)
4063 {
4064 /* No full path name match, try a match with a URL or a "nofile"
4065 * buffer, these don't use the full path. */
4066 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4067 if (buf->b_fname != NULL
4068 && (path_with_url(buf->b_fname)
4069#ifdef FEAT_QUICKFIX
4070 || bt_nofile(buf)
4071#endif
4072 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004073 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004074 break;
4075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 return buf;
4078}
4079
4080/*
4081 * "bufexists(expr)" function
4082 */
4083 static void
4084f_bufexists(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004085 typeval *argvars;
4086 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004088 retvar->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089}
4090
4091/*
4092 * "buflisted(expr)" function
4093 */
4094 static void
4095f_buflisted(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004096 typeval *argvars;
4097 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 buf_T *buf;
4100
4101 buf = find_buffer(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004102 retvar->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103}
4104
4105/*
4106 * "bufloaded(expr)" function
4107 */
4108 static void
4109f_bufloaded(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004110 typeval *argvars;
4111 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 buf_T *buf;
4114
4115 buf = find_buffer(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116 retvar->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
4119/*
4120 * Get buffer by number or pattern.
4121 */
4122 static buf_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123get_buf_vartv(avar)
4124 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004126 char_u *name = avar->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 int save_magic;
4128 char_u *save_cpo;
4129 buf_T *buf;
4130
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 if (avar->v_type == VAR_NUMBER)
4132 return buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (name == NULL || *name == NUL)
4134 return curbuf;
4135 if (name[0] == '$' && name[1] == NUL)
4136 return lastbuf;
4137
4138 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
4139 save_magic = p_magic;
4140 p_magic = TRUE;
4141 save_cpo = p_cpo;
4142 p_cpo = (char_u *)"";
4143
4144 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
4145 TRUE, FALSE));
4146
4147 p_magic = save_magic;
4148 p_cpo = save_cpo;
4149
4150 /* If not found, try expanding the name, like done for bufexists(). */
4151 if (buf == NULL)
4152 buf = find_buffer(avar);
4153
4154 return buf;
4155}
4156
4157/*
4158 * "bufname(expr)" function
4159 */
4160 static void
4161f_bufname(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004162 typeval *argvars;
4163 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 buf_T *buf;
4166
4167 ++emsg_off;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004168 buf = get_buf_vartv(&argvars[0]);
4169 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004171 retvar->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004173 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 --emsg_off;
4175}
4176
4177/*
4178 * "bufnr(expr)" function
4179 */
4180 static void
4181f_bufnr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004182 typeval *argvars;
4183 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
4185 buf_T *buf;
4186
4187 ++emsg_off;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004188 buf = get_buf_vartv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (buf != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004190 retvar->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004192 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 --emsg_off;
4194}
4195
4196/*
4197 * "bufwinnr(nr)" function
4198 */
4199 static void
4200f_bufwinnr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004201 typeval *argvars;
4202 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204#ifdef FEAT_WINDOWS
4205 win_T *wp;
4206 int winnr = 0;
4207#endif
4208 buf_T *buf;
4209
4210 ++emsg_off;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 buf = get_buf_vartv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#ifdef FEAT_WINDOWS
4213 for (wp = firstwin; wp; wp = wp->w_next)
4214 {
4215 ++winnr;
4216 if (wp->w_buffer == buf)
4217 break;
4218 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004219 retvar->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 retvar->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#endif
4223 --emsg_off;
4224}
4225
4226/*
4227 * "byte2line(byte)" function
4228 */
4229/*ARGSUSED*/
4230 static void
4231f_byte2line(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232 typeval *argvars;
4233 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235#ifndef FEAT_BYTEOFF
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004236 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#else
4238 long boff = 0;
4239
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004240 boff = get_vartv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 if (boff < 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004242 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244 retvar->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 (linenr_T)0, &boff);
4246#endif
4247}
4248
4249/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004250 * "byteidx()" function
4251 */
4252/*ARGSUSED*/
4253 static void
4254f_byteidx(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255 typeval *argvars;
4256 typeval *retvar;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004257{
4258#ifdef FEAT_MBYTE
4259 char_u *t;
4260#endif
4261 char_u *str;
4262 long idx;
4263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 str = get_vartv_string(&argvars[0]);
4265 idx = get_vartv_number(&argvars[1]);
4266 retvar->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004267 if (idx < 0)
4268 return;
4269
4270#ifdef FEAT_MBYTE
4271 t = str;
4272 for ( ; idx > 0; idx--)
4273 {
4274 if (*t == NUL) /* EOL reached */
4275 return;
4276 t += mb_ptr2len_check(t);
4277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 retvar->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004279#else
4280 if (idx <= STRLEN(str))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004281 retvar->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004282#endif
4283}
4284
4285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 * "char2nr(string)" function
4287 */
4288 static void
4289f_char2nr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004290 typeval *argvars;
4291 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292{
4293#ifdef FEAT_MBYTE
4294 if (has_mbyte)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 retvar->vval.v_number =
4296 (*mb_ptr2char)(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 else
4298#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004299 retvar->vval.v_number = get_vartv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300}
4301
4302/*
4303 * "cindent(lnum)" function
4304 */
4305 static void
4306f_cindent(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004307 typeval *argvars;
4308 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309{
4310#ifdef FEAT_CINDENT
4311 pos_T pos;
4312 linenr_T lnum;
4313
4314 pos = curwin->w_cursor;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004315 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4317 {
4318 curwin->w_cursor.lnum = lnum;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004319 retvar->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 curwin->w_cursor = pos;
4321 }
4322 else
4323#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004324 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325}
4326
4327/*
4328 * "col(string)" function
4329 */
4330 static void
4331f_col(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004332 typeval *argvars;
4333 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
4335 colnr_T col = 0;
4336 pos_T *fp;
4337
4338 fp = var2fpos(&argvars[0], FALSE);
4339 if (fp != NULL)
4340 {
4341 if (fp->col == MAXCOL)
4342 {
4343 /* '> can be MAXCOL, get the length of the line then */
4344 if (fp->lnum <= curbuf->b_ml.ml_line_count)
4345 col = STRLEN(ml_get(fp->lnum)) + 1;
4346 else
4347 col = MAXCOL;
4348 }
4349 else
4350 {
4351 col = fp->col + 1;
4352#ifdef FEAT_VIRTUALEDIT
4353 /* col(".") when the cursor is on the NUL at the end of the line
4354 * because of "coladd" can be seen as an extra column. */
4355 if (virtual_active() && fp == &curwin->w_cursor)
4356 {
4357 char_u *p = ml_get_cursor();
4358
4359 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
4360 curwin->w_virtcol - curwin->w_cursor.coladd))
4361 {
4362# ifdef FEAT_MBYTE
4363 int l;
4364
4365 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
4366 col += l;
4367# else
4368 if (*p != NUL && p[1] == NUL)
4369 ++col;
4370# endif
4371 }
4372 }
4373#endif
4374 }
4375 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004376 retvar->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377}
4378
4379/*
4380 * "confirm(message, buttons[, default [, type]])" function
4381 */
4382/*ARGSUSED*/
4383 static void
4384f_confirm(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004385 typeval *argvars;
4386 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
4388#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4389 char_u *message;
4390 char_u *buttons = NULL;
4391 char_u buf[NUMBUFLEN];
4392 char_u buf2[NUMBUFLEN];
4393 int def = 1;
4394 int type = VIM_GENERIC;
4395 int c;
4396
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004397 message = get_vartv_string(&argvars[0]);
4398 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004400 buttons = get_vartv_string_buf(&argvars[1], buf);
4401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 def = get_vartv_number(&argvars[2]);
4404 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
4406 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 c = *get_vartv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 switch (TOUPPER_ASC(c))
4409 {
4410 case 'E': type = VIM_ERROR; break;
4411 case 'Q': type = VIM_QUESTION; break;
4412 case 'I': type = VIM_INFO; break;
4413 case 'W': type = VIM_WARNING; break;
4414 case 'G': type = VIM_GENERIC; break;
4415 }
4416 }
4417 }
4418 }
4419
4420 if (buttons == NULL || *buttons == NUL)
4421 buttons = (char_u *)_("&Ok");
4422
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 retvar->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 def, NULL);
4425#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427#endif
4428}
4429
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430/*
4431 * "copy()" function
4432 */
4433 static void
4434f_copy(argvars, retvar)
4435 typeval *argvars;
4436 typeval *retvar;
4437{
4438 if (argvars[0].v_type == VAR_LIST)
4439 {
4440 retvar->v_type = VAR_LIST;
4441 retvar->vval.v_list = list_copy(argvars[0].vval.v_list, FALSE);
4442 }
4443 else
4444 copy_vartv(&argvars[0], retvar);
4445}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447/*
4448 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
4449 *
4450 * Checks the existence of a cscope connection.
4451 */
4452/*ARGSUSED*/
4453 static void
4454f_cscope_connection(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 typeval *argvars;
4456 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458#ifdef FEAT_CSCOPE
4459 int num = 0;
4460 char_u *dbpath = NULL;
4461 char_u *prepend = NULL;
4462 char_u buf[NUMBUFLEN];
4463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 if (argvars[0].v_type != VAR_UNKNOWN
4465 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004467 num = (int)get_vartv_number(&argvars[0]);
4468 dbpath = get_vartv_string(&argvars[1]);
4469 if (argvars[2].v_type != VAR_UNKNOWN)
4470 prepend = get_vartv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 retvar->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476#endif
4477}
4478
4479/*
4480 * "cursor(lnum, col)" function
4481 *
4482 * Moves the cursor to the specified line and column
4483 */
4484/*ARGSUSED*/
4485 static void
4486f_cursor(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487 typeval *argvars;
4488 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
4490 long line, col;
4491
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004492 line = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (line > 0)
4494 curwin->w_cursor.lnum = line;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 col = get_vartv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (col > 0)
4497 curwin->w_cursor.col = col - 1;
4498#ifdef FEAT_VIRTUALEDIT
4499 curwin->w_cursor.coladd = 0;
4500#endif
4501
4502 /* Make sure the cursor is in a valid position. */
4503 check_cursor();
4504#ifdef FEAT_MBYTE
4505 /* Correct cursor for multi-byte character. */
4506 if (has_mbyte)
4507 mb_adjust_cursor();
4508#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004509
4510 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511}
4512
4513/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
4516 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517f_deepcopy(argvars, retvar)
4518 typeval *argvars;
4519 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004523 retvar->v_type = VAR_LIST;
4524 retvar->vval.v_list = list_copy(argvars[0].vval.v_list, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004526 else
4527 copy_vartv(&argvars[0], retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528}
4529
4530/*
4531 * "delete()" function
4532 */
4533 static void
4534f_delete(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535 typeval *argvars;
4536 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
4538 if (check_restricted() || check_secure())
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541 retvar->vval.v_number = mch_remove(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542}
4543
4544/*
4545 * "did_filetype()" function
4546 */
4547/*ARGSUSED*/
4548 static void
4549f_did_filetype(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550 typeval *argvars;
4551 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
4553#ifdef FEAT_AUTOCMD
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 retvar->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557#endif
4558}
4559
4560/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00004561 * "diff_filler()" function
4562 */
4563/*ARGSUSED*/
4564 static void
4565f_diff_filler(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 typeval *argvars;
4567 typeval *retvar;
Bram Moolenaar47136d72004-10-12 20:02:24 +00004568{
4569#ifdef FEAT_DIFF
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 retvar->vval.v_number = diff_check_fill(curwin, get_vartv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00004571#endif
4572}
4573
4574/*
4575 * "diff_hlID()" function
4576 */
4577/*ARGSUSED*/
4578 static void
4579f_diff_hlID(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 typeval *argvars;
4581 typeval *retvar;
Bram Moolenaar47136d72004-10-12 20:02:24 +00004582{
4583#ifdef FEAT_DIFF
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 linenr_T lnum = get_vartv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00004585 static linenr_T prev_lnum = 0;
4586 static int changedtick = 0;
4587 static int fnum = 0;
4588 static int change_start = 0;
4589 static int change_end = 0;
4590 static enum hlf_value hlID = 0;
4591 int filler_lines;
4592 int col;
4593
4594 if (lnum != prev_lnum
4595 || changedtick != curbuf->b_changedtick
4596 || fnum != curbuf->b_fnum)
4597 {
4598 /* New line, buffer, change: need to get the values. */
4599 filler_lines = diff_check(curwin, lnum);
4600 if (filler_lines < 0)
4601 {
4602 if (filler_lines == -1)
4603 {
4604 change_start = MAXCOL;
4605 change_end = -1;
4606 if (diff_find_change(curwin, lnum, &change_start, &change_end))
4607 hlID = HLF_ADD; /* added line */
4608 else
4609 hlID = HLF_CHD; /* changed line */
4610 }
4611 else
4612 hlID = HLF_ADD; /* added line */
4613 }
4614 else
4615 hlID = (enum hlf_value)0;
4616 prev_lnum = lnum;
4617 changedtick = curbuf->b_changedtick;
4618 fnum = curbuf->b_fnum;
4619 }
4620
4621 if (hlID == HLF_CHD || hlID == HLF_TXD)
4622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 col = get_vartv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00004624 if (col >= change_start && col <= change_end)
4625 hlID = HLF_TXD; /* changed text */
4626 else
4627 hlID = HLF_CHD; /* changed line */
4628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 retvar->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00004630#endif
4631}
4632
4633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 * "escape({string}, {chars})" function
4635 */
4636 static void
4637f_escape(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 typeval *argvars;
4639 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 char_u buf[NUMBUFLEN];
4642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 retvar->vval.v_string =
4644 vim_strsave_escaped(get_vartv_string(&argvars[0]),
4645 get_vartv_string_buf(&argvars[1], buf));
4646 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647}
4648
4649/*
4650 * "eventhandler()" function
4651 */
4652/*ARGSUSED*/
4653 static void
4654f_eventhandler(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 typeval *argvars;
4656 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 retvar->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659}
4660
4661/*
4662 * "executable()" function
4663 */
4664 static void
4665f_executable(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 typeval *argvars;
4667 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 retvar->vval.v_number = mch_can_exe(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670}
4671
4672/*
4673 * "exists()" function
4674 */
4675 static void
4676f_exists(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 typeval *argvars;
4678 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 char_u *p;
4681 char_u *name;
4682 int n = FALSE;
4683 int len = 0;
4684
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 p = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (*p == '$') /* environment variable */
4687 {
4688 /* first try "normal" environment variables (fast) */
4689 if (mch_getenv(p + 1) != NULL)
4690 n = TRUE;
4691 else
4692 {
4693 /* try expanding things like $VIM and ${HOME} */
4694 p = expand_env_save(p);
4695 if (p != NULL && *p != '$')
4696 n = TRUE;
4697 vim_free(p);
4698 }
4699 }
4700 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 n = (get_option_vartv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 else if (*p == '*') /* internal or user defined function */
4703 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 }
4706 else if (*p == ':')
4707 {
4708 n = cmd_exists(p + 1);
4709 }
4710 else if (*p == '#')
4711 {
4712#ifdef FEAT_AUTOCMD
4713 name = p + 1;
4714 p = vim_strchr(name, '#');
4715 if (p != NULL)
4716 n = au_exists(name, p, p + 1);
4717 else
4718 n = au_exists(name, name + STRLEN(name), NULL);
4719#endif
4720 }
4721 else /* internal variable */
4722 {
4723#ifdef FEAT_MAGIC_BRACES
4724 char_u *expr_start;
4725 char_u *expr_end;
4726 char_u *temp_string = NULL;
4727 char_u *s;
4728#endif
4729 name = p;
4730
4731#ifdef FEAT_MAGIC_BRACES
4732 /* Find the end of the name. */
4733 s = find_name_end(name, &expr_start, &expr_end);
4734 if (expr_start != NULL)
4735 {
4736 temp_string = make_expanded_name(name, expr_start, expr_end, s);
4737 if (temp_string != NULL)
4738 {
4739 len = STRLEN(temp_string);
4740 name = temp_string;
4741 }
4742 }
4743#endif
4744 if (len == 0)
4745 len = get_id_len(&p);
4746 if (len != 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 n = (get_var_vartv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749#ifdef FEAT_MAGIC_BRACES
4750 vim_free(temp_string);
4751#endif
4752 }
4753
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 retvar->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755}
4756
4757/*
4758 * "expand()" function
4759 */
4760 static void
4761f_expand(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 typeval *argvars;
4763 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 char_u *s;
4766 int len;
4767 char_u *errormsg;
4768 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
4769 expand_T xpc;
4770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 retvar->v_type = VAR_STRING;
4772 s = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (*s == '%' || *s == '#' || *s == '<')
4774 {
4775 ++emsg_off;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 retvar->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 --emsg_off;
4778 }
4779 else
4780 {
4781 /* When the optional second argument is non-zero, don't remove matches
4782 * for 'suffixes' and 'wildignore' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 if (argvars[1].v_type != VAR_UNKNOWN && get_vartv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 flags |= WILD_KEEP_ALL;
4785 ExpandInit(&xpc);
4786 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 retvar->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 ExpandCleanup(&xpc);
4789 }
4790}
4791
4792/*
4793 * "filereadable()" function
4794 */
4795 static void
4796f_filereadable(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 typeval *argvars;
4798 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 FILE *fd;
4801 char_u *p;
4802 int n;
4803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 p = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
4806 {
4807 n = TRUE;
4808 fclose(fd);
4809 }
4810 else
4811 n = FALSE;
4812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 retvar->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814}
4815
4816/*
4817 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
4818 * rights to write into.
4819 */
4820 static void
4821f_filewritable(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 typeval *argvars;
4823 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 char_u *p;
4826 int retval = 0;
4827#if defined(UNIX) || defined(VMS)
4828 int perm = 0;
4829#endif
4830
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 p = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#if defined(UNIX) || defined(VMS)
4833 perm = mch_getperm(p);
4834#endif
4835#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
4836 if (
4837# ifdef WIN3264
4838 mch_writable(p) &&
4839# else
4840# if defined(UNIX) || defined(VMS)
4841 (perm & 0222) &&
4842# endif
4843# endif
4844 mch_access((char *)p, W_OK) == 0
4845 )
4846#endif
4847 {
4848 ++retval;
4849 if (mch_isdir(p))
4850 ++retval;
4851 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 retvar->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853}
4854
4855/*
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004856 * "finddir({fname}[, {path}[, {count}]])" function
4857 */
4858 static void
4859f_finddir(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 typeval *argvars;
4861 typeval *retvar;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004862{
4863 f_findfilendir(argvars, retvar, TRUE);
4864}
4865
4866/*
4867 * "findfile({fname}[, {path}[, {count}]])" function
4868 */
4869 static void
4870f_findfile(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 typeval *argvars;
4872 typeval *retvar;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004873{
4874 f_findfilendir(argvars, retvar, FALSE);
4875}
4876
4877 static void
4878f_findfilendir(argvars, retvar, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 typeval *argvars;
4880 typeval *retvar;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004881 int dir;
4882{
4883#ifdef FEAT_SEARCHPATH
4884 char_u *fname;
4885 char_u *fresult = NULL;
4886 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4887 char_u *p;
4888 char_u pathbuf[NUMBUFLEN];
4889 int count = 1;
4890 int first = TRUE;
4891
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 fname = get_vartv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004893
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004895 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 p = get_vartv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004897 if (*p != NUL)
4898 path = p;
4899
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004900 if (argvars[2].v_type != VAR_UNKNOWN)
4901 count = get_vartv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004902 }
4903
4904 do
4905 {
4906 vim_free(fresult);
4907 fresult = find_file_in_path_option(first ? fname : NULL,
4908 first ? (int)STRLEN(fname) : 0,
4909 0, first, path, dir, NULL);
4910 first = FALSE;
4911 } while (--count > 0 && fresult != NULL);
4912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 retvar->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004914#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004915 retvar->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004916#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 retvar->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004918}
4919
4920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 * "fnamemodify({fname}, {mods})" function
4922 */
4923 static void
4924f_fnamemodify(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004925 typeval *argvars;
4926 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
4928 char_u *fname;
4929 char_u *mods;
4930 int usedlen = 0;
4931 int len;
4932 char_u *fbuf = NULL;
4933 char_u buf[NUMBUFLEN];
4934
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 fname = get_vartv_string(&argvars[0]);
4936 mods = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 len = (int)STRLEN(fname);
4938
4939 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
4940
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (fname == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 retvar->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 vim_free(fbuf);
4947}
4948
4949/*
4950 * "foldclosed()" function
4951 */
4952 static void
4953f_foldclosed(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 typeval *argvars;
4955 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 foldclosed_both(argvars, retvar, FALSE);
4958}
4959
4960/*
4961 * "foldclosedend()" function
4962 */
4963 static void
4964f_foldclosedend(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 typeval *argvars;
4966 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 foldclosed_both(argvars, retvar, TRUE);
4969}
4970
4971/*
4972 * "foldclosed()" function
4973 */
4974 static void
4975foldclosed_both(argvars, retvar, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976 typeval *argvars;
4977 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 int end;
4979{
4980#ifdef FEAT_FOLDING
4981 linenr_T lnum;
4982 linenr_T first, last;
4983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4986 {
4987 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4988 {
4989 if (end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990 retvar->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004992 retvar->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 return;
4994 }
4995 }
4996#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004997 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998}
4999
5000/*
5001 * "foldlevel()" function
5002 */
5003 static void
5004f_foldlevel(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 typeval *argvars;
5006 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
5008#ifdef FEAT_FOLDING
5009 linenr_T lnum;
5010
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013 retvar->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 else
5015#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005016 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017}
5018
5019/*
5020 * "foldtext()" function
5021 */
5022/*ARGSUSED*/
5023 static void
5024f_foldtext(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 typeval *argvars;
5026 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028#ifdef FEAT_FOLDING
5029 linenr_T lnum;
5030 char_u *s;
5031 char_u *r;
5032 int len;
5033 char *txt;
5034#endif
5035
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036 retvar->v_type = VAR_STRING;
5037 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#ifdef FEAT_FOLDING
5039 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
5040 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
5041 && vimvars[VV_FOLDDASHES].val != NULL)
5042 {
5043 /* Find first non-empty line in the fold. */
5044 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
5045 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
5046 {
5047 if (!linewhite(lnum))
5048 break;
5049 ++lnum;
5050 }
5051
5052 /* Find interesting text in this line. */
5053 s = skipwhite(ml_get(lnum));
5054 /* skip C comment-start */
5055 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005058 if (*skipwhite(s) == NUL
5059 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
5060 {
5061 s = skipwhite(ml_get(lnum + 1));
5062 if (*s == '*')
5063 s = skipwhite(s + 1);
5064 }
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 txt = _("+-%s%3ld lines: ");
5067 r = alloc((unsigned)(STRLEN(txt)
5068 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
5069 + 20 /* for %3ld */
5070 + STRLEN(s))); /* concatenated */
5071 if (r != NULL)
5072 {
5073 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
5074 (long)((linenr_T)vimvars[VV_FOLDEND].val
5075 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
5076 len = (int)STRLEN(r);
5077 STRCAT(r, s);
5078 /* remove 'foldmarker' and 'commentstring' */
5079 foldtext_cleanup(r + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 retvar->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 }
5083#endif
5084}
5085
5086/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005087 * "foldtextresult(lnum)" function
5088 */
5089/*ARGSUSED*/
5090 static void
5091f_foldtextresult(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 typeval *argvars;
5093 typeval *retvar;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005094{
5095#ifdef FEAT_FOLDING
5096 linenr_T lnum;
5097 char_u *text;
5098 char_u buf[51];
5099 foldinfo_T foldinfo;
5100 int fold_count;
5101#endif
5102
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 retvar->v_type = VAR_STRING;
5104 retvar->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005105#ifdef FEAT_FOLDING
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005106 lnum = get_vartv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005107 fold_count = foldedCount(curwin, lnum, &foldinfo);
5108 if (fold_count > 0)
5109 {
5110 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
5111 &foldinfo, buf);
5112 if (text == buf)
5113 text = vim_strsave(text);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 retvar->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005115 }
5116#endif
5117}
5118
5119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * "foreground()" function
5121 */
5122/*ARGSUSED*/
5123 static void
5124f_foreground(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125 typeval *argvars;
5126 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef FEAT_GUI
5130 if (gui.in_use)
5131 gui_mch_set_foreground();
5132#else
5133# ifdef WIN32
5134 win32_set_foreground();
5135# endif
5136#endif
5137}
5138
5139/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 * "function()" function
5141 */
5142/*ARGSUSED*/
5143 static void
5144f_function(argvars, retvar)
5145 typeval *argvars;
5146 typeval *retvar;
5147{
5148 char_u *s;
5149
5150 s = get_vartv_string(&argvars[0]);
5151 if (s == NULL || *s == NUL || isdigit(*s))
5152 EMSG2(_(e_invarg2), s);
5153 else if (!function_exists(s))
5154 EMSG2(_("E999: Unknown function: %s"), s);
5155 else
5156 {
5157 retvar->vval.v_string = vim_strsave(s);
5158 retvar->v_type = VAR_FUNC;
5159 }
5160}
5161
5162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 * "getchar()" function
5164 */
5165 static void
5166f_getchar(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 typeval *argvars;
5168 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 varnumber_T n;
5171
5172 ++no_mapping;
5173 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 /* getchar(): blocking wait. */
5176 n = safe_vgetc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 else if (get_vartv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 /* getchar(1): only check if char avail */
5179 n = vpeekc();
5180 else if (vpeekc() == NUL)
5181 /* getchar(0) and no char avail: return zero */
5182 n = 0;
5183 else
5184 /* getchar(0) and char avail: return char */
5185 n = safe_vgetc();
5186 --no_mapping;
5187 --allow_keys;
5188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 retvar->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 if (IS_SPECIAL(n) || mod_mask != 0)
5191 {
5192 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5193 int i = 0;
5194
5195 /* Turn a special key into three bytes, plus modifier. */
5196 if (mod_mask != 0)
5197 {
5198 temp[i++] = K_SPECIAL;
5199 temp[i++] = KS_MODIFIER;
5200 temp[i++] = mod_mask;
5201 }
5202 if (IS_SPECIAL(n))
5203 {
5204 temp[i++] = K_SPECIAL;
5205 temp[i++] = K_SECOND(n);
5206 temp[i++] = K_THIRD(n);
5207 }
5208#ifdef FEAT_MBYTE
5209 else if (has_mbyte)
5210 i += (*mb_char2bytes)(n, temp + i);
5211#endif
5212 else
5213 temp[i++] = n;
5214 temp[i++] = NUL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 retvar->v_type = VAR_STRING;
5216 retvar->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218}
5219
5220/*
5221 * "getcharmod()" function
5222 */
5223/*ARGSUSED*/
5224 static void
5225f_getcharmod(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 typeval *argvars;
5227 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 retvar->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230}
5231
5232/*
5233 * "getcmdline()" function
5234 */
5235/*ARGSUSED*/
5236 static void
5237f_getcmdline(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 typeval *argvars;
5239 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 retvar->v_type = VAR_STRING;
5242 retvar->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243}
5244
5245/*
5246 * "getcmdpos()" function
5247 */
5248/*ARGSUSED*/
5249 static void
5250f_getcmdpos(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 typeval *argvars;
5252 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 retvar->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255}
5256
5257/*
5258 * "getbufvar()" function
5259 */
5260 static void
5261f_getbufvar(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 typeval *argvars;
5263 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 buf_T *buf;
5266 buf_T *save_curbuf;
5267 char_u *varname;
5268 VAR v;
5269
5270 ++emsg_off;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 buf = get_buf_vartv(&argvars[0]);
5272 varname = get_vartv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 retvar->v_type = VAR_STRING;
5275 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 if (buf != NULL && varname != NULL)
5278 {
5279 if (*varname == '&') /* buffer-local-option */
5280 {
5281 /* set curbuf to be our buf, temporarily */
5282 save_curbuf = curbuf;
5283 curbuf = buf;
5284
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 get_option_vartv(&varname, retvar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
5287 /* restore previous notion of curbuf */
5288 curbuf = save_curbuf;
5289 }
5290 else
5291 {
5292 /* look up the variable */
5293 v = find_var_in_ga(&buf->b_vars, varname);
5294 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 copy_vartv(&v->tv, retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 }
5298
5299 --emsg_off;
5300}
5301
5302/*
5303 * "getcwd()" function
5304 */
5305/*ARGSUSED*/
5306 static void
5307f_getcwd(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 typeval *argvars;
5309 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
5311 char_u cwd[MAXPATHL];
5312
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 else
5317 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 retvar->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 slash_adjust(retvar->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322 }
5323}
5324
5325/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005326 * "getfontname()" function
5327 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005328/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005329 static void
5330f_getfontname(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 typeval *argvars;
5332 typeval *retvar;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005333{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 retvar->v_type = VAR_STRING;
5335 retvar->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005336#ifdef FEAT_GUI
5337 if (gui.in_use)
5338 {
5339 GuiFont font;
5340 char_u *name = NULL;
5341
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005343 {
5344 /* Get the "Normal" font. Either the name saved by
5345 * hl_set_font_name() or from the font ID. */
5346 font = gui.norm_font;
5347 name = hl_get_font_name();
5348 }
5349 else
5350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 name = get_vartv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005352 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5353 return;
5354 font = gui_mch_get_font(name, FALSE);
5355 if (font == NOFONT)
5356 return; /* Invalid font name, return empty string. */
5357 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 retvar->vval.v_string = gui_mch_get_fontname(font, name);
5359 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005360 gui_mch_free_font(font);
5361 }
5362#endif
5363}
5364
5365/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005366 * "getfperm({fname})" function
5367 */
5368 static void
5369f_getfperm(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 typeval *argvars;
5371 typeval *retvar;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005372{
5373 char_u *fname;
5374 struct stat st;
5375 char_u *perm = NULL;
5376 char_u flags[] = "rwx";
5377 int i;
5378
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 fname = get_vartv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005380
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 retvar->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005382 if (mch_stat((char *)fname, &st) >= 0)
5383 {
5384 perm = vim_strsave((char_u *)"---------");
5385 if (perm != NULL)
5386 {
5387 for (i = 0; i < 9; i++)
5388 {
5389 if (st.st_mode & (1 << (8 - i)))
5390 perm[i] = flags[i % 3];
5391 }
5392 }
5393 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 retvar->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005395}
5396
5397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 * "getfsize({fname})" function
5399 */
5400 static void
5401f_getfsize(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 typeval *argvars;
5403 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405 char_u *fname;
5406 struct stat st;
5407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 fname = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 retvar->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 if (mch_stat((char *)fname, &st) >= 0)
5413 {
5414 if (mch_isdir(fname))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 retvar->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421}
5422
5423/*
5424 * "getftime({fname})" function
5425 */
5426 static void
5427f_getftime(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 typeval *argvars;
5429 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 char_u *fname;
5432 struct stat st;
5433
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 fname = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 retvar->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440}
5441
5442/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005443 * "getftype({fname})" function
5444 */
5445 static void
5446f_getftype(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 typeval *argvars;
5448 typeval *retvar;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005449{
5450 char_u *fname;
5451 struct stat st;
5452 char_u *type = NULL;
5453 char *t;
5454
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 fname = get_vartv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005456
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 retvar->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005458 if (mch_lstat((char *)fname, &st) >= 0)
5459 {
5460#ifdef S_ISREG
5461 if (S_ISREG(st.st_mode))
5462 t = "file";
5463 else if (S_ISDIR(st.st_mode))
5464 t = "dir";
5465# ifdef S_ISLNK
5466 else if (S_ISLNK(st.st_mode))
5467 t = "link";
5468# endif
5469# ifdef S_ISBLK
5470 else if (S_ISBLK(st.st_mode))
5471 t = "bdev";
5472# endif
5473# ifdef S_ISCHR
5474 else if (S_ISCHR(st.st_mode))
5475 t = "cdev";
5476# endif
5477# ifdef S_ISFIFO
5478 else if (S_ISFIFO(st.st_mode))
5479 t = "fifo";
5480# endif
5481# ifdef S_ISSOCK
5482 else if (S_ISSOCK(st.st_mode))
5483 t = "fifo";
5484# endif
5485 else
5486 t = "other";
5487#else
5488# ifdef S_IFMT
5489 switch (st.st_mode & S_IFMT)
5490 {
5491 case S_IFREG: t = "file"; break;
5492 case S_IFDIR: t = "dir"; break;
5493# ifdef S_IFLNK
5494 case S_IFLNK: t = "link"; break;
5495# endif
5496# ifdef S_IFBLK
5497 case S_IFBLK: t = "bdev"; break;
5498# endif
5499# ifdef S_IFCHR
5500 case S_IFCHR: t = "cdev"; break;
5501# endif
5502# ifdef S_IFIFO
5503 case S_IFIFO: t = "fifo"; break;
5504# endif
5505# ifdef S_IFSOCK
5506 case S_IFSOCK: t = "socket"; break;
5507# endif
5508 default: t = "other";
5509 }
5510# else
5511 if (mch_isdir(fname))
5512 t = "dir";
5513 else
5514 t = "file";
5515# endif
5516#endif
5517 type = vim_strsave((char_u *)t);
5518 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 retvar->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005520}
5521
5522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 * "getreg()" function
5524 */
5525 static void
5526f_getreg(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 typeval *argvars;
5528 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 char_u *strregname;
5531 int regname;
5532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 if (argvars[0].v_type != VAR_UNKNOWN)
5534 strregname = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 else
5536 strregname = vimvars[VV_REG].val;
5537 regname = (strregname == NULL ? '"' : *strregname);
5538 if (regname == 0)
5539 regname = '"';
5540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 retvar->v_type = VAR_STRING;
5542 retvar->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
5544
5545/*
5546 * "getregtype()" function
5547 */
5548 static void
5549f_getregtype(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 typeval *argvars;
5551 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 char_u *strregname;
5554 int regname;
5555 char_u buf[NUMBUFLEN + 2];
5556 long reglen = 0;
5557
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 if (argvars[0].v_type != VAR_UNKNOWN)
5559 strregname = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 else
5561 /* Default to v:register */
5562 strregname = vimvars[VV_REG].val;
5563
5564 regname = (strregname == NULL ? '"' : *strregname);
5565 if (regname == 0)
5566 regname = '"';
5567
5568 buf[0] = NUL;
5569 buf[1] = NUL;
5570 switch (get_reg_type(regname, &reglen))
5571 {
5572 case MLINE: buf[0] = 'V'; break;
5573 case MCHAR: buf[0] = 'v'; break;
5574#ifdef FEAT_VISUAL
5575 case MBLOCK:
5576 buf[0] = Ctrl_V;
5577 sprintf((char *)buf + 1, "%ld", reglen + 1);
5578 break;
5579#endif
5580 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 retvar->v_type = VAR_STRING;
5582 retvar->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
5585/*
5586 * "getline(lnum)" function
5587 */
5588 static void
5589f_getline(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 typeval *argvars;
5591 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 linenr_T lnum;
5594 char_u *p;
5595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5599 p = ml_get(lnum);
5600 else
5601 p = (char_u *)"";
5602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 retvar->v_type = VAR_STRING;
5604 retvar->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605}
5606
5607/*
5608 * "getwinposx()" function
5609 */
5610/*ARGSUSED*/
5611 static void
5612f_getwinposx(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 typeval *argvars;
5614 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617#ifdef FEAT_GUI
5618 if (gui.in_use)
5619 {
5620 int x, y;
5621
5622 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 retvar->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625#endif
5626}
5627
5628/*
5629 * "getwinposy()" function
5630 */
5631/*ARGSUSED*/
5632 static void
5633f_getwinposy(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 typeval *argvars;
5635 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638#ifdef FEAT_GUI
5639 if (gui.in_use)
5640 {
5641 int x, y;
5642
5643 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 retvar->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646#endif
5647}
5648
5649/*
5650 * "getwinvar()" function
5651 */
5652 static void
5653f_getwinvar(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 typeval *argvars;
5655 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 win_T *win, *oldcurwin;
5658 char_u *varname;
5659 VAR v;
5660
5661 ++emsg_off;
5662 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 varname = get_vartv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 retvar->v_type = VAR_STRING;
5666 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (win != NULL && varname != NULL)
5669 {
5670 if (*varname == '&') /* window-local-option */
5671 {
5672 /* set curwin to be our win, temporarily */
5673 oldcurwin = curwin;
5674 curwin = win;
5675
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676 get_option_vartv(&varname, retvar, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /* restore previous notion of curwin */
5679 curwin = oldcurwin;
5680 }
5681 else
5682 {
5683 /* look up the variable */
5684 v = find_var_in_ga(&win->w_vars, varname);
5685 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 copy_vartv(&v->tv, retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 }
5689
5690 --emsg_off;
5691}
5692
5693/*
5694 * "glob()" function
5695 */
5696 static void
5697f_glob(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 typeval *argvars;
5699 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 expand_T xpc;
5702
5703 ExpandInit(&xpc);
5704 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 retvar->v_type = VAR_STRING;
5706 retvar->vval.v_string = ExpandOne(&xpc, get_vartv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
5708 ExpandCleanup(&xpc);
5709}
5710
5711/*
5712 * "globpath()" function
5713 */
5714 static void
5715f_globpath(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 typeval *argvars;
5717 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 char_u buf1[NUMBUFLEN];
5720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 retvar->v_type = VAR_STRING;
5722 retvar->vval.v_string = globpath(get_vartv_string(&argvars[0]),
5723 get_vartv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726/*
5727 * "has()" function
5728 */
5729 static void
5730f_has(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 typeval *argvars;
5732 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 int i;
5735 char_u *name;
5736 int n = FALSE;
5737 static char *(has_list[]) =
5738 {
5739#ifdef AMIGA
5740 "amiga",
5741# ifdef FEAT_ARP
5742 "arp",
5743# endif
5744#endif
5745#ifdef __BEOS__
5746 "beos",
5747#endif
5748#ifdef MSDOS
5749# ifdef DJGPP
5750 "dos32",
5751# else
5752 "dos16",
5753# endif
5754#endif
5755#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
5756 "mac",
5757#endif
5758#if defined(MACOS_X_UNIX)
5759 "macunix",
5760#endif
5761#ifdef OS2
5762 "os2",
5763#endif
5764#ifdef __QNX__
5765 "qnx",
5766#endif
5767#ifdef RISCOS
5768 "riscos",
5769#endif
5770#ifdef UNIX
5771 "unix",
5772#endif
5773#ifdef VMS
5774 "vms",
5775#endif
5776#ifdef WIN16
5777 "win16",
5778#endif
5779#ifdef WIN32
5780 "win32",
5781#endif
5782#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5783 "win32unix",
5784#endif
5785#ifdef WIN64
5786 "win64",
5787#endif
5788#ifdef EBCDIC
5789 "ebcdic",
5790#endif
5791#ifndef CASE_INSENSITIVE_FILENAME
5792 "fname_case",
5793#endif
5794#ifdef FEAT_ARABIC
5795 "arabic",
5796#endif
5797#ifdef FEAT_AUTOCMD
5798 "autocmd",
5799#endif
5800#ifdef FEAT_BEVAL
5801 "balloon_eval",
5802#endif
5803#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5804 "builtin_terms",
5805# ifdef ALL_BUILTIN_TCAPS
5806 "all_builtin_terms",
5807# endif
5808#endif
5809#ifdef FEAT_BYTEOFF
5810 "byte_offset",
5811#endif
5812#ifdef FEAT_CINDENT
5813 "cindent",
5814#endif
5815#ifdef FEAT_CLIENTSERVER
5816 "clientserver",
5817#endif
5818#ifdef FEAT_CLIPBOARD
5819 "clipboard",
5820#endif
5821#ifdef FEAT_CMDL_COMPL
5822 "cmdline_compl",
5823#endif
5824#ifdef FEAT_CMDHIST
5825 "cmdline_hist",
5826#endif
5827#ifdef FEAT_COMMENTS
5828 "comments",
5829#endif
5830#ifdef FEAT_CRYPT
5831 "cryptv",
5832#endif
5833#ifdef FEAT_CSCOPE
5834 "cscope",
5835#endif
5836#ifdef DEBUG
5837 "debug",
5838#endif
5839#ifdef FEAT_CON_DIALOG
5840 "dialog_con",
5841#endif
5842#ifdef FEAT_GUI_DIALOG
5843 "dialog_gui",
5844#endif
5845#ifdef FEAT_DIFF
5846 "diff",
5847#endif
5848#ifdef FEAT_DIGRAPHS
5849 "digraphs",
5850#endif
5851#ifdef FEAT_DND
5852 "dnd",
5853#endif
5854#ifdef FEAT_EMACS_TAGS
5855 "emacs_tags",
5856#endif
5857 "eval", /* always present, of course! */
5858#ifdef FEAT_EX_EXTRA
5859 "ex_extra",
5860#endif
5861#ifdef FEAT_SEARCH_EXTRA
5862 "extra_search",
5863#endif
5864#ifdef FEAT_FKMAP
5865 "farsi",
5866#endif
5867#ifdef FEAT_SEARCHPATH
5868 "file_in_path",
5869#endif
5870#ifdef FEAT_FIND_ID
5871 "find_in_path",
5872#endif
5873#ifdef FEAT_FOLDING
5874 "folding",
5875#endif
5876#ifdef FEAT_FOOTER
5877 "footer",
5878#endif
5879#if !defined(USE_SYSTEM) && defined(UNIX)
5880 "fork",
5881#endif
5882#ifdef FEAT_GETTEXT
5883 "gettext",
5884#endif
5885#ifdef FEAT_GUI
5886 "gui",
5887#endif
5888#ifdef FEAT_GUI_ATHENA
5889# ifdef FEAT_GUI_NEXTAW
5890 "gui_neXtaw",
5891# else
5892 "gui_athena",
5893# endif
5894#endif
5895#ifdef FEAT_GUI_BEOS
5896 "gui_beos",
5897#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00005898#ifdef FEAT_GUI_KDE
5899 "gui_kde",
5900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901#ifdef FEAT_GUI_GTK
5902 "gui_gtk",
5903# ifdef HAVE_GTK2
5904 "gui_gtk2",
5905# endif
5906#endif
5907#ifdef FEAT_GUI_MAC
5908 "gui_mac",
5909#endif
5910#ifdef FEAT_GUI_MOTIF
5911 "gui_motif",
5912#endif
5913#ifdef FEAT_GUI_PHOTON
5914 "gui_photon",
5915#endif
5916#ifdef FEAT_GUI_W16
5917 "gui_win16",
5918#endif
5919#ifdef FEAT_GUI_W32
5920 "gui_win32",
5921#endif
5922#ifdef FEAT_HANGULIN
5923 "hangul_input",
5924#endif
5925#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5926 "iconv",
5927#endif
5928#ifdef FEAT_INS_EXPAND
5929 "insert_expand",
5930#endif
5931#ifdef FEAT_JUMPLIST
5932 "jumplist",
5933#endif
5934#ifdef FEAT_KEYMAP
5935 "keymap",
5936#endif
5937#ifdef FEAT_LANGMAP
5938 "langmap",
5939#endif
5940#ifdef FEAT_LIBCALL
5941 "libcall",
5942#endif
5943#ifdef FEAT_LINEBREAK
5944 "linebreak",
5945#endif
5946#ifdef FEAT_LISP
5947 "lispindent",
5948#endif
5949#ifdef FEAT_LISTCMDS
5950 "listcmds",
5951#endif
5952#ifdef FEAT_LOCALMAP
5953 "localmap",
5954#endif
5955#ifdef FEAT_MENU
5956 "menu",
5957#endif
5958#ifdef FEAT_SESSION
5959 "mksession",
5960#endif
5961#ifdef FEAT_MODIFY_FNAME
5962 "modify_fname",
5963#endif
5964#ifdef FEAT_MOUSE
5965 "mouse",
5966#endif
5967#ifdef FEAT_MOUSESHAPE
5968 "mouseshape",
5969#endif
5970#if defined(UNIX) || defined(VMS)
5971# ifdef FEAT_MOUSE_DEC
5972 "mouse_dec",
5973# endif
5974# ifdef FEAT_MOUSE_GPM
5975 "mouse_gpm",
5976# endif
5977# ifdef FEAT_MOUSE_JSB
5978 "mouse_jsbterm",
5979# endif
5980# ifdef FEAT_MOUSE_NET
5981 "mouse_netterm",
5982# endif
5983# ifdef FEAT_MOUSE_PTERM
5984 "mouse_pterm",
5985# endif
5986# ifdef FEAT_MOUSE_XTERM
5987 "mouse_xterm",
5988# endif
5989#endif
5990#ifdef FEAT_MBYTE
5991 "multi_byte",
5992#endif
5993#ifdef FEAT_MBYTE_IME
5994 "multi_byte_ime",
5995#endif
5996#ifdef FEAT_MULTI_LANG
5997 "multi_lang",
5998#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005999#ifdef FEAT_MZSCHEME
6000 "mzscheme",
6001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#ifdef FEAT_OLE
6003 "ole",
6004#endif
6005#ifdef FEAT_OSFILETYPE
6006 "osfiletype",
6007#endif
6008#ifdef FEAT_PATH_EXTRA
6009 "path_extra",
6010#endif
6011#ifdef FEAT_PERL
6012#ifndef DYNAMIC_PERL
6013 "perl",
6014#endif
6015#endif
6016#ifdef FEAT_PYTHON
6017#ifndef DYNAMIC_PYTHON
6018 "python",
6019#endif
6020#endif
6021#ifdef FEAT_POSTSCRIPT
6022 "postscript",
6023#endif
6024#ifdef FEAT_PRINTER
6025 "printer",
6026#endif
6027#ifdef FEAT_QUICKFIX
6028 "quickfix",
6029#endif
6030#ifdef FEAT_RIGHTLEFT
6031 "rightleft",
6032#endif
6033#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6034 "ruby",
6035#endif
6036#ifdef FEAT_SCROLLBIND
6037 "scrollbind",
6038#endif
6039#ifdef FEAT_CMDL_INFO
6040 "showcmd",
6041 "cmdline_info",
6042#endif
6043#ifdef FEAT_SIGNS
6044 "signs",
6045#endif
6046#ifdef FEAT_SMARTINDENT
6047 "smartindent",
6048#endif
6049#ifdef FEAT_SNIFF
6050 "sniff",
6051#endif
6052#ifdef FEAT_STL_OPT
6053 "statusline",
6054#endif
6055#ifdef FEAT_SUN_WORKSHOP
6056 "sun_workshop",
6057#endif
6058#ifdef FEAT_NETBEANS_INTG
6059 "netbeans_intg",
6060#endif
6061#ifdef FEAT_SYN_HL
6062 "syntax",
6063#endif
6064#if defined(USE_SYSTEM) || !defined(UNIX)
6065 "system",
6066#endif
6067#ifdef FEAT_TAG_BINS
6068 "tag_binary",
6069#endif
6070#ifdef FEAT_TAG_OLDSTATIC
6071 "tag_old_static",
6072#endif
6073#ifdef FEAT_TAG_ANYWHITE
6074 "tag_any_white",
6075#endif
6076#ifdef FEAT_TCL
6077# ifndef DYNAMIC_TCL
6078 "tcl",
6079# endif
6080#endif
6081#ifdef TERMINFO
6082 "terminfo",
6083#endif
6084#ifdef FEAT_TERMRESPONSE
6085 "termresponse",
6086#endif
6087#ifdef FEAT_TEXTOBJ
6088 "textobjects",
6089#endif
6090#ifdef HAVE_TGETENT
6091 "tgetent",
6092#endif
6093#ifdef FEAT_TITLE
6094 "title",
6095#endif
6096#ifdef FEAT_TOOLBAR
6097 "toolbar",
6098#endif
6099#ifdef FEAT_USR_CMDS
6100 "user-commands", /* was accidentally included in 5.4 */
6101 "user_commands",
6102#endif
6103#ifdef FEAT_VIMINFO
6104 "viminfo",
6105#endif
6106#ifdef FEAT_VERTSPLIT
6107 "vertsplit",
6108#endif
6109#ifdef FEAT_VIRTUALEDIT
6110 "virtualedit",
6111#endif
6112#ifdef FEAT_VISUAL
6113 "visual",
6114#endif
6115#ifdef FEAT_VISUALEXTRA
6116 "visualextra",
6117#endif
6118#ifdef FEAT_VREPLACE
6119 "vreplace",
6120#endif
6121#ifdef FEAT_WILDIGN
6122 "wildignore",
6123#endif
6124#ifdef FEAT_WILDMENU
6125 "wildmenu",
6126#endif
6127#ifdef FEAT_WINDOWS
6128 "windows",
6129#endif
6130#ifdef FEAT_WAK
6131 "winaltkeys",
6132#endif
6133#ifdef FEAT_WRITEBACKUP
6134 "writebackup",
6135#endif
6136#ifdef FEAT_XIM
6137 "xim",
6138#endif
6139#ifdef FEAT_XFONTSET
6140 "xfontset",
6141#endif
6142#ifdef USE_XSMP
6143 "xsmp",
6144#endif
6145#ifdef USE_XSMP_INTERACT
6146 "xsmp_interact",
6147#endif
6148#ifdef FEAT_XCLIPBOARD
6149 "xterm_clipboard",
6150#endif
6151#ifdef FEAT_XTERM_SAVE
6152 "xterm_save",
6153#endif
6154#if defined(UNIX) && defined(FEAT_X11)
6155 "X11",
6156#endif
6157 NULL
6158 };
6159
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 name = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 for (i = 0; has_list[i] != NULL; ++i)
6162 if (STRICMP(name, has_list[i]) == 0)
6163 {
6164 n = TRUE;
6165 break;
6166 }
6167
6168 if (n == FALSE)
6169 {
6170 if (STRNICMP(name, "patch", 5) == 0)
6171 n = has_patch(atoi((char *)name + 5));
6172 else if (STRICMP(name, "vim_starting") == 0)
6173 n = (starting != 0);
6174#ifdef DYNAMIC_TCL
6175 else if (STRICMP(name, "tcl") == 0)
6176 n = tcl_enabled(FALSE);
6177#endif
6178#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6179 else if (STRICMP(name, "iconv") == 0)
6180 n = iconv_enabled(FALSE);
6181#endif
6182#ifdef DYNAMIC_RUBY
6183 else if (STRICMP(name, "ruby") == 0)
6184 n = ruby_enabled(FALSE);
6185#endif
6186#ifdef DYNAMIC_PYTHON
6187 else if (STRICMP(name, "python") == 0)
6188 n = python_enabled(FALSE);
6189#endif
6190#ifdef DYNAMIC_PERL
6191 else if (STRICMP(name, "perl") == 0)
6192 n = perl_enabled(FALSE);
6193#endif
6194#ifdef FEAT_GUI
6195 else if (STRICMP(name, "gui_running") == 0)
6196 n = (gui.in_use || gui.starting);
6197# ifdef FEAT_GUI_W32
6198 else if (STRICMP(name, "gui_win32s") == 0)
6199 n = gui_is_win32s();
6200# endif
6201# ifdef FEAT_BROWSE
6202 else if (STRICMP(name, "browse") == 0)
6203 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6204# endif
6205#endif
6206#ifdef FEAT_SYN_HL
6207 else if (STRICMP(name, "syntax_items") == 0)
6208 n = syntax_present(curbuf);
6209#endif
6210#if defined(WIN3264)
6211 else if (STRICMP(name, "win95") == 0)
6212 n = mch_windows95();
6213#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00006214#ifdef FEAT_NETBEANS_INTG
6215 else if (STRICMP(name, "netbeans_enabled") == 0)
6216 n = usingNetbeans;
6217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 retvar->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221}
6222
6223/*
6224 * "hasmapto()" function
6225 */
6226 static void
6227f_hasmapto(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 typeval *argvars;
6229 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 char_u *name;
6232 char_u *mode;
6233 char_u buf[NUMBUFLEN];
6234
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006235 name = get_vartv_string(&argvars[0]);
6236 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 mode = (char_u *)"nvo";
6238 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239 mode = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 if (map_to_exists(name, mode))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 retvar->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 retvar->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245}
6246
6247/*
6248 * "histadd()" function
6249 */
6250/*ARGSUSED*/
6251 static void
6252f_histadd(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 typeval *argvars;
6254 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256#ifdef FEAT_CMDHIST
6257 int histype;
6258 char_u *str;
6259 char_u buf[NUMBUFLEN];
6260#endif
6261
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 retvar->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (check_restricted() || check_secure())
6264 return;
6265#ifdef FEAT_CMDHIST
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 histype = get_histtype(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (histype >= 0)
6268 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 str = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 if (*str != NUL)
6271 {
6272 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 retvar->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 return;
6275 }
6276 }
6277#endif
6278}
6279
6280/*
6281 * "histdel()" function
6282 */
6283/*ARGSUSED*/
6284 static void
6285f_histdel(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 typeval *argvars;
6287 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289#ifdef FEAT_CMDHIST
6290 int n;
6291 char_u buf[NUMBUFLEN];
6292
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 /* only one argument: clear entire history */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 n = clr_history(get_histtype(get_vartv_string(&argvars[0])));
6296 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 /* index given: remove that entry */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 n = del_history_idx(get_histtype(get_vartv_string(&argvars[0])),
6299 (int)get_vartv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 else
6301 /* string given: remove all matching entries */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 n = del_history_entry(get_histtype(get_vartv_string(&argvars[0])),
6303 get_vartv_string_buf(&argvars[1], buf));
6304 retvar->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307#endif
6308}
6309
6310/*
6311 * "histget()" function
6312 */
6313/*ARGSUSED*/
6314 static void
6315f_histget(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316 typeval *argvars;
6317 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319#ifdef FEAT_CMDHIST
6320 int type;
6321 int idx;
6322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 type = get_histtype(get_vartv_string(&argvars[0]));
6324 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 idx = get_history_idx(type);
6326 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 idx = (int)get_vartv_number(&argvars[1]);
6328 retvar->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333}
6334
6335/*
6336 * "histnr()" function
6337 */
6338/*ARGSUSED*/
6339 static void
6340f_histnr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341 typeval *argvars;
6342 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 int i;
6345
6346#ifdef FEAT_CMDHIST
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 i = get_histtype(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (i >= HIST_CMD && i < HIST_COUNT)
6349 i = get_history_idx(i);
6350 else
6351#endif
6352 i = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 retvar->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356/*
6357 * "highlight_exists()" function
6358 */
6359 static void
6360f_hlexists(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 typeval *argvars;
6362 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 retvar->vval.v_number = highlight_exists(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365}
6366
6367/*
6368 * "highlightID(name)" function
6369 */
6370 static void
6371f_hlID(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 typeval *argvars;
6373 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 retvar->vval.v_number = syn_name2id(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376}
6377
6378/*
6379 * "hostname()" function
6380 */
6381/*ARGSUSED*/
6382 static void
6383f_hostname(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 typeval *argvars;
6385 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 char_u hostname[256];
6388
6389 mch_get_host_name(hostname, 256);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 retvar->v_type = VAR_STRING;
6391 retvar->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392}
6393
6394/*
6395 * iconv() function
6396 */
6397/*ARGSUSED*/
6398 static void
6399f_iconv(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 typeval *argvars;
6401 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403#ifdef FEAT_MBYTE
6404 char_u buf1[NUMBUFLEN];
6405 char_u buf2[NUMBUFLEN];
6406 char_u *from, *to, *str;
6407 vimconv_T vimconv;
6408#endif
6409
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410 retvar->v_type = VAR_STRING;
6411 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413#ifdef FEAT_MBYTE
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 str = get_vartv_string(&argvars[0]);
6415 from = enc_canonize(enc_skip(get_vartv_string_buf(&argvars[1], buf1)));
6416 to = enc_canonize(enc_skip(get_vartv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 vimconv.vc_type = CONV_NONE;
6418 convert_setup(&vimconv, from, to);
6419
6420 /* If the encodings are equal, no conversion needed. */
6421 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 retvar->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 retvar->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426 convert_setup(&vimconv, NULL, NULL);
6427 vim_free(from);
6428 vim_free(to);
6429#endif
6430}
6431
6432/*
6433 * "indent()" function
6434 */
6435 static void
6436f_indent(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437 typeval *argvars;
6438 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 linenr_T lnum;
6441
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 retvar->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447}
6448
6449static int inputsecret_flag = 0;
6450
6451/*
6452 * "input()" function
6453 * Also handles inputsecret() when inputsecret is set.
6454 */
6455 static void
6456f_input(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 typeval *argvars;
6458 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 char_u *prompt = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 char_u *p = NULL;
6462 int c;
6463 char_u buf[NUMBUFLEN];
6464 int cmd_silent_save = cmd_silent;
6465
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468#ifdef NO_CONSOLE_INPUT
6469 /* While starting up, there is no place to enter text. */
6470 if (no_console_input())
6471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006472 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 return;
6474 }
6475#endif
6476
6477 cmd_silent = FALSE; /* Want to see the prompt. */
6478 if (prompt != NULL)
6479 {
6480 /* Only the part of the message after the last NL is considered as
6481 * prompt for the command line */
6482 p = vim_strrchr(prompt, '\n');
6483 if (p == NULL)
6484 p = prompt;
6485 else
6486 {
6487 ++p;
6488 c = *p;
6489 *p = NUL;
6490 msg_start();
6491 msg_clr_eos();
6492 msg_puts_attr(prompt, echo_attr);
6493 msg_didout = FALSE;
6494 msg_starthere();
6495 *p = c;
6496 }
6497 cmdline_row = msg_row;
6498 }
6499
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 if (argvars[1].v_type != VAR_UNKNOWN)
6501 stuffReadbuffSpec(get_vartv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 retvar->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
6505
6506 /* since the user typed this, no need to wait for return */
6507 need_wait_return = FALSE;
6508 msg_didout = FALSE;
6509 cmd_silent = cmd_silent_save;
6510}
6511
6512/*
6513 * "inputdialog()" function
6514 */
6515 static void
6516f_inputdialog(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 typeval *argvars;
6518 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520#if defined(FEAT_GUI_TEXTDIALOG)
6521 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6522 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6523 {
6524 char_u *message;
6525 char_u buf[NUMBUFLEN];
6526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 message = get_vartv_string(&argvars[0]);
6528 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 STRNCPY(IObuff, get_vartv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 IObuff[IOSIZE - 1] = NUL;
6532 }
6533 else
6534 IObuff[0] = NUL;
6535 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
6536 1, IObuff) == 1)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 retvar->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 else
6539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 if (argvars[1].v_type != VAR_UNKNOWN
6541 && argvars[2].v_type != VAR_UNKNOWN)
6542 retvar->vval.v_string = vim_strsave(
6543 get_vartv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549 else
6550#endif
6551 f_input(argvars, retvar);
6552}
6553
6554static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6555
6556/*
6557 * "inputrestore()" function
6558 */
6559/*ARGSUSED*/
6560 static void
6561f_inputrestore(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 typeval *argvars;
6563 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 if (ga_userinput.ga_len > 0)
6566 {
6567 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6569 + ga_userinput.ga_len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 retvar->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572 else if (p_verbose > 1)
6573 {
6574 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 retvar->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 }
6577}
6578
6579/*
6580 * "inputsave()" function
6581 */
6582/*ARGSUSED*/
6583 static void
6584f_inputsave(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 typeval *argvars;
6586 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 /* Add an entry to the stack of typehead storage. */
6589 if (ga_grow(&ga_userinput, 1) == OK)
6590 {
6591 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6592 + ga_userinput.ga_len);
6593 ++ga_userinput.ga_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 retvar->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 retvar->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598}
6599
6600/*
6601 * "inputsecret()" function
6602 */
6603 static void
6604f_inputsecret(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 typeval *argvars;
6606 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 ++cmdline_star;
6609 ++inputsecret_flag;
6610 f_input(argvars, retvar);
6611 --cmdline_star;
6612 --inputsecret_flag;
6613}
6614
6615/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 * "insert()" function
6617 */
6618 static void
6619f_insert(argvars, retvar)
6620 typeval *argvars;
6621 typeval *retvar;
6622{
6623 long before = 0;
6624 long n;
6625 listitem *item;
6626 listitem *ni;
6627 listvar *l;
6628
6629 if (argvars[0].v_type != VAR_LIST)
6630 EMSG(_("E999: First argument of insert() must be a list"));
6631 else if ((l = argvars[0].vval.v_list) != NULL)
6632 {
6633 if (argvars[2].v_type != VAR_UNKNOWN)
6634 before = get_vartv_number(&argvars[2]);
6635
6636 if (before < 0)
6637 {
6638 /* Count from the end: -1 is before last item. */
6639 item = l->lv_last;
6640 for (n = before + 1; n < 0 && item != NULL; ++n)
6641 item = item->li_prev;
6642 if (item == NULL)
6643 n = 1; /* report an error, don't append */
6644 }
6645 else
6646 {
6647 /* Can't use list_find() here, we allow one past the end. */
6648 item = l->lv_first;
6649 for (n = before; n > 0 && item != NULL; --n)
6650 item = item->li_next;
6651 }
6652 if (n > 0)
6653 EMSGN(_(e_listidx), before);
6654 else
6655 {
6656 ni = listitem_alloc();
6657 if (ni != NULL)
6658 {
6659 copy_vartv(&argvars[1], &ni->li_tv);
6660 if (item == NULL)
6661 /* Append new item at end of list. */
6662 list_append(l, ni);
6663 else
6664 {
6665 /* Insert new item before existing item. */
6666 ni->li_prev = item->li_prev;
6667 ni->li_next = item;
6668 if (item->li_prev == NULL)
6669 l->lv_first = ni;
6670 else
6671 item->li_prev->li_next = ni;
6672 item->li_prev = ni;
6673 }
6674 copy_vartv(&argvars[0], retvar);
6675 }
6676 }
6677 }
6678}
6679
6680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 * "isdirectory()" function
6682 */
6683 static void
6684f_isdirectory(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685 typeval *argvars;
6686 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688 retvar->vval.v_number = mch_isdir(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689}
6690
6691/*
6692 * "last_buffer_nr()" function.
6693 */
6694/*ARGSUSED*/
6695 static void
6696f_last_buffer_nr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 typeval *argvars;
6698 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 int n = 0;
6701 buf_T *buf;
6702
6703 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6704 if (n < buf->b_fnum)
6705 n = buf->b_fnum;
6706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 retvar->vval.v_number = n;
6708}
6709
6710/*
6711 * "len()" function
6712 */
6713 static void
6714f_len(argvars, retvar)
6715 typeval *argvars;
6716 typeval *retvar;
6717{
6718 switch (argvars[0].v_type)
6719 {
6720 case VAR_STRING:
6721 case VAR_NUMBER:
6722 retvar->vval.v_number = (varnumber_T)STRLEN(
6723 get_vartv_string(&argvars[0]));
6724 break;
6725 case VAR_LIST:
6726 retvar->vval.v_number = list_len(argvars[0].vval.v_list);
6727 break;
6728 default:
6729 EMSG(_("E999: Invalid type for len()"));
6730 break;
6731 }
6732}
6733
6734/*
6735 * "libcall()" function
6736 */
6737 static void
6738f_libcall(argvars, retvar)
6739 typeval *argvars;
6740 typeval *retvar;
6741{
6742 libcall_common(argvars, retvar, VAR_STRING);
6743}
6744
6745/*
6746 * "libcallnr()" function
6747 */
6748 static void
6749f_libcallnr(argvars, retvar)
6750 typeval *argvars;
6751 typeval *retvar;
6752{
6753 libcall_common(argvars, retvar, VAR_NUMBER);
6754}
6755
6756 static void
6757libcall_common(argvars, retvar, type)
6758 typeval *argvars;
6759 typeval *retvar;
6760 int type;
6761{
6762#ifdef FEAT_LIBCALL
6763 char_u *string_in;
6764 char_u **string_result;
6765 int nr_result;
6766#endif
6767
6768 retvar->v_type = type;
6769 if (type == VAR_NUMBER)
6770 retvar->vval.v_number = 0;
6771 else
6772 retvar->vval.v_string = NULL;
6773
6774 if (check_restricted() || check_secure())
6775 return;
6776
6777#ifdef FEAT_LIBCALL
6778 /* The first two args must be strings, otherwise its meaningless */
6779 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6780 {
6781 if (argvars[2].v_type == VAR_NUMBER)
6782 string_in = NULL;
6783 else
6784 string_in = argvars[2].vval.v_string;
6785 if (type == VAR_NUMBER)
6786 string_result = NULL;
6787 else
6788 string_result = &retvar->vval.v_string;
6789 if (mch_libcall(argvars[0].vval.v_string,
6790 argvars[1].vval.v_string,
6791 string_in,
6792 argvars[2].vval.v_number,
6793 string_result,
6794 &nr_result) == OK
6795 && type == VAR_NUMBER)
6796 retvar->vval.v_number = nr_result;
6797 }
6798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799}
6800
6801/*
6802 * "line(string)" function
6803 */
6804 static void
6805f_line(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006806 typeval *argvars;
6807 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 linenr_T lnum = 0;
6810 pos_T *fp;
6811
6812 fp = var2fpos(&argvars[0], TRUE);
6813 if (fp != NULL)
6814 lnum = fp->lnum;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815 retvar->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816}
6817
6818/*
6819 * "line2byte(lnum)" function
6820 */
6821/*ARGSUSED*/
6822 static void
6823f_line2byte(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 typeval *argvars;
6825 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
6827#ifndef FEAT_BYTEOFF
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006828 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829#else
6830 linenr_T lnum;
6831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 retvar->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6837 if (retvar->vval.v_number >= 0)
6838 ++retvar->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839#endif
6840}
6841
6842/*
6843 * "lispindent(lnum)" function
6844 */
6845 static void
6846f_lispindent(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 typeval *argvars;
6848 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850#ifdef FEAT_LISP
6851 pos_T pos;
6852 linenr_T lnum;
6853
6854 pos = curwin->w_cursor;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6857 {
6858 curwin->w_cursor.lnum = lnum;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006859 retvar->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 curwin->w_cursor = pos;
6861 }
6862 else
6863#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006864 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865}
6866
6867/*
6868 * "localtime()" function
6869 */
6870/*ARGSUSED*/
6871 static void
6872f_localtime(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006873 typeval *argvars;
6874 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006876 retvar->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878
6879/*
6880 * "maparg()" function
6881 */
6882 static void
6883f_maparg(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006884 typeval *argvars;
6885 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 get_maparg(argvars, retvar, TRUE);
6888}
6889
6890/*
6891 * "mapcheck()" function
6892 */
6893 static void
6894f_mapcheck(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006895 typeval *argvars;
6896 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 get_maparg(argvars, retvar, FALSE);
6899}
6900
6901 static void
6902get_maparg(argvars, retvar, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006903 typeval *argvars;
6904 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 int exact;
6906{
6907 char_u *keys;
6908 char_u *which;
6909 char_u buf[NUMBUFLEN];
6910 char_u *keys_buf = NULL;
6911 char_u *rhs;
6912 int mode;
6913 garray_T ga;
6914
6915 /* return empty string for failure */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006916 retvar->v_type = VAR_STRING;
6917 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006919 keys = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (*keys == NUL)
6921 return;
6922
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006923 if (argvars[1].v_type != VAR_UNKNOWN)
6924 which = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 else
6926 which = (char_u *)"";
6927 mode = get_map_mode(&which, 0);
6928
6929 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
6930 rhs = check_map(keys, mode, exact);
6931 vim_free(keys_buf);
6932 if (rhs != NULL)
6933 {
6934 ga_init(&ga);
6935 ga.ga_itemsize = 1;
6936 ga.ga_growsize = 40;
6937
6938 while (*rhs != NUL)
6939 ga_concat(&ga, str2special(&rhs, FALSE));
6940
6941 ga_append(&ga, NUL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006942 retvar->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944}
6945
6946/*
6947 * "match()" function
6948 */
6949 static void
6950f_match(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006951 typeval *argvars;
6952 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 find_some_match(argvars, retvar, 1);
6955}
6956
6957/*
6958 * "matchend()" function
6959 */
6960 static void
6961f_matchend(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006962 typeval *argvars;
6963 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 find_some_match(argvars, retvar, 0);
6966}
6967
6968/*
6969 * "matchstr()" function
6970 */
6971 static void
6972f_matchstr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006973 typeval *argvars;
6974 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 find_some_match(argvars, retvar, 2);
6977}
6978
6979 static void
6980find_some_match(argvars, retvar, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006981 typeval *argvars;
6982 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 int type;
6984{
6985 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006986 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 char_u *pat;
6988 regmatch_T regmatch;
6989 char_u patbuf[NUMBUFLEN];
6990 char_u *save_cpo;
6991 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006992 long nth = 1;
6993 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6996 save_cpo = p_cpo;
6997 p_cpo = (char_u *)"";
6998
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006999 expr = str = get_vartv_string(&argvars[0]);
7000 pat = get_vartv_string_buf(&argvars[1], patbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 if (type == 2)
7003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007004 retvar->v_type = VAR_STRING;
7005 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007008 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007012 start = get_vartv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (start < 0)
7014 start = 0;
7015 if (start > (long)STRLEN(str))
7016 goto theend;
7017 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007018
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007019 if (argvars[3].v_type != VAR_UNKNOWN)
7020 nth = get_vartv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022
7023 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7024 if (regmatch.regprog != NULL)
7025 {
7026 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007027
7028 while (1)
7029 {
7030 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
7031 if (!match || --nth <= 0)
7032 break;
7033 /* Advance to just after the match. */
7034#ifdef FEAT_MBYTE
7035 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
7036#else
7037 str = regmatch.startp[0] + 1;
7038#endif
7039 }
7040
7041 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {
7043 if (type == 2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007044 retvar->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 (int)(regmatch.endp[0] - regmatch.startp[0]));
7046 else
7047 {
7048 if (type != 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007049 retvar->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 (varnumber_T)(regmatch.startp[0] - str);
7051 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007052 retvar->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007054 retvar->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
7056 }
7057 vim_free(regmatch.regprog);
7058 }
7059
7060theend:
7061 p_cpo = save_cpo;
7062}
7063
7064/*
7065 * "mode()" function
7066 */
7067/*ARGSUSED*/
7068 static void
7069f_mode(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007070 typeval *argvars;
7071 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 char_u buf[2];
7074
7075#ifdef FEAT_VISUAL
7076 if (VIsual_active)
7077 {
7078 if (VIsual_select)
7079 buf[0] = VIsual_mode + 's' - 'v';
7080 else
7081 buf[0] = VIsual_mode;
7082 }
7083 else
7084#endif
7085 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
7086 buf[0] = 'r';
7087 else if (State & INSERT)
7088 {
7089 if (State & REPLACE_FLAG)
7090 buf[0] = 'R';
7091 else
7092 buf[0] = 'i';
7093 }
7094 else if (State & CMDLINE)
7095 buf[0] = 'c';
7096 else
7097 buf[0] = 'n';
7098
7099 buf[1] = NUL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 retvar->vval.v_string = vim_strsave(buf);
7101 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
7104/*
7105 * "nr2char()" function
7106 */
7107 static void
7108f_nr2char(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 typeval *argvars;
7110 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 char_u buf[NUMBUFLEN];
7113
7114#ifdef FEAT_MBYTE
7115 if (has_mbyte)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 buf[(*mb_char2bytes)((int)get_vartv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 else
7118#endif
7119 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007120 buf[0] = (char_u)get_vartv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 buf[1] = NUL;
7122 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007123 retvar->v_type = VAR_STRING;
7124 retvar->vval.v_string = vim_strsave(buf);
7125}
7126
7127/*
7128 * "remove({list}, {idx})" function
7129 */
7130 static void
7131f_remove(argvars, retvar)
7132 typeval *argvars;
7133 typeval *retvar;
7134{
7135 listvar *l;
7136 listitem *item;
7137 long idx;
7138
7139 if (argvars[0].v_type != VAR_LIST)
7140 EMSG(_("E999: First argument of remove() must be a list"));
7141 else if ((l = argvars[0].vval.v_list) != NULL)
7142 {
7143 idx = get_vartv_number(&argvars[1]);
7144 item = list_getrem(l, idx);
7145 if (item == NULL)
7146 EMSGN(_(e_listidx), idx);
7147 else
7148 {
7149 *retvar = item->li_tv;
7150 vim_free(item);
7151 }
7152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153}
7154
7155/*
7156 * "rename({from}, {to})" function
7157 */
7158 static void
7159f_rename(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007160 typeval *argvars;
7161 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163 char_u buf[NUMBUFLEN];
7164
7165 if (check_restricted() || check_secure())
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 retvar->vval.v_number = vim_rename(get_vartv_string(&argvars[0]),
7169 get_vartv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170}
7171
7172/*
7173 * "resolve()" function
7174 */
7175 static void
7176f_resolve(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007177 typeval *argvars;
7178 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 char_u *p;
7181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 p = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183#ifdef FEAT_SHORTCUT
7184 {
7185 char_u *v = NULL;
7186
7187 v = mch_resolve_shortcut(p);
7188 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007189 retvar->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007191 retvar->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193#else
7194# ifdef HAVE_READLINK
7195 {
7196 char_u buf[MAXPATHL + 1];
7197 char_u *cpy;
7198 int len;
7199 char_u *remain = NULL;
7200 char_u *q;
7201 int is_relative_to_current = FALSE;
7202 int has_trailing_pathsep = FALSE;
7203 int limit = 100;
7204
7205 p = vim_strsave(p);
7206
7207 if (p[0] == '.' && (vim_ispathsep(p[1])
7208 || (p[1] == '.' && (vim_ispathsep(p[2])))))
7209 is_relative_to_current = TRUE;
7210
7211 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007212 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 has_trailing_pathsep = TRUE;
7214
7215 q = getnextcomp(p);
7216 if (*q != NUL)
7217 {
7218 /* Separate the first path component in "p", and keep the
7219 * remainder (beginning with the path separator). */
7220 remain = vim_strsave(q - 1);
7221 q[-1] = NUL;
7222 }
7223
7224 for (;;)
7225 {
7226 for (;;)
7227 {
7228 len = readlink((char *)p, (char *)buf, MAXPATHL);
7229 if (len <= 0)
7230 break;
7231 buf[len] = NUL;
7232
7233 if (limit-- == 0)
7234 {
7235 vim_free(p);
7236 vim_free(remain);
7237 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007238 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 goto fail;
7240 }
7241
7242 /* Ensure that the result will have a trailing path separator
7243 * if the argument has one. */
7244 if (remain == NULL && has_trailing_pathsep)
7245 add_pathsep(buf);
7246
7247 /* Separate the first path component in the link value and
7248 * concatenate the remainders. */
7249 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
7250 if (*q != NUL)
7251 {
7252 if (remain == NULL)
7253 remain = vim_strsave(q - 1);
7254 else
7255 {
7256 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
7257 if (cpy != NULL)
7258 {
7259 STRCAT(cpy, remain);
7260 vim_free(remain);
7261 remain = cpy;
7262 }
7263 }
7264 q[-1] = NUL;
7265 }
7266
7267 q = gettail(p);
7268 if (q > p && *q == NUL)
7269 {
7270 /* Ignore trailing path separator. */
7271 q[-1] = NUL;
7272 q = gettail(p);
7273 }
7274 if (q > p && !mch_isFullName(buf))
7275 {
7276 /* symlink is relative to directory of argument */
7277 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
7278 if (cpy != NULL)
7279 {
7280 STRCPY(cpy, p);
7281 STRCPY(gettail(cpy), buf);
7282 vim_free(p);
7283 p = cpy;
7284 }
7285 }
7286 else
7287 {
7288 vim_free(p);
7289 p = vim_strsave(buf);
7290 }
7291 }
7292
7293 if (remain == NULL)
7294 break;
7295
7296 /* Append the first path component of "remain" to "p". */
7297 q = getnextcomp(remain + 1);
7298 len = q - remain - (*q != NUL);
7299 cpy = vim_strnsave(p, STRLEN(p) + len);
7300 if (cpy != NULL)
7301 {
7302 STRNCAT(cpy, remain, len);
7303 vim_free(p);
7304 p = cpy;
7305 }
7306 /* Shorten "remain". */
7307 if (*q != NUL)
7308 STRCPY(remain, q - 1);
7309 else
7310 {
7311 vim_free(remain);
7312 remain = NULL;
7313 }
7314 }
7315
7316 /* If the result is a relative path name, make it explicitly relative to
7317 * the current directory if and only if the argument had this form. */
7318 if (!vim_ispathsep(*p))
7319 {
7320 if (is_relative_to_current
7321 && *p != NUL
7322 && !(p[0] == '.'
7323 && (p[1] == NUL
7324 || vim_ispathsep(p[1])
7325 || (p[1] == '.'
7326 && (p[2] == NUL
7327 || vim_ispathsep(p[2]))))))
7328 {
7329 /* Prepend "./". */
7330 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
7331 if (cpy != NULL)
7332 {
7333 STRCAT(cpy, p);
7334 vim_free(p);
7335 p = cpy;
7336 }
7337 }
7338 else if (!is_relative_to_current)
7339 {
7340 /* Strip leading "./". */
7341 q = p;
7342 while (q[0] == '.' && vim_ispathsep(q[1]))
7343 q += 2;
7344 if (q > p)
7345 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
7346 }
7347 }
7348
7349 /* Ensure that the result will have no trailing path separator
7350 * if the argument had none. But keep "/" or "//". */
7351 if (!has_trailing_pathsep)
7352 {
7353 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007354 if (after_pathsep(p, q))
7355 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 }
7357
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007358 retvar->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
7360# else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007361 retvar->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362# endif
7363#endif
7364
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 simplify_filename(retvar->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
7367#ifdef HAVE_READLINK
7368fail:
7369#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371}
7372
7373/*
7374 * "simplify()" function
7375 */
7376 static void
7377f_simplify(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007378 typeval *argvars;
7379 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 char_u *p;
7382
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007383 p = get_vartv_string(&argvars[0]);
7384 retvar->vval.v_string = vim_strsave(p);
7385 simplify_filename(retvar->vval.v_string); /* simplify in place */
7386 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387}
7388
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007389#define SP_NOMOVE 1 /* don't move cursor */
7390#define SP_REPEAT 2 /* repeat to find outer pair */
7391#define SP_RETCOUNT 4 /* return matchcount */
7392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393/*
7394 * "search()" function
7395 */
7396 static void
7397f_search(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007398 typeval *argvars;
7399 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401 char_u *pat;
7402 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007403 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int save_p_ws = p_ws;
7405 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007406 int flags = 0;
7407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007408 retvar->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007410 pat = get_vartv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007411 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
7412 if (dir == 0)
7413 goto theend;
7414 if ((flags & ~SP_NOMOVE) != 0)
7415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416 EMSG2(_(e_invarg2), get_vartv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007417 goto theend;
7418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007420 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
7422 SEARCH_KEEP, RE_SEARCH) != FAIL)
7423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007424 retvar->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 curwin->w_cursor = pos;
7426 /* "/$" will put the cursor after the end of the line, may need to
7427 * correct that here */
7428 check_cursor();
7429 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007430
7431 /* If 'n' flag is used: restore cursor position. */
7432 if (flags & SP_NOMOVE)
7433 curwin->w_cursor = save_cursor;
7434theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 p_ws = save_p_ws;
7436}
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438/*
7439 * "searchpair()" function
7440 */
7441 static void
7442f_searchpair(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007443 typeval *argvars;
7444 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 char_u *spat, *mpat, *epat;
7447 char_u *skip;
7448 char_u *pat, *pat2, *pat3;
7449 pos_T pos;
7450 pos_T firstpos;
7451 pos_T save_cursor;
7452 pos_T save_pos;
7453 int save_p_ws = p_ws;
7454 char_u *save_cpo;
7455 int dir;
7456 int flags = 0;
7457 char_u nbuf1[NUMBUFLEN];
7458 char_u nbuf2[NUMBUFLEN];
7459 char_u nbuf3[NUMBUFLEN];
7460 int n;
7461 int r;
7462 int nest = 1;
7463 int err;
7464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007465 retvar->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
7467 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7468 save_cpo = p_cpo;
7469 p_cpo = (char_u *)"";
7470
7471 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 spat = get_vartv_string(&argvars[0]);
7473 mpat = get_vartv_string_buf(&argvars[1], nbuf1);
7474 epat = get_vartv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 /* Make two search patterns: start/end (pat2, for in nested pairs) and
7477 * start/middle/end (pat3, for the top pair). */
7478 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
7479 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
7480 if (pat2 == NULL || pat3 == NULL)
7481 goto theend;
7482 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
7483 if (*mpat == NUL)
7484 STRCPY(pat3, pat2);
7485 else
7486 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
7487 spat, epat, mpat);
7488
7489 /* Handle the optional fourth argument: flags */
7490 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007491 if (dir == 0)
7492 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007495 if (argvars[3].v_type == VAR_UNKNOWN
7496 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 skip = (char_u *)"";
7498 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007499 skip = get_vartv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 save_cursor = curwin->w_cursor;
7502 pos = curwin->w_cursor;
7503 firstpos.lnum = 0;
7504 pat = pat3;
7505 for (;;)
7506 {
7507 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
7508 SEARCH_KEEP, RE_SEARCH);
7509 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
7510 /* didn't find it or found the first match again: FAIL */
7511 break;
7512
7513 if (firstpos.lnum == 0)
7514 firstpos = pos;
7515
7516 /* If the skip pattern matches, ignore this match. */
7517 if (*skip != NUL)
7518 {
7519 save_pos = curwin->w_cursor;
7520 curwin->w_cursor = pos;
7521 r = eval_to_bool(skip, &err, NULL, FALSE);
7522 curwin->w_cursor = save_pos;
7523 if (err)
7524 {
7525 /* Evaluating {skip} caused an error, break here. */
7526 curwin->w_cursor = save_cursor;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007527 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 break;
7529 }
7530 if (r)
7531 continue;
7532 }
7533
7534 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7535 {
7536 /* Found end when searching backwards or start when searching
7537 * forward: nested pair. */
7538 ++nest;
7539 pat = pat2; /* nested, don't search for middle */
7540 }
7541 else
7542 {
7543 /* Found end when searching forward or start when searching
7544 * backward: end of (nested) pair; or found middle in outer pair. */
7545 if (--nest == 1)
7546 pat = pat3; /* outer level, search for middle */
7547 }
7548
7549 if (nest == 0)
7550 {
7551 /* Found the match: return matchcount or line number. */
7552 if (flags & SP_RETCOUNT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007553 ++retvar->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 retvar->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 curwin->w_cursor = pos;
7557 if (!(flags & SP_REPEAT))
7558 break;
7559 nest = 1; /* search for next unmatched */
7560 }
7561 }
7562
7563 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007564 if ((flags & SP_NOMOVE) || retvar->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 curwin->w_cursor = save_cursor;
7566
7567theend:
7568 vim_free(pat2);
7569 vim_free(pat3);
7570 p_ws = save_p_ws;
7571 p_cpo = save_cpo;
7572}
7573
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007574/*
7575 * Get flags for a search function.
7576 * Possibly sets "p_ws".
7577 * Returns BACKWARD, FORWARD or zero (for an error).
7578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 static int
7580get_search_arg(varp, flagsp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 int *flagsp;
7583{
7584 int dir = FORWARD;
7585 char_u *flags;
7586 char_u nbuf[NUMBUFLEN];
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007587 int mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 if (varp->v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007591 flags = get_vartv_string_buf(varp, nbuf);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007592 while (*flags != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007594 switch (*flags)
7595 {
7596 case 'b': dir = BACKWARD; break;
7597 case 'w': p_ws = TRUE; break;
7598 case 'W': p_ws = FALSE; break;
7599 default: mask = 0;
7600 if (flagsp != NULL)
7601 switch (*flags)
7602 {
7603 case 'n': mask = SP_NOMOVE; break;
7604 case 'r': mask = SP_REPEAT; break;
7605 case 'm': mask = SP_RETCOUNT; break;
7606 }
7607 if (mask == 0)
7608 {
7609 EMSG2(_(e_invarg2), flags);
7610 dir = 0;
7611 }
7612 else
7613 *flagsp |= mask;
7614 }
7615 if (dir == 0)
7616 break;
7617 ++flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
7619 }
7620 return dir;
7621}
7622
7623/*
7624 * "setbufvar()" function
7625 */
7626/*ARGSUSED*/
7627 static void
7628f_setbufvar(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 typeval *argvars;
7630 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
7632 buf_T *buf;
7633#ifdef FEAT_AUTOCMD
7634 aco_save_T aco;
7635#else
7636 buf_T *save_curbuf;
7637#endif
7638 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007639 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 char_u nbuf[NUMBUFLEN];
7641
7642 if (check_restricted() || check_secure())
7643 return;
7644 ++emsg_off;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 buf = get_buf_vartv(&argvars[0]);
7646 varname = get_vartv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 varp = &argvars[2];
7648
7649 if (buf != NULL && varname != NULL && varp != NULL)
7650 {
7651 /* set curbuf to be our buf, temporarily */
7652#ifdef FEAT_AUTOCMD
7653 aucmd_prepbuf(&aco, buf);
7654#else
7655 save_curbuf = curbuf;
7656 curbuf = buf;
7657#endif
7658
7659 if (*varname == '&')
7660 {
7661 ++varname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007662 set_option_value(varname, get_vartv_number(varp),
7663 get_vartv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665 else
7666 {
7667 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
7668 if (bufvarname != NULL)
7669 {
7670 STRCPY(bufvarname, "b:");
7671 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007672 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 vim_free(bufvarname);
7674 }
7675 }
7676
7677 /* reset notion of buffer */
7678#ifdef FEAT_AUTOCMD
7679 aucmd_restbuf(&aco);
7680#else
7681 curbuf = save_curbuf;
7682#endif
7683 }
7684 --emsg_off;
7685}
7686
7687/*
7688 * "setcmdpos()" function
7689 */
7690 static void
7691f_setcmdpos(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692 typeval *argvars;
7693 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 retvar->vval.v_number = set_cmdline_pos(
7696 (int)get_vartv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697}
7698
7699/*
7700 * "setline()" function
7701 */
7702 static void
7703f_setline(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 typeval *argvars;
7705 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 linenr_T lnum;
7708 char_u *line;
7709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007710 lnum = get_vartv_lnum(argvars);
7711 line = get_vartv_string(&argvars[1]);
7712 retvar->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
7714 if (lnum >= 1
7715 && lnum <= curbuf->b_ml.ml_line_count
7716 && u_savesub(lnum) == OK
7717 && ml_replace(lnum, line, TRUE) == OK)
7718 {
7719 changed_bytes(lnum, 0);
7720 check_cursor_col();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007721 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723}
7724
7725/*
7726 * "setreg()" function
7727 */
7728 static void
7729f_setreg(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 typeval *argvars;
7731 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 int regname;
7734 char_u *strregname;
7735 char_u *stropt;
7736 int append;
7737 char_u yank_type;
7738 long block_len;
7739
7740 block_len = -1;
7741 yank_type = MAUTO;
7742 append = FALSE;
7743
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007744 strregname = get_vartv_string(argvars);
7745 retvar->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
7747 regname = (strregname == NULL ? '"' : *strregname);
7748 if (regname == 0 || regname == '@')
7749 regname = '"';
7750 else if (regname == '=')
7751 return;
7752
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755 for (stropt = get_vartv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 switch (*stropt)
7757 {
7758 case 'a': case 'A': /* append */
7759 append = TRUE;
7760 break;
7761 case 'v': case 'c': /* character-wise selection */
7762 yank_type = MCHAR;
7763 break;
7764 case 'V': case 'l': /* line-wise selection */
7765 yank_type = MLINE;
7766 break;
7767#ifdef FEAT_VISUAL
7768 case 'b': case Ctrl_V: /* block-wise selection */
7769 yank_type = MBLOCK;
7770 if (VIM_ISDIGIT(stropt[1]))
7771 {
7772 ++stropt;
7773 block_len = getdigits(&stropt) - 1;
7774 --stropt;
7775 }
7776 break;
7777#endif
7778 }
7779 }
7780
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 write_reg_contents_ex(regname, get_vartv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 append, yank_type, block_len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007783 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784}
7785
7786
7787/*
7788 * "setwinvar(expr)" function
7789 */
7790/*ARGSUSED*/
7791 static void
7792f_setwinvar(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 typeval *argvars;
7794 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 win_T *win;
7797#ifdef FEAT_WINDOWS
7798 win_T *save_curwin;
7799#endif
7800 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 char_u nbuf[NUMBUFLEN];
7803
7804 if (check_restricted() || check_secure())
7805 return;
7806 ++emsg_off;
7807 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 varname = get_vartv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 varp = &argvars[2];
7810
7811 if (win != NULL && varname != NULL && varp != NULL)
7812 {
7813#ifdef FEAT_WINDOWS
7814 /* set curwin to be our win, temporarily */
7815 save_curwin = curwin;
7816 curwin = win;
7817 curbuf = curwin->w_buffer;
7818#endif
7819
7820 if (*varname == '&')
7821 {
7822 ++varname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 set_option_value(varname, get_vartv_number(varp),
7824 get_vartv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 }
7826 else
7827 {
7828 winvarname = alloc((unsigned)STRLEN(varname) + 3);
7829 if (winvarname != NULL)
7830 {
7831 STRCPY(winvarname, "w:");
7832 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007833 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 vim_free(winvarname);
7835 }
7836 }
7837
7838#ifdef FEAT_WINDOWS
7839 /* Restore current window, if it's still valid (autocomands can make
7840 * it invalid). */
7841 if (win_valid(save_curwin))
7842 {
7843 curwin = save_curwin;
7844 curbuf = curwin->w_buffer;
7845 }
7846#endif
7847 }
7848 --emsg_off;
7849}
7850
7851/*
7852 * "nextnonblank()" function
7853 */
7854 static void
7855f_nextnonblank(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856 typeval *argvars;
7857 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 linenr_T lnum;
7860
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007861 for (lnum = get_vartv_lnum(argvars); ; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {
7863 if (lnum > curbuf->b_ml.ml_line_count)
7864 {
7865 lnum = 0;
7866 break;
7867 }
7868 if (*skipwhite(ml_get(lnum)) != NUL)
7869 break;
7870 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 retvar->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
7875 * "prevnonblank()" function
7876 */
7877 static void
7878f_prevnonblank(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 typeval *argvars;
7880 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 linenr_T lnum;
7883
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 lnum = get_vartv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7886 lnum = 0;
7887 else
7888 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7889 --lnum;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 retvar->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891}
7892
7893#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
7894static void make_connection __ARGS((void));
7895static int check_connection __ARGS((void));
7896
7897 static void
7898make_connection()
7899{
7900 if (X_DISPLAY == NULL
7901# ifdef FEAT_GUI
7902 && !gui.in_use
7903# endif
7904 )
7905 {
7906 x_force_connect = TRUE;
7907 setup_term_clip();
7908 x_force_connect = FALSE;
7909 }
7910}
7911
7912 static int
7913check_connection()
7914{
7915 make_connection();
7916 if (X_DISPLAY == NULL)
7917 {
7918 EMSG(_("E240: No connection to Vim server"));
7919 return FAIL;
7920 }
7921 return OK;
7922}
7923#endif
7924
7925/*ARGSUSED*/
7926 static void
7927f_serverlist(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 typeval *argvars;
7929 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 char_u *r = NULL;
7932
7933#ifdef FEAT_CLIENTSERVER
7934# ifdef WIN32
7935 r = serverGetVimNames();
7936# else
7937 make_connection();
7938 if (X_DISPLAY != NULL)
7939 r = serverGetVimNames(X_DISPLAY);
7940# endif
7941#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942 retvar->v_type = VAR_STRING;
7943 retvar->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944}
7945
7946/*ARGSUSED*/
7947 static void
7948f_remote_peek(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 typeval *argvars;
7950 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952#ifdef FEAT_CLIENTSERVER
7953 var v;
7954 char_u *s = NULL;
7955# ifdef WIN32
7956 int n = 0;
7957# endif
7958
7959 if (check_restricted() || check_secure())
7960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 return;
7963 }
7964# ifdef WIN32
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007965 sscanf(get_vartv_string(&argvars[0]), "%x", &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (n == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 else
7969 {
7970 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007971 retvar->vval.v_number = (s != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973# else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007974 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (check_connection() == FAIL)
7976 return;
7977
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 retvar->vval.v_number = serverPeekReply(X_DISPLAY,
7979 serverStrToWin(get_vartv_string(&argvars[0])), &s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980# endif
7981
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007982 if (argvars[1].v_type != VAR_UNKNOWN && retvar->vval.v_number > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007984 v.tv.v_type = VAR_STRING;
7985 v.tv.vval.v_string = vim_strsave(s);
7986 set_var(get_vartv_string(&argvars[1]), &v.tv, FALSE);
7987 vim_free(v.tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007990 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991#endif
7992}
7993
7994/*ARGSUSED*/
7995 static void
7996f_remote_read(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 typeval *argvars;
7998 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999{
8000 char_u *r = NULL;
8001
8002#ifdef FEAT_CLIENTSERVER
8003 if (!check_restricted() && !check_secure())
8004 {
8005# ifdef WIN32
8006 /* The server's HWND is encoded in the 'id' parameter */
8007 int n = 0;
8008
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009 sscanf(get_vartv_string(&argvars[0]), "%x", &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (n != 0)
8011 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8012 if (r == NULL)
8013# else
8014 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015 serverStrToWin(get_vartv_string(&argvars[0])), &r, FALSE) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016# endif
8017 EMSG(_("E277: Unable to read a server reply"));
8018 }
8019#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008020 retvar->v_type = VAR_STRING;
8021 retvar->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024/*ARGSUSED*/
8025 static void
8026f_server2client(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008027 typeval *argvars;
8028 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030#ifdef FEAT_CLIENTSERVER
8031 char_u buf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032 char_u *server = get_vartv_string(&argvars[0]);
8033 char_u *reply = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (check_restricted() || check_secure())
8037 return;
8038# ifdef FEAT_X11
8039 if (check_connection() == FAIL)
8040 return;
8041# endif
8042
8043 if (serverSendReply(server, reply) < 0)
8044 {
8045 EMSG(_("E258: Unable to send to client"));
8046 return;
8047 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051#endif
8052}
8053
8054#ifdef FEAT_CLIENTSERVER
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008055static void remote_common __ARGS((typeval *argvars, typeval *retvar, int expr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
8057 static void
8058remote_common(argvars, retvar, expr)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008059 typeval *argvars;
8060 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 int expr;
8062{
8063 char_u *server_name;
8064 char_u *keys;
8065 char_u *r = NULL;
8066 char_u buf[NUMBUFLEN];
8067# ifdef WIN32
8068 HWND w;
8069# else
8070 Window w;
8071# endif
8072
8073 if (check_restricted() || check_secure())
8074 return;
8075
8076# ifdef FEAT_X11
8077 if (check_connection() == FAIL)
8078 return;
8079# endif
8080
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 server_name = get_vartv_string(&argvars[0]);
8082 keys = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083# ifdef WIN32
8084 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8085# else
8086 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8087 < 0)
8088# endif
8089 {
8090 if (r != NULL)
8091 EMSG(r); /* sending worked but evaluation failed */
8092 else
8093 EMSG2(_("E241: Unable to send to %s"), server_name);
8094 return;
8095 }
8096
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 retvar->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008099 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
8101 var v;
8102 char_u str[30];
8103
8104 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008105 v.tv.v_type = VAR_STRING;
8106 v.tv.vval.v_string = vim_strsave(str);
8107 set_var(get_vartv_string(&argvars[2]), &v.tv, FALSE);
8108 vim_free(v.tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 }
8110}
8111#endif
8112
8113/*
8114 * "remote_expr()" function
8115 */
8116/*ARGSUSED*/
8117 static void
8118f_remote_expr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 typeval *argvars;
8120 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 retvar->v_type = VAR_STRING;
8123 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124#ifdef FEAT_CLIENTSERVER
8125 remote_common(argvars, retvar, TRUE);
8126#endif
8127}
8128
8129/*
8130 * "remote_send()" function
8131 */
8132/*ARGSUSED*/
8133 static void
8134f_remote_send(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 typeval *argvars;
8136 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 retvar->v_type = VAR_STRING;
8139 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140#ifdef FEAT_CLIENTSERVER
8141 remote_common(argvars, retvar, FALSE);
8142#endif
8143}
8144
8145/*
8146 * "remote_foreground()" function
8147 */
8148/*ARGSUSED*/
8149 static void
8150f_remote_foreground(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 typeval *argvars;
8152 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155#ifdef FEAT_CLIENTSERVER
8156# ifdef WIN32
8157 /* On Win32 it's done in this application. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 serverForeground(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159# else
8160 /* Send a foreground() expression to the server. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008161 argvars[1].v_type = VAR_STRING;
8162 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8163 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 remote_common(argvars, retvar, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165 vim_free(argvars[1].vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166# endif
8167#endif
8168}
8169
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008170/*
8171 * "repeat()" function
8172 */
8173/*ARGSUSED*/
8174 static void
8175f_repeat(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008176 typeval *argvars;
8177 typeval *retvar;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008178{
8179 char_u *p;
8180 int n;
8181 int slen;
8182 int len;
8183 char_u *r;
8184 int i;
8185
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 p = get_vartv_string(&argvars[0]);
8187 n = get_vartv_number(&argvars[1]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008189 retvar->v_type = VAR_STRING;
8190 retvar->vval.v_string = NULL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008191
8192 slen = (int)STRLEN(p);
8193 len = slen * n;
8194
8195 if (len <= 0)
8196 return;
8197
8198 r = alloc(len + 1);
8199 if (r != NULL)
8200 {
8201 for (i = 0; i < n; i++)
8202 mch_memmove(r + i * slen, p, (size_t)slen);
8203 r[len] = NUL;
8204 }
8205
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 retvar->vval.v_string = r;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008207}
8208
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209#ifdef HAVE_STRFTIME
8210/*
8211 * "strftime({format}[, {time}])" function
8212 */
8213 static void
8214f_strftime(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 typeval *argvars;
8216 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 char_u result_buf[256];
8219 struct tm *curtime;
8220 time_t seconds;
8221 char_u *p;
8222
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008223 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 p = get_vartv_string(&argvars[0]);
8226 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 seconds = time(NULL);
8228 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 seconds = (time_t)get_vartv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 curtime = localtime(&seconds);
8231 /* MSVC returns NULL for an invalid value of seconds. */
8232 if (curtime == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008233 retvar->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 else
8235 {
8236# ifdef FEAT_MBYTE
8237 vimconv_T conv;
8238 char_u *enc;
8239
8240 conv.vc_type = CONV_NONE;
8241 enc = enc_locale();
8242 convert_setup(&conv, p_enc, enc);
8243 if (conv.vc_type != CONV_NONE)
8244 p = string_convert(&conv, p, NULL);
8245# endif
8246 if (p != NULL)
8247 (void)strftime((char *)result_buf, sizeof(result_buf),
8248 (char *)p, curtime);
8249 else
8250 result_buf[0] = NUL;
8251
8252# ifdef FEAT_MBYTE
8253 if (conv.vc_type != CONV_NONE)
8254 vim_free(p);
8255 convert_setup(&conv, enc, p_enc);
8256 if (conv.vc_type != CONV_NONE)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 retvar->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 else
8259# endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008260 retvar->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261
8262# ifdef FEAT_MBYTE
8263 /* Release conversion descriptors */
8264 convert_setup(&conv, NULL, NULL);
8265 vim_free(enc);
8266# endif
8267 }
8268}
8269#endif
8270
8271/*
8272 * "stridx()" function
8273 */
8274 static void
8275f_stridx(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008276 typeval *argvars;
8277 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278{
8279 char_u buf[NUMBUFLEN];
8280 char_u *needle;
8281 char_u *haystack;
8282 char_u *pos;
8283
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 needle = get_vartv_string(&argvars[1]);
8285 haystack = get_vartv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8287
8288 if (pos == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008289 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 retvar->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292}
8293
8294/*
8295 * "strridx()" function
8296 */
8297 static void
8298f_strridx(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 typeval *argvars;
8300 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
8302 char_u buf[NUMBUFLEN];
8303 char_u *needle;
8304 char_u *haystack;
8305 char_u *rest;
8306 char_u *lastmatch = NULL;
8307
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 needle = get_vartv_string(&argvars[1]);
8309 haystack = get_vartv_string_buf(&argvars[0], buf);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008310 if (*needle == NUL)
8311 /* Empty string matches past the end. */
8312 lastmatch = haystack + STRLEN(haystack);
8313 else
8314 for (rest = haystack; *rest != '\0'; ++rest)
8315 {
8316 rest = (char_u *)strstr((char *)rest, (char *)needle);
8317 if (rest == NULL)
8318 break;
8319 lastmatch = rest;
8320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322 if (lastmatch == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 retvar->vval.v_number = (varnumber_T)(lastmatch - haystack);
8326}
8327
8328/*
8329 * "string()" function
8330 */
8331 static void
8332f_string(argvars, retvar)
8333 typeval *argvars;
8334 typeval *retvar;
8335{
8336 char_u *tofree;
8337
8338 retvar->v_type = VAR_STRING;
8339 retvar->vval.v_string = tv2string(&argvars[0], &tofree);
8340 if (tofree == NULL)
8341 retvar->vval.v_string = vim_strsave(retvar->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
8344/*
8345 * "strlen()" function
8346 */
8347 static void
8348f_strlen(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008349 typeval *argvars;
8350 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 retvar->vval.v_number = (varnumber_T)(STRLEN(
8353 get_vartv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354}
8355
8356/*
8357 * "strpart()" function
8358 */
8359 static void
8360f_strpart(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361 typeval *argvars;
8362 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 char_u *p;
8365 int n;
8366 int len;
8367 int slen;
8368
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008369 p = get_vartv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 slen = (int)STRLEN(p);
8371
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008372 n = get_vartv_number(&argvars[1]);
8373 if (argvars[2].v_type != VAR_UNKNOWN)
8374 len = get_vartv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 else
8376 len = slen - n; /* default len: all bytes that are available. */
8377
8378 /*
8379 * Only return the overlap between the specified part and the actual
8380 * string.
8381 */
8382 if (n < 0)
8383 {
8384 len += n;
8385 n = 0;
8386 }
8387 else if (n > slen)
8388 n = slen;
8389 if (len < 0)
8390 len = 0;
8391 else if (n + len > slen)
8392 len = slen - n;
8393
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008394 retvar->v_type = VAR_STRING;
8395 retvar->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
8397
8398/*
8399 * "strtrans()" function
8400 */
8401 static void
8402f_strtrans(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008403 typeval *argvars;
8404 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008406 retvar->v_type = VAR_STRING;
8407 retvar->vval.v_string = transstr(get_vartv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409
8410/*
8411 * "synID(line, col, trans)" function
8412 */
8413/*ARGSUSED*/
8414 static void
8415f_synID(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008416 typeval *argvars;
8417 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 int id = 0;
8420#ifdef FEAT_SYN_HL
8421 long line;
8422 long col;
8423 int trans;
8424
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008425 line = get_vartv_lnum(argvars);
8426 col = get_vartv_number(&argvars[1]) - 1;
8427 trans = get_vartv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
8429 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
8430 && col >= 0 && col < (long)STRLEN(ml_get(line)))
8431 id = syn_get_id(line, col, trans);
8432#endif
8433
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 retvar->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435}
8436
8437/*
8438 * "synIDattr(id, what [, mode])" function
8439 */
8440/*ARGSUSED*/
8441 static void
8442f_synIDattr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 typeval *argvars;
8444 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445{
8446 char_u *p = NULL;
8447#ifdef FEAT_SYN_HL
8448 int id;
8449 char_u *what;
8450 char_u *mode;
8451 char_u modebuf[NUMBUFLEN];
8452 int modec;
8453
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008454 id = get_vartv_number(&argvars[0]);
8455 what = get_vartv_string(&argvars[1]);
8456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008458 mode = get_vartv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 modec = TOLOWER_ASC(mode[0]);
8460 if (modec != 't' && modec != 'c'
8461#ifdef FEAT_GUI
8462 && modec != 'g'
8463#endif
8464 )
8465 modec = 0; /* replace invalid with current */
8466 }
8467 else
8468 {
8469#ifdef FEAT_GUI
8470 if (gui.in_use)
8471 modec = 'g';
8472 else
8473#endif
8474 if (t_colors > 1)
8475 modec = 'c';
8476 else
8477 modec = 't';
8478 }
8479
8480
8481 switch (TOLOWER_ASC(what[0]))
8482 {
8483 case 'b':
8484 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
8485 p = highlight_color(id, what, modec);
8486 else /* bold */
8487 p = highlight_has_attr(id, HL_BOLD, modec);
8488 break;
8489
8490 case 'f': /* fg[#] */
8491 p = highlight_color(id, what, modec);
8492 break;
8493
8494 case 'i':
8495 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
8496 p = highlight_has_attr(id, HL_INVERSE, modec);
8497 else /* italic */
8498 p = highlight_has_attr(id, HL_ITALIC, modec);
8499 break;
8500
8501 case 'n': /* name */
8502 p = get_highlight_name(NULL, id - 1);
8503 break;
8504
8505 case 'r': /* reverse */
8506 p = highlight_has_attr(id, HL_INVERSE, modec);
8507 break;
8508
8509 case 's': /* standout */
8510 p = highlight_has_attr(id, HL_STANDOUT, modec);
8511 break;
8512
8513 case 'u': /* underline */
8514 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8515 break;
8516 }
8517
8518 if (p != NULL)
8519 p = vim_strsave(p);
8520#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 retvar->v_type = VAR_STRING;
8522 retvar->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524
8525/*
8526 * "synIDtrans(id)" function
8527 */
8528/*ARGSUSED*/
8529 static void
8530f_synIDtrans(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 typeval *argvars;
8532 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 int id;
8535
8536#ifdef FEAT_SYN_HL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 id = get_vartv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
8539 if (id > 0)
8540 id = syn_get_final_id(id);
8541 else
8542#endif
8543 id = 0;
8544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 retvar->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546}
8547
8548/*
8549 * "system()" function
8550 */
8551 static void
8552f_system(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 typeval *argvars;
8554 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008556 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008558 char_u *infile = NULL;
8559 char_u buf[NUMBUFLEN];
8560 int err = FALSE;
8561 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008564 {
8565 /*
8566 * Write the string to a temp file, to be used for input of the shell
8567 * command.
8568 */
8569 if ((infile = vim_tempname('i')) == NULL)
8570 {
8571 EMSG(_(e_notmp));
8572 return;
8573 }
8574
8575 fd = mch_fopen((char *)infile, WRITEBIN);
8576 if (fd == NULL)
8577 {
8578 EMSG2(_(e_notopen), infile);
8579 goto done;
8580 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 p = get_vartv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008582 if (fwrite(p, STRLEN(p), 1, fd) != 1)
8583 err = TRUE;
8584 if (fclose(fd) != 0)
8585 err = TRUE;
8586 if (err)
8587 {
8588 EMSG(_("E677: Error writing temp file"));
8589 goto done;
8590 }
8591 }
8592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 res = get_cmd_output(get_vartv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#ifdef USE_CR
8596 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008597 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 {
8599 char_u *s;
8600
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008601 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {
8603 if (*s == CAR)
8604 *s = NL;
8605 }
8606 }
8607#else
8608# ifdef USE_CRNL
8609 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008610 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
8612 char_u *s, *d;
8613
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008614 d = res;
8615 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 if (s[0] == CAR && s[1] == NL)
8618 ++s;
8619 *d++ = *s;
8620 }
8621 *d = NUL;
8622 }
8623# endif
8624#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008625
8626done:
8627 if (infile != NULL)
8628 {
8629 mch_remove(infile);
8630 vim_free(infile);
8631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008632 retvar->v_type = VAR_STRING;
8633 retvar->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634}
8635
8636/*
8637 * "submatch()" function
8638 */
8639 static void
8640f_submatch(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008641 typeval *argvars;
8642 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 retvar->v_type = VAR_STRING;
8645 retvar->vval.v_string = reg_submatch((int)get_vartv_number(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646}
8647
8648/*
8649 * "substitute()" function
8650 */
8651 static void
8652f_substitute(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008653 typeval *argvars;
8654 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 char_u patbuf[NUMBUFLEN];
8657 char_u subbuf[NUMBUFLEN];
8658 char_u flagsbuf[NUMBUFLEN];
8659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 retvar->v_type = VAR_STRING;
8661 retvar->vval.v_string = do_string_sub(
8662 get_vartv_string(&argvars[0]),
8663 get_vartv_string_buf(&argvars[1], patbuf),
8664 get_vartv_string_buf(&argvars[2], subbuf),
8665 get_vartv_string_buf(&argvars[3], flagsbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666}
8667
8668/*
8669 * "tempname()" function
8670 */
8671/*ARGSUSED*/
8672 static void
8673f_tempname(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674 typeval *argvars;
8675 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 static int x = 'A';
8678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008679 retvar->v_type = VAR_STRING;
8680 retvar->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681
8682 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
8683 * names. Skip 'I' and 'O', they are used for shell redirection. */
8684 do
8685 {
8686 if (x == 'Z')
8687 x = '0';
8688 else if (x == '9')
8689 x = 'A';
8690 else
8691 {
8692#ifdef EBCDIC
8693 if (x == 'I')
8694 x = 'J';
8695 else if (x == 'R')
8696 x = 'S';
8697 else
8698#endif
8699 ++x;
8700 }
8701 } while (x == 'I' || x == 'O');
8702}
8703
8704/*
8705 * "tolower(string)" function
8706 */
8707 static void
8708f_tolower(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008709 typeval *argvars;
8710 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 char_u *p;
8713
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 p = vim_strsave(get_vartv_string(&argvars[0]));
8715 retvar->v_type = VAR_STRING;
8716 retvar->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 if (p != NULL)
8719 while (*p != NUL)
8720 {
8721#ifdef FEAT_MBYTE
8722 int l;
8723
8724 if (enc_utf8)
8725 {
8726 int c, lc;
8727
8728 c = utf_ptr2char(p);
8729 lc = utf_tolower(c);
8730 l = utf_ptr2len_check(p);
8731 /* TODO: reallocate string when byte count changes. */
8732 if (utf_char2len(lc) == l)
8733 utf_char2bytes(lc, p);
8734 p += l;
8735 }
8736 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
8737 p += l; /* skip multi-byte character */
8738 else
8739#endif
8740 {
8741 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
8742 ++p;
8743 }
8744 }
8745}
8746
8747/*
8748 * "toupper(string)" function
8749 */
8750 static void
8751f_toupper(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 typeval *argvars;
8753 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 char_u *p;
8756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 p = vim_strsave(get_vartv_string(&argvars[0]));
8758 retvar->v_type = VAR_STRING;
8759 retvar->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
8761 if (p != NULL)
8762 while (*p != NUL)
8763 {
8764#ifdef FEAT_MBYTE
8765 int l;
8766
8767 if (enc_utf8)
8768 {
8769 int c, uc;
8770
8771 c = utf_ptr2char(p);
8772 uc = utf_toupper(c);
8773 l = utf_ptr2len_check(p);
8774 /* TODO: reallocate string when byte count changes. */
8775 if (utf_char2len(uc) == l)
8776 utf_char2bytes(uc, p);
8777 p += l;
8778 }
8779 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
8780 p += l; /* skip multi-byte character */
8781 else
8782#endif
8783 {
8784 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
8785 p++;
8786 }
8787 }
8788}
8789
8790/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00008791 * "tr(string, fromstr, tostr)" function
8792 */
8793 static void
8794f_tr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008795 typeval *argvars;
8796 typeval *retvar;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008797{
8798 char_u *instr;
8799 char_u *fromstr;
8800 char_u *tostr;
8801 char_u *p;
8802#ifdef FEAT_MBYTE
8803 int inlen;
8804 int fromlen;
8805 int tolen;
8806 int idx;
8807 char_u *cpstr;
8808 int cplen;
8809 int first = TRUE;
8810#endif
8811 char_u buf[NUMBUFLEN];
8812 char_u buf2[NUMBUFLEN];
8813 garray_T ga;
8814
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008815 instr = get_vartv_string(&argvars[0]);
8816 fromstr = get_vartv_string_buf(&argvars[1], buf);
8817 tostr = get_vartv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +00008818
8819 /* Default return value: empty string. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008820 retvar->v_type = VAR_STRING;
8821 retvar->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008822 ga_init2(&ga, (int)sizeof(char), 80);
8823
8824#ifdef FEAT_MBYTE
8825 if (!has_mbyte)
8826#endif
8827 /* not multi-byte: fromstr and tostr must be the same length */
8828 if (STRLEN(fromstr) != STRLEN(tostr))
8829 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008830#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +00008831error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008832#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +00008833 EMSG2(_(e_invarg2), fromstr);
8834 ga_clear(&ga);
8835 return;
8836 }
8837
8838 /* fromstr and tostr have to contain the same number of chars */
8839 while (*instr != NUL)
8840 {
8841#ifdef FEAT_MBYTE
8842 if (has_mbyte)
8843 {
8844 inlen = mb_ptr2len_check(instr);
8845 cpstr = instr;
8846 cplen = inlen;
8847 idx = 0;
8848 for (p = fromstr; *p != NUL; p += fromlen)
8849 {
8850 fromlen = mb_ptr2len_check(p);
8851 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
8852 {
8853 for (p = tostr; *p != NUL; p += tolen)
8854 {
8855 tolen = mb_ptr2len_check(p);
8856 if (idx-- == 0)
8857 {
8858 cplen = tolen;
8859 cpstr = p;
8860 break;
8861 }
8862 }
8863 if (*p == NUL) /* tostr is shorter than fromstr */
8864 goto error;
8865 break;
8866 }
8867 ++idx;
8868 }
8869
8870 if (first && cpstr == instr)
8871 {
8872 /* Check that fromstr and tostr have the same number of
8873 * (multi-byte) characters. Done only once when a character
8874 * of instr doesn't appear in fromstr. */
8875 first = FALSE;
8876 for (p = tostr; *p != NUL; p += tolen)
8877 {
8878 tolen = mb_ptr2len_check(p);
8879 --idx;
8880 }
8881 if (idx != 0)
8882 goto error;
8883 }
8884
8885 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00008886 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +00008887 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008888
8889 instr += inlen;
8890 }
8891 else
8892#endif
8893 {
8894 /* When not using multi-byte chars we can do it faster. */
8895 p = vim_strchr(fromstr, *instr);
8896 if (p != NULL)
8897 ga_append(&ga, tostr[p - fromstr]);
8898 else
8899 ga_append(&ga, *instr);
8900 ++instr;
8901 }
8902 }
8903
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008904 retvar->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008905}
8906
8907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 * "type(expr)" function
8909 */
8910 static void
8911f_type(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 typeval *argvars;
8913 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008915 if (argvars[0].v_type == VAR_NUMBER)
8916 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008918 retvar->vval.v_number = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920
8921/*
8922 * "virtcol(string)" function
8923 */
8924 static void
8925f_virtcol(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008926 typeval *argvars;
8927 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 colnr_T vcol = 0;
8930 pos_T *fp;
8931
8932 fp = var2fpos(&argvars[0], FALSE);
8933 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
8934 {
8935 getvvcol(curwin, fp, NULL, NULL, &vcol);
8936 ++vcol;
8937 }
8938
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008939 retvar->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940}
8941
8942/*
8943 * "visualmode()" function
8944 */
8945/*ARGSUSED*/
8946 static void
8947f_visualmode(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008948 typeval *argvars;
8949 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950{
8951#ifdef FEAT_VISUAL
8952 char_u str[2];
8953
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 str[0] = curbuf->b_visual_mode_eval;
8956 str[1] = NUL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 retvar->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
8959 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008960 if ((argvars[0].v_type == VAR_NUMBER
8961 && argvars[0].vval.v_number != 0)
8962 || (argvars[0].v_type == VAR_STRING
8963 && *get_vartv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 curbuf->b_visual_mode_eval = NUL;
8965#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008966 retvar->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967#endif
8968}
8969
8970/*
8971 * "winbufnr(nr)" function
8972 */
8973 static void
8974f_winbufnr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008975 typeval *argvars;
8976 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 win_T *wp;
8979
8980 wp = find_win_by_nr(&argvars[0]);
8981 if (wp == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008982 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 retvar->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985}
8986
8987/*
8988 * "wincol()" function
8989 */
8990/*ARGSUSED*/
8991 static void
8992f_wincol(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 typeval *argvars;
8994 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996 validate_cursor();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 retvar->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
9000/*
9001 * "winheight(nr)" function
9002 */
9003 static void
9004f_winheight(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 typeval *argvars;
9006 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 win_T *wp;
9009
9010 wp = find_win_by_nr(&argvars[0]);
9011 if (wp == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 retvar->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015}
9016
9017/*
9018 * "winline()" function
9019 */
9020/*ARGSUSED*/
9021 static void
9022f_winline(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009023 typeval *argvars;
9024 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 validate_cursor();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009027 retvar->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
9030/*
9031 * "winnr()" function
9032 */
9033/* ARGSUSED */
9034 static void
9035f_winnr(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009036 typeval *argvars;
9037 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 int nr = 1;
9040#ifdef FEAT_WINDOWS
9041 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009042 win_T *twin = curwin;
9043 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009045 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009047 arg = get_vartv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009048 if (STRCMP(arg, "$") == 0)
9049 twin = lastwin;
9050 else if (STRCMP(arg, "#") == 0)
9051 {
9052 twin = prevwin;
9053 if (prevwin == NULL)
9054 nr = 0;
9055 }
9056 else
9057 {
9058 EMSG2(_(e_invexpr2), arg);
9059 nr = 0;
9060 }
9061 }
9062
9063 if (nr > 0)
9064 for (wp = firstwin; wp != twin; wp = wp->w_next)
9065 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009067 retvar->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068}
9069
9070/*
9071 * "winrestcmd()" function
9072 */
9073/* ARGSUSED */
9074 static void
9075f_winrestcmd(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009076 typeval *argvars;
9077 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079#ifdef FEAT_WINDOWS
9080 win_T *wp;
9081 int winnr = 1;
9082 garray_T ga;
9083 char_u buf[50];
9084
9085 ga_init2(&ga, (int)sizeof(char), 70);
9086 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9087 {
9088 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
9089 ga_concat(&ga, buf);
9090# ifdef FEAT_VERTSPLIT
9091 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
9092 ga_concat(&ga, buf);
9093# endif
9094 ++winnr;
9095 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00009096 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009098 retvar->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009100 retvar->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009102 retvar->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
9105/*
9106 * "winwidth(nr)" function
9107 */
9108 static void
9109f_winwidth(argvars, retvar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009110 typeval *argvars;
9111 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 win_T *wp;
9114
9115 wp = find_win_by_nr(&argvars[0]);
9116 if (wp == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009117 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 else
9119#ifdef FEAT_VERTSPLIT
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009120 retvar->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009122 retvar->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#endif
9124}
9125
9126 static win_T *
9127find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009128 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130#ifdef FEAT_WINDOWS
9131 win_T *wp;
9132#endif
9133 int nr;
9134
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009135 nr = get_vartv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136
9137#ifdef FEAT_WINDOWS
9138 if (nr == 0)
9139 return curwin;
9140
9141 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9142 if (--nr <= 0)
9143 break;
9144 return wp;
9145#else
9146 if (nr == 0 || nr == 1)
9147 return curwin;
9148 return NULL;
9149#endif
9150}
9151
9152/*
9153 * Translate a String variable into a position.
9154 */
9155 static pos_T *
9156var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009157 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 int lnum; /* TRUE when $ is last line */
9159{
9160 char_u *name;
9161 static pos_T pos;
9162 pos_T *pp;
9163
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 name = get_vartv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (name[0] == '.') /* cursor */
9166 return &curwin->w_cursor;
9167 if (name[0] == '\'') /* mark */
9168 {
9169 pp = getmark(name[1], FALSE);
9170 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
9171 return NULL;
9172 return pp;
9173 }
9174 if (name[0] == '$') /* last column or line */
9175 {
9176 if (lnum)
9177 {
9178 pos.lnum = curbuf->b_ml.ml_line_count;
9179 pos.col = 0;
9180 }
9181 else
9182 {
9183 pos.lnum = curwin->w_cursor.lnum;
9184 pos.col = (colnr_T)STRLEN(ml_get_curline());
9185 }
9186 return &pos;
9187 }
9188 return NULL;
9189}
9190
9191/*
9192 * Get the length of an environment variable name.
9193 * Advance "arg" to the first character after the name.
9194 * Return 0 for error.
9195 */
9196 static int
9197get_env_len(arg)
9198 char_u **arg;
9199{
9200 char_u *p;
9201 int len;
9202
9203 for (p = *arg; vim_isIDc(*p); ++p)
9204 ;
9205 if (p == *arg) /* no name found */
9206 return 0;
9207
9208 len = (int)(p - *arg);
9209 *arg = p;
9210 return len;
9211}
9212
9213/*
9214 * Get the length of the name of a function or internal variable.
9215 * "arg" is advanced to the first non-white character after the name.
9216 * Return 0 if something is wrong.
9217 */
9218 static int
9219get_id_len(arg)
9220 char_u **arg;
9221{
9222 char_u *p;
9223 int len;
9224
9225 /* Find the end of the name. */
9226 for (p = *arg; eval_isnamec(*p); ++p)
9227 ;
9228 if (p == *arg) /* no name found */
9229 return 0;
9230
9231 len = (int)(p - *arg);
9232 *arg = skipwhite(p);
9233
9234 return len;
9235}
9236
9237/*
9238 * Get the length of the name of a function.
9239 * "arg" is advanced to the first non-white character after the name.
9240 * Return 0 if something is wrong.
9241 * If the name contains 'magic' {}'s, expand them and return the
9242 * expanded name in an allocated string via 'alias' - caller must free.
9243 */
9244 static int
9245get_func_len(arg, alias, evaluate)
9246 char_u **arg;
9247 char_u **alias;
9248 int evaluate;
9249{
9250 int len;
9251#ifdef FEAT_MAGIC_BRACES
9252 char_u *p;
9253 char_u *expr_start;
9254 char_u *expr_end;
9255#endif
9256
9257 *alias = NULL; /* default to no alias */
9258
9259 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
9260 && (*arg)[2] == (int)KE_SNR)
9261 {
9262 /* hard coded <SNR>, already translated */
9263 *arg += 3;
9264 return get_id_len(arg) + 3;
9265 }
9266 len = eval_fname_script(*arg);
9267 if (len > 0)
9268 {
9269 /* literal "<SID>", "s:" or "<SNR>" */
9270 *arg += len;
9271 }
9272
9273#ifdef FEAT_MAGIC_BRACES
9274 /*
9275 * Find the end of the name;
9276 */
9277 p = find_name_end(*arg, &expr_start, &expr_end);
9278 /* check for {} construction */
9279 if (expr_start != NULL)
9280 {
9281 char_u *temp_string;
9282
9283 if (!evaluate)
9284 {
9285 len += (int)(p - *arg);
9286 *arg = skipwhite(p);
9287 return len;
9288 }
9289
9290 /*
9291 * Include any <SID> etc in the expanded string:
9292 * Thus the -len here.
9293 */
9294 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
9295 if (temp_string == NULL)
9296 return 0;
9297 *alias = temp_string;
9298 *arg = skipwhite(p);
9299 return (int)STRLEN(temp_string);
9300 }
9301#endif
9302
9303 len += get_id_len(arg);
9304 if (len == 0)
9305 EMSG2(_(e_invexpr2), *arg);
9306
9307 return len;
9308}
9309
9310 static char_u *
9311find_name_end(arg, expr_start, expr_end)
9312 char_u *arg;
9313 char_u **expr_start;
9314 char_u **expr_end;
9315{
9316 int nesting = 0;
9317 char_u *p;
9318
9319 *expr_start = NULL;
9320 *expr_end = NULL;
9321
9322 for (p = arg; (*p != NUL && (eval_isnamec(*p) || nesting != 0)); ++p)
9323 {
9324#ifdef FEAT_MAGIC_BRACES
9325 if (*p == '{')
9326 {
9327 nesting++;
9328 if (*expr_start == NULL)
9329 *expr_start = p;
9330 }
9331 else if (*p == '}')
9332 {
9333 nesting--;
9334 if (nesting == 0 && *expr_end == NULL)
9335 *expr_end = p;
9336 }
9337#endif
9338 }
9339
9340 return p;
9341}
9342
9343/*
9344 * Return TRUE if character "c" can be used in a variable or function name.
9345 */
9346 static int
9347eval_isnamec(c)
9348 int c;
9349{
9350 return (ASCII_ISALNUM(c) || c == '_' || c == ':'
9351#ifdef FEAT_MAGIC_BRACES
9352 || c == '{' || c == '}'
9353#endif
9354 );
9355}
9356
9357/*
9358 * Find a v: variable.
9359 * Return it's index, or -1 if not found.
9360 */
9361 static int
9362find_vim_var(name, len)
9363 char_u *name;
9364 int len; /* length of "name" */
9365{
9366 char_u *vname;
9367 int vlen;
9368 int i;
9369
9370 /*
9371 * Ignore "v:" for old built-in variables, require it for new ones.
9372 */
9373 if (name[0] == 'v' && name[1] == ':')
9374 {
9375 vname = name + 2;
9376 vlen = len - 2;
9377 }
9378 else
9379 {
9380 vname = name;
9381 vlen = len;
9382 }
9383 for (i = 0; i < VV_LEN; ++i)
9384 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
9385 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
9386 return i;
9387 return -1;
9388}
9389
9390/*
9391 * Set number v: variable to "val".
9392 */
9393 void
9394set_vim_var_nr(idx, val)
9395 int idx;
9396 long val;
9397{
9398 vimvars[idx].val = (char_u *)val;
9399}
9400
9401/*
9402 * Get number v: variable value;
9403 */
9404 long
9405get_vim_var_nr(idx)
9406 int idx;
9407{
9408 return (long)vimvars[idx].val;
9409}
9410
9411/*
9412 * Set v:count, v:count1 and v:prevcount.
9413 */
9414 void
9415set_vcount(count, count1)
9416 long count;
9417 long count1;
9418{
9419 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
9420 vimvars[VV_COUNT].val = (char_u *)count;
9421 vimvars[VV_COUNT1].val = (char_u *)count1;
9422}
9423
9424/*
9425 * Set string v: variable to a copy of "val".
9426 */
9427 void
9428set_vim_var_string(idx, val, len)
9429 int idx;
9430 char_u *val;
9431 int len; /* length of "val" to use or -1 (whole string) */
9432{
9433 vim_free(vimvars[idx].val);
9434 if (val == NULL)
9435 vimvars[idx].val = NULL;
9436 else if (len == -1)
9437 vimvars[idx].val = vim_strsave(val);
9438 else
9439 vimvars[idx].val = vim_strnsave(val, len);
9440}
9441
9442/*
9443 * Set v:register if needed.
9444 */
9445 void
9446set_reg_var(c)
9447 int c;
9448{
9449 char_u regname;
9450
9451 if (c == 0 || c == ' ')
9452 regname = '"';
9453 else
9454 regname = c;
9455 /* Avoid free/alloc when the value is already right. */
9456 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
9457 set_vim_var_string(VV_REG, &regname, 1);
9458}
9459
9460/*
9461 * Get or set v:exception. If "oldval" == NULL, return the current value.
9462 * Otherwise, restore the value to "oldval" and return NULL.
9463 * Must always be called in pairs to save and restore v:exception! Does not
9464 * take care of memory allocations.
9465 */
9466 char_u *
9467v_exception(oldval)
9468 char_u *oldval;
9469{
9470 if (oldval == NULL)
9471 return vimvars[VV_EXCEPTION].val;
9472
9473 vimvars[VV_EXCEPTION].val = oldval;
9474 return NULL;
9475}
9476
9477/*
9478 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
9479 * Otherwise, restore the value to "oldval" and return NULL.
9480 * Must always be called in pairs to save and restore v:throwpoint! Does not
9481 * take care of memory allocations.
9482 */
9483 char_u *
9484v_throwpoint(oldval)
9485 char_u *oldval;
9486{
9487 if (oldval == NULL)
9488 return vimvars[VV_THROWPOINT].val;
9489
9490 vimvars[VV_THROWPOINT].val = oldval;
9491 return NULL;
9492}
9493
9494#if defined(FEAT_AUTOCMD) || defined(PROTO)
9495/*
9496 * Set v:cmdarg.
9497 * If "eap" != NULL, use "eap" to generate the value and return the old value.
9498 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
9499 * Must always be called in pairs!
9500 */
9501 char_u *
9502set_cmdarg(eap, oldarg)
9503 exarg_T *eap;
9504 char_u *oldarg;
9505{
9506 char_u *oldval;
9507 char_u *newval;
9508 unsigned len;
9509
9510 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009511 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009513 vim_free(oldval);
9514 vimvars[VV_CMDARG].val = oldarg;
9515 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 }
9517
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009518 if (eap->force_bin == FORCE_BIN)
9519 len = 6;
9520 else if (eap->force_bin == FORCE_NOBIN)
9521 len = 8;
9522 else
9523 len = 0;
9524 if (eap->force_ff != 0)
9525 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
9526# ifdef FEAT_MBYTE
9527 if (eap->force_enc != 0)
9528 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
9529# endif
9530
9531 newval = alloc(len + 1);
9532 if (newval == NULL)
9533 return NULL;
9534
9535 if (eap->force_bin == FORCE_BIN)
9536 sprintf((char *)newval, " ++bin");
9537 else if (eap->force_bin == FORCE_NOBIN)
9538 sprintf((char *)newval, " ++nobin");
9539 else
9540 *newval = NUL;
9541 if (eap->force_ff != 0)
9542 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
9543 eap->cmd + eap->force_ff);
9544# ifdef FEAT_MBYTE
9545 if (eap->force_enc != 0)
9546 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
9547 eap->cmd + eap->force_enc);
9548# endif
9549 vimvars[VV_CMDARG].val = newval;
9550 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551}
9552#endif
9553
9554/*
9555 * Get the value of internal variable "name".
9556 * Return OK or FAIL.
9557 */
9558 static int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559get_var_vartv(name, len, retvar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 char_u *name;
9561 int len; /* length of "name" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009562 typeval *retvar; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 int ret = OK;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009565 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 VAR v;
9567 int cc;
9568 int i;
9569
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009570 tv.v_type = VAR_UNKNOWN;
9571
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 /* truncate the name, so that we can use strcmp() */
9573 cc = name[len];
9574 name[len] = NUL;
9575
9576 /*
9577 * Check for "b:changedtick".
9578 */
9579 if (STRCMP(name, "b:changedtick") == 0)
9580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009581 tv.v_type = VAR_NUMBER;
9582 tv.vval.v_number = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 }
9584
9585 /*
9586 * Check for built-in v: variables.
9587 */
9588 else if ((i = find_vim_var(name, len)) >= 0)
9589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009590 tv.v_type = vimvars[i].type;
9591 if (tv.v_type == VAR_NUMBER)
9592 tv.vval.v_number = (long)vimvars[i].val;
9593 else
9594 tv.vval.v_string = vimvars[i].val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 }
9596
9597 /*
9598 * Check for user-defined variables.
9599 */
9600 else
9601 {
9602 v = find_var(name, FALSE);
9603 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009604 tv = v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 }
9606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009607 if (tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 {
9609 if (retvar != NULL)
9610 EMSG2(_("E121: Undefined variable: %s"), name);
9611 ret = FAIL;
9612 }
9613 else if (retvar != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009614 copy_vartv(&tv, retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615
9616 name[len] = cc;
9617
9618 return ret;
9619}
9620
9621/*
9622 * Allocate memory for a variable, and make it emtpy (0 or NULL value).
9623 */
9624 static VAR
9625alloc_var()
9626{
9627 return (VAR)alloc_clear((unsigned)sizeof(var));
9628}
9629
9630/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009631 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
9632 * value).
9633 */
9634 static typeval *
9635alloc_vartv()
9636{
9637 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
9638}
9639
9640/*
9641 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 * The string "s" must have been allocated, it is consumed.
9643 * Return NULL for out of memory, the variable otherwise.
9644 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009645 static typeval *
9646alloc_string_vartv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 char_u *s;
9648{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009649 typeval *retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009651 retvar = alloc_vartv();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 if (retvar != NULL)
9653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009654 retvar->v_type = VAR_STRING;
9655 retvar->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 }
9657 else
9658 vim_free(s);
9659 return retvar;
9660}
9661
9662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009663 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 */
9665 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009666free_vartv(varp)
9667 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669 if (varp != NULL)
9670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009671 switch (varp->v_type)
9672 {
9673 case VAR_STRING:
9674 case VAR_FUNC:
9675 vim_free(varp->vval.v_string);
9676 break;
9677 case VAR_LIST:
9678 list_unref(varp->vval.v_list);
9679 break;
9680 default:
9681 break;
9682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 vim_free(varp);
9684 }
9685}
9686
9687/*
9688 * Free the memory for a variable value and set the value to NULL or 0.
9689 */
9690 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691clear_vartv(varp)
9692 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 if (varp != NULL)
9695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009696 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 case VAR_STRING:
9699 case VAR_FUNC:
9700 vim_free(varp->vval.v_string);
9701 varp->vval.v_string = NULL;
9702 break;
9703 case VAR_LIST:
9704 list_unref(varp->vval.v_list);
9705 break;
9706 default:
9707 varp->vval.v_number = 0;
9708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 }
9711}
9712
9713/*
9714 * Get the number value of a variable.
9715 * If it is a String variable, uses vim_str2nr().
9716 */
9717 static long
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009718get_vartv_number(varp)
9719 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009721 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009723 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009725 case VAR_NUMBER:
9726 n = (long)(varp->vval.v_number);
9727 break;
9728 case VAR_FUNC:
9729 EMSG(_("E999: Using function reference as a number"));
9730 break;
9731 case VAR_STRING:
9732 if (varp->vval.v_string != NULL)
9733 vim_str2nr(varp->vval.v_string, NULL, NULL,
9734 TRUE, TRUE, &n, NULL);
9735 break;
9736 default:
9737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009739 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740}
9741
9742/*
9743 * Get the lnum from the first argument. Also accepts ".", "$", etc.
9744 */
9745 static linenr_T
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009746get_vartv_lnum(argvars)
9747 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009749 typeval retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 linenr_T lnum;
9751
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009752 lnum = get_vartv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 if (lnum == 0) /* no valid number, try using line() */
9754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009755 retvar.v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 f_line(argvars, &retvar);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009757 lnum = retvar.vval.v_number;
9758 clear_vartv(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 }
9760 return lnum;
9761}
9762
9763/*
9764 * Get the string value of a variable.
9765 * If it is a Number variable, the number is converted into a string.
9766 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
9767 * get_var_string_buf() uses a given buffer.
9768 * If the String variable has never been set, return an empty string.
9769 * Never returns NULL;
9770 */
9771 static char_u *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009772get_vartv_string(varp)
9773 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
9775 static char_u mybuf[NUMBUFLEN];
9776
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009777 return get_vartv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778}
9779
9780 static char_u *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009781get_vartv_string_buf(varp, buf)
9782 typeval *varp;
9783 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009785 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009787 case VAR_NUMBER:
9788 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
9789 return buf;
9790 case VAR_FUNC:
9791 EMSG(_("E99: using Funcref as a String"));
9792 break;
9793 case VAR_LIST:
9794 EMSG(_("E99: using List as a String"));
9795 break;
9796 case VAR_STRING:
9797 if (varp->vval.v_string != NULL)
9798 return varp->vval.v_string;
9799 break;
9800 default:
9801 EMSG(_("E999: Internal error: get_vartv_string_buf()"));
9802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009804 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
9807/*
9808 * Find variable "name" in the list of variables.
9809 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009810 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 */
9812 static VAR
9813find_var(name, writing)
9814 char_u *name;
9815 int writing;
9816{
9817 int i;
9818 char_u *varname;
9819 garray_T *gap;
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 if (name[0] == 'a' && name[1] == ':')
9822 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009823 /* Function arguments "a:".
9824 * NOTE: We use a typecast, because function arguments don't have a
9825 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (writing)
9827 {
9828 EMSG2(_(e_readonlyvar), name);
9829 return NULL;
9830 }
9831 name += 2;
9832 if (current_funccal == NULL)
9833 return NULL;
9834 if (VIM_ISDIGIT(*name))
9835 {
9836 i = atol((char *)name);
9837 if (i == 0) /* a:0 */
9838 return &current_funccal->a0_var;
9839 i += current_funccal->func->args.ga_len;
9840 if (i > current_funccal->argcount) /* a:999 */
9841 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009842 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
9844 if (STRCMP(name, "firstline") == 0)
9845 return &(current_funccal->firstline);
9846 if (STRCMP(name, "lastline") == 0)
9847 return &(current_funccal->lastline);
9848 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
9849 if (STRCMP(name, ((char_u **)
9850 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009851 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 return NULL;
9853 }
9854
9855 gap = find_var_ga(name, &varname);
9856 if (gap == NULL)
9857 return NULL;
9858 return find_var_in_ga(gap, varname);
9859}
9860
9861 static VAR
9862find_var_in_ga(gap, varname)
9863 garray_T *gap;
9864 char_u *varname;
9865{
9866 int i;
9867
9868 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
9870 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 break;
9872 if (i < 0)
9873 return NULL;
9874 return &VAR_GAP_ENTRY(i, gap);
9875}
9876
9877/*
9878 * Find the growarray and start of name without ':' for a variable name.
9879 */
9880 static garray_T *
9881find_var_ga(name, varname)
9882 char_u *name;
9883 char_u **varname;
9884{
9885 if (name[1] != ':')
9886 {
9887 /* If not "x:name" there must not be any ":" in the name. */
9888 if (vim_strchr(name, ':') != NULL)
9889 return NULL;
9890 *varname = name;
9891 if (current_funccal == NULL)
9892 return &variables; /* global variable */
9893 return &current_funccal->l_vars; /* local function variable */
9894 }
9895 *varname = name + 2;
9896 if (*name == 'b') /* buffer variable */
9897 return &curbuf->b_vars;
9898 if (*name == 'w') /* window variable */
9899 return &curwin->w_vars;
9900 if (*name == 'g') /* global variable */
9901 return &variables;
9902 if (*name == 'l' && current_funccal != NULL)/* local function variable */
9903 return &current_funccal->l_vars;
9904 if (*name == 's' /* script variable */
9905 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
9906 return &SCRIPT_VARS(current_SID);
9907 return NULL;
9908}
9909
9910/*
9911 * Get the string value of a (global/local) variable.
9912 * Returns NULL when it doesn't exist.
9913 */
9914 char_u *
9915get_var_value(name)
9916 char_u *name;
9917{
9918 VAR v;
9919
9920 v = find_var(name, FALSE);
9921 if (v == NULL)
9922 return NULL;
9923 return get_var_string(v);
9924}
9925
9926/*
9927 * Allocate a new growarry for a sourced script. It will be used while
9928 * sourcing this script and when executing functions defined in the script.
9929 */
9930 void
9931new_script_vars(id)
9932 scid_T id;
9933{
9934 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
9935 {
9936 while (ga_scripts.ga_len < id)
9937 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009938 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 }
9941 }
9942}
9943
9944/*
9945 * Initialize internal variables for use.
9946 */
9947 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009948vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 garray_T *gap;
9950{
9951 ga_init2(gap, (int)sizeof(var), 4);
9952}
9953
9954/*
9955 * Clean up a list of internal variables.
9956 */
9957 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009958vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 garray_T *gap;
9960{
9961 int i;
9962
9963 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009964 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 ga_clear(gap);
9966}
9967
9968 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009969clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 VAR v;
9971{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009972 vim_free(v->v_name);
9973 v->v_name = NULL;
9974 clear_vartv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975}
9976
9977/*
9978 * List the value of one internal variable.
9979 */
9980 static void
9981list_one_var(v, prefix)
9982 VAR v;
9983 char_u *prefix;
9984{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009985 char_u *tofree;
9986 char_u *s;
9987
9988 s = tv2string(&v->tv, &tofree);
9989 list_one_var_a(prefix, v->v_name, v->tv.v_type,
9990 s == NULL ? (char_u *)"" : s);
9991 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992}
9993
9994/*
9995 * List the value of one "v:" variable.
9996 */
9997 static void
9998list_vim_var(i)
9999 int i; /* index in vimvars[] */
10000{
10001 char_u *p;
10002 char_u numbuf[NUMBUFLEN];
10003
10004 if (vimvars[i].type == VAR_NUMBER)
10005 {
10006 p = numbuf;
10007 sprintf((char *)p, "%ld", (long)vimvars[i].val);
10008 }
10009 else if (vimvars[i].val == NULL)
10010 p = (char_u *)"";
10011 else
10012 p = vimvars[i].val;
10013 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
10014 vimvars[i].type, p);
10015}
10016
10017 static void
10018list_one_var_a(prefix, name, type, string)
10019 char_u *prefix;
10020 char_u *name;
10021 int type;
10022 char_u *string;
10023{
10024 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
10025 if (name != NULL) /* "a:" vars don't have a name stored */
10026 msg_puts(name);
10027 msg_putchar(' ');
10028 msg_advance(22);
10029 if (type == VAR_NUMBER)
10030 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010031 else if (type == VAR_FUNC)
10032 msg_putchar('*');
10033 else if (type == VAR_LIST)
10034 {
10035 msg_putchar('[');
10036 if (*string == '[')
10037 ++string;
10038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 else
10040 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010041
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010043
10044 if (type == VAR_FUNC)
10045 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * Set variable "name" to value in "varp".
10050 * If the variable already exists, the value is updated.
10051 * Otherwise the variable is created.
10052 */
10053 static void
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010054set_var(name, varp, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 char_u *name;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010056 typeval *varp;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010057 int copy; /* make copy of value in "varp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 int i;
10060 VAR v;
10061 char_u *varname;
10062 garray_T *gap;
10063
10064 /*
10065 * Handle setting internal v: variables.
10066 */
10067 i = find_vim_var(name, (int)STRLEN(name));
10068 if (i >= 0)
10069 {
10070 if (vimvars[i].flags & VV_RO)
10071 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010072 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
10073 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 else
10075 {
10076 if (vimvars[i].type == VAR_STRING)
10077 {
10078 vim_free(vimvars[i].val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010079 if (copy || varp->v_type != VAR_STRING)
10080 vimvars[i].val = vim_strsave(get_vartv_string(varp));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010081 else
10082 {
10083 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010084 vimvars[i].val = varp->vval.v_string;
10085 varp->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 }
10088 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010089 vimvars[i].val = (char_u *)get_vartv_number(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 }
10091 return;
10092 }
10093
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010094 if (varp->v_type == VAR_FUNC)
10095 {
10096 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
10097 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
10098 ? name[2] : name[0]))
10099 {
10100 EMSG2(_("E999: Funcref variable name must start with a capital: %s"), name);
10101 return;
10102 }
10103 if (function_exists(name))
10104 {
10105 EMSG2(_("E999: Variable name conflicts with existing function: %s"), name);
10106 return;
10107 }
10108 }
10109
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 v = find_var(name, TRUE);
10111 if (v != NULL) /* existing variable, only need to free string */
10112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010113 if (v->tv.v_type != varp->v_type
10114 && !((v->tv.v_type == VAR_STRING
10115 || v->tv.v_type == VAR_NUMBER)
10116 && (varp->v_type == VAR_STRING
10117 || varp->v_type == VAR_NUMBER)))
10118 {
10119 EMSG2(_("E999: Variable type mismatch for: %s"), name);
10120 return;
10121 }
10122 if (v->tv.v_type == VAR_STRING || v->tv.v_type == VAR_FUNC)
10123 vim_free(v->tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 }
10125 else /* add a new variable */
10126 {
10127 gap = find_var_ga(name, &varname);
10128 if (gap == NULL) /* illegal name */
10129 {
10130 EMSG2(_("E461: Illegal variable name: %s"), name);
10131 return;
10132 }
10133
10134 /* Try to use an empty entry */
10135 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010136 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 break;
10138 if (i < 0) /* need to allocate more room */
10139 {
10140 if (ga_grow(gap, 1) == FAIL)
10141 return;
10142 i = gap->ga_len;
10143 }
10144 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010145 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 return;
10147 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010150 if (copy || (varp->v_type != VAR_STRING && varp->v_type != VAR_FUNC))
10151 copy_vartv(varp, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010152 else
10153 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010154 v->tv.v_type = varp->v_type;
10155 v->tv.vval.v_string = varp->vval.v_string;
10156 varp->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158}
10159
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010160/*
10161 * Copy the values from typeval "from" to typeval "to".
10162 * When needed allocates string or increases reference count.
10163 * Does not make a copy of a list!
10164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010166copy_vartv(from, to)
10167 typeval *from;
10168 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010170 to->v_type = from->v_type;
10171 switch (from->v_type)
10172 {
10173 case VAR_NUMBER:
10174 to->vval.v_number = from->vval.v_number;
10175 break;
10176 case VAR_STRING:
10177 case VAR_FUNC:
10178 if (from->vval.v_string == NULL)
10179 to->vval.v_string = NULL;
10180 else
10181 to->vval.v_string = vim_strsave(from->vval.v_string);
10182 break;
10183 case VAR_LIST:
10184 if (from->vval.v_list == NULL)
10185 to->vval.v_list = NULL;
10186 else
10187 {
10188 to->vval.v_list = from->vval.v_list;
10189 ++to->vval.v_list->lv_refcount;
10190 }
10191 break;
10192 default:
10193 EMSG(_("E999: Internal error: copy_vartv()"));
10194 break;
10195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196}
10197
10198/*
10199 * ":echo expr1 ..." print each argument separated with a space, add a
10200 * newline at the end.
10201 * ":echon expr1 ..." print each argument plain.
10202 */
10203 void
10204ex_echo(eap)
10205 exarg_T *eap;
10206{
10207 char_u *arg = eap->arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010208 typeval retvar;
10209 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 char_u *p;
10211 int needclr = TRUE;
10212 int atstart = TRUE;
10213
10214 if (eap->skip)
10215 ++emsg_skip;
10216 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
10217 {
10218 p = arg;
10219 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
10220 {
10221 /*
10222 * Report the invalid expression unless the expression evaluation
10223 * has been cancelled due to an aborting error, an interrupt, or an
10224 * exception.
10225 */
10226 if (!aborting())
10227 EMSG2(_(e_invexpr2), p);
10228 break;
10229 }
10230 if (!eap->skip)
10231 {
10232 if (atstart)
10233 {
10234 atstart = FALSE;
10235 /* Call msg_start() after eval1(), evaluating the expression
10236 * may cause a message to appear. */
10237 if (eap->cmdidx == CMD_echo)
10238 msg_start();
10239 }
10240 else if (eap->cmdidx == CMD_echo)
10241 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010242 for (p = tv2string(&retvar, &tofree); *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 if (*p == '\n' || *p == '\r' || *p == TAB)
10244 {
10245 if (*p != TAB && needclr)
10246 {
10247 /* remove any text still there from the command */
10248 msg_clr_eos();
10249 needclr = FALSE;
10250 }
10251 msg_putchar_attr(*p, echo_attr);
10252 }
10253 else
10254 {
10255#ifdef FEAT_MBYTE
10256 if (has_mbyte)
10257 {
10258 int i = (*mb_ptr2len_check)(p);
10259
10260 (void)msg_outtrans_len_attr(p, i, echo_attr);
10261 p += i - 1;
10262 }
10263 else
10264#endif
10265 (void)msg_outtrans_len_attr(p, 1, echo_attr);
10266 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010267 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010269 clear_vartv(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 arg = skipwhite(arg);
10271 }
10272 eap->nextcmd = check_nextcmd(arg);
10273
10274 if (eap->skip)
10275 --emsg_skip;
10276 else
10277 {
10278 /* remove text that may still be there from the command */
10279 if (needclr)
10280 msg_clr_eos();
10281 if (eap->cmdidx == CMD_echo)
10282 msg_end();
10283 }
10284}
10285
10286/*
10287 * ":echohl {name}".
10288 */
10289 void
10290ex_echohl(eap)
10291 exarg_T *eap;
10292{
10293 int id;
10294
10295 id = syn_name2id(eap->arg);
10296 if (id == 0)
10297 echo_attr = 0;
10298 else
10299 echo_attr = syn_id2attr(id);
10300}
10301
10302/*
10303 * ":execute expr1 ..." execute the result of an expression.
10304 * ":echomsg expr1 ..." Print a message
10305 * ":echoerr expr1 ..." Print an error
10306 * Each gets spaces around each argument and a newline at the end for
10307 * echo commands
10308 */
10309 void
10310ex_execute(eap)
10311 exarg_T *eap;
10312{
10313 char_u *arg = eap->arg;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010314 typeval retvar;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 int ret = OK;
10316 char_u *p;
10317 garray_T ga;
10318 int len;
10319 int save_did_emsg;
10320
10321 ga_init2(&ga, 1, 80);
10322
10323 if (eap->skip)
10324 ++emsg_skip;
10325 while (*arg != NUL && *arg != '|' && *arg != '\n')
10326 {
10327 p = arg;
10328 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
10329 {
10330 /*
10331 * Report the invalid expression unless the expression evaluation
10332 * has been cancelled due to an aborting error, an interrupt, or an
10333 * exception.
10334 */
10335 if (!aborting())
10336 EMSG2(_(e_invexpr2), p);
10337 ret = FAIL;
10338 break;
10339 }
10340
10341 if (!eap->skip)
10342 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343 p = get_vartv_string(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 len = (int)STRLEN(p);
10345 if (ga_grow(&ga, len + 2) == FAIL)
10346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010347 clear_vartv(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 ret = FAIL;
10349 break;
10350 }
10351 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 ga.ga_len += len;
10355 }
10356
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010357 clear_vartv(&retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 arg = skipwhite(arg);
10359 }
10360
10361 if (ret != FAIL && ga.ga_data != NULL)
10362 {
10363 if (eap->cmdidx == CMD_echomsg)
10364 MSG_ATTR(ga.ga_data, echo_attr);
10365 else if (eap->cmdidx == CMD_echoerr)
10366 {
10367 /* We don't want to abort following commands, restore did_emsg. */
10368 save_did_emsg = did_emsg;
10369 EMSG((char_u *)ga.ga_data);
10370 if (!force_abort)
10371 did_emsg = save_did_emsg;
10372 }
10373 else if (eap->cmdidx == CMD_execute)
10374 do_cmdline((char_u *)ga.ga_data,
10375 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
10376 }
10377
10378 ga_clear(&ga);
10379
10380 if (eap->skip)
10381 --emsg_skip;
10382
10383 eap->nextcmd = check_nextcmd(arg);
10384}
10385
10386/*
10387 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
10388 * "arg" points to the "&" or '+' when called, to "option" when returning.
10389 * Returns NULL when no option name found. Otherwise pointer to the char
10390 * after the option name.
10391 */
10392 static char_u *
10393find_option_end(arg, opt_flags)
10394 char_u **arg;
10395 int *opt_flags;
10396{
10397 char_u *p = *arg;
10398
10399 ++p;
10400 if (*p == 'g' && p[1] == ':')
10401 {
10402 *opt_flags = OPT_GLOBAL;
10403 p += 2;
10404 }
10405 else if (*p == 'l' && p[1] == ':')
10406 {
10407 *opt_flags = OPT_LOCAL;
10408 p += 2;
10409 }
10410 else
10411 *opt_flags = 0;
10412
10413 if (!ASCII_ISALPHA(*p))
10414 return NULL;
10415 *arg = p;
10416
10417 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
10418 p += 4; /* termcap option */
10419 else
10420 while (ASCII_ISALPHA(*p))
10421 ++p;
10422 return p;
10423}
10424
10425/*
10426 * ":function"
10427 */
10428 void
10429ex_function(eap)
10430 exarg_T *eap;
10431{
10432 char_u *theline;
10433 int j;
10434 int c;
10435#ifdef FEAT_MAGIC_BRACES
10436 int saved_did_emsg;
10437#endif
10438 char_u *name = NULL;
10439 char_u *p;
10440 char_u *arg;
10441 garray_T newargs;
10442 garray_T newlines;
10443 int varargs = FALSE;
10444 int mustend = FALSE;
10445 int flags = 0;
10446 ufunc_T *fp;
10447 int indent;
10448 int nesting;
10449 char_u *skip_until = NULL;
10450 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010451 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452
10453 /*
10454 * ":function" without argument: list functions.
10455 */
10456 if (ends_excmd(*eap->arg))
10457 {
10458 if (!eap->skip)
10459 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
10460 list_func_head(fp, FALSE);
10461 eap->nextcmd = check_nextcmd(eap->arg);
10462 return;
10463 }
10464
10465 p = eap->arg;
10466 name = trans_function_name(&p, eap->skip, FALSE);
10467 if (name == NULL && !eap->skip)
10468 {
10469 /*
10470 * Return on an invalid expression in braces, unless the expression
10471 * evaluation has been cancelled due to an aborting error, an
10472 * interrupt, or an exception.
10473 */
10474 if (!aborting())
10475 return;
10476 else
10477 eap->skip = TRUE;
10478 }
10479#ifdef FEAT_MAGIC_BRACES
10480 /* An error in a function call during evaluation of an expression in magic
10481 * braces should not cause the function not to be defined. */
10482 saved_did_emsg = did_emsg;
10483 did_emsg = FALSE;
10484#endif
10485
10486 /*
10487 * ":function func" with only function name: list function.
10488 */
10489 if (vim_strchr(p, '(') == NULL)
10490 {
10491 if (!ends_excmd(*skipwhite(p)))
10492 {
10493 EMSG(_(e_trailing));
10494 goto erret_name;
10495 }
10496 eap->nextcmd = check_nextcmd(p);
10497 if (eap->nextcmd != NULL)
10498 *p = NUL;
10499 if (!eap->skip && !got_int)
10500 {
10501 fp = find_func(name);
10502 if (fp != NULL)
10503 {
10504 list_func_head(fp, TRUE);
10505 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
10506 {
10507 msg_putchar('\n');
10508 msg_outnum((long)(j + 1));
10509 if (j < 9)
10510 msg_putchar(' ');
10511 if (j < 99)
10512 msg_putchar(' ');
10513 msg_prt_line(FUNCLINE(fp, j));
10514 out_flush(); /* show a line at a time */
10515 ui_breakcheck();
10516 }
10517 if (!got_int)
10518 {
10519 msg_putchar('\n');
10520 msg_puts((char_u *)" endfunction");
10521 }
10522 }
10523 else
10524 EMSG2(_("E123: Undefined function: %s"), eap->arg);
10525 }
10526 goto erret_name;
10527 }
10528
10529 /*
10530 * ":function name(arg1, arg2)" Define function.
10531 */
10532 p = skipwhite(p);
10533 if (*p != '(')
10534 {
10535 if (!eap->skip)
10536 {
10537 EMSG2(_("E124: Missing '(': %s"), eap->arg);
10538 goto erret_name;
10539 }
10540 /* attempt to continue by skipping some text */
10541 if (vim_strchr(p, '(') != NULL)
10542 p = vim_strchr(p, '(');
10543 }
10544 p = skipwhite(p + 1);
10545
10546 ga_init2(&newargs, (int)sizeof(char_u *), 3);
10547 ga_init2(&newlines, (int)sizeof(char_u *), 3);
10548
10549 /*
10550 * Isolate the arguments: "arg1, arg2, ...)"
10551 */
10552 while (*p != ')')
10553 {
10554 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
10555 {
10556 varargs = TRUE;
10557 p += 3;
10558 mustend = TRUE;
10559 }
10560 else
10561 {
10562 arg = p;
10563 while (ASCII_ISALNUM(*p) || *p == '_')
10564 ++p;
10565 if (arg == p || isdigit(*arg)
10566 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
10567 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
10568 {
10569 if (!eap->skip)
10570 EMSG2(_("E125: Illegal argument: %s"), arg);
10571 break;
10572 }
10573 if (ga_grow(&newargs, 1) == FAIL)
10574 goto erret;
10575 c = *p;
10576 *p = NUL;
10577 arg = vim_strsave(arg);
10578 if (arg == NULL)
10579 goto erret;
10580 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
10581 *p = c;
10582 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (*p == ',')
10584 ++p;
10585 else
10586 mustend = TRUE;
10587 }
10588 p = skipwhite(p);
10589 if (mustend && *p != ')')
10590 {
10591 if (!eap->skip)
10592 EMSG2(_(e_invarg2), eap->arg);
10593 break;
10594 }
10595 }
10596 ++p; /* skip the ')' */
10597
10598 /* find extra arguments "range" and "abort" */
10599 for (;;)
10600 {
10601 p = skipwhite(p);
10602 if (STRNCMP(p, "range", 5) == 0)
10603 {
10604 flags |= FC_RANGE;
10605 p += 5;
10606 }
10607 else if (STRNCMP(p, "abort", 5) == 0)
10608 {
10609 flags |= FC_ABORT;
10610 p += 5;
10611 }
10612 else
10613 break;
10614 }
10615
10616 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
10617 EMSG(_(e_trailing));
10618
10619 /*
10620 * Read the body of the function, until ":endfunction" is found.
10621 */
10622 if (KeyTyped)
10623 {
10624 /* Check if the function already exists, don't let the user type the
10625 * whole function before telling him it doesn't work! For a script we
10626 * need to skip the body to be able to find what follows. */
10627 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
10628 EMSG2(_(e_funcexts), name);
10629
10630 msg_putchar('\n'); /* don't overwrite the function name */
10631 cmdline_row = msg_row;
10632 }
10633
10634 indent = 2;
10635 nesting = 0;
10636 for (;;)
10637 {
10638 msg_scroll = TRUE;
10639 need_wait_return = FALSE;
10640 if (eap->getline == NULL)
10641 theline = getcmdline(':', 0L, indent);
10642 else
10643 theline = eap->getline(':', eap->cookie, indent);
10644 if (KeyTyped)
10645 lines_left = Rows - 1;
10646 if (theline == NULL)
10647 {
10648 EMSG(_("E126: Missing :endfunction"));
10649 goto erret;
10650 }
10651
10652 if (skip_until != NULL)
10653 {
10654 /* between ":append" and "." and between ":python <<EOF" and "EOF"
10655 * don't check for ":endfunc". */
10656 if (STRCMP(theline, skip_until) == 0)
10657 {
10658 vim_free(skip_until);
10659 skip_until = NULL;
10660 }
10661 }
10662 else
10663 {
10664 /* skip ':' and blanks*/
10665 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
10666 ;
10667
10668 /* Check for "endfunction" (should be more strict...). */
10669 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
10670 {
10671 vim_free(theline);
10672 break;
10673 }
10674
10675 /* Increase indent inside "if", "while", and "try", decrease
10676 * at "end". */
10677 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
10678 indent -= 2;
10679 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
10680 || STRNCMP(p, "try", 3) == 0)
10681 indent += 2;
10682
10683 /* Check for defining a function inside this function. */
10684 if (STRNCMP(p, "fu", 2) == 0)
10685 {
10686 p = skipwhite(skiptowhite(p));
10687 p += eval_fname_script(p);
10688 if (ASCII_ISALPHA(*p))
10689 {
10690 vim_free(trans_function_name(&p, TRUE, FALSE));
10691 if (*skipwhite(p) == '(')
10692 {
10693 ++nesting;
10694 indent += 2;
10695 }
10696 }
10697 }
10698
10699 /* Check for ":append" or ":insert". */
10700 p = skip_range(p, NULL);
10701 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
10702 || (p[0] == 'i'
10703 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
10704 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
10705 skip_until = vim_strsave((char_u *)".");
10706
10707 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
10708 arg = skipwhite(skiptowhite(p));
10709 if (arg[0] == '<' && arg[1] =='<'
10710 && ((p[0] == 'p' && p[1] == 'y'
10711 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
10712 || (p[0] == 'p' && p[1] == 'e'
10713 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
10714 || (p[0] == 't' && p[1] == 'c'
10715 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
10716 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
10717 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010718 || (p[0] == 'm' && p[1] == 'z'
10719 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 ))
10721 {
10722 /* ":python <<" continues until a dot, like ":append" */
10723 p = skipwhite(arg + 2);
10724 if (*p == NUL)
10725 skip_until = vim_strsave((char_u *)".");
10726 else
10727 skip_until = vim_strsave(p);
10728 }
10729 }
10730
10731 /* Add the line to the function. */
10732 if (ga_grow(&newlines, 1) == FAIL)
10733 goto erret;
10734 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
10735 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 }
10737
10738 /* Don't define the function when skipping commands or when an error was
10739 * detected. */
10740 if (eap->skip || did_emsg)
10741 goto erret;
10742
10743 /*
10744 * If there are no errors, add the function
10745 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010746 v = find_var(name, FALSE);
10747 if (v != NULL && v->tv.v_type == VAR_FUNC)
10748 {
10749 EMSG2(_("E999: Function name conflicts with variable: %s"), name);
10750 goto erret;
10751 }
10752
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 fp = find_func(name);
10754 if (fp != NULL)
10755 {
10756 if (!eap->forceit)
10757 {
10758 EMSG2(_(e_funcexts), name);
10759 goto erret;
10760 }
10761 if (fp->calls)
10762 {
10763 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
10764 goto erret;
10765 }
10766 /* redefine existing function */
10767 ga_clear_strings(&(fp->args));
10768 ga_clear_strings(&(fp->lines));
10769 vim_free(name);
10770 }
10771 else
10772 {
10773 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
10774 if (fp == NULL)
10775 goto erret;
10776 /* insert the new function in the function list */
10777 fp->next = firstfunc;
10778 firstfunc = fp;
10779 fp->name = name;
10780 }
10781 fp->args = newargs;
10782 fp->lines = newlines;
10783 fp->varargs = varargs;
10784 fp->flags = flags;
10785 fp->calls = 0;
10786 fp->script_ID = current_SID;
10787#ifdef FEAT_MAGIC_BRACES
10788 did_emsg |= saved_did_emsg;
10789#endif
10790 vim_free(skip_until);
10791 return;
10792
10793erret:
10794 vim_free(skip_until);
10795 ga_clear_strings(&newargs);
10796 ga_clear_strings(&newlines);
10797erret_name:
10798 vim_free(name);
10799#ifdef FEAT_MAGIC_BRACES
10800 did_emsg |= saved_did_emsg;
10801#endif
10802}
10803
10804/*
10805 * Get a function name, translating "<SID>" and "<SNR>".
10806 * Returns the function name in allocated memory, or NULL for failure.
10807 * Advances "pp" to just after the function name (if no error).
10808 */
10809 static char_u *
10810trans_function_name(pp, skip, internal)
10811 char_u **pp;
10812 int skip; /* only find the end, don't evaluate */
10813 int internal; /* TRUE if internal function name OK */
10814{
10815 char_u *name;
10816 char_u *start;
10817 char_u *end;
10818 int lead;
10819 char_u sid_buf[20];
10820 char_u *temp_string = NULL;
10821 char_u *expr_start, *expr_end;
10822 int len;
10823
10824 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
10825 start = *pp;
10826 lead = eval_fname_script(start);
10827 if (lead > 0)
10828 start += lead;
10829 end = find_name_end(start, &expr_start, &expr_end);
10830 if (end == start)
10831 {
10832 if (!skip)
10833 EMSG(_("E129: Function name required"));
10834 return NULL;
10835 }
10836#ifdef FEAT_MAGIC_BRACES
10837 if (expr_start != NULL && !skip)
10838 {
10839 /* expand magic curlies */
10840 temp_string = make_expanded_name(start, expr_start, expr_end, end);
10841 if (temp_string == NULL)
10842 {
10843 /*
10844 * Report an invalid expression in braces, unless the expression
10845 * evaluation has been cancelled due to an aborting error, an
10846 * interrupt, or an exception.
10847 */
10848 if (!aborting())
10849 EMSG2(_(e_invarg2), start);
10850 else
10851 *pp = end;
10852 return NULL;
10853 }
10854 start = temp_string;
10855 len = (int)STRLEN(temp_string);
10856 }
10857 else
10858#endif
10859 len = (int)(end - start);
10860
10861 /*
10862 * Copy the function name to allocated memory.
10863 * Accept <SID>name() inside a script, translate into <SNR>123_name().
10864 * Accept <SNR>123_name() outside a script.
10865 */
10866 if (skip)
10867 lead = 0; /* do nothing */
10868 else if (lead > 0)
10869 {
10870 lead = 3;
10871 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
10872 {
10873 if (current_SID <= 0)
10874 {
10875 EMSG(_(e_usingsid));
10876 return NULL;
10877 }
10878 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
10879 lead += (int)STRLEN(sid_buf);
10880 }
10881 }
10882 else if (!internal && !ASCII_ISUPPER(*start))
10883 {
10884 EMSG2(_("E128: Function name must start with a capital: %s"), start);
10885 return NULL;
10886 }
10887 name = alloc((unsigned)(len + lead + 1));
10888 if (name != NULL)
10889 {
10890 if (lead > 0)
10891 {
10892 name[0] = K_SPECIAL;
10893 name[1] = KS_EXTRA;
10894 name[2] = (int)KE_SNR;
10895 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
10896 STRCPY(name + 3, sid_buf);
10897 }
10898 mch_memmove(name + lead, start, (size_t)len);
10899 name[len + lead] = NUL;
10900 }
10901 *pp = end;
10902
10903 vim_free(temp_string);
10904 return name;
10905}
10906
10907/*
10908 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
10909 * Return 2 if "p" starts with "s:".
10910 * Return 0 otherwise.
10911 */
10912 static int
10913eval_fname_script(p)
10914 char_u *p;
10915{
10916 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
10917 || STRNICMP(p + 1, "SNR>", 4) == 0))
10918 return 5;
10919 if (p[0] == 's' && p[1] == ':')
10920 return 2;
10921 return 0;
10922}
10923
10924/*
10925 * Return TRUE if "p" starts with "<SID>" or "s:".
10926 * Only works if eval_fname_script() returned non-zero for "p"!
10927 */
10928 static int
10929eval_fname_sid(p)
10930 char_u *p;
10931{
10932 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
10933}
10934
10935/*
10936 * List the head of the function: "name(arg1, arg2)".
10937 */
10938 static void
10939list_func_head(fp, indent)
10940 ufunc_T *fp;
10941 int indent;
10942{
10943 int j;
10944
10945 msg_start();
10946 if (indent)
10947 MSG_PUTS(" ");
10948 MSG_PUTS("function ");
10949 if (fp->name[0] == K_SPECIAL)
10950 {
10951 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
10952 msg_puts(fp->name + 3);
10953 }
10954 else
10955 msg_puts(fp->name);
10956 msg_putchar('(');
10957 for (j = 0; j < fp->args.ga_len; ++j)
10958 {
10959 if (j)
10960 MSG_PUTS(", ");
10961 msg_puts(FUNCARG(fp, j));
10962 }
10963 if (fp->varargs)
10964 {
10965 if (j)
10966 MSG_PUTS(", ");
10967 MSG_PUTS("...");
10968 }
10969 msg_putchar(')');
10970}
10971
10972/*
10973 * Find a function by name, return pointer to it in ufuncs.
10974 * Return NULL for unknown function.
10975 */
10976 static ufunc_T *
10977find_func(name)
10978 char_u *name;
10979{
10980 ufunc_T *fp;
10981
10982 for (fp = firstfunc; fp != NULL; fp = fp->next)
10983 if (STRCMP(name, fp->name) == 0)
10984 break;
10985 return fp;
10986}
10987
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010988/*
10989 * Return TRUE if a function "name" exists.
10990 */
10991 static int
10992function_exists(name)
10993 char_u *name;
10994{
10995 char_u *p = name;
10996 int n = FALSE;
10997
10998 p = trans_function_name(&p, FALSE, TRUE);
10999 if (p != NULL)
11000 {
11001 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
11002 n = (find_func(p) != NULL);
11003 else if (ASCII_ISLOWER(*p))
11004 n = (find_internal_func(p) >= 0);
11005 vim_free(p);
11006 }
11007 return n;
11008}
11009
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11011
11012/*
11013 * Function given to ExpandGeneric() to obtain the list of user defined
11014 * function names.
11015 */
11016 char_u *
11017get_user_func_name(xp, idx)
11018 expand_T *xp;
11019 int idx;
11020{
11021 static ufunc_T *fp = NULL;
11022
11023 if (idx == 0)
11024 fp = firstfunc;
11025 if (fp != NULL)
11026 {
11027 if (STRLEN(fp->name) + 4 >= IOSIZE)
11028 return fp->name; /* prevents overflow */
11029
11030 cat_func_name(IObuff, fp);
11031 if (xp->xp_context != EXPAND_USER_FUNC)
11032 {
11033 STRCAT(IObuff, "(");
11034 if (!fp->varargs && fp->args.ga_len == 0)
11035 STRCAT(IObuff, ")");
11036 }
11037
11038 fp = fp->next;
11039 return IObuff;
11040 }
11041 return NULL;
11042}
11043
11044#endif /* FEAT_CMDL_COMPL */
11045
11046/*
11047 * Copy the function name of "fp" to buffer "buf".
11048 * "buf" must be able to hold the function name plus three bytes.
11049 * Takes care of script-local function names.
11050 */
11051 static void
11052cat_func_name(buf, fp)
11053 char_u *buf;
11054 ufunc_T *fp;
11055{
11056 if (fp->name[0] == K_SPECIAL)
11057 {
11058 STRCPY(buf, "<SNR>");
11059 STRCAT(buf, fp->name + 3);
11060 }
11061 else
11062 STRCPY(buf, fp->name);
11063}
11064
11065/*
11066 * ":delfunction {name}"
11067 */
11068 void
11069ex_delfunction(eap)
11070 exarg_T *eap;
11071{
11072 ufunc_T *fp = NULL, *pfp;
11073 char_u *p;
11074 char_u *name;
11075
11076 p = eap->arg;
11077 name = trans_function_name(&p, eap->skip, FALSE);
11078 if (name == NULL)
11079 return;
11080 if (!ends_excmd(*skipwhite(p)))
11081 {
11082 vim_free(name);
11083 EMSG(_(e_trailing));
11084 return;
11085 }
11086 eap->nextcmd = check_nextcmd(p);
11087 if (eap->nextcmd != NULL)
11088 *p = NUL;
11089
11090 if (!eap->skip)
11091 fp = find_func(name);
11092 vim_free(name);
11093
11094 if (!eap->skip)
11095 {
11096 if (fp == NULL)
11097 {
11098 EMSG2(_("E130: Undefined function: %s"), eap->arg);
11099 return;
11100 }
11101 if (fp->calls)
11102 {
11103 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
11104 return;
11105 }
11106
11107 /* clear this function */
11108 vim_free(fp->name);
11109 ga_clear_strings(&(fp->args));
11110 ga_clear_strings(&(fp->lines));
11111
11112 /* remove the function from the function list */
11113 if (firstfunc == fp)
11114 firstfunc = fp->next;
11115 else
11116 {
11117 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
11118 if (pfp->next == fp)
11119 {
11120 pfp->next = fp->next;
11121 break;
11122 }
11123 }
11124 vim_free(fp);
11125 }
11126}
11127
11128/*
11129 * Call a user function.
11130 */
11131 static void
11132call_user_func(fp, argcount, argvars, retvar, firstline, lastline)
11133 ufunc_T *fp; /* pointer to function */
11134 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011135 typeval *argvars; /* arguments */
11136 typeval *retvar; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 linenr_T firstline; /* first line of range */
11138 linenr_T lastline; /* last line of range */
11139{
11140 char_u *save_sourcing_name;
11141 linenr_T save_sourcing_lnum;
11142 scid_T save_current_SID;
11143 struct funccall fc;
11144 struct funccall *save_fcp = current_funccal;
11145 int save_did_emsg;
11146 static int depth = 0;
11147
11148 /* If depth of calling is getting too high, don't execute the function */
11149 if (depth >= p_mfd)
11150 {
11151 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011152 retvar->v_type = VAR_NUMBER;
11153 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 return;
11155 }
11156 ++depth;
11157
11158 line_breakcheck(); /* check for CTRL-C hit */
11159
11160 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011161 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 fc.func = fp;
11163 fc.argcount = argcount;
11164 fc.argvars = argvars;
11165 fc.retvar = retvar;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011166 retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 fc.linenr = 0;
11168 fc.returned = FALSE;
11169 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011170 fc.a0_var.tv.v_type = VAR_NUMBER;
11171 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
11172 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011174 fc.firstline.tv.v_type = VAR_NUMBER;
11175 fc.firstline.tv.vval.v_number = firstline;
11176 fc.firstline.v_name = NULL;
11177 fc.lastline.tv.v_type = VAR_NUMBER;
11178 fc.lastline.tv.vval.v_number = lastline;
11179 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 /* Check if this function has a breakpoint. */
11181 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
11182 fc.dbg_tick = debug_tick;
11183
11184 /* Don't redraw while executing the function. */
11185 ++RedrawingDisabled;
11186 save_sourcing_name = sourcing_name;
11187 save_sourcing_lnum = sourcing_lnum;
11188 sourcing_lnum = 1;
11189 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
11190 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
11191 if (sourcing_name != NULL)
11192 {
11193 if (save_sourcing_name != NULL
11194 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
11195 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
11196 else
11197 STRCPY(sourcing_name, "function ");
11198 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
11199
11200 if (p_verbose >= 12)
11201 {
11202 ++no_wait_return;
11203 msg_scroll = TRUE; /* always scroll up, don't overwrite */
11204 msg_str((char_u *)_("calling %s"), sourcing_name);
11205 if (p_verbose >= 14)
11206 {
11207 int i;
11208 char_u buf[MSG_BUF_LEN];
11209
11210 msg_puts((char_u *)"(");
11211 for (i = 0; i < argcount; ++i)
11212 {
11213 if (i > 0)
11214 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011215 if (argvars[i].v_type == VAR_NUMBER)
11216 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 else
11218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011219 trunc_string(get_vartv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 buf, MSG_BUF_LEN);
11221 msg_puts((char_u *)"\"");
11222 msg_puts(buf);
11223 msg_puts((char_u *)"\"");
11224 }
11225 }
11226 msg_puts((char_u *)")");
11227 }
11228 msg_puts((char_u *)"\n"); /* don't overwrite this either */
11229 cmdline_row = msg_row;
11230 --no_wait_return;
11231 }
11232 }
11233 save_current_SID = current_SID;
11234 current_SID = fp->script_ID;
11235 save_did_emsg = did_emsg;
11236 did_emsg = FALSE;
11237
11238 /* call do_cmdline() to execute the lines */
11239 do_cmdline(NULL, get_func_line, (void *)&fc,
11240 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
11241
11242 --RedrawingDisabled;
11243
11244 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011245 if ((did_emsg && (fp->flags & FC_ABORT)) || retvar->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011247 clear_vartv(retvar);
11248 retvar->v_type = VAR_NUMBER;
11249 retvar->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 }
11251
11252 /* when being verbose, mention the return value */
11253 if (p_verbose >= 12)
11254 {
11255 char_u *sn, *val;
11256
11257 ++no_wait_return;
11258 msg_scroll = TRUE; /* always scroll up, don't overwrite */
11259
11260 /* Make sure the output fits in IObuff. */
11261 sn = sourcing_name;
11262 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
11263 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
11264
11265 if (aborting())
11266 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011267 else if (fc.retvar->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011269 (long)fc.retvar->vval.v_number);
11270 else if (fc.retvar->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011272 val = get_vartv_string(fc.retvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 if (STRLEN(val) > IOSIZE / 2 - 50)
11274 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
11275 smsg((char_u *)_("%s returning \"%s\""), sn, val);
11276 }
11277 msg_puts((char_u *)"\n"); /* don't overwrite this either */
11278 cmdline_row = msg_row;
11279 --no_wait_return;
11280 }
11281
11282 vim_free(sourcing_name);
11283 sourcing_name = save_sourcing_name;
11284 sourcing_lnum = save_sourcing_lnum;
11285 current_SID = save_current_SID;
11286
11287 if (p_verbose >= 12 && sourcing_name != NULL)
11288 {
11289 ++no_wait_return;
11290 msg_scroll = TRUE; /* always scroll up, don't overwrite */
11291 msg_str((char_u *)_("continuing in %s"), sourcing_name);
11292 msg_puts((char_u *)"\n"); /* don't overwrite this either */
11293 cmdline_row = msg_row;
11294 --no_wait_return;
11295 }
11296
11297 did_emsg |= save_did_emsg;
11298 current_funccal = save_fcp;
11299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 --depth;
11302}
11303
11304/*
11305 * ":return [expr]"
11306 */
11307 void
11308ex_return(eap)
11309 exarg_T *eap;
11310{
11311 char_u *arg = eap->arg;
11312 var retvar;
11313 int returning = FALSE;
11314
11315 if (current_funccal == NULL)
11316 {
11317 EMSG(_("E133: :return not inside a function"));
11318 return;
11319 }
11320
11321 if (eap->skip)
11322 ++emsg_skip;
11323
11324 eap->nextcmd = NULL;
11325 if ((*arg != NUL && *arg != '|' && *arg != '\n')
11326 && eval0(arg, &retvar, &eap->nextcmd, !eap->skip) != FAIL)
11327 {
11328 if (!eap->skip)
11329 returning = do_return(eap, FALSE, TRUE, &retvar);
11330 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011331 clear_vartv(&retvar.tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
11333 /* It's safer to return also on error. */
11334 else if (!eap->skip)
11335 {
11336 /*
11337 * Return unless the expression evaluation has been cancelled due to an
11338 * aborting error, an interrupt, or an exception.
11339 */
11340 if (!aborting())
11341 returning = do_return(eap, FALSE, TRUE, NULL);
11342 }
11343
11344 /* When skipping or the return gets pending, advance to the next command
11345 * in this line (!returning). Otherwise, ignore the rest of the line.
11346 * Following lines will be ignored by get_func_line(). */
11347 if (returning)
11348 eap->nextcmd = NULL;
11349 else if (eap->nextcmd == NULL) /* no argument */
11350 eap->nextcmd = check_nextcmd(arg);
11351
11352 if (eap->skip)
11353 --emsg_skip;
11354}
11355
11356/*
11357 * Return from a function. Possibly makes the return pending. Also called
11358 * for a pending return at the ":endtry" or after returning from an extra
11359 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
11360 * when called due to a ":return" command. "value" may point to a variable
11361 * with the return value. Returns TRUE when the return can be carried out,
11362 * FALSE when the return gets pending.
11363 */
11364 int
11365do_return(eap, reanimate, is_cmd, value)
11366 exarg_T *eap;
11367 int reanimate;
11368 int is_cmd;
11369 void *value;
11370{
11371 int idx;
11372 struct condstack *cstack = eap->cstack;
11373
11374 if (reanimate)
11375 /* Undo the return. */
11376 current_funccal->returned = FALSE;
11377
11378 /*
11379 * Cleanup (and inactivate) conditionals, but stop when a try conditional
11380 * not in its finally clause (which then is to be executed next) is found.
11381 * In this case, make the ":return" pending for execution at the ":endtry".
11382 * Otherwise, return normally.
11383 */
11384 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
11385 if (idx >= 0)
11386 {
11387 cstack->cs_pending[idx] = CSTP_RETURN;
11388
11389 if (!is_cmd && !reanimate)
11390 /* A pending return again gets pending. "value" points to an
11391 * allocated variable with the value of the original ":return"'s
11392 * argument if present or is NULL else. */
11393 cstack->cs_retvar[idx] = value;
11394 else
11395 {
11396 /* When undoing a return in order to make it pending, get the stored
11397 * return value. */
11398 if (reanimate)
11399 value = current_funccal->retvar;
11400
11401 if (value != NULL)
11402 {
11403 /* Store the value of the pending return. */
11404 if ((cstack->cs_retvar[idx] = alloc_var()) != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011405 *(typeval *)cstack->cs_retvar[idx] = *(typeval *)value;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 else
11407 EMSG(_(e_outofmem));
11408 }
11409 else
11410 cstack->cs_retvar[idx] = NULL;
11411
11412 if (reanimate)
11413 {
11414 /* The pending return value could be overwritten by a ":return"
11415 * without argument in a finally clause; reset the default
11416 * return value. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011417 current_funccal->retvar->v_type = VAR_NUMBER;
11418 current_funccal->retvar->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 }
11420 }
11421 report_make_pending(CSTP_RETURN, value);
11422 }
11423 else
11424 {
11425 current_funccal->returned = TRUE;
11426
11427 /* If the return is carried out now, store the return value. For
11428 * a return immediately after reanimation, the value is already
11429 * there. */
11430 if (!reanimate && value != NULL)
11431 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011432 clear_vartv(current_funccal->retvar);
11433 *current_funccal->retvar = *(typeval *)value;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 if (!is_cmd)
11435 vim_free(value);
11436 }
11437 }
11438
11439 return idx < 0;
11440}
11441
11442/*
11443 * Free the variable with a pending return value.
11444 */
11445 void
11446discard_pending_return(retvar)
11447 void *retvar;
11448{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011449 /* The variable was copied from one with an undefined v_name. So we can't
11450 * use free_vartv() to clear and free it. */
11451 clear_vartv(&((VAR)retvar)->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 vim_free(retvar);
11453}
11454
11455/*
11456 * Generate a return command for producing the value of "retvar". The result
11457 * is an allocated string. Used by report_pending() for verbose messages.
11458 */
11459 char_u *
11460get_return_cmd(retvar)
11461 void *retvar;
11462{
11463 char_u *s = IObuff;
11464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011465 if (retvar == NULL || ((VAR)retvar)->tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 s = (char_u *)":return";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011467 else if (((VAR)retvar)->tv.v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 sprintf((char *)IObuff, ":return \"%s\"",
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011469 ((VAR)retvar)->tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 else
11471 sprintf((char *)IObuff, ":return %ld",
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472 (long)(((VAR)retvar)->tv.vval.v_number));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 return vim_strsave(s);
11474}
11475
11476/*
11477 * Get next function line.
11478 * Called by do_cmdline() to get the next line.
11479 * Returns allocated string, or NULL for end of function.
11480 */
11481/* ARGSUSED */
11482 char_u *
11483get_func_line(c, cookie, indent)
11484 int c; /* not used */
11485 void *cookie;
11486 int indent; /* not used */
11487{
11488 struct funccall *fcp = (struct funccall *)cookie;
11489 char_u *retval;
11490 garray_T *gap; /* growarray with function lines */
11491
11492 /* If breakpoints have been added/deleted need to check for it. */
11493 if (fcp->dbg_tick != debug_tick)
11494 {
11495 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
11496 sourcing_lnum);
11497 fcp->dbg_tick = debug_tick;
11498 }
11499
11500 gap = &fcp->func->lines;
11501 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
11502 retval = NULL;
11503 else if (fcp->returned || fcp->linenr >= gap->ga_len)
11504 retval = NULL;
11505 else
11506 {
11507 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
11508 sourcing_lnum = fcp->linenr;
11509 }
11510
11511 /* Did we encounter a breakpoint? */
11512 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
11513 {
11514 dbg_breakpoint(fcp->func->name, sourcing_lnum);
11515 /* Find next breakpoint. */
11516 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
11517 sourcing_lnum);
11518 fcp->dbg_tick = debug_tick;
11519 }
11520
11521 return retval;
11522}
11523
11524/*
11525 * Return TRUE if the currently active function should be ended, because a
11526 * return was encountered or an error occured. Used inside a ":while".
11527 */
11528 int
11529func_has_ended(cookie)
11530 void *cookie;
11531{
11532 struct funccall *fcp = (struct funccall *)cookie;
11533
11534 /* Ignore the "abort" flag if the abortion behavior has been changed due to
11535 * an error inside a try conditional. */
11536 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
11537 || fcp->returned);
11538}
11539
11540/*
11541 * return TRUE if cookie indicates a function which "abort"s on errors.
11542 */
11543 int
11544func_has_abort(cookie)
11545 void *cookie;
11546{
11547 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
11548}
11549
11550#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
11551typedef enum
11552{
11553 VAR_FLAVOUR_DEFAULT,
11554 VAR_FLAVOUR_SESSION,
11555 VAR_FLAVOUR_VIMINFO
11556} var_flavour_T;
11557
11558static var_flavour_T var_flavour __ARGS((char_u *varname));
11559
11560 static var_flavour_T
11561var_flavour(varname)
11562 char_u *varname;
11563{
11564 char_u *p = varname;
11565
11566 if (ASCII_ISUPPER(*p))
11567 {
11568 while (*(++p))
11569 if (ASCII_ISLOWER(*p))
11570 return VAR_FLAVOUR_SESSION;
11571 return VAR_FLAVOUR_VIMINFO;
11572 }
11573 else
11574 return VAR_FLAVOUR_DEFAULT;
11575}
11576#endif
11577
11578#if defined(FEAT_VIMINFO) || defined(PROTO)
11579/*
11580 * Restore global vars that start with a capital from the viminfo file
11581 */
11582 int
11583read_viminfo_varlist(virp, writing)
11584 vir_T *virp;
11585 int writing;
11586{
11587 char_u *tab;
11588 int is_string = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011589 typeval *vartvp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 char_u *val;
11591
11592 if (!writing && (find_viminfo_parameter('!') != NULL))
11593 {
11594 tab = vim_strchr(virp->vir_line + 1, '\t');
11595 if (tab != NULL)
11596 {
11597 *tab++ = '\0'; /* isolate the variable name */
11598 if (*tab == 'S') /* string var */
11599 is_string = TRUE;
11600
11601 tab = vim_strchr(tab, '\t');
11602 if (tab != NULL)
11603 {
11604 /* create a nameless variable to hold the value */
11605 if (is_string)
11606 {
11607 val = viminfo_readstring(virp,
11608 (int)(tab - virp->vir_line + 1), TRUE);
11609 if (val != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610 vartvp = alloc_string_vartv(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 }
11612 else
11613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011614 vartvp = alloc_vartv();
11615 if (vartvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011617 vartvp->v_type = VAR_NUMBER;
11618 vartvp->vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 }
11620 }
11621 /* assign the value to the variable */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011622 if (vartvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011624 set_var(virp->vir_line + 1, vartvp, FALSE);
11625 free_vartv(vartvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 }
11627 }
11628 }
11629 }
11630
11631 return viminfo_readline(virp);
11632}
11633
11634/*
11635 * Write global vars that start with a capital to the viminfo file
11636 */
11637 void
11638write_viminfo_varlist(fp)
11639 FILE *fp;
11640{
11641 garray_T *gap = &variables; /* global variable */
11642 VAR this_var;
11643 int i;
11644
11645 if (find_viminfo_parameter('!') == NULL)
11646 return;
11647
11648 fprintf(fp, _("\n# global variables:\n"));
11649 for (i = gap->ga_len; --i >= 0; )
11650 {
11651 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011652 if (this_var->v_name != NULL
11653 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 fprintf(fp, "!%s\t%s\t", this_var->v_name,
11656 (this_var->tv.v_type == VAR_STRING) ? "STR" : "NUM");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 viminfo_writestring(fp, get_var_string(this_var));
11658 }
11659 }
11660}
11661#endif
11662
11663#if defined(FEAT_SESSION) || defined(PROTO)
11664 int
11665store_session_globals(fd)
11666 FILE *fd;
11667{
11668 garray_T *gap = &variables; /* global variable */
11669 VAR this_var;
11670 int i;
11671 char_u *p, *t;
11672
11673 for (i = gap->ga_len; --i >= 0; )
11674 {
11675 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011676 if (this_var->v_name != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011678 if (var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 {
11680 /* Escapse special characters with a backslash. Turn a LF and
11681 * CR into \n and \r. */
11682 p = vim_strsave_escaped(get_var_string(this_var),
11683 (char_u *)"\\\"\n\r");
11684 if (p == NULL) /* out of memory */
11685 continue;
11686 for (t = p; *t != NUL; ++t)
11687 if (*t == '\n')
11688 *t = 'n';
11689 else if (*t == '\r')
11690 *t = 'r';
11691 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011692 this_var->v_name,
11693 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
11694 p,
11695 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 || put_eol(fd) == FAIL)
11697 {
11698 vim_free(p);
11699 return FAIL;
11700 }
11701 vim_free(p);
11702 }
11703
11704 }
11705 }
11706 return OK;
11707}
11708#endif
11709
11710#endif /* FEAT_EVAL */
11711
11712#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
11713
11714
11715#ifdef WIN3264
11716/*
11717 * Functions for ":8" filename modifier: get 8.3 version of a filename.
11718 */
11719static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
11720static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
11721static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
11722
11723/*
11724 * Get the short pathname of a file.
11725 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
11726 */
11727 static int
11728get_short_pathname(fnamep, bufp, fnamelen)
11729 char_u **fnamep;
11730 char_u **bufp;
11731 int *fnamelen;
11732{
11733 int l,len;
11734 char_u *newbuf;
11735
11736 len = *fnamelen;
11737
11738 l = GetShortPathName(*fnamep, *fnamep, len);
11739 if (l > len - 1)
11740 {
11741 /* If that doesn't work (not enough space), then save the string
11742 * and try again with a new buffer big enough
11743 */
11744 newbuf = vim_strnsave(*fnamep, l);
11745 if (newbuf == NULL)
11746 return 0;
11747
11748 vim_free(*bufp);
11749 *fnamep = *bufp = newbuf;
11750
11751 l = GetShortPathName(*fnamep,*fnamep,l+1);
11752
11753 /* Really should always succeed, as the buffer is big enough */
11754 }
11755
11756 *fnamelen = l;
11757 return 1;
11758}
11759
11760/*
11761 * Create a short path name. Returns the length of the buffer it needs.
11762 * Doesn't copy over the end of the buffer passed in.
11763 */
11764 static int
11765shortpath_for_invalid_fname(fname, bufp, fnamelen)
11766 char_u **fname;
11767 char_u **bufp;
11768 int *fnamelen;
11769{
11770 char_u *s, *p, *pbuf2, *pbuf3;
11771 char_u ch;
11772 int l,len,len2,plen,slen;
11773
11774 /* Make a copy */
11775 len2 = *fnamelen;
11776 pbuf2 = vim_strnsave(*fname, len2);
11777 pbuf3 = NULL;
11778
11779 s = pbuf2 + len2 - 1; /* Find the end */
11780 slen = 1;
11781 plen = len2;
11782
11783 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011784 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 {
11786 --s;
11787 ++slen;
11788 --plen;
11789 }
11790
11791 do
11792 {
11793 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011794 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 {
11796 --s;
11797 ++slen;
11798 --plen;
11799 }
11800 if (s <= pbuf2)
11801 break;
11802
11803 /* Remeber the character that is about to be blatted */
11804 ch = *s;
11805 *s = 0; /* get_short_pathname requires a null-terminated string */
11806
11807 /* Try it in situ */
11808 p = pbuf2;
11809 if (!get_short_pathname(&p, &pbuf3, &plen))
11810 {
11811 vim_free(pbuf2);
11812 return -1;
11813 }
11814 *s = ch; /* Preserve the string */
11815 } while (plen == 0);
11816
11817 if (plen > 0)
11818 {
11819 /* Remeber the length of the new string. */
11820 *fnamelen = len = plen + slen;
11821 vim_free(*bufp);
11822 if (len > len2)
11823 {
11824 /* If there's not enough space in the currently allocated string,
11825 * then copy it to a buffer big enough.
11826 */
11827 *fname= *bufp = vim_strnsave(p, len);
11828 if (*fname == NULL)
11829 return -1;
11830 }
11831 else
11832 {
11833 /* Transfer pbuf2 to being the main buffer (it's big enough) */
11834 *fname = *bufp = pbuf2;
11835 if (p != pbuf2)
11836 strncpy(*fname, p, plen);
11837 pbuf2 = NULL;
11838 }
11839 /* Concat the next bit */
11840 strncpy(*fname + plen, s, slen);
11841 (*fname)[len] = '\0';
11842 }
11843 vim_free(pbuf3);
11844 vim_free(pbuf2);
11845 return 0;
11846}
11847
11848/*
11849 * Get a pathname for a partial path.
11850 */
11851 static int
11852shortpath_for_partial(fnamep, bufp, fnamelen)
11853 char_u **fnamep;
11854 char_u **bufp;
11855 int *fnamelen;
11856{
11857 int sepcount, len, tflen;
11858 char_u *p;
11859 char_u *pbuf, *tfname;
11860 int hasTilde;
11861
11862 /* Count up the path seperators from the RHS.. so we know which part
11863 * of the path to return.
11864 */
11865 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011866 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 if (vim_ispathsep(*p))
11868 ++sepcount;
11869
11870 /* Need full path first (use expand_env() to remove a "~/") */
11871 hasTilde = (**fnamep == '~');
11872 if (hasTilde)
11873 pbuf = tfname = expand_env_save(*fnamep);
11874 else
11875 pbuf = tfname = FullName_save(*fnamep, FALSE);
11876
11877 len = tflen = STRLEN(tfname);
11878
11879 if (!get_short_pathname(&tfname, &pbuf, &len))
11880 return -1;
11881
11882 if (len == 0)
11883 {
11884 /* Don't have a valid filename, so shorten the rest of the
11885 * path if we can. This CAN give us invalid 8.3 filenames, but
11886 * there's not a lot of point in guessing what it might be.
11887 */
11888 len = tflen;
11889 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
11890 return -1;
11891 }
11892
11893 /* Count the paths backward to find the beginning of the desired string. */
11894 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011895 {
11896#ifdef FEAT_MBYTE
11897 if (has_mbyte)
11898 p -= mb_head_off(tfname, p);
11899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 if (vim_ispathsep(*p))
11901 {
11902 if (sepcount == 0 || (hasTilde && sepcount == 1))
11903 break;
11904 else
11905 sepcount --;
11906 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 if (hasTilde)
11909 {
11910 --p;
11911 if (p >= tfname)
11912 *p = '~';
11913 else
11914 return -1;
11915 }
11916 else
11917 ++p;
11918
11919 /* Copy in the string - p indexes into tfname - allocated at pbuf */
11920 vim_free(*bufp);
11921 *fnamelen = (int)STRLEN(p);
11922 *bufp = pbuf;
11923 *fnamep = p;
11924
11925 return 0;
11926}
11927#endif /* WIN3264 */
11928
11929/*
11930 * Adjust a filename, according to a string of modifiers.
11931 * *fnamep must be NUL terminated when called. When returning, the length is
11932 * determined by *fnamelen.
11933 * Returns valid flags.
11934 * When there is an error, *fnamep is set to NULL.
11935 */
11936 int
11937modify_fname(src, usedlen, fnamep, bufp, fnamelen)
11938 char_u *src; /* string with modifiers */
11939 int *usedlen; /* characters after src that are used */
11940 char_u **fnamep; /* file name so far */
11941 char_u **bufp; /* buffer for allocated file name or NULL */
11942 int *fnamelen; /* length of fnamep */
11943{
11944 int valid = 0;
11945 char_u *tail;
11946 char_u *s, *p, *pbuf;
11947 char_u dirname[MAXPATHL];
11948 int c;
11949 int has_fullname = 0;
11950#ifdef WIN3264
11951 int has_shortname = 0;
11952#endif
11953
11954repeat:
11955 /* ":p" - full path/file_name */
11956 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
11957 {
11958 has_fullname = 1;
11959
11960 valid |= VALID_PATH;
11961 *usedlen += 2;
11962
11963 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
11964 if ((*fnamep)[0] == '~'
11965#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
11966 && ((*fnamep)[1] == '/'
11967# ifdef BACKSLASH_IN_FILENAME
11968 || (*fnamep)[1] == '\\'
11969# endif
11970 || (*fnamep)[1] == NUL)
11971
11972#endif
11973 )
11974 {
11975 *fnamep = expand_env_save(*fnamep);
11976 vim_free(*bufp); /* free any allocated file name */
11977 *bufp = *fnamep;
11978 if (*fnamep == NULL)
11979 return -1;
11980 }
11981
11982 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011983 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 {
11985 if (vim_ispathsep(*p)
11986 && p[1] == '.'
11987 && (p[2] == NUL
11988 || vim_ispathsep(p[2])
11989 || (p[2] == '.'
11990 && (p[3] == NUL || vim_ispathsep(p[3])))))
11991 break;
11992 }
11993
11994 /* FullName_save() is slow, don't use it when not needed. */
11995 if (*p != NUL || !vim_isAbsName(*fnamep))
11996 {
11997 *fnamep = FullName_save(*fnamep, *p != NUL);
11998 vim_free(*bufp); /* free any allocated file name */
11999 *bufp = *fnamep;
12000 if (*fnamep == NULL)
12001 return -1;
12002 }
12003
12004 /* Append a path separator to a directory. */
12005 if (mch_isdir(*fnamep))
12006 {
12007 /* Make room for one or two extra characters. */
12008 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
12009 vim_free(*bufp); /* free any allocated file name */
12010 *bufp = *fnamep;
12011 if (*fnamep == NULL)
12012 return -1;
12013 add_pathsep(*fnamep);
12014 }
12015 }
12016
12017 /* ":." - path relative to the current directory */
12018 /* ":~" - path relative to the home directory */
12019 /* ":8" - shortname path - postponed till after */
12020 while (src[*usedlen] == ':'
12021 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
12022 {
12023 *usedlen += 2;
12024 if (c == '8')
12025 {
12026#ifdef WIN3264
12027 has_shortname = 1; /* Postpone this. */
12028#endif
12029 continue;
12030 }
12031 pbuf = NULL;
12032 /* Need full path first (use expand_env() to remove a "~/") */
12033 if (!has_fullname)
12034 {
12035 if (c == '.' && **fnamep == '~')
12036 p = pbuf = expand_env_save(*fnamep);
12037 else
12038 p = pbuf = FullName_save(*fnamep, FALSE);
12039 }
12040 else
12041 p = *fnamep;
12042
12043 has_fullname = 0;
12044
12045 if (p != NULL)
12046 {
12047 if (c == '.')
12048 {
12049 mch_dirname(dirname, MAXPATHL);
12050 s = shorten_fname(p, dirname);
12051 if (s != NULL)
12052 {
12053 *fnamep = s;
12054 if (pbuf != NULL)
12055 {
12056 vim_free(*bufp); /* free any allocated file name */
12057 *bufp = pbuf;
12058 pbuf = NULL;
12059 }
12060 }
12061 }
12062 else
12063 {
12064 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
12065 /* Only replace it when it starts with '~' */
12066 if (*dirname == '~')
12067 {
12068 s = vim_strsave(dirname);
12069 if (s != NULL)
12070 {
12071 *fnamep = s;
12072 vim_free(*bufp);
12073 *bufp = s;
12074 }
12075 }
12076 }
12077 vim_free(pbuf);
12078 }
12079 }
12080
12081 tail = gettail(*fnamep);
12082 *fnamelen = (int)STRLEN(*fnamep);
12083
12084 /* ":h" - head, remove "/file_name", can be repeated */
12085 /* Don't remove the first "/" or "c:\" */
12086 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
12087 {
12088 valid |= VALID_HEAD;
12089 *usedlen += 2;
12090 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012091 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 --tail;
12093 *fnamelen = (int)(tail - *fnamep);
12094#ifdef VMS
12095 if (*fnamelen > 0)
12096 *fnamelen += 1; /* the path separator is part of the path */
12097#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012098 while (tail > s && !after_pathsep(s, tail))
12099 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 }
12101
12102 /* ":8" - shortname */
12103 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
12104 {
12105 *usedlen += 2;
12106#ifdef WIN3264
12107 has_shortname = 1;
12108#endif
12109 }
12110
12111#ifdef WIN3264
12112 /* Check shortname after we have done 'heads' and before we do 'tails'
12113 */
12114 if (has_shortname)
12115 {
12116 pbuf = NULL;
12117 /* Copy the string if it is shortened by :h */
12118 if (*fnamelen < (int)STRLEN(*fnamep))
12119 {
12120 p = vim_strnsave(*fnamep, *fnamelen);
12121 if (p == 0)
12122 return -1;
12123 vim_free(*bufp);
12124 *bufp = *fnamep = p;
12125 }
12126
12127 /* Split into two implementations - makes it easier. First is where
12128 * there isn't a full name already, second is where there is.
12129 */
12130 if (!has_fullname && !vim_isAbsName(*fnamep))
12131 {
12132 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
12133 return -1;
12134 }
12135 else
12136 {
12137 int l;
12138
12139 /* Simple case, already have the full-name
12140 * Nearly always shorter, so try first time. */
12141 l = *fnamelen;
12142 if (!get_short_pathname(fnamep, bufp, &l))
12143 return -1;
12144
12145 if (l == 0)
12146 {
12147 /* Couldn't find the filename.. search the paths.
12148 */
12149 l = *fnamelen;
12150 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
12151 return -1;
12152 }
12153 *fnamelen = l;
12154 }
12155 }
12156#endif /* WIN3264 */
12157
12158 /* ":t" - tail, just the basename */
12159 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
12160 {
12161 *usedlen += 2;
12162 *fnamelen -= (int)(tail - *fnamep);
12163 *fnamep = tail;
12164 }
12165
12166 /* ":e" - extension, can be repeated */
12167 /* ":r" - root, without extension, can be repeated */
12168 while (src[*usedlen] == ':'
12169 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
12170 {
12171 /* find a '.' in the tail:
12172 * - for second :e: before the current fname
12173 * - otherwise: The last '.'
12174 */
12175 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
12176 s = *fnamep - 2;
12177 else
12178 s = *fnamep + *fnamelen - 1;
12179 for ( ; s > tail; --s)
12180 if (s[0] == '.')
12181 break;
12182 if (src[*usedlen + 1] == 'e') /* :e */
12183 {
12184 if (s > tail)
12185 {
12186 *fnamelen += (int)(*fnamep - (s + 1));
12187 *fnamep = s + 1;
12188#ifdef VMS
12189 /* cut version from the extension */
12190 s = *fnamep + *fnamelen - 1;
12191 for ( ; s > *fnamep; --s)
12192 if (s[0] == ';')
12193 break;
12194 if (s > *fnamep)
12195 *fnamelen = s - *fnamep;
12196#endif
12197 }
12198 else if (*fnamep <= tail)
12199 *fnamelen = 0;
12200 }
12201 else /* :r */
12202 {
12203 if (s > tail) /* remove one extension */
12204 *fnamelen = (int)(s - *fnamep);
12205 }
12206 *usedlen += 2;
12207 }
12208
12209 /* ":s?pat?foo?" - substitute */
12210 /* ":gs?pat?foo?" - global substitute */
12211 if (src[*usedlen] == ':'
12212 && (src[*usedlen + 1] == 's'
12213 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
12214 {
12215 char_u *str;
12216 char_u *pat;
12217 char_u *sub;
12218 int sep;
12219 char_u *flags;
12220 int didit = FALSE;
12221
12222 flags = (char_u *)"";
12223 s = src + *usedlen + 2;
12224 if (src[*usedlen + 1] == 'g')
12225 {
12226 flags = (char_u *)"g";
12227 ++s;
12228 }
12229
12230 sep = *s++;
12231 if (sep)
12232 {
12233 /* find end of pattern */
12234 p = vim_strchr(s, sep);
12235 if (p != NULL)
12236 {
12237 pat = vim_strnsave(s, (int)(p - s));
12238 if (pat != NULL)
12239 {
12240 s = p + 1;
12241 /* find end of substitution */
12242 p = vim_strchr(s, sep);
12243 if (p != NULL)
12244 {
12245 sub = vim_strnsave(s, (int)(p - s));
12246 str = vim_strnsave(*fnamep, *fnamelen);
12247 if (sub != NULL && str != NULL)
12248 {
12249 *usedlen = (int)(p + 1 - src);
12250 s = do_string_sub(str, pat, sub, flags);
12251 if (s != NULL)
12252 {
12253 *fnamep = s;
12254 *fnamelen = (int)STRLEN(s);
12255 vim_free(*bufp);
12256 *bufp = s;
12257 didit = TRUE;
12258 }
12259 }
12260 vim_free(sub);
12261 vim_free(str);
12262 }
12263 vim_free(pat);
12264 }
12265 }
12266 /* after using ":s", repeat all the modifiers */
12267 if (didit)
12268 goto repeat;
12269 }
12270 }
12271
12272 return valid;
12273}
12274
12275/*
12276 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
12277 * "flags" can be "g" to do a global substitute.
12278 * Returns an allocated string, NULL for error.
12279 */
12280 char_u *
12281do_string_sub(str, pat, sub, flags)
12282 char_u *str;
12283 char_u *pat;
12284 char_u *sub;
12285 char_u *flags;
12286{
12287 int sublen;
12288 regmatch_T regmatch;
12289 int i;
12290 int do_all;
12291 char_u *tail;
12292 garray_T ga;
12293 char_u *ret;
12294 char_u *save_cpo;
12295
12296 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
12297 save_cpo = p_cpo;
12298 p_cpo = (char_u *)"";
12299
12300 ga_init2(&ga, 1, 200);
12301
12302 do_all = (flags[0] == 'g');
12303
12304 regmatch.rm_ic = p_ic;
12305 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12306 if (regmatch.regprog != NULL)
12307 {
12308 tail = str;
12309 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
12310 {
12311 /*
12312 * Get some space for a temporary buffer to do the substitution
12313 * into. It will contain:
12314 * - The text up to where the match is.
12315 * - The substituted text.
12316 * - The text after the match.
12317 */
12318 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
12319 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
12320 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
12321 {
12322 ga_clear(&ga);
12323 break;
12324 }
12325
12326 /* copy the text up to where the match is */
12327 i = (int)(regmatch.startp[0] - tail);
12328 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
12329 /* add the substituted text */
12330 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
12331 + ga.ga_len + i, TRUE, TRUE, FALSE);
12332 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 /* avoid getting stuck on a match with an empty string */
12334 if (tail == regmatch.endp[0])
12335 {
12336 if (*tail == NUL)
12337 break;
12338 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
12339 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 }
12341 else
12342 {
12343 tail = regmatch.endp[0];
12344 if (*tail == NUL)
12345 break;
12346 }
12347 if (!do_all)
12348 break;
12349 }
12350
12351 if (ga.ga_data != NULL)
12352 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
12353
12354 vim_free(regmatch.regprog);
12355 }
12356
12357 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
12358 ga_clear(&ga);
12359 p_cpo = save_cpo;
12360
12361 return ret;
12362}
12363
12364#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */