blob: dcd86e519ac5c2206994b9a9b94ecf6788c1422c [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/*
40 * Structure to hold an internal variable.
41 */
42typedef struct
43{
44 char_u *var_name; /* name of variable */
45 char var_type; /* VAR_NUMBER or VAR_STRING */
46 union
47 {
48 varnumber_T var_number; /* number value */
49 char_u *var_string; /* string value (Careful: can be NULL!) */
50 } var_val;
51} var;
52
53#define VAR_UNKNOWN 0
54#define VAR_NUMBER 1
55#define VAR_STRING 2
56
57typedef var * VAR;
58
59/*
60 * All user-defined global variables are stored in "variables".
61 */
62garray_T variables = {0, 0, sizeof(var), 4, NULL};
63
64/*
65 * Array to hold an array with variables local to each sourced script.
66 */
67static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
68#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
69
70
71#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
72#define VAR_GAP_ENTRY(idx, gap) (((VAR)(gap->ga_data))[idx])
73#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
74#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
75
76static int echo_attr = 0; /* attributes used for ":echo" */
77
78/*
79 * Structure to hold info for a user function.
80 */
81typedef struct ufunc ufunc_T;
82
83struct ufunc
84{
85 ufunc_T *next; /* next function in list */
86 char_u *name; /* name of function; can start with <SNR>123_
87 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
88 int varargs; /* variable nr of arguments */
89 int flags;
90 int calls; /* nr of active calls */
91 garray_T args; /* arguments */
92 garray_T lines; /* function lines */
93 scid_T script_ID; /* ID of script where function was defined,
94 used for s: variables */
95};
96
97/* function flags */
98#define FC_ABORT 1 /* abort function on error */
99#define FC_RANGE 2 /* function accepts range */
100
101/*
102 * All user-defined functions are found in the forward-linked function list.
103 * The first function is pointed at by firstfunc.
104 */
105ufunc_T *firstfunc = NULL;
106
107#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
108#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
109
110/* structure to hold info for a function that is currently being executed. */
111struct funccall
112{
113 ufunc_T *func; /* function being called */
114 int linenr; /* next line to be executed */
115 int returned; /* ":return" used */
116 int argcount; /* nr of arguments */
117 VAR argvars; /* arguments */
118 var a0_var; /* "a:0" variable */
119 var firstline; /* "a:firstline" variable */
120 var lastline; /* "a:lastline" variable */
121 garray_T l_vars; /* local function variables */
122 VAR retvar; /* return value variable */
123 linenr_T breakpoint; /* next line with breakpoint or zero */
124 int dbg_tick; /* debug_tick when breakpoint was set */
125 int level; /* top nesting level of executed function */
126};
127
128/*
129 * Return the name of the executed function.
130 */
131 char_u *
132func_name(cookie)
133 void *cookie;
134{
135 return ((struct funccall *)cookie)->func->name;
136}
137
138/*
139 * Return the address holding the next breakpoint line for a funccall cookie.
140 */
141 linenr_T *
142func_breakpoint(cookie)
143 void *cookie;
144{
145 return &((struct funccall *)cookie)->breakpoint;
146}
147
148/*
149 * Return the address holding the debug tick for a funccall cookie.
150 */
151 int *
152func_dbg_tick(cookie)
153 void *cookie;
154{
155 return &((struct funccall *)cookie)->dbg_tick;
156}
157
158/*
159 * Return the nesting level for a funccall cookie.
160 */
161 int
162func_level(cookie)
163 void *cookie;
164{
165 return ((struct funccall *)cookie)->level;
166}
167
168/* pointer to funccal for currently active function */
169struct funccall *current_funccal = NULL;
170
171/*
172 * Return TRUE when a function was ended by a ":return" command.
173 */
174 int
175current_func_returned()
176{
177 return current_funccal->returned;
178}
179
180
181/*
182 * Array to hold the value of v: variables.
183 */
184#include "version.h"
185
186/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000187#define VV_COMPAT 1 /* compatible, also used without "v:" */
188#define VV_RO 2 /* read-only */
189#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
191struct vimvar
192{
193 char *name; /* name of variable, without v: */
194 int len; /* length of name */
195 char_u *val; /* current value (can also be a number!) */
196 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000197 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198} vimvars[VV_LEN] =
199{ /* The order here must match the VV_ defines in vim.h! */
200 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
201 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
202 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
203 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
204 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
205 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
206 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
207 VV_COMPAT+VV_RO},
208 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
209 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
210 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000211 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
213 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
214 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
215 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
216 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
217 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
218 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
219 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
220 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
221 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
222 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
223 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000224 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
225 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
226 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
227 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
229 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
230 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
231 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
232 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
233 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
234 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000235 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236};
237
238static int eval0 __ARGS((char_u *arg, VAR retvar, char_u **nextcmd, int evaluate));
239static int eval1 __ARGS((char_u **arg, VAR retvar, int evaluate));
240static int eval2 __ARGS((char_u **arg, VAR retvar, int evaluate));
241static int eval3 __ARGS((char_u **arg, VAR retvar, int evaluate));
242static int eval4 __ARGS((char_u **arg, VAR retvar, int evaluate));
243static int eval5 __ARGS((char_u **arg, VAR retvar, int evaluate));
244static int eval6 __ARGS((char_u **arg, VAR retvar, int evaluate));
245static int eval7 __ARGS((char_u **arg, VAR retvar, int evaluate));
246static int get_option_var __ARGS((char_u **arg, VAR retvar, int evaluate));
247static int get_string_var __ARGS((char_u **arg, VAR retvar, int evaluate));
248static int get_lit_string_var __ARGS((char_u **arg, VAR retvar, int evaluate));
249static int get_env_var __ARGS((char_u **arg, VAR retvar, int evaluate));
250static int find_internal_func __ARGS((char_u *name));
251static int get_func_var __ARGS((char_u *name, int len, VAR retvar, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
252static int call_func __ARGS((char_u *name, int len, VAR retvar, int argcount, VAR argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
253static void f_append __ARGS((VAR argvars, VAR retvar));
254static void f_argc __ARGS((VAR argvars, VAR retvar));
255static void f_argidx __ARGS((VAR argvars, VAR retvar));
256static void f_argv __ARGS((VAR argvars, VAR retvar));
257static void f_browse __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000258static void f_browsedir __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259static buf_T *find_buffer __ARGS((VAR avar));
260static void f_bufexists __ARGS((VAR argvars, VAR retvar));
261static void f_buflisted __ARGS((VAR argvars, VAR retvar));
262static void f_bufloaded __ARGS((VAR argvars, VAR retvar));
263static buf_T *get_buf_var __ARGS((VAR avar));
264static void f_bufname __ARGS((VAR argvars, VAR retvar));
265static void f_bufnr __ARGS((VAR argvars, VAR retvar));
266static void f_bufwinnr __ARGS((VAR argvars, VAR retvar));
267static void f_byte2line __ARGS((VAR argvars, VAR retvar));
Bram Moolenaarab79bcb2004-07-18 21:34:53 +0000268static void f_byteidx __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269static void f_char2nr __ARGS((VAR argvars, VAR retvar));
270static void f_cindent __ARGS((VAR argvars, VAR retvar));
271static void f_col __ARGS((VAR argvars, VAR retvar));
272static void f_confirm __ARGS((VAR argvars, VAR retvar));
273static void f_cscope_connection __ARGS((VAR argvars, VAR retvar));
274static void f_cursor __ARGS((VAR argsvars, VAR retvar));
275static void f_delete __ARGS((VAR argvars, VAR retvar));
276static void f_did_filetype __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar47136d72004-10-12 20:02:24 +0000277static void f_diff_filler __ARGS((VAR argvars, VAR retvar));
278static void f_diff_hlID __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static void f_escape __ARGS((VAR argvars, VAR retvar));
280static void f_eventhandler __ARGS((VAR argvars, VAR retvar));
281static void f_executable __ARGS((VAR argvars, VAR retvar));
282static void f_exists __ARGS((VAR argvars, VAR retvar));
283static void f_expand __ARGS((VAR argvars, VAR retvar));
284static void f_filereadable __ARGS((VAR argvars, VAR retvar));
285static void f_filewritable __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +0000286static void f_finddir __ARGS((VAR argvars, VAR retvar));
287static void f_findfile __ARGS((VAR argvars, VAR retvar));
288static void f_findfilendir __ARGS((VAR argvars, VAR retvar, int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static void f_fnamemodify __ARGS((VAR argvars, VAR retvar));
290static void f_foldclosed __ARGS((VAR argvars, VAR retvar));
291static void f_foldclosedend __ARGS((VAR argvars, VAR retvar));
292static void foldclosed_both __ARGS((VAR argvars, VAR retvar, int end));
293static void f_foldlevel __ARGS((VAR argvars, VAR retvar));
294static void f_foldtext __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000295static void f_foldtextresult __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void f_foreground __ARGS((VAR argvars, VAR retvar));
297static void f_getbufvar __ARGS((VAR argvars, VAR retvar));
298static void f_getchar __ARGS((VAR argvars, VAR retvar));
299static void f_getcharmod __ARGS((VAR argvars, VAR retvar));
300static void f_getcmdline __ARGS((VAR argvars, VAR retvar));
301static void f_getcmdpos __ARGS((VAR argvars, VAR retvar));
302static void f_getcwd __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar46c9c732004-12-12 11:37:09 +0000303static void f_getfontname __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000304static void f_getfperm __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static void f_getfsize __ARGS((VAR argvars, VAR retvar));
306static void f_getftime __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000307static void f_getftype __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static void f_getline __ARGS((VAR argvars, VAR retvar));
309static void f_getreg __ARGS((VAR argvars, VAR retvar));
310static void f_getregtype __ARGS((VAR argvars, VAR retvar));
311static void f_getwinposx __ARGS((VAR argvars, VAR retvar));
312static void f_getwinposy __ARGS((VAR argvars, VAR retvar));
313static void f_getwinvar __ARGS((VAR argvars, VAR retvar));
314static void f_glob __ARGS((VAR argvars, VAR retvar));
315static void f_globpath __ARGS((VAR argvars, VAR retvar));
316static void f_has __ARGS((VAR argvars, VAR retvar));
317static void f_hasmapto __ARGS((VAR argvars, VAR retvar));
318static void f_histadd __ARGS((VAR argvars, VAR retvar));
319static void f_histdel __ARGS((VAR argvars, VAR retvar));
320static void f_histget __ARGS((VAR argvars, VAR retvar));
321static void f_histnr __ARGS((VAR argvars, VAR retvar));
322static void f_hlexists __ARGS((VAR argvars, VAR retvar));
323static void f_hlID __ARGS((VAR argvars, VAR retvar));
324static void f_hostname __ARGS((VAR argvars, VAR retvar));
325static void f_iconv __ARGS((VAR argvars, VAR retvar));
326static void f_indent __ARGS((VAR argvars, VAR retvar));
327static void f_isdirectory __ARGS((VAR argvars, VAR retvar));
328static void f_input __ARGS((VAR argvars, VAR retvar));
329static void f_inputdialog __ARGS((VAR argvars, VAR retvar));
330static void f_inputrestore __ARGS((VAR argvars, VAR retvar));
331static void f_inputsave __ARGS((VAR argvars, VAR retvar));
332static void f_inputsecret __ARGS((VAR argvars, VAR retvar));
333static void f_last_buffer_nr __ARGS((VAR argvars, VAR retvar));
334static void f_libcall __ARGS((VAR argvars, VAR retvar));
335static void f_libcallnr __ARGS((VAR argvars, VAR retvar));
336static void libcall_common __ARGS((VAR argvars, VAR retvar, int type));
337static void f_line __ARGS((VAR argvars, VAR retvar));
338static void f_line2byte __ARGS((VAR argvars, VAR retvar));
339static void f_lispindent __ARGS((VAR argvars, VAR retvar));
340static void f_localtime __ARGS((VAR argvars, VAR retvar));
341static void f_maparg __ARGS((VAR argvars, VAR retvar));
342static void f_mapcheck __ARGS((VAR argvars, VAR retvar));
343static void get_maparg __ARGS((VAR argvars, VAR retvar, int exact));
344static void f_match __ARGS((VAR argvars, VAR retvar));
345static void f_matchend __ARGS((VAR argvars, VAR retvar));
346static void f_matchstr __ARGS((VAR argvars, VAR retvar));
347static void f_mode __ARGS((VAR argvars, VAR retvar));
348static void f_nextnonblank __ARGS((VAR argvars, VAR retvar));
349static void f_nr2char __ARGS((VAR argvars, VAR retvar));
350static void f_prevnonblank __ARGS((VAR argvars, VAR retvar));
351static void f_setbufvar __ARGS((VAR argvars, VAR retvar));
352static void f_setcmdpos __ARGS((VAR argvars, VAR retvar));
353static void f_setwinvar __ARGS((VAR argvars, VAR retvar));
354static void f_rename __ARGS((VAR argvars, VAR retvar));
355static void f_resolve __ARGS((VAR argvars, VAR retvar));
356static void f_search __ARGS((VAR argvars, VAR retvar));
357static void f_searchpair __ARGS((VAR argvars, VAR retvar));
358static int get_search_arg __ARGS((VAR varp, int *flagsp));
359static void f_remote_expr __ARGS((VAR argvars, VAR retvar));
360static void f_remote_foreground __ARGS((VAR argvars, VAR retvar));
361static void f_remote_peek __ARGS((VAR argvars, VAR retvar));
362static void f_remote_read __ARGS((VAR argvars, VAR retvar));
363static void f_remote_send __ARGS((VAR argvars, VAR retvar));
Bram Moolenaarab79bcb2004-07-18 21:34:53 +0000364static void f_repeat __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365static void f_server2client __ARGS((VAR argvars, VAR retvar));
366static void f_serverlist __ARGS((VAR argvars, VAR retvar));
367static void f_setline __ARGS((VAR argvars, VAR retvar));
368static void f_setreg __ARGS((VAR argvars, VAR retvar));
369static void f_simplify __ARGS((VAR argvars, VAR retvar));
370static void find_some_match __ARGS((VAR argvars, VAR retvar, int start));
371static void f_strftime __ARGS((VAR argvars, VAR retvar));
372static void f_stridx __ARGS((VAR argvars, VAR retvar));
373static void f_strlen __ARGS((VAR argvars, VAR retvar));
374static void f_strpart __ARGS((VAR argvars, VAR retvar));
375static void f_strridx __ARGS((VAR argvars, VAR retvar));
376static void f_strtrans __ARGS((VAR argvars, VAR retvar));
377static void f_synID __ARGS((VAR argvars, VAR retvar));
378static void f_synIDattr __ARGS((VAR argvars, VAR retvar));
379static void f_synIDtrans __ARGS((VAR argvars, VAR retvar));
380static void f_system __ARGS((VAR argvars, VAR retvar));
381static void f_submatch __ARGS((VAR argvars, VAR retvar));
382static void f_substitute __ARGS((VAR argvars, VAR retvar));
383static void f_tempname __ARGS((VAR argvars, VAR retvar));
384static void f_tolower __ARGS((VAR argvars, VAR retvar));
385static void f_toupper __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar8299df92004-07-10 09:47:34 +0000386static void f_tr __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static void f_type __ARGS((VAR argvars, VAR retvar));
388static void f_virtcol __ARGS((VAR argvars, VAR retvar));
389static void f_visualmode __ARGS((VAR argvars, VAR retvar));
390static void f_winbufnr __ARGS((VAR argvars, VAR retvar));
391static void f_wincol __ARGS((VAR argvars, VAR retvar));
392static void f_winheight __ARGS((VAR argvars, VAR retvar));
393static void f_winline __ARGS((VAR argvars, VAR retvar));
394static void f_winnr __ARGS((VAR argvars, VAR retvar));
395static void f_winrestcmd __ARGS((VAR argvars, VAR retvar));
396static void f_winwidth __ARGS((VAR argvars, VAR retvar));
397static win_T *find_win_by_nr __ARGS((VAR vp));
398static pos_T *var2fpos __ARGS((VAR varp, int lnum));
399static int get_env_len __ARGS((char_u **arg));
400static int get_id_len __ARGS((char_u **arg));
401static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
402static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end));
403static int eval_isnamec __ARGS((int c));
404static int find_vim_var __ARGS((char_u *name, int len));
405static int get_var_var __ARGS((char_u *name, int len, VAR retvar));
406static VAR alloc_var __ARGS((void));
407static VAR alloc_string_var __ARGS((char_u *string));
408static void free_var __ARGS((VAR varp));
409static void clear_var __ARGS((VAR varp));
410static long get_var_number __ARGS((VAR varp));
411static linenr_T get_var_lnum __ARGS((VAR argvars));
412static char_u *get_var_string __ARGS((VAR varp));
413static char_u *get_var_string_buf __ARGS((VAR varp, char_u *buf));
414static VAR find_var __ARGS((char_u *name, int writing));
415static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
416static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
417static void var_free_one __ARGS((VAR v));
418static void list_one_var __ARGS((VAR v, char_u *prefix));
419static void list_vim_var __ARGS((int i));
420static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
421static void set_var __ARGS((char_u *name, VAR varp));
422static void copy_var __ARGS((VAR from, VAR to));
423static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
424static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
425static int eval_fname_script __ARGS((char_u *p));
426static int eval_fname_sid __ARGS((char_u *p));
427static void list_func_head __ARGS((ufunc_T *fp, int indent));
428static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
429static ufunc_T *find_func __ARGS((char_u *name));
430static void call_user_func __ARGS((ufunc_T *fp, int argcount, VAR argvars, VAR retvar, linenr_T firstline, linenr_T lastline));
431
432/* Magic braces are always enabled, otherwise Vim scripts would not be
433 * portable. */
434#define FEAT_MAGIC_BRACES
435
436#ifdef FEAT_MAGIC_BRACES
437static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
438#endif
439
440/*
441 * Set an internal variable to a string value. Creates the variable if it does
442 * not already exist.
443 */
444 void
445set_internal_string_var(name, value)
446 char_u *name;
447 char_u *value;
448{
449 char_u *val;
450 VAR varp;
451
452 val = vim_strsave(value);
453 if (val != NULL)
454 {
455 varp = alloc_string_var(val);
456 if (varp != NULL)
457 {
458 set_var(name, varp);
459 free_var(varp);
460 }
461 }
462}
463
464# if defined(FEAT_MBYTE) || defined(PROTO)
465 int
466eval_charconvert(enc_from, enc_to, fname_from, fname_to)
467 char_u *enc_from;
468 char_u *enc_to;
469 char_u *fname_from;
470 char_u *fname_to;
471{
472 int err = FALSE;
473
474 set_vim_var_string(VV_CC_FROM, enc_from, -1);
475 set_vim_var_string(VV_CC_TO, enc_to, -1);
476 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
477 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
478 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
479 err = TRUE;
480 set_vim_var_string(VV_CC_FROM, NULL, -1);
481 set_vim_var_string(VV_CC_TO, NULL, -1);
482 set_vim_var_string(VV_FNAME_IN, NULL, -1);
483 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
484
485 if (err)
486 return FAIL;
487 return OK;
488}
489# endif
490
491# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
492 int
493eval_printexpr(fname, args)
494 char_u *fname;
495 char_u *args;
496{
497 int err = FALSE;
498
499 set_vim_var_string(VV_FNAME_IN, fname, -1);
500 set_vim_var_string(VV_CMDARG, args, -1);
501 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
502 err = TRUE;
503 set_vim_var_string(VV_FNAME_IN, NULL, -1);
504 set_vim_var_string(VV_CMDARG, NULL, -1);
505
506 if (err)
507 {
508 mch_remove(fname);
509 return FAIL;
510 }
511 return OK;
512}
513# endif
514
515# if defined(FEAT_DIFF) || defined(PROTO)
516 void
517eval_diff(origfile, newfile, outfile)
518 char_u *origfile;
519 char_u *newfile;
520 char_u *outfile;
521{
522 int err = FALSE;
523
524 set_vim_var_string(VV_FNAME_IN, origfile, -1);
525 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
526 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
527 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
528 set_vim_var_string(VV_FNAME_IN, NULL, -1);
529 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
530 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
531}
532
533 void
534eval_patch(origfile, difffile, outfile)
535 char_u *origfile;
536 char_u *difffile;
537 char_u *outfile;
538{
539 int err;
540
541 set_vim_var_string(VV_FNAME_IN, origfile, -1);
542 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
543 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
544 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
545 set_vim_var_string(VV_FNAME_IN, NULL, -1);
546 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
547 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
548}
549# endif
550
551/*
552 * Top level evaluation function, returning a boolean.
553 * Sets "error" to TRUE if there was an error.
554 * Return TRUE or FALSE.
555 */
556 int
557eval_to_bool(arg, error, nextcmd, skip)
558 char_u *arg;
559 int *error;
560 char_u **nextcmd;
561 int skip; /* only parse, don't execute */
562{
563 var retvar;
564 int retval = FALSE;
565
566 if (skip)
567 ++emsg_skip;
568 if (eval0(arg, &retvar, nextcmd, !skip) == FAIL)
569 {
570 *error = TRUE;
571 }
572 else
573 {
574 *error = FALSE;
575 if (!skip)
576 {
577 retval = (get_var_number(&retvar) != 0);
578 clear_var(&retvar);
579 }
580 }
581 if (skip)
582 --emsg_skip;
583
584 return retval;
585}
586
587/*
588 * Top level evaluation function, returning a string. If "skip" is TRUE,
589 * only parsing to "nextcmd" is done, without reporting errors. Return
590 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
591 */
592 char_u *
593eval_to_string_skip(arg, nextcmd, skip)
594 char_u *arg;
595 char_u **nextcmd;
596 int skip; /* only parse, don't execute */
597{
598 var retvar;
599 char_u *retval;
600
601 if (skip)
602 ++emsg_skip;
603 if (eval0(arg, &retvar, nextcmd, !skip) == FAIL || skip)
604 retval = NULL;
605 else
606 {
607 retval = vim_strsave(get_var_string(&retvar));
608 clear_var(&retvar);
609 }
610 if (skip)
611 --emsg_skip;
612
613 return retval;
614}
615
616/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000617 * Skip over an expression at "*pp".
618 * Return FAIL for an error, OK otherwise.
619 */
620 int
621skip_expr(pp)
622 char_u **pp;
623{
624 var retvar;
625
626 *pp = skipwhite(*pp);
627 return eval1(pp, &retvar, FALSE);
628}
629
630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 * Top level evaluation function, returning a string.
632 * Return pointer to allocated memory, or NULL for failure.
633 */
634 char_u *
635eval_to_string(arg, nextcmd)
636 char_u *arg;
637 char_u **nextcmd;
638{
639 var retvar;
640 char_u *retval;
641
642 if (eval0(arg, &retvar, nextcmd, TRUE) == FAIL)
643 retval = NULL;
644 else
645 {
646 retval = vim_strsave(get_var_string(&retvar));
647 clear_var(&retvar);
648 }
649
650 return retval;
651}
652
653/*
654 * Call eval_to_string() with "sandbox" set and not using local variables.
655 */
656 char_u *
657eval_to_string_safe(arg, nextcmd)
658 char_u *arg;
659 char_u **nextcmd;
660{
661 char_u *retval;
662 void *save_funccalp;
663
664 save_funccalp = save_funccal();
665 ++sandbox;
666 retval = eval_to_string(arg, nextcmd);
667 --sandbox;
668 restore_funccal(save_funccalp);
669 return retval;
670}
671
672#if 0 /* not used */
673/*
674 * Top level evaluation function, returning a string.
675 * Advances "arg" to the first non-blank after the evaluated expression.
676 * Return pointer to allocated memory, or NULL for failure.
677 * Doesn't give error messages.
678 */
679 char_u *
680eval_arg_to_string(arg)
681 char_u **arg;
682{
683 var retvar;
684 char_u *retval;
685 int ret;
686
687 ++emsg_off;
688
689 ret = eval1(arg, &retvar, TRUE);
690 if (ret == FAIL)
691 retval = NULL;
692 else
693 {
694 retval = vim_strsave(get_var_string(&retvar));
695 clear_var(&retvar);
696 }
697
698 --emsg_off;
699
700 return retval;
701}
702#endif
703
704/*
705 * Top level evaluation function, returning a number.
706 * Evaluates "expr" silently.
707 * Returns -1 for an error.
708 */
709 int
710eval_to_number(expr)
711 char_u *expr;
712{
713 var retvar;
714 int retval;
715 char_u *p = expr;
716
717 ++emsg_off;
718
719 if (eval1(&p, &retvar, TRUE) == FAIL)
720 retval = -1;
721 else
722 {
723 retval = get_var_number(&retvar);
724 clear_var(&retvar);
725 }
726 --emsg_off;
727
728 return retval;
729}
730
731#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
732/*
733 * Call some vimL function and return the result as a string
734 * Uses argv[argc] for the function arguments.
735 */
736 char_u *
737call_vim_function(func, argc, argv, safe)
738 char_u *func;
739 int argc;
740 char_u **argv;
741 int safe; /* use the sandbox */
742{
743 char_u *retval = NULL;
744 var retvar;
745 VAR argvars;
746 long n;
747 int len;
748 int i;
749 int doesrange;
750 void *save_funccalp = NULL;
751
752 argvars = (VAR)alloc((unsigned)(argc * sizeof(var)));
753 if (argvars == NULL)
754 return NULL;
755
756 for (i = 0; i < argc; i++)
757 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000758 /* Pass a NULL or empty argument as an empty string */
759 if (argv[i] == NULL || *argv[i] == NUL)
760 {
761 argvars[i].var_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000762 argvars[i].var_val.var_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000763 continue;
764 }
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 /* Recognize a number argument, the others must be strings. */
767 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
768 if (len != 0 && len == (int)STRLEN(argv[i]))
769 {
770 argvars[i].var_type = VAR_NUMBER;
771 argvars[i].var_val.var_number = n;
772 }
773 else
774 {
775 argvars[i].var_type = VAR_STRING;
776 argvars[i].var_val.var_string = argv[i];
777 }
778 }
779
780 if (safe)
781 {
782 save_funccalp = save_funccal();
783 ++sandbox;
784 }
785
786 retvar.var_type = VAR_UNKNOWN; /* clear_var() uses this */
787 if (call_func(func, (int)STRLEN(func), &retvar, argc, argvars,
788 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
789 &doesrange, TRUE) == OK)
790 retval = vim_strsave(get_var_string(&retvar));
791
792 clear_var(&retvar);
793 vim_free(argvars);
794
795 if (safe)
796 {
797 --sandbox;
798 restore_funccal(save_funccalp);
799 }
800 return retval;
801}
802#endif
803
804/*
805 * Save the current function call pointer, and set it to NULL.
806 * Used when executing autocommands and for ":source".
807 */
808 void *
809save_funccal()
810{
811 struct funccall *fc;
812
813 fc = current_funccal;
814 current_funccal = NULL;
815 return (void *)fc;
816}
817
818 void
819restore_funccal(fc)
820 void *fc;
821{
822 current_funccal = (struct funccall *)fc;
823}
824
825#ifdef FEAT_FOLDING
826/*
827 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
828 * it in "*cp". Doesn't give error messages.
829 */
830 int
831eval_foldexpr(arg, cp)
832 char_u *arg;
833 int *cp;
834{
835 var retvar;
836 int retval;
837 char_u *s;
838
839 ++emsg_off;
840 ++sandbox;
841 *cp = NUL;
842 if (eval0(arg, &retvar, NULL, TRUE) == FAIL)
843 retval = 0;
844 else
845 {
846 /* If the result is a number, just return the number. */
847 if (retvar.var_type == VAR_NUMBER)
848 retval = retvar.var_val.var_number;
849 else if (retvar.var_type == VAR_UNKNOWN
850 || retvar.var_val.var_string == NULL)
851 retval = 0;
852 else
853 {
854 /* If the result is a string, check if there is a non-digit before
855 * the number. */
856 s = retvar.var_val.var_string;
857 if (!VIM_ISDIGIT(*s) && *s != '-')
858 *cp = *s++;
859 retval = atol((char *)s);
860 }
861 clear_var(&retvar);
862 }
863 --emsg_off;
864 --sandbox;
865
866 return retval;
867}
868#endif
869
870#ifdef FEAT_MAGIC_BRACES
871/*
872 * Expands out the 'magic' {}'s in a variable/function name.
873 * Note that this can call itself recursively, to deal with
874 * constructs like foo{bar}{baz}{bam}
875 * The four pointer arguments point to "foo{expre}ss{ion}bar"
876 * "in_start" ^
877 * "expr_start" ^
878 * "expr_end" ^
879 * "in_end" ^
880 *
881 * Returns a new allocated string, which the caller must free.
882 * Returns NULL for failure.
883 */
884 static char_u *
885make_expanded_name(in_start, expr_start, expr_end, in_end)
886 char_u *in_start;
887 char_u *expr_start;
888 char_u *expr_end;
889 char_u *in_end;
890{
891 char_u c1;
892 char_u *retval = NULL;
893 char_u *temp_result;
894 char_u *nextcmd = NULL;
895
896 if (expr_end == NULL || in_end == NULL)
897 return NULL;
898 *expr_start = NUL;
899 *expr_end = NUL;
900 c1 = *in_end;
901 *in_end = NUL;
902
903 temp_result = eval_to_string(expr_start + 1, &nextcmd);
904 if (temp_result != NULL && nextcmd == NULL)
905 {
906 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
907 + (in_end - expr_end) + 1));
908
909 if (retval != NULL)
910 {
911 STRCPY(retval, in_start);
912 STRCAT(retval, temp_result);
913 STRCAT(retval, expr_end + 1);
914 }
915 }
916 vim_free(temp_result);
917
918 *in_end = c1; /* put char back for error messages */
919 *expr_start = '{';
920 *expr_end = '}';
921
922 if (retval != NULL)
923 {
924 temp_result = find_name_end(retval, &expr_start, &expr_end);
925 if (expr_start != NULL)
926 {
927 /* Further expansion! */
928 temp_result = make_expanded_name(retval, expr_start,
929 expr_end, temp_result);
930 vim_free(retval);
931 retval = temp_result;
932 }
933 }
934
935 return retval;
936
937}
938#endif /* FEAT_MAGIC_BRACES */
939
940/*
941 * ":let var = expr" assignment command.
942 * ":let var" list one variable value
943 * ":let" list all variable values
944 */
945 void
946ex_let(eap)
947 exarg_T *eap;
948{
949 char_u *arg = eap->arg;
950 char_u *expr;
951 char_u *name;
952 VAR varp;
953 var retvar;
954 char_u *p;
955 int c1 = 0, c2;
956 int i;
957 char_u *expr_start;
958 char_u *expr_end;
959 char_u *name_end;
960
961 name_end = find_name_end(arg, &expr_start, &expr_end);
962 expr = vim_strchr(name_end, '=');
963 if (expr == NULL)
964 {
965 if (ends_excmd(*arg))
966 {
967 if (!eap->skip)
968 {
969 /*
970 * List all variables.
971 */
972 for (i = 0; i < variables.ga_len && !got_int; ++i)
973 if (VAR_ENTRY(i).var_name != NULL)
974 list_one_var(&VAR_ENTRY(i), (char_u *)"");
975 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
976 if (BVAR_ENTRY(i).var_name != NULL)
977 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
978 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
979 if (WVAR_ENTRY(i).var_name != NULL)
980 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
981 for (i = 0; i < VV_LEN && !got_int; ++i)
982 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
983 list_vim_var(i);
984 }
985 }
986 else
987 {
988 int error = FALSE;
989
990 /*
991 * List variables.
992 */
993 while (!ends_excmd(*arg) && !got_int)
994 {
995 char_u *temp_string = NULL;
996 int arg_len;
997
998 /* Find the end of the name. */
999 name_end = find_name_end(arg, &expr_start, &expr_end);
1000
1001 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1002 {
1003 emsg_severe = TRUE;
1004 EMSG(_(e_trailing));
1005 break;
1006 }
1007 if (!error && !eap->skip)
1008 {
1009#ifdef FEAT_MAGIC_BRACES
1010 if (expr_start != NULL)
1011 {
1012 temp_string = make_expanded_name(arg, expr_start,
1013 expr_end, name_end);
1014 if (temp_string == NULL)
1015 {
1016 /*
1017 * Report an invalid expression in braces, unless
1018 * the expression evaluation has been cancelled due
1019 * to an aborting error, an interrupt, or an
1020 * exception.
1021 */
1022 if (!aborting())
1023 {
1024 emsg_severe = TRUE;
1025 EMSG2(_(e_invarg2), arg);
1026 break;
1027 }
1028 error = TRUE;
1029 arg = skipwhite(name_end);
1030 continue;
1031 }
1032 arg = temp_string;
1033 arg_len = STRLEN(temp_string);
1034 }
1035 else
1036#endif
1037 {
1038 c1 = *name_end;
1039 *name_end = NUL;
1040 arg_len = (int)(name_end - arg);
1041 }
1042 i = find_vim_var(arg, arg_len);
1043 if (i >= 0)
1044 list_vim_var(i);
1045 else if (STRCMP("b:changedtick", arg) == 0)
1046 {
1047 char_u numbuf[NUMBUFLEN];
1048
1049 sprintf((char *)numbuf, "%ld",
1050 (long)curbuf->b_changedtick);
1051 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1052 VAR_NUMBER, numbuf);
1053 }
1054 else
1055 {
1056 varp = find_var(arg, FALSE);
1057 if (varp == NULL)
1058 {
1059 /* Skip further arguments but do continue to
1060 * search for a trailing command. */
1061 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1062 error = TRUE;
1063 }
1064 else
1065 {
1066 name = vim_strchr(arg, ':');
1067 if (name != NULL)
1068 {
1069 /* "a:" vars have no name stored, use whole
1070 * arg */
1071 if (arg[0] == 'a' && arg[1] == ':')
1072 c2 = NUL;
1073 else
1074 {
1075 c2 = *++name;
1076 *name = NUL;
1077 }
1078 list_one_var(varp, arg);
1079 if (c2 != NUL)
1080 *name = c2;
1081 }
1082 else
1083 list_one_var(varp, (char_u *)"");
1084 }
1085 }
1086#ifdef FEAT_MAGIC_BRACES
1087 if (expr_start != NULL)
1088 vim_free(temp_string);
1089 else
1090#endif
1091 *name_end = c1;
1092 }
1093 arg = skipwhite(name_end);
1094 }
1095 }
1096 eap->nextcmd = check_nextcmd(arg);
1097 }
1098 else
1099 {
1100 if (eap->skip)
1101 ++emsg_skip;
1102 i = eval0(expr + 1, &retvar, &eap->nextcmd, !eap->skip);
1103 if (eap->skip)
1104 {
1105 if (i != FAIL)
1106 clear_var(&retvar);
1107 --emsg_skip;
1108 }
1109 else if (i != FAIL)
1110 {
1111 /*
1112 * ":let $VAR = expr": Set environment variable.
1113 */
1114 if (*arg == '$')
1115 {
1116 int len;
1117 int cc;
1118
1119 /* Find the end of the name. */
1120 ++arg;
1121 name = arg;
1122 len = get_env_len(&arg);
1123 if (len == 0)
1124 EMSG2(_(e_invarg2), name - 1);
1125 else
1126 {
1127 if (*skipwhite(arg) != '=')
1128 EMSG(_(e_letunexp));
1129 else
1130 {
1131 cc = name[len];
1132 name[len] = NUL;
1133 p = get_var_string(&retvar);
1134 vim_setenv(name, p);
1135 if (STRICMP(name, "HOME") == 0)
1136 init_homedir();
1137 else if (didset_vim && STRICMP(name, "VIM") == 0)
1138 didset_vim = FALSE;
1139 else if (didset_vimruntime
1140 && STRICMP(name, "VIMRUNTIME") == 0)
1141 didset_vimruntime = FALSE;
1142 name[len] = cc;
1143 }
1144 }
1145 }
1146
1147 /*
1148 * ":let &option = expr": Set option value.
1149 * ":let &l:option = expr": Set local option value.
1150 * ":let &g:option = expr": Set global option value.
1151 */
1152 else if (*arg == '&')
1153 {
1154 int opt_flags;
1155
1156 /*
1157 * Find the end of the name;
1158 */
1159 p = find_option_end(&arg, &opt_flags);
1160 if (p == NULL || *skipwhite(p) != '=')
1161 EMSG(_(e_letunexp));
1162 else
1163 {
1164 c1 = *p;
1165 *p = NUL;
1166 set_option_value(arg, get_var_number(&retvar),
1167 get_var_string(&retvar), opt_flags);
1168 *p = c1; /* put back for error messages */
1169 }
1170 }
1171
1172 /*
1173 * ":let @r = expr": Set register contents.
1174 */
1175 else if (*arg == '@')
1176 {
1177 ++arg;
1178 if (*skipwhite(arg + 1) != '=')
1179 EMSG(_(e_letunexp));
1180 else
1181 write_reg_contents(*arg == '@' ? '"' : *arg,
1182 get_var_string(&retvar), -1, FALSE);
1183 }
1184
1185 /*
1186 * ":let var = expr": Set internal variable.
1187 */
1188 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1189 {
1190 /* Find the end of the name. */
1191 p = find_name_end(arg, &expr_start, &expr_end);
1192
1193 if (*skipwhite(p) != '=')
1194 EMSG(_(e_letunexp));
1195 else if (p - arg == 13
1196 && STRNCMP(arg, "b:changedtick", 13) == 0)
1197 EMSG2(_(e_readonlyvar), arg);
1198#ifdef FEAT_MAGIC_BRACES
1199 else if (expr_start != NULL)
1200 {
1201 char_u *temp_string;
1202
1203 temp_string = make_expanded_name(arg, expr_start,
1204 expr_end, p);
1205 if (temp_string == NULL)
1206 {
1207 /*
1208 * Report an invalid expression in braces, unless the
1209 * expression evaluation has been cancelled due to an
1210 * aborting error, an interrupt, or an exception.
1211 */
1212 if (!aborting())
1213 EMSG2(_(e_invarg2), arg);
1214 }
1215 else
1216 {
1217 set_var(temp_string, &retvar);
1218 vim_free(temp_string);
1219 }
1220 }
1221#endif
1222 else
1223 {
1224 c1 = *p;
1225 *p = NUL;
1226 set_var(arg, &retvar);
1227 *p = c1; /* put char back for error messages */
1228 }
1229 }
1230
1231 else
1232 {
1233 EMSG2(_(e_invarg2), arg);
1234 }
1235
1236 clear_var(&retvar);
1237 }
1238 }
1239}
1240
1241#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1242
1243 void
1244set_context_for_expression(xp, arg, cmdidx)
1245 expand_T *xp;
1246 char_u *arg;
1247 cmdidx_T cmdidx;
1248{
1249 int got_eq = FALSE;
1250 int c;
1251
1252 xp->xp_context = cmdidx == CMD_let ? EXPAND_USER_VARS
1253 : cmdidx == CMD_call ? EXPAND_FUNCTIONS
1254 : EXPAND_EXPRESSION;
1255 while ((xp->xp_pattern = vim_strpbrk(arg,
1256 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1257 {
1258 c = *xp->xp_pattern;
1259 if (c == '&')
1260 {
1261 c = xp->xp_pattern[1];
1262 if (c == '&')
1263 {
1264 ++xp->xp_pattern;
1265 xp->xp_context = cmdidx != CMD_let || got_eq
1266 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1267 }
1268 else if (c != ' ')
1269 xp->xp_context = EXPAND_SETTINGS;
1270 }
1271 else if (c == '$')
1272 {
1273 /* environment variable */
1274 xp->xp_context = EXPAND_ENV_VARS;
1275 }
1276 else if (c == '=')
1277 {
1278 got_eq = TRUE;
1279 xp->xp_context = EXPAND_EXPRESSION;
1280 }
1281 else if (c == '<'
1282 && xp->xp_context == EXPAND_FUNCTIONS
1283 && vim_strchr(xp->xp_pattern, '(') == NULL)
1284 {
1285 /* Function name can start with "<SNR>" */
1286 break;
1287 }
1288 else if (cmdidx != CMD_let || got_eq)
1289 {
1290 if (c == '"') /* string */
1291 {
1292 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1293 if (c == '\\' && xp->xp_pattern[1] != NUL)
1294 ++xp->xp_pattern;
1295 xp->xp_context = EXPAND_NOTHING;
1296 }
1297 else if (c == '\'') /* literal string */
1298 {
1299 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1300 /* skip */ ;
1301 xp->xp_context = EXPAND_NOTHING;
1302 }
1303 else if (c == '|')
1304 {
1305 if (xp->xp_pattern[1] == '|')
1306 {
1307 ++xp->xp_pattern;
1308 xp->xp_context = EXPAND_EXPRESSION;
1309 }
1310 else
1311 xp->xp_context = EXPAND_COMMANDS;
1312 }
1313 else
1314 xp->xp_context = EXPAND_EXPRESSION;
1315 }
1316 else
1317 xp->xp_context = EXPAND_NOTHING;
1318 arg = xp->xp_pattern;
1319 if (*arg != NUL)
1320 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1321 /* skip */ ;
1322 }
1323 xp->xp_pattern = arg;
1324}
1325
1326#endif /* FEAT_CMDL_COMPL */
1327
1328/*
1329 * ":1,25call func(arg1, arg2)" function call.
1330 */
1331 void
1332ex_call(eap)
1333 exarg_T *eap;
1334{
1335 char_u *arg = eap->arg;
1336 char_u *startarg;
1337 char_u *alias;
1338 char_u *name;
1339 var retvar;
1340 int len;
1341 linenr_T lnum;
1342 int doesrange;
1343 int failed = FALSE;
1344
1345 name = arg;
1346 len = get_func_len(&arg, &alias, !eap->skip);
1347 if (len == 0)
1348 goto end;
1349 if (alias != NULL)
1350 name = alias;
1351
1352 startarg = arg;
1353 retvar.var_type = VAR_UNKNOWN; /* clear_var() uses this */
1354
1355 if (*startarg != '(')
1356 {
1357 EMSG2(_("E107: Missing braces: %s"), name);
1358 goto end;
1359 }
1360
1361 /*
1362 * When skipping, evaluate the function once, to find the end of the
1363 * arguments.
1364 * When the function takes a range, this is discovered after the first
1365 * call, and the loop is broken.
1366 */
1367 if (eap->skip)
1368 {
1369 ++emsg_skip;
1370 lnum = eap->line2; /* do it once, also with an invalid range */
1371 }
1372 else
1373 lnum = eap->line1;
1374 for ( ; lnum <= eap->line2; ++lnum)
1375 {
1376 if (!eap->skip && eap->addr_count > 0)
1377 {
1378 curwin->w_cursor.lnum = lnum;
1379 curwin->w_cursor.col = 0;
1380 }
1381 arg = startarg;
1382 if (get_func_var(name, len, &retvar, &arg,
1383 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
1384 {
1385 failed = TRUE;
1386 break;
1387 }
1388 clear_var(&retvar);
1389 if (doesrange || eap->skip)
1390 break;
1391 /* Stop when immediately aborting on error, or when an interrupt
1392 * occurred or an exception was thrown but not caught. get_func_var()
1393 * returned OK, so that the check for trailing characters below is
1394 * executed. */
1395 if (aborting())
1396 break;
1397 }
1398 if (eap->skip)
1399 --emsg_skip;
1400
1401 if (!failed)
1402 {
1403 /* Check for trailing illegal characters and a following command. */
1404 if (!ends_excmd(*arg))
1405 {
1406 emsg_severe = TRUE;
1407 EMSG(_(e_trailing));
1408 }
1409 else
1410 eap->nextcmd = check_nextcmd(arg);
1411 }
1412
1413end:
1414 if (alias != NULL)
1415 vim_free(alias);
1416}
1417
1418/*
1419 * ":unlet[!] var1 ... " command.
1420 */
1421 void
1422ex_unlet(eap)
1423 exarg_T *eap;
1424{
1425 char_u *arg = eap->arg;
1426 char_u *name_end;
1427 char_u cc;
1428 char_u *expr_start;
1429 char_u *expr_end;
1430 int error = FALSE;
1431
1432 do
1433 {
1434 /* Find the end of the name. */
1435 name_end = find_name_end(arg, &expr_start, &expr_end);
1436
1437 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1438 {
1439 emsg_severe = TRUE;
1440 EMSG(_(e_trailing));
1441 break;
1442 }
1443
1444 if (!error && !eap->skip)
1445 {
1446#ifdef FEAT_MAGIC_BRACES
1447 if (expr_start != NULL)
1448 {
1449 char_u *temp_string;
1450
1451 temp_string = make_expanded_name(arg, expr_start,
1452 expr_end, name_end);
1453 if (temp_string == NULL)
1454 {
1455 /*
1456 * Report an invalid expression in braces, unless the
1457 * expression evaluation has been cancelled due to an
1458 * aborting error, an interrupt, or an exception.
1459 */
1460 if (!aborting())
1461 {
1462 emsg_severe = TRUE;
1463 EMSG2(_(e_invarg2), arg);
1464 break;
1465 }
1466 error = TRUE;
1467 }
1468 else
1469 {
1470 if (do_unlet(temp_string) == FAIL && !eap->forceit)
1471 {
1472 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
1473 error = TRUE;
1474 }
1475 vim_free(temp_string);
1476 }
1477 }
1478 else
1479#endif
1480 {
1481 cc = *name_end;
1482 *name_end = NUL;
1483
1484 if (do_unlet(arg) == FAIL && !eap->forceit)
1485 {
1486 EMSG2(_("E108: No such variable: \"%s\""), arg);
1487 error = TRUE;
1488 }
1489
1490 *name_end = cc;
1491 }
1492 }
1493 arg = skipwhite(name_end);
1494 } while (!ends_excmd(*arg));
1495
1496 eap->nextcmd = check_nextcmd(arg);
1497}
1498
1499/*
1500 * "unlet" a variable. Return OK if it existed, FAIL if not.
1501 */
1502 int
1503do_unlet(name)
1504 char_u *name;
1505{
1506 VAR v;
1507
1508 v = find_var(name, TRUE);
1509 if (v != NULL)
1510 {
1511 var_free_one(v);
1512 return OK;
1513 }
1514 return FAIL;
1515}
1516
1517#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1518/*
1519 * Delete all "menutrans_" variables.
1520 */
1521 void
1522del_menutrans_vars()
1523{
1524 int i;
1525
1526 for (i = 0; i < variables.ga_len; ++i)
1527 if (VAR_ENTRY(i).var_name != NULL
1528 && STRNCMP(VAR_ENTRY(i).var_name, "menutrans_", 10) == 0)
1529 var_free_one(&VAR_ENTRY(i));
1530}
1531#endif
1532
1533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1534
1535/*
1536 * Local string buffer for the next two functions to store a variable name
1537 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1538 * get_user_var_name().
1539 */
1540
1541static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
1542
1543static char_u *varnamebuf = NULL;
1544static int varnamebuflen = 0;
1545
1546/*
1547 * Function to concatenate a prefix and a variable name.
1548 */
1549 static char_u *
1550cat_prefix_varname(prefix, name)
1551 int prefix;
1552 char_u *name;
1553{
1554 int len;
1555
1556 len = (int)STRLEN(name) + 3;
1557 if (len > varnamebuflen)
1558 {
1559 vim_free(varnamebuf);
1560 len += 10; /* some additional space */
1561 varnamebuf = alloc(len);
1562 if (varnamebuf == NULL)
1563 {
1564 varnamebuflen = 0;
1565 return NULL;
1566 }
1567 varnamebuflen = len;
1568 }
1569 *varnamebuf = prefix;
1570 varnamebuf[1] = ':';
1571 STRCPY(varnamebuf + 2, name);
1572 return varnamebuf;
1573}
1574
1575/*
1576 * Function given to ExpandGeneric() to obtain the list of user defined
1577 * (global/buffer/window/built-in) variable names.
1578 */
1579/*ARGSUSED*/
1580 char_u *
1581get_user_var_name(xp, idx)
1582 expand_T *xp;
1583 int idx;
1584{
1585 static int gidx;
1586 static int bidx;
1587 static int widx;
1588 static int vidx;
1589 char_u *name;
1590
1591 if (idx == 0)
1592 gidx = bidx = widx = vidx = 0;
1593 if (gidx < variables.ga_len) /* Global variables */
1594 {
1595 while ((name = VAR_ENTRY(gidx++).var_name) == NULL
1596 && gidx < variables.ga_len)
1597 /* skip */;
1598 if (name != NULL)
1599 {
1600 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1601 return cat_prefix_varname('g', name);
1602 else
1603 return name;
1604 }
1605 }
1606 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
1607 {
1608 while ((name = BVAR_ENTRY(bidx++).var_name) == NULL
1609 && bidx < curbuf->b_vars.ga_len)
1610 /* skip */;
1611 if (name != NULL)
1612 return cat_prefix_varname('b', name);
1613 }
1614 if (bidx == curbuf->b_vars.ga_len)
1615 {
1616 ++bidx;
1617 return (char_u *)"b:changedtick";
1618 }
1619 if (widx < curwin->w_vars.ga_len) /* Current window variables */
1620 {
1621 while ((name = WVAR_ENTRY(widx++).var_name) == NULL
1622 && widx < curwin->w_vars.ga_len)
1623 /* skip */;
1624 if (name != NULL)
1625 return cat_prefix_varname('w', name);
1626 }
1627 if (vidx < VV_LEN) /* Built-in variables */
1628 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
1629
1630 vim_free(varnamebuf);
1631 varnamebuf = NULL;
1632 varnamebuflen = 0;
1633 return NULL;
1634}
1635
1636#endif /* FEAT_CMDL_COMPL */
1637
1638/*
1639 * types for expressions.
1640 */
1641typedef enum
1642{
1643 TYPE_UNKNOWN = 0
1644 , TYPE_EQUAL /* == */
1645 , TYPE_NEQUAL /* != */
1646 , TYPE_GREATER /* > */
1647 , TYPE_GEQUAL /* >= */
1648 , TYPE_SMALLER /* < */
1649 , TYPE_SEQUAL /* <= */
1650 , TYPE_MATCH /* =~ */
1651 , TYPE_NOMATCH /* !~ */
1652} exptype_T;
1653
1654/*
1655 * The "evaluate" argument: When FALSE, the argument is only parsed but not
1656 * executed. The function may return OK, but the retvar will be of type
1657 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1658 */
1659
1660/*
1661 * Handle zero level expression.
1662 * This calls eval1() and handles error message and nextcmd.
1663 * Return OK or FAIL.
1664 */
1665 static int
1666eval0(arg, retvar, nextcmd, evaluate)
1667 char_u *arg;
1668 VAR retvar;
1669 char_u **nextcmd;
1670 int evaluate;
1671{
1672 int ret;
1673 char_u *p;
1674
1675 p = skipwhite(arg);
1676 ret = eval1(&p, retvar, evaluate);
1677 if (ret == FAIL || !ends_excmd(*p))
1678 {
1679 if (ret != FAIL)
1680 clear_var(retvar);
1681 /*
1682 * Report the invalid expression unless the expression evaluation has
1683 * been cancelled due to an aborting error, an interrupt, or an
1684 * exception.
1685 */
1686 if (!aborting())
1687 EMSG2(_(e_invexpr2), arg);
1688 ret = FAIL;
1689 }
1690 if (nextcmd != NULL)
1691 *nextcmd = check_nextcmd(p);
1692
1693 return ret;
1694}
1695
1696/*
1697 * Handle top level expression:
1698 * expr1 ? expr0 : expr0
1699 *
1700 * "arg" must point to the first non-white of the expression.
1701 * "arg" is advanced to the next non-white after the recognized expression.
1702 *
1703 * Return OK or FAIL.
1704 */
1705 static int
1706eval1(arg, retvar, evaluate)
1707 char_u **arg;
1708 VAR retvar;
1709 int evaluate;
1710{
1711 int result;
1712 var var2;
1713
1714 /*
1715 * Get the first variable.
1716 */
1717 if (eval2(arg, retvar, evaluate) == FAIL)
1718 return FAIL;
1719
1720 if ((*arg)[0] == '?')
1721 {
1722 result = FALSE;
1723 if (evaluate)
1724 {
1725 if (get_var_number(retvar) != 0)
1726 result = TRUE;
1727 clear_var(retvar);
1728 }
1729
1730 /*
1731 * Get the second variable.
1732 */
1733 *arg = skipwhite(*arg + 1);
1734 if (eval1(arg, retvar, evaluate && result) == FAIL) /* recursive! */
1735 return FAIL;
1736
1737 /*
1738 * Check for the ":".
1739 */
1740 if ((*arg)[0] != ':')
1741 {
1742 EMSG(_("E109: Missing ':' after '?'"));
1743 if (evaluate && result)
1744 clear_var(retvar);
1745 return FAIL;
1746 }
1747
1748 /*
1749 * Get the third variable.
1750 */
1751 *arg = skipwhite(*arg + 1);
1752 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
1753 {
1754 if (evaluate && result)
1755 clear_var(retvar);
1756 return FAIL;
1757 }
1758 if (evaluate && !result)
1759 *retvar = var2;
1760 }
1761
1762 return OK;
1763}
1764
1765/*
1766 * Handle first level expression:
1767 * expr2 || expr2 || expr2 logical OR
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
1775eval2(arg, retvar, evaluate)
1776 char_u **arg;
1777 VAR retvar;
1778 int evaluate;
1779{
1780 var var2;
1781 long result;
1782 int first;
1783
1784 /*
1785 * Get the first variable.
1786 */
1787 if (eval3(arg, retvar, evaluate) == FAIL)
1788 return FAIL;
1789
1790 /*
1791 * Repeat until there is no following "||".
1792 */
1793 first = TRUE;
1794 result = FALSE;
1795 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1796 {
1797 if (evaluate && first)
1798 {
1799 if (get_var_number(retvar) != 0)
1800 result = TRUE;
1801 clear_var(retvar);
1802 first = FALSE;
1803 }
1804
1805 /*
1806 * Get the second variable.
1807 */
1808 *arg = skipwhite(*arg + 2);
1809 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1810 return FAIL;
1811
1812 /*
1813 * Compute the result.
1814 */
1815 if (evaluate && !result)
1816 {
1817 if (get_var_number(&var2) != 0)
1818 result = TRUE;
1819 clear_var(&var2);
1820 }
1821 if (evaluate)
1822 {
1823 retvar->var_type = VAR_NUMBER;
1824 retvar->var_val.var_number = result;
1825 }
1826 }
1827
1828 return OK;
1829}
1830
1831/*
1832 * Handle second level expression:
1833 * expr3 && expr3 && expr3 logical AND
1834 *
1835 * "arg" must point to the first non-white of the expression.
1836 * "arg" is advanced to the next non-white after the recognized expression.
1837 *
1838 * Return OK or FAIL.
1839 */
1840 static int
1841eval3(arg, retvar, evaluate)
1842 char_u **arg;
1843 VAR retvar;
1844 int evaluate;
1845{
1846 var var2;
1847 long result;
1848 int first;
1849
1850 /*
1851 * Get the first variable.
1852 */
1853 if (eval4(arg, retvar, evaluate) == FAIL)
1854 return FAIL;
1855
1856 /*
1857 * Repeat until there is no following "&&".
1858 */
1859 first = TRUE;
1860 result = TRUE;
1861 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1862 {
1863 if (evaluate && first)
1864 {
1865 if (get_var_number(retvar) == 0)
1866 result = FALSE;
1867 clear_var(retvar);
1868 first = FALSE;
1869 }
1870
1871 /*
1872 * Get the second variable.
1873 */
1874 *arg = skipwhite(*arg + 2);
1875 if (eval4(arg, &var2, evaluate && result) == FAIL)
1876 return FAIL;
1877
1878 /*
1879 * Compute the result.
1880 */
1881 if (evaluate && result)
1882 {
1883 if (get_var_number(&var2) == 0)
1884 result = FALSE;
1885 clear_var(&var2);
1886 }
1887 if (evaluate)
1888 {
1889 retvar->var_type = VAR_NUMBER;
1890 retvar->var_val.var_number = result;
1891 }
1892 }
1893
1894 return OK;
1895}
1896
1897/*
1898 * Handle third level expression:
1899 * var1 == var2
1900 * var1 =~ var2
1901 * var1 != var2
1902 * var1 !~ var2
1903 * var1 > var2
1904 * var1 >= var2
1905 * var1 < var2
1906 * var1 <= var2
1907 *
1908 * "arg" must point to the first non-white of the expression.
1909 * "arg" is advanced to the next non-white after the recognized expression.
1910 *
1911 * Return OK or FAIL.
1912 */
1913 static int
1914eval4(arg, retvar, evaluate)
1915 char_u **arg;
1916 VAR retvar;
1917 int evaluate;
1918{
1919 var var2;
1920 char_u *p;
1921 int i;
1922 exptype_T type = TYPE_UNKNOWN;
1923 int len = 2;
1924 long n1, n2;
1925 char_u *s1, *s2;
1926 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1927 regmatch_T regmatch;
1928 int ic;
1929 char_u *save_cpo;
1930
1931 /*
1932 * Get the first variable.
1933 */
1934 if (eval5(arg, retvar, evaluate) == FAIL)
1935 return FAIL;
1936
1937 p = *arg;
1938 switch (p[0])
1939 {
1940 case '=': if (p[1] == '=')
1941 type = TYPE_EQUAL;
1942 else if (p[1] == '~')
1943 type = TYPE_MATCH;
1944 break;
1945 case '!': if (p[1] == '=')
1946 type = TYPE_NEQUAL;
1947 else if (p[1] == '~')
1948 type = TYPE_NOMATCH;
1949 break;
1950 case '>': if (p[1] != '=')
1951 {
1952 type = TYPE_GREATER;
1953 len = 1;
1954 }
1955 else
1956 type = TYPE_GEQUAL;
1957 break;
1958 case '<': if (p[1] != '=')
1959 {
1960 type = TYPE_SMALLER;
1961 len = 1;
1962 }
1963 else
1964 type = TYPE_SEQUAL;
1965 break;
1966 }
1967
1968 /*
1969 * If there is a comparitive operator, use it.
1970 */
1971 if (type != TYPE_UNKNOWN)
1972 {
1973 /* extra question mark appended: ignore case */
1974 if (p[len] == '?')
1975 {
1976 ic = TRUE;
1977 ++len;
1978 }
1979 /* extra '#' appended: match case */
1980 else if (p[len] == '#')
1981 {
1982 ic = FALSE;
1983 ++len;
1984 }
1985 /* nothing appened: use 'ignorecase' */
1986 else
1987 ic = p_ic;
1988
1989 /*
1990 * Get the second variable.
1991 */
1992 *arg = skipwhite(p + len);
1993 if (eval5(arg, &var2, evaluate) == FAIL)
1994 {
1995 clear_var(retvar);
1996 return FAIL;
1997 }
1998
1999 if (evaluate)
2000 {
2001 /*
2002 * If one of the two variables is a number, compare as a number.
2003 * When using "=~" or "!~", always compare as string.
2004 */
2005 if ((retvar->var_type == VAR_NUMBER || var2.var_type == VAR_NUMBER)
2006 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2007 {
2008 n1 = get_var_number(retvar);
2009 n2 = get_var_number(&var2);
2010 switch (type)
2011 {
2012 case TYPE_EQUAL: n1 = (n1 == n2); break;
2013 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2014 case TYPE_GREATER: n1 = (n1 > n2); break;
2015 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2016 case TYPE_SMALLER: n1 = (n1 < n2); break;
2017 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2018 case TYPE_UNKNOWN:
2019 case TYPE_MATCH:
2020 case TYPE_NOMATCH: break; /* avoid gcc warning */
2021 }
2022 }
2023 else
2024 {
2025 s1 = get_var_string_buf(retvar, buf1);
2026 s2 = get_var_string_buf(&var2, buf2);
2027 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2028 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2029 else
2030 i = 0;
2031 n1 = FALSE;
2032 switch (type)
2033 {
2034 case TYPE_EQUAL: n1 = (i == 0); break;
2035 case TYPE_NEQUAL: n1 = (i != 0); break;
2036 case TYPE_GREATER: n1 = (i > 0); break;
2037 case TYPE_GEQUAL: n1 = (i >= 0); break;
2038 case TYPE_SMALLER: n1 = (i < 0); break;
2039 case TYPE_SEQUAL: n1 = (i <= 0); break;
2040
2041 case TYPE_MATCH:
2042 case TYPE_NOMATCH:
2043 /* avoid 'l' flag in 'cpoptions' */
2044 save_cpo = p_cpo;
2045 p_cpo = (char_u *)"";
2046 regmatch.regprog = vim_regcomp(s2,
2047 RE_MAGIC + RE_STRING);
2048 regmatch.rm_ic = ic;
2049 if (regmatch.regprog != NULL)
2050 {
2051 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2052 vim_free(regmatch.regprog);
2053 if (type == TYPE_NOMATCH)
2054 n1 = !n1;
2055 }
2056 p_cpo = save_cpo;
2057 break;
2058
2059 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2060 }
2061 }
2062 clear_var(retvar);
2063 clear_var(&var2);
2064 retvar->var_type = VAR_NUMBER;
2065 retvar->var_val.var_number = n1;
2066 }
2067 }
2068
2069 return OK;
2070}
2071
2072/*
2073 * Handle fourth level expression:
2074 * + number addition
2075 * - number subtraction
2076 * . string concatenation
2077 *
2078 * "arg" must point to the first non-white of the expression.
2079 * "arg" is advanced to the next non-white after the recognized expression.
2080 *
2081 * Return OK or FAIL.
2082 */
2083 static int
2084eval5(arg, retvar, evaluate)
2085 char_u **arg;
2086 VAR retvar;
2087 int evaluate;
2088{
2089 var var2;
2090 int op;
2091 long n1, n2;
2092 char_u *s1, *s2;
2093 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2094 char_u *p;
2095
2096 /*
2097 * Get the first variable.
2098 */
2099 if (eval6(arg, retvar, evaluate) == FAIL)
2100 return FAIL;
2101
2102 /*
2103 * Repeat computing, until no '+', '-' or '.' is following.
2104 */
2105 for (;;)
2106 {
2107 op = **arg;
2108 if (op != '+' && op != '-' && op != '.')
2109 break;
2110
2111 /*
2112 * Get the second variable.
2113 */
2114 *arg = skipwhite(*arg + 1);
2115 if (eval6(arg, &var2, evaluate) == FAIL)
2116 {
2117 clear_var(retvar);
2118 return FAIL;
2119 }
2120
2121 if (evaluate)
2122 {
2123 /*
2124 * Compute the result.
2125 */
2126 if (op == '.')
2127 {
2128 s1 = get_var_string_buf(retvar, buf1);
2129 s2 = get_var_string_buf(&var2, buf2);
2130 op = (int)STRLEN(s1);
2131 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2132 if (p != NULL)
2133 {
2134 STRCPY(p, s1);
2135 STRCPY(p + op, s2);
2136 }
2137 clear_var(retvar);
2138 retvar->var_type = VAR_STRING;
2139 retvar->var_val.var_string = p;
2140 }
2141 else
2142 {
2143 n1 = get_var_number(retvar);
2144 n2 = get_var_number(&var2);
2145 clear_var(retvar);
2146 if (op == '+')
2147 n1 = n1 + n2;
2148 else
2149 n1 = n1 - n2;
2150 retvar->var_type = VAR_NUMBER;
2151 retvar->var_val.var_number = n1;
2152 }
2153 clear_var(&var2);
2154 }
2155 }
2156 return OK;
2157}
2158
2159/*
2160 * Handle fifth level expression:
2161 * * number multiplication
2162 * / number division
2163 * % number modulo
2164 *
2165 * "arg" must point to the first non-white of the expression.
2166 * "arg" is advanced to the next non-white after the recognized expression.
2167 *
2168 * Return OK or FAIL.
2169 */
2170 static int
2171eval6(arg, retvar, evaluate)
2172 char_u **arg;
2173 VAR retvar;
2174 int evaluate;
2175{
2176 var var2;
2177 int op;
2178 long n1, n2;
2179
2180 /*
2181 * Get the first variable.
2182 */
2183 if (eval7(arg, retvar, evaluate) == FAIL)
2184 return FAIL;
2185
2186 /*
2187 * Repeat computing, until no '*', '/' or '%' is following.
2188 */
2189 for (;;)
2190 {
2191 op = **arg;
2192 if (op != '*' && op != '/' && op != '%')
2193 break;
2194
2195 if (evaluate)
2196 {
2197 n1 = get_var_number(retvar);
2198 clear_var(retvar);
2199 }
2200 else
2201 n1 = 0;
2202
2203 /*
2204 * Get the second variable.
2205 */
2206 *arg = skipwhite(*arg + 1);
2207 if (eval7(arg, &var2, evaluate) == FAIL)
2208 return FAIL;
2209
2210 if (evaluate)
2211 {
2212 n2 = get_var_number(&var2);
2213 clear_var(&var2);
2214
2215 /*
2216 * Compute the result.
2217 */
2218 if (op == '*')
2219 n1 = n1 * n2;
2220 else if (op == '/')
2221 {
2222 if (n2 == 0) /* give an error message? */
2223 n1 = 0x7fffffffL;
2224 else
2225 n1 = n1 / n2;
2226 }
2227 else
2228 {
2229 if (n2 == 0) /* give an error message? */
2230 n1 = 0;
2231 else
2232 n1 = n1 % n2;
2233 }
2234 retvar->var_type = VAR_NUMBER;
2235 retvar->var_val.var_number = n1;
2236 }
2237 }
2238
2239 return OK;
2240}
2241
2242/*
2243 * Handle sixth level expression:
2244 * number number constant
2245 * "string" string contstant
2246 * 'string' literal string contstant
2247 * &option-name option value
2248 * @r register contents
2249 * identifier variable value
2250 * function() function call
2251 * $VAR environment variable
2252 * (expression) nested expression
2253 *
2254 * Also handle:
2255 * ! in front logical NOT
2256 * - in front unary minus
2257 * + in front unary plus (ignored)
2258 * trailing [] subscript in String
2259 *
2260 * "arg" must point to the first non-white of the expression.
2261 * "arg" is advanced to the next non-white after the recognized expression.
2262 *
2263 * Return OK or FAIL.
2264 */
2265 static int
2266eval7(arg, retvar, evaluate)
2267 char_u **arg;
2268 VAR retvar;
2269 int evaluate;
2270{
2271 var var2;
2272 long n;
2273 int len;
2274 char_u *s;
2275 int val;
2276 char_u *start_leader, *end_leader;
2277 int ret = OK;
2278 char_u *alias;
2279
2280 /*
2281 * Initialise variable so that clear_var() can't mistake this for a string
2282 * and free a string that isn't there.
2283 */
2284 retvar->var_type = VAR_UNKNOWN;
2285
2286 /*
2287 * Skip '!' and '-' characters. They are handled later.
2288 */
2289 start_leader = *arg;
2290 while (**arg == '!' || **arg == '-' || **arg == '+')
2291 *arg = skipwhite(*arg + 1);
2292 end_leader = *arg;
2293
2294 switch (**arg)
2295 {
2296 /*
2297 * Number constant.
2298 */
2299 case '0':
2300 case '1':
2301 case '2':
2302 case '3':
2303 case '4':
2304 case '5':
2305 case '6':
2306 case '7':
2307 case '8':
2308 case '9':
2309 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
2310 *arg += len;
2311 if (evaluate)
2312 {
2313 retvar->var_type = VAR_NUMBER;
2314 retvar->var_val.var_number = n;
2315 }
2316 break;
2317
2318 /*
2319 * String constant: "string".
2320 */
2321 case '"': ret = get_string_var(arg, retvar, evaluate);
2322 break;
2323
2324 /*
2325 * Literal string constant: 'string'.
2326 */
2327 case '\'': ret = get_lit_string_var(arg, retvar, evaluate);
2328 break;
2329
2330 /*
2331 * Option value: &name
2332 */
2333 case '&': ret = get_option_var(arg, retvar, evaluate);
2334 break;
2335
2336 /*
2337 * Environment variable: $VAR.
2338 */
2339 case '$': ret = get_env_var(arg, retvar, evaluate);
2340 break;
2341
2342 /*
2343 * Register contents: @r.
2344 */
2345 case '@': ++*arg;
2346 if (evaluate)
2347 {
2348 retvar->var_type = VAR_STRING;
2349 retvar->var_val.var_string = get_reg_contents(**arg, FALSE);
2350 }
2351 if (**arg != NUL)
2352 ++*arg;
2353 break;
2354
2355 /*
2356 * nested expression: (expression).
2357 */
2358 case '(': *arg = skipwhite(*arg + 1);
2359 ret = eval1(arg, retvar, evaluate); /* recursive! */
2360 if (**arg == ')')
2361 ++*arg;
2362 else if (ret == OK)
2363 {
2364 EMSG(_("E110: Missing ')'"));
2365 clear_var(retvar);
2366 ret = FAIL;
2367 }
2368 break;
2369
2370 /*
2371 * Must be a variable or function name then.
2372 */
2373 default: s = *arg;
2374 len = get_func_len(arg, &alias, evaluate);
2375 if (alias != NULL)
2376 s = alias;
2377
2378 if (len == 0)
2379 ret = FAIL;
2380 else
2381 {
2382 if (**arg == '(') /* recursive! */
2383 {
2384 ret = get_func_var(s, len, retvar, arg,
2385 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
2386 &len, evaluate);
2387 /* Stop the expression evaluation when immediately
2388 * aborting on error, or when an interrupt occurred or
2389 * an exception was thrown but not caught. */
2390 if (aborting())
2391 {
2392 if (ret == OK)
2393 clear_var(retvar);
2394 ret = FAIL;
2395 }
2396 }
2397 else if (evaluate)
2398 ret = get_var_var(s, len, retvar);
2399 }
2400
2401 if (alias != NULL)
2402 vim_free(alias);
2403
2404 break;
2405 }
2406 *arg = skipwhite(*arg);
2407
2408 /*
2409 * Handle expr[expr] subscript.
2410 */
2411 if (**arg == '[' && ret == OK)
2412 {
2413 /*
2414 * Get the variable from inside the [].
2415 */
2416 *arg = skipwhite(*arg + 1);
2417 if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
2418 {
2419 clear_var(retvar);
2420 return FAIL;
2421 }
2422
2423 /* Check for the ']'. */
2424 if (**arg != ']')
2425 {
2426 EMSG(_("E111: Missing ']'"));
2427 clear_var(retvar);
2428 clear_var(&var2);
2429 return FAIL;
2430 }
2431
2432 if (evaluate)
2433 {
2434 n = get_var_number(&var2);
2435 clear_var(&var2);
2436
2437 /*
2438 * The resulting variable is a string of a single character.
2439 * If the index is too big or negative, the result is empty.
2440 */
2441 s = get_var_string(retvar);
2442 if (n >= (long)STRLEN(s) || n < 0)
2443 s = NULL;
2444 else
2445 s = vim_strnsave(s + n, 1);
2446 clear_var(retvar);
2447 retvar->var_type = VAR_STRING;
2448 retvar->var_val.var_string = s;
2449 }
2450 *arg = skipwhite(*arg + 1); /* skip the ']' */
2451 }
2452
2453 /*
2454 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2455 */
2456 if (ret == OK && evaluate && end_leader > start_leader)
2457 {
2458 val = get_var_number(retvar);
2459 while (end_leader > start_leader)
2460 {
2461 --end_leader;
2462 if (*end_leader == '!')
2463 val = !val;
2464 else if (*end_leader == '-')
2465 val = -val;
2466 }
2467 clear_var(retvar);
2468 retvar->var_type = VAR_NUMBER;
2469 retvar->var_val.var_number = val;
2470 }
2471
2472 return ret;
2473}
2474
2475/*
2476 * Get an option value.
2477 * "arg" points to the '&' or '+' before the option name.
2478 * "arg" is advanced to character after the option name.
2479 * Return OK or FAIL.
2480 */
2481 static int
2482get_option_var(arg, retvar, evaluate)
2483 char_u **arg;
2484 VAR retvar; /* when NULL, only check if option exists */
2485 int evaluate;
2486{
2487 char_u *option_end;
2488 long numval;
2489 char_u *stringval;
2490 int opt_type;
2491 int c;
2492 int working = (**arg == '+'); /* has("+option") */
2493 int ret = OK;
2494 int opt_flags;
2495
2496 /*
2497 * Isolate the option name and find its value.
2498 */
2499 option_end = find_option_end(arg, &opt_flags);
2500 if (option_end == NULL)
2501 {
2502 if (retvar != NULL)
2503 EMSG2(_("E112: Option name missing: %s"), *arg);
2504 return FAIL;
2505 }
2506
2507 if (!evaluate)
2508 {
2509 *arg = option_end;
2510 return OK;
2511 }
2512
2513 c = *option_end;
2514 *option_end = NUL;
2515 opt_type = get_option_value(*arg, &numval,
2516 retvar == NULL ? NULL : &stringval, opt_flags);
2517
2518 if (opt_type == -3) /* invalid name */
2519 {
2520 if (retvar != NULL)
2521 EMSG2(_("E113: Unknown option: %s"), *arg);
2522 ret = FAIL;
2523 }
2524 else if (retvar != NULL)
2525 {
2526 if (opt_type == -2) /* hidden string option */
2527 {
2528 retvar->var_type = VAR_STRING;
2529 retvar->var_val.var_string = NULL;
2530 }
2531 else if (opt_type == -1) /* hidden number option */
2532 {
2533 retvar->var_type = VAR_NUMBER;
2534 retvar->var_val.var_number = 0;
2535 }
2536 else if (opt_type == 1) /* number option */
2537 {
2538 retvar->var_type = VAR_NUMBER;
2539 retvar->var_val.var_number = numval;
2540 }
2541 else /* string option */
2542 {
2543 retvar->var_type = VAR_STRING;
2544 retvar->var_val.var_string = stringval;
2545 }
2546 }
2547 else if (working && (opt_type == -2 || opt_type == -1))
2548 ret = FAIL;
2549
2550 *option_end = c; /* put back for error messages */
2551 *arg = option_end;
2552
2553 return ret;
2554}
2555
2556/*
2557 * Allocate a variable for a string constant.
2558 * Return OK or FAIL.
2559 */
2560 static int
2561get_string_var(arg, retvar, evaluate)
2562 char_u **arg;
2563 VAR retvar;
2564 int evaluate;
2565{
2566 char_u *p;
2567 char_u *name;
2568 int i;
2569 int extra = 0;
2570
2571 /*
2572 * Find the end of the string, skipping backslashed characters.
2573 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002574 for (p = *arg + 1; *p && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 if (*p == '\\' && p[1] != NUL)
2577 {
2578 ++p;
2579 /* A "\<x>" form occupies at least 4 characters, and produces up
2580 * to 6 characters: reserve space for 2 extra */
2581 if (*p == '<')
2582 extra += 2;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586 if (*p != '"')
2587 {
2588 EMSG2(_("E114: Missing quote: %s"), *arg);
2589 return FAIL;
2590 }
2591
2592 /* If only parsing, set *arg and return here */
2593 if (!evaluate)
2594 {
2595 *arg = p + 1;
2596 return OK;
2597 }
2598
2599 /*
2600 * Copy the string into allocated memory, handling backslashed
2601 * characters.
2602 */
2603 name = alloc((unsigned)(p - *arg + extra));
2604 if (name == NULL)
2605 return FAIL;
2606
2607 i = 0;
2608 for (p = *arg + 1; *p && *p != '"'; ++p)
2609 {
2610 if (*p == '\\')
2611 {
2612 switch (*++p)
2613 {
2614 case 'b': name[i++] = BS; break;
2615 case 'e': name[i++] = ESC; break;
2616 case 'f': name[i++] = FF; break;
2617 case 'n': name[i++] = NL; break;
2618 case 'r': name[i++] = CAR; break;
2619 case 't': name[i++] = TAB; break;
2620
2621 case 'X': /* hex: "\x1", "\x12" */
2622 case 'x':
2623 case 'u': /* Unicode: "\u0023" */
2624 case 'U':
2625 if (vim_isxdigit(p[1]))
2626 {
2627 int n, nr;
2628 int c = toupper(*p);
2629
2630 if (c == 'X')
2631 n = 2;
2632 else
2633 n = 4;
2634 nr = 0;
2635 while (--n >= 0 && vim_isxdigit(p[1]))
2636 {
2637 ++p;
2638 nr = (nr << 4) + hex2nr(*p);
2639 }
2640#ifdef FEAT_MBYTE
2641 /* For "\u" store the number according to
2642 * 'encoding'. */
2643 if (c != 'X')
2644 i += (*mb_char2bytes)(nr, name + i);
2645 else
2646#endif
2647 name[i++] = nr;
2648 }
2649 else
2650 name[i++] = *p;
2651 break;
2652
2653 /* octal: "\1", "\12", "\123" */
2654 case '0':
2655 case '1':
2656 case '2':
2657 case '3':
2658 case '4':
2659 case '5':
2660 case '6':
2661 case '7': name[i] = *p - '0';
2662 if (p[1] >= '0' && p[1] <= '7')
2663 {
2664 ++p;
2665 name[i] = (name[i] << 3) + *p - '0';
2666 if (p[1] >= '0' && p[1] <= '7')
2667 {
2668 ++p;
2669 name[i] = (name[i] << 3) + *p - '0';
2670 }
2671 }
2672 ++i;
2673 break;
2674
2675 /* Special key, e.g.: "\<C-W>" */
2676 case '<': extra = trans_special(&p, name + i, TRUE);
2677 if (extra != 0)
2678 {
2679 i += extra;
2680 --p;
2681 break;
2682 }
2683 /* FALLTHROUGH */
2684
2685 default: name[i++] = *p;
2686 break;
2687 }
2688 }
2689 else
2690 name[i++] = *p;
2691
2692#ifdef FEAT_MBYTE
2693 /* For a multi-byte character copy the bytes after the first one. */
2694 if (has_mbyte)
2695 {
2696 int l = (*mb_ptr2len_check)(p);
2697
2698 while (--l > 0)
2699 name[i++] = *++p;
2700 }
2701#endif
2702 }
2703 name[i] = NUL;
2704 *arg = p + 1;
2705
2706 retvar->var_type = VAR_STRING;
2707 retvar->var_val.var_string = name;
2708
2709 return OK;
2710}
2711
2712/*
2713 * Allocate a variable for an backtick-string constant.
2714 * Return OK or FAIL.
2715 */
2716 static int
2717get_lit_string_var(arg, retvar, evaluate)
2718 char_u **arg;
2719 VAR retvar;
2720 int evaluate;
2721{
2722 char_u *p;
2723 char_u *name;
2724
2725 /*
2726 * Find the end of the string.
2727 */
2728 p = vim_strchr(*arg + 1, '\'');
2729 if (p == NULL)
2730 {
2731 EMSG2(_("E115: Missing quote: %s"), *arg);
2732 return FAIL;
2733 }
2734
2735 if (evaluate)
2736 {
2737 /*
2738 * Copy the string into allocated memory.
2739 */
2740 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
2741 if (name == NULL)
2742 return FAIL;
2743
2744 retvar->var_type = VAR_STRING;
2745 retvar->var_val.var_string = name;
2746 }
2747
2748 *arg = p + 1;
2749
2750 return OK;
2751}
2752
2753/*
2754 * Get the value of an environment variable.
2755 * "arg" is pointing to the '$'. It is advanced to after the name.
2756 * If the environment variable was not set, silently assume it is empty.
2757 * Always return OK.
2758 */
2759 static int
2760get_env_var(arg, retvar, evaluate)
2761 char_u **arg;
2762 VAR retvar;
2763 int evaluate;
2764{
2765 char_u *string = NULL;
2766 int len;
2767 int cc;
2768 char_u *name;
2769
2770 ++*arg;
2771 name = *arg;
2772 len = get_env_len(arg);
2773 if (evaluate)
2774 {
2775 if (len != 0)
2776 {
2777 cc = name[len];
2778 name[len] = NUL;
2779 /* first try mch_getenv(), fast for normal environment vars */
2780 string = mch_getenv(name);
2781 if (string != NULL && *string != NUL)
2782 string = vim_strsave(string);
2783 else
2784 {
2785 /* next try expanding things like $VIM and ${HOME} */
2786 string = expand_env_save(name - 1);
2787 if (string != NULL && *string == '$')
2788 {
2789 vim_free(string);
2790 string = NULL;
2791 }
2792 }
2793 name[len] = cc;
2794 }
2795 retvar->var_type = VAR_STRING;
2796 retvar->var_val.var_string = string;
2797 }
2798
2799 return OK;
2800}
2801
2802/*
2803 * Array with names and number of arguments of all internal functions
2804 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
2805 */
2806static struct fst
2807{
2808 char *f_name; /* function name */
2809 char f_min_argc; /* minimal number of arguments */
2810 char f_max_argc; /* maximal number of arguments */
2811 void (*f_func) __ARGS((VAR args, VAR rvar)); /* impl. function */
2812} functions[] =
2813{
2814 {"append", 2, 2, f_append},
2815 {"argc", 0, 0, f_argc},
2816 {"argidx", 0, 0, f_argidx},
2817 {"argv", 1, 1, f_argv},
2818 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002819 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {"bufexists", 1, 1, f_bufexists},
2821 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
2822 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
2823 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
2824 {"buflisted", 1, 1, f_buflisted},
2825 {"bufloaded", 1, 1, f_bufloaded},
2826 {"bufname", 1, 1, f_bufname},
2827 {"bufnr", 1, 1, f_bufnr},
2828 {"bufwinnr", 1, 1, f_bufwinnr},
2829 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00002830 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {"char2nr", 1, 1, f_char2nr},
2832 {"cindent", 1, 1, f_cindent},
2833 {"col", 1, 1, f_col},
2834 {"confirm", 1, 4, f_confirm},
2835 {"cscope_connection",0,3, f_cscope_connection},
2836 {"cursor", 2, 2, f_cursor},
2837 {"delete", 1, 1, f_delete},
2838 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00002839 {"diff_filler", 1, 1, f_diff_filler},
2840 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {"escape", 2, 2, f_escape},
2842 {"eventhandler", 0, 0, f_eventhandler},
2843 {"executable", 1, 1, f_executable},
2844 {"exists", 1, 1, f_exists},
2845 {"expand", 1, 2, f_expand},
2846 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
2847 {"filereadable", 1, 1, f_filereadable},
2848 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002849 {"finddir", 1, 3, f_finddir},
2850 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {"fnamemodify", 2, 2, f_fnamemodify},
2852 {"foldclosed", 1, 1, f_foldclosed},
2853 {"foldclosedend", 1, 1, f_foldclosedend},
2854 {"foldlevel", 1, 1, f_foldlevel},
2855 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002856 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {"foreground", 0, 0, f_foreground},
2858 {"getbufvar", 2, 2, f_getbufvar},
2859 {"getchar", 0, 1, f_getchar},
2860 {"getcharmod", 0, 0, f_getcharmod},
2861 {"getcmdline", 0, 0, f_getcmdline},
2862 {"getcmdpos", 0, 0, f_getcmdpos},
2863 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002864 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002865 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {"getfsize", 1, 1, f_getfsize},
2867 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002868 {"getftype", 1, 1, f_getftype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {"getline", 1, 1, f_getline},
2870 {"getreg", 0, 1, f_getreg},
2871 {"getregtype", 0, 1, f_getregtype},
2872 {"getwinposx", 0, 0, f_getwinposx},
2873 {"getwinposy", 0, 0, f_getwinposy},
2874 {"getwinvar", 2, 2, f_getwinvar},
2875 {"glob", 1, 1, f_glob},
2876 {"globpath", 2, 2, f_globpath},
2877 {"has", 1, 1, f_has},
2878 {"hasmapto", 1, 2, f_hasmapto},
2879 {"highlightID", 1, 1, f_hlID}, /* obsolete */
2880 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
2881 {"histadd", 2, 2, f_histadd},
2882 {"histdel", 1, 2, f_histdel},
2883 {"histget", 1, 2, f_histget},
2884 {"histnr", 1, 1, f_histnr},
2885 {"hlID", 1, 1, f_hlID},
2886 {"hlexists", 1, 1, f_hlexists},
2887 {"hostname", 0, 0, f_hostname},
2888 {"iconv", 3, 3, f_iconv},
2889 {"indent", 1, 1, f_indent},
2890 {"input", 1, 2, f_input},
2891 {"inputdialog", 1, 3, f_inputdialog},
2892 {"inputrestore", 0, 0, f_inputrestore},
2893 {"inputsave", 0, 0, f_inputsave},
2894 {"inputsecret", 1, 2, f_inputsecret},
2895 {"isdirectory", 1, 1, f_isdirectory},
2896 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
2897 {"libcall", 3, 3, f_libcall},
2898 {"libcallnr", 3, 3, f_libcallnr},
2899 {"line", 1, 1, f_line},
2900 {"line2byte", 1, 1, f_line2byte},
2901 {"lispindent", 1, 1, f_lispindent},
2902 {"localtime", 0, 0, f_localtime},
2903 {"maparg", 1, 2, f_maparg},
2904 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002905 {"match", 2, 4, f_match},
2906 {"matchend", 2, 4, f_matchend},
2907 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {"mode", 0, 0, f_mode},
2909 {"nextnonblank", 1, 1, f_nextnonblank},
2910 {"nr2char", 1, 1, f_nr2char},
2911 {"prevnonblank", 1, 1, f_prevnonblank},
2912 {"remote_expr", 2, 3, f_remote_expr},
2913 {"remote_foreground", 1, 1, f_remote_foreground},
2914 {"remote_peek", 1, 2, f_remote_peek},
2915 {"remote_read", 1, 1, f_remote_read},
2916 {"remote_send", 2, 3, f_remote_send},
2917 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00002918 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {"resolve", 1, 1, f_resolve},
2920 {"search", 1, 2, f_search},
2921 {"searchpair", 3, 5, f_searchpair},
2922 {"server2client", 2, 2, f_server2client},
2923 {"serverlist", 0, 0, f_serverlist},
2924 {"setbufvar", 3, 3, f_setbufvar},
2925 {"setcmdpos", 1, 1, f_setcmdpos},
2926 {"setline", 2, 2, f_setline},
2927 {"setreg", 2, 3, f_setreg},
2928 {"setwinvar", 3, 3, f_setwinvar},
2929 {"simplify", 1, 1, f_simplify},
2930#ifdef HAVE_STRFTIME
2931 {"strftime", 1, 2, f_strftime},
2932#endif
2933 {"stridx", 2, 2, f_stridx},
2934 {"strlen", 1, 1, f_strlen},
2935 {"strpart", 2, 3, f_strpart},
2936 {"strridx", 2, 2, f_strridx},
2937 {"strtrans", 1, 1, f_strtrans},
2938 {"submatch", 1, 1, f_submatch},
2939 {"substitute", 4, 4, f_substitute},
2940 {"synID", 3, 3, f_synID},
2941 {"synIDattr", 2, 3, f_synIDattr},
2942 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002943 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {"tempname", 0, 0, f_tempname},
2945 {"tolower", 1, 1, f_tolower},
2946 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00002947 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {"type", 1, 1, f_type},
2949 {"virtcol", 1, 1, f_virtcol},
2950 {"visualmode", 0, 1, f_visualmode},
2951 {"winbufnr", 1, 1, f_winbufnr},
2952 {"wincol", 0, 0, f_wincol},
2953 {"winheight", 1, 1, f_winheight},
2954 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002955 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {"winrestcmd", 0, 0, f_winrestcmd},
2957 {"winwidth", 1, 1, f_winwidth},
2958};
2959
2960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2961
2962/*
2963 * Function given to ExpandGeneric() to obtain the list of internal
2964 * or user defined function names.
2965 */
2966 char_u *
2967get_function_name(xp, idx)
2968 expand_T *xp;
2969 int idx;
2970{
2971 static int intidx = -1;
2972 char_u *name;
2973
2974 if (idx == 0)
2975 intidx = -1;
2976 if (intidx < 0)
2977 {
2978 name = get_user_func_name(xp, idx);
2979 if (name != NULL)
2980 return name;
2981 }
2982 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
2983 {
2984 STRCPY(IObuff, functions[intidx].f_name);
2985 STRCAT(IObuff, "(");
2986 if (functions[intidx].f_max_argc == 0)
2987 STRCAT(IObuff, ")");
2988 return IObuff;
2989 }
2990
2991 return NULL;
2992}
2993
2994/*
2995 * Function given to ExpandGeneric() to obtain the list of internal or
2996 * user defined variable or function names.
2997 */
2998/*ARGSUSED*/
2999 char_u *
3000get_expr_name(xp, idx)
3001 expand_T *xp;
3002 int idx;
3003{
3004 static int intidx = -1;
3005 char_u *name;
3006
3007 if (idx == 0)
3008 intidx = -1;
3009 if (intidx < 0)
3010 {
3011 name = get_function_name(xp, idx);
3012 if (name != NULL)
3013 return name;
3014 }
3015 return get_user_var_name(xp, ++intidx);
3016}
3017
3018#endif /* FEAT_CMDL_COMPL */
3019
3020/*
3021 * Find internal function in table above.
3022 * Return index, or -1 if not found
3023 */
3024 static int
3025find_internal_func(name)
3026 char_u *name; /* name of the function */
3027{
3028 int first = 0;
3029 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
3030 int cmp;
3031 int x;
3032
3033 /*
3034 * Find the function name in the table. Binary search.
3035 */
3036 while (first <= last)
3037 {
3038 x = first + ((unsigned)(last - first) >> 1);
3039 cmp = STRCMP(name, functions[x].f_name);
3040 if (cmp < 0)
3041 last = x - 1;
3042 else if (cmp > 0)
3043 first = x + 1;
3044 else
3045 return x;
3046 }
3047 return -1;
3048}
3049
3050/*
3051 * Allocate a variable for the result of a function.
3052 * Return OK or FAIL.
3053 */
3054 static int
3055get_func_var(name, len, retvar, arg, firstline, lastline, doesrange, evaluate)
3056 char_u *name; /* name of the function */
3057 int len; /* length of "name" */
3058 VAR retvar;
3059 char_u **arg; /* argument, pointing to the '(' */
3060 linenr_T firstline; /* first line of range */
3061 linenr_T lastline; /* last line of range */
3062 int *doesrange; /* return: function handled range */
3063 int evaluate;
3064{
3065 char_u *argp;
3066 int ret = OK;
3067#define MAX_FUNC_ARGS 20
3068 var argvars[MAX_FUNC_ARGS]; /* vars for arguments */
3069 int argcount = 0; /* number of arguments found */
3070
3071 /*
3072 * Get the arguments.
3073 */
3074 argp = *arg;
3075 while (argcount < MAX_FUNC_ARGS)
3076 {
3077 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
3078 if (*argp == ')' || *argp == ',' || *argp == NUL)
3079 break;
3080 argvars[argcount].var_name = NULL; /* the name is not stored */
3081 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
3082 {
3083 ret = FAIL;
3084 break;
3085 }
3086 ++argcount;
3087 if (*argp != ',')
3088 break;
3089 }
3090 if (*argp == ')')
3091 ++argp;
3092 else
3093 ret = FAIL;
3094
3095 if (ret == OK)
3096 ret = call_func(name, len, retvar, argcount, argvars,
3097 firstline, lastline, doesrange, evaluate);
3098 else if (!aborting())
3099 EMSG2(_("E116: Invalid arguments for function %s"), name);
3100
3101 while (--argcount >= 0)
3102 clear_var(&argvars[argcount]);
3103
3104 *arg = skipwhite(argp);
3105 return ret;
3106}
3107
3108
3109/*
3110 * Call a function with its resolved parameters
3111 * Return OK or FAIL.
3112 */
3113 static int
3114call_func(name, len, retvar, argcount, argvars, firstline, lastline,
3115 doesrange, evaluate)
3116 char_u *name; /* name of the function */
3117 int len; /* length of "name" */
3118 VAR retvar; /* return value goes here */
3119 int argcount; /* number of "argvars" */
3120 VAR argvars; /* vars for arguments */
3121 linenr_T firstline; /* first line of range */
3122 linenr_T lastline; /* last line of range */
3123 int *doesrange; /* return: function handled range */
3124 int evaluate;
3125{
3126 int ret = FAIL;
3127 static char *errors[] =
3128 {N_("E117: Unknown function: %s"),
3129 N_("E118: Too many arguments for function: %s"),
3130 N_("E119: Not enough arguments for function: %s"),
3131 N_("E120: Using <SID> not in a script context: %s"),
3132 };
3133#define ERROR_UNKNOWN 0
3134#define ERROR_TOOMANY 1
3135#define ERROR_TOOFEW 2
3136#define ERROR_SCRIPT 3
3137#define ERROR_NONE 4
3138#define ERROR_OTHER 5
3139 int error = ERROR_NONE;
3140 int i;
3141 int llen;
3142 ufunc_T *fp;
3143 int cc;
3144#define FLEN_FIXED 40
3145 char_u fname_buf[FLEN_FIXED + 1];
3146 char_u *fname;
3147
3148 /*
3149 * In a script change <SID>name() and s:name() to K_SNR 123_name().
3150 * Change <SNR>123_name() to K_SNR 123_name().
3151 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
3152 */
3153 cc = name[len];
3154 name[len] = NUL;
3155 llen = eval_fname_script(name);
3156 if (llen > 0)
3157 {
3158 fname_buf[0] = K_SPECIAL;
3159 fname_buf[1] = KS_EXTRA;
3160 fname_buf[2] = (int)KE_SNR;
3161 i = 3;
3162 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
3163 {
3164 if (current_SID <= 0)
3165 error = ERROR_SCRIPT;
3166 else
3167 {
3168 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
3169 i = (int)STRLEN(fname_buf);
3170 }
3171 }
3172 if (i + STRLEN(name + llen) < FLEN_FIXED)
3173 {
3174 STRCPY(fname_buf + i, name + llen);
3175 fname = fname_buf;
3176 }
3177 else
3178 {
3179 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
3180 if (fname == NULL)
3181 error = ERROR_OTHER;
3182 else
3183 {
3184 mch_memmove(fname, fname_buf, (size_t)i);
3185 STRCPY(fname + i, name + llen);
3186 }
3187 }
3188 }
3189 else
3190 fname = name;
3191
3192 *doesrange = FALSE;
3193
3194
3195 /* execute the function if no errors detected and executing */
3196 if (evaluate && error == ERROR_NONE)
3197 {
3198 retvar->var_type = VAR_NUMBER; /* default is number retvar */
3199 error = ERROR_UNKNOWN;
3200
3201 if (!ASCII_ISLOWER(fname[0]))
3202 {
3203 /*
3204 * User defined function.
3205 */
3206 fp = find_func(fname);
3207#ifdef FEAT_AUTOCMD
3208 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
3209 fname, fname, TRUE, NULL)
3210#ifdef FEAT_EVAL
3211 && !aborting()
3212#endif
3213 )
3214 {
3215 /* executed an autocommand, search for function again */
3216 fp = find_func(fname);
3217 }
3218#endif
3219 if (fp != NULL)
3220 {
3221 if (fp->flags & FC_RANGE)
3222 *doesrange = TRUE;
3223 if (argcount < fp->args.ga_len)
3224 error = ERROR_TOOFEW;
3225 else if (!fp->varargs && argcount > fp->args.ga_len)
3226 error = ERROR_TOOMANY;
3227 else
3228 {
3229 /*
3230 * Call the user function.
3231 * Save and restore search patterns, script variables and
3232 * redo buffer.
3233 */
3234 save_search_patterns();
3235 saveRedobuff();
3236 ++fp->calls;
3237 call_user_func(fp, argcount, argvars, retvar,
3238 firstline, lastline);
3239 --fp->calls;
3240 restoreRedobuff();
3241 restore_search_patterns();
3242 error = ERROR_NONE;
3243 }
3244 }
3245 }
3246 else
3247 {
3248 /*
3249 * Find the function name in the table, call its implementation.
3250 */
3251 i = find_internal_func(fname);
3252 if (i >= 0)
3253 {
3254 if (argcount < functions[i].f_min_argc)
3255 error = ERROR_TOOFEW;
3256 else if (argcount > functions[i].f_max_argc)
3257 error = ERROR_TOOMANY;
3258 else
3259 {
3260 argvars[argcount].var_type = VAR_UNKNOWN;
3261 functions[i].f_func(argvars, retvar);
3262 error = ERROR_NONE;
3263 }
3264 }
3265 }
3266 /*
3267 * The function call (or "FuncUndefined" autocommand sequence) might
3268 * have been aborted by an error, an interrupt, or an explicitly thrown
3269 * exception that has not been caught so far. This situation can be
3270 * tested for by calling aborting(). For an error in an internal
3271 * function or for the "E132" error in call_user_func(), however, the
3272 * throw point at which the "force_abort" flag (temporarily reset by
3273 * emsg()) is normally updated has not been reached yet. We need to
3274 * update that flag first to make aborting() reliable.
3275 */
3276 update_force_abort();
3277 }
3278 if (error == ERROR_NONE)
3279 ret = OK;
3280
3281 /*
3282 * Report an error unless the argument evaluation or function call has been
3283 * cancelled due to an aborting error, an interrupt, or an exception.
3284 */
3285 if (error < ERROR_NONE && !aborting())
3286 EMSG2((char_u *)_(errors[error]), name);
3287
3288 name[len] = cc;
3289 if (fname != name && fname != fname_buf)
3290 vim_free(fname);
3291
3292 return ret;
3293}
3294
3295/*********************************************
3296 * Implementation of the built-in functions
3297 */
3298
3299/*
3300 * "append(lnum, string)" function
3301 */
3302 static void
3303f_append(argvars, retvar)
3304 VAR argvars;
3305 VAR retvar;
3306{
3307 long lnum;
3308
3309 lnum = get_var_lnum(argvars);
3310 retvar->var_val.var_number = 1; /* Default: Failed */
3311
3312 if (lnum >= 0
3313 && lnum <= curbuf->b_ml.ml_line_count
3314 && u_save(lnum, lnum + 1) == OK)
3315 {
3316 ml_append(lnum, get_var_string(&argvars[1]), (colnr_T)0, FALSE);
3317 if (curwin->w_cursor.lnum > lnum)
3318 ++curwin->w_cursor.lnum;
3319 appended_lines_mark(lnum, 1L);
3320 retvar->var_val.var_number = 0;
3321 }
3322}
3323
3324/*
3325 * "argc()" function
3326 */
3327/* ARGSUSED */
3328 static void
3329f_argc(argvars, retvar)
3330 VAR argvars;
3331 VAR retvar;
3332{
3333 retvar->var_val.var_number = ARGCOUNT;
3334}
3335
3336/*
3337 * "argidx()" function
3338 */
3339/* ARGSUSED */
3340 static void
3341f_argidx(argvars, retvar)
3342 VAR argvars;
3343 VAR retvar;
3344{
3345 retvar->var_val.var_number = curwin->w_arg_idx;
3346}
3347
3348/*
3349 * "argv(nr)" function
3350 */
3351 static void
3352f_argv(argvars, retvar)
3353 VAR argvars;
3354 VAR retvar;
3355{
3356 int idx;
3357
3358 idx = get_var_number(&argvars[0]);
3359 if (idx >= 0 && idx < ARGCOUNT)
3360 retvar->var_val.var_string = vim_strsave(alist_name(&ARGLIST[idx]));
3361 else
3362 retvar->var_val.var_string = NULL;
3363 retvar->var_type = VAR_STRING;
3364}
3365
3366/*
3367 * "browse(save, title, initdir, default)" function
3368 */
3369/* ARGSUSED */
3370 static void
3371f_browse(argvars, retvar)
3372 VAR argvars;
3373 VAR retvar;
3374{
3375#ifdef FEAT_BROWSE
3376 int save;
3377 char_u *title;
3378 char_u *initdir;
3379 char_u *defname;
3380 char_u buf[NUMBUFLEN];
3381 char_u buf2[NUMBUFLEN];
3382
3383 save = get_var_number(&argvars[0]);
3384 title = get_var_string(&argvars[1]);
3385 initdir = get_var_string_buf(&argvars[2], buf);
3386 defname = get_var_string_buf(&argvars[3], buf2);
3387
3388 retvar->var_val.var_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003389 do_browse(save ? BROWSE_SAVE : 0,
3390 title, defname, NULL, initdir, NULL, curbuf);
3391#else
3392 retvar->var_val.var_string = NULL;
3393#endif
3394 retvar->var_type = VAR_STRING;
3395}
3396
3397/*
3398 * "browsedir(title, initdir)" function
3399 */
3400/* ARGSUSED */
3401 static void
3402f_browsedir(argvars, retvar)
3403 VAR argvars;
3404 VAR retvar;
3405{
3406#ifdef FEAT_BROWSE
3407 char_u *title;
3408 char_u *initdir;
3409 char_u buf[NUMBUFLEN];
3410
3411 title = get_var_string(&argvars[0]);
3412 initdir = get_var_string_buf(&argvars[1], buf);
3413
3414 retvar->var_val.var_string = do_browse(BROWSE_DIR,
3415 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#else
3417 retvar->var_val.var_string = NULL;
3418#endif
3419 retvar->var_type = VAR_STRING;
3420}
3421
3422/*
3423 * Find a buffer by number or exact name.
3424 */
3425 static buf_T *
3426find_buffer(avar)
3427 VAR avar;
3428{
3429 buf_T *buf = NULL;
3430 char_u *name;
3431
3432 if (avar->var_type == VAR_NUMBER)
3433 buf = buflist_findnr((int)avar->var_val.var_number);
3434 else if (avar->var_val.var_string != NULL)
3435 {
3436 /* First make the name into a full path name */
3437 name = FullName_save(avar->var_val.var_string,
3438#ifdef UNIX
3439 TRUE /* force expansion, get rid of symbolic links */
3440#else
3441 FALSE
3442#endif
3443 );
3444 if (name != NULL)
3445 {
3446 buf = buflist_findname(name);
3447 vim_free(name);
3448 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003449 if (buf == NULL)
3450 {
3451 /* No full path name match, try a match with a URL or a "nofile"
3452 * buffer, these don't use the full path. */
3453 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3454 if (buf->b_fname != NULL
3455 && (path_with_url(buf->b_fname)
3456#ifdef FEAT_QUICKFIX
3457 || bt_nofile(buf)
3458#endif
3459 )
3460 && STRCMP(buf->b_fname, avar->var_val.var_string) == 0)
3461 break;
3462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464 return buf;
3465}
3466
3467/*
3468 * "bufexists(expr)" function
3469 */
3470 static void
3471f_bufexists(argvars, retvar)
3472 VAR argvars;
3473 VAR retvar;
3474{
3475 retvar->var_val.var_number = (find_buffer(&argvars[0]) != NULL);
3476}
3477
3478/*
3479 * "buflisted(expr)" function
3480 */
3481 static void
3482f_buflisted(argvars, retvar)
3483 VAR argvars;
3484 VAR retvar;
3485{
3486 buf_T *buf;
3487
3488 buf = find_buffer(&argvars[0]);
3489 retvar->var_val.var_number = (buf != NULL && buf->b_p_bl);
3490}
3491
3492/*
3493 * "bufloaded(expr)" function
3494 */
3495 static void
3496f_bufloaded(argvars, retvar)
3497 VAR argvars;
3498 VAR retvar;
3499{
3500 buf_T *buf;
3501
3502 buf = find_buffer(&argvars[0]);
3503 retvar->var_val.var_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
3504}
3505
3506/*
3507 * Get buffer by number or pattern.
3508 */
3509 static buf_T *
3510get_buf_var(avar)
3511 VAR avar;
3512{
3513 char_u *name = avar->var_val.var_string;
3514 int save_magic;
3515 char_u *save_cpo;
3516 buf_T *buf;
3517
3518 if (avar->var_type == VAR_NUMBER)
3519 return buflist_findnr((int)avar->var_val.var_number);
3520 if (name == NULL || *name == NUL)
3521 return curbuf;
3522 if (name[0] == '$' && name[1] == NUL)
3523 return lastbuf;
3524
3525 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
3526 save_magic = p_magic;
3527 p_magic = TRUE;
3528 save_cpo = p_cpo;
3529 p_cpo = (char_u *)"";
3530
3531 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
3532 TRUE, FALSE));
3533
3534 p_magic = save_magic;
3535 p_cpo = save_cpo;
3536
3537 /* If not found, try expanding the name, like done for bufexists(). */
3538 if (buf == NULL)
3539 buf = find_buffer(avar);
3540
3541 return buf;
3542}
3543
3544/*
3545 * "bufname(expr)" function
3546 */
3547 static void
3548f_bufname(argvars, retvar)
3549 VAR argvars;
3550 VAR retvar;
3551{
3552 buf_T *buf;
3553
3554 ++emsg_off;
3555 buf = get_buf_var(&argvars[0]);
3556 retvar->var_type = VAR_STRING;
3557 if (buf != NULL && buf->b_fname != NULL)
3558 retvar->var_val.var_string = vim_strsave(buf->b_fname);
3559 else
3560 retvar->var_val.var_string = NULL;
3561 --emsg_off;
3562}
3563
3564/*
3565 * "bufnr(expr)" function
3566 */
3567 static void
3568f_bufnr(argvars, retvar)
3569 VAR argvars;
3570 VAR retvar;
3571{
3572 buf_T *buf;
3573
3574 ++emsg_off;
3575 buf = get_buf_var(&argvars[0]);
3576 if (buf != NULL)
3577 retvar->var_val.var_number = buf->b_fnum;
3578 else
3579 retvar->var_val.var_number = -1;
3580 --emsg_off;
3581}
3582
3583/*
3584 * "bufwinnr(nr)" function
3585 */
3586 static void
3587f_bufwinnr(argvars, retvar)
3588 VAR argvars;
3589 VAR retvar;
3590{
3591#ifdef FEAT_WINDOWS
3592 win_T *wp;
3593 int winnr = 0;
3594#endif
3595 buf_T *buf;
3596
3597 ++emsg_off;
3598 buf = get_buf_var(&argvars[0]);
3599#ifdef FEAT_WINDOWS
3600 for (wp = firstwin; wp; wp = wp->w_next)
3601 {
3602 ++winnr;
3603 if (wp->w_buffer == buf)
3604 break;
3605 }
3606 retvar->var_val.var_number = (wp != NULL ? winnr : -1);
3607#else
3608 retvar->var_val.var_number = (curwin->w_buffer == buf ? 1 : -1);
3609#endif
3610 --emsg_off;
3611}
3612
3613/*
3614 * "byte2line(byte)" function
3615 */
3616/*ARGSUSED*/
3617 static void
3618f_byte2line(argvars, retvar)
3619 VAR argvars;
3620 VAR retvar;
3621{
3622#ifndef FEAT_BYTEOFF
3623 retvar->var_val.var_number = -1;
3624#else
3625 long boff = 0;
3626
3627 boff = get_var_number(&argvars[0]) - 1;
3628 if (boff < 0)
3629 retvar->var_val.var_number = -1;
3630 else
3631 retvar->var_val.var_number = ml_find_line_or_offset(curbuf,
3632 (linenr_T)0, &boff);
3633#endif
3634}
3635
3636/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003637 * "byteidx()" function
3638 */
3639/*ARGSUSED*/
3640 static void
3641f_byteidx(argvars, retvar)
3642 VAR argvars;
3643 VAR retvar;
3644{
3645#ifdef FEAT_MBYTE
3646 char_u *t;
3647#endif
3648 char_u *str;
3649 long idx;
3650
3651 str = get_var_string(&argvars[0]);
3652 idx = get_var_number(&argvars[1]);
3653 retvar->var_val.var_number = -1;
3654 if (idx < 0)
3655 return;
3656
3657#ifdef FEAT_MBYTE
3658 t = str;
3659 for ( ; idx > 0; idx--)
3660 {
3661 if (*t == NUL) /* EOL reached */
3662 return;
3663 t += mb_ptr2len_check(t);
3664 }
3665 retvar->var_val.var_number = t - str;
3666#else
3667 if (idx <= STRLEN(str))
3668 retvar->var_val.var_number = idx;
3669#endif
3670}
3671
3672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 * "char2nr(string)" function
3674 */
3675 static void
3676f_char2nr(argvars, retvar)
3677 VAR argvars;
3678 VAR retvar;
3679{
3680#ifdef FEAT_MBYTE
3681 if (has_mbyte)
3682 retvar->var_val.var_number =
3683 (*mb_ptr2char)(get_var_string(&argvars[0]));
3684 else
3685#endif
3686 retvar->var_val.var_number = get_var_string(&argvars[0])[0];
3687}
3688
3689/*
3690 * "cindent(lnum)" function
3691 */
3692 static void
3693f_cindent(argvars, retvar)
3694 VAR argvars;
3695 VAR retvar;
3696{
3697#ifdef FEAT_CINDENT
3698 pos_T pos;
3699 linenr_T lnum;
3700
3701 pos = curwin->w_cursor;
3702 lnum = get_var_lnum(argvars);
3703 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3704 {
3705 curwin->w_cursor.lnum = lnum;
3706 retvar->var_val.var_number = get_c_indent();
3707 curwin->w_cursor = pos;
3708 }
3709 else
3710#endif
3711 retvar->var_val.var_number = -1;
3712}
3713
3714/*
3715 * "col(string)" function
3716 */
3717 static void
3718f_col(argvars, retvar)
3719 VAR argvars;
3720 VAR retvar;
3721{
3722 colnr_T col = 0;
3723 pos_T *fp;
3724
3725 fp = var2fpos(&argvars[0], FALSE);
3726 if (fp != NULL)
3727 {
3728 if (fp->col == MAXCOL)
3729 {
3730 /* '> can be MAXCOL, get the length of the line then */
3731 if (fp->lnum <= curbuf->b_ml.ml_line_count)
3732 col = STRLEN(ml_get(fp->lnum)) + 1;
3733 else
3734 col = MAXCOL;
3735 }
3736 else
3737 {
3738 col = fp->col + 1;
3739#ifdef FEAT_VIRTUALEDIT
3740 /* col(".") when the cursor is on the NUL at the end of the line
3741 * because of "coladd" can be seen as an extra column. */
3742 if (virtual_active() && fp == &curwin->w_cursor)
3743 {
3744 char_u *p = ml_get_cursor();
3745
3746 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
3747 curwin->w_virtcol - curwin->w_cursor.coladd))
3748 {
3749# ifdef FEAT_MBYTE
3750 int l;
3751
3752 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
3753 col += l;
3754# else
3755 if (*p != NUL && p[1] == NUL)
3756 ++col;
3757# endif
3758 }
3759 }
3760#endif
3761 }
3762 }
3763 retvar->var_val.var_number = col;
3764}
3765
3766/*
3767 * "confirm(message, buttons[, default [, type]])" function
3768 */
3769/*ARGSUSED*/
3770 static void
3771f_confirm(argvars, retvar)
3772 VAR argvars;
3773 VAR retvar;
3774{
3775#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3776 char_u *message;
3777 char_u *buttons = NULL;
3778 char_u buf[NUMBUFLEN];
3779 char_u buf2[NUMBUFLEN];
3780 int def = 1;
3781 int type = VIM_GENERIC;
3782 int c;
3783
3784 message = get_var_string(&argvars[0]);
3785 if (argvars[1].var_type != VAR_UNKNOWN)
3786 {
3787 buttons = get_var_string_buf(&argvars[1], buf);
3788 if (argvars[2].var_type != VAR_UNKNOWN)
3789 {
3790 def = get_var_number(&argvars[2]);
3791 if (argvars[3].var_type != VAR_UNKNOWN)
3792 {
3793 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
3794 c = *get_var_string_buf(&argvars[3], buf2);
3795 switch (TOUPPER_ASC(c))
3796 {
3797 case 'E': type = VIM_ERROR; break;
3798 case 'Q': type = VIM_QUESTION; break;
3799 case 'I': type = VIM_INFO; break;
3800 case 'W': type = VIM_WARNING; break;
3801 case 'G': type = VIM_GENERIC; break;
3802 }
3803 }
3804 }
3805 }
3806
3807 if (buttons == NULL || *buttons == NUL)
3808 buttons = (char_u *)_("&Ok");
3809
3810 retvar->var_val.var_number = do_dialog(type, NULL, message, buttons,
3811 def, NULL);
3812#else
3813 retvar->var_val.var_number = 0;
3814#endif
3815}
3816
3817
3818/*
3819 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
3820 *
3821 * Checks the existence of a cscope connection.
3822 */
3823/*ARGSUSED*/
3824 static void
3825f_cscope_connection(argvars, retvar)
3826 VAR argvars;
3827 VAR retvar;
3828{
3829#ifdef FEAT_CSCOPE
3830 int num = 0;
3831 char_u *dbpath = NULL;
3832 char_u *prepend = NULL;
3833 char_u buf[NUMBUFLEN];
3834
3835 if (argvars[0].var_type != VAR_UNKNOWN
3836 && argvars[1].var_type != VAR_UNKNOWN)
3837 {
3838 num = (int)get_var_number(&argvars[0]);
3839 dbpath = get_var_string(&argvars[1]);
3840 if (argvars[2].var_type != VAR_UNKNOWN)
3841 prepend = get_var_string_buf(&argvars[2], buf);
3842 }
3843
3844 retvar->var_val.var_number = cs_connection(num, dbpath, prepend);
3845#else
3846 retvar->var_val.var_number = 0;
3847#endif
3848}
3849
3850/*
3851 * "cursor(lnum, col)" function
3852 *
3853 * Moves the cursor to the specified line and column
3854 */
3855/*ARGSUSED*/
3856 static void
3857f_cursor(argvars, retvar)
3858 VAR argvars;
3859 VAR retvar;
3860{
3861 long line, col;
3862
3863 line = get_var_lnum(argvars);
3864 if (line > 0)
3865 curwin->w_cursor.lnum = line;
3866 col = get_var_number(&argvars[1]);
3867 if (col > 0)
3868 curwin->w_cursor.col = col - 1;
3869#ifdef FEAT_VIRTUALEDIT
3870 curwin->w_cursor.coladd = 0;
3871#endif
3872
3873 /* Make sure the cursor is in a valid position. */
3874 check_cursor();
3875#ifdef FEAT_MBYTE
3876 /* Correct cursor for multi-byte character. */
3877 if (has_mbyte)
3878 mb_adjust_cursor();
3879#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003880
3881 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882}
3883
3884/*
3885 * "libcall()" function
3886 */
3887 static void
3888f_libcall(argvars, retvar)
3889 VAR argvars;
3890 VAR retvar;
3891{
3892 libcall_common(argvars, retvar, VAR_STRING);
3893}
3894
3895/*
3896 * "libcallnr()" function
3897 */
3898 static void
3899f_libcallnr(argvars, retvar)
3900 VAR argvars;
3901 VAR retvar;
3902{
3903 libcall_common(argvars, retvar, VAR_NUMBER);
3904}
3905
3906 static void
3907libcall_common(argvars, retvar, type)
3908 VAR argvars;
3909 VAR retvar;
3910 int type;
3911{
3912#ifdef FEAT_LIBCALL
3913 char_u *string_in;
3914 char_u **string_result;
3915 int nr_result;
3916#endif
3917
3918 retvar->var_type = type;
3919 if (type == VAR_NUMBER)
3920 retvar->var_val.var_number = 0;
3921 else
3922 retvar->var_val.var_string = NULL;
3923
3924 if (check_restricted() || check_secure())
3925 return;
3926
3927#ifdef FEAT_LIBCALL
3928 /* The first two args must be strings, otherwise its meaningless */
3929 if (argvars[0].var_type == VAR_STRING && argvars[1].var_type == VAR_STRING)
3930 {
3931 if (argvars[2].var_type == VAR_NUMBER)
3932 string_in = NULL;
3933 else
3934 string_in = argvars[2].var_val.var_string;
3935 if (type == VAR_NUMBER)
3936 string_result = NULL;
3937 else
3938 string_result = &retvar->var_val.var_string;
3939 if (mch_libcall(argvars[0].var_val.var_string,
3940 argvars[1].var_val.var_string,
3941 string_in,
3942 argvars[2].var_val.var_number,
3943 string_result,
3944 &nr_result) == OK
3945 && type == VAR_NUMBER)
3946 retvar->var_val.var_number = nr_result;
3947 }
3948#endif
3949}
3950
3951/*
3952 * "delete()" function
3953 */
3954 static void
3955f_delete(argvars, retvar)
3956 VAR argvars;
3957 VAR retvar;
3958{
3959 if (check_restricted() || check_secure())
3960 retvar->var_val.var_number = -1;
3961 else
3962 retvar->var_val.var_number = mch_remove(get_var_string(&argvars[0]));
3963}
3964
3965/*
3966 * "did_filetype()" function
3967 */
3968/*ARGSUSED*/
3969 static void
3970f_did_filetype(argvars, retvar)
3971 VAR argvars;
3972 VAR retvar;
3973{
3974#ifdef FEAT_AUTOCMD
3975 retvar->var_val.var_number = did_filetype;
3976#else
3977 retvar->var_val.var_number = 0;
3978#endif
3979}
3980
3981/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00003982 * "diff_filler()" function
3983 */
3984/*ARGSUSED*/
3985 static void
3986f_diff_filler(argvars, retvar)
3987 VAR argvars;
3988 VAR retvar;
3989{
3990#ifdef FEAT_DIFF
3991 retvar->var_val.var_number = diff_check_fill(curwin, get_var_lnum(argvars));
3992#endif
3993}
3994
3995/*
3996 * "diff_hlID()" function
3997 */
3998/*ARGSUSED*/
3999 static void
4000f_diff_hlID(argvars, retvar)
4001 VAR argvars;
4002 VAR retvar;
4003{
4004#ifdef FEAT_DIFF
4005 linenr_T lnum = get_var_lnum(argvars);
4006 static linenr_T prev_lnum = 0;
4007 static int changedtick = 0;
4008 static int fnum = 0;
4009 static int change_start = 0;
4010 static int change_end = 0;
4011 static enum hlf_value hlID = 0;
4012 int filler_lines;
4013 int col;
4014
4015 if (lnum != prev_lnum
4016 || changedtick != curbuf->b_changedtick
4017 || fnum != curbuf->b_fnum)
4018 {
4019 /* New line, buffer, change: need to get the values. */
4020 filler_lines = diff_check(curwin, lnum);
4021 if (filler_lines < 0)
4022 {
4023 if (filler_lines == -1)
4024 {
4025 change_start = MAXCOL;
4026 change_end = -1;
4027 if (diff_find_change(curwin, lnum, &change_start, &change_end))
4028 hlID = HLF_ADD; /* added line */
4029 else
4030 hlID = HLF_CHD; /* changed line */
4031 }
4032 else
4033 hlID = HLF_ADD; /* added line */
4034 }
4035 else
4036 hlID = (enum hlf_value)0;
4037 prev_lnum = lnum;
4038 changedtick = curbuf->b_changedtick;
4039 fnum = curbuf->b_fnum;
4040 }
4041
4042 if (hlID == HLF_CHD || hlID == HLF_TXD)
4043 {
4044 col = get_var_number(&argvars[1]) - 1;
4045 if (col >= change_start && col <= change_end)
4046 hlID = HLF_TXD; /* changed text */
4047 else
4048 hlID = HLF_CHD; /* changed line */
4049 }
4050 retvar->var_val.var_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
4051#endif
4052}
4053
4054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 * "escape({string}, {chars})" function
4056 */
4057 static void
4058f_escape(argvars, retvar)
4059 VAR argvars;
4060 VAR retvar;
4061{
4062 char_u buf[NUMBUFLEN];
4063
4064 retvar->var_val.var_string =
4065 vim_strsave_escaped(get_var_string(&argvars[0]),
4066 get_var_string_buf(&argvars[1], buf));
4067 retvar->var_type = VAR_STRING;
4068}
4069
4070/*
4071 * "eventhandler()" function
4072 */
4073/*ARGSUSED*/
4074 static void
4075f_eventhandler(argvars, retvar)
4076 VAR argvars;
4077 VAR retvar;
4078{
4079 retvar->var_val.var_number = vgetc_busy;
4080}
4081
4082/*
4083 * "executable()" function
4084 */
4085 static void
4086f_executable(argvars, retvar)
4087 VAR argvars;
4088 VAR retvar;
4089{
4090 retvar->var_val.var_number = mch_can_exe(get_var_string(&argvars[0]));
4091}
4092
4093/*
4094 * "exists()" function
4095 */
4096 static void
4097f_exists(argvars, retvar)
4098 VAR argvars;
4099 VAR retvar;
4100{
4101 char_u *p;
4102 char_u *name;
4103 int n = FALSE;
4104 int len = 0;
4105
4106 p = get_var_string(&argvars[0]);
4107 if (*p == '$') /* environment variable */
4108 {
4109 /* first try "normal" environment variables (fast) */
4110 if (mch_getenv(p + 1) != NULL)
4111 n = TRUE;
4112 else
4113 {
4114 /* try expanding things like $VIM and ${HOME} */
4115 p = expand_env_save(p);
4116 if (p != NULL && *p != '$')
4117 n = TRUE;
4118 vim_free(p);
4119 }
4120 }
4121 else if (*p == '&' || *p == '+') /* option */
4122 n = (get_option_var(&p, NULL, TRUE) == OK);
4123 else if (*p == '*') /* internal or user defined function */
4124 {
4125 ++p;
4126 p = trans_function_name(&p, FALSE, TRUE);
4127 if (p != NULL)
4128 {
4129 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
4130 n = (find_func(p) != NULL);
4131 else if (ASCII_ISLOWER(*p))
4132 n = (find_internal_func(p) >= 0);
4133 vim_free(p);
4134 }
4135 }
4136 else if (*p == ':')
4137 {
4138 n = cmd_exists(p + 1);
4139 }
4140 else if (*p == '#')
4141 {
4142#ifdef FEAT_AUTOCMD
4143 name = p + 1;
4144 p = vim_strchr(name, '#');
4145 if (p != NULL)
4146 n = au_exists(name, p, p + 1);
4147 else
4148 n = au_exists(name, name + STRLEN(name), NULL);
4149#endif
4150 }
4151 else /* internal variable */
4152 {
4153#ifdef FEAT_MAGIC_BRACES
4154 char_u *expr_start;
4155 char_u *expr_end;
4156 char_u *temp_string = NULL;
4157 char_u *s;
4158#endif
4159 name = p;
4160
4161#ifdef FEAT_MAGIC_BRACES
4162 /* Find the end of the name. */
4163 s = find_name_end(name, &expr_start, &expr_end);
4164 if (expr_start != NULL)
4165 {
4166 temp_string = make_expanded_name(name, expr_start, expr_end, s);
4167 if (temp_string != NULL)
4168 {
4169 len = STRLEN(temp_string);
4170 name = temp_string;
4171 }
4172 }
4173#endif
4174 if (len == 0)
4175 len = get_id_len(&p);
4176 if (len != 0)
4177 n = (get_var_var(name, len, NULL) == OK);
4178
4179#ifdef FEAT_MAGIC_BRACES
4180 vim_free(temp_string);
4181#endif
4182 }
4183
4184 retvar->var_val.var_number = n;
4185}
4186
4187/*
4188 * "expand()" function
4189 */
4190 static void
4191f_expand(argvars, retvar)
4192 VAR argvars;
4193 VAR retvar;
4194{
4195 char_u *s;
4196 int len;
4197 char_u *errormsg;
4198 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
4199 expand_T xpc;
4200
4201 retvar->var_type = VAR_STRING;
4202 s = get_var_string(&argvars[0]);
4203 if (*s == '%' || *s == '#' || *s == '<')
4204 {
4205 ++emsg_off;
4206 retvar->var_val.var_string = eval_vars(s, &len, NULL, &errormsg, s);
4207 --emsg_off;
4208 }
4209 else
4210 {
4211 /* When the optional second argument is non-zero, don't remove matches
4212 * for 'suffixes' and 'wildignore' */
4213 if (argvars[1].var_type != VAR_UNKNOWN && get_var_number(&argvars[1]))
4214 flags |= WILD_KEEP_ALL;
4215 ExpandInit(&xpc);
4216 xpc.xp_context = EXPAND_FILES;
4217 retvar->var_val.var_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
4218 ExpandCleanup(&xpc);
4219 }
4220}
4221
4222/*
4223 * "filereadable()" function
4224 */
4225 static void
4226f_filereadable(argvars, retvar)
4227 VAR argvars;
4228 VAR retvar;
4229{
4230 FILE *fd;
4231 char_u *p;
4232 int n;
4233
4234 p = get_var_string(&argvars[0]);
4235 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
4236 {
4237 n = TRUE;
4238 fclose(fd);
4239 }
4240 else
4241 n = FALSE;
4242
4243 retvar->var_val.var_number = n;
4244}
4245
4246/*
4247 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
4248 * rights to write into.
4249 */
4250 static void
4251f_filewritable(argvars, retvar)
4252 VAR argvars;
4253 VAR retvar;
4254{
4255 char_u *p;
4256 int retval = 0;
4257#if defined(UNIX) || defined(VMS)
4258 int perm = 0;
4259#endif
4260
4261 p = get_var_string(&argvars[0]);
4262#if defined(UNIX) || defined(VMS)
4263 perm = mch_getperm(p);
4264#endif
4265#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
4266 if (
4267# ifdef WIN3264
4268 mch_writable(p) &&
4269# else
4270# if defined(UNIX) || defined(VMS)
4271 (perm & 0222) &&
4272# endif
4273# endif
4274 mch_access((char *)p, W_OK) == 0
4275 )
4276#endif
4277 {
4278 ++retval;
4279 if (mch_isdir(p))
4280 ++retval;
4281 }
4282 retvar->var_val.var_number = retval;
4283}
4284
4285/*
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004286 * "finddir({fname}[, {path}[, {count}]])" function
4287 */
4288 static void
4289f_finddir(argvars, retvar)
4290 VAR argvars;
4291 VAR retvar;
4292{
4293 f_findfilendir(argvars, retvar, TRUE);
4294}
4295
4296/*
4297 * "findfile({fname}[, {path}[, {count}]])" function
4298 */
4299 static void
4300f_findfile(argvars, retvar)
4301 VAR argvars;
4302 VAR retvar;
4303{
4304 f_findfilendir(argvars, retvar, FALSE);
4305}
4306
4307 static void
4308f_findfilendir(argvars, retvar, dir)
4309 VAR argvars;
4310 VAR retvar;
4311 int dir;
4312{
4313#ifdef FEAT_SEARCHPATH
4314 char_u *fname;
4315 char_u *fresult = NULL;
4316 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4317 char_u *p;
4318 char_u pathbuf[NUMBUFLEN];
4319 int count = 1;
4320 int first = TRUE;
4321
4322 fname = get_var_string(&argvars[0]);
4323
4324 if (argvars[1].var_type != VAR_UNKNOWN)
4325 {
4326 p = get_var_string_buf(&argvars[1], pathbuf);
4327 if (*p != NUL)
4328 path = p;
4329
4330 if (argvars[2].var_type != VAR_UNKNOWN)
4331 count = get_var_number(&argvars[2]);
4332 }
4333
4334 do
4335 {
4336 vim_free(fresult);
4337 fresult = find_file_in_path_option(first ? fname : NULL,
4338 first ? (int)STRLEN(fname) : 0,
4339 0, first, path, dir, NULL);
4340 first = FALSE;
4341 } while (--count > 0 && fresult != NULL);
4342
4343 retvar->var_val.var_string = fresult;
4344#else
4345 retvar->var_val.var_string = NULL;
4346#endif
4347 retvar->var_type = VAR_STRING;
4348}
4349
4350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 * "fnamemodify({fname}, {mods})" function
4352 */
4353 static void
4354f_fnamemodify(argvars, retvar)
4355 VAR argvars;
4356 VAR retvar;
4357{
4358 char_u *fname;
4359 char_u *mods;
4360 int usedlen = 0;
4361 int len;
4362 char_u *fbuf = NULL;
4363 char_u buf[NUMBUFLEN];
4364
4365 fname = get_var_string(&argvars[0]);
4366 mods = get_var_string_buf(&argvars[1], buf);
4367 len = (int)STRLEN(fname);
4368
4369 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
4370
4371 retvar->var_type = VAR_STRING;
4372 if (fname == NULL)
4373 retvar->var_val.var_string = NULL;
4374 else
4375 retvar->var_val.var_string = vim_strnsave(fname, len);
4376 vim_free(fbuf);
4377}
4378
4379/*
4380 * "foldclosed()" function
4381 */
4382 static void
4383f_foldclosed(argvars, retvar)
4384 VAR argvars;
4385 VAR retvar;
4386{
4387 foldclosed_both(argvars, retvar, FALSE);
4388}
4389
4390/*
4391 * "foldclosedend()" function
4392 */
4393 static void
4394f_foldclosedend(argvars, retvar)
4395 VAR argvars;
4396 VAR retvar;
4397{
4398 foldclosed_both(argvars, retvar, TRUE);
4399}
4400
4401/*
4402 * "foldclosed()" function
4403 */
4404 static void
4405foldclosed_both(argvars, retvar, end)
4406 VAR argvars;
4407 VAR retvar;
4408 int end;
4409{
4410#ifdef FEAT_FOLDING
4411 linenr_T lnum;
4412 linenr_T first, last;
4413
4414 lnum = get_var_lnum(argvars);
4415 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4416 {
4417 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4418 {
4419 if (end)
4420 retvar->var_val.var_number = (varnumber_T)last;
4421 else
4422 retvar->var_val.var_number = (varnumber_T)first;
4423 return;
4424 }
4425 }
4426#endif
4427 retvar->var_val.var_number = -1;
4428}
4429
4430/*
4431 * "foldlevel()" function
4432 */
4433 static void
4434f_foldlevel(argvars, retvar)
4435 VAR argvars;
4436 VAR retvar;
4437{
4438#ifdef FEAT_FOLDING
4439 linenr_T lnum;
4440
4441 lnum = get_var_lnum(argvars);
4442 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4443 retvar->var_val.var_number = foldLevel(lnum);
4444 else
4445#endif
4446 retvar->var_val.var_number = 0;
4447}
4448
4449/*
4450 * "foldtext()" function
4451 */
4452/*ARGSUSED*/
4453 static void
4454f_foldtext(argvars, retvar)
4455 VAR argvars;
4456 VAR retvar;
4457{
4458#ifdef FEAT_FOLDING
4459 linenr_T lnum;
4460 char_u *s;
4461 char_u *r;
4462 int len;
4463 char *txt;
4464#endif
4465
4466 retvar->var_type = VAR_STRING;
4467 retvar->var_val.var_string = NULL;
4468#ifdef FEAT_FOLDING
4469 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
4470 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
4471 && vimvars[VV_FOLDDASHES].val != NULL)
4472 {
4473 /* Find first non-empty line in the fold. */
4474 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
4475 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
4476 {
4477 if (!linewhite(lnum))
4478 break;
4479 ++lnum;
4480 }
4481
4482 /* Find interesting text in this line. */
4483 s = skipwhite(ml_get(lnum));
4484 /* skip C comment-start */
4485 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004488 if (*skipwhite(s) == NUL
4489 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
4490 {
4491 s = skipwhite(ml_get(lnum + 1));
4492 if (*s == '*')
4493 s = skipwhite(s + 1);
4494 }
4495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 txt = _("+-%s%3ld lines: ");
4497 r = alloc((unsigned)(STRLEN(txt)
4498 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
4499 + 20 /* for %3ld */
4500 + STRLEN(s))); /* concatenated */
4501 if (r != NULL)
4502 {
4503 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
4504 (long)((linenr_T)vimvars[VV_FOLDEND].val
4505 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
4506 len = (int)STRLEN(r);
4507 STRCAT(r, s);
4508 /* remove 'foldmarker' and 'commentstring' */
4509 foldtext_cleanup(r + len);
4510 retvar->var_val.var_string = r;
4511 }
4512 }
4513#endif
4514}
4515
4516/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004517 * "foldtextresult(lnum)" function
4518 */
4519/*ARGSUSED*/
4520 static void
4521f_foldtextresult(argvars, retvar)
4522 VAR argvars;
4523 VAR retvar;
4524{
4525#ifdef FEAT_FOLDING
4526 linenr_T lnum;
4527 char_u *text;
4528 char_u buf[51];
4529 foldinfo_T foldinfo;
4530 int fold_count;
4531#endif
4532
4533 retvar->var_type = VAR_STRING;
4534 retvar->var_val.var_string = NULL;
4535#ifdef FEAT_FOLDING
4536 lnum = get_var_lnum(argvars);
4537 fold_count = foldedCount(curwin, lnum, &foldinfo);
4538 if (fold_count > 0)
4539 {
4540 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4541 &foldinfo, buf);
4542 if (text == buf)
4543 text = vim_strsave(text);
4544 retvar->var_val.var_string = text;
4545 }
4546#endif
4547}
4548
4549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 * "foreground()" function
4551 */
4552/*ARGSUSED*/
4553 static void
4554f_foreground(argvars, retvar)
4555 VAR argvars;
4556 VAR retvar;
4557{
4558 retvar->var_val.var_number = 0;
4559#ifdef FEAT_GUI
4560 if (gui.in_use)
4561 gui_mch_set_foreground();
4562#else
4563# ifdef WIN32
4564 win32_set_foreground();
4565# endif
4566#endif
4567}
4568
4569/*
4570 * "getchar()" function
4571 */
4572 static void
4573f_getchar(argvars, retvar)
4574 VAR argvars;
4575 VAR retvar;
4576{
4577 varnumber_T n;
4578
4579 ++no_mapping;
4580 ++allow_keys;
4581 if (argvars[0].var_type == VAR_UNKNOWN)
4582 /* getchar(): blocking wait. */
4583 n = safe_vgetc();
4584 else if (get_var_number(&argvars[0]) == 1)
4585 /* getchar(1): only check if char avail */
4586 n = vpeekc();
4587 else if (vpeekc() == NUL)
4588 /* getchar(0) and no char avail: return zero */
4589 n = 0;
4590 else
4591 /* getchar(0) and char avail: return char */
4592 n = safe_vgetc();
4593 --no_mapping;
4594 --allow_keys;
4595
4596 retvar->var_val.var_number = n;
4597 if (IS_SPECIAL(n) || mod_mask != 0)
4598 {
4599 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4600 int i = 0;
4601
4602 /* Turn a special key into three bytes, plus modifier. */
4603 if (mod_mask != 0)
4604 {
4605 temp[i++] = K_SPECIAL;
4606 temp[i++] = KS_MODIFIER;
4607 temp[i++] = mod_mask;
4608 }
4609 if (IS_SPECIAL(n))
4610 {
4611 temp[i++] = K_SPECIAL;
4612 temp[i++] = K_SECOND(n);
4613 temp[i++] = K_THIRD(n);
4614 }
4615#ifdef FEAT_MBYTE
4616 else if (has_mbyte)
4617 i += (*mb_char2bytes)(n, temp + i);
4618#endif
4619 else
4620 temp[i++] = n;
4621 temp[i++] = NUL;
4622 retvar->var_type = VAR_STRING;
4623 retvar->var_val.var_string = vim_strsave(temp);
4624 }
4625}
4626
4627/*
4628 * "getcharmod()" function
4629 */
4630/*ARGSUSED*/
4631 static void
4632f_getcharmod(argvars, retvar)
4633 VAR argvars;
4634 VAR retvar;
4635{
4636 retvar->var_val.var_number = mod_mask;
4637}
4638
4639/*
4640 * "getcmdline()" function
4641 */
4642/*ARGSUSED*/
4643 static void
4644f_getcmdline(argvars, retvar)
4645 VAR argvars;
4646 VAR retvar;
4647{
4648 retvar->var_type = VAR_STRING;
4649 retvar->var_val.var_string = get_cmdline_str();
4650}
4651
4652/*
4653 * "getcmdpos()" function
4654 */
4655/*ARGSUSED*/
4656 static void
4657f_getcmdpos(argvars, retvar)
4658 VAR argvars;
4659 VAR retvar;
4660{
4661 retvar->var_val.var_number = get_cmdline_pos() + 1;
4662}
4663
4664/*
4665 * "getbufvar()" function
4666 */
4667 static void
4668f_getbufvar(argvars, retvar)
4669 VAR argvars;
4670 VAR retvar;
4671{
4672 buf_T *buf;
4673 buf_T *save_curbuf;
4674 char_u *varname;
4675 VAR v;
4676
4677 ++emsg_off;
4678 buf = get_buf_var(&argvars[0]);
4679 varname = get_var_string(&argvars[1]);
4680
4681 retvar->var_type = VAR_STRING;
4682 retvar->var_val.var_string = NULL;
4683
4684 if (buf != NULL && varname != NULL)
4685 {
4686 if (*varname == '&') /* buffer-local-option */
4687 {
4688 /* set curbuf to be our buf, temporarily */
4689 save_curbuf = curbuf;
4690 curbuf = buf;
4691
4692 get_option_var(&varname, retvar, TRUE);
4693
4694 /* restore previous notion of curbuf */
4695 curbuf = save_curbuf;
4696 }
4697 else
4698 {
4699 /* look up the variable */
4700 v = find_var_in_ga(&buf->b_vars, varname);
4701 if (v != NULL)
4702 copy_var(v, retvar);
4703 }
4704 }
4705
4706 --emsg_off;
4707}
4708
4709/*
4710 * "getcwd()" function
4711 */
4712/*ARGSUSED*/
4713 static void
4714f_getcwd(argvars, retvar)
4715 VAR argvars;
4716 VAR retvar;
4717{
4718 char_u cwd[MAXPATHL];
4719
4720 retvar->var_type = VAR_STRING;
4721 if (mch_dirname(cwd, MAXPATHL) == FAIL)
4722 retvar->var_val.var_string = NULL;
4723 else
4724 {
4725 retvar->var_val.var_string = vim_strsave(cwd);
4726#ifdef BACKSLASH_IN_FILENAME
4727 slash_adjust(retvar->var_val.var_string);
4728#endif
4729 }
4730}
4731
4732/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004733 * "getfontname()" function
4734 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004735/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004736 static void
4737f_getfontname(argvars, retvar)
4738 VAR argvars;
4739 VAR retvar;
4740{
4741 retvar->var_type = VAR_STRING;
4742 retvar->var_val.var_string = NULL;
4743#ifdef FEAT_GUI
4744 if (gui.in_use)
4745 {
4746 GuiFont font;
4747 char_u *name = NULL;
4748
4749 if (argvars[0].var_type == VAR_UNKNOWN)
4750 {
4751 /* Get the "Normal" font. Either the name saved by
4752 * hl_set_font_name() or from the font ID. */
4753 font = gui.norm_font;
4754 name = hl_get_font_name();
4755 }
4756 else
4757 {
4758 name = get_var_string(&argvars[0]);
4759 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4760 return;
4761 font = gui_mch_get_font(name, FALSE);
4762 if (font == NOFONT)
4763 return; /* Invalid font name, return empty string. */
4764 }
4765 retvar->var_val.var_string = gui_mch_get_fontname(font, name);
4766 if (argvars[0].var_type != VAR_UNKNOWN)
4767 gui_mch_free_font(font);
4768 }
4769#endif
4770}
4771
4772/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004773 * "getfperm({fname})" function
4774 */
4775 static void
4776f_getfperm(argvars, retvar)
4777 VAR argvars;
4778 VAR retvar;
4779{
4780 char_u *fname;
4781 struct stat st;
4782 char_u *perm = NULL;
4783 char_u flags[] = "rwx";
4784 int i;
4785
4786 fname = get_var_string(&argvars[0]);
4787
4788 retvar->var_type = VAR_STRING;
4789 if (mch_stat((char *)fname, &st) >= 0)
4790 {
4791 perm = vim_strsave((char_u *)"---------");
4792 if (perm != NULL)
4793 {
4794 for (i = 0; i < 9; i++)
4795 {
4796 if (st.st_mode & (1 << (8 - i)))
4797 perm[i] = flags[i % 3];
4798 }
4799 }
4800 }
4801 retvar->var_val.var_string = perm;
4802}
4803
4804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 * "getfsize({fname})" function
4806 */
4807 static void
4808f_getfsize(argvars, retvar)
4809 VAR argvars;
4810 VAR retvar;
4811{
4812 char_u *fname;
4813 struct stat st;
4814
4815 fname = get_var_string(&argvars[0]);
4816
4817 retvar->var_type = VAR_NUMBER;
4818
4819 if (mch_stat((char *)fname, &st) >= 0)
4820 {
4821 if (mch_isdir(fname))
4822 retvar->var_val.var_number = 0;
4823 else
4824 retvar->var_val.var_number = (varnumber_T)st.st_size;
4825 }
4826 else
4827 retvar->var_val.var_number = -1;
4828}
4829
4830/*
4831 * "getftime({fname})" function
4832 */
4833 static void
4834f_getftime(argvars, retvar)
4835 VAR argvars;
4836 VAR retvar;
4837{
4838 char_u *fname;
4839 struct stat st;
4840
4841 fname = get_var_string(&argvars[0]);
4842
4843 if (mch_stat((char *)fname, &st) >= 0)
4844 retvar->var_val.var_number = (varnumber_T)st.st_mtime;
4845 else
4846 retvar->var_val.var_number = -1;
4847}
4848
4849/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004850 * "getftype({fname})" function
4851 */
4852 static void
4853f_getftype(argvars, retvar)
4854 VAR argvars;
4855 VAR retvar;
4856{
4857 char_u *fname;
4858 struct stat st;
4859 char_u *type = NULL;
4860 char *t;
4861
4862 fname = get_var_string(&argvars[0]);
4863
4864 retvar->var_type = VAR_STRING;
4865 if (mch_lstat((char *)fname, &st) >= 0)
4866 {
4867#ifdef S_ISREG
4868 if (S_ISREG(st.st_mode))
4869 t = "file";
4870 else if (S_ISDIR(st.st_mode))
4871 t = "dir";
4872# ifdef S_ISLNK
4873 else if (S_ISLNK(st.st_mode))
4874 t = "link";
4875# endif
4876# ifdef S_ISBLK
4877 else if (S_ISBLK(st.st_mode))
4878 t = "bdev";
4879# endif
4880# ifdef S_ISCHR
4881 else if (S_ISCHR(st.st_mode))
4882 t = "cdev";
4883# endif
4884# ifdef S_ISFIFO
4885 else if (S_ISFIFO(st.st_mode))
4886 t = "fifo";
4887# endif
4888# ifdef S_ISSOCK
4889 else if (S_ISSOCK(st.st_mode))
4890 t = "fifo";
4891# endif
4892 else
4893 t = "other";
4894#else
4895# ifdef S_IFMT
4896 switch (st.st_mode & S_IFMT)
4897 {
4898 case S_IFREG: t = "file"; break;
4899 case S_IFDIR: t = "dir"; break;
4900# ifdef S_IFLNK
4901 case S_IFLNK: t = "link"; break;
4902# endif
4903# ifdef S_IFBLK
4904 case S_IFBLK: t = "bdev"; break;
4905# endif
4906# ifdef S_IFCHR
4907 case S_IFCHR: t = "cdev"; break;
4908# endif
4909# ifdef S_IFIFO
4910 case S_IFIFO: t = "fifo"; break;
4911# endif
4912# ifdef S_IFSOCK
4913 case S_IFSOCK: t = "socket"; break;
4914# endif
4915 default: t = "other";
4916 }
4917# else
4918 if (mch_isdir(fname))
4919 t = "dir";
4920 else
4921 t = "file";
4922# endif
4923#endif
4924 type = vim_strsave((char_u *)t);
4925 }
4926 retvar->var_val.var_string = type;
4927}
4928
4929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 * "getreg()" function
4931 */
4932 static void
4933f_getreg(argvars, retvar)
4934 VAR argvars;
4935 VAR retvar;
4936{
4937 char_u *strregname;
4938 int regname;
4939
4940 if (argvars[0].var_type != VAR_UNKNOWN)
4941 strregname = get_var_string(&argvars[0]);
4942 else
4943 strregname = vimvars[VV_REG].val;
4944 regname = (strregname == NULL ? '"' : *strregname);
4945 if (regname == 0)
4946 regname = '"';
4947
4948 retvar->var_type = VAR_STRING;
4949 retvar->var_val.var_string = get_reg_contents(regname, TRUE);
4950}
4951
4952/*
4953 * "getregtype()" function
4954 */
4955 static void
4956f_getregtype(argvars, retvar)
4957 VAR argvars;
4958 VAR retvar;
4959{
4960 char_u *strregname;
4961 int regname;
4962 char_u buf[NUMBUFLEN + 2];
4963 long reglen = 0;
4964
4965 if (argvars[0].var_type != VAR_UNKNOWN)
4966 strregname = get_var_string(&argvars[0]);
4967 else
4968 /* Default to v:register */
4969 strregname = vimvars[VV_REG].val;
4970
4971 regname = (strregname == NULL ? '"' : *strregname);
4972 if (regname == 0)
4973 regname = '"';
4974
4975 buf[0] = NUL;
4976 buf[1] = NUL;
4977 switch (get_reg_type(regname, &reglen))
4978 {
4979 case MLINE: buf[0] = 'V'; break;
4980 case MCHAR: buf[0] = 'v'; break;
4981#ifdef FEAT_VISUAL
4982 case MBLOCK:
4983 buf[0] = Ctrl_V;
4984 sprintf((char *)buf + 1, "%ld", reglen + 1);
4985 break;
4986#endif
4987 }
4988 retvar->var_type = VAR_STRING;
4989 retvar->var_val.var_string = vim_strsave(buf);
4990}
4991
4992/*
4993 * "getline(lnum)" function
4994 */
4995 static void
4996f_getline(argvars, retvar)
4997 VAR argvars;
4998 VAR retvar;
4999{
5000 linenr_T lnum;
5001 char_u *p;
5002
5003 lnum = get_var_lnum(argvars);
5004
5005 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5006 p = ml_get(lnum);
5007 else
5008 p = (char_u *)"";
5009
5010 retvar->var_type = VAR_STRING;
5011 retvar->var_val.var_string = vim_strsave(p);
5012}
5013
5014/*
5015 * "getwinposx()" function
5016 */
5017/*ARGSUSED*/
5018 static void
5019f_getwinposx(argvars, retvar)
5020 VAR argvars;
5021 VAR retvar;
5022{
5023 retvar->var_val.var_number = -1;
5024#ifdef FEAT_GUI
5025 if (gui.in_use)
5026 {
5027 int x, y;
5028
5029 if (gui_mch_get_winpos(&x, &y) == OK)
5030 retvar->var_val.var_number = x;
5031 }
5032#endif
5033}
5034
5035/*
5036 * "getwinposy()" function
5037 */
5038/*ARGSUSED*/
5039 static void
5040f_getwinposy(argvars, retvar)
5041 VAR argvars;
5042 VAR retvar;
5043{
5044 retvar->var_val.var_number = -1;
5045#ifdef FEAT_GUI
5046 if (gui.in_use)
5047 {
5048 int x, y;
5049
5050 if (gui_mch_get_winpos(&x, &y) == OK)
5051 retvar->var_val.var_number = y;
5052 }
5053#endif
5054}
5055
5056/*
5057 * "getwinvar()" function
5058 */
5059 static void
5060f_getwinvar(argvars, retvar)
5061 VAR argvars;
5062 VAR retvar;
5063{
5064 win_T *win, *oldcurwin;
5065 char_u *varname;
5066 VAR v;
5067
5068 ++emsg_off;
5069 win = find_win_by_nr(&argvars[0]);
5070 varname = get_var_string(&argvars[1]);
5071
5072 retvar->var_type = VAR_STRING;
5073 retvar->var_val.var_string = NULL;
5074
5075 if (win != NULL && varname != NULL)
5076 {
5077 if (*varname == '&') /* window-local-option */
5078 {
5079 /* set curwin to be our win, temporarily */
5080 oldcurwin = curwin;
5081 curwin = win;
5082
5083 get_option_var(&varname, retvar , 1);
5084
5085 /* restore previous notion of curwin */
5086 curwin = oldcurwin;
5087 }
5088 else
5089 {
5090 /* look up the variable */
5091 v = find_var_in_ga(&win->w_vars, varname);
5092 if (v != NULL)
5093 copy_var(v, retvar);
5094 }
5095 }
5096
5097 --emsg_off;
5098}
5099
5100/*
5101 * "glob()" function
5102 */
5103 static void
5104f_glob(argvars, retvar)
5105 VAR argvars;
5106 VAR retvar;
5107{
5108 expand_T xpc;
5109
5110 ExpandInit(&xpc);
5111 xpc.xp_context = EXPAND_FILES;
5112 retvar->var_type = VAR_STRING;
5113 retvar->var_val.var_string = ExpandOne(&xpc, get_var_string(&argvars[0]),
5114 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
5115 ExpandCleanup(&xpc);
5116}
5117
5118/*
5119 * "globpath()" function
5120 */
5121 static void
5122f_globpath(argvars, retvar)
5123 VAR argvars;
5124 VAR retvar;
5125{
5126 char_u buf1[NUMBUFLEN];
5127
5128 retvar->var_type = VAR_STRING;
5129 retvar->var_val.var_string = globpath(get_var_string(&argvars[0]),
5130 get_var_string_buf(&argvars[1], buf1));
5131}
5132
5133/*
5134 * "has()" function
5135 */
5136 static void
5137f_has(argvars, retvar)
5138 VAR argvars;
5139 VAR retvar;
5140{
5141 int i;
5142 char_u *name;
5143 int n = FALSE;
5144 static char *(has_list[]) =
5145 {
5146#ifdef AMIGA
5147 "amiga",
5148# ifdef FEAT_ARP
5149 "arp",
5150# endif
5151#endif
5152#ifdef __BEOS__
5153 "beos",
5154#endif
5155#ifdef MSDOS
5156# ifdef DJGPP
5157 "dos32",
5158# else
5159 "dos16",
5160# endif
5161#endif
5162#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
5163 "mac",
5164#endif
5165#if defined(MACOS_X_UNIX)
5166 "macunix",
5167#endif
5168#ifdef OS2
5169 "os2",
5170#endif
5171#ifdef __QNX__
5172 "qnx",
5173#endif
5174#ifdef RISCOS
5175 "riscos",
5176#endif
5177#ifdef UNIX
5178 "unix",
5179#endif
5180#ifdef VMS
5181 "vms",
5182#endif
5183#ifdef WIN16
5184 "win16",
5185#endif
5186#ifdef WIN32
5187 "win32",
5188#endif
5189#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5190 "win32unix",
5191#endif
5192#ifdef WIN64
5193 "win64",
5194#endif
5195#ifdef EBCDIC
5196 "ebcdic",
5197#endif
5198#ifndef CASE_INSENSITIVE_FILENAME
5199 "fname_case",
5200#endif
5201#ifdef FEAT_ARABIC
5202 "arabic",
5203#endif
5204#ifdef FEAT_AUTOCMD
5205 "autocmd",
5206#endif
5207#ifdef FEAT_BEVAL
5208 "balloon_eval",
5209#endif
5210#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5211 "builtin_terms",
5212# ifdef ALL_BUILTIN_TCAPS
5213 "all_builtin_terms",
5214# endif
5215#endif
5216#ifdef FEAT_BYTEOFF
5217 "byte_offset",
5218#endif
5219#ifdef FEAT_CINDENT
5220 "cindent",
5221#endif
5222#ifdef FEAT_CLIENTSERVER
5223 "clientserver",
5224#endif
5225#ifdef FEAT_CLIPBOARD
5226 "clipboard",
5227#endif
5228#ifdef FEAT_CMDL_COMPL
5229 "cmdline_compl",
5230#endif
5231#ifdef FEAT_CMDHIST
5232 "cmdline_hist",
5233#endif
5234#ifdef FEAT_COMMENTS
5235 "comments",
5236#endif
5237#ifdef FEAT_CRYPT
5238 "cryptv",
5239#endif
5240#ifdef FEAT_CSCOPE
5241 "cscope",
5242#endif
5243#ifdef DEBUG
5244 "debug",
5245#endif
5246#ifdef FEAT_CON_DIALOG
5247 "dialog_con",
5248#endif
5249#ifdef FEAT_GUI_DIALOG
5250 "dialog_gui",
5251#endif
5252#ifdef FEAT_DIFF
5253 "diff",
5254#endif
5255#ifdef FEAT_DIGRAPHS
5256 "digraphs",
5257#endif
5258#ifdef FEAT_DND
5259 "dnd",
5260#endif
5261#ifdef FEAT_EMACS_TAGS
5262 "emacs_tags",
5263#endif
5264 "eval", /* always present, of course! */
5265#ifdef FEAT_EX_EXTRA
5266 "ex_extra",
5267#endif
5268#ifdef FEAT_SEARCH_EXTRA
5269 "extra_search",
5270#endif
5271#ifdef FEAT_FKMAP
5272 "farsi",
5273#endif
5274#ifdef FEAT_SEARCHPATH
5275 "file_in_path",
5276#endif
5277#ifdef FEAT_FIND_ID
5278 "find_in_path",
5279#endif
5280#ifdef FEAT_FOLDING
5281 "folding",
5282#endif
5283#ifdef FEAT_FOOTER
5284 "footer",
5285#endif
5286#if !defined(USE_SYSTEM) && defined(UNIX)
5287 "fork",
5288#endif
5289#ifdef FEAT_GETTEXT
5290 "gettext",
5291#endif
5292#ifdef FEAT_GUI
5293 "gui",
5294#endif
5295#ifdef FEAT_GUI_ATHENA
5296# ifdef FEAT_GUI_NEXTAW
5297 "gui_neXtaw",
5298# else
5299 "gui_athena",
5300# endif
5301#endif
5302#ifdef FEAT_GUI_BEOS
5303 "gui_beos",
5304#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00005305#ifdef FEAT_GUI_KDE
5306 "gui_kde",
5307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#ifdef FEAT_GUI_GTK
5309 "gui_gtk",
5310# ifdef HAVE_GTK2
5311 "gui_gtk2",
5312# endif
5313#endif
5314#ifdef FEAT_GUI_MAC
5315 "gui_mac",
5316#endif
5317#ifdef FEAT_GUI_MOTIF
5318 "gui_motif",
5319#endif
5320#ifdef FEAT_GUI_PHOTON
5321 "gui_photon",
5322#endif
5323#ifdef FEAT_GUI_W16
5324 "gui_win16",
5325#endif
5326#ifdef FEAT_GUI_W32
5327 "gui_win32",
5328#endif
5329#ifdef FEAT_HANGULIN
5330 "hangul_input",
5331#endif
5332#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5333 "iconv",
5334#endif
5335#ifdef FEAT_INS_EXPAND
5336 "insert_expand",
5337#endif
5338#ifdef FEAT_JUMPLIST
5339 "jumplist",
5340#endif
5341#ifdef FEAT_KEYMAP
5342 "keymap",
5343#endif
5344#ifdef FEAT_LANGMAP
5345 "langmap",
5346#endif
5347#ifdef FEAT_LIBCALL
5348 "libcall",
5349#endif
5350#ifdef FEAT_LINEBREAK
5351 "linebreak",
5352#endif
5353#ifdef FEAT_LISP
5354 "lispindent",
5355#endif
5356#ifdef FEAT_LISTCMDS
5357 "listcmds",
5358#endif
5359#ifdef FEAT_LOCALMAP
5360 "localmap",
5361#endif
5362#ifdef FEAT_MENU
5363 "menu",
5364#endif
5365#ifdef FEAT_SESSION
5366 "mksession",
5367#endif
5368#ifdef FEAT_MODIFY_FNAME
5369 "modify_fname",
5370#endif
5371#ifdef FEAT_MOUSE
5372 "mouse",
5373#endif
5374#ifdef FEAT_MOUSESHAPE
5375 "mouseshape",
5376#endif
5377#if defined(UNIX) || defined(VMS)
5378# ifdef FEAT_MOUSE_DEC
5379 "mouse_dec",
5380# endif
5381# ifdef FEAT_MOUSE_GPM
5382 "mouse_gpm",
5383# endif
5384# ifdef FEAT_MOUSE_JSB
5385 "mouse_jsbterm",
5386# endif
5387# ifdef FEAT_MOUSE_NET
5388 "mouse_netterm",
5389# endif
5390# ifdef FEAT_MOUSE_PTERM
5391 "mouse_pterm",
5392# endif
5393# ifdef FEAT_MOUSE_XTERM
5394 "mouse_xterm",
5395# endif
5396#endif
5397#ifdef FEAT_MBYTE
5398 "multi_byte",
5399#endif
5400#ifdef FEAT_MBYTE_IME
5401 "multi_byte_ime",
5402#endif
5403#ifdef FEAT_MULTI_LANG
5404 "multi_lang",
5405#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005406#ifdef FEAT_MZSCHEME
5407 "mzscheme",
5408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#ifdef FEAT_OLE
5410 "ole",
5411#endif
5412#ifdef FEAT_OSFILETYPE
5413 "osfiletype",
5414#endif
5415#ifdef FEAT_PATH_EXTRA
5416 "path_extra",
5417#endif
5418#ifdef FEAT_PERL
5419#ifndef DYNAMIC_PERL
5420 "perl",
5421#endif
5422#endif
5423#ifdef FEAT_PYTHON
5424#ifndef DYNAMIC_PYTHON
5425 "python",
5426#endif
5427#endif
5428#ifdef FEAT_POSTSCRIPT
5429 "postscript",
5430#endif
5431#ifdef FEAT_PRINTER
5432 "printer",
5433#endif
5434#ifdef FEAT_QUICKFIX
5435 "quickfix",
5436#endif
5437#ifdef FEAT_RIGHTLEFT
5438 "rightleft",
5439#endif
5440#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5441 "ruby",
5442#endif
5443#ifdef FEAT_SCROLLBIND
5444 "scrollbind",
5445#endif
5446#ifdef FEAT_CMDL_INFO
5447 "showcmd",
5448 "cmdline_info",
5449#endif
5450#ifdef FEAT_SIGNS
5451 "signs",
5452#endif
5453#ifdef FEAT_SMARTINDENT
5454 "smartindent",
5455#endif
5456#ifdef FEAT_SNIFF
5457 "sniff",
5458#endif
5459#ifdef FEAT_STL_OPT
5460 "statusline",
5461#endif
5462#ifdef FEAT_SUN_WORKSHOP
5463 "sun_workshop",
5464#endif
5465#ifdef FEAT_NETBEANS_INTG
5466 "netbeans_intg",
5467#endif
5468#ifdef FEAT_SYN_HL
5469 "syntax",
5470#endif
5471#if defined(USE_SYSTEM) || !defined(UNIX)
5472 "system",
5473#endif
5474#ifdef FEAT_TAG_BINS
5475 "tag_binary",
5476#endif
5477#ifdef FEAT_TAG_OLDSTATIC
5478 "tag_old_static",
5479#endif
5480#ifdef FEAT_TAG_ANYWHITE
5481 "tag_any_white",
5482#endif
5483#ifdef FEAT_TCL
5484# ifndef DYNAMIC_TCL
5485 "tcl",
5486# endif
5487#endif
5488#ifdef TERMINFO
5489 "terminfo",
5490#endif
5491#ifdef FEAT_TERMRESPONSE
5492 "termresponse",
5493#endif
5494#ifdef FEAT_TEXTOBJ
5495 "textobjects",
5496#endif
5497#ifdef HAVE_TGETENT
5498 "tgetent",
5499#endif
5500#ifdef FEAT_TITLE
5501 "title",
5502#endif
5503#ifdef FEAT_TOOLBAR
5504 "toolbar",
5505#endif
5506#ifdef FEAT_USR_CMDS
5507 "user-commands", /* was accidentally included in 5.4 */
5508 "user_commands",
5509#endif
5510#ifdef FEAT_VIMINFO
5511 "viminfo",
5512#endif
5513#ifdef FEAT_VERTSPLIT
5514 "vertsplit",
5515#endif
5516#ifdef FEAT_VIRTUALEDIT
5517 "virtualedit",
5518#endif
5519#ifdef FEAT_VISUAL
5520 "visual",
5521#endif
5522#ifdef FEAT_VISUALEXTRA
5523 "visualextra",
5524#endif
5525#ifdef FEAT_VREPLACE
5526 "vreplace",
5527#endif
5528#ifdef FEAT_WILDIGN
5529 "wildignore",
5530#endif
5531#ifdef FEAT_WILDMENU
5532 "wildmenu",
5533#endif
5534#ifdef FEAT_WINDOWS
5535 "windows",
5536#endif
5537#ifdef FEAT_WAK
5538 "winaltkeys",
5539#endif
5540#ifdef FEAT_WRITEBACKUP
5541 "writebackup",
5542#endif
5543#ifdef FEAT_XIM
5544 "xim",
5545#endif
5546#ifdef FEAT_XFONTSET
5547 "xfontset",
5548#endif
5549#ifdef USE_XSMP
5550 "xsmp",
5551#endif
5552#ifdef USE_XSMP_INTERACT
5553 "xsmp_interact",
5554#endif
5555#ifdef FEAT_XCLIPBOARD
5556 "xterm_clipboard",
5557#endif
5558#ifdef FEAT_XTERM_SAVE
5559 "xterm_save",
5560#endif
5561#if defined(UNIX) && defined(FEAT_X11)
5562 "X11",
5563#endif
5564 NULL
5565 };
5566
5567 name = get_var_string(&argvars[0]);
5568 for (i = 0; has_list[i] != NULL; ++i)
5569 if (STRICMP(name, has_list[i]) == 0)
5570 {
5571 n = TRUE;
5572 break;
5573 }
5574
5575 if (n == FALSE)
5576 {
5577 if (STRNICMP(name, "patch", 5) == 0)
5578 n = has_patch(atoi((char *)name + 5));
5579 else if (STRICMP(name, "vim_starting") == 0)
5580 n = (starting != 0);
5581#ifdef DYNAMIC_TCL
5582 else if (STRICMP(name, "tcl") == 0)
5583 n = tcl_enabled(FALSE);
5584#endif
5585#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5586 else if (STRICMP(name, "iconv") == 0)
5587 n = iconv_enabled(FALSE);
5588#endif
5589#ifdef DYNAMIC_RUBY
5590 else if (STRICMP(name, "ruby") == 0)
5591 n = ruby_enabled(FALSE);
5592#endif
5593#ifdef DYNAMIC_PYTHON
5594 else if (STRICMP(name, "python") == 0)
5595 n = python_enabled(FALSE);
5596#endif
5597#ifdef DYNAMIC_PERL
5598 else if (STRICMP(name, "perl") == 0)
5599 n = perl_enabled(FALSE);
5600#endif
5601#ifdef FEAT_GUI
5602 else if (STRICMP(name, "gui_running") == 0)
5603 n = (gui.in_use || gui.starting);
5604# ifdef FEAT_GUI_W32
5605 else if (STRICMP(name, "gui_win32s") == 0)
5606 n = gui_is_win32s();
5607# endif
5608# ifdef FEAT_BROWSE
5609 else if (STRICMP(name, "browse") == 0)
5610 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
5611# endif
5612#endif
5613#ifdef FEAT_SYN_HL
5614 else if (STRICMP(name, "syntax_items") == 0)
5615 n = syntax_present(curbuf);
5616#endif
5617#if defined(WIN3264)
5618 else if (STRICMP(name, "win95") == 0)
5619 n = mch_windows95();
5620#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00005621#ifdef FEAT_NETBEANS_INTG
5622 else if (STRICMP(name, "netbeans_enabled") == 0)
5623 n = usingNetbeans;
5624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626
5627 retvar->var_val.var_number = n;
5628}
5629
5630/*
5631 * "hasmapto()" function
5632 */
5633 static void
5634f_hasmapto(argvars, retvar)
5635 VAR argvars;
5636 VAR retvar;
5637{
5638 char_u *name;
5639 char_u *mode;
5640 char_u buf[NUMBUFLEN];
5641
5642 name = get_var_string(&argvars[0]);
5643 if (argvars[1].var_type == VAR_UNKNOWN)
5644 mode = (char_u *)"nvo";
5645 else
5646 mode = get_var_string_buf(&argvars[1], buf);
5647
5648 if (map_to_exists(name, mode))
5649 retvar->var_val.var_number = TRUE;
5650 else
5651 retvar->var_val.var_number = FALSE;
5652}
5653
5654/*
5655 * "histadd()" function
5656 */
5657/*ARGSUSED*/
5658 static void
5659f_histadd(argvars, retvar)
5660 VAR argvars;
5661 VAR retvar;
5662{
5663#ifdef FEAT_CMDHIST
5664 int histype;
5665 char_u *str;
5666 char_u buf[NUMBUFLEN];
5667#endif
5668
5669 retvar->var_val.var_number = FALSE;
5670 if (check_restricted() || check_secure())
5671 return;
5672#ifdef FEAT_CMDHIST
5673 histype = get_histtype(get_var_string(&argvars[0]));
5674 if (histype >= 0)
5675 {
5676 str = get_var_string_buf(&argvars[1], buf);
5677 if (*str != NUL)
5678 {
5679 add_to_history(histype, str, FALSE, NUL);
5680 retvar->var_val.var_number = TRUE;
5681 return;
5682 }
5683 }
5684#endif
5685}
5686
5687/*
5688 * "histdel()" function
5689 */
5690/*ARGSUSED*/
5691 static void
5692f_histdel(argvars, retvar)
5693 VAR argvars;
5694 VAR retvar;
5695{
5696#ifdef FEAT_CMDHIST
5697 int n;
5698 char_u buf[NUMBUFLEN];
5699
5700 if (argvars[1].var_type == VAR_UNKNOWN)
5701 /* only one argument: clear entire history */
5702 n = clr_history(get_histtype(get_var_string(&argvars[0])));
5703 else if (argvars[1].var_type == VAR_NUMBER)
5704 /* index given: remove that entry */
5705 n = del_history_idx(get_histtype(get_var_string(&argvars[0])),
5706 (int)get_var_number(&argvars[1]));
5707 else
5708 /* string given: remove all matching entries */
5709 n = del_history_entry(get_histtype(get_var_string(&argvars[0])),
5710 get_var_string_buf(&argvars[1], buf));
5711 retvar->var_val.var_number = n;
5712#else
5713 retvar->var_val.var_number = 0;
5714#endif
5715}
5716
5717/*
5718 * "histget()" function
5719 */
5720/*ARGSUSED*/
5721 static void
5722f_histget(argvars, retvar)
5723 VAR argvars;
5724 VAR retvar;
5725{
5726#ifdef FEAT_CMDHIST
5727 int type;
5728 int idx;
5729
5730 type = get_histtype(get_var_string(&argvars[0]));
5731 if (argvars[1].var_type == VAR_UNKNOWN)
5732 idx = get_history_idx(type);
5733 else
5734 idx = (int)get_var_number(&argvars[1]);
5735 retvar->var_val.var_string = vim_strsave(get_history_entry(type, idx));
5736#else
5737 retvar->var_val.var_string = NULL;
5738#endif
5739 retvar->var_type = VAR_STRING;
5740}
5741
5742/*
5743 * "histnr()" function
5744 */
5745/*ARGSUSED*/
5746 static void
5747f_histnr(argvars, retvar)
5748 VAR argvars;
5749 VAR retvar;
5750{
5751 int i;
5752
5753#ifdef FEAT_CMDHIST
5754 i = get_histtype(get_var_string(&argvars[0]));
5755 if (i >= HIST_CMD && i < HIST_COUNT)
5756 i = get_history_idx(i);
5757 else
5758#endif
5759 i = -1;
5760 retvar->var_val.var_number = i;
5761}
5762
5763/*
5764 * "highlight_exists()" function
5765 */
5766 static void
5767f_hlexists(argvars, retvar)
5768 VAR argvars;
5769 VAR retvar;
5770{
5771 retvar->var_val.var_number = highlight_exists(get_var_string(&argvars[0]));
5772}
5773
5774/*
5775 * "highlightID(name)" function
5776 */
5777 static void
5778f_hlID(argvars, retvar)
5779 VAR argvars;
5780 VAR retvar;
5781{
5782 retvar->var_val.var_number = syn_name2id(get_var_string(&argvars[0]));
5783}
5784
5785/*
5786 * "hostname()" function
5787 */
5788/*ARGSUSED*/
5789 static void
5790f_hostname(argvars, retvar)
5791 VAR argvars;
5792 VAR retvar;
5793{
5794 char_u hostname[256];
5795
5796 mch_get_host_name(hostname, 256);
5797 retvar->var_type = VAR_STRING;
5798 retvar->var_val.var_string = vim_strsave(hostname);
5799}
5800
5801/*
5802 * iconv() function
5803 */
5804/*ARGSUSED*/
5805 static void
5806f_iconv(argvars, retvar)
5807 VAR argvars;
5808 VAR retvar;
5809{
5810#ifdef FEAT_MBYTE
5811 char_u buf1[NUMBUFLEN];
5812 char_u buf2[NUMBUFLEN];
5813 char_u *from, *to, *str;
5814 vimconv_T vimconv;
5815#endif
5816
5817 retvar->var_type = VAR_STRING;
5818 retvar->var_val.var_string = NULL;
5819
5820#ifdef FEAT_MBYTE
5821 str = get_var_string(&argvars[0]);
5822 from = enc_canonize(enc_skip(get_var_string_buf(&argvars[1], buf1)));
5823 to = enc_canonize(enc_skip(get_var_string_buf(&argvars[2], buf2)));
5824 vimconv.vc_type = CONV_NONE;
5825 convert_setup(&vimconv, from, to);
5826
5827 /* If the encodings are equal, no conversion needed. */
5828 if (vimconv.vc_type == CONV_NONE)
5829 retvar->var_val.var_string = vim_strsave(str);
5830 else
5831 retvar->var_val.var_string = string_convert(&vimconv, str, NULL);
5832
5833 convert_setup(&vimconv, NULL, NULL);
5834 vim_free(from);
5835 vim_free(to);
5836#endif
5837}
5838
5839/*
5840 * "indent()" function
5841 */
5842 static void
5843f_indent(argvars, retvar)
5844 VAR argvars;
5845 VAR retvar;
5846{
5847 linenr_T lnum;
5848
5849 lnum = get_var_lnum(argvars);
5850 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5851 retvar->var_val.var_number = get_indent_lnum(lnum);
5852 else
5853 retvar->var_val.var_number = -1;
5854}
5855
5856static int inputsecret_flag = 0;
5857
5858/*
5859 * "input()" function
5860 * Also handles inputsecret() when inputsecret is set.
5861 */
5862 static void
5863f_input(argvars, retvar)
5864 VAR argvars;
5865 VAR retvar;
5866{
5867 char_u *prompt = get_var_string(&argvars[0]);
5868 char_u *p = NULL;
5869 int c;
5870 char_u buf[NUMBUFLEN];
5871 int cmd_silent_save = cmd_silent;
5872
5873 retvar->var_type = VAR_STRING;
5874
5875#ifdef NO_CONSOLE_INPUT
5876 /* While starting up, there is no place to enter text. */
5877 if (no_console_input())
5878 {
5879 retvar->var_val.var_string = NULL;
5880 return;
5881 }
5882#endif
5883
5884 cmd_silent = FALSE; /* Want to see the prompt. */
5885 if (prompt != NULL)
5886 {
5887 /* Only the part of the message after the last NL is considered as
5888 * prompt for the command line */
5889 p = vim_strrchr(prompt, '\n');
5890 if (p == NULL)
5891 p = prompt;
5892 else
5893 {
5894 ++p;
5895 c = *p;
5896 *p = NUL;
5897 msg_start();
5898 msg_clr_eos();
5899 msg_puts_attr(prompt, echo_attr);
5900 msg_didout = FALSE;
5901 msg_starthere();
5902 *p = c;
5903 }
5904 cmdline_row = msg_row;
5905 }
5906
5907 if (argvars[1].var_type != VAR_UNKNOWN)
5908 stuffReadbuffSpec(get_var_string_buf(&argvars[1], buf));
5909
5910 retvar->var_val.var_string =
5911 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
5912
5913 /* since the user typed this, no need to wait for return */
5914 need_wait_return = FALSE;
5915 msg_didout = FALSE;
5916 cmd_silent = cmd_silent_save;
5917}
5918
5919/*
5920 * "inputdialog()" function
5921 */
5922 static void
5923f_inputdialog(argvars, retvar)
5924 VAR argvars;
5925 VAR retvar;
5926{
5927#if defined(FEAT_GUI_TEXTDIALOG)
5928 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
5929 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5930 {
5931 char_u *message;
5932 char_u buf[NUMBUFLEN];
5933
5934 message = get_var_string(&argvars[0]);
5935 if (argvars[1].var_type != VAR_UNKNOWN)
5936 {
5937 STRNCPY(IObuff, get_var_string_buf(&argvars[1], buf), IOSIZE);
5938 IObuff[IOSIZE - 1] = NUL;
5939 }
5940 else
5941 IObuff[0] = NUL;
5942 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
5943 1, IObuff) == 1)
5944 retvar->var_val.var_string = vim_strsave(IObuff);
5945 else
5946 {
5947 if (argvars[1].var_type != VAR_UNKNOWN
5948 && argvars[2].var_type != VAR_UNKNOWN)
5949 retvar->var_val.var_string = vim_strsave(
5950 get_var_string_buf(&argvars[2], buf));
5951 else
5952 retvar->var_val.var_string = NULL;
5953 }
5954 retvar->var_type = VAR_STRING;
5955 }
5956 else
5957#endif
5958 f_input(argvars, retvar);
5959}
5960
5961static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5962
5963/*
5964 * "inputrestore()" function
5965 */
5966/*ARGSUSED*/
5967 static void
5968f_inputrestore(argvars, retvar)
5969 VAR argvars;
5970 VAR retvar;
5971{
5972 if (ga_userinput.ga_len > 0)
5973 {
5974 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5976 + ga_userinput.ga_len);
5977 retvar->var_val.var_number = 0; /* OK */
5978 }
5979 else if (p_verbose > 1)
5980 {
5981 msg((char_u *)_("called inputrestore() more often than inputsave()"));
5982 retvar->var_val.var_number = 1; /* Failed */
5983 }
5984}
5985
5986/*
5987 * "inputsave()" function
5988 */
5989/*ARGSUSED*/
5990 static void
5991f_inputsave(argvars, retvar)
5992 VAR argvars;
5993 VAR retvar;
5994{
5995 /* Add an entry to the stack of typehead storage. */
5996 if (ga_grow(&ga_userinput, 1) == OK)
5997 {
5998 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5999 + ga_userinput.ga_len);
6000 ++ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 retvar->var_val.var_number = 0; /* OK */
6002 }
6003 else
6004 retvar->var_val.var_number = 1; /* Failed */
6005}
6006
6007/*
6008 * "inputsecret()" function
6009 */
6010 static void
6011f_inputsecret(argvars, retvar)
6012 VAR argvars;
6013 VAR retvar;
6014{
6015 ++cmdline_star;
6016 ++inputsecret_flag;
6017 f_input(argvars, retvar);
6018 --cmdline_star;
6019 --inputsecret_flag;
6020}
6021
6022/*
6023 * "isdirectory()" function
6024 */
6025 static void
6026f_isdirectory(argvars, retvar)
6027 VAR argvars;
6028 VAR retvar;
6029{
6030 retvar->var_val.var_number = mch_isdir(get_var_string(&argvars[0]));
6031}
6032
6033/*
6034 * "last_buffer_nr()" function.
6035 */
6036/*ARGSUSED*/
6037 static void
6038f_last_buffer_nr(argvars, retvar)
6039 VAR argvars;
6040 VAR retvar;
6041{
6042 int n = 0;
6043 buf_T *buf;
6044
6045 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6046 if (n < buf->b_fnum)
6047 n = buf->b_fnum;
6048
6049 retvar->var_val.var_number = n;
6050}
6051
6052/*
6053 * "line(string)" function
6054 */
6055 static void
6056f_line(argvars, retvar)
6057 VAR argvars;
6058 VAR retvar;
6059{
6060 linenr_T lnum = 0;
6061 pos_T *fp;
6062
6063 fp = var2fpos(&argvars[0], TRUE);
6064 if (fp != NULL)
6065 lnum = fp->lnum;
6066 retvar->var_val.var_number = lnum;
6067}
6068
6069/*
6070 * "line2byte(lnum)" function
6071 */
6072/*ARGSUSED*/
6073 static void
6074f_line2byte(argvars, retvar)
6075 VAR argvars;
6076 VAR retvar;
6077{
6078#ifndef FEAT_BYTEOFF
6079 retvar->var_val.var_number = -1;
6080#else
6081 linenr_T lnum;
6082
6083 lnum = get_var_lnum(argvars);
6084 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6085 retvar->var_val.var_number = -1;
6086 else
6087 retvar->var_val.var_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6088 if (retvar->var_val.var_number >= 0)
6089 ++retvar->var_val.var_number;
6090#endif
6091}
6092
6093/*
6094 * "lispindent(lnum)" function
6095 */
6096 static void
6097f_lispindent(argvars, retvar)
6098 VAR argvars;
6099 VAR retvar;
6100{
6101#ifdef FEAT_LISP
6102 pos_T pos;
6103 linenr_T lnum;
6104
6105 pos = curwin->w_cursor;
6106 lnum = get_var_lnum(argvars);
6107 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6108 {
6109 curwin->w_cursor.lnum = lnum;
6110 retvar->var_val.var_number = get_lisp_indent();
6111 curwin->w_cursor = pos;
6112 }
6113 else
6114#endif
6115 retvar->var_val.var_number = -1;
6116}
6117
6118/*
6119 * "localtime()" function
6120 */
6121/*ARGSUSED*/
6122 static void
6123f_localtime(argvars, retvar)
6124 VAR argvars;
6125 VAR retvar;
6126{
6127 retvar->var_val.var_number = (varnumber_T)time(NULL);
6128}
6129
6130/*
6131 * "maparg()" function
6132 */
6133 static void
6134f_maparg(argvars, retvar)
6135 VAR argvars;
6136 VAR retvar;
6137{
6138 get_maparg(argvars, retvar, TRUE);
6139}
6140
6141/*
6142 * "mapcheck()" function
6143 */
6144 static void
6145f_mapcheck(argvars, retvar)
6146 VAR argvars;
6147 VAR retvar;
6148{
6149 get_maparg(argvars, retvar, FALSE);
6150}
6151
6152 static void
6153get_maparg(argvars, retvar, exact)
6154 VAR argvars;
6155 VAR retvar;
6156 int exact;
6157{
6158 char_u *keys;
6159 char_u *which;
6160 char_u buf[NUMBUFLEN];
6161 char_u *keys_buf = NULL;
6162 char_u *rhs;
6163 int mode;
6164 garray_T ga;
6165
6166 /* return empty string for failure */
6167 retvar->var_type = VAR_STRING;
6168 retvar->var_val.var_string = NULL;
6169
6170 keys = get_var_string(&argvars[0]);
6171 if (*keys == NUL)
6172 return;
6173
6174 if (argvars[1].var_type != VAR_UNKNOWN)
6175 which = get_var_string_buf(&argvars[1], buf);
6176 else
6177 which = (char_u *)"";
6178 mode = get_map_mode(&which, 0);
6179
6180 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
6181 rhs = check_map(keys, mode, exact);
6182 vim_free(keys_buf);
6183 if (rhs != NULL)
6184 {
6185 ga_init(&ga);
6186 ga.ga_itemsize = 1;
6187 ga.ga_growsize = 40;
6188
6189 while (*rhs != NUL)
6190 ga_concat(&ga, str2special(&rhs, FALSE));
6191
6192 ga_append(&ga, NUL);
6193 retvar->var_val.var_string = (char_u *)ga.ga_data;
6194 }
6195}
6196
6197/*
6198 * "match()" function
6199 */
6200 static void
6201f_match(argvars, retvar)
6202 VAR argvars;
6203 VAR retvar;
6204{
6205 find_some_match(argvars, retvar, 1);
6206}
6207
6208/*
6209 * "matchend()" function
6210 */
6211 static void
6212f_matchend(argvars, retvar)
6213 VAR argvars;
6214 VAR retvar;
6215{
6216 find_some_match(argvars, retvar, 0);
6217}
6218
6219/*
6220 * "matchstr()" function
6221 */
6222 static void
6223f_matchstr(argvars, retvar)
6224 VAR argvars;
6225 VAR retvar;
6226{
6227 find_some_match(argvars, retvar, 2);
6228}
6229
6230 static void
6231find_some_match(argvars, retvar, type)
6232 VAR argvars;
6233 VAR retvar;
6234 int type;
6235{
6236 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006237 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 char_u *pat;
6239 regmatch_T regmatch;
6240 char_u patbuf[NUMBUFLEN];
6241 char_u *save_cpo;
6242 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006243 long nth = 1;
6244 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6247 save_cpo = p_cpo;
6248 p_cpo = (char_u *)"";
6249
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006250 expr = str = get_var_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 pat = get_var_string_buf(&argvars[1], patbuf);
6252
6253 if (type == 2)
6254 {
6255 retvar->var_type = VAR_STRING;
6256 retvar->var_val.var_string = NULL;
6257 }
6258 else
6259 retvar->var_val.var_number = -1;
6260
6261 if (argvars[2].var_type != VAR_UNKNOWN)
6262 {
6263 start = get_var_number(&argvars[2]);
6264 if (start < 0)
6265 start = 0;
6266 if (start > (long)STRLEN(str))
6267 goto theend;
6268 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006269
6270 if (argvars[3].var_type != VAR_UNKNOWN)
6271 nth = get_var_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273
6274 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6275 if (regmatch.regprog != NULL)
6276 {
6277 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006278
6279 while (1)
6280 {
6281 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
6282 if (!match || --nth <= 0)
6283 break;
6284 /* Advance to just after the match. */
6285#ifdef FEAT_MBYTE
6286 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
6287#else
6288 str = regmatch.startp[0] + 1;
6289#endif
6290 }
6291
6292 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294 if (type == 2)
6295 retvar->var_val.var_string = vim_strnsave(regmatch.startp[0],
6296 (int)(regmatch.endp[0] - regmatch.startp[0]));
6297 else
6298 {
6299 if (type != 0)
6300 retvar->var_val.var_number =
6301 (varnumber_T)(regmatch.startp[0] - str);
6302 else
6303 retvar->var_val.var_number =
6304 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006305 retvar->var_val.var_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307 }
6308 vim_free(regmatch.regprog);
6309 }
6310
6311theend:
6312 p_cpo = save_cpo;
6313}
6314
6315/*
6316 * "mode()" function
6317 */
6318/*ARGSUSED*/
6319 static void
6320f_mode(argvars, retvar)
6321 VAR argvars;
6322 VAR retvar;
6323{
6324 char_u buf[2];
6325
6326#ifdef FEAT_VISUAL
6327 if (VIsual_active)
6328 {
6329 if (VIsual_select)
6330 buf[0] = VIsual_mode + 's' - 'v';
6331 else
6332 buf[0] = VIsual_mode;
6333 }
6334 else
6335#endif
6336 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
6337 buf[0] = 'r';
6338 else if (State & INSERT)
6339 {
6340 if (State & REPLACE_FLAG)
6341 buf[0] = 'R';
6342 else
6343 buf[0] = 'i';
6344 }
6345 else if (State & CMDLINE)
6346 buf[0] = 'c';
6347 else
6348 buf[0] = 'n';
6349
6350 buf[1] = NUL;
6351 retvar->var_val.var_string = vim_strsave(buf);
6352 retvar->var_type = VAR_STRING;
6353}
6354
6355/*
6356 * "nr2char()" function
6357 */
6358 static void
6359f_nr2char(argvars, retvar)
6360 VAR argvars;
6361 VAR retvar;
6362{
6363 char_u buf[NUMBUFLEN];
6364
6365#ifdef FEAT_MBYTE
6366 if (has_mbyte)
6367 buf[(*mb_char2bytes)((int)get_var_number(&argvars[0]), buf)] = NUL;
6368 else
6369#endif
6370 {
6371 buf[0] = (char_u)get_var_number(&argvars[0]);
6372 buf[1] = NUL;
6373 }
6374 retvar->var_type = VAR_STRING;
6375 retvar->var_val.var_string = vim_strsave(buf);
6376}
6377
6378/*
6379 * "rename({from}, {to})" function
6380 */
6381 static void
6382f_rename(argvars, retvar)
6383 VAR argvars;
6384 VAR retvar;
6385{
6386 char_u buf[NUMBUFLEN];
6387
6388 if (check_restricted() || check_secure())
6389 retvar->var_val.var_number = -1;
6390 else
6391 retvar->var_val.var_number = vim_rename(get_var_string(&argvars[0]),
6392 get_var_string_buf(&argvars[1], buf));
6393}
6394
6395/*
6396 * "resolve()" function
6397 */
6398 static void
6399f_resolve(argvars, retvar)
6400 VAR argvars;
6401 VAR retvar;
6402{
6403 char_u *p;
6404
6405 p = get_var_string(&argvars[0]);
6406#ifdef FEAT_SHORTCUT
6407 {
6408 char_u *v = NULL;
6409
6410 v = mch_resolve_shortcut(p);
6411 if (v != NULL)
6412 retvar->var_val.var_string = v;
6413 else
6414 retvar->var_val.var_string = vim_strsave(p);
6415 }
6416#else
6417# ifdef HAVE_READLINK
6418 {
6419 char_u buf[MAXPATHL + 1];
6420 char_u *cpy;
6421 int len;
6422 char_u *remain = NULL;
6423 char_u *q;
6424 int is_relative_to_current = FALSE;
6425 int has_trailing_pathsep = FALSE;
6426 int limit = 100;
6427
6428 p = vim_strsave(p);
6429
6430 if (p[0] == '.' && (vim_ispathsep(p[1])
6431 || (p[1] == '.' && (vim_ispathsep(p[2])))))
6432 is_relative_to_current = TRUE;
6433
6434 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006435 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 has_trailing_pathsep = TRUE;
6437
6438 q = getnextcomp(p);
6439 if (*q != NUL)
6440 {
6441 /* Separate the first path component in "p", and keep the
6442 * remainder (beginning with the path separator). */
6443 remain = vim_strsave(q - 1);
6444 q[-1] = NUL;
6445 }
6446
6447 for (;;)
6448 {
6449 for (;;)
6450 {
6451 len = readlink((char *)p, (char *)buf, MAXPATHL);
6452 if (len <= 0)
6453 break;
6454 buf[len] = NUL;
6455
6456 if (limit-- == 0)
6457 {
6458 vim_free(p);
6459 vim_free(remain);
6460 EMSG(_("E655: Too many symbolic links (cycle?)"));
6461 retvar->var_val.var_string = NULL;
6462 goto fail;
6463 }
6464
6465 /* Ensure that the result will have a trailing path separator
6466 * if the argument has one. */
6467 if (remain == NULL && has_trailing_pathsep)
6468 add_pathsep(buf);
6469
6470 /* Separate the first path component in the link value and
6471 * concatenate the remainders. */
6472 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
6473 if (*q != NUL)
6474 {
6475 if (remain == NULL)
6476 remain = vim_strsave(q - 1);
6477 else
6478 {
6479 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
6480 if (cpy != NULL)
6481 {
6482 STRCAT(cpy, remain);
6483 vim_free(remain);
6484 remain = cpy;
6485 }
6486 }
6487 q[-1] = NUL;
6488 }
6489
6490 q = gettail(p);
6491 if (q > p && *q == NUL)
6492 {
6493 /* Ignore trailing path separator. */
6494 q[-1] = NUL;
6495 q = gettail(p);
6496 }
6497 if (q > p && !mch_isFullName(buf))
6498 {
6499 /* symlink is relative to directory of argument */
6500 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
6501 if (cpy != NULL)
6502 {
6503 STRCPY(cpy, p);
6504 STRCPY(gettail(cpy), buf);
6505 vim_free(p);
6506 p = cpy;
6507 }
6508 }
6509 else
6510 {
6511 vim_free(p);
6512 p = vim_strsave(buf);
6513 }
6514 }
6515
6516 if (remain == NULL)
6517 break;
6518
6519 /* Append the first path component of "remain" to "p". */
6520 q = getnextcomp(remain + 1);
6521 len = q - remain - (*q != NUL);
6522 cpy = vim_strnsave(p, STRLEN(p) + len);
6523 if (cpy != NULL)
6524 {
6525 STRNCAT(cpy, remain, len);
6526 vim_free(p);
6527 p = cpy;
6528 }
6529 /* Shorten "remain". */
6530 if (*q != NUL)
6531 STRCPY(remain, q - 1);
6532 else
6533 {
6534 vim_free(remain);
6535 remain = NULL;
6536 }
6537 }
6538
6539 /* If the result is a relative path name, make it explicitly relative to
6540 * the current directory if and only if the argument had this form. */
6541 if (!vim_ispathsep(*p))
6542 {
6543 if (is_relative_to_current
6544 && *p != NUL
6545 && !(p[0] == '.'
6546 && (p[1] == NUL
6547 || vim_ispathsep(p[1])
6548 || (p[1] == '.'
6549 && (p[2] == NUL
6550 || vim_ispathsep(p[2]))))))
6551 {
6552 /* Prepend "./". */
6553 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
6554 if (cpy != NULL)
6555 {
6556 STRCAT(cpy, p);
6557 vim_free(p);
6558 p = cpy;
6559 }
6560 }
6561 else if (!is_relative_to_current)
6562 {
6563 /* Strip leading "./". */
6564 q = p;
6565 while (q[0] == '.' && vim_ispathsep(q[1]))
6566 q += 2;
6567 if (q > p)
6568 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
6569 }
6570 }
6571
6572 /* Ensure that the result will have no trailing path separator
6573 * if the argument had none. But keep "/" or "//". */
6574 if (!has_trailing_pathsep)
6575 {
6576 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006577 if (after_pathsep(p, q))
6578 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580
6581 retvar->var_val.var_string = p;
6582 }
6583# else
6584 retvar->var_val.var_string = vim_strsave(p);
6585# endif
6586#endif
6587
6588 simplify_filename(retvar->var_val.var_string);
6589
6590#ifdef HAVE_READLINK
6591fail:
6592#endif
6593 retvar->var_type = VAR_STRING;
6594}
6595
6596/*
6597 * "simplify()" function
6598 */
6599 static void
6600f_simplify(argvars, retvar)
6601 VAR argvars;
6602 VAR retvar;
6603{
6604 char_u *p;
6605
6606 p = get_var_string(&argvars[0]);
6607 retvar->var_val.var_string = vim_strsave(p);
6608 simplify_filename(retvar->var_val.var_string); /* simplify in place */
6609 retvar->var_type = VAR_STRING;
6610}
6611
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006612#define SP_NOMOVE 1 /* don't move cursor */
6613#define SP_REPEAT 2 /* repeat to find outer pair */
6614#define SP_RETCOUNT 4 /* return matchcount */
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616/*
6617 * "search()" function
6618 */
6619 static void
6620f_search(argvars, retvar)
6621 VAR argvars;
6622 VAR retvar;
6623{
6624 char_u *pat;
6625 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006626 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 int save_p_ws = p_ws;
6628 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006629 int flags = 0;
6630
6631 retvar->var_val.var_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633 pat = get_var_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006634 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
6635 if (dir == 0)
6636 goto theend;
6637 if ((flags & ~SP_NOMOVE) != 0)
6638 {
6639 EMSG2(_(e_invarg2), get_var_string(&argvars[1]));
6640 goto theend;
6641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006643 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
6645 SEARCH_KEEP, RE_SEARCH) != FAIL)
6646 {
6647 retvar->var_val.var_number = pos.lnum;
6648 curwin->w_cursor = pos;
6649 /* "/$" will put the cursor after the end of the line, may need to
6650 * correct that here */
6651 check_cursor();
6652 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006653
6654 /* If 'n' flag is used: restore cursor position. */
6655 if (flags & SP_NOMOVE)
6656 curwin->w_cursor = save_cursor;
6657theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 p_ws = save_p_ws;
6659}
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661/*
6662 * "searchpair()" function
6663 */
6664 static void
6665f_searchpair(argvars, retvar)
6666 VAR argvars;
6667 VAR retvar;
6668{
6669 char_u *spat, *mpat, *epat;
6670 char_u *skip;
6671 char_u *pat, *pat2, *pat3;
6672 pos_T pos;
6673 pos_T firstpos;
6674 pos_T save_cursor;
6675 pos_T save_pos;
6676 int save_p_ws = p_ws;
6677 char_u *save_cpo;
6678 int dir;
6679 int flags = 0;
6680 char_u nbuf1[NUMBUFLEN];
6681 char_u nbuf2[NUMBUFLEN];
6682 char_u nbuf3[NUMBUFLEN];
6683 int n;
6684 int r;
6685 int nest = 1;
6686 int err;
6687
6688 retvar->var_val.var_number = 0; /* default: FAIL */
6689
6690 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6691 save_cpo = p_cpo;
6692 p_cpo = (char_u *)"";
6693
6694 /* Get the three pattern arguments: start, middle, end. */
6695 spat = get_var_string(&argvars[0]);
6696 mpat = get_var_string_buf(&argvars[1], nbuf1);
6697 epat = get_var_string_buf(&argvars[2], nbuf2);
6698
6699 /* Make two search patterns: start/end (pat2, for in nested pairs) and
6700 * start/middle/end (pat3, for the top pair). */
6701 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
6702 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
6703 if (pat2 == NULL || pat3 == NULL)
6704 goto theend;
6705 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
6706 if (*mpat == NUL)
6707 STRCPY(pat3, pat2);
6708 else
6709 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
6710 spat, epat, mpat);
6711
6712 /* Handle the optional fourth argument: flags */
6713 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006714 if (dir == 0)
6715 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 /* Optional fifth argument: skip expresion */
6718 if (argvars[3].var_type == VAR_UNKNOWN
6719 || argvars[4].var_type == VAR_UNKNOWN)
6720 skip = (char_u *)"";
6721 else
6722 skip = get_var_string_buf(&argvars[4], nbuf3);
6723
6724 save_cursor = curwin->w_cursor;
6725 pos = curwin->w_cursor;
6726 firstpos.lnum = 0;
6727 pat = pat3;
6728 for (;;)
6729 {
6730 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
6731 SEARCH_KEEP, RE_SEARCH);
6732 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
6733 /* didn't find it or found the first match again: FAIL */
6734 break;
6735
6736 if (firstpos.lnum == 0)
6737 firstpos = pos;
6738
6739 /* If the skip pattern matches, ignore this match. */
6740 if (*skip != NUL)
6741 {
6742 save_pos = curwin->w_cursor;
6743 curwin->w_cursor = pos;
6744 r = eval_to_bool(skip, &err, NULL, FALSE);
6745 curwin->w_cursor = save_pos;
6746 if (err)
6747 {
6748 /* Evaluating {skip} caused an error, break here. */
6749 curwin->w_cursor = save_cursor;
6750 retvar->var_val.var_number = -1;
6751 break;
6752 }
6753 if (r)
6754 continue;
6755 }
6756
6757 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
6758 {
6759 /* Found end when searching backwards or start when searching
6760 * forward: nested pair. */
6761 ++nest;
6762 pat = pat2; /* nested, don't search for middle */
6763 }
6764 else
6765 {
6766 /* Found end when searching forward or start when searching
6767 * backward: end of (nested) pair; or found middle in outer pair. */
6768 if (--nest == 1)
6769 pat = pat3; /* outer level, search for middle */
6770 }
6771
6772 if (nest == 0)
6773 {
6774 /* Found the match: return matchcount or line number. */
6775 if (flags & SP_RETCOUNT)
6776 ++retvar->var_val.var_number;
6777 else
6778 retvar->var_val.var_number = pos.lnum;
6779 curwin->w_cursor = pos;
6780 if (!(flags & SP_REPEAT))
6781 break;
6782 nest = 1; /* search for next unmatched */
6783 }
6784 }
6785
6786 /* If 'n' flag is used or search failed: restore cursor position. */
6787 if ((flags & SP_NOMOVE) || retvar->var_val.var_number == 0)
6788 curwin->w_cursor = save_cursor;
6789
6790theend:
6791 vim_free(pat2);
6792 vim_free(pat3);
6793 p_ws = save_p_ws;
6794 p_cpo = save_cpo;
6795}
6796
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006797/*
6798 * Get flags for a search function.
6799 * Possibly sets "p_ws".
6800 * Returns BACKWARD, FORWARD or zero (for an error).
6801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 static int
6803get_search_arg(varp, flagsp)
6804 VAR varp;
6805 int *flagsp;
6806{
6807 int dir = FORWARD;
6808 char_u *flags;
6809 char_u nbuf[NUMBUFLEN];
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006810 int mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
6812 if (varp->var_type != VAR_UNKNOWN)
6813 {
6814 flags = get_var_string_buf(varp, nbuf);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006815 while (*flags != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006817 switch (*flags)
6818 {
6819 case 'b': dir = BACKWARD; break;
6820 case 'w': p_ws = TRUE; break;
6821 case 'W': p_ws = FALSE; break;
6822 default: mask = 0;
6823 if (flagsp != NULL)
6824 switch (*flags)
6825 {
6826 case 'n': mask = SP_NOMOVE; break;
6827 case 'r': mask = SP_REPEAT; break;
6828 case 'm': mask = SP_RETCOUNT; break;
6829 }
6830 if (mask == 0)
6831 {
6832 EMSG2(_(e_invarg2), flags);
6833 dir = 0;
6834 }
6835 else
6836 *flagsp |= mask;
6837 }
6838 if (dir == 0)
6839 break;
6840 ++flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842 }
6843 return dir;
6844}
6845
6846/*
6847 * "setbufvar()" function
6848 */
6849/*ARGSUSED*/
6850 static void
6851f_setbufvar(argvars, retvar)
6852 VAR argvars;
6853 VAR retvar;
6854{
6855 buf_T *buf;
6856#ifdef FEAT_AUTOCMD
6857 aco_save_T aco;
6858#else
6859 buf_T *save_curbuf;
6860#endif
6861 char_u *varname, *bufvarname;
6862 VAR varp;
6863 char_u nbuf[NUMBUFLEN];
6864
6865 if (check_restricted() || check_secure())
6866 return;
6867 ++emsg_off;
6868 buf = get_buf_var(&argvars[0]);
6869 varname = get_var_string(&argvars[1]);
6870 varp = &argvars[2];
6871
6872 if (buf != NULL && varname != NULL && varp != NULL)
6873 {
6874 /* set curbuf to be our buf, temporarily */
6875#ifdef FEAT_AUTOCMD
6876 aucmd_prepbuf(&aco, buf);
6877#else
6878 save_curbuf = curbuf;
6879 curbuf = buf;
6880#endif
6881
6882 if (*varname == '&')
6883 {
6884 ++varname;
6885 set_option_value(varname, get_var_number(varp),
6886 get_var_string_buf(varp, nbuf), OPT_LOCAL);
6887 }
6888 else
6889 {
6890 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
6891 if (bufvarname != NULL)
6892 {
6893 STRCPY(bufvarname, "b:");
6894 STRCPY(bufvarname + 2, varname);
6895 set_var(bufvarname, varp);
6896 vim_free(bufvarname);
6897 }
6898 }
6899
6900 /* reset notion of buffer */
6901#ifdef FEAT_AUTOCMD
6902 aucmd_restbuf(&aco);
6903#else
6904 curbuf = save_curbuf;
6905#endif
6906 }
6907 --emsg_off;
6908}
6909
6910/*
6911 * "setcmdpos()" function
6912 */
6913 static void
6914f_setcmdpos(argvars, retvar)
6915 VAR argvars;
6916 VAR retvar;
6917{
6918 retvar->var_val.var_number = set_cmdline_pos(
6919 (int)get_var_number(&argvars[0]) - 1);
6920}
6921
6922/*
6923 * "setline()" function
6924 */
6925 static void
6926f_setline(argvars, retvar)
6927 VAR argvars;
6928 VAR retvar;
6929{
6930 linenr_T lnum;
6931 char_u *line;
6932
6933 lnum = get_var_lnum(argvars);
6934 line = get_var_string(&argvars[1]);
6935 retvar->var_val.var_number = 1; /* FAIL is default */
6936
6937 if (lnum >= 1
6938 && lnum <= curbuf->b_ml.ml_line_count
6939 && u_savesub(lnum) == OK
6940 && ml_replace(lnum, line, TRUE) == OK)
6941 {
6942 changed_bytes(lnum, 0);
6943 check_cursor_col();
6944 retvar->var_val.var_number = 0;
6945 }
6946}
6947
6948/*
6949 * "setreg()" function
6950 */
6951 static void
6952f_setreg(argvars, retvar)
6953 VAR argvars;
6954 VAR retvar;
6955{
6956 int regname;
6957 char_u *strregname;
6958 char_u *stropt;
6959 int append;
6960 char_u yank_type;
6961 long block_len;
6962
6963 block_len = -1;
6964 yank_type = MAUTO;
6965 append = FALSE;
6966
6967 strregname = get_var_string(argvars);
6968 retvar->var_val.var_number = 1; /* FAIL is default */
6969
6970 regname = (strregname == NULL ? '"' : *strregname);
6971 if (regname == 0 || regname == '@')
6972 regname = '"';
6973 else if (regname == '=')
6974 return;
6975
6976 if (argvars[2].var_type != VAR_UNKNOWN)
6977 {
6978 for (stropt = get_var_string(&argvars[2]); *stropt != NUL; ++stropt)
6979 switch (*stropt)
6980 {
6981 case 'a': case 'A': /* append */
6982 append = TRUE;
6983 break;
6984 case 'v': case 'c': /* character-wise selection */
6985 yank_type = MCHAR;
6986 break;
6987 case 'V': case 'l': /* line-wise selection */
6988 yank_type = MLINE;
6989 break;
6990#ifdef FEAT_VISUAL
6991 case 'b': case Ctrl_V: /* block-wise selection */
6992 yank_type = MBLOCK;
6993 if (VIM_ISDIGIT(stropt[1]))
6994 {
6995 ++stropt;
6996 block_len = getdigits(&stropt) - 1;
6997 --stropt;
6998 }
6999 break;
7000#endif
7001 }
7002 }
7003
7004 write_reg_contents_ex(regname, get_var_string(&argvars[1]), -1,
7005 append, yank_type, block_len);
7006 retvar->var_val.var_number = 0;
7007}
7008
7009
7010/*
7011 * "setwinvar(expr)" function
7012 */
7013/*ARGSUSED*/
7014 static void
7015f_setwinvar(argvars, retvar)
7016 VAR argvars;
7017 VAR retvar;
7018{
7019 win_T *win;
7020#ifdef FEAT_WINDOWS
7021 win_T *save_curwin;
7022#endif
7023 char_u *varname, *winvarname;
7024 VAR varp;
7025 char_u nbuf[NUMBUFLEN];
7026
7027 if (check_restricted() || check_secure())
7028 return;
7029 ++emsg_off;
7030 win = find_win_by_nr(&argvars[0]);
7031 varname = get_var_string(&argvars[1]);
7032 varp = &argvars[2];
7033
7034 if (win != NULL && varname != NULL && varp != NULL)
7035 {
7036#ifdef FEAT_WINDOWS
7037 /* set curwin to be our win, temporarily */
7038 save_curwin = curwin;
7039 curwin = win;
7040 curbuf = curwin->w_buffer;
7041#endif
7042
7043 if (*varname == '&')
7044 {
7045 ++varname;
7046 set_option_value(varname, get_var_number(varp),
7047 get_var_string_buf(varp, nbuf), OPT_LOCAL);
7048 }
7049 else
7050 {
7051 winvarname = alloc((unsigned)STRLEN(varname) + 3);
7052 if (winvarname != NULL)
7053 {
7054 STRCPY(winvarname, "w:");
7055 STRCPY(winvarname + 2, varname);
7056 set_var(winvarname, varp);
7057 vim_free(winvarname);
7058 }
7059 }
7060
7061#ifdef FEAT_WINDOWS
7062 /* Restore current window, if it's still valid (autocomands can make
7063 * it invalid). */
7064 if (win_valid(save_curwin))
7065 {
7066 curwin = save_curwin;
7067 curbuf = curwin->w_buffer;
7068 }
7069#endif
7070 }
7071 --emsg_off;
7072}
7073
7074/*
7075 * "nextnonblank()" function
7076 */
7077 static void
7078f_nextnonblank(argvars, retvar)
7079 VAR argvars;
7080 VAR retvar;
7081{
7082 linenr_T lnum;
7083
7084 for (lnum = get_var_lnum(argvars); ; ++lnum)
7085 {
7086 if (lnum > curbuf->b_ml.ml_line_count)
7087 {
7088 lnum = 0;
7089 break;
7090 }
7091 if (*skipwhite(ml_get(lnum)) != NUL)
7092 break;
7093 }
7094 retvar->var_val.var_number = lnum;
7095}
7096
7097/*
7098 * "prevnonblank()" function
7099 */
7100 static void
7101f_prevnonblank(argvars, retvar)
7102 VAR argvars;
7103 VAR retvar;
7104{
7105 linenr_T lnum;
7106
7107 lnum = get_var_lnum(argvars);
7108 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7109 lnum = 0;
7110 else
7111 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7112 --lnum;
7113 retvar->var_val.var_number = lnum;
7114}
7115
7116#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
7117static void make_connection __ARGS((void));
7118static int check_connection __ARGS((void));
7119
7120 static void
7121make_connection()
7122{
7123 if (X_DISPLAY == NULL
7124# ifdef FEAT_GUI
7125 && !gui.in_use
7126# endif
7127 )
7128 {
7129 x_force_connect = TRUE;
7130 setup_term_clip();
7131 x_force_connect = FALSE;
7132 }
7133}
7134
7135 static int
7136check_connection()
7137{
7138 make_connection();
7139 if (X_DISPLAY == NULL)
7140 {
7141 EMSG(_("E240: No connection to Vim server"));
7142 return FAIL;
7143 }
7144 return OK;
7145}
7146#endif
7147
7148/*ARGSUSED*/
7149 static void
7150f_serverlist(argvars, retvar)
7151 VAR argvars;
7152 VAR retvar;
7153{
7154 char_u *r = NULL;
7155
7156#ifdef FEAT_CLIENTSERVER
7157# ifdef WIN32
7158 r = serverGetVimNames();
7159# else
7160 make_connection();
7161 if (X_DISPLAY != NULL)
7162 r = serverGetVimNames(X_DISPLAY);
7163# endif
7164#endif
7165 retvar->var_type = VAR_STRING;
7166 retvar->var_val.var_string = r;
7167}
7168
7169/*ARGSUSED*/
7170 static void
7171f_remote_peek(argvars, retvar)
7172 VAR argvars;
7173 VAR retvar;
7174{
7175#ifdef FEAT_CLIENTSERVER
7176 var v;
7177 char_u *s = NULL;
7178# ifdef WIN32
7179 int n = 0;
7180# endif
7181
7182 if (check_restricted() || check_secure())
7183 {
7184 retvar->var_val.var_number = -1;
7185 return;
7186 }
7187# ifdef WIN32
7188 sscanf(get_var_string(&argvars[0]), "%x", &n);
7189 if (n == 0)
7190 retvar->var_val.var_number = -1;
7191 else
7192 {
7193 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
7194 retvar->var_val.var_number = (s != NULL);
7195 }
7196# else
7197 retvar->var_val.var_number = 0;
7198 if (check_connection() == FAIL)
7199 return;
7200
7201 retvar->var_val.var_number = serverPeekReply(X_DISPLAY,
7202 serverStrToWin(get_var_string(&argvars[0])), &s);
7203# endif
7204
7205 if (argvars[1].var_type != VAR_UNKNOWN && retvar->var_val.var_number > 0)
7206 {
7207 v.var_type = VAR_STRING;
7208 v.var_val.var_string = vim_strsave(s);
7209 set_var(get_var_string(&argvars[1]), &v);
7210 }
7211#else
7212 retvar->var_val.var_number = -1;
7213#endif
7214}
7215
7216/*ARGSUSED*/
7217 static void
7218f_remote_read(argvars, retvar)
7219 VAR argvars;
7220 VAR retvar;
7221{
7222 char_u *r = NULL;
7223
7224#ifdef FEAT_CLIENTSERVER
7225 if (!check_restricted() && !check_secure())
7226 {
7227# ifdef WIN32
7228 /* The server's HWND is encoded in the 'id' parameter */
7229 int n = 0;
7230
7231 sscanf(get_var_string(&argvars[0]), "%x", &n);
7232 if (n != 0)
7233 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
7234 if (r == NULL)
7235# else
7236 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
7237 serverStrToWin(get_var_string(&argvars[0])), &r, FALSE) < 0)
7238# endif
7239 EMSG(_("E277: Unable to read a server reply"));
7240 }
7241#endif
7242 retvar->var_type = VAR_STRING;
7243 retvar->var_val.var_string = r;
7244}
7245
7246/*ARGSUSED*/
7247 static void
7248f_server2client(argvars, retvar)
7249 VAR argvars;
7250 VAR retvar;
7251{
7252#ifdef FEAT_CLIENTSERVER
7253 char_u buf[NUMBUFLEN];
7254 char_u *server = get_var_string(&argvars[0]);
7255 char_u *reply = get_var_string_buf(&argvars[1], buf);
7256
7257 retvar->var_val.var_number = -1;
7258 if (check_restricted() || check_secure())
7259 return;
7260# ifdef FEAT_X11
7261 if (check_connection() == FAIL)
7262 return;
7263# endif
7264
7265 if (serverSendReply(server, reply) < 0)
7266 {
7267 EMSG(_("E258: Unable to send to client"));
7268 return;
7269 }
7270 retvar->var_val.var_number = 0;
7271#else
7272 retvar->var_val.var_number = -1;
7273#endif
7274}
7275
7276#ifdef FEAT_CLIENTSERVER
7277static void remote_common __ARGS((VAR argvars, VAR retvar, int expr));
7278
7279 static void
7280remote_common(argvars, retvar, expr)
7281 VAR argvars;
7282 VAR retvar;
7283 int expr;
7284{
7285 char_u *server_name;
7286 char_u *keys;
7287 char_u *r = NULL;
7288 char_u buf[NUMBUFLEN];
7289# ifdef WIN32
7290 HWND w;
7291# else
7292 Window w;
7293# endif
7294
7295 if (check_restricted() || check_secure())
7296 return;
7297
7298# ifdef FEAT_X11
7299 if (check_connection() == FAIL)
7300 return;
7301# endif
7302
7303 server_name = get_var_string(&argvars[0]);
7304 keys = get_var_string_buf(&argvars[1], buf);
7305# ifdef WIN32
7306 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
7307# else
7308 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
7309 < 0)
7310# endif
7311 {
7312 if (r != NULL)
7313 EMSG(r); /* sending worked but evaluation failed */
7314 else
7315 EMSG2(_("E241: Unable to send to %s"), server_name);
7316 return;
7317 }
7318
7319 retvar->var_val.var_string = r;
7320
7321 if (argvars[2].var_type != VAR_UNKNOWN)
7322 {
7323 var v;
7324 char_u str[30];
7325
7326 sprintf((char *)str, "0x%x", (unsigned int)w);
7327 v.var_type = VAR_STRING;
7328 v.var_val.var_string = vim_strsave(str);
7329 set_var(get_var_string(&argvars[2]), &v);
7330 }
7331}
7332#endif
7333
7334/*
7335 * "remote_expr()" function
7336 */
7337/*ARGSUSED*/
7338 static void
7339f_remote_expr(argvars, retvar)
7340 VAR argvars;
7341 VAR retvar;
7342{
7343 retvar->var_type = VAR_STRING;
7344 retvar->var_val.var_string = NULL;
7345#ifdef FEAT_CLIENTSERVER
7346 remote_common(argvars, retvar, TRUE);
7347#endif
7348}
7349
7350/*
7351 * "remote_send()" function
7352 */
7353/*ARGSUSED*/
7354 static void
7355f_remote_send(argvars, retvar)
7356 VAR argvars;
7357 VAR retvar;
7358{
7359 retvar->var_type = VAR_STRING;
7360 retvar->var_val.var_string = NULL;
7361#ifdef FEAT_CLIENTSERVER
7362 remote_common(argvars, retvar, FALSE);
7363#endif
7364}
7365
7366/*
7367 * "remote_foreground()" function
7368 */
7369/*ARGSUSED*/
7370 static void
7371f_remote_foreground(argvars, retvar)
7372 VAR argvars;
7373 VAR retvar;
7374{
7375 retvar->var_val.var_number = 0;
7376#ifdef FEAT_CLIENTSERVER
7377# ifdef WIN32
7378 /* On Win32 it's done in this application. */
7379 serverForeground(get_var_string(&argvars[0]));
7380# else
7381 /* Send a foreground() expression to the server. */
7382 argvars[1].var_type = VAR_STRING;
7383 argvars[1].var_val.var_string = vim_strsave((char_u *)"foreground()");
7384 argvars[2].var_type = VAR_UNKNOWN;
7385 remote_common(argvars, retvar, TRUE);
7386 vim_free(argvars[1].var_val.var_string);
7387# endif
7388#endif
7389}
7390
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007391/*
7392 * "repeat()" function
7393 */
7394/*ARGSUSED*/
7395 static void
7396f_repeat(argvars, retvar)
7397 VAR argvars;
7398 VAR retvar;
7399{
7400 char_u *p;
7401 int n;
7402 int slen;
7403 int len;
7404 char_u *r;
7405 int i;
7406
7407 p = get_var_string(&argvars[0]);
7408 n = get_var_number(&argvars[1]);
7409
7410 retvar->var_type = VAR_STRING;
7411 retvar->var_val.var_string = NULL;
7412
7413 slen = (int)STRLEN(p);
7414 len = slen * n;
7415
7416 if (len <= 0)
7417 return;
7418
7419 r = alloc(len + 1);
7420 if (r != NULL)
7421 {
7422 for (i = 0; i < n; i++)
7423 mch_memmove(r + i * slen, p, (size_t)slen);
7424 r[len] = NUL;
7425 }
7426
7427 retvar->var_val.var_string = r;
7428}
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430#ifdef HAVE_STRFTIME
7431/*
7432 * "strftime({format}[, {time}])" function
7433 */
7434 static void
7435f_strftime(argvars, retvar)
7436 VAR argvars;
7437 VAR retvar;
7438{
7439 char_u result_buf[256];
7440 struct tm *curtime;
7441 time_t seconds;
7442 char_u *p;
7443
7444 retvar->var_type = VAR_STRING;
7445
7446 p = get_var_string(&argvars[0]);
7447 if (argvars[1].var_type == VAR_UNKNOWN)
7448 seconds = time(NULL);
7449 else
7450 seconds = (time_t)get_var_number(&argvars[1]);
7451 curtime = localtime(&seconds);
7452 /* MSVC returns NULL for an invalid value of seconds. */
7453 if (curtime == NULL)
7454 retvar->var_val.var_string = vim_strsave((char_u *)_("(Invalid)"));
7455 else
7456 {
7457# ifdef FEAT_MBYTE
7458 vimconv_T conv;
7459 char_u *enc;
7460
7461 conv.vc_type = CONV_NONE;
7462 enc = enc_locale();
7463 convert_setup(&conv, p_enc, enc);
7464 if (conv.vc_type != CONV_NONE)
7465 p = string_convert(&conv, p, NULL);
7466# endif
7467 if (p != NULL)
7468 (void)strftime((char *)result_buf, sizeof(result_buf),
7469 (char *)p, curtime);
7470 else
7471 result_buf[0] = NUL;
7472
7473# ifdef FEAT_MBYTE
7474 if (conv.vc_type != CONV_NONE)
7475 vim_free(p);
7476 convert_setup(&conv, enc, p_enc);
7477 if (conv.vc_type != CONV_NONE)
7478 retvar->var_val.var_string =
7479 string_convert(&conv, result_buf, NULL);
7480 else
7481# endif
7482 retvar->var_val.var_string = vim_strsave(result_buf);
7483
7484# ifdef FEAT_MBYTE
7485 /* Release conversion descriptors */
7486 convert_setup(&conv, NULL, NULL);
7487 vim_free(enc);
7488# endif
7489 }
7490}
7491#endif
7492
7493/*
7494 * "stridx()" function
7495 */
7496 static void
7497f_stridx(argvars, retvar)
7498 VAR argvars;
7499 VAR retvar;
7500{
7501 char_u buf[NUMBUFLEN];
7502 char_u *needle;
7503 char_u *haystack;
7504 char_u *pos;
7505
7506 needle = get_var_string(&argvars[1]);
7507 haystack = get_var_string_buf(&argvars[0], buf);
7508 pos = (char_u *)strstr((char *)haystack, (char *)needle);
7509
7510 if (pos == NULL)
7511 retvar->var_val.var_number = -1;
7512 else
7513 retvar->var_val.var_number = (varnumber_T) (pos - haystack);
7514}
7515
7516/*
7517 * "strridx()" function
7518 */
7519 static void
7520f_strridx(argvars, retvar)
7521 VAR argvars;
7522 VAR retvar;
7523{
7524 char_u buf[NUMBUFLEN];
7525 char_u *needle;
7526 char_u *haystack;
7527 char_u *rest;
7528 char_u *lastmatch = NULL;
7529
7530 needle = get_var_string(&argvars[1]);
7531 haystack = get_var_string_buf(&argvars[0], buf);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007532 if (*needle == NUL)
7533 /* Empty string matches past the end. */
7534 lastmatch = haystack + STRLEN(haystack);
7535 else
7536 for (rest = haystack; *rest != '\0'; ++rest)
7537 {
7538 rest = (char_u *)strstr((char *)rest, (char *)needle);
7539 if (rest == NULL)
7540 break;
7541 lastmatch = rest;
7542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
7544 if (lastmatch == NULL)
7545 retvar->var_val.var_number = -1;
7546 else
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007547 retvar->var_val.var_number = (varnumber_T)(lastmatch - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550/*
7551 * "strlen()" function
7552 */
7553 static void
7554f_strlen(argvars, retvar)
7555 VAR argvars;
7556 VAR retvar;
7557{
7558 retvar->var_val.var_number = (varnumber_T) (STRLEN(get_var_string(&argvars[0])));
7559}
7560
7561/*
7562 * "strpart()" function
7563 */
7564 static void
7565f_strpart(argvars, retvar)
7566 VAR argvars;
7567 VAR retvar;
7568{
7569 char_u *p;
7570 int n;
7571 int len;
7572 int slen;
7573
7574 p = get_var_string(&argvars[0]);
7575 slen = (int)STRLEN(p);
7576
7577 n = get_var_number(&argvars[1]);
7578 if (argvars[2].var_type != VAR_UNKNOWN)
7579 len = get_var_number(&argvars[2]);
7580 else
7581 len = slen - n; /* default len: all bytes that are available. */
7582
7583 /*
7584 * Only return the overlap between the specified part and the actual
7585 * string.
7586 */
7587 if (n < 0)
7588 {
7589 len += n;
7590 n = 0;
7591 }
7592 else if (n > slen)
7593 n = slen;
7594 if (len < 0)
7595 len = 0;
7596 else if (n + len > slen)
7597 len = slen - n;
7598
7599 retvar->var_type = VAR_STRING;
7600 retvar->var_val.var_string = vim_strnsave(p + n, len);
7601}
7602
7603/*
7604 * "strtrans()" function
7605 */
7606 static void
7607f_strtrans(argvars, retvar)
7608 VAR argvars;
7609 VAR retvar;
7610{
7611 retvar->var_type = VAR_STRING;
7612 retvar->var_val.var_string = transstr(get_var_string(&argvars[0]));
7613}
7614
7615/*
7616 * "synID(line, col, trans)" function
7617 */
7618/*ARGSUSED*/
7619 static void
7620f_synID(argvars, retvar)
7621 VAR argvars;
7622 VAR retvar;
7623{
7624 int id = 0;
7625#ifdef FEAT_SYN_HL
7626 long line;
7627 long col;
7628 int trans;
7629
7630 line = get_var_lnum(argvars);
7631 col = get_var_number(&argvars[1]) - 1;
7632 trans = get_var_number(&argvars[2]);
7633
7634 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
7635 && col >= 0 && col < (long)STRLEN(ml_get(line)))
7636 id = syn_get_id(line, col, trans);
7637#endif
7638
7639 retvar->var_val.var_number = id;
7640}
7641
7642/*
7643 * "synIDattr(id, what [, mode])" function
7644 */
7645/*ARGSUSED*/
7646 static void
7647f_synIDattr(argvars, retvar)
7648 VAR argvars;
7649 VAR retvar;
7650{
7651 char_u *p = NULL;
7652#ifdef FEAT_SYN_HL
7653 int id;
7654 char_u *what;
7655 char_u *mode;
7656 char_u modebuf[NUMBUFLEN];
7657 int modec;
7658
7659 id = get_var_number(&argvars[0]);
7660 what = get_var_string(&argvars[1]);
7661 if (argvars[2].var_type != VAR_UNKNOWN)
7662 {
7663 mode = get_var_string_buf(&argvars[2], modebuf);
7664 modec = TOLOWER_ASC(mode[0]);
7665 if (modec != 't' && modec != 'c'
7666#ifdef FEAT_GUI
7667 && modec != 'g'
7668#endif
7669 )
7670 modec = 0; /* replace invalid with current */
7671 }
7672 else
7673 {
7674#ifdef FEAT_GUI
7675 if (gui.in_use)
7676 modec = 'g';
7677 else
7678#endif
7679 if (t_colors > 1)
7680 modec = 'c';
7681 else
7682 modec = 't';
7683 }
7684
7685
7686 switch (TOLOWER_ASC(what[0]))
7687 {
7688 case 'b':
7689 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
7690 p = highlight_color(id, what, modec);
7691 else /* bold */
7692 p = highlight_has_attr(id, HL_BOLD, modec);
7693 break;
7694
7695 case 'f': /* fg[#] */
7696 p = highlight_color(id, what, modec);
7697 break;
7698
7699 case 'i':
7700 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
7701 p = highlight_has_attr(id, HL_INVERSE, modec);
7702 else /* italic */
7703 p = highlight_has_attr(id, HL_ITALIC, modec);
7704 break;
7705
7706 case 'n': /* name */
7707 p = get_highlight_name(NULL, id - 1);
7708 break;
7709
7710 case 'r': /* reverse */
7711 p = highlight_has_attr(id, HL_INVERSE, modec);
7712 break;
7713
7714 case 's': /* standout */
7715 p = highlight_has_attr(id, HL_STANDOUT, modec);
7716 break;
7717
7718 case 'u': /* underline */
7719 p = highlight_has_attr(id, HL_UNDERLINE, modec);
7720 break;
7721 }
7722
7723 if (p != NULL)
7724 p = vim_strsave(p);
7725#endif
7726 retvar->var_type = VAR_STRING;
7727 retvar->var_val.var_string = p;
7728}
7729
7730/*
7731 * "synIDtrans(id)" function
7732 */
7733/*ARGSUSED*/
7734 static void
7735f_synIDtrans(argvars, retvar)
7736 VAR argvars;
7737 VAR retvar;
7738{
7739 int id;
7740
7741#ifdef FEAT_SYN_HL
7742 id = get_var_number(&argvars[0]);
7743
7744 if (id > 0)
7745 id = syn_get_final_id(id);
7746 else
7747#endif
7748 id = 0;
7749
7750 retvar->var_val.var_number = id;
7751}
7752
7753/*
7754 * "system()" function
7755 */
7756 static void
7757f_system(argvars, retvar)
7758 VAR argvars;
7759 VAR retvar;
7760{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007761 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007763 char_u *infile = NULL;
7764 char_u buf[NUMBUFLEN];
7765 int err = FALSE;
7766 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007768 if (argvars[1].var_type != VAR_UNKNOWN)
7769 {
7770 /*
7771 * Write the string to a temp file, to be used for input of the shell
7772 * command.
7773 */
7774 if ((infile = vim_tempname('i')) == NULL)
7775 {
7776 EMSG(_(e_notmp));
7777 return;
7778 }
7779
7780 fd = mch_fopen((char *)infile, WRITEBIN);
7781 if (fd == NULL)
7782 {
7783 EMSG2(_(e_notopen), infile);
7784 goto done;
7785 }
7786 p = get_var_string_buf(&argvars[1], buf);
7787 if (fwrite(p, STRLEN(p), 1, fd) != 1)
7788 err = TRUE;
7789 if (fclose(fd) != 0)
7790 err = TRUE;
7791 if (err)
7792 {
7793 EMSG(_("E677: Error writing temp file"));
7794 goto done;
7795 }
7796 }
7797
7798 res = get_cmd_output(get_var_string(&argvars[0]), infile, SHELL_SILENT);
7799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800#ifdef USE_CR
7801 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007802 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {
7804 char_u *s;
7805
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007806 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {
7808 if (*s == CAR)
7809 *s = NL;
7810 }
7811 }
7812#else
7813# ifdef USE_CRNL
7814 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007815 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 char_u *s, *d;
7818
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007819 d = res;
7820 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
7822 if (s[0] == CAR && s[1] == NL)
7823 ++s;
7824 *d++ = *s;
7825 }
7826 *d = NUL;
7827 }
7828# endif
7829#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007830
7831done:
7832 if (infile != NULL)
7833 {
7834 mch_remove(infile);
7835 vim_free(infile);
7836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 retvar->var_type = VAR_STRING;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007838 retvar->var_val.var_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839}
7840
7841/*
7842 * "submatch()" function
7843 */
7844 static void
7845f_submatch(argvars, retvar)
7846 VAR argvars;
7847 VAR retvar;
7848{
7849 retvar->var_type = VAR_STRING;
7850 retvar->var_val.var_string = reg_submatch((int)get_var_number(&argvars[0]));
7851}
7852
7853/*
7854 * "substitute()" function
7855 */
7856 static void
7857f_substitute(argvars, retvar)
7858 VAR argvars;
7859 VAR retvar;
7860{
7861 char_u patbuf[NUMBUFLEN];
7862 char_u subbuf[NUMBUFLEN];
7863 char_u flagsbuf[NUMBUFLEN];
7864
7865 retvar->var_type = VAR_STRING;
7866 retvar->var_val.var_string = do_string_sub(
7867 get_var_string(&argvars[0]),
7868 get_var_string_buf(&argvars[1], patbuf),
7869 get_var_string_buf(&argvars[2], subbuf),
7870 get_var_string_buf(&argvars[3], flagsbuf));
7871}
7872
7873/*
7874 * "tempname()" function
7875 */
7876/*ARGSUSED*/
7877 static void
7878f_tempname(argvars, retvar)
7879 VAR argvars;
7880 VAR retvar;
7881{
7882 static int x = 'A';
7883
7884 retvar->var_type = VAR_STRING;
7885 retvar->var_val.var_string = vim_tempname(x);
7886
7887 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
7888 * names. Skip 'I' and 'O', they are used for shell redirection. */
7889 do
7890 {
7891 if (x == 'Z')
7892 x = '0';
7893 else if (x == '9')
7894 x = 'A';
7895 else
7896 {
7897#ifdef EBCDIC
7898 if (x == 'I')
7899 x = 'J';
7900 else if (x == 'R')
7901 x = 'S';
7902 else
7903#endif
7904 ++x;
7905 }
7906 } while (x == 'I' || x == 'O');
7907}
7908
7909/*
7910 * "tolower(string)" function
7911 */
7912 static void
7913f_tolower(argvars, retvar)
7914 VAR argvars;
7915 VAR retvar;
7916{
7917 char_u *p;
7918
7919 p = vim_strsave(get_var_string(&argvars[0]));
7920 retvar->var_type = VAR_STRING;
7921 retvar->var_val.var_string = p;
7922
7923 if (p != NULL)
7924 while (*p != NUL)
7925 {
7926#ifdef FEAT_MBYTE
7927 int l;
7928
7929 if (enc_utf8)
7930 {
7931 int c, lc;
7932
7933 c = utf_ptr2char(p);
7934 lc = utf_tolower(c);
7935 l = utf_ptr2len_check(p);
7936 /* TODO: reallocate string when byte count changes. */
7937 if (utf_char2len(lc) == l)
7938 utf_char2bytes(lc, p);
7939 p += l;
7940 }
7941 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7942 p += l; /* skip multi-byte character */
7943 else
7944#endif
7945 {
7946 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
7947 ++p;
7948 }
7949 }
7950}
7951
7952/*
7953 * "toupper(string)" function
7954 */
7955 static void
7956f_toupper(argvars, retvar)
7957 VAR argvars;
7958 VAR retvar;
7959{
7960 char_u *p;
7961
7962 p = vim_strsave(get_var_string(&argvars[0]));
7963 retvar->var_type = VAR_STRING;
7964 retvar->var_val.var_string = p;
7965
7966 if (p != NULL)
7967 while (*p != NUL)
7968 {
7969#ifdef FEAT_MBYTE
7970 int l;
7971
7972 if (enc_utf8)
7973 {
7974 int c, uc;
7975
7976 c = utf_ptr2char(p);
7977 uc = utf_toupper(c);
7978 l = utf_ptr2len_check(p);
7979 /* TODO: reallocate string when byte count changes. */
7980 if (utf_char2len(uc) == l)
7981 utf_char2bytes(uc, p);
7982 p += l;
7983 }
7984 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7985 p += l; /* skip multi-byte character */
7986 else
7987#endif
7988 {
7989 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
7990 p++;
7991 }
7992 }
7993}
7994
7995/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00007996 * "tr(string, fromstr, tostr)" function
7997 */
7998 static void
7999f_tr(argvars, retvar)
8000 VAR argvars;
8001 VAR retvar;
8002{
8003 char_u *instr;
8004 char_u *fromstr;
8005 char_u *tostr;
8006 char_u *p;
8007#ifdef FEAT_MBYTE
8008 int inlen;
8009 int fromlen;
8010 int tolen;
8011 int idx;
8012 char_u *cpstr;
8013 int cplen;
8014 int first = TRUE;
8015#endif
8016 char_u buf[NUMBUFLEN];
8017 char_u buf2[NUMBUFLEN];
8018 garray_T ga;
8019
8020 instr = get_var_string(&argvars[0]);
8021 fromstr = get_var_string_buf(&argvars[1], buf);
8022 tostr = get_var_string_buf(&argvars[2], buf2);
8023
8024 /* Default return value: empty string. */
8025 retvar->var_type = VAR_STRING;
8026 retvar->var_val.var_string = NULL;
8027 ga_init2(&ga, (int)sizeof(char), 80);
8028
8029#ifdef FEAT_MBYTE
8030 if (!has_mbyte)
8031#endif
8032 /* not multi-byte: fromstr and tostr must be the same length */
8033 if (STRLEN(fromstr) != STRLEN(tostr))
8034 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008035#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +00008036error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008037#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +00008038 EMSG2(_(e_invarg2), fromstr);
8039 ga_clear(&ga);
8040 return;
8041 }
8042
8043 /* fromstr and tostr have to contain the same number of chars */
8044 while (*instr != NUL)
8045 {
8046#ifdef FEAT_MBYTE
8047 if (has_mbyte)
8048 {
8049 inlen = mb_ptr2len_check(instr);
8050 cpstr = instr;
8051 cplen = inlen;
8052 idx = 0;
8053 for (p = fromstr; *p != NUL; p += fromlen)
8054 {
8055 fromlen = mb_ptr2len_check(p);
8056 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
8057 {
8058 for (p = tostr; *p != NUL; p += tolen)
8059 {
8060 tolen = mb_ptr2len_check(p);
8061 if (idx-- == 0)
8062 {
8063 cplen = tolen;
8064 cpstr = p;
8065 break;
8066 }
8067 }
8068 if (*p == NUL) /* tostr is shorter than fromstr */
8069 goto error;
8070 break;
8071 }
8072 ++idx;
8073 }
8074
8075 if (first && cpstr == instr)
8076 {
8077 /* Check that fromstr and tostr have the same number of
8078 * (multi-byte) characters. Done only once when a character
8079 * of instr doesn't appear in fromstr. */
8080 first = FALSE;
8081 for (p = tostr; *p != NUL; p += tolen)
8082 {
8083 tolen = mb_ptr2len_check(p);
8084 --idx;
8085 }
8086 if (idx != 0)
8087 goto error;
8088 }
8089
8090 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00008091 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +00008092 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008093
8094 instr += inlen;
8095 }
8096 else
8097#endif
8098 {
8099 /* When not using multi-byte chars we can do it faster. */
8100 p = vim_strchr(fromstr, *instr);
8101 if (p != NULL)
8102 ga_append(&ga, tostr[p - fromstr]);
8103 else
8104 ga_append(&ga, *instr);
8105 ++instr;
8106 }
8107 }
8108
8109 retvar->var_val.var_string = ga.ga_data;
8110}
8111
8112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 * "type(expr)" function
8114 */
8115 static void
8116f_type(argvars, retvar)
8117 VAR argvars;
8118 VAR retvar;
8119{
8120 if (argvars[0].var_type == VAR_NUMBER)
8121 retvar->var_val.var_number = 0;
8122 else
8123 retvar->var_val.var_number = 1;
8124}
8125
8126/*
8127 * "virtcol(string)" function
8128 */
8129 static void
8130f_virtcol(argvars, retvar)
8131 VAR argvars;
8132 VAR retvar;
8133{
8134 colnr_T vcol = 0;
8135 pos_T *fp;
8136
8137 fp = var2fpos(&argvars[0], FALSE);
8138 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
8139 {
8140 getvvcol(curwin, fp, NULL, NULL, &vcol);
8141 ++vcol;
8142 }
8143
8144 retvar->var_val.var_number = vcol;
8145}
8146
8147/*
8148 * "visualmode()" function
8149 */
8150/*ARGSUSED*/
8151 static void
8152f_visualmode(argvars, retvar)
8153 VAR argvars;
8154 VAR retvar;
8155{
8156#ifdef FEAT_VISUAL
8157 char_u str[2];
8158
8159 retvar->var_type = VAR_STRING;
8160 str[0] = curbuf->b_visual_mode_eval;
8161 str[1] = NUL;
8162 retvar->var_val.var_string = vim_strsave(str);
8163
8164 /* A non-zero number or non-empty string argument: reset mode. */
8165 if ((argvars[0].var_type == VAR_NUMBER
8166 && argvars[0].var_val.var_number != 0)
8167 || (argvars[0].var_type == VAR_STRING
8168 && *get_var_string(&argvars[0]) != NUL))
8169 curbuf->b_visual_mode_eval = NUL;
8170#else
8171 retvar->var_val.var_number = 0; /* return anything, it won't work anyway */
8172#endif
8173}
8174
8175/*
8176 * "winbufnr(nr)" function
8177 */
8178 static void
8179f_winbufnr(argvars, retvar)
8180 VAR argvars;
8181 VAR retvar;
8182{
8183 win_T *wp;
8184
8185 wp = find_win_by_nr(&argvars[0]);
8186 if (wp == NULL)
8187 retvar->var_val.var_number = -1;
8188 else
8189 retvar->var_val.var_number = wp->w_buffer->b_fnum;
8190}
8191
8192/*
8193 * "wincol()" function
8194 */
8195/*ARGSUSED*/
8196 static void
8197f_wincol(argvars, retvar)
8198 VAR argvars;
8199 VAR retvar;
8200{
8201 validate_cursor();
8202 retvar->var_val.var_number = curwin->w_wcol + 1;
8203}
8204
8205/*
8206 * "winheight(nr)" function
8207 */
8208 static void
8209f_winheight(argvars, retvar)
8210 VAR argvars;
8211 VAR retvar;
8212{
8213 win_T *wp;
8214
8215 wp = find_win_by_nr(&argvars[0]);
8216 if (wp == NULL)
8217 retvar->var_val.var_number = -1;
8218 else
8219 retvar->var_val.var_number = wp->w_height;
8220}
8221
8222/*
8223 * "winline()" function
8224 */
8225/*ARGSUSED*/
8226 static void
8227f_winline(argvars, retvar)
8228 VAR argvars;
8229 VAR retvar;
8230{
8231 validate_cursor();
8232 retvar->var_val.var_number = curwin->w_wrow + 1;
8233}
8234
8235/*
8236 * "winnr()" function
8237 */
8238/* ARGSUSED */
8239 static void
8240f_winnr(argvars, retvar)
8241 VAR argvars;
8242 VAR retvar;
8243{
8244 int nr = 1;
8245#ifdef FEAT_WINDOWS
8246 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008247 win_T *twin = curwin;
8248 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008250 if (argvars[0].var_type != VAR_UNKNOWN)
8251 {
8252 arg = get_var_string(&argvars[0]);
8253 if (STRCMP(arg, "$") == 0)
8254 twin = lastwin;
8255 else if (STRCMP(arg, "#") == 0)
8256 {
8257 twin = prevwin;
8258 if (prevwin == NULL)
8259 nr = 0;
8260 }
8261 else
8262 {
8263 EMSG2(_(e_invexpr2), arg);
8264 nr = 0;
8265 }
8266 }
8267
8268 if (nr > 0)
8269 for (wp = firstwin; wp != twin; wp = wp->w_next)
8270 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271#endif
8272 retvar->var_val.var_number = nr;
8273}
8274
8275/*
8276 * "winrestcmd()" function
8277 */
8278/* ARGSUSED */
8279 static void
8280f_winrestcmd(argvars, retvar)
8281 VAR argvars;
8282 VAR retvar;
8283{
8284#ifdef FEAT_WINDOWS
8285 win_T *wp;
8286 int winnr = 1;
8287 garray_T ga;
8288 char_u buf[50];
8289
8290 ga_init2(&ga, (int)sizeof(char), 70);
8291 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8292 {
8293 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
8294 ga_concat(&ga, buf);
8295# ifdef FEAT_VERTSPLIT
8296 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
8297 ga_concat(&ga, buf);
8298# endif
8299 ++winnr;
8300 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00008301 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302
8303 retvar->var_val.var_string = ga.ga_data;
8304#else
8305 retvar->var_val.var_string = NULL;
8306#endif
8307 retvar->var_type = VAR_STRING;
8308}
8309
8310/*
8311 * "winwidth(nr)" function
8312 */
8313 static void
8314f_winwidth(argvars, retvar)
8315 VAR argvars;
8316 VAR retvar;
8317{
8318 win_T *wp;
8319
8320 wp = find_win_by_nr(&argvars[0]);
8321 if (wp == NULL)
8322 retvar->var_val.var_number = -1;
8323 else
8324#ifdef FEAT_VERTSPLIT
8325 retvar->var_val.var_number = wp->w_width;
8326#else
8327 retvar->var_val.var_number = Columns;
8328#endif
8329}
8330
8331 static win_T *
8332find_win_by_nr(vp)
8333 VAR vp;
8334{
8335#ifdef FEAT_WINDOWS
8336 win_T *wp;
8337#endif
8338 int nr;
8339
8340 nr = get_var_number(vp);
8341
8342#ifdef FEAT_WINDOWS
8343 if (nr == 0)
8344 return curwin;
8345
8346 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8347 if (--nr <= 0)
8348 break;
8349 return wp;
8350#else
8351 if (nr == 0 || nr == 1)
8352 return curwin;
8353 return NULL;
8354#endif
8355}
8356
8357/*
8358 * Translate a String variable into a position.
8359 */
8360 static pos_T *
8361var2fpos(varp, lnum)
8362 VAR varp;
8363 int lnum; /* TRUE when $ is last line */
8364{
8365 char_u *name;
8366 static pos_T pos;
8367 pos_T *pp;
8368
8369 name = get_var_string(varp);
8370 if (name[0] == '.') /* cursor */
8371 return &curwin->w_cursor;
8372 if (name[0] == '\'') /* mark */
8373 {
8374 pp = getmark(name[1], FALSE);
8375 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
8376 return NULL;
8377 return pp;
8378 }
8379 if (name[0] == '$') /* last column or line */
8380 {
8381 if (lnum)
8382 {
8383 pos.lnum = curbuf->b_ml.ml_line_count;
8384 pos.col = 0;
8385 }
8386 else
8387 {
8388 pos.lnum = curwin->w_cursor.lnum;
8389 pos.col = (colnr_T)STRLEN(ml_get_curline());
8390 }
8391 return &pos;
8392 }
8393 return NULL;
8394}
8395
8396/*
8397 * Get the length of an environment variable name.
8398 * Advance "arg" to the first character after the name.
8399 * Return 0 for error.
8400 */
8401 static int
8402get_env_len(arg)
8403 char_u **arg;
8404{
8405 char_u *p;
8406 int len;
8407
8408 for (p = *arg; vim_isIDc(*p); ++p)
8409 ;
8410 if (p == *arg) /* no name found */
8411 return 0;
8412
8413 len = (int)(p - *arg);
8414 *arg = p;
8415 return len;
8416}
8417
8418/*
8419 * Get the length of the name of a function or internal variable.
8420 * "arg" is advanced to the first non-white character after the name.
8421 * Return 0 if something is wrong.
8422 */
8423 static int
8424get_id_len(arg)
8425 char_u **arg;
8426{
8427 char_u *p;
8428 int len;
8429
8430 /* Find the end of the name. */
8431 for (p = *arg; eval_isnamec(*p); ++p)
8432 ;
8433 if (p == *arg) /* no name found */
8434 return 0;
8435
8436 len = (int)(p - *arg);
8437 *arg = skipwhite(p);
8438
8439 return len;
8440}
8441
8442/*
8443 * Get the length of the name of a function.
8444 * "arg" is advanced to the first non-white character after the name.
8445 * Return 0 if something is wrong.
8446 * If the name contains 'magic' {}'s, expand them and return the
8447 * expanded name in an allocated string via 'alias' - caller must free.
8448 */
8449 static int
8450get_func_len(arg, alias, evaluate)
8451 char_u **arg;
8452 char_u **alias;
8453 int evaluate;
8454{
8455 int len;
8456#ifdef FEAT_MAGIC_BRACES
8457 char_u *p;
8458 char_u *expr_start;
8459 char_u *expr_end;
8460#endif
8461
8462 *alias = NULL; /* default to no alias */
8463
8464 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
8465 && (*arg)[2] == (int)KE_SNR)
8466 {
8467 /* hard coded <SNR>, already translated */
8468 *arg += 3;
8469 return get_id_len(arg) + 3;
8470 }
8471 len = eval_fname_script(*arg);
8472 if (len > 0)
8473 {
8474 /* literal "<SID>", "s:" or "<SNR>" */
8475 *arg += len;
8476 }
8477
8478#ifdef FEAT_MAGIC_BRACES
8479 /*
8480 * Find the end of the name;
8481 */
8482 p = find_name_end(*arg, &expr_start, &expr_end);
8483 /* check for {} construction */
8484 if (expr_start != NULL)
8485 {
8486 char_u *temp_string;
8487
8488 if (!evaluate)
8489 {
8490 len += (int)(p - *arg);
8491 *arg = skipwhite(p);
8492 return len;
8493 }
8494
8495 /*
8496 * Include any <SID> etc in the expanded string:
8497 * Thus the -len here.
8498 */
8499 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
8500 if (temp_string == NULL)
8501 return 0;
8502 *alias = temp_string;
8503 *arg = skipwhite(p);
8504 return (int)STRLEN(temp_string);
8505 }
8506#endif
8507
8508 len += get_id_len(arg);
8509 if (len == 0)
8510 EMSG2(_(e_invexpr2), *arg);
8511
8512 return len;
8513}
8514
8515 static char_u *
8516find_name_end(arg, expr_start, expr_end)
8517 char_u *arg;
8518 char_u **expr_start;
8519 char_u **expr_end;
8520{
8521 int nesting = 0;
8522 char_u *p;
8523
8524 *expr_start = NULL;
8525 *expr_end = NULL;
8526
8527 for (p = arg; (*p != NUL && (eval_isnamec(*p) || nesting != 0)); ++p)
8528 {
8529#ifdef FEAT_MAGIC_BRACES
8530 if (*p == '{')
8531 {
8532 nesting++;
8533 if (*expr_start == NULL)
8534 *expr_start = p;
8535 }
8536 else if (*p == '}')
8537 {
8538 nesting--;
8539 if (nesting == 0 && *expr_end == NULL)
8540 *expr_end = p;
8541 }
8542#endif
8543 }
8544
8545 return p;
8546}
8547
8548/*
8549 * Return TRUE if character "c" can be used in a variable or function name.
8550 */
8551 static int
8552eval_isnamec(c)
8553 int c;
8554{
8555 return (ASCII_ISALNUM(c) || c == '_' || c == ':'
8556#ifdef FEAT_MAGIC_BRACES
8557 || c == '{' || c == '}'
8558#endif
8559 );
8560}
8561
8562/*
8563 * Find a v: variable.
8564 * Return it's index, or -1 if not found.
8565 */
8566 static int
8567find_vim_var(name, len)
8568 char_u *name;
8569 int len; /* length of "name" */
8570{
8571 char_u *vname;
8572 int vlen;
8573 int i;
8574
8575 /*
8576 * Ignore "v:" for old built-in variables, require it for new ones.
8577 */
8578 if (name[0] == 'v' && name[1] == ':')
8579 {
8580 vname = name + 2;
8581 vlen = len - 2;
8582 }
8583 else
8584 {
8585 vname = name;
8586 vlen = len;
8587 }
8588 for (i = 0; i < VV_LEN; ++i)
8589 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
8590 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
8591 return i;
8592 return -1;
8593}
8594
8595/*
8596 * Set number v: variable to "val".
8597 */
8598 void
8599set_vim_var_nr(idx, val)
8600 int idx;
8601 long val;
8602{
8603 vimvars[idx].val = (char_u *)val;
8604}
8605
8606/*
8607 * Get number v: variable value;
8608 */
8609 long
8610get_vim_var_nr(idx)
8611 int idx;
8612{
8613 return (long)vimvars[idx].val;
8614}
8615
8616/*
8617 * Set v:count, v:count1 and v:prevcount.
8618 */
8619 void
8620set_vcount(count, count1)
8621 long count;
8622 long count1;
8623{
8624 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
8625 vimvars[VV_COUNT].val = (char_u *)count;
8626 vimvars[VV_COUNT1].val = (char_u *)count1;
8627}
8628
8629/*
8630 * Set string v: variable to a copy of "val".
8631 */
8632 void
8633set_vim_var_string(idx, val, len)
8634 int idx;
8635 char_u *val;
8636 int len; /* length of "val" to use or -1 (whole string) */
8637{
8638 vim_free(vimvars[idx].val);
8639 if (val == NULL)
8640 vimvars[idx].val = NULL;
8641 else if (len == -1)
8642 vimvars[idx].val = vim_strsave(val);
8643 else
8644 vimvars[idx].val = vim_strnsave(val, len);
8645}
8646
8647/*
8648 * Set v:register if needed.
8649 */
8650 void
8651set_reg_var(c)
8652 int c;
8653{
8654 char_u regname;
8655
8656 if (c == 0 || c == ' ')
8657 regname = '"';
8658 else
8659 regname = c;
8660 /* Avoid free/alloc when the value is already right. */
8661 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
8662 set_vim_var_string(VV_REG, &regname, 1);
8663}
8664
8665/*
8666 * Get or set v:exception. If "oldval" == NULL, return the current value.
8667 * Otherwise, restore the value to "oldval" and return NULL.
8668 * Must always be called in pairs to save and restore v:exception! Does not
8669 * take care of memory allocations.
8670 */
8671 char_u *
8672v_exception(oldval)
8673 char_u *oldval;
8674{
8675 if (oldval == NULL)
8676 return vimvars[VV_EXCEPTION].val;
8677
8678 vimvars[VV_EXCEPTION].val = oldval;
8679 return NULL;
8680}
8681
8682/*
8683 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
8684 * Otherwise, restore the value to "oldval" and return NULL.
8685 * Must always be called in pairs to save and restore v:throwpoint! Does not
8686 * take care of memory allocations.
8687 */
8688 char_u *
8689v_throwpoint(oldval)
8690 char_u *oldval;
8691{
8692 if (oldval == NULL)
8693 return vimvars[VV_THROWPOINT].val;
8694
8695 vimvars[VV_THROWPOINT].val = oldval;
8696 return NULL;
8697}
8698
8699#if defined(FEAT_AUTOCMD) || defined(PROTO)
8700/*
8701 * Set v:cmdarg.
8702 * If "eap" != NULL, use "eap" to generate the value and return the old value.
8703 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
8704 * Must always be called in pairs!
8705 */
8706 char_u *
8707set_cmdarg(eap, oldarg)
8708 exarg_T *eap;
8709 char_u *oldarg;
8710{
8711 char_u *oldval;
8712 char_u *newval;
8713 unsigned len;
8714
8715 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008716 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008718 vim_free(oldval);
8719 vimvars[VV_CMDARG].val = oldarg;
8720 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 }
8722
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008723 if (eap->force_bin == FORCE_BIN)
8724 len = 6;
8725 else if (eap->force_bin == FORCE_NOBIN)
8726 len = 8;
8727 else
8728 len = 0;
8729 if (eap->force_ff != 0)
8730 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
8731# ifdef FEAT_MBYTE
8732 if (eap->force_enc != 0)
8733 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
8734# endif
8735
8736 newval = alloc(len + 1);
8737 if (newval == NULL)
8738 return NULL;
8739
8740 if (eap->force_bin == FORCE_BIN)
8741 sprintf((char *)newval, " ++bin");
8742 else if (eap->force_bin == FORCE_NOBIN)
8743 sprintf((char *)newval, " ++nobin");
8744 else
8745 *newval = NUL;
8746 if (eap->force_ff != 0)
8747 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
8748 eap->cmd + eap->force_ff);
8749# ifdef FEAT_MBYTE
8750 if (eap->force_enc != 0)
8751 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
8752 eap->cmd + eap->force_enc);
8753# endif
8754 vimvars[VV_CMDARG].val = newval;
8755 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
8757#endif
8758
8759/*
8760 * Get the value of internal variable "name".
8761 * Return OK or FAIL.
8762 */
8763 static int
8764get_var_var(name, len, retvar)
8765 char_u *name;
8766 int len; /* length of "name" */
8767 VAR retvar; /* NULL when only checking existence */
8768{
8769 int ret = OK;
8770 int type = VAR_UNKNOWN;
8771 long number = 1;
8772 char_u *string = NULL;
8773 VAR v;
8774 int cc;
8775 int i;
8776
8777 /* truncate the name, so that we can use strcmp() */
8778 cc = name[len];
8779 name[len] = NUL;
8780
8781 /*
8782 * Check for "b:changedtick".
8783 */
8784 if (STRCMP(name, "b:changedtick") == 0)
8785 {
8786 type = VAR_NUMBER;
8787 number = curbuf->b_changedtick;
8788 }
8789
8790 /*
8791 * Check for built-in v: variables.
8792 */
8793 else if ((i = find_vim_var(name, len)) >= 0)
8794 {
8795 type = vimvars[i].type;
8796 number = (long)vimvars[i].val;
8797 string = vimvars[i].val;
8798 }
8799
8800 /*
8801 * Check for user-defined variables.
8802 */
8803 else
8804 {
8805 v = find_var(name, FALSE);
8806 if (v != NULL)
8807 {
8808 type = v->var_type;
8809 number = v->var_val.var_number;
8810 string = v->var_val.var_string;
8811 }
8812 }
8813
8814 if (type == VAR_UNKNOWN)
8815 {
8816 if (retvar != NULL)
8817 EMSG2(_("E121: Undefined variable: %s"), name);
8818 ret = FAIL;
8819 }
8820 else if (retvar != NULL)
8821 {
8822 retvar->var_type = type;
8823 if (type == VAR_NUMBER)
8824 retvar->var_val.var_number = number;
8825 else if (type == VAR_STRING)
8826 {
8827 if (string != NULL)
8828 string = vim_strsave(string);
8829 retvar->var_val.var_string = string;
8830 }
8831 }
8832
8833 name[len] = cc;
8834
8835 return ret;
8836}
8837
8838/*
8839 * Allocate memory for a variable, and make it emtpy (0 or NULL value).
8840 */
8841 static VAR
8842alloc_var()
8843{
8844 return (VAR)alloc_clear((unsigned)sizeof(var));
8845}
8846
8847/*
8848 * Allocate memory for a variable, and assign a string to it.
8849 * The string "s" must have been allocated, it is consumed.
8850 * Return NULL for out of memory, the variable otherwise.
8851 */
8852 static VAR
8853alloc_string_var(s)
8854 char_u *s;
8855{
8856 VAR retvar;
8857
8858 retvar = alloc_var();
8859 if (retvar != NULL)
8860 {
8861 retvar->var_type = VAR_STRING;
8862 retvar->var_val.var_string = s;
8863 }
8864 else
8865 vim_free(s);
8866 return retvar;
8867}
8868
8869/*
8870 * Free the memory for a variable.
8871 */
8872 static void
8873free_var(varp)
8874 VAR varp;
8875{
8876 if (varp != NULL)
8877 {
8878 if (varp->var_type == VAR_STRING)
8879 vim_free(varp->var_val.var_string);
8880 vim_free(varp->var_name);
8881 vim_free(varp);
8882 }
8883}
8884
8885/*
8886 * Free the memory for a variable value and set the value to NULL or 0.
8887 */
8888 static void
8889clear_var(varp)
8890 VAR varp;
8891{
8892 if (varp != NULL)
8893 {
8894 if (varp->var_type == VAR_STRING)
8895 {
8896 vim_free(varp->var_val.var_string);
8897 varp->var_val.var_string = NULL;
8898 }
8899 else
8900 varp->var_val.var_number = 0;
8901 }
8902}
8903
8904/*
8905 * Get the number value of a variable.
8906 * If it is a String variable, uses vim_str2nr().
8907 */
8908 static long
8909get_var_number(varp)
8910 VAR varp;
8911{
8912 long n;
8913
8914 if (varp->var_type == VAR_NUMBER)
8915 return (long)(varp->var_val.var_number);
8916 else if (varp->var_type == VAR_UNKNOWN || varp->var_val.var_string == NULL)
8917 return 0L;
8918 else
8919 {
8920 vim_str2nr(varp->var_val.var_string, NULL, NULL, TRUE, TRUE, &n, NULL);
8921 return n;
8922 }
8923}
8924
8925/*
8926 * Get the lnum from the first argument. Also accepts ".", "$", etc.
8927 */
8928 static linenr_T
8929get_var_lnum(argvars)
8930 VAR argvars;
8931{
8932 var retvar;
8933 linenr_T lnum;
8934
8935 lnum = get_var_number(&argvars[0]);
8936 if (lnum == 0) /* no valid number, try using line() */
8937 {
8938 retvar.var_type = VAR_NUMBER;
8939 f_line(argvars, &retvar);
8940 lnum = retvar.var_val.var_number;
8941 clear_var(&retvar);
8942 }
8943 return lnum;
8944}
8945
8946/*
8947 * Get the string value of a variable.
8948 * If it is a Number variable, the number is converted into a string.
8949 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
8950 * get_var_string_buf() uses a given buffer.
8951 * If the String variable has never been set, return an empty string.
8952 * Never returns NULL;
8953 */
8954 static char_u *
8955get_var_string(varp)
8956 VAR varp;
8957{
8958 static char_u mybuf[NUMBUFLEN];
8959
8960 return get_var_string_buf(varp, mybuf);
8961}
8962
8963 static char_u *
8964get_var_string_buf(varp, buf)
8965 VAR varp;
8966 char_u *buf;
8967{
8968 if (varp->var_type == VAR_NUMBER)
8969 {
8970 sprintf((char *)buf, "%ld", (long)varp->var_val.var_number);
8971 return buf;
8972 }
8973 else if (varp->var_val.var_string == NULL)
8974 return (char_u *)"";
8975 else
8976 return varp->var_val.var_string;
8977}
8978
8979/*
8980 * Find variable "name" in the list of variables.
8981 * Return a pointer to it if found, NULL if not found.
8982 */
8983 static VAR
8984find_var(name, writing)
8985 char_u *name;
8986 int writing;
8987{
8988 int i;
8989 char_u *varname;
8990 garray_T *gap;
8991
8992 /* Check for function arguments "a:" */
8993 if (name[0] == 'a' && name[1] == ':')
8994 {
8995 if (writing)
8996 {
8997 EMSG2(_(e_readonlyvar), name);
8998 return NULL;
8999 }
9000 name += 2;
9001 if (current_funccal == NULL)
9002 return NULL;
9003 if (VIM_ISDIGIT(*name))
9004 {
9005 i = atol((char *)name);
9006 if (i == 0) /* a:0 */
9007 return &current_funccal->a0_var;
9008 i += current_funccal->func->args.ga_len;
9009 if (i > current_funccal->argcount) /* a:999 */
9010 return NULL;
9011 return &(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
9012 }
9013 if (STRCMP(name, "firstline") == 0)
9014 return &(current_funccal->firstline);
9015 if (STRCMP(name, "lastline") == 0)
9016 return &(current_funccal->lastline);
9017 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
9018 if (STRCMP(name, ((char_u **)
9019 (current_funccal->func->args.ga_data))[i]) == 0)
9020 return &(current_funccal->argvars[i]); /* a:name */
9021 return NULL;
9022 }
9023
9024 gap = find_var_ga(name, &varname);
9025 if (gap == NULL)
9026 return NULL;
9027 return find_var_in_ga(gap, varname);
9028}
9029
9030 static VAR
9031find_var_in_ga(gap, varname)
9032 garray_T *gap;
9033 char_u *varname;
9034{
9035 int i;
9036
9037 for (i = gap->ga_len; --i >= 0; )
9038 if (VAR_GAP_ENTRY(i, gap).var_name != NULL
9039 && STRCMP(VAR_GAP_ENTRY(i, gap).var_name, varname) == 0)
9040 break;
9041 if (i < 0)
9042 return NULL;
9043 return &VAR_GAP_ENTRY(i, gap);
9044}
9045
9046/*
9047 * Find the growarray and start of name without ':' for a variable name.
9048 */
9049 static garray_T *
9050find_var_ga(name, varname)
9051 char_u *name;
9052 char_u **varname;
9053{
9054 if (name[1] != ':')
9055 {
9056 /* If not "x:name" there must not be any ":" in the name. */
9057 if (vim_strchr(name, ':') != NULL)
9058 return NULL;
9059 *varname = name;
9060 if (current_funccal == NULL)
9061 return &variables; /* global variable */
9062 return &current_funccal->l_vars; /* local function variable */
9063 }
9064 *varname = name + 2;
9065 if (*name == 'b') /* buffer variable */
9066 return &curbuf->b_vars;
9067 if (*name == 'w') /* window variable */
9068 return &curwin->w_vars;
9069 if (*name == 'g') /* global variable */
9070 return &variables;
9071 if (*name == 'l' && current_funccal != NULL)/* local function variable */
9072 return &current_funccal->l_vars;
9073 if (*name == 's' /* script variable */
9074 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
9075 return &SCRIPT_VARS(current_SID);
9076 return NULL;
9077}
9078
9079/*
9080 * Get the string value of a (global/local) variable.
9081 * Returns NULL when it doesn't exist.
9082 */
9083 char_u *
9084get_var_value(name)
9085 char_u *name;
9086{
9087 VAR v;
9088
9089 v = find_var(name, FALSE);
9090 if (v == NULL)
9091 return NULL;
9092 return get_var_string(v);
9093}
9094
9095/*
9096 * Allocate a new growarry for a sourced script. It will be used while
9097 * sourcing this script and when executing functions defined in the script.
9098 */
9099 void
9100new_script_vars(id)
9101 scid_T id;
9102{
9103 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
9104 {
9105 while (ga_scripts.ga_len < id)
9106 {
9107 var_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
9108 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110 }
9111}
9112
9113/*
9114 * Initialize internal variables for use.
9115 */
9116 void
9117var_init(gap)
9118 garray_T *gap;
9119{
9120 ga_init2(gap, (int)sizeof(var), 4);
9121}
9122
9123/*
9124 * Clean up a list of internal variables.
9125 */
9126 void
9127var_clear(gap)
9128 garray_T *gap;
9129{
9130 int i;
9131
9132 for (i = gap->ga_len; --i >= 0; )
9133 var_free_one(&VAR_GAP_ENTRY(i, gap));
9134 ga_clear(gap);
9135}
9136
9137 static void
9138var_free_one(v)
9139 VAR v;
9140{
9141 vim_free(v->var_name);
9142 v->var_name = NULL;
9143 if (v->var_type == VAR_STRING)
9144 vim_free(v->var_val.var_string);
9145 v->var_val.var_string = NULL;
9146}
9147
9148/*
9149 * List the value of one internal variable.
9150 */
9151 static void
9152list_one_var(v, prefix)
9153 VAR v;
9154 char_u *prefix;
9155{
9156 list_one_var_a(prefix, v->var_name, v->var_type, get_var_string(v));
9157}
9158
9159/*
9160 * List the value of one "v:" variable.
9161 */
9162 static void
9163list_vim_var(i)
9164 int i; /* index in vimvars[] */
9165{
9166 char_u *p;
9167 char_u numbuf[NUMBUFLEN];
9168
9169 if (vimvars[i].type == VAR_NUMBER)
9170 {
9171 p = numbuf;
9172 sprintf((char *)p, "%ld", (long)vimvars[i].val);
9173 }
9174 else if (vimvars[i].val == NULL)
9175 p = (char_u *)"";
9176 else
9177 p = vimvars[i].val;
9178 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
9179 vimvars[i].type, p);
9180}
9181
9182 static void
9183list_one_var_a(prefix, name, type, string)
9184 char_u *prefix;
9185 char_u *name;
9186 int type;
9187 char_u *string;
9188{
9189 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
9190 if (name != NULL) /* "a:" vars don't have a name stored */
9191 msg_puts(name);
9192 msg_putchar(' ');
9193 msg_advance(22);
9194 if (type == VAR_NUMBER)
9195 msg_putchar('#');
9196 else
9197 msg_putchar(' ');
9198 msg_outtrans(string);
9199}
9200
9201/*
9202 * Set variable "name" to value in "varp".
9203 * If the variable already exists, the value is updated.
9204 * Otherwise the variable is created.
9205 */
9206 static void
9207set_var(name, varp)
9208 char_u *name;
9209 VAR varp;
9210{
9211 int i;
9212 VAR v;
9213 char_u *varname;
9214 garray_T *gap;
9215
9216 /*
9217 * Handle setting internal v: variables.
9218 */
9219 i = find_vim_var(name, (int)STRLEN(name));
9220 if (i >= 0)
9221 {
9222 if (vimvars[i].flags & VV_RO)
9223 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009224 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
9225 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 else
9227 {
9228 if (vimvars[i].type == VAR_STRING)
9229 {
9230 vim_free(vimvars[i].val);
9231 vimvars[i].val = vim_strsave(get_var_string(varp));
9232 }
9233 else
9234 vimvars[i].val = (char_u *)(long)varp->var_val.var_number;
9235 }
9236 return;
9237 }
9238
9239 v = find_var(name, TRUE);
9240 if (v != NULL) /* existing variable, only need to free string */
9241 {
9242 if (v->var_type == VAR_STRING)
9243 vim_free(v->var_val.var_string);
9244 }
9245 else /* add a new variable */
9246 {
9247 gap = find_var_ga(name, &varname);
9248 if (gap == NULL) /* illegal name */
9249 {
9250 EMSG2(_("E461: Illegal variable name: %s"), name);
9251 return;
9252 }
9253
9254 /* Try to use an empty entry */
9255 for (i = gap->ga_len; --i >= 0; )
9256 if (VAR_GAP_ENTRY(i, gap).var_name == NULL)
9257 break;
9258 if (i < 0) /* need to allocate more room */
9259 {
9260 if (ga_grow(gap, 1) == FAIL)
9261 return;
9262 i = gap->ga_len;
9263 }
9264 v = &VAR_GAP_ENTRY(i, gap);
9265 if ((v->var_name = vim_strsave(varname)) == NULL)
9266 return;
9267 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 copy_var(varp, v);
9271}
9272
9273 static void
9274copy_var(from, to)
9275 VAR from;
9276 VAR to;
9277{
9278 to->var_type = from->var_type;
9279 if (from->var_type == VAR_STRING)
9280 to->var_val.var_string = vim_strsave(get_var_string(from));
9281 else
9282 to->var_val.var_number = from->var_val.var_number;
9283}
9284
9285/*
9286 * ":echo expr1 ..." print each argument separated with a space, add a
9287 * newline at the end.
9288 * ":echon expr1 ..." print each argument plain.
9289 */
9290 void
9291ex_echo(eap)
9292 exarg_T *eap;
9293{
9294 char_u *arg = eap->arg;
9295 var retvar;
9296 char_u *p;
9297 int needclr = TRUE;
9298 int atstart = TRUE;
9299
9300 if (eap->skip)
9301 ++emsg_skip;
9302 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
9303 {
9304 p = arg;
9305 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9306 {
9307 /*
9308 * Report the invalid expression unless the expression evaluation
9309 * has been cancelled due to an aborting error, an interrupt, or an
9310 * exception.
9311 */
9312 if (!aborting())
9313 EMSG2(_(e_invexpr2), p);
9314 break;
9315 }
9316 if (!eap->skip)
9317 {
9318 if (atstart)
9319 {
9320 atstart = FALSE;
9321 /* Call msg_start() after eval1(), evaluating the expression
9322 * may cause a message to appear. */
9323 if (eap->cmdidx == CMD_echo)
9324 msg_start();
9325 }
9326 else if (eap->cmdidx == CMD_echo)
9327 msg_puts_attr((char_u *)" ", echo_attr);
9328 for (p = get_var_string(&retvar); *p != NUL && !got_int; ++p)
9329 if (*p == '\n' || *p == '\r' || *p == TAB)
9330 {
9331 if (*p != TAB && needclr)
9332 {
9333 /* remove any text still there from the command */
9334 msg_clr_eos();
9335 needclr = FALSE;
9336 }
9337 msg_putchar_attr(*p, echo_attr);
9338 }
9339 else
9340 {
9341#ifdef FEAT_MBYTE
9342 if (has_mbyte)
9343 {
9344 int i = (*mb_ptr2len_check)(p);
9345
9346 (void)msg_outtrans_len_attr(p, i, echo_attr);
9347 p += i - 1;
9348 }
9349 else
9350#endif
9351 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9352 }
9353 }
9354 clear_var(&retvar);
9355 arg = skipwhite(arg);
9356 }
9357 eap->nextcmd = check_nextcmd(arg);
9358
9359 if (eap->skip)
9360 --emsg_skip;
9361 else
9362 {
9363 /* remove text that may still be there from the command */
9364 if (needclr)
9365 msg_clr_eos();
9366 if (eap->cmdidx == CMD_echo)
9367 msg_end();
9368 }
9369}
9370
9371/*
9372 * ":echohl {name}".
9373 */
9374 void
9375ex_echohl(eap)
9376 exarg_T *eap;
9377{
9378 int id;
9379
9380 id = syn_name2id(eap->arg);
9381 if (id == 0)
9382 echo_attr = 0;
9383 else
9384 echo_attr = syn_id2attr(id);
9385}
9386
9387/*
9388 * ":execute expr1 ..." execute the result of an expression.
9389 * ":echomsg expr1 ..." Print a message
9390 * ":echoerr expr1 ..." Print an error
9391 * Each gets spaces around each argument and a newline at the end for
9392 * echo commands
9393 */
9394 void
9395ex_execute(eap)
9396 exarg_T *eap;
9397{
9398 char_u *arg = eap->arg;
9399 var retvar;
9400 int ret = OK;
9401 char_u *p;
9402 garray_T ga;
9403 int len;
9404 int save_did_emsg;
9405
9406 ga_init2(&ga, 1, 80);
9407
9408 if (eap->skip)
9409 ++emsg_skip;
9410 while (*arg != NUL && *arg != '|' && *arg != '\n')
9411 {
9412 p = arg;
9413 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9414 {
9415 /*
9416 * Report the invalid expression unless the expression evaluation
9417 * has been cancelled due to an aborting error, an interrupt, or an
9418 * exception.
9419 */
9420 if (!aborting())
9421 EMSG2(_(e_invexpr2), p);
9422 ret = FAIL;
9423 break;
9424 }
9425
9426 if (!eap->skip)
9427 {
9428 p = get_var_string(&retvar);
9429 len = (int)STRLEN(p);
9430 if (ga_grow(&ga, len + 2) == FAIL)
9431 {
9432 clear_var(&retvar);
9433 ret = FAIL;
9434 break;
9435 }
9436 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 ga.ga_len += len;
9440 }
9441
9442 clear_var(&retvar);
9443 arg = skipwhite(arg);
9444 }
9445
9446 if (ret != FAIL && ga.ga_data != NULL)
9447 {
9448 if (eap->cmdidx == CMD_echomsg)
9449 MSG_ATTR(ga.ga_data, echo_attr);
9450 else if (eap->cmdidx == CMD_echoerr)
9451 {
9452 /* We don't want to abort following commands, restore did_emsg. */
9453 save_did_emsg = did_emsg;
9454 EMSG((char_u *)ga.ga_data);
9455 if (!force_abort)
9456 did_emsg = save_did_emsg;
9457 }
9458 else if (eap->cmdidx == CMD_execute)
9459 do_cmdline((char_u *)ga.ga_data,
9460 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9461 }
9462
9463 ga_clear(&ga);
9464
9465 if (eap->skip)
9466 --emsg_skip;
9467
9468 eap->nextcmd = check_nextcmd(arg);
9469}
9470
9471/*
9472 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9473 * "arg" points to the "&" or '+' when called, to "option" when returning.
9474 * Returns NULL when no option name found. Otherwise pointer to the char
9475 * after the option name.
9476 */
9477 static char_u *
9478find_option_end(arg, opt_flags)
9479 char_u **arg;
9480 int *opt_flags;
9481{
9482 char_u *p = *arg;
9483
9484 ++p;
9485 if (*p == 'g' && p[1] == ':')
9486 {
9487 *opt_flags = OPT_GLOBAL;
9488 p += 2;
9489 }
9490 else if (*p == 'l' && p[1] == ':')
9491 {
9492 *opt_flags = OPT_LOCAL;
9493 p += 2;
9494 }
9495 else
9496 *opt_flags = 0;
9497
9498 if (!ASCII_ISALPHA(*p))
9499 return NULL;
9500 *arg = p;
9501
9502 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9503 p += 4; /* termcap option */
9504 else
9505 while (ASCII_ISALPHA(*p))
9506 ++p;
9507 return p;
9508}
9509
9510/*
9511 * ":function"
9512 */
9513 void
9514ex_function(eap)
9515 exarg_T *eap;
9516{
9517 char_u *theline;
9518 int j;
9519 int c;
9520#ifdef FEAT_MAGIC_BRACES
9521 int saved_did_emsg;
9522#endif
9523 char_u *name = NULL;
9524 char_u *p;
9525 char_u *arg;
9526 garray_T newargs;
9527 garray_T newlines;
9528 int varargs = FALSE;
9529 int mustend = FALSE;
9530 int flags = 0;
9531 ufunc_T *fp;
9532 int indent;
9533 int nesting;
9534 char_u *skip_until = NULL;
9535 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
9536
9537 /*
9538 * ":function" without argument: list functions.
9539 */
9540 if (ends_excmd(*eap->arg))
9541 {
9542 if (!eap->skip)
9543 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
9544 list_func_head(fp, FALSE);
9545 eap->nextcmd = check_nextcmd(eap->arg);
9546 return;
9547 }
9548
9549 p = eap->arg;
9550 name = trans_function_name(&p, eap->skip, FALSE);
9551 if (name == NULL && !eap->skip)
9552 {
9553 /*
9554 * Return on an invalid expression in braces, unless the expression
9555 * evaluation has been cancelled due to an aborting error, an
9556 * interrupt, or an exception.
9557 */
9558 if (!aborting())
9559 return;
9560 else
9561 eap->skip = TRUE;
9562 }
9563#ifdef FEAT_MAGIC_BRACES
9564 /* An error in a function call during evaluation of an expression in magic
9565 * braces should not cause the function not to be defined. */
9566 saved_did_emsg = did_emsg;
9567 did_emsg = FALSE;
9568#endif
9569
9570 /*
9571 * ":function func" with only function name: list function.
9572 */
9573 if (vim_strchr(p, '(') == NULL)
9574 {
9575 if (!ends_excmd(*skipwhite(p)))
9576 {
9577 EMSG(_(e_trailing));
9578 goto erret_name;
9579 }
9580 eap->nextcmd = check_nextcmd(p);
9581 if (eap->nextcmd != NULL)
9582 *p = NUL;
9583 if (!eap->skip && !got_int)
9584 {
9585 fp = find_func(name);
9586 if (fp != NULL)
9587 {
9588 list_func_head(fp, TRUE);
9589 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
9590 {
9591 msg_putchar('\n');
9592 msg_outnum((long)(j + 1));
9593 if (j < 9)
9594 msg_putchar(' ');
9595 if (j < 99)
9596 msg_putchar(' ');
9597 msg_prt_line(FUNCLINE(fp, j));
9598 out_flush(); /* show a line at a time */
9599 ui_breakcheck();
9600 }
9601 if (!got_int)
9602 {
9603 msg_putchar('\n');
9604 msg_puts((char_u *)" endfunction");
9605 }
9606 }
9607 else
9608 EMSG2(_("E123: Undefined function: %s"), eap->arg);
9609 }
9610 goto erret_name;
9611 }
9612
9613 /*
9614 * ":function name(arg1, arg2)" Define function.
9615 */
9616 p = skipwhite(p);
9617 if (*p != '(')
9618 {
9619 if (!eap->skip)
9620 {
9621 EMSG2(_("E124: Missing '(': %s"), eap->arg);
9622 goto erret_name;
9623 }
9624 /* attempt to continue by skipping some text */
9625 if (vim_strchr(p, '(') != NULL)
9626 p = vim_strchr(p, '(');
9627 }
9628 p = skipwhite(p + 1);
9629
9630 ga_init2(&newargs, (int)sizeof(char_u *), 3);
9631 ga_init2(&newlines, (int)sizeof(char_u *), 3);
9632
9633 /*
9634 * Isolate the arguments: "arg1, arg2, ...)"
9635 */
9636 while (*p != ')')
9637 {
9638 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
9639 {
9640 varargs = TRUE;
9641 p += 3;
9642 mustend = TRUE;
9643 }
9644 else
9645 {
9646 arg = p;
9647 while (ASCII_ISALNUM(*p) || *p == '_')
9648 ++p;
9649 if (arg == p || isdigit(*arg)
9650 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
9651 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
9652 {
9653 if (!eap->skip)
9654 EMSG2(_("E125: Illegal argument: %s"), arg);
9655 break;
9656 }
9657 if (ga_grow(&newargs, 1) == FAIL)
9658 goto erret;
9659 c = *p;
9660 *p = NUL;
9661 arg = vim_strsave(arg);
9662 if (arg == NULL)
9663 goto erret;
9664 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
9665 *p = c;
9666 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 if (*p == ',')
9668 ++p;
9669 else
9670 mustend = TRUE;
9671 }
9672 p = skipwhite(p);
9673 if (mustend && *p != ')')
9674 {
9675 if (!eap->skip)
9676 EMSG2(_(e_invarg2), eap->arg);
9677 break;
9678 }
9679 }
9680 ++p; /* skip the ')' */
9681
9682 /* find extra arguments "range" and "abort" */
9683 for (;;)
9684 {
9685 p = skipwhite(p);
9686 if (STRNCMP(p, "range", 5) == 0)
9687 {
9688 flags |= FC_RANGE;
9689 p += 5;
9690 }
9691 else if (STRNCMP(p, "abort", 5) == 0)
9692 {
9693 flags |= FC_ABORT;
9694 p += 5;
9695 }
9696 else
9697 break;
9698 }
9699
9700 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
9701 EMSG(_(e_trailing));
9702
9703 /*
9704 * Read the body of the function, until ":endfunction" is found.
9705 */
9706 if (KeyTyped)
9707 {
9708 /* Check if the function already exists, don't let the user type the
9709 * whole function before telling him it doesn't work! For a script we
9710 * need to skip the body to be able to find what follows. */
9711 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
9712 EMSG2(_(e_funcexts), name);
9713
9714 msg_putchar('\n'); /* don't overwrite the function name */
9715 cmdline_row = msg_row;
9716 }
9717
9718 indent = 2;
9719 nesting = 0;
9720 for (;;)
9721 {
9722 msg_scroll = TRUE;
9723 need_wait_return = FALSE;
9724 if (eap->getline == NULL)
9725 theline = getcmdline(':', 0L, indent);
9726 else
9727 theline = eap->getline(':', eap->cookie, indent);
9728 if (KeyTyped)
9729 lines_left = Rows - 1;
9730 if (theline == NULL)
9731 {
9732 EMSG(_("E126: Missing :endfunction"));
9733 goto erret;
9734 }
9735
9736 if (skip_until != NULL)
9737 {
9738 /* between ":append" and "." and between ":python <<EOF" and "EOF"
9739 * don't check for ":endfunc". */
9740 if (STRCMP(theline, skip_until) == 0)
9741 {
9742 vim_free(skip_until);
9743 skip_until = NULL;
9744 }
9745 }
9746 else
9747 {
9748 /* skip ':' and blanks*/
9749 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
9750 ;
9751
9752 /* Check for "endfunction" (should be more strict...). */
9753 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
9754 {
9755 vim_free(theline);
9756 break;
9757 }
9758
9759 /* Increase indent inside "if", "while", and "try", decrease
9760 * at "end". */
9761 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
9762 indent -= 2;
9763 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
9764 || STRNCMP(p, "try", 3) == 0)
9765 indent += 2;
9766
9767 /* Check for defining a function inside this function. */
9768 if (STRNCMP(p, "fu", 2) == 0)
9769 {
9770 p = skipwhite(skiptowhite(p));
9771 p += eval_fname_script(p);
9772 if (ASCII_ISALPHA(*p))
9773 {
9774 vim_free(trans_function_name(&p, TRUE, FALSE));
9775 if (*skipwhite(p) == '(')
9776 {
9777 ++nesting;
9778 indent += 2;
9779 }
9780 }
9781 }
9782
9783 /* Check for ":append" or ":insert". */
9784 p = skip_range(p, NULL);
9785 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
9786 || (p[0] == 'i'
9787 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
9788 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
9789 skip_until = vim_strsave((char_u *)".");
9790
9791 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
9792 arg = skipwhite(skiptowhite(p));
9793 if (arg[0] == '<' && arg[1] =='<'
9794 && ((p[0] == 'p' && p[1] == 'y'
9795 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
9796 || (p[0] == 'p' && p[1] == 'e'
9797 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
9798 || (p[0] == 't' && p[1] == 'c'
9799 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
9800 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
9801 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009802 || (p[0] == 'm' && p[1] == 'z'
9803 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 ))
9805 {
9806 /* ":python <<" continues until a dot, like ":append" */
9807 p = skipwhite(arg + 2);
9808 if (*p == NUL)
9809 skip_until = vim_strsave((char_u *)".");
9810 else
9811 skip_until = vim_strsave(p);
9812 }
9813 }
9814
9815 /* Add the line to the function. */
9816 if (ga_grow(&newlines, 1) == FAIL)
9817 goto erret;
9818 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
9819 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 }
9821
9822 /* Don't define the function when skipping commands or when an error was
9823 * detected. */
9824 if (eap->skip || did_emsg)
9825 goto erret;
9826
9827 /*
9828 * If there are no errors, add the function
9829 */
9830 fp = find_func(name);
9831 if (fp != NULL)
9832 {
9833 if (!eap->forceit)
9834 {
9835 EMSG2(_(e_funcexts), name);
9836 goto erret;
9837 }
9838 if (fp->calls)
9839 {
9840 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
9841 goto erret;
9842 }
9843 /* redefine existing function */
9844 ga_clear_strings(&(fp->args));
9845 ga_clear_strings(&(fp->lines));
9846 vim_free(name);
9847 }
9848 else
9849 {
9850 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
9851 if (fp == NULL)
9852 goto erret;
9853 /* insert the new function in the function list */
9854 fp->next = firstfunc;
9855 firstfunc = fp;
9856 fp->name = name;
9857 }
9858 fp->args = newargs;
9859 fp->lines = newlines;
9860 fp->varargs = varargs;
9861 fp->flags = flags;
9862 fp->calls = 0;
9863 fp->script_ID = current_SID;
9864#ifdef FEAT_MAGIC_BRACES
9865 did_emsg |= saved_did_emsg;
9866#endif
9867 vim_free(skip_until);
9868 return;
9869
9870erret:
9871 vim_free(skip_until);
9872 ga_clear_strings(&newargs);
9873 ga_clear_strings(&newlines);
9874erret_name:
9875 vim_free(name);
9876#ifdef FEAT_MAGIC_BRACES
9877 did_emsg |= saved_did_emsg;
9878#endif
9879}
9880
9881/*
9882 * Get a function name, translating "<SID>" and "<SNR>".
9883 * Returns the function name in allocated memory, or NULL for failure.
9884 * Advances "pp" to just after the function name (if no error).
9885 */
9886 static char_u *
9887trans_function_name(pp, skip, internal)
9888 char_u **pp;
9889 int skip; /* only find the end, don't evaluate */
9890 int internal; /* TRUE if internal function name OK */
9891{
9892 char_u *name;
9893 char_u *start;
9894 char_u *end;
9895 int lead;
9896 char_u sid_buf[20];
9897 char_u *temp_string = NULL;
9898 char_u *expr_start, *expr_end;
9899 int len;
9900
9901 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
9902 start = *pp;
9903 lead = eval_fname_script(start);
9904 if (lead > 0)
9905 start += lead;
9906 end = find_name_end(start, &expr_start, &expr_end);
9907 if (end == start)
9908 {
9909 if (!skip)
9910 EMSG(_("E129: Function name required"));
9911 return NULL;
9912 }
9913#ifdef FEAT_MAGIC_BRACES
9914 if (expr_start != NULL && !skip)
9915 {
9916 /* expand magic curlies */
9917 temp_string = make_expanded_name(start, expr_start, expr_end, end);
9918 if (temp_string == NULL)
9919 {
9920 /*
9921 * Report an invalid expression in braces, unless the expression
9922 * evaluation has been cancelled due to an aborting error, an
9923 * interrupt, or an exception.
9924 */
9925 if (!aborting())
9926 EMSG2(_(e_invarg2), start);
9927 else
9928 *pp = end;
9929 return NULL;
9930 }
9931 start = temp_string;
9932 len = (int)STRLEN(temp_string);
9933 }
9934 else
9935#endif
9936 len = (int)(end - start);
9937
9938 /*
9939 * Copy the function name to allocated memory.
9940 * Accept <SID>name() inside a script, translate into <SNR>123_name().
9941 * Accept <SNR>123_name() outside a script.
9942 */
9943 if (skip)
9944 lead = 0; /* do nothing */
9945 else if (lead > 0)
9946 {
9947 lead = 3;
9948 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9949 {
9950 if (current_SID <= 0)
9951 {
9952 EMSG(_(e_usingsid));
9953 return NULL;
9954 }
9955 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
9956 lead += (int)STRLEN(sid_buf);
9957 }
9958 }
9959 else if (!internal && !ASCII_ISUPPER(*start))
9960 {
9961 EMSG2(_("E128: Function name must start with a capital: %s"), start);
9962 return NULL;
9963 }
9964 name = alloc((unsigned)(len + lead + 1));
9965 if (name != NULL)
9966 {
9967 if (lead > 0)
9968 {
9969 name[0] = K_SPECIAL;
9970 name[1] = KS_EXTRA;
9971 name[2] = (int)KE_SNR;
9972 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9973 STRCPY(name + 3, sid_buf);
9974 }
9975 mch_memmove(name + lead, start, (size_t)len);
9976 name[len + lead] = NUL;
9977 }
9978 *pp = end;
9979
9980 vim_free(temp_string);
9981 return name;
9982}
9983
9984/*
9985 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
9986 * Return 2 if "p" starts with "s:".
9987 * Return 0 otherwise.
9988 */
9989 static int
9990eval_fname_script(p)
9991 char_u *p;
9992{
9993 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
9994 || STRNICMP(p + 1, "SNR>", 4) == 0))
9995 return 5;
9996 if (p[0] == 's' && p[1] == ':')
9997 return 2;
9998 return 0;
9999}
10000
10001/*
10002 * Return TRUE if "p" starts with "<SID>" or "s:".
10003 * Only works if eval_fname_script() returned non-zero for "p"!
10004 */
10005 static int
10006eval_fname_sid(p)
10007 char_u *p;
10008{
10009 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
10010}
10011
10012/*
10013 * List the head of the function: "name(arg1, arg2)".
10014 */
10015 static void
10016list_func_head(fp, indent)
10017 ufunc_T *fp;
10018 int indent;
10019{
10020 int j;
10021
10022 msg_start();
10023 if (indent)
10024 MSG_PUTS(" ");
10025 MSG_PUTS("function ");
10026 if (fp->name[0] == K_SPECIAL)
10027 {
10028 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
10029 msg_puts(fp->name + 3);
10030 }
10031 else
10032 msg_puts(fp->name);
10033 msg_putchar('(');
10034 for (j = 0; j < fp->args.ga_len; ++j)
10035 {
10036 if (j)
10037 MSG_PUTS(", ");
10038 msg_puts(FUNCARG(fp, j));
10039 }
10040 if (fp->varargs)
10041 {
10042 if (j)
10043 MSG_PUTS(", ");
10044 MSG_PUTS("...");
10045 }
10046 msg_putchar(')');
10047}
10048
10049/*
10050 * Find a function by name, return pointer to it in ufuncs.
10051 * Return NULL for unknown function.
10052 */
10053 static ufunc_T *
10054find_func(name)
10055 char_u *name;
10056{
10057 ufunc_T *fp;
10058
10059 for (fp = firstfunc; fp != NULL; fp = fp->next)
10060 if (STRCMP(name, fp->name) == 0)
10061 break;
10062 return fp;
10063}
10064
10065#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10066
10067/*
10068 * Function given to ExpandGeneric() to obtain the list of user defined
10069 * function names.
10070 */
10071 char_u *
10072get_user_func_name(xp, idx)
10073 expand_T *xp;
10074 int idx;
10075{
10076 static ufunc_T *fp = NULL;
10077
10078 if (idx == 0)
10079 fp = firstfunc;
10080 if (fp != NULL)
10081 {
10082 if (STRLEN(fp->name) + 4 >= IOSIZE)
10083 return fp->name; /* prevents overflow */
10084
10085 cat_func_name(IObuff, fp);
10086 if (xp->xp_context != EXPAND_USER_FUNC)
10087 {
10088 STRCAT(IObuff, "(");
10089 if (!fp->varargs && fp->args.ga_len == 0)
10090 STRCAT(IObuff, ")");
10091 }
10092
10093 fp = fp->next;
10094 return IObuff;
10095 }
10096 return NULL;
10097}
10098
10099#endif /* FEAT_CMDL_COMPL */
10100
10101/*
10102 * Copy the function name of "fp" to buffer "buf".
10103 * "buf" must be able to hold the function name plus three bytes.
10104 * Takes care of script-local function names.
10105 */
10106 static void
10107cat_func_name(buf, fp)
10108 char_u *buf;
10109 ufunc_T *fp;
10110{
10111 if (fp->name[0] == K_SPECIAL)
10112 {
10113 STRCPY(buf, "<SNR>");
10114 STRCAT(buf, fp->name + 3);
10115 }
10116 else
10117 STRCPY(buf, fp->name);
10118}
10119
10120/*
10121 * ":delfunction {name}"
10122 */
10123 void
10124ex_delfunction(eap)
10125 exarg_T *eap;
10126{
10127 ufunc_T *fp = NULL, *pfp;
10128 char_u *p;
10129 char_u *name;
10130
10131 p = eap->arg;
10132 name = trans_function_name(&p, eap->skip, FALSE);
10133 if (name == NULL)
10134 return;
10135 if (!ends_excmd(*skipwhite(p)))
10136 {
10137 vim_free(name);
10138 EMSG(_(e_trailing));
10139 return;
10140 }
10141 eap->nextcmd = check_nextcmd(p);
10142 if (eap->nextcmd != NULL)
10143 *p = NUL;
10144
10145 if (!eap->skip)
10146 fp = find_func(name);
10147 vim_free(name);
10148
10149 if (!eap->skip)
10150 {
10151 if (fp == NULL)
10152 {
10153 EMSG2(_("E130: Undefined function: %s"), eap->arg);
10154 return;
10155 }
10156 if (fp->calls)
10157 {
10158 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
10159 return;
10160 }
10161
10162 /* clear this function */
10163 vim_free(fp->name);
10164 ga_clear_strings(&(fp->args));
10165 ga_clear_strings(&(fp->lines));
10166
10167 /* remove the function from the function list */
10168 if (firstfunc == fp)
10169 firstfunc = fp->next;
10170 else
10171 {
10172 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
10173 if (pfp->next == fp)
10174 {
10175 pfp->next = fp->next;
10176 break;
10177 }
10178 }
10179 vim_free(fp);
10180 }
10181}
10182
10183/*
10184 * Call a user function.
10185 */
10186 static void
10187call_user_func(fp, argcount, argvars, retvar, firstline, lastline)
10188 ufunc_T *fp; /* pointer to function */
10189 int argcount; /* nr of args */
10190 VAR argvars; /* arguments */
10191 VAR retvar; /* return value */
10192 linenr_T firstline; /* first line of range */
10193 linenr_T lastline; /* last line of range */
10194{
10195 char_u *save_sourcing_name;
10196 linenr_T save_sourcing_lnum;
10197 scid_T save_current_SID;
10198 struct funccall fc;
10199 struct funccall *save_fcp = current_funccal;
10200 int save_did_emsg;
10201 static int depth = 0;
10202
10203 /* If depth of calling is getting too high, don't execute the function */
10204 if (depth >= p_mfd)
10205 {
10206 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
10207 retvar->var_type = VAR_NUMBER;
10208 retvar->var_val.var_number = -1;
10209 return;
10210 }
10211 ++depth;
10212
10213 line_breakcheck(); /* check for CTRL-C hit */
10214
10215 /* set local variables */
10216 var_init(&fc.l_vars);
10217 fc.func = fp;
10218 fc.argcount = argcount;
10219 fc.argvars = argvars;
10220 fc.retvar = retvar;
10221 retvar->var_val.var_number = 0;
10222 fc.linenr = 0;
10223 fc.returned = FALSE;
10224 fc.level = ex_nesting_level;
10225 fc.a0_var.var_type = VAR_NUMBER;
10226 fc.a0_var.var_val.var_number = argcount - fp->args.ga_len;
10227 fc.a0_var.var_name = NULL;
10228 current_funccal = &fc;
10229 fc.firstline.var_type = VAR_NUMBER;
10230 fc.firstline.var_val.var_number = firstline;
10231 fc.firstline.var_name = NULL;
10232 fc.lastline.var_type = VAR_NUMBER;
10233 fc.lastline.var_val.var_number = lastline;
10234 fc.lastline.var_name = NULL;
10235 /* Check if this function has a breakpoint. */
10236 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
10237 fc.dbg_tick = debug_tick;
10238
10239 /* Don't redraw while executing the function. */
10240 ++RedrawingDisabled;
10241 save_sourcing_name = sourcing_name;
10242 save_sourcing_lnum = sourcing_lnum;
10243 sourcing_lnum = 1;
10244 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
10245 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
10246 if (sourcing_name != NULL)
10247 {
10248 if (save_sourcing_name != NULL
10249 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
10250 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
10251 else
10252 STRCPY(sourcing_name, "function ");
10253 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
10254
10255 if (p_verbose >= 12)
10256 {
10257 ++no_wait_return;
10258 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10259 msg_str((char_u *)_("calling %s"), sourcing_name);
10260 if (p_verbose >= 14)
10261 {
10262 int i;
10263 char_u buf[MSG_BUF_LEN];
10264
10265 msg_puts((char_u *)"(");
10266 for (i = 0; i < argcount; ++i)
10267 {
10268 if (i > 0)
10269 msg_puts((char_u *)", ");
10270 if (argvars[i].var_type == VAR_NUMBER)
10271 msg_outnum((long)argvars[i].var_val.var_number);
10272 else
10273 {
10274 trunc_string(get_var_string(&argvars[i]),
10275 buf, MSG_BUF_LEN);
10276 msg_puts((char_u *)"\"");
10277 msg_puts(buf);
10278 msg_puts((char_u *)"\"");
10279 }
10280 }
10281 msg_puts((char_u *)")");
10282 }
10283 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10284 cmdline_row = msg_row;
10285 --no_wait_return;
10286 }
10287 }
10288 save_current_SID = current_SID;
10289 current_SID = fp->script_ID;
10290 save_did_emsg = did_emsg;
10291 did_emsg = FALSE;
10292
10293 /* call do_cmdline() to execute the lines */
10294 do_cmdline(NULL, get_func_line, (void *)&fc,
10295 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
10296
10297 --RedrawingDisabled;
10298
10299 /* when the function was aborted because of an error, return -1 */
10300 if ((did_emsg && (fp->flags & FC_ABORT)) || retvar->var_type == VAR_UNKNOWN)
10301 {
10302 clear_var(retvar);
10303 retvar->var_type = VAR_NUMBER;
10304 retvar->var_val.var_number = -1;
10305 }
10306
10307 /* when being verbose, mention the return value */
10308 if (p_verbose >= 12)
10309 {
10310 char_u *sn, *val;
10311
10312 ++no_wait_return;
10313 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10314
10315 /* Make sure the output fits in IObuff. */
10316 sn = sourcing_name;
10317 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
10318 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
10319
10320 if (aborting())
10321 smsg((char_u *)_("%s aborted"), sn);
10322 else if (fc.retvar->var_type == VAR_NUMBER)
10323 smsg((char_u *)_("%s returning #%ld"), sn,
10324 (long)fc.retvar->var_val.var_number);
10325 else if (fc.retvar->var_type == VAR_STRING)
10326 {
10327 val = get_var_string(fc.retvar);
10328 if (STRLEN(val) > IOSIZE / 2 - 50)
10329 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
10330 smsg((char_u *)_("%s returning \"%s\""), sn, val);
10331 }
10332 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10333 cmdline_row = msg_row;
10334 --no_wait_return;
10335 }
10336
10337 vim_free(sourcing_name);
10338 sourcing_name = save_sourcing_name;
10339 sourcing_lnum = save_sourcing_lnum;
10340 current_SID = save_current_SID;
10341
10342 if (p_verbose >= 12 && sourcing_name != NULL)
10343 {
10344 ++no_wait_return;
10345 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10346 msg_str((char_u *)_("continuing in %s"), sourcing_name);
10347 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10348 cmdline_row = msg_row;
10349 --no_wait_return;
10350 }
10351
10352 did_emsg |= save_did_emsg;
10353 current_funccal = save_fcp;
10354
10355 var_clear(&fc.l_vars); /* free all local variables */
10356 --depth;
10357}
10358
10359/*
10360 * ":return [expr]"
10361 */
10362 void
10363ex_return(eap)
10364 exarg_T *eap;
10365{
10366 char_u *arg = eap->arg;
10367 var retvar;
10368 int returning = FALSE;
10369
10370 if (current_funccal == NULL)
10371 {
10372 EMSG(_("E133: :return not inside a function"));
10373 return;
10374 }
10375
10376 if (eap->skip)
10377 ++emsg_skip;
10378
10379 eap->nextcmd = NULL;
10380 if ((*arg != NUL && *arg != '|' && *arg != '\n')
10381 && eval0(arg, &retvar, &eap->nextcmd, !eap->skip) != FAIL)
10382 {
10383 if (!eap->skip)
10384 returning = do_return(eap, FALSE, TRUE, &retvar);
10385 else
10386 clear_var(&retvar);
10387 }
10388 /* It's safer to return also on error. */
10389 else if (!eap->skip)
10390 {
10391 /*
10392 * Return unless the expression evaluation has been cancelled due to an
10393 * aborting error, an interrupt, or an exception.
10394 */
10395 if (!aborting())
10396 returning = do_return(eap, FALSE, TRUE, NULL);
10397 }
10398
10399 /* When skipping or the return gets pending, advance to the next command
10400 * in this line (!returning). Otherwise, ignore the rest of the line.
10401 * Following lines will be ignored by get_func_line(). */
10402 if (returning)
10403 eap->nextcmd = NULL;
10404 else if (eap->nextcmd == NULL) /* no argument */
10405 eap->nextcmd = check_nextcmd(arg);
10406
10407 if (eap->skip)
10408 --emsg_skip;
10409}
10410
10411/*
10412 * Return from a function. Possibly makes the return pending. Also called
10413 * for a pending return at the ":endtry" or after returning from an extra
10414 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
10415 * when called due to a ":return" command. "value" may point to a variable
10416 * with the return value. Returns TRUE when the return can be carried out,
10417 * FALSE when the return gets pending.
10418 */
10419 int
10420do_return(eap, reanimate, is_cmd, value)
10421 exarg_T *eap;
10422 int reanimate;
10423 int is_cmd;
10424 void *value;
10425{
10426 int idx;
10427 struct condstack *cstack = eap->cstack;
10428
10429 if (reanimate)
10430 /* Undo the return. */
10431 current_funccal->returned = FALSE;
10432
10433 /*
10434 * Cleanup (and inactivate) conditionals, but stop when a try conditional
10435 * not in its finally clause (which then is to be executed next) is found.
10436 * In this case, make the ":return" pending for execution at the ":endtry".
10437 * Otherwise, return normally.
10438 */
10439 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
10440 if (idx >= 0)
10441 {
10442 cstack->cs_pending[idx] = CSTP_RETURN;
10443
10444 if (!is_cmd && !reanimate)
10445 /* A pending return again gets pending. "value" points to an
10446 * allocated variable with the value of the original ":return"'s
10447 * argument if present or is NULL else. */
10448 cstack->cs_retvar[idx] = value;
10449 else
10450 {
10451 /* When undoing a return in order to make it pending, get the stored
10452 * return value. */
10453 if (reanimate)
10454 value = current_funccal->retvar;
10455
10456 if (value != NULL)
10457 {
10458 /* Store the value of the pending return. */
10459 if ((cstack->cs_retvar[idx] = alloc_var()) != NULL)
10460 *(VAR)cstack->cs_retvar[idx] = *(VAR)value;
10461 else
10462 EMSG(_(e_outofmem));
10463 }
10464 else
10465 cstack->cs_retvar[idx] = NULL;
10466
10467 if (reanimate)
10468 {
10469 /* The pending return value could be overwritten by a ":return"
10470 * without argument in a finally clause; reset the default
10471 * return value. */
10472 current_funccal->retvar->var_type = VAR_NUMBER;
10473 current_funccal->retvar->var_val.var_number = 0;
10474 }
10475 }
10476 report_make_pending(CSTP_RETURN, value);
10477 }
10478 else
10479 {
10480 current_funccal->returned = TRUE;
10481
10482 /* If the return is carried out now, store the return value. For
10483 * a return immediately after reanimation, the value is already
10484 * there. */
10485 if (!reanimate && value != NULL)
10486 {
10487 clear_var(current_funccal->retvar);
10488 *current_funccal->retvar = *(VAR)value;
10489 if (!is_cmd)
10490 vim_free(value);
10491 }
10492 }
10493
10494 return idx < 0;
10495}
10496
10497/*
10498 * Free the variable with a pending return value.
10499 */
10500 void
10501discard_pending_return(retvar)
10502 void *retvar;
10503{
10504 /* The variable was copied from one with an undefined var_name. So we can't
10505 * use free_var() to clear and free it. */
10506 clear_var((VAR)retvar);
10507 vim_free(retvar);
10508}
10509
10510/*
10511 * Generate a return command for producing the value of "retvar". The result
10512 * is an allocated string. Used by report_pending() for verbose messages.
10513 */
10514 char_u *
10515get_return_cmd(retvar)
10516 void *retvar;
10517{
10518 char_u *s = IObuff;
10519
10520 if (retvar == NULL || ((VAR)retvar)->var_type == VAR_UNKNOWN)
10521 s = (char_u *)":return";
10522 else if (((VAR)retvar)->var_type == VAR_STRING)
10523 sprintf((char *)IObuff, ":return \"%s\"",
10524 ((VAR)retvar)->var_val.var_string);
10525 else
10526 sprintf((char *)IObuff, ":return %ld",
10527 (long)(((VAR)retvar)->var_val.var_number));
10528 return vim_strsave(s);
10529}
10530
10531/*
10532 * Get next function line.
10533 * Called by do_cmdline() to get the next line.
10534 * Returns allocated string, or NULL for end of function.
10535 */
10536/* ARGSUSED */
10537 char_u *
10538get_func_line(c, cookie, indent)
10539 int c; /* not used */
10540 void *cookie;
10541 int indent; /* not used */
10542{
10543 struct funccall *fcp = (struct funccall *)cookie;
10544 char_u *retval;
10545 garray_T *gap; /* growarray with function lines */
10546
10547 /* If breakpoints have been added/deleted need to check for it. */
10548 if (fcp->dbg_tick != debug_tick)
10549 {
10550 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10551 sourcing_lnum);
10552 fcp->dbg_tick = debug_tick;
10553 }
10554
10555 gap = &fcp->func->lines;
10556 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10557 retval = NULL;
10558 else if (fcp->returned || fcp->linenr >= gap->ga_len)
10559 retval = NULL;
10560 else
10561 {
10562 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
10563 sourcing_lnum = fcp->linenr;
10564 }
10565
10566 /* Did we encounter a breakpoint? */
10567 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
10568 {
10569 dbg_breakpoint(fcp->func->name, sourcing_lnum);
10570 /* Find next breakpoint. */
10571 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10572 sourcing_lnum);
10573 fcp->dbg_tick = debug_tick;
10574 }
10575
10576 return retval;
10577}
10578
10579/*
10580 * Return TRUE if the currently active function should be ended, because a
10581 * return was encountered or an error occured. Used inside a ":while".
10582 */
10583 int
10584func_has_ended(cookie)
10585 void *cookie;
10586{
10587 struct funccall *fcp = (struct funccall *)cookie;
10588
10589 /* Ignore the "abort" flag if the abortion behavior has been changed due to
10590 * an error inside a try conditional. */
10591 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10592 || fcp->returned);
10593}
10594
10595/*
10596 * return TRUE if cookie indicates a function which "abort"s on errors.
10597 */
10598 int
10599func_has_abort(cookie)
10600 void *cookie;
10601{
10602 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
10603}
10604
10605#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
10606typedef enum
10607{
10608 VAR_FLAVOUR_DEFAULT,
10609 VAR_FLAVOUR_SESSION,
10610 VAR_FLAVOUR_VIMINFO
10611} var_flavour_T;
10612
10613static var_flavour_T var_flavour __ARGS((char_u *varname));
10614
10615 static var_flavour_T
10616var_flavour(varname)
10617 char_u *varname;
10618{
10619 char_u *p = varname;
10620
10621 if (ASCII_ISUPPER(*p))
10622 {
10623 while (*(++p))
10624 if (ASCII_ISLOWER(*p))
10625 return VAR_FLAVOUR_SESSION;
10626 return VAR_FLAVOUR_VIMINFO;
10627 }
10628 else
10629 return VAR_FLAVOUR_DEFAULT;
10630}
10631#endif
10632
10633#if defined(FEAT_VIMINFO) || defined(PROTO)
10634/*
10635 * Restore global vars that start with a capital from the viminfo file
10636 */
10637 int
10638read_viminfo_varlist(virp, writing)
10639 vir_T *virp;
10640 int writing;
10641{
10642 char_u *tab;
10643 int is_string = FALSE;
10644 VAR varp = NULL;
10645 char_u *val;
10646
10647 if (!writing && (find_viminfo_parameter('!') != NULL))
10648 {
10649 tab = vim_strchr(virp->vir_line + 1, '\t');
10650 if (tab != NULL)
10651 {
10652 *tab++ = '\0'; /* isolate the variable name */
10653 if (*tab == 'S') /* string var */
10654 is_string = TRUE;
10655
10656 tab = vim_strchr(tab, '\t');
10657 if (tab != NULL)
10658 {
10659 /* create a nameless variable to hold the value */
10660 if (is_string)
10661 {
10662 val = viminfo_readstring(virp,
10663 (int)(tab - virp->vir_line + 1), TRUE);
10664 if (val != NULL)
10665 varp = alloc_string_var(val);
10666 }
10667 else
10668 {
10669 varp = alloc_var();
10670 if (varp != NULL)
10671 {
10672 varp->var_type = VAR_NUMBER;
10673 varp->var_val.var_number = atol((char *)tab + 1);
10674 }
10675 }
10676 /* assign the value to the variable */
10677 if (varp != NULL)
10678 {
10679 set_var(virp->vir_line + 1, varp);
10680 free_var(varp);
10681 }
10682 }
10683 }
10684 }
10685
10686 return viminfo_readline(virp);
10687}
10688
10689/*
10690 * Write global vars that start with a capital to the viminfo file
10691 */
10692 void
10693write_viminfo_varlist(fp)
10694 FILE *fp;
10695{
10696 garray_T *gap = &variables; /* global variable */
10697 VAR this_var;
10698 int i;
10699
10700 if (find_viminfo_parameter('!') == NULL)
10701 return;
10702
10703 fprintf(fp, _("\n# global variables:\n"));
10704 for (i = gap->ga_len; --i >= 0; )
10705 {
10706 this_var = &VAR_GAP_ENTRY(i, gap);
10707 if (this_var->var_name != NULL
10708 && var_flavour(this_var->var_name) == VAR_FLAVOUR_VIMINFO)
10709 {
10710 fprintf(fp, "!%s\t%s\t", this_var->var_name,
10711 (this_var->var_type == VAR_STRING) ? "STR" : "NUM");
10712 viminfo_writestring(fp, get_var_string(this_var));
10713 }
10714 }
10715}
10716#endif
10717
10718#if defined(FEAT_SESSION) || defined(PROTO)
10719 int
10720store_session_globals(fd)
10721 FILE *fd;
10722{
10723 garray_T *gap = &variables; /* global variable */
10724 VAR this_var;
10725 int i;
10726 char_u *p, *t;
10727
10728 for (i = gap->ga_len; --i >= 0; )
10729 {
10730 this_var = &VAR_GAP_ENTRY(i, gap);
10731 if (this_var->var_name != NULL)
10732 {
10733 if (var_flavour(this_var->var_name) == VAR_FLAVOUR_SESSION)
10734 {
10735 /* Escapse special characters with a backslash. Turn a LF and
10736 * CR into \n and \r. */
10737 p = vim_strsave_escaped(get_var_string(this_var),
10738 (char_u *)"\\\"\n\r");
10739 if (p == NULL) /* out of memory */
10740 continue;
10741 for (t = p; *t != NUL; ++t)
10742 if (*t == '\n')
10743 *t = 'n';
10744 else if (*t == '\r')
10745 *t = 'r';
10746 if ((fprintf(fd, "let %s = %c%s%c",
10747 this_var->var_name,
10748 (this_var->var_type == VAR_STRING) ? '"' : ' ',
10749 p,
10750 (this_var->var_type == VAR_STRING) ? '"' : ' ') < 0)
10751 || put_eol(fd) == FAIL)
10752 {
10753 vim_free(p);
10754 return FAIL;
10755 }
10756 vim_free(p);
10757 }
10758
10759 }
10760 }
10761 return OK;
10762}
10763#endif
10764
10765#endif /* FEAT_EVAL */
10766
10767#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
10768
10769
10770#ifdef WIN3264
10771/*
10772 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10773 */
10774static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10775static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
10776static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10777
10778/*
10779 * Get the short pathname of a file.
10780 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
10781 */
10782 static int
10783get_short_pathname(fnamep, bufp, fnamelen)
10784 char_u **fnamep;
10785 char_u **bufp;
10786 int *fnamelen;
10787{
10788 int l,len;
10789 char_u *newbuf;
10790
10791 len = *fnamelen;
10792
10793 l = GetShortPathName(*fnamep, *fnamep, len);
10794 if (l > len - 1)
10795 {
10796 /* If that doesn't work (not enough space), then save the string
10797 * and try again with a new buffer big enough
10798 */
10799 newbuf = vim_strnsave(*fnamep, l);
10800 if (newbuf == NULL)
10801 return 0;
10802
10803 vim_free(*bufp);
10804 *fnamep = *bufp = newbuf;
10805
10806 l = GetShortPathName(*fnamep,*fnamep,l+1);
10807
10808 /* Really should always succeed, as the buffer is big enough */
10809 }
10810
10811 *fnamelen = l;
10812 return 1;
10813}
10814
10815/*
10816 * Create a short path name. Returns the length of the buffer it needs.
10817 * Doesn't copy over the end of the buffer passed in.
10818 */
10819 static int
10820shortpath_for_invalid_fname(fname, bufp, fnamelen)
10821 char_u **fname;
10822 char_u **bufp;
10823 int *fnamelen;
10824{
10825 char_u *s, *p, *pbuf2, *pbuf3;
10826 char_u ch;
10827 int l,len,len2,plen,slen;
10828
10829 /* Make a copy */
10830 len2 = *fnamelen;
10831 pbuf2 = vim_strnsave(*fname, len2);
10832 pbuf3 = NULL;
10833
10834 s = pbuf2 + len2 - 1; /* Find the end */
10835 slen = 1;
10836 plen = len2;
10837
10838 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010839 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
10841 --s;
10842 ++slen;
10843 --plen;
10844 }
10845
10846 do
10847 {
10848 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010849 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 {
10851 --s;
10852 ++slen;
10853 --plen;
10854 }
10855 if (s <= pbuf2)
10856 break;
10857
10858 /* Remeber the character that is about to be blatted */
10859 ch = *s;
10860 *s = 0; /* get_short_pathname requires a null-terminated string */
10861
10862 /* Try it in situ */
10863 p = pbuf2;
10864 if (!get_short_pathname(&p, &pbuf3, &plen))
10865 {
10866 vim_free(pbuf2);
10867 return -1;
10868 }
10869 *s = ch; /* Preserve the string */
10870 } while (plen == 0);
10871
10872 if (plen > 0)
10873 {
10874 /* Remeber the length of the new string. */
10875 *fnamelen = len = plen + slen;
10876 vim_free(*bufp);
10877 if (len > len2)
10878 {
10879 /* If there's not enough space in the currently allocated string,
10880 * then copy it to a buffer big enough.
10881 */
10882 *fname= *bufp = vim_strnsave(p, len);
10883 if (*fname == NULL)
10884 return -1;
10885 }
10886 else
10887 {
10888 /* Transfer pbuf2 to being the main buffer (it's big enough) */
10889 *fname = *bufp = pbuf2;
10890 if (p != pbuf2)
10891 strncpy(*fname, p, plen);
10892 pbuf2 = NULL;
10893 }
10894 /* Concat the next bit */
10895 strncpy(*fname + plen, s, slen);
10896 (*fname)[len] = '\0';
10897 }
10898 vim_free(pbuf3);
10899 vim_free(pbuf2);
10900 return 0;
10901}
10902
10903/*
10904 * Get a pathname for a partial path.
10905 */
10906 static int
10907shortpath_for_partial(fnamep, bufp, fnamelen)
10908 char_u **fnamep;
10909 char_u **bufp;
10910 int *fnamelen;
10911{
10912 int sepcount, len, tflen;
10913 char_u *p;
10914 char_u *pbuf, *tfname;
10915 int hasTilde;
10916
10917 /* Count up the path seperators from the RHS.. so we know which part
10918 * of the path to return.
10919 */
10920 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010921 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 if (vim_ispathsep(*p))
10923 ++sepcount;
10924
10925 /* Need full path first (use expand_env() to remove a "~/") */
10926 hasTilde = (**fnamep == '~');
10927 if (hasTilde)
10928 pbuf = tfname = expand_env_save(*fnamep);
10929 else
10930 pbuf = tfname = FullName_save(*fnamep, FALSE);
10931
10932 len = tflen = STRLEN(tfname);
10933
10934 if (!get_short_pathname(&tfname, &pbuf, &len))
10935 return -1;
10936
10937 if (len == 0)
10938 {
10939 /* Don't have a valid filename, so shorten the rest of the
10940 * path if we can. This CAN give us invalid 8.3 filenames, but
10941 * there's not a lot of point in guessing what it might be.
10942 */
10943 len = tflen;
10944 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
10945 return -1;
10946 }
10947
10948 /* Count the paths backward to find the beginning of the desired string. */
10949 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010950 {
10951#ifdef FEAT_MBYTE
10952 if (has_mbyte)
10953 p -= mb_head_off(tfname, p);
10954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 if (vim_ispathsep(*p))
10956 {
10957 if (sepcount == 0 || (hasTilde && sepcount == 1))
10958 break;
10959 else
10960 sepcount --;
10961 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 if (hasTilde)
10964 {
10965 --p;
10966 if (p >= tfname)
10967 *p = '~';
10968 else
10969 return -1;
10970 }
10971 else
10972 ++p;
10973
10974 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10975 vim_free(*bufp);
10976 *fnamelen = (int)STRLEN(p);
10977 *bufp = pbuf;
10978 *fnamep = p;
10979
10980 return 0;
10981}
10982#endif /* WIN3264 */
10983
10984/*
10985 * Adjust a filename, according to a string of modifiers.
10986 * *fnamep must be NUL terminated when called. When returning, the length is
10987 * determined by *fnamelen.
10988 * Returns valid flags.
10989 * When there is an error, *fnamep is set to NULL.
10990 */
10991 int
10992modify_fname(src, usedlen, fnamep, bufp, fnamelen)
10993 char_u *src; /* string with modifiers */
10994 int *usedlen; /* characters after src that are used */
10995 char_u **fnamep; /* file name so far */
10996 char_u **bufp; /* buffer for allocated file name or NULL */
10997 int *fnamelen; /* length of fnamep */
10998{
10999 int valid = 0;
11000 char_u *tail;
11001 char_u *s, *p, *pbuf;
11002 char_u dirname[MAXPATHL];
11003 int c;
11004 int has_fullname = 0;
11005#ifdef WIN3264
11006 int has_shortname = 0;
11007#endif
11008
11009repeat:
11010 /* ":p" - full path/file_name */
11011 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
11012 {
11013 has_fullname = 1;
11014
11015 valid |= VALID_PATH;
11016 *usedlen += 2;
11017
11018 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
11019 if ((*fnamep)[0] == '~'
11020#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
11021 && ((*fnamep)[1] == '/'
11022# ifdef BACKSLASH_IN_FILENAME
11023 || (*fnamep)[1] == '\\'
11024# endif
11025 || (*fnamep)[1] == NUL)
11026
11027#endif
11028 )
11029 {
11030 *fnamep = expand_env_save(*fnamep);
11031 vim_free(*bufp); /* free any allocated file name */
11032 *bufp = *fnamep;
11033 if (*fnamep == NULL)
11034 return -1;
11035 }
11036
11037 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011038 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 {
11040 if (vim_ispathsep(*p)
11041 && p[1] == '.'
11042 && (p[2] == NUL
11043 || vim_ispathsep(p[2])
11044 || (p[2] == '.'
11045 && (p[3] == NUL || vim_ispathsep(p[3])))))
11046 break;
11047 }
11048
11049 /* FullName_save() is slow, don't use it when not needed. */
11050 if (*p != NUL || !vim_isAbsName(*fnamep))
11051 {
11052 *fnamep = FullName_save(*fnamep, *p != NUL);
11053 vim_free(*bufp); /* free any allocated file name */
11054 *bufp = *fnamep;
11055 if (*fnamep == NULL)
11056 return -1;
11057 }
11058
11059 /* Append a path separator to a directory. */
11060 if (mch_isdir(*fnamep))
11061 {
11062 /* Make room for one or two extra characters. */
11063 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
11064 vim_free(*bufp); /* free any allocated file name */
11065 *bufp = *fnamep;
11066 if (*fnamep == NULL)
11067 return -1;
11068 add_pathsep(*fnamep);
11069 }
11070 }
11071
11072 /* ":." - path relative to the current directory */
11073 /* ":~" - path relative to the home directory */
11074 /* ":8" - shortname path - postponed till after */
11075 while (src[*usedlen] == ':'
11076 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
11077 {
11078 *usedlen += 2;
11079 if (c == '8')
11080 {
11081#ifdef WIN3264
11082 has_shortname = 1; /* Postpone this. */
11083#endif
11084 continue;
11085 }
11086 pbuf = NULL;
11087 /* Need full path first (use expand_env() to remove a "~/") */
11088 if (!has_fullname)
11089 {
11090 if (c == '.' && **fnamep == '~')
11091 p = pbuf = expand_env_save(*fnamep);
11092 else
11093 p = pbuf = FullName_save(*fnamep, FALSE);
11094 }
11095 else
11096 p = *fnamep;
11097
11098 has_fullname = 0;
11099
11100 if (p != NULL)
11101 {
11102 if (c == '.')
11103 {
11104 mch_dirname(dirname, MAXPATHL);
11105 s = shorten_fname(p, dirname);
11106 if (s != NULL)
11107 {
11108 *fnamep = s;
11109 if (pbuf != NULL)
11110 {
11111 vim_free(*bufp); /* free any allocated file name */
11112 *bufp = pbuf;
11113 pbuf = NULL;
11114 }
11115 }
11116 }
11117 else
11118 {
11119 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
11120 /* Only replace it when it starts with '~' */
11121 if (*dirname == '~')
11122 {
11123 s = vim_strsave(dirname);
11124 if (s != NULL)
11125 {
11126 *fnamep = s;
11127 vim_free(*bufp);
11128 *bufp = s;
11129 }
11130 }
11131 }
11132 vim_free(pbuf);
11133 }
11134 }
11135
11136 tail = gettail(*fnamep);
11137 *fnamelen = (int)STRLEN(*fnamep);
11138
11139 /* ":h" - head, remove "/file_name", can be repeated */
11140 /* Don't remove the first "/" or "c:\" */
11141 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
11142 {
11143 valid |= VALID_HEAD;
11144 *usedlen += 2;
11145 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011146 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 --tail;
11148 *fnamelen = (int)(tail - *fnamep);
11149#ifdef VMS
11150 if (*fnamelen > 0)
11151 *fnamelen += 1; /* the path separator is part of the path */
11152#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011153 while (tail > s && !after_pathsep(s, tail))
11154 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 }
11156
11157 /* ":8" - shortname */
11158 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
11159 {
11160 *usedlen += 2;
11161#ifdef WIN3264
11162 has_shortname = 1;
11163#endif
11164 }
11165
11166#ifdef WIN3264
11167 /* Check shortname after we have done 'heads' and before we do 'tails'
11168 */
11169 if (has_shortname)
11170 {
11171 pbuf = NULL;
11172 /* Copy the string if it is shortened by :h */
11173 if (*fnamelen < (int)STRLEN(*fnamep))
11174 {
11175 p = vim_strnsave(*fnamep, *fnamelen);
11176 if (p == 0)
11177 return -1;
11178 vim_free(*bufp);
11179 *bufp = *fnamep = p;
11180 }
11181
11182 /* Split into two implementations - makes it easier. First is where
11183 * there isn't a full name already, second is where there is.
11184 */
11185 if (!has_fullname && !vim_isAbsName(*fnamep))
11186 {
11187 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
11188 return -1;
11189 }
11190 else
11191 {
11192 int l;
11193
11194 /* Simple case, already have the full-name
11195 * Nearly always shorter, so try first time. */
11196 l = *fnamelen;
11197 if (!get_short_pathname(fnamep, bufp, &l))
11198 return -1;
11199
11200 if (l == 0)
11201 {
11202 /* Couldn't find the filename.. search the paths.
11203 */
11204 l = *fnamelen;
11205 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
11206 return -1;
11207 }
11208 *fnamelen = l;
11209 }
11210 }
11211#endif /* WIN3264 */
11212
11213 /* ":t" - tail, just the basename */
11214 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
11215 {
11216 *usedlen += 2;
11217 *fnamelen -= (int)(tail - *fnamep);
11218 *fnamep = tail;
11219 }
11220
11221 /* ":e" - extension, can be repeated */
11222 /* ":r" - root, without extension, can be repeated */
11223 while (src[*usedlen] == ':'
11224 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
11225 {
11226 /* find a '.' in the tail:
11227 * - for second :e: before the current fname
11228 * - otherwise: The last '.'
11229 */
11230 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
11231 s = *fnamep - 2;
11232 else
11233 s = *fnamep + *fnamelen - 1;
11234 for ( ; s > tail; --s)
11235 if (s[0] == '.')
11236 break;
11237 if (src[*usedlen + 1] == 'e') /* :e */
11238 {
11239 if (s > tail)
11240 {
11241 *fnamelen += (int)(*fnamep - (s + 1));
11242 *fnamep = s + 1;
11243#ifdef VMS
11244 /* cut version from the extension */
11245 s = *fnamep + *fnamelen - 1;
11246 for ( ; s > *fnamep; --s)
11247 if (s[0] == ';')
11248 break;
11249 if (s > *fnamep)
11250 *fnamelen = s - *fnamep;
11251#endif
11252 }
11253 else if (*fnamep <= tail)
11254 *fnamelen = 0;
11255 }
11256 else /* :r */
11257 {
11258 if (s > tail) /* remove one extension */
11259 *fnamelen = (int)(s - *fnamep);
11260 }
11261 *usedlen += 2;
11262 }
11263
11264 /* ":s?pat?foo?" - substitute */
11265 /* ":gs?pat?foo?" - global substitute */
11266 if (src[*usedlen] == ':'
11267 && (src[*usedlen + 1] == 's'
11268 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
11269 {
11270 char_u *str;
11271 char_u *pat;
11272 char_u *sub;
11273 int sep;
11274 char_u *flags;
11275 int didit = FALSE;
11276
11277 flags = (char_u *)"";
11278 s = src + *usedlen + 2;
11279 if (src[*usedlen + 1] == 'g')
11280 {
11281 flags = (char_u *)"g";
11282 ++s;
11283 }
11284
11285 sep = *s++;
11286 if (sep)
11287 {
11288 /* find end of pattern */
11289 p = vim_strchr(s, sep);
11290 if (p != NULL)
11291 {
11292 pat = vim_strnsave(s, (int)(p - s));
11293 if (pat != NULL)
11294 {
11295 s = p + 1;
11296 /* find end of substitution */
11297 p = vim_strchr(s, sep);
11298 if (p != NULL)
11299 {
11300 sub = vim_strnsave(s, (int)(p - s));
11301 str = vim_strnsave(*fnamep, *fnamelen);
11302 if (sub != NULL && str != NULL)
11303 {
11304 *usedlen = (int)(p + 1 - src);
11305 s = do_string_sub(str, pat, sub, flags);
11306 if (s != NULL)
11307 {
11308 *fnamep = s;
11309 *fnamelen = (int)STRLEN(s);
11310 vim_free(*bufp);
11311 *bufp = s;
11312 didit = TRUE;
11313 }
11314 }
11315 vim_free(sub);
11316 vim_free(str);
11317 }
11318 vim_free(pat);
11319 }
11320 }
11321 /* after using ":s", repeat all the modifiers */
11322 if (didit)
11323 goto repeat;
11324 }
11325 }
11326
11327 return valid;
11328}
11329
11330/*
11331 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
11332 * "flags" can be "g" to do a global substitute.
11333 * Returns an allocated string, NULL for error.
11334 */
11335 char_u *
11336do_string_sub(str, pat, sub, flags)
11337 char_u *str;
11338 char_u *pat;
11339 char_u *sub;
11340 char_u *flags;
11341{
11342 int sublen;
11343 regmatch_T regmatch;
11344 int i;
11345 int do_all;
11346 char_u *tail;
11347 garray_T ga;
11348 char_u *ret;
11349 char_u *save_cpo;
11350
11351 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
11352 save_cpo = p_cpo;
11353 p_cpo = (char_u *)"";
11354
11355 ga_init2(&ga, 1, 200);
11356
11357 do_all = (flags[0] == 'g');
11358
11359 regmatch.rm_ic = p_ic;
11360 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11361 if (regmatch.regprog != NULL)
11362 {
11363 tail = str;
11364 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
11365 {
11366 /*
11367 * Get some space for a temporary buffer to do the substitution
11368 * into. It will contain:
11369 * - The text up to where the match is.
11370 * - The substituted text.
11371 * - The text after the match.
11372 */
11373 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
11374 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
11375 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11376 {
11377 ga_clear(&ga);
11378 break;
11379 }
11380
11381 /* copy the text up to where the match is */
11382 i = (int)(regmatch.startp[0] - tail);
11383 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11384 /* add the substituted text */
11385 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
11386 + ga.ga_len + i, TRUE, TRUE, FALSE);
11387 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 /* avoid getting stuck on a match with an empty string */
11389 if (tail == regmatch.endp[0])
11390 {
11391 if (*tail == NUL)
11392 break;
11393 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
11394 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 }
11396 else
11397 {
11398 tail = regmatch.endp[0];
11399 if (*tail == NUL)
11400 break;
11401 }
11402 if (!do_all)
11403 break;
11404 }
11405
11406 if (ga.ga_data != NULL)
11407 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11408
11409 vim_free(regmatch.regprog);
11410 }
11411
11412 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11413 ga_clear(&ga);
11414 p_cpo = save_cpo;
11415
11416 return ret;
11417}
11418
11419#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */