blob: 8a2c2052e58119745dfde310f6de09fc0f937579 [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));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +0000421static void set_var __ARGS((char_u *name, VAR varp, int copy));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422static 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 {
Bram Moolenaar1c2fda22005-01-02 11:43:19 +0000458 set_var(name, varp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 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 {
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001217 set_var(temp_string, &retvar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 vim_free(temp_string);
1219 }
1220 }
1221#endif
1222 else
1223 {
1224 c1 = *p;
1225 *p = NUL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00001226 set_var(arg, &retvar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 *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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 if (avar->var_type == VAR_NUMBER)
3432 buf = buflist_findnr((int)avar->var_val.var_number);
3433 else if (avar->var_val.var_string != NULL)
3434 {
Bram Moolenaar8fc061c2004-12-29 21:03:02 +00003435 buf = buflist_findname_exp(avar->var_val.var_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003436 if (buf == NULL)
3437 {
3438 /* No full path name match, try a match with a URL or a "nofile"
3439 * buffer, these don't use the full path. */
3440 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3441 if (buf->b_fname != NULL
3442 && (path_with_url(buf->b_fname)
3443#ifdef FEAT_QUICKFIX
3444 || bt_nofile(buf)
3445#endif
3446 )
3447 && STRCMP(buf->b_fname, avar->var_val.var_string) == 0)
3448 break;
3449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451 return buf;
3452}
3453
3454/*
3455 * "bufexists(expr)" function
3456 */
3457 static void
3458f_bufexists(argvars, retvar)
3459 VAR argvars;
3460 VAR retvar;
3461{
3462 retvar->var_val.var_number = (find_buffer(&argvars[0]) != NULL);
3463}
3464
3465/*
3466 * "buflisted(expr)" function
3467 */
3468 static void
3469f_buflisted(argvars, retvar)
3470 VAR argvars;
3471 VAR retvar;
3472{
3473 buf_T *buf;
3474
3475 buf = find_buffer(&argvars[0]);
3476 retvar->var_val.var_number = (buf != NULL && buf->b_p_bl);
3477}
3478
3479/*
3480 * "bufloaded(expr)" function
3481 */
3482 static void
3483f_bufloaded(argvars, retvar)
3484 VAR argvars;
3485 VAR retvar;
3486{
3487 buf_T *buf;
3488
3489 buf = find_buffer(&argvars[0]);
3490 retvar->var_val.var_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
3491}
3492
3493/*
3494 * Get buffer by number or pattern.
3495 */
3496 static buf_T *
3497get_buf_var(avar)
3498 VAR avar;
3499{
3500 char_u *name = avar->var_val.var_string;
3501 int save_magic;
3502 char_u *save_cpo;
3503 buf_T *buf;
3504
3505 if (avar->var_type == VAR_NUMBER)
3506 return buflist_findnr((int)avar->var_val.var_number);
3507 if (name == NULL || *name == NUL)
3508 return curbuf;
3509 if (name[0] == '$' && name[1] == NUL)
3510 return lastbuf;
3511
3512 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
3513 save_magic = p_magic;
3514 p_magic = TRUE;
3515 save_cpo = p_cpo;
3516 p_cpo = (char_u *)"";
3517
3518 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
3519 TRUE, FALSE));
3520
3521 p_magic = save_magic;
3522 p_cpo = save_cpo;
3523
3524 /* If not found, try expanding the name, like done for bufexists(). */
3525 if (buf == NULL)
3526 buf = find_buffer(avar);
3527
3528 return buf;
3529}
3530
3531/*
3532 * "bufname(expr)" function
3533 */
3534 static void
3535f_bufname(argvars, retvar)
3536 VAR argvars;
3537 VAR retvar;
3538{
3539 buf_T *buf;
3540
3541 ++emsg_off;
3542 buf = get_buf_var(&argvars[0]);
3543 retvar->var_type = VAR_STRING;
3544 if (buf != NULL && buf->b_fname != NULL)
3545 retvar->var_val.var_string = vim_strsave(buf->b_fname);
3546 else
3547 retvar->var_val.var_string = NULL;
3548 --emsg_off;
3549}
3550
3551/*
3552 * "bufnr(expr)" function
3553 */
3554 static void
3555f_bufnr(argvars, retvar)
3556 VAR argvars;
3557 VAR retvar;
3558{
3559 buf_T *buf;
3560
3561 ++emsg_off;
3562 buf = get_buf_var(&argvars[0]);
3563 if (buf != NULL)
3564 retvar->var_val.var_number = buf->b_fnum;
3565 else
3566 retvar->var_val.var_number = -1;
3567 --emsg_off;
3568}
3569
3570/*
3571 * "bufwinnr(nr)" function
3572 */
3573 static void
3574f_bufwinnr(argvars, retvar)
3575 VAR argvars;
3576 VAR retvar;
3577{
3578#ifdef FEAT_WINDOWS
3579 win_T *wp;
3580 int winnr = 0;
3581#endif
3582 buf_T *buf;
3583
3584 ++emsg_off;
3585 buf = get_buf_var(&argvars[0]);
3586#ifdef FEAT_WINDOWS
3587 for (wp = firstwin; wp; wp = wp->w_next)
3588 {
3589 ++winnr;
3590 if (wp->w_buffer == buf)
3591 break;
3592 }
3593 retvar->var_val.var_number = (wp != NULL ? winnr : -1);
3594#else
3595 retvar->var_val.var_number = (curwin->w_buffer == buf ? 1 : -1);
3596#endif
3597 --emsg_off;
3598}
3599
3600/*
3601 * "byte2line(byte)" function
3602 */
3603/*ARGSUSED*/
3604 static void
3605f_byte2line(argvars, retvar)
3606 VAR argvars;
3607 VAR retvar;
3608{
3609#ifndef FEAT_BYTEOFF
3610 retvar->var_val.var_number = -1;
3611#else
3612 long boff = 0;
3613
3614 boff = get_var_number(&argvars[0]) - 1;
3615 if (boff < 0)
3616 retvar->var_val.var_number = -1;
3617 else
3618 retvar->var_val.var_number = ml_find_line_or_offset(curbuf,
3619 (linenr_T)0, &boff);
3620#endif
3621}
3622
3623/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003624 * "byteidx()" function
3625 */
3626/*ARGSUSED*/
3627 static void
3628f_byteidx(argvars, retvar)
3629 VAR argvars;
3630 VAR retvar;
3631{
3632#ifdef FEAT_MBYTE
3633 char_u *t;
3634#endif
3635 char_u *str;
3636 long idx;
3637
3638 str = get_var_string(&argvars[0]);
3639 idx = get_var_number(&argvars[1]);
3640 retvar->var_val.var_number = -1;
3641 if (idx < 0)
3642 return;
3643
3644#ifdef FEAT_MBYTE
3645 t = str;
3646 for ( ; idx > 0; idx--)
3647 {
3648 if (*t == NUL) /* EOL reached */
3649 return;
3650 t += mb_ptr2len_check(t);
3651 }
3652 retvar->var_val.var_number = t - str;
3653#else
3654 if (idx <= STRLEN(str))
3655 retvar->var_val.var_number = idx;
3656#endif
3657}
3658
3659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 * "char2nr(string)" function
3661 */
3662 static void
3663f_char2nr(argvars, retvar)
3664 VAR argvars;
3665 VAR retvar;
3666{
3667#ifdef FEAT_MBYTE
3668 if (has_mbyte)
3669 retvar->var_val.var_number =
3670 (*mb_ptr2char)(get_var_string(&argvars[0]));
3671 else
3672#endif
3673 retvar->var_val.var_number = get_var_string(&argvars[0])[0];
3674}
3675
3676/*
3677 * "cindent(lnum)" function
3678 */
3679 static void
3680f_cindent(argvars, retvar)
3681 VAR argvars;
3682 VAR retvar;
3683{
3684#ifdef FEAT_CINDENT
3685 pos_T pos;
3686 linenr_T lnum;
3687
3688 pos = curwin->w_cursor;
3689 lnum = get_var_lnum(argvars);
3690 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3691 {
3692 curwin->w_cursor.lnum = lnum;
3693 retvar->var_val.var_number = get_c_indent();
3694 curwin->w_cursor = pos;
3695 }
3696 else
3697#endif
3698 retvar->var_val.var_number = -1;
3699}
3700
3701/*
3702 * "col(string)" function
3703 */
3704 static void
3705f_col(argvars, retvar)
3706 VAR argvars;
3707 VAR retvar;
3708{
3709 colnr_T col = 0;
3710 pos_T *fp;
3711
3712 fp = var2fpos(&argvars[0], FALSE);
3713 if (fp != NULL)
3714 {
3715 if (fp->col == MAXCOL)
3716 {
3717 /* '> can be MAXCOL, get the length of the line then */
3718 if (fp->lnum <= curbuf->b_ml.ml_line_count)
3719 col = STRLEN(ml_get(fp->lnum)) + 1;
3720 else
3721 col = MAXCOL;
3722 }
3723 else
3724 {
3725 col = fp->col + 1;
3726#ifdef FEAT_VIRTUALEDIT
3727 /* col(".") when the cursor is on the NUL at the end of the line
3728 * because of "coladd" can be seen as an extra column. */
3729 if (virtual_active() && fp == &curwin->w_cursor)
3730 {
3731 char_u *p = ml_get_cursor();
3732
3733 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
3734 curwin->w_virtcol - curwin->w_cursor.coladd))
3735 {
3736# ifdef FEAT_MBYTE
3737 int l;
3738
3739 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
3740 col += l;
3741# else
3742 if (*p != NUL && p[1] == NUL)
3743 ++col;
3744# endif
3745 }
3746 }
3747#endif
3748 }
3749 }
3750 retvar->var_val.var_number = col;
3751}
3752
3753/*
3754 * "confirm(message, buttons[, default [, type]])" function
3755 */
3756/*ARGSUSED*/
3757 static void
3758f_confirm(argvars, retvar)
3759 VAR argvars;
3760 VAR retvar;
3761{
3762#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3763 char_u *message;
3764 char_u *buttons = NULL;
3765 char_u buf[NUMBUFLEN];
3766 char_u buf2[NUMBUFLEN];
3767 int def = 1;
3768 int type = VIM_GENERIC;
3769 int c;
3770
3771 message = get_var_string(&argvars[0]);
3772 if (argvars[1].var_type != VAR_UNKNOWN)
3773 {
3774 buttons = get_var_string_buf(&argvars[1], buf);
3775 if (argvars[2].var_type != VAR_UNKNOWN)
3776 {
3777 def = get_var_number(&argvars[2]);
3778 if (argvars[3].var_type != VAR_UNKNOWN)
3779 {
3780 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
3781 c = *get_var_string_buf(&argvars[3], buf2);
3782 switch (TOUPPER_ASC(c))
3783 {
3784 case 'E': type = VIM_ERROR; break;
3785 case 'Q': type = VIM_QUESTION; break;
3786 case 'I': type = VIM_INFO; break;
3787 case 'W': type = VIM_WARNING; break;
3788 case 'G': type = VIM_GENERIC; break;
3789 }
3790 }
3791 }
3792 }
3793
3794 if (buttons == NULL || *buttons == NUL)
3795 buttons = (char_u *)_("&Ok");
3796
3797 retvar->var_val.var_number = do_dialog(type, NULL, message, buttons,
3798 def, NULL);
3799#else
3800 retvar->var_val.var_number = 0;
3801#endif
3802}
3803
3804
3805/*
3806 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
3807 *
3808 * Checks the existence of a cscope connection.
3809 */
3810/*ARGSUSED*/
3811 static void
3812f_cscope_connection(argvars, retvar)
3813 VAR argvars;
3814 VAR retvar;
3815{
3816#ifdef FEAT_CSCOPE
3817 int num = 0;
3818 char_u *dbpath = NULL;
3819 char_u *prepend = NULL;
3820 char_u buf[NUMBUFLEN];
3821
3822 if (argvars[0].var_type != VAR_UNKNOWN
3823 && argvars[1].var_type != VAR_UNKNOWN)
3824 {
3825 num = (int)get_var_number(&argvars[0]);
3826 dbpath = get_var_string(&argvars[1]);
3827 if (argvars[2].var_type != VAR_UNKNOWN)
3828 prepend = get_var_string_buf(&argvars[2], buf);
3829 }
3830
3831 retvar->var_val.var_number = cs_connection(num, dbpath, prepend);
3832#else
3833 retvar->var_val.var_number = 0;
3834#endif
3835}
3836
3837/*
3838 * "cursor(lnum, col)" function
3839 *
3840 * Moves the cursor to the specified line and column
3841 */
3842/*ARGSUSED*/
3843 static void
3844f_cursor(argvars, retvar)
3845 VAR argvars;
3846 VAR retvar;
3847{
3848 long line, col;
3849
3850 line = get_var_lnum(argvars);
3851 if (line > 0)
3852 curwin->w_cursor.lnum = line;
3853 col = get_var_number(&argvars[1]);
3854 if (col > 0)
3855 curwin->w_cursor.col = col - 1;
3856#ifdef FEAT_VIRTUALEDIT
3857 curwin->w_cursor.coladd = 0;
3858#endif
3859
3860 /* Make sure the cursor is in a valid position. */
3861 check_cursor();
3862#ifdef FEAT_MBYTE
3863 /* Correct cursor for multi-byte character. */
3864 if (has_mbyte)
3865 mb_adjust_cursor();
3866#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00003867
3868 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869}
3870
3871/*
3872 * "libcall()" function
3873 */
3874 static void
3875f_libcall(argvars, retvar)
3876 VAR argvars;
3877 VAR retvar;
3878{
3879 libcall_common(argvars, retvar, VAR_STRING);
3880}
3881
3882/*
3883 * "libcallnr()" function
3884 */
3885 static void
3886f_libcallnr(argvars, retvar)
3887 VAR argvars;
3888 VAR retvar;
3889{
3890 libcall_common(argvars, retvar, VAR_NUMBER);
3891}
3892
3893 static void
3894libcall_common(argvars, retvar, type)
3895 VAR argvars;
3896 VAR retvar;
3897 int type;
3898{
3899#ifdef FEAT_LIBCALL
3900 char_u *string_in;
3901 char_u **string_result;
3902 int nr_result;
3903#endif
3904
3905 retvar->var_type = type;
3906 if (type == VAR_NUMBER)
3907 retvar->var_val.var_number = 0;
3908 else
3909 retvar->var_val.var_string = NULL;
3910
3911 if (check_restricted() || check_secure())
3912 return;
3913
3914#ifdef FEAT_LIBCALL
3915 /* The first two args must be strings, otherwise its meaningless */
3916 if (argvars[0].var_type == VAR_STRING && argvars[1].var_type == VAR_STRING)
3917 {
3918 if (argvars[2].var_type == VAR_NUMBER)
3919 string_in = NULL;
3920 else
3921 string_in = argvars[2].var_val.var_string;
3922 if (type == VAR_NUMBER)
3923 string_result = NULL;
3924 else
3925 string_result = &retvar->var_val.var_string;
3926 if (mch_libcall(argvars[0].var_val.var_string,
3927 argvars[1].var_val.var_string,
3928 string_in,
3929 argvars[2].var_val.var_number,
3930 string_result,
3931 &nr_result) == OK
3932 && type == VAR_NUMBER)
3933 retvar->var_val.var_number = nr_result;
3934 }
3935#endif
3936}
3937
3938/*
3939 * "delete()" function
3940 */
3941 static void
3942f_delete(argvars, retvar)
3943 VAR argvars;
3944 VAR retvar;
3945{
3946 if (check_restricted() || check_secure())
3947 retvar->var_val.var_number = -1;
3948 else
3949 retvar->var_val.var_number = mch_remove(get_var_string(&argvars[0]));
3950}
3951
3952/*
3953 * "did_filetype()" function
3954 */
3955/*ARGSUSED*/
3956 static void
3957f_did_filetype(argvars, retvar)
3958 VAR argvars;
3959 VAR retvar;
3960{
3961#ifdef FEAT_AUTOCMD
3962 retvar->var_val.var_number = did_filetype;
3963#else
3964 retvar->var_val.var_number = 0;
3965#endif
3966}
3967
3968/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00003969 * "diff_filler()" function
3970 */
3971/*ARGSUSED*/
3972 static void
3973f_diff_filler(argvars, retvar)
3974 VAR argvars;
3975 VAR retvar;
3976{
3977#ifdef FEAT_DIFF
3978 retvar->var_val.var_number = diff_check_fill(curwin, get_var_lnum(argvars));
3979#endif
3980}
3981
3982/*
3983 * "diff_hlID()" function
3984 */
3985/*ARGSUSED*/
3986 static void
3987f_diff_hlID(argvars, retvar)
3988 VAR argvars;
3989 VAR retvar;
3990{
3991#ifdef FEAT_DIFF
3992 linenr_T lnum = get_var_lnum(argvars);
3993 static linenr_T prev_lnum = 0;
3994 static int changedtick = 0;
3995 static int fnum = 0;
3996 static int change_start = 0;
3997 static int change_end = 0;
3998 static enum hlf_value hlID = 0;
3999 int filler_lines;
4000 int col;
4001
4002 if (lnum != prev_lnum
4003 || changedtick != curbuf->b_changedtick
4004 || fnum != curbuf->b_fnum)
4005 {
4006 /* New line, buffer, change: need to get the values. */
4007 filler_lines = diff_check(curwin, lnum);
4008 if (filler_lines < 0)
4009 {
4010 if (filler_lines == -1)
4011 {
4012 change_start = MAXCOL;
4013 change_end = -1;
4014 if (diff_find_change(curwin, lnum, &change_start, &change_end))
4015 hlID = HLF_ADD; /* added line */
4016 else
4017 hlID = HLF_CHD; /* changed line */
4018 }
4019 else
4020 hlID = HLF_ADD; /* added line */
4021 }
4022 else
4023 hlID = (enum hlf_value)0;
4024 prev_lnum = lnum;
4025 changedtick = curbuf->b_changedtick;
4026 fnum = curbuf->b_fnum;
4027 }
4028
4029 if (hlID == HLF_CHD || hlID == HLF_TXD)
4030 {
4031 col = get_var_number(&argvars[1]) - 1;
4032 if (col >= change_start && col <= change_end)
4033 hlID = HLF_TXD; /* changed text */
4034 else
4035 hlID = HLF_CHD; /* changed line */
4036 }
4037 retvar->var_val.var_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
4038#endif
4039}
4040
4041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * "escape({string}, {chars})" function
4043 */
4044 static void
4045f_escape(argvars, retvar)
4046 VAR argvars;
4047 VAR retvar;
4048{
4049 char_u buf[NUMBUFLEN];
4050
4051 retvar->var_val.var_string =
4052 vim_strsave_escaped(get_var_string(&argvars[0]),
4053 get_var_string_buf(&argvars[1], buf));
4054 retvar->var_type = VAR_STRING;
4055}
4056
4057/*
4058 * "eventhandler()" function
4059 */
4060/*ARGSUSED*/
4061 static void
4062f_eventhandler(argvars, retvar)
4063 VAR argvars;
4064 VAR retvar;
4065{
4066 retvar->var_val.var_number = vgetc_busy;
4067}
4068
4069/*
4070 * "executable()" function
4071 */
4072 static void
4073f_executable(argvars, retvar)
4074 VAR argvars;
4075 VAR retvar;
4076{
4077 retvar->var_val.var_number = mch_can_exe(get_var_string(&argvars[0]));
4078}
4079
4080/*
4081 * "exists()" function
4082 */
4083 static void
4084f_exists(argvars, retvar)
4085 VAR argvars;
4086 VAR retvar;
4087{
4088 char_u *p;
4089 char_u *name;
4090 int n = FALSE;
4091 int len = 0;
4092
4093 p = get_var_string(&argvars[0]);
4094 if (*p == '$') /* environment variable */
4095 {
4096 /* first try "normal" environment variables (fast) */
4097 if (mch_getenv(p + 1) != NULL)
4098 n = TRUE;
4099 else
4100 {
4101 /* try expanding things like $VIM and ${HOME} */
4102 p = expand_env_save(p);
4103 if (p != NULL && *p != '$')
4104 n = TRUE;
4105 vim_free(p);
4106 }
4107 }
4108 else if (*p == '&' || *p == '+') /* option */
4109 n = (get_option_var(&p, NULL, TRUE) == OK);
4110 else if (*p == '*') /* internal or user defined function */
4111 {
4112 ++p;
4113 p = trans_function_name(&p, FALSE, TRUE);
4114 if (p != NULL)
4115 {
4116 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
4117 n = (find_func(p) != NULL);
4118 else if (ASCII_ISLOWER(*p))
4119 n = (find_internal_func(p) >= 0);
4120 vim_free(p);
4121 }
4122 }
4123 else if (*p == ':')
4124 {
4125 n = cmd_exists(p + 1);
4126 }
4127 else if (*p == '#')
4128 {
4129#ifdef FEAT_AUTOCMD
4130 name = p + 1;
4131 p = vim_strchr(name, '#');
4132 if (p != NULL)
4133 n = au_exists(name, p, p + 1);
4134 else
4135 n = au_exists(name, name + STRLEN(name), NULL);
4136#endif
4137 }
4138 else /* internal variable */
4139 {
4140#ifdef FEAT_MAGIC_BRACES
4141 char_u *expr_start;
4142 char_u *expr_end;
4143 char_u *temp_string = NULL;
4144 char_u *s;
4145#endif
4146 name = p;
4147
4148#ifdef FEAT_MAGIC_BRACES
4149 /* Find the end of the name. */
4150 s = find_name_end(name, &expr_start, &expr_end);
4151 if (expr_start != NULL)
4152 {
4153 temp_string = make_expanded_name(name, expr_start, expr_end, s);
4154 if (temp_string != NULL)
4155 {
4156 len = STRLEN(temp_string);
4157 name = temp_string;
4158 }
4159 }
4160#endif
4161 if (len == 0)
4162 len = get_id_len(&p);
4163 if (len != 0)
4164 n = (get_var_var(name, len, NULL) == OK);
4165
4166#ifdef FEAT_MAGIC_BRACES
4167 vim_free(temp_string);
4168#endif
4169 }
4170
4171 retvar->var_val.var_number = n;
4172}
4173
4174/*
4175 * "expand()" function
4176 */
4177 static void
4178f_expand(argvars, retvar)
4179 VAR argvars;
4180 VAR retvar;
4181{
4182 char_u *s;
4183 int len;
4184 char_u *errormsg;
4185 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
4186 expand_T xpc;
4187
4188 retvar->var_type = VAR_STRING;
4189 s = get_var_string(&argvars[0]);
4190 if (*s == '%' || *s == '#' || *s == '<')
4191 {
4192 ++emsg_off;
4193 retvar->var_val.var_string = eval_vars(s, &len, NULL, &errormsg, s);
4194 --emsg_off;
4195 }
4196 else
4197 {
4198 /* When the optional second argument is non-zero, don't remove matches
4199 * for 'suffixes' and 'wildignore' */
4200 if (argvars[1].var_type != VAR_UNKNOWN && get_var_number(&argvars[1]))
4201 flags |= WILD_KEEP_ALL;
4202 ExpandInit(&xpc);
4203 xpc.xp_context = EXPAND_FILES;
4204 retvar->var_val.var_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
4205 ExpandCleanup(&xpc);
4206 }
4207}
4208
4209/*
4210 * "filereadable()" function
4211 */
4212 static void
4213f_filereadable(argvars, retvar)
4214 VAR argvars;
4215 VAR retvar;
4216{
4217 FILE *fd;
4218 char_u *p;
4219 int n;
4220
4221 p = get_var_string(&argvars[0]);
4222 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
4223 {
4224 n = TRUE;
4225 fclose(fd);
4226 }
4227 else
4228 n = FALSE;
4229
4230 retvar->var_val.var_number = n;
4231}
4232
4233/*
4234 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
4235 * rights to write into.
4236 */
4237 static void
4238f_filewritable(argvars, retvar)
4239 VAR argvars;
4240 VAR retvar;
4241{
4242 char_u *p;
4243 int retval = 0;
4244#if defined(UNIX) || defined(VMS)
4245 int perm = 0;
4246#endif
4247
4248 p = get_var_string(&argvars[0]);
4249#if defined(UNIX) || defined(VMS)
4250 perm = mch_getperm(p);
4251#endif
4252#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
4253 if (
4254# ifdef WIN3264
4255 mch_writable(p) &&
4256# else
4257# if defined(UNIX) || defined(VMS)
4258 (perm & 0222) &&
4259# endif
4260# endif
4261 mch_access((char *)p, W_OK) == 0
4262 )
4263#endif
4264 {
4265 ++retval;
4266 if (mch_isdir(p))
4267 ++retval;
4268 }
4269 retvar->var_val.var_number = retval;
4270}
4271
4272/*
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004273 * "finddir({fname}[, {path}[, {count}]])" function
4274 */
4275 static void
4276f_finddir(argvars, retvar)
4277 VAR argvars;
4278 VAR retvar;
4279{
4280 f_findfilendir(argvars, retvar, TRUE);
4281}
4282
4283/*
4284 * "findfile({fname}[, {path}[, {count}]])" function
4285 */
4286 static void
4287f_findfile(argvars, retvar)
4288 VAR argvars;
4289 VAR retvar;
4290{
4291 f_findfilendir(argvars, retvar, FALSE);
4292}
4293
4294 static void
4295f_findfilendir(argvars, retvar, dir)
4296 VAR argvars;
4297 VAR retvar;
4298 int dir;
4299{
4300#ifdef FEAT_SEARCHPATH
4301 char_u *fname;
4302 char_u *fresult = NULL;
4303 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4304 char_u *p;
4305 char_u pathbuf[NUMBUFLEN];
4306 int count = 1;
4307 int first = TRUE;
4308
4309 fname = get_var_string(&argvars[0]);
4310
4311 if (argvars[1].var_type != VAR_UNKNOWN)
4312 {
4313 p = get_var_string_buf(&argvars[1], pathbuf);
4314 if (*p != NUL)
4315 path = p;
4316
4317 if (argvars[2].var_type != VAR_UNKNOWN)
4318 count = get_var_number(&argvars[2]);
4319 }
4320
4321 do
4322 {
4323 vim_free(fresult);
4324 fresult = find_file_in_path_option(first ? fname : NULL,
4325 first ? (int)STRLEN(fname) : 0,
4326 0, first, path, dir, NULL);
4327 first = FALSE;
4328 } while (--count > 0 && fresult != NULL);
4329
4330 retvar->var_val.var_string = fresult;
4331#else
4332 retvar->var_val.var_string = NULL;
4333#endif
4334 retvar->var_type = VAR_STRING;
4335}
4336
4337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 * "fnamemodify({fname}, {mods})" function
4339 */
4340 static void
4341f_fnamemodify(argvars, retvar)
4342 VAR argvars;
4343 VAR retvar;
4344{
4345 char_u *fname;
4346 char_u *mods;
4347 int usedlen = 0;
4348 int len;
4349 char_u *fbuf = NULL;
4350 char_u buf[NUMBUFLEN];
4351
4352 fname = get_var_string(&argvars[0]);
4353 mods = get_var_string_buf(&argvars[1], buf);
4354 len = (int)STRLEN(fname);
4355
4356 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
4357
4358 retvar->var_type = VAR_STRING;
4359 if (fname == NULL)
4360 retvar->var_val.var_string = NULL;
4361 else
4362 retvar->var_val.var_string = vim_strnsave(fname, len);
4363 vim_free(fbuf);
4364}
4365
4366/*
4367 * "foldclosed()" function
4368 */
4369 static void
4370f_foldclosed(argvars, retvar)
4371 VAR argvars;
4372 VAR retvar;
4373{
4374 foldclosed_both(argvars, retvar, FALSE);
4375}
4376
4377/*
4378 * "foldclosedend()" function
4379 */
4380 static void
4381f_foldclosedend(argvars, retvar)
4382 VAR argvars;
4383 VAR retvar;
4384{
4385 foldclosed_both(argvars, retvar, TRUE);
4386}
4387
4388/*
4389 * "foldclosed()" function
4390 */
4391 static void
4392foldclosed_both(argvars, retvar, end)
4393 VAR argvars;
4394 VAR retvar;
4395 int end;
4396{
4397#ifdef FEAT_FOLDING
4398 linenr_T lnum;
4399 linenr_T first, last;
4400
4401 lnum = get_var_lnum(argvars);
4402 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4403 {
4404 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4405 {
4406 if (end)
4407 retvar->var_val.var_number = (varnumber_T)last;
4408 else
4409 retvar->var_val.var_number = (varnumber_T)first;
4410 return;
4411 }
4412 }
4413#endif
4414 retvar->var_val.var_number = -1;
4415}
4416
4417/*
4418 * "foldlevel()" function
4419 */
4420 static void
4421f_foldlevel(argvars, retvar)
4422 VAR argvars;
4423 VAR retvar;
4424{
4425#ifdef FEAT_FOLDING
4426 linenr_T lnum;
4427
4428 lnum = get_var_lnum(argvars);
4429 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4430 retvar->var_val.var_number = foldLevel(lnum);
4431 else
4432#endif
4433 retvar->var_val.var_number = 0;
4434}
4435
4436/*
4437 * "foldtext()" function
4438 */
4439/*ARGSUSED*/
4440 static void
4441f_foldtext(argvars, retvar)
4442 VAR argvars;
4443 VAR retvar;
4444{
4445#ifdef FEAT_FOLDING
4446 linenr_T lnum;
4447 char_u *s;
4448 char_u *r;
4449 int len;
4450 char *txt;
4451#endif
4452
4453 retvar->var_type = VAR_STRING;
4454 retvar->var_val.var_string = NULL;
4455#ifdef FEAT_FOLDING
4456 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
4457 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
4458 && vimvars[VV_FOLDDASHES].val != NULL)
4459 {
4460 /* Find first non-empty line in the fold. */
4461 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
4462 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
4463 {
4464 if (!linewhite(lnum))
4465 break;
4466 ++lnum;
4467 }
4468
4469 /* Find interesting text in this line. */
4470 s = skipwhite(ml_get(lnum));
4471 /* skip C comment-start */
4472 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004475 if (*skipwhite(s) == NUL
4476 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
4477 {
4478 s = skipwhite(ml_get(lnum + 1));
4479 if (*s == '*')
4480 s = skipwhite(s + 1);
4481 }
4482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 txt = _("+-%s%3ld lines: ");
4484 r = alloc((unsigned)(STRLEN(txt)
4485 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
4486 + 20 /* for %3ld */
4487 + STRLEN(s))); /* concatenated */
4488 if (r != NULL)
4489 {
4490 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
4491 (long)((linenr_T)vimvars[VV_FOLDEND].val
4492 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
4493 len = (int)STRLEN(r);
4494 STRCAT(r, s);
4495 /* remove 'foldmarker' and 'commentstring' */
4496 foldtext_cleanup(r + len);
4497 retvar->var_val.var_string = r;
4498 }
4499 }
4500#endif
4501}
4502
4503/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004504 * "foldtextresult(lnum)" function
4505 */
4506/*ARGSUSED*/
4507 static void
4508f_foldtextresult(argvars, retvar)
4509 VAR argvars;
4510 VAR retvar;
4511{
4512#ifdef FEAT_FOLDING
4513 linenr_T lnum;
4514 char_u *text;
4515 char_u buf[51];
4516 foldinfo_T foldinfo;
4517 int fold_count;
4518#endif
4519
4520 retvar->var_type = VAR_STRING;
4521 retvar->var_val.var_string = NULL;
4522#ifdef FEAT_FOLDING
4523 lnum = get_var_lnum(argvars);
4524 fold_count = foldedCount(curwin, lnum, &foldinfo);
4525 if (fold_count > 0)
4526 {
4527 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4528 &foldinfo, buf);
4529 if (text == buf)
4530 text = vim_strsave(text);
4531 retvar->var_val.var_string = text;
4532 }
4533#endif
4534}
4535
4536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * "foreground()" function
4538 */
4539/*ARGSUSED*/
4540 static void
4541f_foreground(argvars, retvar)
4542 VAR argvars;
4543 VAR retvar;
4544{
4545 retvar->var_val.var_number = 0;
4546#ifdef FEAT_GUI
4547 if (gui.in_use)
4548 gui_mch_set_foreground();
4549#else
4550# ifdef WIN32
4551 win32_set_foreground();
4552# endif
4553#endif
4554}
4555
4556/*
4557 * "getchar()" function
4558 */
4559 static void
4560f_getchar(argvars, retvar)
4561 VAR argvars;
4562 VAR retvar;
4563{
4564 varnumber_T n;
4565
4566 ++no_mapping;
4567 ++allow_keys;
4568 if (argvars[0].var_type == VAR_UNKNOWN)
4569 /* getchar(): blocking wait. */
4570 n = safe_vgetc();
4571 else if (get_var_number(&argvars[0]) == 1)
4572 /* getchar(1): only check if char avail */
4573 n = vpeekc();
4574 else if (vpeekc() == NUL)
4575 /* getchar(0) and no char avail: return zero */
4576 n = 0;
4577 else
4578 /* getchar(0) and char avail: return char */
4579 n = safe_vgetc();
4580 --no_mapping;
4581 --allow_keys;
4582
4583 retvar->var_val.var_number = n;
4584 if (IS_SPECIAL(n) || mod_mask != 0)
4585 {
4586 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4587 int i = 0;
4588
4589 /* Turn a special key into three bytes, plus modifier. */
4590 if (mod_mask != 0)
4591 {
4592 temp[i++] = K_SPECIAL;
4593 temp[i++] = KS_MODIFIER;
4594 temp[i++] = mod_mask;
4595 }
4596 if (IS_SPECIAL(n))
4597 {
4598 temp[i++] = K_SPECIAL;
4599 temp[i++] = K_SECOND(n);
4600 temp[i++] = K_THIRD(n);
4601 }
4602#ifdef FEAT_MBYTE
4603 else if (has_mbyte)
4604 i += (*mb_char2bytes)(n, temp + i);
4605#endif
4606 else
4607 temp[i++] = n;
4608 temp[i++] = NUL;
4609 retvar->var_type = VAR_STRING;
4610 retvar->var_val.var_string = vim_strsave(temp);
4611 }
4612}
4613
4614/*
4615 * "getcharmod()" function
4616 */
4617/*ARGSUSED*/
4618 static void
4619f_getcharmod(argvars, retvar)
4620 VAR argvars;
4621 VAR retvar;
4622{
4623 retvar->var_val.var_number = mod_mask;
4624}
4625
4626/*
4627 * "getcmdline()" function
4628 */
4629/*ARGSUSED*/
4630 static void
4631f_getcmdline(argvars, retvar)
4632 VAR argvars;
4633 VAR retvar;
4634{
4635 retvar->var_type = VAR_STRING;
4636 retvar->var_val.var_string = get_cmdline_str();
4637}
4638
4639/*
4640 * "getcmdpos()" function
4641 */
4642/*ARGSUSED*/
4643 static void
4644f_getcmdpos(argvars, retvar)
4645 VAR argvars;
4646 VAR retvar;
4647{
4648 retvar->var_val.var_number = get_cmdline_pos() + 1;
4649}
4650
4651/*
4652 * "getbufvar()" function
4653 */
4654 static void
4655f_getbufvar(argvars, retvar)
4656 VAR argvars;
4657 VAR retvar;
4658{
4659 buf_T *buf;
4660 buf_T *save_curbuf;
4661 char_u *varname;
4662 VAR v;
4663
4664 ++emsg_off;
4665 buf = get_buf_var(&argvars[0]);
4666 varname = get_var_string(&argvars[1]);
4667
4668 retvar->var_type = VAR_STRING;
4669 retvar->var_val.var_string = NULL;
4670
4671 if (buf != NULL && varname != NULL)
4672 {
4673 if (*varname == '&') /* buffer-local-option */
4674 {
4675 /* set curbuf to be our buf, temporarily */
4676 save_curbuf = curbuf;
4677 curbuf = buf;
4678
4679 get_option_var(&varname, retvar, TRUE);
4680
4681 /* restore previous notion of curbuf */
4682 curbuf = save_curbuf;
4683 }
4684 else
4685 {
4686 /* look up the variable */
4687 v = find_var_in_ga(&buf->b_vars, varname);
4688 if (v != NULL)
4689 copy_var(v, retvar);
4690 }
4691 }
4692
4693 --emsg_off;
4694}
4695
4696/*
4697 * "getcwd()" function
4698 */
4699/*ARGSUSED*/
4700 static void
4701f_getcwd(argvars, retvar)
4702 VAR argvars;
4703 VAR retvar;
4704{
4705 char_u cwd[MAXPATHL];
4706
4707 retvar->var_type = VAR_STRING;
4708 if (mch_dirname(cwd, MAXPATHL) == FAIL)
4709 retvar->var_val.var_string = NULL;
4710 else
4711 {
4712 retvar->var_val.var_string = vim_strsave(cwd);
4713#ifdef BACKSLASH_IN_FILENAME
4714 slash_adjust(retvar->var_val.var_string);
4715#endif
4716 }
4717}
4718
4719/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004720 * "getfontname()" function
4721 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004722/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004723 static void
4724f_getfontname(argvars, retvar)
4725 VAR argvars;
4726 VAR retvar;
4727{
4728 retvar->var_type = VAR_STRING;
4729 retvar->var_val.var_string = NULL;
4730#ifdef FEAT_GUI
4731 if (gui.in_use)
4732 {
4733 GuiFont font;
4734 char_u *name = NULL;
4735
4736 if (argvars[0].var_type == VAR_UNKNOWN)
4737 {
4738 /* Get the "Normal" font. Either the name saved by
4739 * hl_set_font_name() or from the font ID. */
4740 font = gui.norm_font;
4741 name = hl_get_font_name();
4742 }
4743 else
4744 {
4745 name = get_var_string(&argvars[0]);
4746 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4747 return;
4748 font = gui_mch_get_font(name, FALSE);
4749 if (font == NOFONT)
4750 return; /* Invalid font name, return empty string. */
4751 }
4752 retvar->var_val.var_string = gui_mch_get_fontname(font, name);
4753 if (argvars[0].var_type != VAR_UNKNOWN)
4754 gui_mch_free_font(font);
4755 }
4756#endif
4757}
4758
4759/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004760 * "getfperm({fname})" function
4761 */
4762 static void
4763f_getfperm(argvars, retvar)
4764 VAR argvars;
4765 VAR retvar;
4766{
4767 char_u *fname;
4768 struct stat st;
4769 char_u *perm = NULL;
4770 char_u flags[] = "rwx";
4771 int i;
4772
4773 fname = get_var_string(&argvars[0]);
4774
4775 retvar->var_type = VAR_STRING;
4776 if (mch_stat((char *)fname, &st) >= 0)
4777 {
4778 perm = vim_strsave((char_u *)"---------");
4779 if (perm != NULL)
4780 {
4781 for (i = 0; i < 9; i++)
4782 {
4783 if (st.st_mode & (1 << (8 - i)))
4784 perm[i] = flags[i % 3];
4785 }
4786 }
4787 }
4788 retvar->var_val.var_string = perm;
4789}
4790
4791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 * "getfsize({fname})" function
4793 */
4794 static void
4795f_getfsize(argvars, retvar)
4796 VAR argvars;
4797 VAR retvar;
4798{
4799 char_u *fname;
4800 struct stat st;
4801
4802 fname = get_var_string(&argvars[0]);
4803
4804 retvar->var_type = VAR_NUMBER;
4805
4806 if (mch_stat((char *)fname, &st) >= 0)
4807 {
4808 if (mch_isdir(fname))
4809 retvar->var_val.var_number = 0;
4810 else
4811 retvar->var_val.var_number = (varnumber_T)st.st_size;
4812 }
4813 else
4814 retvar->var_val.var_number = -1;
4815}
4816
4817/*
4818 * "getftime({fname})" function
4819 */
4820 static void
4821f_getftime(argvars, retvar)
4822 VAR argvars;
4823 VAR retvar;
4824{
4825 char_u *fname;
4826 struct stat st;
4827
4828 fname = get_var_string(&argvars[0]);
4829
4830 if (mch_stat((char *)fname, &st) >= 0)
4831 retvar->var_val.var_number = (varnumber_T)st.st_mtime;
4832 else
4833 retvar->var_val.var_number = -1;
4834}
4835
4836/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004837 * "getftype({fname})" function
4838 */
4839 static void
4840f_getftype(argvars, retvar)
4841 VAR argvars;
4842 VAR retvar;
4843{
4844 char_u *fname;
4845 struct stat st;
4846 char_u *type = NULL;
4847 char *t;
4848
4849 fname = get_var_string(&argvars[0]);
4850
4851 retvar->var_type = VAR_STRING;
4852 if (mch_lstat((char *)fname, &st) >= 0)
4853 {
4854#ifdef S_ISREG
4855 if (S_ISREG(st.st_mode))
4856 t = "file";
4857 else if (S_ISDIR(st.st_mode))
4858 t = "dir";
4859# ifdef S_ISLNK
4860 else if (S_ISLNK(st.st_mode))
4861 t = "link";
4862# endif
4863# ifdef S_ISBLK
4864 else if (S_ISBLK(st.st_mode))
4865 t = "bdev";
4866# endif
4867# ifdef S_ISCHR
4868 else if (S_ISCHR(st.st_mode))
4869 t = "cdev";
4870# endif
4871# ifdef S_ISFIFO
4872 else if (S_ISFIFO(st.st_mode))
4873 t = "fifo";
4874# endif
4875# ifdef S_ISSOCK
4876 else if (S_ISSOCK(st.st_mode))
4877 t = "fifo";
4878# endif
4879 else
4880 t = "other";
4881#else
4882# ifdef S_IFMT
4883 switch (st.st_mode & S_IFMT)
4884 {
4885 case S_IFREG: t = "file"; break;
4886 case S_IFDIR: t = "dir"; break;
4887# ifdef S_IFLNK
4888 case S_IFLNK: t = "link"; break;
4889# endif
4890# ifdef S_IFBLK
4891 case S_IFBLK: t = "bdev"; break;
4892# endif
4893# ifdef S_IFCHR
4894 case S_IFCHR: t = "cdev"; break;
4895# endif
4896# ifdef S_IFIFO
4897 case S_IFIFO: t = "fifo"; break;
4898# endif
4899# ifdef S_IFSOCK
4900 case S_IFSOCK: t = "socket"; break;
4901# endif
4902 default: t = "other";
4903 }
4904# else
4905 if (mch_isdir(fname))
4906 t = "dir";
4907 else
4908 t = "file";
4909# endif
4910#endif
4911 type = vim_strsave((char_u *)t);
4912 }
4913 retvar->var_val.var_string = type;
4914}
4915
4916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 * "getreg()" function
4918 */
4919 static void
4920f_getreg(argvars, retvar)
4921 VAR argvars;
4922 VAR retvar;
4923{
4924 char_u *strregname;
4925 int regname;
4926
4927 if (argvars[0].var_type != VAR_UNKNOWN)
4928 strregname = get_var_string(&argvars[0]);
4929 else
4930 strregname = vimvars[VV_REG].val;
4931 regname = (strregname == NULL ? '"' : *strregname);
4932 if (regname == 0)
4933 regname = '"';
4934
4935 retvar->var_type = VAR_STRING;
4936 retvar->var_val.var_string = get_reg_contents(regname, TRUE);
4937}
4938
4939/*
4940 * "getregtype()" function
4941 */
4942 static void
4943f_getregtype(argvars, retvar)
4944 VAR argvars;
4945 VAR retvar;
4946{
4947 char_u *strregname;
4948 int regname;
4949 char_u buf[NUMBUFLEN + 2];
4950 long reglen = 0;
4951
4952 if (argvars[0].var_type != VAR_UNKNOWN)
4953 strregname = get_var_string(&argvars[0]);
4954 else
4955 /* Default to v:register */
4956 strregname = vimvars[VV_REG].val;
4957
4958 regname = (strregname == NULL ? '"' : *strregname);
4959 if (regname == 0)
4960 regname = '"';
4961
4962 buf[0] = NUL;
4963 buf[1] = NUL;
4964 switch (get_reg_type(regname, &reglen))
4965 {
4966 case MLINE: buf[0] = 'V'; break;
4967 case MCHAR: buf[0] = 'v'; break;
4968#ifdef FEAT_VISUAL
4969 case MBLOCK:
4970 buf[0] = Ctrl_V;
4971 sprintf((char *)buf + 1, "%ld", reglen + 1);
4972 break;
4973#endif
4974 }
4975 retvar->var_type = VAR_STRING;
4976 retvar->var_val.var_string = vim_strsave(buf);
4977}
4978
4979/*
4980 * "getline(lnum)" function
4981 */
4982 static void
4983f_getline(argvars, retvar)
4984 VAR argvars;
4985 VAR retvar;
4986{
4987 linenr_T lnum;
4988 char_u *p;
4989
4990 lnum = get_var_lnum(argvars);
4991
4992 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4993 p = ml_get(lnum);
4994 else
4995 p = (char_u *)"";
4996
4997 retvar->var_type = VAR_STRING;
4998 retvar->var_val.var_string = vim_strsave(p);
4999}
5000
5001/*
5002 * "getwinposx()" function
5003 */
5004/*ARGSUSED*/
5005 static void
5006f_getwinposx(argvars, retvar)
5007 VAR argvars;
5008 VAR retvar;
5009{
5010 retvar->var_val.var_number = -1;
5011#ifdef FEAT_GUI
5012 if (gui.in_use)
5013 {
5014 int x, y;
5015
5016 if (gui_mch_get_winpos(&x, &y) == OK)
5017 retvar->var_val.var_number = x;
5018 }
5019#endif
5020}
5021
5022/*
5023 * "getwinposy()" function
5024 */
5025/*ARGSUSED*/
5026 static void
5027f_getwinposy(argvars, retvar)
5028 VAR argvars;
5029 VAR retvar;
5030{
5031 retvar->var_val.var_number = -1;
5032#ifdef FEAT_GUI
5033 if (gui.in_use)
5034 {
5035 int x, y;
5036
5037 if (gui_mch_get_winpos(&x, &y) == OK)
5038 retvar->var_val.var_number = y;
5039 }
5040#endif
5041}
5042
5043/*
5044 * "getwinvar()" function
5045 */
5046 static void
5047f_getwinvar(argvars, retvar)
5048 VAR argvars;
5049 VAR retvar;
5050{
5051 win_T *win, *oldcurwin;
5052 char_u *varname;
5053 VAR v;
5054
5055 ++emsg_off;
5056 win = find_win_by_nr(&argvars[0]);
5057 varname = get_var_string(&argvars[1]);
5058
5059 retvar->var_type = VAR_STRING;
5060 retvar->var_val.var_string = NULL;
5061
5062 if (win != NULL && varname != NULL)
5063 {
5064 if (*varname == '&') /* window-local-option */
5065 {
5066 /* set curwin to be our win, temporarily */
5067 oldcurwin = curwin;
5068 curwin = win;
5069
5070 get_option_var(&varname, retvar , 1);
5071
5072 /* restore previous notion of curwin */
5073 curwin = oldcurwin;
5074 }
5075 else
5076 {
5077 /* look up the variable */
5078 v = find_var_in_ga(&win->w_vars, varname);
5079 if (v != NULL)
5080 copy_var(v, retvar);
5081 }
5082 }
5083
5084 --emsg_off;
5085}
5086
5087/*
5088 * "glob()" function
5089 */
5090 static void
5091f_glob(argvars, retvar)
5092 VAR argvars;
5093 VAR retvar;
5094{
5095 expand_T xpc;
5096
5097 ExpandInit(&xpc);
5098 xpc.xp_context = EXPAND_FILES;
5099 retvar->var_type = VAR_STRING;
5100 retvar->var_val.var_string = ExpandOne(&xpc, get_var_string(&argvars[0]),
5101 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
5102 ExpandCleanup(&xpc);
5103}
5104
5105/*
5106 * "globpath()" function
5107 */
5108 static void
5109f_globpath(argvars, retvar)
5110 VAR argvars;
5111 VAR retvar;
5112{
5113 char_u buf1[NUMBUFLEN];
5114
5115 retvar->var_type = VAR_STRING;
5116 retvar->var_val.var_string = globpath(get_var_string(&argvars[0]),
5117 get_var_string_buf(&argvars[1], buf1));
5118}
5119
5120/*
5121 * "has()" function
5122 */
5123 static void
5124f_has(argvars, retvar)
5125 VAR argvars;
5126 VAR retvar;
5127{
5128 int i;
5129 char_u *name;
5130 int n = FALSE;
5131 static char *(has_list[]) =
5132 {
5133#ifdef AMIGA
5134 "amiga",
5135# ifdef FEAT_ARP
5136 "arp",
5137# endif
5138#endif
5139#ifdef __BEOS__
5140 "beos",
5141#endif
5142#ifdef MSDOS
5143# ifdef DJGPP
5144 "dos32",
5145# else
5146 "dos16",
5147# endif
5148#endif
5149#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
5150 "mac",
5151#endif
5152#if defined(MACOS_X_UNIX)
5153 "macunix",
5154#endif
5155#ifdef OS2
5156 "os2",
5157#endif
5158#ifdef __QNX__
5159 "qnx",
5160#endif
5161#ifdef RISCOS
5162 "riscos",
5163#endif
5164#ifdef UNIX
5165 "unix",
5166#endif
5167#ifdef VMS
5168 "vms",
5169#endif
5170#ifdef WIN16
5171 "win16",
5172#endif
5173#ifdef WIN32
5174 "win32",
5175#endif
5176#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5177 "win32unix",
5178#endif
5179#ifdef WIN64
5180 "win64",
5181#endif
5182#ifdef EBCDIC
5183 "ebcdic",
5184#endif
5185#ifndef CASE_INSENSITIVE_FILENAME
5186 "fname_case",
5187#endif
5188#ifdef FEAT_ARABIC
5189 "arabic",
5190#endif
5191#ifdef FEAT_AUTOCMD
5192 "autocmd",
5193#endif
5194#ifdef FEAT_BEVAL
5195 "balloon_eval",
5196#endif
5197#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5198 "builtin_terms",
5199# ifdef ALL_BUILTIN_TCAPS
5200 "all_builtin_terms",
5201# endif
5202#endif
5203#ifdef FEAT_BYTEOFF
5204 "byte_offset",
5205#endif
5206#ifdef FEAT_CINDENT
5207 "cindent",
5208#endif
5209#ifdef FEAT_CLIENTSERVER
5210 "clientserver",
5211#endif
5212#ifdef FEAT_CLIPBOARD
5213 "clipboard",
5214#endif
5215#ifdef FEAT_CMDL_COMPL
5216 "cmdline_compl",
5217#endif
5218#ifdef FEAT_CMDHIST
5219 "cmdline_hist",
5220#endif
5221#ifdef FEAT_COMMENTS
5222 "comments",
5223#endif
5224#ifdef FEAT_CRYPT
5225 "cryptv",
5226#endif
5227#ifdef FEAT_CSCOPE
5228 "cscope",
5229#endif
5230#ifdef DEBUG
5231 "debug",
5232#endif
5233#ifdef FEAT_CON_DIALOG
5234 "dialog_con",
5235#endif
5236#ifdef FEAT_GUI_DIALOG
5237 "dialog_gui",
5238#endif
5239#ifdef FEAT_DIFF
5240 "diff",
5241#endif
5242#ifdef FEAT_DIGRAPHS
5243 "digraphs",
5244#endif
5245#ifdef FEAT_DND
5246 "dnd",
5247#endif
5248#ifdef FEAT_EMACS_TAGS
5249 "emacs_tags",
5250#endif
5251 "eval", /* always present, of course! */
5252#ifdef FEAT_EX_EXTRA
5253 "ex_extra",
5254#endif
5255#ifdef FEAT_SEARCH_EXTRA
5256 "extra_search",
5257#endif
5258#ifdef FEAT_FKMAP
5259 "farsi",
5260#endif
5261#ifdef FEAT_SEARCHPATH
5262 "file_in_path",
5263#endif
5264#ifdef FEAT_FIND_ID
5265 "find_in_path",
5266#endif
5267#ifdef FEAT_FOLDING
5268 "folding",
5269#endif
5270#ifdef FEAT_FOOTER
5271 "footer",
5272#endif
5273#if !defined(USE_SYSTEM) && defined(UNIX)
5274 "fork",
5275#endif
5276#ifdef FEAT_GETTEXT
5277 "gettext",
5278#endif
5279#ifdef FEAT_GUI
5280 "gui",
5281#endif
5282#ifdef FEAT_GUI_ATHENA
5283# ifdef FEAT_GUI_NEXTAW
5284 "gui_neXtaw",
5285# else
5286 "gui_athena",
5287# endif
5288#endif
5289#ifdef FEAT_GUI_BEOS
5290 "gui_beos",
5291#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00005292#ifdef FEAT_GUI_KDE
5293 "gui_kde",
5294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#ifdef FEAT_GUI_GTK
5296 "gui_gtk",
5297# ifdef HAVE_GTK2
5298 "gui_gtk2",
5299# endif
5300#endif
5301#ifdef FEAT_GUI_MAC
5302 "gui_mac",
5303#endif
5304#ifdef FEAT_GUI_MOTIF
5305 "gui_motif",
5306#endif
5307#ifdef FEAT_GUI_PHOTON
5308 "gui_photon",
5309#endif
5310#ifdef FEAT_GUI_W16
5311 "gui_win16",
5312#endif
5313#ifdef FEAT_GUI_W32
5314 "gui_win32",
5315#endif
5316#ifdef FEAT_HANGULIN
5317 "hangul_input",
5318#endif
5319#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5320 "iconv",
5321#endif
5322#ifdef FEAT_INS_EXPAND
5323 "insert_expand",
5324#endif
5325#ifdef FEAT_JUMPLIST
5326 "jumplist",
5327#endif
5328#ifdef FEAT_KEYMAP
5329 "keymap",
5330#endif
5331#ifdef FEAT_LANGMAP
5332 "langmap",
5333#endif
5334#ifdef FEAT_LIBCALL
5335 "libcall",
5336#endif
5337#ifdef FEAT_LINEBREAK
5338 "linebreak",
5339#endif
5340#ifdef FEAT_LISP
5341 "lispindent",
5342#endif
5343#ifdef FEAT_LISTCMDS
5344 "listcmds",
5345#endif
5346#ifdef FEAT_LOCALMAP
5347 "localmap",
5348#endif
5349#ifdef FEAT_MENU
5350 "menu",
5351#endif
5352#ifdef FEAT_SESSION
5353 "mksession",
5354#endif
5355#ifdef FEAT_MODIFY_FNAME
5356 "modify_fname",
5357#endif
5358#ifdef FEAT_MOUSE
5359 "mouse",
5360#endif
5361#ifdef FEAT_MOUSESHAPE
5362 "mouseshape",
5363#endif
5364#if defined(UNIX) || defined(VMS)
5365# ifdef FEAT_MOUSE_DEC
5366 "mouse_dec",
5367# endif
5368# ifdef FEAT_MOUSE_GPM
5369 "mouse_gpm",
5370# endif
5371# ifdef FEAT_MOUSE_JSB
5372 "mouse_jsbterm",
5373# endif
5374# ifdef FEAT_MOUSE_NET
5375 "mouse_netterm",
5376# endif
5377# ifdef FEAT_MOUSE_PTERM
5378 "mouse_pterm",
5379# endif
5380# ifdef FEAT_MOUSE_XTERM
5381 "mouse_xterm",
5382# endif
5383#endif
5384#ifdef FEAT_MBYTE
5385 "multi_byte",
5386#endif
5387#ifdef FEAT_MBYTE_IME
5388 "multi_byte_ime",
5389#endif
5390#ifdef FEAT_MULTI_LANG
5391 "multi_lang",
5392#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005393#ifdef FEAT_MZSCHEME
5394 "mzscheme",
5395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#ifdef FEAT_OLE
5397 "ole",
5398#endif
5399#ifdef FEAT_OSFILETYPE
5400 "osfiletype",
5401#endif
5402#ifdef FEAT_PATH_EXTRA
5403 "path_extra",
5404#endif
5405#ifdef FEAT_PERL
5406#ifndef DYNAMIC_PERL
5407 "perl",
5408#endif
5409#endif
5410#ifdef FEAT_PYTHON
5411#ifndef DYNAMIC_PYTHON
5412 "python",
5413#endif
5414#endif
5415#ifdef FEAT_POSTSCRIPT
5416 "postscript",
5417#endif
5418#ifdef FEAT_PRINTER
5419 "printer",
5420#endif
5421#ifdef FEAT_QUICKFIX
5422 "quickfix",
5423#endif
5424#ifdef FEAT_RIGHTLEFT
5425 "rightleft",
5426#endif
5427#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5428 "ruby",
5429#endif
5430#ifdef FEAT_SCROLLBIND
5431 "scrollbind",
5432#endif
5433#ifdef FEAT_CMDL_INFO
5434 "showcmd",
5435 "cmdline_info",
5436#endif
5437#ifdef FEAT_SIGNS
5438 "signs",
5439#endif
5440#ifdef FEAT_SMARTINDENT
5441 "smartindent",
5442#endif
5443#ifdef FEAT_SNIFF
5444 "sniff",
5445#endif
5446#ifdef FEAT_STL_OPT
5447 "statusline",
5448#endif
5449#ifdef FEAT_SUN_WORKSHOP
5450 "sun_workshop",
5451#endif
5452#ifdef FEAT_NETBEANS_INTG
5453 "netbeans_intg",
5454#endif
5455#ifdef FEAT_SYN_HL
5456 "syntax",
5457#endif
5458#if defined(USE_SYSTEM) || !defined(UNIX)
5459 "system",
5460#endif
5461#ifdef FEAT_TAG_BINS
5462 "tag_binary",
5463#endif
5464#ifdef FEAT_TAG_OLDSTATIC
5465 "tag_old_static",
5466#endif
5467#ifdef FEAT_TAG_ANYWHITE
5468 "tag_any_white",
5469#endif
5470#ifdef FEAT_TCL
5471# ifndef DYNAMIC_TCL
5472 "tcl",
5473# endif
5474#endif
5475#ifdef TERMINFO
5476 "terminfo",
5477#endif
5478#ifdef FEAT_TERMRESPONSE
5479 "termresponse",
5480#endif
5481#ifdef FEAT_TEXTOBJ
5482 "textobjects",
5483#endif
5484#ifdef HAVE_TGETENT
5485 "tgetent",
5486#endif
5487#ifdef FEAT_TITLE
5488 "title",
5489#endif
5490#ifdef FEAT_TOOLBAR
5491 "toolbar",
5492#endif
5493#ifdef FEAT_USR_CMDS
5494 "user-commands", /* was accidentally included in 5.4 */
5495 "user_commands",
5496#endif
5497#ifdef FEAT_VIMINFO
5498 "viminfo",
5499#endif
5500#ifdef FEAT_VERTSPLIT
5501 "vertsplit",
5502#endif
5503#ifdef FEAT_VIRTUALEDIT
5504 "virtualedit",
5505#endif
5506#ifdef FEAT_VISUAL
5507 "visual",
5508#endif
5509#ifdef FEAT_VISUALEXTRA
5510 "visualextra",
5511#endif
5512#ifdef FEAT_VREPLACE
5513 "vreplace",
5514#endif
5515#ifdef FEAT_WILDIGN
5516 "wildignore",
5517#endif
5518#ifdef FEAT_WILDMENU
5519 "wildmenu",
5520#endif
5521#ifdef FEAT_WINDOWS
5522 "windows",
5523#endif
5524#ifdef FEAT_WAK
5525 "winaltkeys",
5526#endif
5527#ifdef FEAT_WRITEBACKUP
5528 "writebackup",
5529#endif
5530#ifdef FEAT_XIM
5531 "xim",
5532#endif
5533#ifdef FEAT_XFONTSET
5534 "xfontset",
5535#endif
5536#ifdef USE_XSMP
5537 "xsmp",
5538#endif
5539#ifdef USE_XSMP_INTERACT
5540 "xsmp_interact",
5541#endif
5542#ifdef FEAT_XCLIPBOARD
5543 "xterm_clipboard",
5544#endif
5545#ifdef FEAT_XTERM_SAVE
5546 "xterm_save",
5547#endif
5548#if defined(UNIX) && defined(FEAT_X11)
5549 "X11",
5550#endif
5551 NULL
5552 };
5553
5554 name = get_var_string(&argvars[0]);
5555 for (i = 0; has_list[i] != NULL; ++i)
5556 if (STRICMP(name, has_list[i]) == 0)
5557 {
5558 n = TRUE;
5559 break;
5560 }
5561
5562 if (n == FALSE)
5563 {
5564 if (STRNICMP(name, "patch", 5) == 0)
5565 n = has_patch(atoi((char *)name + 5));
5566 else if (STRICMP(name, "vim_starting") == 0)
5567 n = (starting != 0);
5568#ifdef DYNAMIC_TCL
5569 else if (STRICMP(name, "tcl") == 0)
5570 n = tcl_enabled(FALSE);
5571#endif
5572#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5573 else if (STRICMP(name, "iconv") == 0)
5574 n = iconv_enabled(FALSE);
5575#endif
5576#ifdef DYNAMIC_RUBY
5577 else if (STRICMP(name, "ruby") == 0)
5578 n = ruby_enabled(FALSE);
5579#endif
5580#ifdef DYNAMIC_PYTHON
5581 else if (STRICMP(name, "python") == 0)
5582 n = python_enabled(FALSE);
5583#endif
5584#ifdef DYNAMIC_PERL
5585 else if (STRICMP(name, "perl") == 0)
5586 n = perl_enabled(FALSE);
5587#endif
5588#ifdef FEAT_GUI
5589 else if (STRICMP(name, "gui_running") == 0)
5590 n = (gui.in_use || gui.starting);
5591# ifdef FEAT_GUI_W32
5592 else if (STRICMP(name, "gui_win32s") == 0)
5593 n = gui_is_win32s();
5594# endif
5595# ifdef FEAT_BROWSE
5596 else if (STRICMP(name, "browse") == 0)
5597 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
5598# endif
5599#endif
5600#ifdef FEAT_SYN_HL
5601 else if (STRICMP(name, "syntax_items") == 0)
5602 n = syntax_present(curbuf);
5603#endif
5604#if defined(WIN3264)
5605 else if (STRICMP(name, "win95") == 0)
5606 n = mch_windows95();
5607#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00005608#ifdef FEAT_NETBEANS_INTG
5609 else if (STRICMP(name, "netbeans_enabled") == 0)
5610 n = usingNetbeans;
5611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613
5614 retvar->var_val.var_number = n;
5615}
5616
5617/*
5618 * "hasmapto()" function
5619 */
5620 static void
5621f_hasmapto(argvars, retvar)
5622 VAR argvars;
5623 VAR retvar;
5624{
5625 char_u *name;
5626 char_u *mode;
5627 char_u buf[NUMBUFLEN];
5628
5629 name = get_var_string(&argvars[0]);
5630 if (argvars[1].var_type == VAR_UNKNOWN)
5631 mode = (char_u *)"nvo";
5632 else
5633 mode = get_var_string_buf(&argvars[1], buf);
5634
5635 if (map_to_exists(name, mode))
5636 retvar->var_val.var_number = TRUE;
5637 else
5638 retvar->var_val.var_number = FALSE;
5639}
5640
5641/*
5642 * "histadd()" function
5643 */
5644/*ARGSUSED*/
5645 static void
5646f_histadd(argvars, retvar)
5647 VAR argvars;
5648 VAR retvar;
5649{
5650#ifdef FEAT_CMDHIST
5651 int histype;
5652 char_u *str;
5653 char_u buf[NUMBUFLEN];
5654#endif
5655
5656 retvar->var_val.var_number = FALSE;
5657 if (check_restricted() || check_secure())
5658 return;
5659#ifdef FEAT_CMDHIST
5660 histype = get_histtype(get_var_string(&argvars[0]));
5661 if (histype >= 0)
5662 {
5663 str = get_var_string_buf(&argvars[1], buf);
5664 if (*str != NUL)
5665 {
5666 add_to_history(histype, str, FALSE, NUL);
5667 retvar->var_val.var_number = TRUE;
5668 return;
5669 }
5670 }
5671#endif
5672}
5673
5674/*
5675 * "histdel()" function
5676 */
5677/*ARGSUSED*/
5678 static void
5679f_histdel(argvars, retvar)
5680 VAR argvars;
5681 VAR retvar;
5682{
5683#ifdef FEAT_CMDHIST
5684 int n;
5685 char_u buf[NUMBUFLEN];
5686
5687 if (argvars[1].var_type == VAR_UNKNOWN)
5688 /* only one argument: clear entire history */
5689 n = clr_history(get_histtype(get_var_string(&argvars[0])));
5690 else if (argvars[1].var_type == VAR_NUMBER)
5691 /* index given: remove that entry */
5692 n = del_history_idx(get_histtype(get_var_string(&argvars[0])),
5693 (int)get_var_number(&argvars[1]));
5694 else
5695 /* string given: remove all matching entries */
5696 n = del_history_entry(get_histtype(get_var_string(&argvars[0])),
5697 get_var_string_buf(&argvars[1], buf));
5698 retvar->var_val.var_number = n;
5699#else
5700 retvar->var_val.var_number = 0;
5701#endif
5702}
5703
5704/*
5705 * "histget()" function
5706 */
5707/*ARGSUSED*/
5708 static void
5709f_histget(argvars, retvar)
5710 VAR argvars;
5711 VAR retvar;
5712{
5713#ifdef FEAT_CMDHIST
5714 int type;
5715 int idx;
5716
5717 type = get_histtype(get_var_string(&argvars[0]));
5718 if (argvars[1].var_type == VAR_UNKNOWN)
5719 idx = get_history_idx(type);
5720 else
5721 idx = (int)get_var_number(&argvars[1]);
5722 retvar->var_val.var_string = vim_strsave(get_history_entry(type, idx));
5723#else
5724 retvar->var_val.var_string = NULL;
5725#endif
5726 retvar->var_type = VAR_STRING;
5727}
5728
5729/*
5730 * "histnr()" function
5731 */
5732/*ARGSUSED*/
5733 static void
5734f_histnr(argvars, retvar)
5735 VAR argvars;
5736 VAR retvar;
5737{
5738 int i;
5739
5740#ifdef FEAT_CMDHIST
5741 i = get_histtype(get_var_string(&argvars[0]));
5742 if (i >= HIST_CMD && i < HIST_COUNT)
5743 i = get_history_idx(i);
5744 else
5745#endif
5746 i = -1;
5747 retvar->var_val.var_number = i;
5748}
5749
5750/*
5751 * "highlight_exists()" function
5752 */
5753 static void
5754f_hlexists(argvars, retvar)
5755 VAR argvars;
5756 VAR retvar;
5757{
5758 retvar->var_val.var_number = highlight_exists(get_var_string(&argvars[0]));
5759}
5760
5761/*
5762 * "highlightID(name)" function
5763 */
5764 static void
5765f_hlID(argvars, retvar)
5766 VAR argvars;
5767 VAR retvar;
5768{
5769 retvar->var_val.var_number = syn_name2id(get_var_string(&argvars[0]));
5770}
5771
5772/*
5773 * "hostname()" function
5774 */
5775/*ARGSUSED*/
5776 static void
5777f_hostname(argvars, retvar)
5778 VAR argvars;
5779 VAR retvar;
5780{
5781 char_u hostname[256];
5782
5783 mch_get_host_name(hostname, 256);
5784 retvar->var_type = VAR_STRING;
5785 retvar->var_val.var_string = vim_strsave(hostname);
5786}
5787
5788/*
5789 * iconv() function
5790 */
5791/*ARGSUSED*/
5792 static void
5793f_iconv(argvars, retvar)
5794 VAR argvars;
5795 VAR retvar;
5796{
5797#ifdef FEAT_MBYTE
5798 char_u buf1[NUMBUFLEN];
5799 char_u buf2[NUMBUFLEN];
5800 char_u *from, *to, *str;
5801 vimconv_T vimconv;
5802#endif
5803
5804 retvar->var_type = VAR_STRING;
5805 retvar->var_val.var_string = NULL;
5806
5807#ifdef FEAT_MBYTE
5808 str = get_var_string(&argvars[0]);
5809 from = enc_canonize(enc_skip(get_var_string_buf(&argvars[1], buf1)));
5810 to = enc_canonize(enc_skip(get_var_string_buf(&argvars[2], buf2)));
5811 vimconv.vc_type = CONV_NONE;
5812 convert_setup(&vimconv, from, to);
5813
5814 /* If the encodings are equal, no conversion needed. */
5815 if (vimconv.vc_type == CONV_NONE)
5816 retvar->var_val.var_string = vim_strsave(str);
5817 else
5818 retvar->var_val.var_string = string_convert(&vimconv, str, NULL);
5819
5820 convert_setup(&vimconv, NULL, NULL);
5821 vim_free(from);
5822 vim_free(to);
5823#endif
5824}
5825
5826/*
5827 * "indent()" function
5828 */
5829 static void
5830f_indent(argvars, retvar)
5831 VAR argvars;
5832 VAR retvar;
5833{
5834 linenr_T lnum;
5835
5836 lnum = get_var_lnum(argvars);
5837 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5838 retvar->var_val.var_number = get_indent_lnum(lnum);
5839 else
5840 retvar->var_val.var_number = -1;
5841}
5842
5843static int inputsecret_flag = 0;
5844
5845/*
5846 * "input()" function
5847 * Also handles inputsecret() when inputsecret is set.
5848 */
5849 static void
5850f_input(argvars, retvar)
5851 VAR argvars;
5852 VAR retvar;
5853{
5854 char_u *prompt = get_var_string(&argvars[0]);
5855 char_u *p = NULL;
5856 int c;
5857 char_u buf[NUMBUFLEN];
5858 int cmd_silent_save = cmd_silent;
5859
5860 retvar->var_type = VAR_STRING;
5861
5862#ifdef NO_CONSOLE_INPUT
5863 /* While starting up, there is no place to enter text. */
5864 if (no_console_input())
5865 {
5866 retvar->var_val.var_string = NULL;
5867 return;
5868 }
5869#endif
5870
5871 cmd_silent = FALSE; /* Want to see the prompt. */
5872 if (prompt != NULL)
5873 {
5874 /* Only the part of the message after the last NL is considered as
5875 * prompt for the command line */
5876 p = vim_strrchr(prompt, '\n');
5877 if (p == NULL)
5878 p = prompt;
5879 else
5880 {
5881 ++p;
5882 c = *p;
5883 *p = NUL;
5884 msg_start();
5885 msg_clr_eos();
5886 msg_puts_attr(prompt, echo_attr);
5887 msg_didout = FALSE;
5888 msg_starthere();
5889 *p = c;
5890 }
5891 cmdline_row = msg_row;
5892 }
5893
5894 if (argvars[1].var_type != VAR_UNKNOWN)
5895 stuffReadbuffSpec(get_var_string_buf(&argvars[1], buf));
5896
5897 retvar->var_val.var_string =
5898 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
5899
5900 /* since the user typed this, no need to wait for return */
5901 need_wait_return = FALSE;
5902 msg_didout = FALSE;
5903 cmd_silent = cmd_silent_save;
5904}
5905
5906/*
5907 * "inputdialog()" function
5908 */
5909 static void
5910f_inputdialog(argvars, retvar)
5911 VAR argvars;
5912 VAR retvar;
5913{
5914#if defined(FEAT_GUI_TEXTDIALOG)
5915 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
5916 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5917 {
5918 char_u *message;
5919 char_u buf[NUMBUFLEN];
5920
5921 message = get_var_string(&argvars[0]);
5922 if (argvars[1].var_type != VAR_UNKNOWN)
5923 {
5924 STRNCPY(IObuff, get_var_string_buf(&argvars[1], buf), IOSIZE);
5925 IObuff[IOSIZE - 1] = NUL;
5926 }
5927 else
5928 IObuff[0] = NUL;
5929 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
5930 1, IObuff) == 1)
5931 retvar->var_val.var_string = vim_strsave(IObuff);
5932 else
5933 {
5934 if (argvars[1].var_type != VAR_UNKNOWN
5935 && argvars[2].var_type != VAR_UNKNOWN)
5936 retvar->var_val.var_string = vim_strsave(
5937 get_var_string_buf(&argvars[2], buf));
5938 else
5939 retvar->var_val.var_string = NULL;
5940 }
5941 retvar->var_type = VAR_STRING;
5942 }
5943 else
5944#endif
5945 f_input(argvars, retvar);
5946}
5947
5948static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5949
5950/*
5951 * "inputrestore()" function
5952 */
5953/*ARGSUSED*/
5954 static void
5955f_inputrestore(argvars, retvar)
5956 VAR argvars;
5957 VAR retvar;
5958{
5959 if (ga_userinput.ga_len > 0)
5960 {
5961 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5963 + ga_userinput.ga_len);
5964 retvar->var_val.var_number = 0; /* OK */
5965 }
5966 else if (p_verbose > 1)
5967 {
5968 msg((char_u *)_("called inputrestore() more often than inputsave()"));
5969 retvar->var_val.var_number = 1; /* Failed */
5970 }
5971}
5972
5973/*
5974 * "inputsave()" function
5975 */
5976/*ARGSUSED*/
5977 static void
5978f_inputsave(argvars, retvar)
5979 VAR argvars;
5980 VAR retvar;
5981{
5982 /* Add an entry to the stack of typehead storage. */
5983 if (ga_grow(&ga_userinput, 1) == OK)
5984 {
5985 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5986 + ga_userinput.ga_len);
5987 ++ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 retvar->var_val.var_number = 0; /* OK */
5989 }
5990 else
5991 retvar->var_val.var_number = 1; /* Failed */
5992}
5993
5994/*
5995 * "inputsecret()" function
5996 */
5997 static void
5998f_inputsecret(argvars, retvar)
5999 VAR argvars;
6000 VAR retvar;
6001{
6002 ++cmdline_star;
6003 ++inputsecret_flag;
6004 f_input(argvars, retvar);
6005 --cmdline_star;
6006 --inputsecret_flag;
6007}
6008
6009/*
6010 * "isdirectory()" function
6011 */
6012 static void
6013f_isdirectory(argvars, retvar)
6014 VAR argvars;
6015 VAR retvar;
6016{
6017 retvar->var_val.var_number = mch_isdir(get_var_string(&argvars[0]));
6018}
6019
6020/*
6021 * "last_buffer_nr()" function.
6022 */
6023/*ARGSUSED*/
6024 static void
6025f_last_buffer_nr(argvars, retvar)
6026 VAR argvars;
6027 VAR retvar;
6028{
6029 int n = 0;
6030 buf_T *buf;
6031
6032 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6033 if (n < buf->b_fnum)
6034 n = buf->b_fnum;
6035
6036 retvar->var_val.var_number = n;
6037}
6038
6039/*
6040 * "line(string)" function
6041 */
6042 static void
6043f_line(argvars, retvar)
6044 VAR argvars;
6045 VAR retvar;
6046{
6047 linenr_T lnum = 0;
6048 pos_T *fp;
6049
6050 fp = var2fpos(&argvars[0], TRUE);
6051 if (fp != NULL)
6052 lnum = fp->lnum;
6053 retvar->var_val.var_number = lnum;
6054}
6055
6056/*
6057 * "line2byte(lnum)" function
6058 */
6059/*ARGSUSED*/
6060 static void
6061f_line2byte(argvars, retvar)
6062 VAR argvars;
6063 VAR retvar;
6064{
6065#ifndef FEAT_BYTEOFF
6066 retvar->var_val.var_number = -1;
6067#else
6068 linenr_T lnum;
6069
6070 lnum = get_var_lnum(argvars);
6071 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6072 retvar->var_val.var_number = -1;
6073 else
6074 retvar->var_val.var_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6075 if (retvar->var_val.var_number >= 0)
6076 ++retvar->var_val.var_number;
6077#endif
6078}
6079
6080/*
6081 * "lispindent(lnum)" function
6082 */
6083 static void
6084f_lispindent(argvars, retvar)
6085 VAR argvars;
6086 VAR retvar;
6087{
6088#ifdef FEAT_LISP
6089 pos_T pos;
6090 linenr_T lnum;
6091
6092 pos = curwin->w_cursor;
6093 lnum = get_var_lnum(argvars);
6094 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6095 {
6096 curwin->w_cursor.lnum = lnum;
6097 retvar->var_val.var_number = get_lisp_indent();
6098 curwin->w_cursor = pos;
6099 }
6100 else
6101#endif
6102 retvar->var_val.var_number = -1;
6103}
6104
6105/*
6106 * "localtime()" function
6107 */
6108/*ARGSUSED*/
6109 static void
6110f_localtime(argvars, retvar)
6111 VAR argvars;
6112 VAR retvar;
6113{
6114 retvar->var_val.var_number = (varnumber_T)time(NULL);
6115}
6116
6117/*
6118 * "maparg()" function
6119 */
6120 static void
6121f_maparg(argvars, retvar)
6122 VAR argvars;
6123 VAR retvar;
6124{
6125 get_maparg(argvars, retvar, TRUE);
6126}
6127
6128/*
6129 * "mapcheck()" function
6130 */
6131 static void
6132f_mapcheck(argvars, retvar)
6133 VAR argvars;
6134 VAR retvar;
6135{
6136 get_maparg(argvars, retvar, FALSE);
6137}
6138
6139 static void
6140get_maparg(argvars, retvar, exact)
6141 VAR argvars;
6142 VAR retvar;
6143 int exact;
6144{
6145 char_u *keys;
6146 char_u *which;
6147 char_u buf[NUMBUFLEN];
6148 char_u *keys_buf = NULL;
6149 char_u *rhs;
6150 int mode;
6151 garray_T ga;
6152
6153 /* return empty string for failure */
6154 retvar->var_type = VAR_STRING;
6155 retvar->var_val.var_string = NULL;
6156
6157 keys = get_var_string(&argvars[0]);
6158 if (*keys == NUL)
6159 return;
6160
6161 if (argvars[1].var_type != VAR_UNKNOWN)
6162 which = get_var_string_buf(&argvars[1], buf);
6163 else
6164 which = (char_u *)"";
6165 mode = get_map_mode(&which, 0);
6166
6167 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
6168 rhs = check_map(keys, mode, exact);
6169 vim_free(keys_buf);
6170 if (rhs != NULL)
6171 {
6172 ga_init(&ga);
6173 ga.ga_itemsize = 1;
6174 ga.ga_growsize = 40;
6175
6176 while (*rhs != NUL)
6177 ga_concat(&ga, str2special(&rhs, FALSE));
6178
6179 ga_append(&ga, NUL);
6180 retvar->var_val.var_string = (char_u *)ga.ga_data;
6181 }
6182}
6183
6184/*
6185 * "match()" function
6186 */
6187 static void
6188f_match(argvars, retvar)
6189 VAR argvars;
6190 VAR retvar;
6191{
6192 find_some_match(argvars, retvar, 1);
6193}
6194
6195/*
6196 * "matchend()" function
6197 */
6198 static void
6199f_matchend(argvars, retvar)
6200 VAR argvars;
6201 VAR retvar;
6202{
6203 find_some_match(argvars, retvar, 0);
6204}
6205
6206/*
6207 * "matchstr()" function
6208 */
6209 static void
6210f_matchstr(argvars, retvar)
6211 VAR argvars;
6212 VAR retvar;
6213{
6214 find_some_match(argvars, retvar, 2);
6215}
6216
6217 static void
6218find_some_match(argvars, retvar, type)
6219 VAR argvars;
6220 VAR retvar;
6221 int type;
6222{
6223 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006224 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 char_u *pat;
6226 regmatch_T regmatch;
6227 char_u patbuf[NUMBUFLEN];
6228 char_u *save_cpo;
6229 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006230 long nth = 1;
6231 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6234 save_cpo = p_cpo;
6235 p_cpo = (char_u *)"";
6236
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006237 expr = str = get_var_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 pat = get_var_string_buf(&argvars[1], patbuf);
6239
6240 if (type == 2)
6241 {
6242 retvar->var_type = VAR_STRING;
6243 retvar->var_val.var_string = NULL;
6244 }
6245 else
6246 retvar->var_val.var_number = -1;
6247
6248 if (argvars[2].var_type != VAR_UNKNOWN)
6249 {
6250 start = get_var_number(&argvars[2]);
6251 if (start < 0)
6252 start = 0;
6253 if (start > (long)STRLEN(str))
6254 goto theend;
6255 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006256
6257 if (argvars[3].var_type != VAR_UNKNOWN)
6258 nth = get_var_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260
6261 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6262 if (regmatch.regprog != NULL)
6263 {
6264 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006265
6266 while (1)
6267 {
6268 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
6269 if (!match || --nth <= 0)
6270 break;
6271 /* Advance to just after the match. */
6272#ifdef FEAT_MBYTE
6273 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
6274#else
6275 str = regmatch.startp[0] + 1;
6276#endif
6277 }
6278
6279 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
6281 if (type == 2)
6282 retvar->var_val.var_string = vim_strnsave(regmatch.startp[0],
6283 (int)(regmatch.endp[0] - regmatch.startp[0]));
6284 else
6285 {
6286 if (type != 0)
6287 retvar->var_val.var_number =
6288 (varnumber_T)(regmatch.startp[0] - str);
6289 else
6290 retvar->var_val.var_number =
6291 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006292 retvar->var_val.var_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294 }
6295 vim_free(regmatch.regprog);
6296 }
6297
6298theend:
6299 p_cpo = save_cpo;
6300}
6301
6302/*
6303 * "mode()" function
6304 */
6305/*ARGSUSED*/
6306 static void
6307f_mode(argvars, retvar)
6308 VAR argvars;
6309 VAR retvar;
6310{
6311 char_u buf[2];
6312
6313#ifdef FEAT_VISUAL
6314 if (VIsual_active)
6315 {
6316 if (VIsual_select)
6317 buf[0] = VIsual_mode + 's' - 'v';
6318 else
6319 buf[0] = VIsual_mode;
6320 }
6321 else
6322#endif
6323 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
6324 buf[0] = 'r';
6325 else if (State & INSERT)
6326 {
6327 if (State & REPLACE_FLAG)
6328 buf[0] = 'R';
6329 else
6330 buf[0] = 'i';
6331 }
6332 else if (State & CMDLINE)
6333 buf[0] = 'c';
6334 else
6335 buf[0] = 'n';
6336
6337 buf[1] = NUL;
6338 retvar->var_val.var_string = vim_strsave(buf);
6339 retvar->var_type = VAR_STRING;
6340}
6341
6342/*
6343 * "nr2char()" function
6344 */
6345 static void
6346f_nr2char(argvars, retvar)
6347 VAR argvars;
6348 VAR retvar;
6349{
6350 char_u buf[NUMBUFLEN];
6351
6352#ifdef FEAT_MBYTE
6353 if (has_mbyte)
6354 buf[(*mb_char2bytes)((int)get_var_number(&argvars[0]), buf)] = NUL;
6355 else
6356#endif
6357 {
6358 buf[0] = (char_u)get_var_number(&argvars[0]);
6359 buf[1] = NUL;
6360 }
6361 retvar->var_type = VAR_STRING;
6362 retvar->var_val.var_string = vim_strsave(buf);
6363}
6364
6365/*
6366 * "rename({from}, {to})" function
6367 */
6368 static void
6369f_rename(argvars, retvar)
6370 VAR argvars;
6371 VAR retvar;
6372{
6373 char_u buf[NUMBUFLEN];
6374
6375 if (check_restricted() || check_secure())
6376 retvar->var_val.var_number = -1;
6377 else
6378 retvar->var_val.var_number = vim_rename(get_var_string(&argvars[0]),
6379 get_var_string_buf(&argvars[1], buf));
6380}
6381
6382/*
6383 * "resolve()" function
6384 */
6385 static void
6386f_resolve(argvars, retvar)
6387 VAR argvars;
6388 VAR retvar;
6389{
6390 char_u *p;
6391
6392 p = get_var_string(&argvars[0]);
6393#ifdef FEAT_SHORTCUT
6394 {
6395 char_u *v = NULL;
6396
6397 v = mch_resolve_shortcut(p);
6398 if (v != NULL)
6399 retvar->var_val.var_string = v;
6400 else
6401 retvar->var_val.var_string = vim_strsave(p);
6402 }
6403#else
6404# ifdef HAVE_READLINK
6405 {
6406 char_u buf[MAXPATHL + 1];
6407 char_u *cpy;
6408 int len;
6409 char_u *remain = NULL;
6410 char_u *q;
6411 int is_relative_to_current = FALSE;
6412 int has_trailing_pathsep = FALSE;
6413 int limit = 100;
6414
6415 p = vim_strsave(p);
6416
6417 if (p[0] == '.' && (vim_ispathsep(p[1])
6418 || (p[1] == '.' && (vim_ispathsep(p[2])))))
6419 is_relative_to_current = TRUE;
6420
6421 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006422 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 has_trailing_pathsep = TRUE;
6424
6425 q = getnextcomp(p);
6426 if (*q != NUL)
6427 {
6428 /* Separate the first path component in "p", and keep the
6429 * remainder (beginning with the path separator). */
6430 remain = vim_strsave(q - 1);
6431 q[-1] = NUL;
6432 }
6433
6434 for (;;)
6435 {
6436 for (;;)
6437 {
6438 len = readlink((char *)p, (char *)buf, MAXPATHL);
6439 if (len <= 0)
6440 break;
6441 buf[len] = NUL;
6442
6443 if (limit-- == 0)
6444 {
6445 vim_free(p);
6446 vim_free(remain);
6447 EMSG(_("E655: Too many symbolic links (cycle?)"));
6448 retvar->var_val.var_string = NULL;
6449 goto fail;
6450 }
6451
6452 /* Ensure that the result will have a trailing path separator
6453 * if the argument has one. */
6454 if (remain == NULL && has_trailing_pathsep)
6455 add_pathsep(buf);
6456
6457 /* Separate the first path component in the link value and
6458 * concatenate the remainders. */
6459 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
6460 if (*q != NUL)
6461 {
6462 if (remain == NULL)
6463 remain = vim_strsave(q - 1);
6464 else
6465 {
6466 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
6467 if (cpy != NULL)
6468 {
6469 STRCAT(cpy, remain);
6470 vim_free(remain);
6471 remain = cpy;
6472 }
6473 }
6474 q[-1] = NUL;
6475 }
6476
6477 q = gettail(p);
6478 if (q > p && *q == NUL)
6479 {
6480 /* Ignore trailing path separator. */
6481 q[-1] = NUL;
6482 q = gettail(p);
6483 }
6484 if (q > p && !mch_isFullName(buf))
6485 {
6486 /* symlink is relative to directory of argument */
6487 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
6488 if (cpy != NULL)
6489 {
6490 STRCPY(cpy, p);
6491 STRCPY(gettail(cpy), buf);
6492 vim_free(p);
6493 p = cpy;
6494 }
6495 }
6496 else
6497 {
6498 vim_free(p);
6499 p = vim_strsave(buf);
6500 }
6501 }
6502
6503 if (remain == NULL)
6504 break;
6505
6506 /* Append the first path component of "remain" to "p". */
6507 q = getnextcomp(remain + 1);
6508 len = q - remain - (*q != NUL);
6509 cpy = vim_strnsave(p, STRLEN(p) + len);
6510 if (cpy != NULL)
6511 {
6512 STRNCAT(cpy, remain, len);
6513 vim_free(p);
6514 p = cpy;
6515 }
6516 /* Shorten "remain". */
6517 if (*q != NUL)
6518 STRCPY(remain, q - 1);
6519 else
6520 {
6521 vim_free(remain);
6522 remain = NULL;
6523 }
6524 }
6525
6526 /* If the result is a relative path name, make it explicitly relative to
6527 * the current directory if and only if the argument had this form. */
6528 if (!vim_ispathsep(*p))
6529 {
6530 if (is_relative_to_current
6531 && *p != NUL
6532 && !(p[0] == '.'
6533 && (p[1] == NUL
6534 || vim_ispathsep(p[1])
6535 || (p[1] == '.'
6536 && (p[2] == NUL
6537 || vim_ispathsep(p[2]))))))
6538 {
6539 /* Prepend "./". */
6540 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
6541 if (cpy != NULL)
6542 {
6543 STRCAT(cpy, p);
6544 vim_free(p);
6545 p = cpy;
6546 }
6547 }
6548 else if (!is_relative_to_current)
6549 {
6550 /* Strip leading "./". */
6551 q = p;
6552 while (q[0] == '.' && vim_ispathsep(q[1]))
6553 q += 2;
6554 if (q > p)
6555 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
6556 }
6557 }
6558
6559 /* Ensure that the result will have no trailing path separator
6560 * if the argument had none. But keep "/" or "//". */
6561 if (!has_trailing_pathsep)
6562 {
6563 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006564 if (after_pathsep(p, q))
6565 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
6567
6568 retvar->var_val.var_string = p;
6569 }
6570# else
6571 retvar->var_val.var_string = vim_strsave(p);
6572# endif
6573#endif
6574
6575 simplify_filename(retvar->var_val.var_string);
6576
6577#ifdef HAVE_READLINK
6578fail:
6579#endif
6580 retvar->var_type = VAR_STRING;
6581}
6582
6583/*
6584 * "simplify()" function
6585 */
6586 static void
6587f_simplify(argvars, retvar)
6588 VAR argvars;
6589 VAR retvar;
6590{
6591 char_u *p;
6592
6593 p = get_var_string(&argvars[0]);
6594 retvar->var_val.var_string = vim_strsave(p);
6595 simplify_filename(retvar->var_val.var_string); /* simplify in place */
6596 retvar->var_type = VAR_STRING;
6597}
6598
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006599#define SP_NOMOVE 1 /* don't move cursor */
6600#define SP_REPEAT 2 /* repeat to find outer pair */
6601#define SP_RETCOUNT 4 /* return matchcount */
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603/*
6604 * "search()" function
6605 */
6606 static void
6607f_search(argvars, retvar)
6608 VAR argvars;
6609 VAR retvar;
6610{
6611 char_u *pat;
6612 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006613 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 int save_p_ws = p_ws;
6615 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006616 int flags = 0;
6617
6618 retvar->var_val.var_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 pat = get_var_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006621 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
6622 if (dir == 0)
6623 goto theend;
6624 if ((flags & ~SP_NOMOVE) != 0)
6625 {
6626 EMSG2(_(e_invarg2), get_var_string(&argvars[1]));
6627 goto theend;
6628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006630 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
6632 SEARCH_KEEP, RE_SEARCH) != FAIL)
6633 {
6634 retvar->var_val.var_number = pos.lnum;
6635 curwin->w_cursor = pos;
6636 /* "/$" will put the cursor after the end of the line, may need to
6637 * correct that here */
6638 check_cursor();
6639 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006640
6641 /* If 'n' flag is used: restore cursor position. */
6642 if (flags & SP_NOMOVE)
6643 curwin->w_cursor = save_cursor;
6644theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 p_ws = save_p_ws;
6646}
6647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648/*
6649 * "searchpair()" function
6650 */
6651 static void
6652f_searchpair(argvars, retvar)
6653 VAR argvars;
6654 VAR retvar;
6655{
6656 char_u *spat, *mpat, *epat;
6657 char_u *skip;
6658 char_u *pat, *pat2, *pat3;
6659 pos_T pos;
6660 pos_T firstpos;
6661 pos_T save_cursor;
6662 pos_T save_pos;
6663 int save_p_ws = p_ws;
6664 char_u *save_cpo;
6665 int dir;
6666 int flags = 0;
6667 char_u nbuf1[NUMBUFLEN];
6668 char_u nbuf2[NUMBUFLEN];
6669 char_u nbuf3[NUMBUFLEN];
6670 int n;
6671 int r;
6672 int nest = 1;
6673 int err;
6674
6675 retvar->var_val.var_number = 0; /* default: FAIL */
6676
6677 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6678 save_cpo = p_cpo;
6679 p_cpo = (char_u *)"";
6680
6681 /* Get the three pattern arguments: start, middle, end. */
6682 spat = get_var_string(&argvars[0]);
6683 mpat = get_var_string_buf(&argvars[1], nbuf1);
6684 epat = get_var_string_buf(&argvars[2], nbuf2);
6685
6686 /* Make two search patterns: start/end (pat2, for in nested pairs) and
6687 * start/middle/end (pat3, for the top pair). */
6688 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
6689 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
6690 if (pat2 == NULL || pat3 == NULL)
6691 goto theend;
6692 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
6693 if (*mpat == NUL)
6694 STRCPY(pat3, pat2);
6695 else
6696 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
6697 spat, epat, mpat);
6698
6699 /* Handle the optional fourth argument: flags */
6700 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006701 if (dir == 0)
6702 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704 /* Optional fifth argument: skip expresion */
6705 if (argvars[3].var_type == VAR_UNKNOWN
6706 || argvars[4].var_type == VAR_UNKNOWN)
6707 skip = (char_u *)"";
6708 else
6709 skip = get_var_string_buf(&argvars[4], nbuf3);
6710
6711 save_cursor = curwin->w_cursor;
6712 pos = curwin->w_cursor;
6713 firstpos.lnum = 0;
6714 pat = pat3;
6715 for (;;)
6716 {
6717 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
6718 SEARCH_KEEP, RE_SEARCH);
6719 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
6720 /* didn't find it or found the first match again: FAIL */
6721 break;
6722
6723 if (firstpos.lnum == 0)
6724 firstpos = pos;
6725
6726 /* If the skip pattern matches, ignore this match. */
6727 if (*skip != NUL)
6728 {
6729 save_pos = curwin->w_cursor;
6730 curwin->w_cursor = pos;
6731 r = eval_to_bool(skip, &err, NULL, FALSE);
6732 curwin->w_cursor = save_pos;
6733 if (err)
6734 {
6735 /* Evaluating {skip} caused an error, break here. */
6736 curwin->w_cursor = save_cursor;
6737 retvar->var_val.var_number = -1;
6738 break;
6739 }
6740 if (r)
6741 continue;
6742 }
6743
6744 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
6745 {
6746 /* Found end when searching backwards or start when searching
6747 * forward: nested pair. */
6748 ++nest;
6749 pat = pat2; /* nested, don't search for middle */
6750 }
6751 else
6752 {
6753 /* Found end when searching forward or start when searching
6754 * backward: end of (nested) pair; or found middle in outer pair. */
6755 if (--nest == 1)
6756 pat = pat3; /* outer level, search for middle */
6757 }
6758
6759 if (nest == 0)
6760 {
6761 /* Found the match: return matchcount or line number. */
6762 if (flags & SP_RETCOUNT)
6763 ++retvar->var_val.var_number;
6764 else
6765 retvar->var_val.var_number = pos.lnum;
6766 curwin->w_cursor = pos;
6767 if (!(flags & SP_REPEAT))
6768 break;
6769 nest = 1; /* search for next unmatched */
6770 }
6771 }
6772
6773 /* If 'n' flag is used or search failed: restore cursor position. */
6774 if ((flags & SP_NOMOVE) || retvar->var_val.var_number == 0)
6775 curwin->w_cursor = save_cursor;
6776
6777theend:
6778 vim_free(pat2);
6779 vim_free(pat3);
6780 p_ws = save_p_ws;
6781 p_cpo = save_cpo;
6782}
6783
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006784/*
6785 * Get flags for a search function.
6786 * Possibly sets "p_ws".
6787 * Returns BACKWARD, FORWARD or zero (for an error).
6788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 static int
6790get_search_arg(varp, flagsp)
6791 VAR varp;
6792 int *flagsp;
6793{
6794 int dir = FORWARD;
6795 char_u *flags;
6796 char_u nbuf[NUMBUFLEN];
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006797 int mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 if (varp->var_type != VAR_UNKNOWN)
6800 {
6801 flags = get_var_string_buf(varp, nbuf);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006802 while (*flags != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006804 switch (*flags)
6805 {
6806 case 'b': dir = BACKWARD; break;
6807 case 'w': p_ws = TRUE; break;
6808 case 'W': p_ws = FALSE; break;
6809 default: mask = 0;
6810 if (flagsp != NULL)
6811 switch (*flags)
6812 {
6813 case 'n': mask = SP_NOMOVE; break;
6814 case 'r': mask = SP_REPEAT; break;
6815 case 'm': mask = SP_RETCOUNT; break;
6816 }
6817 if (mask == 0)
6818 {
6819 EMSG2(_(e_invarg2), flags);
6820 dir = 0;
6821 }
6822 else
6823 *flagsp |= mask;
6824 }
6825 if (dir == 0)
6826 break;
6827 ++flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829 }
6830 return dir;
6831}
6832
6833/*
6834 * "setbufvar()" function
6835 */
6836/*ARGSUSED*/
6837 static void
6838f_setbufvar(argvars, retvar)
6839 VAR argvars;
6840 VAR retvar;
6841{
6842 buf_T *buf;
6843#ifdef FEAT_AUTOCMD
6844 aco_save_T aco;
6845#else
6846 buf_T *save_curbuf;
6847#endif
6848 char_u *varname, *bufvarname;
6849 VAR varp;
6850 char_u nbuf[NUMBUFLEN];
6851
6852 if (check_restricted() || check_secure())
6853 return;
6854 ++emsg_off;
6855 buf = get_buf_var(&argvars[0]);
6856 varname = get_var_string(&argvars[1]);
6857 varp = &argvars[2];
6858
6859 if (buf != NULL && varname != NULL && varp != NULL)
6860 {
6861 /* set curbuf to be our buf, temporarily */
6862#ifdef FEAT_AUTOCMD
6863 aucmd_prepbuf(&aco, buf);
6864#else
6865 save_curbuf = curbuf;
6866 curbuf = buf;
6867#endif
6868
6869 if (*varname == '&')
6870 {
6871 ++varname;
6872 set_option_value(varname, get_var_number(varp),
6873 get_var_string_buf(varp, nbuf), OPT_LOCAL);
6874 }
6875 else
6876 {
6877 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
6878 if (bufvarname != NULL)
6879 {
6880 STRCPY(bufvarname, "b:");
6881 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00006882 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 vim_free(bufvarname);
6884 }
6885 }
6886
6887 /* reset notion of buffer */
6888#ifdef FEAT_AUTOCMD
6889 aucmd_restbuf(&aco);
6890#else
6891 curbuf = save_curbuf;
6892#endif
6893 }
6894 --emsg_off;
6895}
6896
6897/*
6898 * "setcmdpos()" function
6899 */
6900 static void
6901f_setcmdpos(argvars, retvar)
6902 VAR argvars;
6903 VAR retvar;
6904{
6905 retvar->var_val.var_number = set_cmdline_pos(
6906 (int)get_var_number(&argvars[0]) - 1);
6907}
6908
6909/*
6910 * "setline()" function
6911 */
6912 static void
6913f_setline(argvars, retvar)
6914 VAR argvars;
6915 VAR retvar;
6916{
6917 linenr_T lnum;
6918 char_u *line;
6919
6920 lnum = get_var_lnum(argvars);
6921 line = get_var_string(&argvars[1]);
6922 retvar->var_val.var_number = 1; /* FAIL is default */
6923
6924 if (lnum >= 1
6925 && lnum <= curbuf->b_ml.ml_line_count
6926 && u_savesub(lnum) == OK
6927 && ml_replace(lnum, line, TRUE) == OK)
6928 {
6929 changed_bytes(lnum, 0);
6930 check_cursor_col();
6931 retvar->var_val.var_number = 0;
6932 }
6933}
6934
6935/*
6936 * "setreg()" function
6937 */
6938 static void
6939f_setreg(argvars, retvar)
6940 VAR argvars;
6941 VAR retvar;
6942{
6943 int regname;
6944 char_u *strregname;
6945 char_u *stropt;
6946 int append;
6947 char_u yank_type;
6948 long block_len;
6949
6950 block_len = -1;
6951 yank_type = MAUTO;
6952 append = FALSE;
6953
6954 strregname = get_var_string(argvars);
6955 retvar->var_val.var_number = 1; /* FAIL is default */
6956
6957 regname = (strregname == NULL ? '"' : *strregname);
6958 if (regname == 0 || regname == '@')
6959 regname = '"';
6960 else if (regname == '=')
6961 return;
6962
6963 if (argvars[2].var_type != VAR_UNKNOWN)
6964 {
6965 for (stropt = get_var_string(&argvars[2]); *stropt != NUL; ++stropt)
6966 switch (*stropt)
6967 {
6968 case 'a': case 'A': /* append */
6969 append = TRUE;
6970 break;
6971 case 'v': case 'c': /* character-wise selection */
6972 yank_type = MCHAR;
6973 break;
6974 case 'V': case 'l': /* line-wise selection */
6975 yank_type = MLINE;
6976 break;
6977#ifdef FEAT_VISUAL
6978 case 'b': case Ctrl_V: /* block-wise selection */
6979 yank_type = MBLOCK;
6980 if (VIM_ISDIGIT(stropt[1]))
6981 {
6982 ++stropt;
6983 block_len = getdigits(&stropt) - 1;
6984 --stropt;
6985 }
6986 break;
6987#endif
6988 }
6989 }
6990
6991 write_reg_contents_ex(regname, get_var_string(&argvars[1]), -1,
6992 append, yank_type, block_len);
6993 retvar->var_val.var_number = 0;
6994}
6995
6996
6997/*
6998 * "setwinvar(expr)" function
6999 */
7000/*ARGSUSED*/
7001 static void
7002f_setwinvar(argvars, retvar)
7003 VAR argvars;
7004 VAR retvar;
7005{
7006 win_T *win;
7007#ifdef FEAT_WINDOWS
7008 win_T *save_curwin;
7009#endif
7010 char_u *varname, *winvarname;
7011 VAR varp;
7012 char_u nbuf[NUMBUFLEN];
7013
7014 if (check_restricted() || check_secure())
7015 return;
7016 ++emsg_off;
7017 win = find_win_by_nr(&argvars[0]);
7018 varname = get_var_string(&argvars[1]);
7019 varp = &argvars[2];
7020
7021 if (win != NULL && varname != NULL && varp != NULL)
7022 {
7023#ifdef FEAT_WINDOWS
7024 /* set curwin to be our win, temporarily */
7025 save_curwin = curwin;
7026 curwin = win;
7027 curbuf = curwin->w_buffer;
7028#endif
7029
7030 if (*varname == '&')
7031 {
7032 ++varname;
7033 set_option_value(varname, get_var_number(varp),
7034 get_var_string_buf(varp, nbuf), OPT_LOCAL);
7035 }
7036 else
7037 {
7038 winvarname = alloc((unsigned)STRLEN(varname) + 3);
7039 if (winvarname != NULL)
7040 {
7041 STRCPY(winvarname, "w:");
7042 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007043 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 vim_free(winvarname);
7045 }
7046 }
7047
7048#ifdef FEAT_WINDOWS
7049 /* Restore current window, if it's still valid (autocomands can make
7050 * it invalid). */
7051 if (win_valid(save_curwin))
7052 {
7053 curwin = save_curwin;
7054 curbuf = curwin->w_buffer;
7055 }
7056#endif
7057 }
7058 --emsg_off;
7059}
7060
7061/*
7062 * "nextnonblank()" function
7063 */
7064 static void
7065f_nextnonblank(argvars, retvar)
7066 VAR argvars;
7067 VAR retvar;
7068{
7069 linenr_T lnum;
7070
7071 for (lnum = get_var_lnum(argvars); ; ++lnum)
7072 {
7073 if (lnum > curbuf->b_ml.ml_line_count)
7074 {
7075 lnum = 0;
7076 break;
7077 }
7078 if (*skipwhite(ml_get(lnum)) != NUL)
7079 break;
7080 }
7081 retvar->var_val.var_number = lnum;
7082}
7083
7084/*
7085 * "prevnonblank()" function
7086 */
7087 static void
7088f_prevnonblank(argvars, retvar)
7089 VAR argvars;
7090 VAR retvar;
7091{
7092 linenr_T lnum;
7093
7094 lnum = get_var_lnum(argvars);
7095 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7096 lnum = 0;
7097 else
7098 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7099 --lnum;
7100 retvar->var_val.var_number = lnum;
7101}
7102
7103#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
7104static void make_connection __ARGS((void));
7105static int check_connection __ARGS((void));
7106
7107 static void
7108make_connection()
7109{
7110 if (X_DISPLAY == NULL
7111# ifdef FEAT_GUI
7112 && !gui.in_use
7113# endif
7114 )
7115 {
7116 x_force_connect = TRUE;
7117 setup_term_clip();
7118 x_force_connect = FALSE;
7119 }
7120}
7121
7122 static int
7123check_connection()
7124{
7125 make_connection();
7126 if (X_DISPLAY == NULL)
7127 {
7128 EMSG(_("E240: No connection to Vim server"));
7129 return FAIL;
7130 }
7131 return OK;
7132}
7133#endif
7134
7135/*ARGSUSED*/
7136 static void
7137f_serverlist(argvars, retvar)
7138 VAR argvars;
7139 VAR retvar;
7140{
7141 char_u *r = NULL;
7142
7143#ifdef FEAT_CLIENTSERVER
7144# ifdef WIN32
7145 r = serverGetVimNames();
7146# else
7147 make_connection();
7148 if (X_DISPLAY != NULL)
7149 r = serverGetVimNames(X_DISPLAY);
7150# endif
7151#endif
7152 retvar->var_type = VAR_STRING;
7153 retvar->var_val.var_string = r;
7154}
7155
7156/*ARGSUSED*/
7157 static void
7158f_remote_peek(argvars, retvar)
7159 VAR argvars;
7160 VAR retvar;
7161{
7162#ifdef FEAT_CLIENTSERVER
7163 var v;
7164 char_u *s = NULL;
7165# ifdef WIN32
7166 int n = 0;
7167# endif
7168
7169 if (check_restricted() || check_secure())
7170 {
7171 retvar->var_val.var_number = -1;
7172 return;
7173 }
7174# ifdef WIN32
7175 sscanf(get_var_string(&argvars[0]), "%x", &n);
7176 if (n == 0)
7177 retvar->var_val.var_number = -1;
7178 else
7179 {
7180 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
7181 retvar->var_val.var_number = (s != NULL);
7182 }
7183# else
7184 retvar->var_val.var_number = 0;
7185 if (check_connection() == FAIL)
7186 return;
7187
7188 retvar->var_val.var_number = serverPeekReply(X_DISPLAY,
7189 serverStrToWin(get_var_string(&argvars[0])), &s);
7190# endif
7191
7192 if (argvars[1].var_type != VAR_UNKNOWN && retvar->var_val.var_number > 0)
7193 {
7194 v.var_type = VAR_STRING;
7195 v.var_val.var_string = vim_strsave(s);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007196 set_var(get_var_string(&argvars[1]), &v, FALSE);
7197 vim_free(v.var_val.var_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199#else
7200 retvar->var_val.var_number = -1;
7201#endif
7202}
7203
7204/*ARGSUSED*/
7205 static void
7206f_remote_read(argvars, retvar)
7207 VAR argvars;
7208 VAR retvar;
7209{
7210 char_u *r = NULL;
7211
7212#ifdef FEAT_CLIENTSERVER
7213 if (!check_restricted() && !check_secure())
7214 {
7215# ifdef WIN32
7216 /* The server's HWND is encoded in the 'id' parameter */
7217 int n = 0;
7218
7219 sscanf(get_var_string(&argvars[0]), "%x", &n);
7220 if (n != 0)
7221 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
7222 if (r == NULL)
7223# else
7224 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
7225 serverStrToWin(get_var_string(&argvars[0])), &r, FALSE) < 0)
7226# endif
7227 EMSG(_("E277: Unable to read a server reply"));
7228 }
7229#endif
7230 retvar->var_type = VAR_STRING;
7231 retvar->var_val.var_string = r;
7232}
7233
7234/*ARGSUSED*/
7235 static void
7236f_server2client(argvars, retvar)
7237 VAR argvars;
7238 VAR retvar;
7239{
7240#ifdef FEAT_CLIENTSERVER
7241 char_u buf[NUMBUFLEN];
7242 char_u *server = get_var_string(&argvars[0]);
7243 char_u *reply = get_var_string_buf(&argvars[1], buf);
7244
7245 retvar->var_val.var_number = -1;
7246 if (check_restricted() || check_secure())
7247 return;
7248# ifdef FEAT_X11
7249 if (check_connection() == FAIL)
7250 return;
7251# endif
7252
7253 if (serverSendReply(server, reply) < 0)
7254 {
7255 EMSG(_("E258: Unable to send to client"));
7256 return;
7257 }
7258 retvar->var_val.var_number = 0;
7259#else
7260 retvar->var_val.var_number = -1;
7261#endif
7262}
7263
7264#ifdef FEAT_CLIENTSERVER
7265static void remote_common __ARGS((VAR argvars, VAR retvar, int expr));
7266
7267 static void
7268remote_common(argvars, retvar, expr)
7269 VAR argvars;
7270 VAR retvar;
7271 int expr;
7272{
7273 char_u *server_name;
7274 char_u *keys;
7275 char_u *r = NULL;
7276 char_u buf[NUMBUFLEN];
7277# ifdef WIN32
7278 HWND w;
7279# else
7280 Window w;
7281# endif
7282
7283 if (check_restricted() || check_secure())
7284 return;
7285
7286# ifdef FEAT_X11
7287 if (check_connection() == FAIL)
7288 return;
7289# endif
7290
7291 server_name = get_var_string(&argvars[0]);
7292 keys = get_var_string_buf(&argvars[1], buf);
7293# ifdef WIN32
7294 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
7295# else
7296 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
7297 < 0)
7298# endif
7299 {
7300 if (r != NULL)
7301 EMSG(r); /* sending worked but evaluation failed */
7302 else
7303 EMSG2(_("E241: Unable to send to %s"), server_name);
7304 return;
7305 }
7306
7307 retvar->var_val.var_string = r;
7308
7309 if (argvars[2].var_type != VAR_UNKNOWN)
7310 {
7311 var v;
7312 char_u str[30];
7313
7314 sprintf((char *)str, "0x%x", (unsigned int)w);
7315 v.var_type = VAR_STRING;
7316 v.var_val.var_string = vim_strsave(str);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007317 set_var(get_var_string(&argvars[2]), &v, FALSE);
7318 vim_free(v.var_val.var_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320}
7321#endif
7322
7323/*
7324 * "remote_expr()" function
7325 */
7326/*ARGSUSED*/
7327 static void
7328f_remote_expr(argvars, retvar)
7329 VAR argvars;
7330 VAR retvar;
7331{
7332 retvar->var_type = VAR_STRING;
7333 retvar->var_val.var_string = NULL;
7334#ifdef FEAT_CLIENTSERVER
7335 remote_common(argvars, retvar, TRUE);
7336#endif
7337}
7338
7339/*
7340 * "remote_send()" function
7341 */
7342/*ARGSUSED*/
7343 static void
7344f_remote_send(argvars, retvar)
7345 VAR argvars;
7346 VAR retvar;
7347{
7348 retvar->var_type = VAR_STRING;
7349 retvar->var_val.var_string = NULL;
7350#ifdef FEAT_CLIENTSERVER
7351 remote_common(argvars, retvar, FALSE);
7352#endif
7353}
7354
7355/*
7356 * "remote_foreground()" function
7357 */
7358/*ARGSUSED*/
7359 static void
7360f_remote_foreground(argvars, retvar)
7361 VAR argvars;
7362 VAR retvar;
7363{
7364 retvar->var_val.var_number = 0;
7365#ifdef FEAT_CLIENTSERVER
7366# ifdef WIN32
7367 /* On Win32 it's done in this application. */
7368 serverForeground(get_var_string(&argvars[0]));
7369# else
7370 /* Send a foreground() expression to the server. */
7371 argvars[1].var_type = VAR_STRING;
7372 argvars[1].var_val.var_string = vim_strsave((char_u *)"foreground()");
7373 argvars[2].var_type = VAR_UNKNOWN;
7374 remote_common(argvars, retvar, TRUE);
7375 vim_free(argvars[1].var_val.var_string);
7376# endif
7377#endif
7378}
7379
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007380/*
7381 * "repeat()" function
7382 */
7383/*ARGSUSED*/
7384 static void
7385f_repeat(argvars, retvar)
7386 VAR argvars;
7387 VAR retvar;
7388{
7389 char_u *p;
7390 int n;
7391 int slen;
7392 int len;
7393 char_u *r;
7394 int i;
7395
7396 p = get_var_string(&argvars[0]);
7397 n = get_var_number(&argvars[1]);
7398
7399 retvar->var_type = VAR_STRING;
7400 retvar->var_val.var_string = NULL;
7401
7402 slen = (int)STRLEN(p);
7403 len = slen * n;
7404
7405 if (len <= 0)
7406 return;
7407
7408 r = alloc(len + 1);
7409 if (r != NULL)
7410 {
7411 for (i = 0; i < n; i++)
7412 mch_memmove(r + i * slen, p, (size_t)slen);
7413 r[len] = NUL;
7414 }
7415
7416 retvar->var_val.var_string = r;
7417}
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#ifdef HAVE_STRFTIME
7420/*
7421 * "strftime({format}[, {time}])" function
7422 */
7423 static void
7424f_strftime(argvars, retvar)
7425 VAR argvars;
7426 VAR retvar;
7427{
7428 char_u result_buf[256];
7429 struct tm *curtime;
7430 time_t seconds;
7431 char_u *p;
7432
7433 retvar->var_type = VAR_STRING;
7434
7435 p = get_var_string(&argvars[0]);
7436 if (argvars[1].var_type == VAR_UNKNOWN)
7437 seconds = time(NULL);
7438 else
7439 seconds = (time_t)get_var_number(&argvars[1]);
7440 curtime = localtime(&seconds);
7441 /* MSVC returns NULL for an invalid value of seconds. */
7442 if (curtime == NULL)
7443 retvar->var_val.var_string = vim_strsave((char_u *)_("(Invalid)"));
7444 else
7445 {
7446# ifdef FEAT_MBYTE
7447 vimconv_T conv;
7448 char_u *enc;
7449
7450 conv.vc_type = CONV_NONE;
7451 enc = enc_locale();
7452 convert_setup(&conv, p_enc, enc);
7453 if (conv.vc_type != CONV_NONE)
7454 p = string_convert(&conv, p, NULL);
7455# endif
7456 if (p != NULL)
7457 (void)strftime((char *)result_buf, sizeof(result_buf),
7458 (char *)p, curtime);
7459 else
7460 result_buf[0] = NUL;
7461
7462# ifdef FEAT_MBYTE
7463 if (conv.vc_type != CONV_NONE)
7464 vim_free(p);
7465 convert_setup(&conv, enc, p_enc);
7466 if (conv.vc_type != CONV_NONE)
7467 retvar->var_val.var_string =
7468 string_convert(&conv, result_buf, NULL);
7469 else
7470# endif
7471 retvar->var_val.var_string = vim_strsave(result_buf);
7472
7473# ifdef FEAT_MBYTE
7474 /* Release conversion descriptors */
7475 convert_setup(&conv, NULL, NULL);
7476 vim_free(enc);
7477# endif
7478 }
7479}
7480#endif
7481
7482/*
7483 * "stridx()" function
7484 */
7485 static void
7486f_stridx(argvars, retvar)
7487 VAR argvars;
7488 VAR retvar;
7489{
7490 char_u buf[NUMBUFLEN];
7491 char_u *needle;
7492 char_u *haystack;
7493 char_u *pos;
7494
7495 needle = get_var_string(&argvars[1]);
7496 haystack = get_var_string_buf(&argvars[0], buf);
7497 pos = (char_u *)strstr((char *)haystack, (char *)needle);
7498
7499 if (pos == NULL)
7500 retvar->var_val.var_number = -1;
7501 else
7502 retvar->var_val.var_number = (varnumber_T) (pos - haystack);
7503}
7504
7505/*
7506 * "strridx()" function
7507 */
7508 static void
7509f_strridx(argvars, retvar)
7510 VAR argvars;
7511 VAR retvar;
7512{
7513 char_u buf[NUMBUFLEN];
7514 char_u *needle;
7515 char_u *haystack;
7516 char_u *rest;
7517 char_u *lastmatch = NULL;
7518
7519 needle = get_var_string(&argvars[1]);
7520 haystack = get_var_string_buf(&argvars[0], buf);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007521 if (*needle == NUL)
7522 /* Empty string matches past the end. */
7523 lastmatch = haystack + STRLEN(haystack);
7524 else
7525 for (rest = haystack; *rest != '\0'; ++rest)
7526 {
7527 rest = (char_u *)strstr((char *)rest, (char *)needle);
7528 if (rest == NULL)
7529 break;
7530 lastmatch = rest;
7531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 if (lastmatch == NULL)
7534 retvar->var_val.var_number = -1;
7535 else
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007536 retvar->var_val.var_number = (varnumber_T)(lastmatch - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537}
7538
7539/*
7540 * "strlen()" function
7541 */
7542 static void
7543f_strlen(argvars, retvar)
7544 VAR argvars;
7545 VAR retvar;
7546{
7547 retvar->var_val.var_number = (varnumber_T) (STRLEN(get_var_string(&argvars[0])));
7548}
7549
7550/*
7551 * "strpart()" function
7552 */
7553 static void
7554f_strpart(argvars, retvar)
7555 VAR argvars;
7556 VAR retvar;
7557{
7558 char_u *p;
7559 int n;
7560 int len;
7561 int slen;
7562
7563 p = get_var_string(&argvars[0]);
7564 slen = (int)STRLEN(p);
7565
7566 n = get_var_number(&argvars[1]);
7567 if (argvars[2].var_type != VAR_UNKNOWN)
7568 len = get_var_number(&argvars[2]);
7569 else
7570 len = slen - n; /* default len: all bytes that are available. */
7571
7572 /*
7573 * Only return the overlap between the specified part and the actual
7574 * string.
7575 */
7576 if (n < 0)
7577 {
7578 len += n;
7579 n = 0;
7580 }
7581 else if (n > slen)
7582 n = slen;
7583 if (len < 0)
7584 len = 0;
7585 else if (n + len > slen)
7586 len = slen - n;
7587
7588 retvar->var_type = VAR_STRING;
7589 retvar->var_val.var_string = vim_strnsave(p + n, len);
7590}
7591
7592/*
7593 * "strtrans()" function
7594 */
7595 static void
7596f_strtrans(argvars, retvar)
7597 VAR argvars;
7598 VAR retvar;
7599{
7600 retvar->var_type = VAR_STRING;
7601 retvar->var_val.var_string = transstr(get_var_string(&argvars[0]));
7602}
7603
7604/*
7605 * "synID(line, col, trans)" function
7606 */
7607/*ARGSUSED*/
7608 static void
7609f_synID(argvars, retvar)
7610 VAR argvars;
7611 VAR retvar;
7612{
7613 int id = 0;
7614#ifdef FEAT_SYN_HL
7615 long line;
7616 long col;
7617 int trans;
7618
7619 line = get_var_lnum(argvars);
7620 col = get_var_number(&argvars[1]) - 1;
7621 trans = get_var_number(&argvars[2]);
7622
7623 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
7624 && col >= 0 && col < (long)STRLEN(ml_get(line)))
7625 id = syn_get_id(line, col, trans);
7626#endif
7627
7628 retvar->var_val.var_number = id;
7629}
7630
7631/*
7632 * "synIDattr(id, what [, mode])" function
7633 */
7634/*ARGSUSED*/
7635 static void
7636f_synIDattr(argvars, retvar)
7637 VAR argvars;
7638 VAR retvar;
7639{
7640 char_u *p = NULL;
7641#ifdef FEAT_SYN_HL
7642 int id;
7643 char_u *what;
7644 char_u *mode;
7645 char_u modebuf[NUMBUFLEN];
7646 int modec;
7647
7648 id = get_var_number(&argvars[0]);
7649 what = get_var_string(&argvars[1]);
7650 if (argvars[2].var_type != VAR_UNKNOWN)
7651 {
7652 mode = get_var_string_buf(&argvars[2], modebuf);
7653 modec = TOLOWER_ASC(mode[0]);
7654 if (modec != 't' && modec != 'c'
7655#ifdef FEAT_GUI
7656 && modec != 'g'
7657#endif
7658 )
7659 modec = 0; /* replace invalid with current */
7660 }
7661 else
7662 {
7663#ifdef FEAT_GUI
7664 if (gui.in_use)
7665 modec = 'g';
7666 else
7667#endif
7668 if (t_colors > 1)
7669 modec = 'c';
7670 else
7671 modec = 't';
7672 }
7673
7674
7675 switch (TOLOWER_ASC(what[0]))
7676 {
7677 case 'b':
7678 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
7679 p = highlight_color(id, what, modec);
7680 else /* bold */
7681 p = highlight_has_attr(id, HL_BOLD, modec);
7682 break;
7683
7684 case 'f': /* fg[#] */
7685 p = highlight_color(id, what, modec);
7686 break;
7687
7688 case 'i':
7689 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
7690 p = highlight_has_attr(id, HL_INVERSE, modec);
7691 else /* italic */
7692 p = highlight_has_attr(id, HL_ITALIC, modec);
7693 break;
7694
7695 case 'n': /* name */
7696 p = get_highlight_name(NULL, id - 1);
7697 break;
7698
7699 case 'r': /* reverse */
7700 p = highlight_has_attr(id, HL_INVERSE, modec);
7701 break;
7702
7703 case 's': /* standout */
7704 p = highlight_has_attr(id, HL_STANDOUT, modec);
7705 break;
7706
7707 case 'u': /* underline */
7708 p = highlight_has_attr(id, HL_UNDERLINE, modec);
7709 break;
7710 }
7711
7712 if (p != NULL)
7713 p = vim_strsave(p);
7714#endif
7715 retvar->var_type = VAR_STRING;
7716 retvar->var_val.var_string = p;
7717}
7718
7719/*
7720 * "synIDtrans(id)" function
7721 */
7722/*ARGSUSED*/
7723 static void
7724f_synIDtrans(argvars, retvar)
7725 VAR argvars;
7726 VAR retvar;
7727{
7728 int id;
7729
7730#ifdef FEAT_SYN_HL
7731 id = get_var_number(&argvars[0]);
7732
7733 if (id > 0)
7734 id = syn_get_final_id(id);
7735 else
7736#endif
7737 id = 0;
7738
7739 retvar->var_val.var_number = id;
7740}
7741
7742/*
7743 * "system()" function
7744 */
7745 static void
7746f_system(argvars, retvar)
7747 VAR argvars;
7748 VAR retvar;
7749{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007750 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007752 char_u *infile = NULL;
7753 char_u buf[NUMBUFLEN];
7754 int err = FALSE;
7755 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007757 if (argvars[1].var_type != VAR_UNKNOWN)
7758 {
7759 /*
7760 * Write the string to a temp file, to be used for input of the shell
7761 * command.
7762 */
7763 if ((infile = vim_tempname('i')) == NULL)
7764 {
7765 EMSG(_(e_notmp));
7766 return;
7767 }
7768
7769 fd = mch_fopen((char *)infile, WRITEBIN);
7770 if (fd == NULL)
7771 {
7772 EMSG2(_(e_notopen), infile);
7773 goto done;
7774 }
7775 p = get_var_string_buf(&argvars[1], buf);
7776 if (fwrite(p, STRLEN(p), 1, fd) != 1)
7777 err = TRUE;
7778 if (fclose(fd) != 0)
7779 err = TRUE;
7780 if (err)
7781 {
7782 EMSG(_("E677: Error writing temp file"));
7783 goto done;
7784 }
7785 }
7786
7787 res = get_cmd_output(get_var_string(&argvars[0]), infile, SHELL_SILENT);
7788
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789#ifdef USE_CR
7790 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007791 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {
7793 char_u *s;
7794
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007795 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {
7797 if (*s == CAR)
7798 *s = NL;
7799 }
7800 }
7801#else
7802# ifdef USE_CRNL
7803 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007804 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
7806 char_u *s, *d;
7807
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007808 d = res;
7809 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 {
7811 if (s[0] == CAR && s[1] == NL)
7812 ++s;
7813 *d++ = *s;
7814 }
7815 *d = NUL;
7816 }
7817# endif
7818#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007819
7820done:
7821 if (infile != NULL)
7822 {
7823 mch_remove(infile);
7824 vim_free(infile);
7825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 retvar->var_type = VAR_STRING;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007827 retvar->var_val.var_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828}
7829
7830/*
7831 * "submatch()" function
7832 */
7833 static void
7834f_submatch(argvars, retvar)
7835 VAR argvars;
7836 VAR retvar;
7837{
7838 retvar->var_type = VAR_STRING;
7839 retvar->var_val.var_string = reg_submatch((int)get_var_number(&argvars[0]));
7840}
7841
7842/*
7843 * "substitute()" function
7844 */
7845 static void
7846f_substitute(argvars, retvar)
7847 VAR argvars;
7848 VAR retvar;
7849{
7850 char_u patbuf[NUMBUFLEN];
7851 char_u subbuf[NUMBUFLEN];
7852 char_u flagsbuf[NUMBUFLEN];
7853
7854 retvar->var_type = VAR_STRING;
7855 retvar->var_val.var_string = do_string_sub(
7856 get_var_string(&argvars[0]),
7857 get_var_string_buf(&argvars[1], patbuf),
7858 get_var_string_buf(&argvars[2], subbuf),
7859 get_var_string_buf(&argvars[3], flagsbuf));
7860}
7861
7862/*
7863 * "tempname()" function
7864 */
7865/*ARGSUSED*/
7866 static void
7867f_tempname(argvars, retvar)
7868 VAR argvars;
7869 VAR retvar;
7870{
7871 static int x = 'A';
7872
7873 retvar->var_type = VAR_STRING;
7874 retvar->var_val.var_string = vim_tempname(x);
7875
7876 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
7877 * names. Skip 'I' and 'O', they are used for shell redirection. */
7878 do
7879 {
7880 if (x == 'Z')
7881 x = '0';
7882 else if (x == '9')
7883 x = 'A';
7884 else
7885 {
7886#ifdef EBCDIC
7887 if (x == 'I')
7888 x = 'J';
7889 else if (x == 'R')
7890 x = 'S';
7891 else
7892#endif
7893 ++x;
7894 }
7895 } while (x == 'I' || x == 'O');
7896}
7897
7898/*
7899 * "tolower(string)" function
7900 */
7901 static void
7902f_tolower(argvars, retvar)
7903 VAR argvars;
7904 VAR retvar;
7905{
7906 char_u *p;
7907
7908 p = vim_strsave(get_var_string(&argvars[0]));
7909 retvar->var_type = VAR_STRING;
7910 retvar->var_val.var_string = p;
7911
7912 if (p != NULL)
7913 while (*p != NUL)
7914 {
7915#ifdef FEAT_MBYTE
7916 int l;
7917
7918 if (enc_utf8)
7919 {
7920 int c, lc;
7921
7922 c = utf_ptr2char(p);
7923 lc = utf_tolower(c);
7924 l = utf_ptr2len_check(p);
7925 /* TODO: reallocate string when byte count changes. */
7926 if (utf_char2len(lc) == l)
7927 utf_char2bytes(lc, p);
7928 p += l;
7929 }
7930 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7931 p += l; /* skip multi-byte character */
7932 else
7933#endif
7934 {
7935 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
7936 ++p;
7937 }
7938 }
7939}
7940
7941/*
7942 * "toupper(string)" function
7943 */
7944 static void
7945f_toupper(argvars, retvar)
7946 VAR argvars;
7947 VAR retvar;
7948{
7949 char_u *p;
7950
7951 p = vim_strsave(get_var_string(&argvars[0]));
7952 retvar->var_type = VAR_STRING;
7953 retvar->var_val.var_string = p;
7954
7955 if (p != NULL)
7956 while (*p != NUL)
7957 {
7958#ifdef FEAT_MBYTE
7959 int l;
7960
7961 if (enc_utf8)
7962 {
7963 int c, uc;
7964
7965 c = utf_ptr2char(p);
7966 uc = utf_toupper(c);
7967 l = utf_ptr2len_check(p);
7968 /* TODO: reallocate string when byte count changes. */
7969 if (utf_char2len(uc) == l)
7970 utf_char2bytes(uc, p);
7971 p += l;
7972 }
7973 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7974 p += l; /* skip multi-byte character */
7975 else
7976#endif
7977 {
7978 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
7979 p++;
7980 }
7981 }
7982}
7983
7984/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00007985 * "tr(string, fromstr, tostr)" function
7986 */
7987 static void
7988f_tr(argvars, retvar)
7989 VAR argvars;
7990 VAR retvar;
7991{
7992 char_u *instr;
7993 char_u *fromstr;
7994 char_u *tostr;
7995 char_u *p;
7996#ifdef FEAT_MBYTE
7997 int inlen;
7998 int fromlen;
7999 int tolen;
8000 int idx;
8001 char_u *cpstr;
8002 int cplen;
8003 int first = TRUE;
8004#endif
8005 char_u buf[NUMBUFLEN];
8006 char_u buf2[NUMBUFLEN];
8007 garray_T ga;
8008
8009 instr = get_var_string(&argvars[0]);
8010 fromstr = get_var_string_buf(&argvars[1], buf);
8011 tostr = get_var_string_buf(&argvars[2], buf2);
8012
8013 /* Default return value: empty string. */
8014 retvar->var_type = VAR_STRING;
8015 retvar->var_val.var_string = NULL;
8016 ga_init2(&ga, (int)sizeof(char), 80);
8017
8018#ifdef FEAT_MBYTE
8019 if (!has_mbyte)
8020#endif
8021 /* not multi-byte: fromstr and tostr must be the same length */
8022 if (STRLEN(fromstr) != STRLEN(tostr))
8023 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008024#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +00008025error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008026#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +00008027 EMSG2(_(e_invarg2), fromstr);
8028 ga_clear(&ga);
8029 return;
8030 }
8031
8032 /* fromstr and tostr have to contain the same number of chars */
8033 while (*instr != NUL)
8034 {
8035#ifdef FEAT_MBYTE
8036 if (has_mbyte)
8037 {
8038 inlen = mb_ptr2len_check(instr);
8039 cpstr = instr;
8040 cplen = inlen;
8041 idx = 0;
8042 for (p = fromstr; *p != NUL; p += fromlen)
8043 {
8044 fromlen = mb_ptr2len_check(p);
8045 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
8046 {
8047 for (p = tostr; *p != NUL; p += tolen)
8048 {
8049 tolen = mb_ptr2len_check(p);
8050 if (idx-- == 0)
8051 {
8052 cplen = tolen;
8053 cpstr = p;
8054 break;
8055 }
8056 }
8057 if (*p == NUL) /* tostr is shorter than fromstr */
8058 goto error;
8059 break;
8060 }
8061 ++idx;
8062 }
8063
8064 if (first && cpstr == instr)
8065 {
8066 /* Check that fromstr and tostr have the same number of
8067 * (multi-byte) characters. Done only once when a character
8068 * of instr doesn't appear in fromstr. */
8069 first = FALSE;
8070 for (p = tostr; *p != NUL; p += tolen)
8071 {
8072 tolen = mb_ptr2len_check(p);
8073 --idx;
8074 }
8075 if (idx != 0)
8076 goto error;
8077 }
8078
8079 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00008080 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +00008081 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008082
8083 instr += inlen;
8084 }
8085 else
8086#endif
8087 {
8088 /* When not using multi-byte chars we can do it faster. */
8089 p = vim_strchr(fromstr, *instr);
8090 if (p != NULL)
8091 ga_append(&ga, tostr[p - fromstr]);
8092 else
8093 ga_append(&ga, *instr);
8094 ++instr;
8095 }
8096 }
8097
8098 retvar->var_val.var_string = ga.ga_data;
8099}
8100
8101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 * "type(expr)" function
8103 */
8104 static void
8105f_type(argvars, retvar)
8106 VAR argvars;
8107 VAR retvar;
8108{
8109 if (argvars[0].var_type == VAR_NUMBER)
8110 retvar->var_val.var_number = 0;
8111 else
8112 retvar->var_val.var_number = 1;
8113}
8114
8115/*
8116 * "virtcol(string)" function
8117 */
8118 static void
8119f_virtcol(argvars, retvar)
8120 VAR argvars;
8121 VAR retvar;
8122{
8123 colnr_T vcol = 0;
8124 pos_T *fp;
8125
8126 fp = var2fpos(&argvars[0], FALSE);
8127 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
8128 {
8129 getvvcol(curwin, fp, NULL, NULL, &vcol);
8130 ++vcol;
8131 }
8132
8133 retvar->var_val.var_number = vcol;
8134}
8135
8136/*
8137 * "visualmode()" function
8138 */
8139/*ARGSUSED*/
8140 static void
8141f_visualmode(argvars, retvar)
8142 VAR argvars;
8143 VAR retvar;
8144{
8145#ifdef FEAT_VISUAL
8146 char_u str[2];
8147
8148 retvar->var_type = VAR_STRING;
8149 str[0] = curbuf->b_visual_mode_eval;
8150 str[1] = NUL;
8151 retvar->var_val.var_string = vim_strsave(str);
8152
8153 /* A non-zero number or non-empty string argument: reset mode. */
8154 if ((argvars[0].var_type == VAR_NUMBER
8155 && argvars[0].var_val.var_number != 0)
8156 || (argvars[0].var_type == VAR_STRING
8157 && *get_var_string(&argvars[0]) != NUL))
8158 curbuf->b_visual_mode_eval = NUL;
8159#else
8160 retvar->var_val.var_number = 0; /* return anything, it won't work anyway */
8161#endif
8162}
8163
8164/*
8165 * "winbufnr(nr)" function
8166 */
8167 static void
8168f_winbufnr(argvars, retvar)
8169 VAR argvars;
8170 VAR retvar;
8171{
8172 win_T *wp;
8173
8174 wp = find_win_by_nr(&argvars[0]);
8175 if (wp == NULL)
8176 retvar->var_val.var_number = -1;
8177 else
8178 retvar->var_val.var_number = wp->w_buffer->b_fnum;
8179}
8180
8181/*
8182 * "wincol()" function
8183 */
8184/*ARGSUSED*/
8185 static void
8186f_wincol(argvars, retvar)
8187 VAR argvars;
8188 VAR retvar;
8189{
8190 validate_cursor();
8191 retvar->var_val.var_number = curwin->w_wcol + 1;
8192}
8193
8194/*
8195 * "winheight(nr)" function
8196 */
8197 static void
8198f_winheight(argvars, retvar)
8199 VAR argvars;
8200 VAR retvar;
8201{
8202 win_T *wp;
8203
8204 wp = find_win_by_nr(&argvars[0]);
8205 if (wp == NULL)
8206 retvar->var_val.var_number = -1;
8207 else
8208 retvar->var_val.var_number = wp->w_height;
8209}
8210
8211/*
8212 * "winline()" function
8213 */
8214/*ARGSUSED*/
8215 static void
8216f_winline(argvars, retvar)
8217 VAR argvars;
8218 VAR retvar;
8219{
8220 validate_cursor();
8221 retvar->var_val.var_number = curwin->w_wrow + 1;
8222}
8223
8224/*
8225 * "winnr()" function
8226 */
8227/* ARGSUSED */
8228 static void
8229f_winnr(argvars, retvar)
8230 VAR argvars;
8231 VAR retvar;
8232{
8233 int nr = 1;
8234#ifdef FEAT_WINDOWS
8235 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008236 win_T *twin = curwin;
8237 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008239 if (argvars[0].var_type != VAR_UNKNOWN)
8240 {
8241 arg = get_var_string(&argvars[0]);
8242 if (STRCMP(arg, "$") == 0)
8243 twin = lastwin;
8244 else if (STRCMP(arg, "#") == 0)
8245 {
8246 twin = prevwin;
8247 if (prevwin == NULL)
8248 nr = 0;
8249 }
8250 else
8251 {
8252 EMSG2(_(e_invexpr2), arg);
8253 nr = 0;
8254 }
8255 }
8256
8257 if (nr > 0)
8258 for (wp = firstwin; wp != twin; wp = wp->w_next)
8259 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260#endif
8261 retvar->var_val.var_number = nr;
8262}
8263
8264/*
8265 * "winrestcmd()" function
8266 */
8267/* ARGSUSED */
8268 static void
8269f_winrestcmd(argvars, retvar)
8270 VAR argvars;
8271 VAR retvar;
8272{
8273#ifdef FEAT_WINDOWS
8274 win_T *wp;
8275 int winnr = 1;
8276 garray_T ga;
8277 char_u buf[50];
8278
8279 ga_init2(&ga, (int)sizeof(char), 70);
8280 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8281 {
8282 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
8283 ga_concat(&ga, buf);
8284# ifdef FEAT_VERTSPLIT
8285 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
8286 ga_concat(&ga, buf);
8287# endif
8288 ++winnr;
8289 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00008290 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291
8292 retvar->var_val.var_string = ga.ga_data;
8293#else
8294 retvar->var_val.var_string = NULL;
8295#endif
8296 retvar->var_type = VAR_STRING;
8297}
8298
8299/*
8300 * "winwidth(nr)" function
8301 */
8302 static void
8303f_winwidth(argvars, retvar)
8304 VAR argvars;
8305 VAR retvar;
8306{
8307 win_T *wp;
8308
8309 wp = find_win_by_nr(&argvars[0]);
8310 if (wp == NULL)
8311 retvar->var_val.var_number = -1;
8312 else
8313#ifdef FEAT_VERTSPLIT
8314 retvar->var_val.var_number = wp->w_width;
8315#else
8316 retvar->var_val.var_number = Columns;
8317#endif
8318}
8319
8320 static win_T *
8321find_win_by_nr(vp)
8322 VAR vp;
8323{
8324#ifdef FEAT_WINDOWS
8325 win_T *wp;
8326#endif
8327 int nr;
8328
8329 nr = get_var_number(vp);
8330
8331#ifdef FEAT_WINDOWS
8332 if (nr == 0)
8333 return curwin;
8334
8335 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8336 if (--nr <= 0)
8337 break;
8338 return wp;
8339#else
8340 if (nr == 0 || nr == 1)
8341 return curwin;
8342 return NULL;
8343#endif
8344}
8345
8346/*
8347 * Translate a String variable into a position.
8348 */
8349 static pos_T *
8350var2fpos(varp, lnum)
8351 VAR varp;
8352 int lnum; /* TRUE when $ is last line */
8353{
8354 char_u *name;
8355 static pos_T pos;
8356 pos_T *pp;
8357
8358 name = get_var_string(varp);
8359 if (name[0] == '.') /* cursor */
8360 return &curwin->w_cursor;
8361 if (name[0] == '\'') /* mark */
8362 {
8363 pp = getmark(name[1], FALSE);
8364 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
8365 return NULL;
8366 return pp;
8367 }
8368 if (name[0] == '$') /* last column or line */
8369 {
8370 if (lnum)
8371 {
8372 pos.lnum = curbuf->b_ml.ml_line_count;
8373 pos.col = 0;
8374 }
8375 else
8376 {
8377 pos.lnum = curwin->w_cursor.lnum;
8378 pos.col = (colnr_T)STRLEN(ml_get_curline());
8379 }
8380 return &pos;
8381 }
8382 return NULL;
8383}
8384
8385/*
8386 * Get the length of an environment variable name.
8387 * Advance "arg" to the first character after the name.
8388 * Return 0 for error.
8389 */
8390 static int
8391get_env_len(arg)
8392 char_u **arg;
8393{
8394 char_u *p;
8395 int len;
8396
8397 for (p = *arg; vim_isIDc(*p); ++p)
8398 ;
8399 if (p == *arg) /* no name found */
8400 return 0;
8401
8402 len = (int)(p - *arg);
8403 *arg = p;
8404 return len;
8405}
8406
8407/*
8408 * Get the length of the name of a function or internal variable.
8409 * "arg" is advanced to the first non-white character after the name.
8410 * Return 0 if something is wrong.
8411 */
8412 static int
8413get_id_len(arg)
8414 char_u **arg;
8415{
8416 char_u *p;
8417 int len;
8418
8419 /* Find the end of the name. */
8420 for (p = *arg; eval_isnamec(*p); ++p)
8421 ;
8422 if (p == *arg) /* no name found */
8423 return 0;
8424
8425 len = (int)(p - *arg);
8426 *arg = skipwhite(p);
8427
8428 return len;
8429}
8430
8431/*
8432 * Get the length of the name of a function.
8433 * "arg" is advanced to the first non-white character after the name.
8434 * Return 0 if something is wrong.
8435 * If the name contains 'magic' {}'s, expand them and return the
8436 * expanded name in an allocated string via 'alias' - caller must free.
8437 */
8438 static int
8439get_func_len(arg, alias, evaluate)
8440 char_u **arg;
8441 char_u **alias;
8442 int evaluate;
8443{
8444 int len;
8445#ifdef FEAT_MAGIC_BRACES
8446 char_u *p;
8447 char_u *expr_start;
8448 char_u *expr_end;
8449#endif
8450
8451 *alias = NULL; /* default to no alias */
8452
8453 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
8454 && (*arg)[2] == (int)KE_SNR)
8455 {
8456 /* hard coded <SNR>, already translated */
8457 *arg += 3;
8458 return get_id_len(arg) + 3;
8459 }
8460 len = eval_fname_script(*arg);
8461 if (len > 0)
8462 {
8463 /* literal "<SID>", "s:" or "<SNR>" */
8464 *arg += len;
8465 }
8466
8467#ifdef FEAT_MAGIC_BRACES
8468 /*
8469 * Find the end of the name;
8470 */
8471 p = find_name_end(*arg, &expr_start, &expr_end);
8472 /* check for {} construction */
8473 if (expr_start != NULL)
8474 {
8475 char_u *temp_string;
8476
8477 if (!evaluate)
8478 {
8479 len += (int)(p - *arg);
8480 *arg = skipwhite(p);
8481 return len;
8482 }
8483
8484 /*
8485 * Include any <SID> etc in the expanded string:
8486 * Thus the -len here.
8487 */
8488 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
8489 if (temp_string == NULL)
8490 return 0;
8491 *alias = temp_string;
8492 *arg = skipwhite(p);
8493 return (int)STRLEN(temp_string);
8494 }
8495#endif
8496
8497 len += get_id_len(arg);
8498 if (len == 0)
8499 EMSG2(_(e_invexpr2), *arg);
8500
8501 return len;
8502}
8503
8504 static char_u *
8505find_name_end(arg, expr_start, expr_end)
8506 char_u *arg;
8507 char_u **expr_start;
8508 char_u **expr_end;
8509{
8510 int nesting = 0;
8511 char_u *p;
8512
8513 *expr_start = NULL;
8514 *expr_end = NULL;
8515
8516 for (p = arg; (*p != NUL && (eval_isnamec(*p) || nesting != 0)); ++p)
8517 {
8518#ifdef FEAT_MAGIC_BRACES
8519 if (*p == '{')
8520 {
8521 nesting++;
8522 if (*expr_start == NULL)
8523 *expr_start = p;
8524 }
8525 else if (*p == '}')
8526 {
8527 nesting--;
8528 if (nesting == 0 && *expr_end == NULL)
8529 *expr_end = p;
8530 }
8531#endif
8532 }
8533
8534 return p;
8535}
8536
8537/*
8538 * Return TRUE if character "c" can be used in a variable or function name.
8539 */
8540 static int
8541eval_isnamec(c)
8542 int c;
8543{
8544 return (ASCII_ISALNUM(c) || c == '_' || c == ':'
8545#ifdef FEAT_MAGIC_BRACES
8546 || c == '{' || c == '}'
8547#endif
8548 );
8549}
8550
8551/*
8552 * Find a v: variable.
8553 * Return it's index, or -1 if not found.
8554 */
8555 static int
8556find_vim_var(name, len)
8557 char_u *name;
8558 int len; /* length of "name" */
8559{
8560 char_u *vname;
8561 int vlen;
8562 int i;
8563
8564 /*
8565 * Ignore "v:" for old built-in variables, require it for new ones.
8566 */
8567 if (name[0] == 'v' && name[1] == ':')
8568 {
8569 vname = name + 2;
8570 vlen = len - 2;
8571 }
8572 else
8573 {
8574 vname = name;
8575 vlen = len;
8576 }
8577 for (i = 0; i < VV_LEN; ++i)
8578 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
8579 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
8580 return i;
8581 return -1;
8582}
8583
8584/*
8585 * Set number v: variable to "val".
8586 */
8587 void
8588set_vim_var_nr(idx, val)
8589 int idx;
8590 long val;
8591{
8592 vimvars[idx].val = (char_u *)val;
8593}
8594
8595/*
8596 * Get number v: variable value;
8597 */
8598 long
8599get_vim_var_nr(idx)
8600 int idx;
8601{
8602 return (long)vimvars[idx].val;
8603}
8604
8605/*
8606 * Set v:count, v:count1 and v:prevcount.
8607 */
8608 void
8609set_vcount(count, count1)
8610 long count;
8611 long count1;
8612{
8613 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
8614 vimvars[VV_COUNT].val = (char_u *)count;
8615 vimvars[VV_COUNT1].val = (char_u *)count1;
8616}
8617
8618/*
8619 * Set string v: variable to a copy of "val".
8620 */
8621 void
8622set_vim_var_string(idx, val, len)
8623 int idx;
8624 char_u *val;
8625 int len; /* length of "val" to use or -1 (whole string) */
8626{
8627 vim_free(vimvars[idx].val);
8628 if (val == NULL)
8629 vimvars[idx].val = NULL;
8630 else if (len == -1)
8631 vimvars[idx].val = vim_strsave(val);
8632 else
8633 vimvars[idx].val = vim_strnsave(val, len);
8634}
8635
8636/*
8637 * Set v:register if needed.
8638 */
8639 void
8640set_reg_var(c)
8641 int c;
8642{
8643 char_u regname;
8644
8645 if (c == 0 || c == ' ')
8646 regname = '"';
8647 else
8648 regname = c;
8649 /* Avoid free/alloc when the value is already right. */
8650 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
8651 set_vim_var_string(VV_REG, &regname, 1);
8652}
8653
8654/*
8655 * Get or set v:exception. If "oldval" == NULL, return the current value.
8656 * Otherwise, restore the value to "oldval" and return NULL.
8657 * Must always be called in pairs to save and restore v:exception! Does not
8658 * take care of memory allocations.
8659 */
8660 char_u *
8661v_exception(oldval)
8662 char_u *oldval;
8663{
8664 if (oldval == NULL)
8665 return vimvars[VV_EXCEPTION].val;
8666
8667 vimvars[VV_EXCEPTION].val = oldval;
8668 return NULL;
8669}
8670
8671/*
8672 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
8673 * Otherwise, restore the value to "oldval" and return NULL.
8674 * Must always be called in pairs to save and restore v:throwpoint! Does not
8675 * take care of memory allocations.
8676 */
8677 char_u *
8678v_throwpoint(oldval)
8679 char_u *oldval;
8680{
8681 if (oldval == NULL)
8682 return vimvars[VV_THROWPOINT].val;
8683
8684 vimvars[VV_THROWPOINT].val = oldval;
8685 return NULL;
8686}
8687
8688#if defined(FEAT_AUTOCMD) || defined(PROTO)
8689/*
8690 * Set v:cmdarg.
8691 * If "eap" != NULL, use "eap" to generate the value and return the old value.
8692 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
8693 * Must always be called in pairs!
8694 */
8695 char_u *
8696set_cmdarg(eap, oldarg)
8697 exarg_T *eap;
8698 char_u *oldarg;
8699{
8700 char_u *oldval;
8701 char_u *newval;
8702 unsigned len;
8703
8704 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008705 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008707 vim_free(oldval);
8708 vimvars[VV_CMDARG].val = oldarg;
8709 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 }
8711
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008712 if (eap->force_bin == FORCE_BIN)
8713 len = 6;
8714 else if (eap->force_bin == FORCE_NOBIN)
8715 len = 8;
8716 else
8717 len = 0;
8718 if (eap->force_ff != 0)
8719 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
8720# ifdef FEAT_MBYTE
8721 if (eap->force_enc != 0)
8722 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
8723# endif
8724
8725 newval = alloc(len + 1);
8726 if (newval == NULL)
8727 return NULL;
8728
8729 if (eap->force_bin == FORCE_BIN)
8730 sprintf((char *)newval, " ++bin");
8731 else if (eap->force_bin == FORCE_NOBIN)
8732 sprintf((char *)newval, " ++nobin");
8733 else
8734 *newval = NUL;
8735 if (eap->force_ff != 0)
8736 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
8737 eap->cmd + eap->force_ff);
8738# ifdef FEAT_MBYTE
8739 if (eap->force_enc != 0)
8740 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
8741 eap->cmd + eap->force_enc);
8742# endif
8743 vimvars[VV_CMDARG].val = newval;
8744 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745}
8746#endif
8747
8748/*
8749 * Get the value of internal variable "name".
8750 * Return OK or FAIL.
8751 */
8752 static int
8753get_var_var(name, len, retvar)
8754 char_u *name;
8755 int len; /* length of "name" */
8756 VAR retvar; /* NULL when only checking existence */
8757{
8758 int ret = OK;
8759 int type = VAR_UNKNOWN;
8760 long number = 1;
8761 char_u *string = NULL;
8762 VAR v;
8763 int cc;
8764 int i;
8765
8766 /* truncate the name, so that we can use strcmp() */
8767 cc = name[len];
8768 name[len] = NUL;
8769
8770 /*
8771 * Check for "b:changedtick".
8772 */
8773 if (STRCMP(name, "b:changedtick") == 0)
8774 {
8775 type = VAR_NUMBER;
8776 number = curbuf->b_changedtick;
8777 }
8778
8779 /*
8780 * Check for built-in v: variables.
8781 */
8782 else if ((i = find_vim_var(name, len)) >= 0)
8783 {
8784 type = vimvars[i].type;
8785 number = (long)vimvars[i].val;
8786 string = vimvars[i].val;
8787 }
8788
8789 /*
8790 * Check for user-defined variables.
8791 */
8792 else
8793 {
8794 v = find_var(name, FALSE);
8795 if (v != NULL)
8796 {
8797 type = v->var_type;
8798 number = v->var_val.var_number;
8799 string = v->var_val.var_string;
8800 }
8801 }
8802
8803 if (type == VAR_UNKNOWN)
8804 {
8805 if (retvar != NULL)
8806 EMSG2(_("E121: Undefined variable: %s"), name);
8807 ret = FAIL;
8808 }
8809 else if (retvar != NULL)
8810 {
8811 retvar->var_type = type;
8812 if (type == VAR_NUMBER)
8813 retvar->var_val.var_number = number;
8814 else if (type == VAR_STRING)
8815 {
8816 if (string != NULL)
8817 string = vim_strsave(string);
8818 retvar->var_val.var_string = string;
8819 }
8820 }
8821
8822 name[len] = cc;
8823
8824 return ret;
8825}
8826
8827/*
8828 * Allocate memory for a variable, and make it emtpy (0 or NULL value).
8829 */
8830 static VAR
8831alloc_var()
8832{
8833 return (VAR)alloc_clear((unsigned)sizeof(var));
8834}
8835
8836/*
8837 * Allocate memory for a variable, and assign a string to it.
8838 * The string "s" must have been allocated, it is consumed.
8839 * Return NULL for out of memory, the variable otherwise.
8840 */
8841 static VAR
8842alloc_string_var(s)
8843 char_u *s;
8844{
8845 VAR retvar;
8846
8847 retvar = alloc_var();
8848 if (retvar != NULL)
8849 {
8850 retvar->var_type = VAR_STRING;
8851 retvar->var_val.var_string = s;
8852 }
8853 else
8854 vim_free(s);
8855 return retvar;
8856}
8857
8858/*
8859 * Free the memory for a variable.
8860 */
8861 static void
8862free_var(varp)
8863 VAR varp;
8864{
8865 if (varp != NULL)
8866 {
8867 if (varp->var_type == VAR_STRING)
8868 vim_free(varp->var_val.var_string);
8869 vim_free(varp->var_name);
8870 vim_free(varp);
8871 }
8872}
8873
8874/*
8875 * Free the memory for a variable value and set the value to NULL or 0.
8876 */
8877 static void
8878clear_var(varp)
8879 VAR varp;
8880{
8881 if (varp != NULL)
8882 {
8883 if (varp->var_type == VAR_STRING)
8884 {
8885 vim_free(varp->var_val.var_string);
8886 varp->var_val.var_string = NULL;
8887 }
8888 else
8889 varp->var_val.var_number = 0;
8890 }
8891}
8892
8893/*
8894 * Get the number value of a variable.
8895 * If it is a String variable, uses vim_str2nr().
8896 */
8897 static long
8898get_var_number(varp)
8899 VAR varp;
8900{
8901 long n;
8902
8903 if (varp->var_type == VAR_NUMBER)
8904 return (long)(varp->var_val.var_number);
8905 else if (varp->var_type == VAR_UNKNOWN || varp->var_val.var_string == NULL)
8906 return 0L;
8907 else
8908 {
8909 vim_str2nr(varp->var_val.var_string, NULL, NULL, TRUE, TRUE, &n, NULL);
8910 return n;
8911 }
8912}
8913
8914/*
8915 * Get the lnum from the first argument. Also accepts ".", "$", etc.
8916 */
8917 static linenr_T
8918get_var_lnum(argvars)
8919 VAR argvars;
8920{
8921 var retvar;
8922 linenr_T lnum;
8923
8924 lnum = get_var_number(&argvars[0]);
8925 if (lnum == 0) /* no valid number, try using line() */
8926 {
8927 retvar.var_type = VAR_NUMBER;
8928 f_line(argvars, &retvar);
8929 lnum = retvar.var_val.var_number;
8930 clear_var(&retvar);
8931 }
8932 return lnum;
8933}
8934
8935/*
8936 * Get the string value of a variable.
8937 * If it is a Number variable, the number is converted into a string.
8938 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
8939 * get_var_string_buf() uses a given buffer.
8940 * If the String variable has never been set, return an empty string.
8941 * Never returns NULL;
8942 */
8943 static char_u *
8944get_var_string(varp)
8945 VAR varp;
8946{
8947 static char_u mybuf[NUMBUFLEN];
8948
8949 return get_var_string_buf(varp, mybuf);
8950}
8951
8952 static char_u *
8953get_var_string_buf(varp, buf)
8954 VAR varp;
8955 char_u *buf;
8956{
8957 if (varp->var_type == VAR_NUMBER)
8958 {
8959 sprintf((char *)buf, "%ld", (long)varp->var_val.var_number);
8960 return buf;
8961 }
8962 else if (varp->var_val.var_string == NULL)
8963 return (char_u *)"";
8964 else
8965 return varp->var_val.var_string;
8966}
8967
8968/*
8969 * Find variable "name" in the list of variables.
8970 * Return a pointer to it if found, NULL if not found.
8971 */
8972 static VAR
8973find_var(name, writing)
8974 char_u *name;
8975 int writing;
8976{
8977 int i;
8978 char_u *varname;
8979 garray_T *gap;
8980
8981 /* Check for function arguments "a:" */
8982 if (name[0] == 'a' && name[1] == ':')
8983 {
8984 if (writing)
8985 {
8986 EMSG2(_(e_readonlyvar), name);
8987 return NULL;
8988 }
8989 name += 2;
8990 if (current_funccal == NULL)
8991 return NULL;
8992 if (VIM_ISDIGIT(*name))
8993 {
8994 i = atol((char *)name);
8995 if (i == 0) /* a:0 */
8996 return &current_funccal->a0_var;
8997 i += current_funccal->func->args.ga_len;
8998 if (i > current_funccal->argcount) /* a:999 */
8999 return NULL;
9000 return &(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
9001 }
9002 if (STRCMP(name, "firstline") == 0)
9003 return &(current_funccal->firstline);
9004 if (STRCMP(name, "lastline") == 0)
9005 return &(current_funccal->lastline);
9006 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
9007 if (STRCMP(name, ((char_u **)
9008 (current_funccal->func->args.ga_data))[i]) == 0)
9009 return &(current_funccal->argvars[i]); /* a:name */
9010 return NULL;
9011 }
9012
9013 gap = find_var_ga(name, &varname);
9014 if (gap == NULL)
9015 return NULL;
9016 return find_var_in_ga(gap, varname);
9017}
9018
9019 static VAR
9020find_var_in_ga(gap, varname)
9021 garray_T *gap;
9022 char_u *varname;
9023{
9024 int i;
9025
9026 for (i = gap->ga_len; --i >= 0; )
9027 if (VAR_GAP_ENTRY(i, gap).var_name != NULL
9028 && STRCMP(VAR_GAP_ENTRY(i, gap).var_name, varname) == 0)
9029 break;
9030 if (i < 0)
9031 return NULL;
9032 return &VAR_GAP_ENTRY(i, gap);
9033}
9034
9035/*
9036 * Find the growarray and start of name without ':' for a variable name.
9037 */
9038 static garray_T *
9039find_var_ga(name, varname)
9040 char_u *name;
9041 char_u **varname;
9042{
9043 if (name[1] != ':')
9044 {
9045 /* If not "x:name" there must not be any ":" in the name. */
9046 if (vim_strchr(name, ':') != NULL)
9047 return NULL;
9048 *varname = name;
9049 if (current_funccal == NULL)
9050 return &variables; /* global variable */
9051 return &current_funccal->l_vars; /* local function variable */
9052 }
9053 *varname = name + 2;
9054 if (*name == 'b') /* buffer variable */
9055 return &curbuf->b_vars;
9056 if (*name == 'w') /* window variable */
9057 return &curwin->w_vars;
9058 if (*name == 'g') /* global variable */
9059 return &variables;
9060 if (*name == 'l' && current_funccal != NULL)/* local function variable */
9061 return &current_funccal->l_vars;
9062 if (*name == 's' /* script variable */
9063 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
9064 return &SCRIPT_VARS(current_SID);
9065 return NULL;
9066}
9067
9068/*
9069 * Get the string value of a (global/local) variable.
9070 * Returns NULL when it doesn't exist.
9071 */
9072 char_u *
9073get_var_value(name)
9074 char_u *name;
9075{
9076 VAR v;
9077
9078 v = find_var(name, FALSE);
9079 if (v == NULL)
9080 return NULL;
9081 return get_var_string(v);
9082}
9083
9084/*
9085 * Allocate a new growarry for a sourced script. It will be used while
9086 * sourcing this script and when executing functions defined in the script.
9087 */
9088 void
9089new_script_vars(id)
9090 scid_T id;
9091{
9092 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
9093 {
9094 while (ga_scripts.ga_len < id)
9095 {
9096 var_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
9097 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 }
9099 }
9100}
9101
9102/*
9103 * Initialize internal variables for use.
9104 */
9105 void
9106var_init(gap)
9107 garray_T *gap;
9108{
9109 ga_init2(gap, (int)sizeof(var), 4);
9110}
9111
9112/*
9113 * Clean up a list of internal variables.
9114 */
9115 void
9116var_clear(gap)
9117 garray_T *gap;
9118{
9119 int i;
9120
9121 for (i = gap->ga_len; --i >= 0; )
9122 var_free_one(&VAR_GAP_ENTRY(i, gap));
9123 ga_clear(gap);
9124}
9125
9126 static void
9127var_free_one(v)
9128 VAR v;
9129{
9130 vim_free(v->var_name);
9131 v->var_name = NULL;
9132 if (v->var_type == VAR_STRING)
9133 vim_free(v->var_val.var_string);
9134 v->var_val.var_string = NULL;
9135}
9136
9137/*
9138 * List the value of one internal variable.
9139 */
9140 static void
9141list_one_var(v, prefix)
9142 VAR v;
9143 char_u *prefix;
9144{
9145 list_one_var_a(prefix, v->var_name, v->var_type, get_var_string(v));
9146}
9147
9148/*
9149 * List the value of one "v:" variable.
9150 */
9151 static void
9152list_vim_var(i)
9153 int i; /* index in vimvars[] */
9154{
9155 char_u *p;
9156 char_u numbuf[NUMBUFLEN];
9157
9158 if (vimvars[i].type == VAR_NUMBER)
9159 {
9160 p = numbuf;
9161 sprintf((char *)p, "%ld", (long)vimvars[i].val);
9162 }
9163 else if (vimvars[i].val == NULL)
9164 p = (char_u *)"";
9165 else
9166 p = vimvars[i].val;
9167 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
9168 vimvars[i].type, p);
9169}
9170
9171 static void
9172list_one_var_a(prefix, name, type, string)
9173 char_u *prefix;
9174 char_u *name;
9175 int type;
9176 char_u *string;
9177{
9178 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
9179 if (name != NULL) /* "a:" vars don't have a name stored */
9180 msg_puts(name);
9181 msg_putchar(' ');
9182 msg_advance(22);
9183 if (type == VAR_NUMBER)
9184 msg_putchar('#');
9185 else
9186 msg_putchar(' ');
9187 msg_outtrans(string);
9188}
9189
9190/*
9191 * Set variable "name" to value in "varp".
9192 * If the variable already exists, the value is updated.
9193 * Otherwise the variable is created.
9194 */
9195 static void
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009196set_var(name, varp, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 char_u *name;
9198 VAR varp;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009199 int copy; /* make copy of value in "varp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 int i;
9202 VAR v;
9203 char_u *varname;
9204 garray_T *gap;
9205
9206 /*
9207 * Handle setting internal v: variables.
9208 */
9209 i = find_vim_var(name, (int)STRLEN(name));
9210 if (i >= 0)
9211 {
9212 if (vimvars[i].flags & VV_RO)
9213 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009214 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
9215 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 else
9217 {
9218 if (vimvars[i].type == VAR_STRING)
9219 {
9220 vim_free(vimvars[i].val);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009221 if (copy || varp->var_type != VAR_STRING)
9222 vimvars[i].val = vim_strsave(get_var_string(varp));
9223 else
9224 {
9225 /* Take over the string to avoid an extra alloc/free. */
9226 vimvars[i].val = varp->var_val.var_string;
9227 varp->var_val.var_string = NULL;
9228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 }
9230 else
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009231 vimvars[i].val = (char_u *)get_var_number(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 }
9233 return;
9234 }
9235
9236 v = find_var(name, TRUE);
9237 if (v != NULL) /* existing variable, only need to free string */
9238 {
9239 if (v->var_type == VAR_STRING)
9240 vim_free(v->var_val.var_string);
9241 }
9242 else /* add a new variable */
9243 {
9244 gap = find_var_ga(name, &varname);
9245 if (gap == NULL) /* illegal name */
9246 {
9247 EMSG2(_("E461: Illegal variable name: %s"), name);
9248 return;
9249 }
9250
9251 /* Try to use an empty entry */
9252 for (i = gap->ga_len; --i >= 0; )
9253 if (VAR_GAP_ENTRY(i, gap).var_name == NULL)
9254 break;
9255 if (i < 0) /* need to allocate more room */
9256 {
9257 if (ga_grow(gap, 1) == FAIL)
9258 return;
9259 i = gap->ga_len;
9260 }
9261 v = &VAR_GAP_ENTRY(i, gap);
9262 if ((v->var_name = vim_strsave(varname)) == NULL)
9263 return;
9264 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009267 if (copy || varp->var_type != VAR_STRING)
9268 copy_var(varp, v);
9269 else
9270 {
9271 v->var_type = varp->var_type;
9272 v->var_val.var_string = varp->var_val.var_string;
9273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274}
9275
9276 static void
9277copy_var(from, to)
9278 VAR from;
9279 VAR to;
9280{
9281 to->var_type = from->var_type;
9282 if (from->var_type == VAR_STRING)
9283 to->var_val.var_string = vim_strsave(get_var_string(from));
9284 else
9285 to->var_val.var_number = from->var_val.var_number;
9286}
9287
9288/*
9289 * ":echo expr1 ..." print each argument separated with a space, add a
9290 * newline at the end.
9291 * ":echon expr1 ..." print each argument plain.
9292 */
9293 void
9294ex_echo(eap)
9295 exarg_T *eap;
9296{
9297 char_u *arg = eap->arg;
9298 var retvar;
9299 char_u *p;
9300 int needclr = TRUE;
9301 int atstart = TRUE;
9302
9303 if (eap->skip)
9304 ++emsg_skip;
9305 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
9306 {
9307 p = arg;
9308 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9309 {
9310 /*
9311 * Report the invalid expression unless the expression evaluation
9312 * has been cancelled due to an aborting error, an interrupt, or an
9313 * exception.
9314 */
9315 if (!aborting())
9316 EMSG2(_(e_invexpr2), p);
9317 break;
9318 }
9319 if (!eap->skip)
9320 {
9321 if (atstart)
9322 {
9323 atstart = FALSE;
9324 /* Call msg_start() after eval1(), evaluating the expression
9325 * may cause a message to appear. */
9326 if (eap->cmdidx == CMD_echo)
9327 msg_start();
9328 }
9329 else if (eap->cmdidx == CMD_echo)
9330 msg_puts_attr((char_u *)" ", echo_attr);
9331 for (p = get_var_string(&retvar); *p != NUL && !got_int; ++p)
9332 if (*p == '\n' || *p == '\r' || *p == TAB)
9333 {
9334 if (*p != TAB && needclr)
9335 {
9336 /* remove any text still there from the command */
9337 msg_clr_eos();
9338 needclr = FALSE;
9339 }
9340 msg_putchar_attr(*p, echo_attr);
9341 }
9342 else
9343 {
9344#ifdef FEAT_MBYTE
9345 if (has_mbyte)
9346 {
9347 int i = (*mb_ptr2len_check)(p);
9348
9349 (void)msg_outtrans_len_attr(p, i, echo_attr);
9350 p += i - 1;
9351 }
9352 else
9353#endif
9354 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9355 }
9356 }
9357 clear_var(&retvar);
9358 arg = skipwhite(arg);
9359 }
9360 eap->nextcmd = check_nextcmd(arg);
9361
9362 if (eap->skip)
9363 --emsg_skip;
9364 else
9365 {
9366 /* remove text that may still be there from the command */
9367 if (needclr)
9368 msg_clr_eos();
9369 if (eap->cmdidx == CMD_echo)
9370 msg_end();
9371 }
9372}
9373
9374/*
9375 * ":echohl {name}".
9376 */
9377 void
9378ex_echohl(eap)
9379 exarg_T *eap;
9380{
9381 int id;
9382
9383 id = syn_name2id(eap->arg);
9384 if (id == 0)
9385 echo_attr = 0;
9386 else
9387 echo_attr = syn_id2attr(id);
9388}
9389
9390/*
9391 * ":execute expr1 ..." execute the result of an expression.
9392 * ":echomsg expr1 ..." Print a message
9393 * ":echoerr expr1 ..." Print an error
9394 * Each gets spaces around each argument and a newline at the end for
9395 * echo commands
9396 */
9397 void
9398ex_execute(eap)
9399 exarg_T *eap;
9400{
9401 char_u *arg = eap->arg;
9402 var retvar;
9403 int ret = OK;
9404 char_u *p;
9405 garray_T ga;
9406 int len;
9407 int save_did_emsg;
9408
9409 ga_init2(&ga, 1, 80);
9410
9411 if (eap->skip)
9412 ++emsg_skip;
9413 while (*arg != NUL && *arg != '|' && *arg != '\n')
9414 {
9415 p = arg;
9416 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9417 {
9418 /*
9419 * Report the invalid expression unless the expression evaluation
9420 * has been cancelled due to an aborting error, an interrupt, or an
9421 * exception.
9422 */
9423 if (!aborting())
9424 EMSG2(_(e_invexpr2), p);
9425 ret = FAIL;
9426 break;
9427 }
9428
9429 if (!eap->skip)
9430 {
9431 p = get_var_string(&retvar);
9432 len = (int)STRLEN(p);
9433 if (ga_grow(&ga, len + 2) == FAIL)
9434 {
9435 clear_var(&retvar);
9436 ret = FAIL;
9437 break;
9438 }
9439 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 ga.ga_len += len;
9443 }
9444
9445 clear_var(&retvar);
9446 arg = skipwhite(arg);
9447 }
9448
9449 if (ret != FAIL && ga.ga_data != NULL)
9450 {
9451 if (eap->cmdidx == CMD_echomsg)
9452 MSG_ATTR(ga.ga_data, echo_attr);
9453 else if (eap->cmdidx == CMD_echoerr)
9454 {
9455 /* We don't want to abort following commands, restore did_emsg. */
9456 save_did_emsg = did_emsg;
9457 EMSG((char_u *)ga.ga_data);
9458 if (!force_abort)
9459 did_emsg = save_did_emsg;
9460 }
9461 else if (eap->cmdidx == CMD_execute)
9462 do_cmdline((char_u *)ga.ga_data,
9463 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9464 }
9465
9466 ga_clear(&ga);
9467
9468 if (eap->skip)
9469 --emsg_skip;
9470
9471 eap->nextcmd = check_nextcmd(arg);
9472}
9473
9474/*
9475 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9476 * "arg" points to the "&" or '+' when called, to "option" when returning.
9477 * Returns NULL when no option name found. Otherwise pointer to the char
9478 * after the option name.
9479 */
9480 static char_u *
9481find_option_end(arg, opt_flags)
9482 char_u **arg;
9483 int *opt_flags;
9484{
9485 char_u *p = *arg;
9486
9487 ++p;
9488 if (*p == 'g' && p[1] == ':')
9489 {
9490 *opt_flags = OPT_GLOBAL;
9491 p += 2;
9492 }
9493 else if (*p == 'l' && p[1] == ':')
9494 {
9495 *opt_flags = OPT_LOCAL;
9496 p += 2;
9497 }
9498 else
9499 *opt_flags = 0;
9500
9501 if (!ASCII_ISALPHA(*p))
9502 return NULL;
9503 *arg = p;
9504
9505 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9506 p += 4; /* termcap option */
9507 else
9508 while (ASCII_ISALPHA(*p))
9509 ++p;
9510 return p;
9511}
9512
9513/*
9514 * ":function"
9515 */
9516 void
9517ex_function(eap)
9518 exarg_T *eap;
9519{
9520 char_u *theline;
9521 int j;
9522 int c;
9523#ifdef FEAT_MAGIC_BRACES
9524 int saved_did_emsg;
9525#endif
9526 char_u *name = NULL;
9527 char_u *p;
9528 char_u *arg;
9529 garray_T newargs;
9530 garray_T newlines;
9531 int varargs = FALSE;
9532 int mustend = FALSE;
9533 int flags = 0;
9534 ufunc_T *fp;
9535 int indent;
9536 int nesting;
9537 char_u *skip_until = NULL;
9538 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
9539
9540 /*
9541 * ":function" without argument: list functions.
9542 */
9543 if (ends_excmd(*eap->arg))
9544 {
9545 if (!eap->skip)
9546 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
9547 list_func_head(fp, FALSE);
9548 eap->nextcmd = check_nextcmd(eap->arg);
9549 return;
9550 }
9551
9552 p = eap->arg;
9553 name = trans_function_name(&p, eap->skip, FALSE);
9554 if (name == NULL && !eap->skip)
9555 {
9556 /*
9557 * Return on an invalid expression in braces, unless the expression
9558 * evaluation has been cancelled due to an aborting error, an
9559 * interrupt, or an exception.
9560 */
9561 if (!aborting())
9562 return;
9563 else
9564 eap->skip = TRUE;
9565 }
9566#ifdef FEAT_MAGIC_BRACES
9567 /* An error in a function call during evaluation of an expression in magic
9568 * braces should not cause the function not to be defined. */
9569 saved_did_emsg = did_emsg;
9570 did_emsg = FALSE;
9571#endif
9572
9573 /*
9574 * ":function func" with only function name: list function.
9575 */
9576 if (vim_strchr(p, '(') == NULL)
9577 {
9578 if (!ends_excmd(*skipwhite(p)))
9579 {
9580 EMSG(_(e_trailing));
9581 goto erret_name;
9582 }
9583 eap->nextcmd = check_nextcmd(p);
9584 if (eap->nextcmd != NULL)
9585 *p = NUL;
9586 if (!eap->skip && !got_int)
9587 {
9588 fp = find_func(name);
9589 if (fp != NULL)
9590 {
9591 list_func_head(fp, TRUE);
9592 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
9593 {
9594 msg_putchar('\n');
9595 msg_outnum((long)(j + 1));
9596 if (j < 9)
9597 msg_putchar(' ');
9598 if (j < 99)
9599 msg_putchar(' ');
9600 msg_prt_line(FUNCLINE(fp, j));
9601 out_flush(); /* show a line at a time */
9602 ui_breakcheck();
9603 }
9604 if (!got_int)
9605 {
9606 msg_putchar('\n');
9607 msg_puts((char_u *)" endfunction");
9608 }
9609 }
9610 else
9611 EMSG2(_("E123: Undefined function: %s"), eap->arg);
9612 }
9613 goto erret_name;
9614 }
9615
9616 /*
9617 * ":function name(arg1, arg2)" Define function.
9618 */
9619 p = skipwhite(p);
9620 if (*p != '(')
9621 {
9622 if (!eap->skip)
9623 {
9624 EMSG2(_("E124: Missing '(': %s"), eap->arg);
9625 goto erret_name;
9626 }
9627 /* attempt to continue by skipping some text */
9628 if (vim_strchr(p, '(') != NULL)
9629 p = vim_strchr(p, '(');
9630 }
9631 p = skipwhite(p + 1);
9632
9633 ga_init2(&newargs, (int)sizeof(char_u *), 3);
9634 ga_init2(&newlines, (int)sizeof(char_u *), 3);
9635
9636 /*
9637 * Isolate the arguments: "arg1, arg2, ...)"
9638 */
9639 while (*p != ')')
9640 {
9641 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
9642 {
9643 varargs = TRUE;
9644 p += 3;
9645 mustend = TRUE;
9646 }
9647 else
9648 {
9649 arg = p;
9650 while (ASCII_ISALNUM(*p) || *p == '_')
9651 ++p;
9652 if (arg == p || isdigit(*arg)
9653 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
9654 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
9655 {
9656 if (!eap->skip)
9657 EMSG2(_("E125: Illegal argument: %s"), arg);
9658 break;
9659 }
9660 if (ga_grow(&newargs, 1) == FAIL)
9661 goto erret;
9662 c = *p;
9663 *p = NUL;
9664 arg = vim_strsave(arg);
9665 if (arg == NULL)
9666 goto erret;
9667 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
9668 *p = c;
9669 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (*p == ',')
9671 ++p;
9672 else
9673 mustend = TRUE;
9674 }
9675 p = skipwhite(p);
9676 if (mustend && *p != ')')
9677 {
9678 if (!eap->skip)
9679 EMSG2(_(e_invarg2), eap->arg);
9680 break;
9681 }
9682 }
9683 ++p; /* skip the ')' */
9684
9685 /* find extra arguments "range" and "abort" */
9686 for (;;)
9687 {
9688 p = skipwhite(p);
9689 if (STRNCMP(p, "range", 5) == 0)
9690 {
9691 flags |= FC_RANGE;
9692 p += 5;
9693 }
9694 else if (STRNCMP(p, "abort", 5) == 0)
9695 {
9696 flags |= FC_ABORT;
9697 p += 5;
9698 }
9699 else
9700 break;
9701 }
9702
9703 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
9704 EMSG(_(e_trailing));
9705
9706 /*
9707 * Read the body of the function, until ":endfunction" is found.
9708 */
9709 if (KeyTyped)
9710 {
9711 /* Check if the function already exists, don't let the user type the
9712 * whole function before telling him it doesn't work! For a script we
9713 * need to skip the body to be able to find what follows. */
9714 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
9715 EMSG2(_(e_funcexts), name);
9716
9717 msg_putchar('\n'); /* don't overwrite the function name */
9718 cmdline_row = msg_row;
9719 }
9720
9721 indent = 2;
9722 nesting = 0;
9723 for (;;)
9724 {
9725 msg_scroll = TRUE;
9726 need_wait_return = FALSE;
9727 if (eap->getline == NULL)
9728 theline = getcmdline(':', 0L, indent);
9729 else
9730 theline = eap->getline(':', eap->cookie, indent);
9731 if (KeyTyped)
9732 lines_left = Rows - 1;
9733 if (theline == NULL)
9734 {
9735 EMSG(_("E126: Missing :endfunction"));
9736 goto erret;
9737 }
9738
9739 if (skip_until != NULL)
9740 {
9741 /* between ":append" and "." and between ":python <<EOF" and "EOF"
9742 * don't check for ":endfunc". */
9743 if (STRCMP(theline, skip_until) == 0)
9744 {
9745 vim_free(skip_until);
9746 skip_until = NULL;
9747 }
9748 }
9749 else
9750 {
9751 /* skip ':' and blanks*/
9752 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
9753 ;
9754
9755 /* Check for "endfunction" (should be more strict...). */
9756 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
9757 {
9758 vim_free(theline);
9759 break;
9760 }
9761
9762 /* Increase indent inside "if", "while", and "try", decrease
9763 * at "end". */
9764 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
9765 indent -= 2;
9766 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
9767 || STRNCMP(p, "try", 3) == 0)
9768 indent += 2;
9769
9770 /* Check for defining a function inside this function. */
9771 if (STRNCMP(p, "fu", 2) == 0)
9772 {
9773 p = skipwhite(skiptowhite(p));
9774 p += eval_fname_script(p);
9775 if (ASCII_ISALPHA(*p))
9776 {
9777 vim_free(trans_function_name(&p, TRUE, FALSE));
9778 if (*skipwhite(p) == '(')
9779 {
9780 ++nesting;
9781 indent += 2;
9782 }
9783 }
9784 }
9785
9786 /* Check for ":append" or ":insert". */
9787 p = skip_range(p, NULL);
9788 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
9789 || (p[0] == 'i'
9790 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
9791 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
9792 skip_until = vim_strsave((char_u *)".");
9793
9794 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
9795 arg = skipwhite(skiptowhite(p));
9796 if (arg[0] == '<' && arg[1] =='<'
9797 && ((p[0] == 'p' && p[1] == 'y'
9798 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
9799 || (p[0] == 'p' && p[1] == 'e'
9800 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
9801 || (p[0] == 't' && p[1] == 'c'
9802 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
9803 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
9804 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009805 || (p[0] == 'm' && p[1] == 'z'
9806 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 ))
9808 {
9809 /* ":python <<" continues until a dot, like ":append" */
9810 p = skipwhite(arg + 2);
9811 if (*p == NUL)
9812 skip_until = vim_strsave((char_u *)".");
9813 else
9814 skip_until = vim_strsave(p);
9815 }
9816 }
9817
9818 /* Add the line to the function. */
9819 if (ga_grow(&newlines, 1) == FAIL)
9820 goto erret;
9821 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
9822 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 }
9824
9825 /* Don't define the function when skipping commands or when an error was
9826 * detected. */
9827 if (eap->skip || did_emsg)
9828 goto erret;
9829
9830 /*
9831 * If there are no errors, add the function
9832 */
9833 fp = find_func(name);
9834 if (fp != NULL)
9835 {
9836 if (!eap->forceit)
9837 {
9838 EMSG2(_(e_funcexts), name);
9839 goto erret;
9840 }
9841 if (fp->calls)
9842 {
9843 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
9844 goto erret;
9845 }
9846 /* redefine existing function */
9847 ga_clear_strings(&(fp->args));
9848 ga_clear_strings(&(fp->lines));
9849 vim_free(name);
9850 }
9851 else
9852 {
9853 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
9854 if (fp == NULL)
9855 goto erret;
9856 /* insert the new function in the function list */
9857 fp->next = firstfunc;
9858 firstfunc = fp;
9859 fp->name = name;
9860 }
9861 fp->args = newargs;
9862 fp->lines = newlines;
9863 fp->varargs = varargs;
9864 fp->flags = flags;
9865 fp->calls = 0;
9866 fp->script_ID = current_SID;
9867#ifdef FEAT_MAGIC_BRACES
9868 did_emsg |= saved_did_emsg;
9869#endif
9870 vim_free(skip_until);
9871 return;
9872
9873erret:
9874 vim_free(skip_until);
9875 ga_clear_strings(&newargs);
9876 ga_clear_strings(&newlines);
9877erret_name:
9878 vim_free(name);
9879#ifdef FEAT_MAGIC_BRACES
9880 did_emsg |= saved_did_emsg;
9881#endif
9882}
9883
9884/*
9885 * Get a function name, translating "<SID>" and "<SNR>".
9886 * Returns the function name in allocated memory, or NULL for failure.
9887 * Advances "pp" to just after the function name (if no error).
9888 */
9889 static char_u *
9890trans_function_name(pp, skip, internal)
9891 char_u **pp;
9892 int skip; /* only find the end, don't evaluate */
9893 int internal; /* TRUE if internal function name OK */
9894{
9895 char_u *name;
9896 char_u *start;
9897 char_u *end;
9898 int lead;
9899 char_u sid_buf[20];
9900 char_u *temp_string = NULL;
9901 char_u *expr_start, *expr_end;
9902 int len;
9903
9904 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
9905 start = *pp;
9906 lead = eval_fname_script(start);
9907 if (lead > 0)
9908 start += lead;
9909 end = find_name_end(start, &expr_start, &expr_end);
9910 if (end == start)
9911 {
9912 if (!skip)
9913 EMSG(_("E129: Function name required"));
9914 return NULL;
9915 }
9916#ifdef FEAT_MAGIC_BRACES
9917 if (expr_start != NULL && !skip)
9918 {
9919 /* expand magic curlies */
9920 temp_string = make_expanded_name(start, expr_start, expr_end, end);
9921 if (temp_string == NULL)
9922 {
9923 /*
9924 * Report an invalid expression in braces, unless the expression
9925 * evaluation has been cancelled due to an aborting error, an
9926 * interrupt, or an exception.
9927 */
9928 if (!aborting())
9929 EMSG2(_(e_invarg2), start);
9930 else
9931 *pp = end;
9932 return NULL;
9933 }
9934 start = temp_string;
9935 len = (int)STRLEN(temp_string);
9936 }
9937 else
9938#endif
9939 len = (int)(end - start);
9940
9941 /*
9942 * Copy the function name to allocated memory.
9943 * Accept <SID>name() inside a script, translate into <SNR>123_name().
9944 * Accept <SNR>123_name() outside a script.
9945 */
9946 if (skip)
9947 lead = 0; /* do nothing */
9948 else if (lead > 0)
9949 {
9950 lead = 3;
9951 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9952 {
9953 if (current_SID <= 0)
9954 {
9955 EMSG(_(e_usingsid));
9956 return NULL;
9957 }
9958 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
9959 lead += (int)STRLEN(sid_buf);
9960 }
9961 }
9962 else if (!internal && !ASCII_ISUPPER(*start))
9963 {
9964 EMSG2(_("E128: Function name must start with a capital: %s"), start);
9965 return NULL;
9966 }
9967 name = alloc((unsigned)(len + lead + 1));
9968 if (name != NULL)
9969 {
9970 if (lead > 0)
9971 {
9972 name[0] = K_SPECIAL;
9973 name[1] = KS_EXTRA;
9974 name[2] = (int)KE_SNR;
9975 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9976 STRCPY(name + 3, sid_buf);
9977 }
9978 mch_memmove(name + lead, start, (size_t)len);
9979 name[len + lead] = NUL;
9980 }
9981 *pp = end;
9982
9983 vim_free(temp_string);
9984 return name;
9985}
9986
9987/*
9988 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
9989 * Return 2 if "p" starts with "s:".
9990 * Return 0 otherwise.
9991 */
9992 static int
9993eval_fname_script(p)
9994 char_u *p;
9995{
9996 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
9997 || STRNICMP(p + 1, "SNR>", 4) == 0))
9998 return 5;
9999 if (p[0] == 's' && p[1] == ':')
10000 return 2;
10001 return 0;
10002}
10003
10004/*
10005 * Return TRUE if "p" starts with "<SID>" or "s:".
10006 * Only works if eval_fname_script() returned non-zero for "p"!
10007 */
10008 static int
10009eval_fname_sid(p)
10010 char_u *p;
10011{
10012 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
10013}
10014
10015/*
10016 * List the head of the function: "name(arg1, arg2)".
10017 */
10018 static void
10019list_func_head(fp, indent)
10020 ufunc_T *fp;
10021 int indent;
10022{
10023 int j;
10024
10025 msg_start();
10026 if (indent)
10027 MSG_PUTS(" ");
10028 MSG_PUTS("function ");
10029 if (fp->name[0] == K_SPECIAL)
10030 {
10031 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
10032 msg_puts(fp->name + 3);
10033 }
10034 else
10035 msg_puts(fp->name);
10036 msg_putchar('(');
10037 for (j = 0; j < fp->args.ga_len; ++j)
10038 {
10039 if (j)
10040 MSG_PUTS(", ");
10041 msg_puts(FUNCARG(fp, j));
10042 }
10043 if (fp->varargs)
10044 {
10045 if (j)
10046 MSG_PUTS(", ");
10047 MSG_PUTS("...");
10048 }
10049 msg_putchar(')');
10050}
10051
10052/*
10053 * Find a function by name, return pointer to it in ufuncs.
10054 * Return NULL for unknown function.
10055 */
10056 static ufunc_T *
10057find_func(name)
10058 char_u *name;
10059{
10060 ufunc_T *fp;
10061
10062 for (fp = firstfunc; fp != NULL; fp = fp->next)
10063 if (STRCMP(name, fp->name) == 0)
10064 break;
10065 return fp;
10066}
10067
10068#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10069
10070/*
10071 * Function given to ExpandGeneric() to obtain the list of user defined
10072 * function names.
10073 */
10074 char_u *
10075get_user_func_name(xp, idx)
10076 expand_T *xp;
10077 int idx;
10078{
10079 static ufunc_T *fp = NULL;
10080
10081 if (idx == 0)
10082 fp = firstfunc;
10083 if (fp != NULL)
10084 {
10085 if (STRLEN(fp->name) + 4 >= IOSIZE)
10086 return fp->name; /* prevents overflow */
10087
10088 cat_func_name(IObuff, fp);
10089 if (xp->xp_context != EXPAND_USER_FUNC)
10090 {
10091 STRCAT(IObuff, "(");
10092 if (!fp->varargs && fp->args.ga_len == 0)
10093 STRCAT(IObuff, ")");
10094 }
10095
10096 fp = fp->next;
10097 return IObuff;
10098 }
10099 return NULL;
10100}
10101
10102#endif /* FEAT_CMDL_COMPL */
10103
10104/*
10105 * Copy the function name of "fp" to buffer "buf".
10106 * "buf" must be able to hold the function name plus three bytes.
10107 * Takes care of script-local function names.
10108 */
10109 static void
10110cat_func_name(buf, fp)
10111 char_u *buf;
10112 ufunc_T *fp;
10113{
10114 if (fp->name[0] == K_SPECIAL)
10115 {
10116 STRCPY(buf, "<SNR>");
10117 STRCAT(buf, fp->name + 3);
10118 }
10119 else
10120 STRCPY(buf, fp->name);
10121}
10122
10123/*
10124 * ":delfunction {name}"
10125 */
10126 void
10127ex_delfunction(eap)
10128 exarg_T *eap;
10129{
10130 ufunc_T *fp = NULL, *pfp;
10131 char_u *p;
10132 char_u *name;
10133
10134 p = eap->arg;
10135 name = trans_function_name(&p, eap->skip, FALSE);
10136 if (name == NULL)
10137 return;
10138 if (!ends_excmd(*skipwhite(p)))
10139 {
10140 vim_free(name);
10141 EMSG(_(e_trailing));
10142 return;
10143 }
10144 eap->nextcmd = check_nextcmd(p);
10145 if (eap->nextcmd != NULL)
10146 *p = NUL;
10147
10148 if (!eap->skip)
10149 fp = find_func(name);
10150 vim_free(name);
10151
10152 if (!eap->skip)
10153 {
10154 if (fp == NULL)
10155 {
10156 EMSG2(_("E130: Undefined function: %s"), eap->arg);
10157 return;
10158 }
10159 if (fp->calls)
10160 {
10161 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
10162 return;
10163 }
10164
10165 /* clear this function */
10166 vim_free(fp->name);
10167 ga_clear_strings(&(fp->args));
10168 ga_clear_strings(&(fp->lines));
10169
10170 /* remove the function from the function list */
10171 if (firstfunc == fp)
10172 firstfunc = fp->next;
10173 else
10174 {
10175 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
10176 if (pfp->next == fp)
10177 {
10178 pfp->next = fp->next;
10179 break;
10180 }
10181 }
10182 vim_free(fp);
10183 }
10184}
10185
10186/*
10187 * Call a user function.
10188 */
10189 static void
10190call_user_func(fp, argcount, argvars, retvar, firstline, lastline)
10191 ufunc_T *fp; /* pointer to function */
10192 int argcount; /* nr of args */
10193 VAR argvars; /* arguments */
10194 VAR retvar; /* return value */
10195 linenr_T firstline; /* first line of range */
10196 linenr_T lastline; /* last line of range */
10197{
10198 char_u *save_sourcing_name;
10199 linenr_T save_sourcing_lnum;
10200 scid_T save_current_SID;
10201 struct funccall fc;
10202 struct funccall *save_fcp = current_funccal;
10203 int save_did_emsg;
10204 static int depth = 0;
10205
10206 /* If depth of calling is getting too high, don't execute the function */
10207 if (depth >= p_mfd)
10208 {
10209 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
10210 retvar->var_type = VAR_NUMBER;
10211 retvar->var_val.var_number = -1;
10212 return;
10213 }
10214 ++depth;
10215
10216 line_breakcheck(); /* check for CTRL-C hit */
10217
10218 /* set local variables */
10219 var_init(&fc.l_vars);
10220 fc.func = fp;
10221 fc.argcount = argcount;
10222 fc.argvars = argvars;
10223 fc.retvar = retvar;
10224 retvar->var_val.var_number = 0;
10225 fc.linenr = 0;
10226 fc.returned = FALSE;
10227 fc.level = ex_nesting_level;
10228 fc.a0_var.var_type = VAR_NUMBER;
10229 fc.a0_var.var_val.var_number = argcount - fp->args.ga_len;
10230 fc.a0_var.var_name = NULL;
10231 current_funccal = &fc;
10232 fc.firstline.var_type = VAR_NUMBER;
10233 fc.firstline.var_val.var_number = firstline;
10234 fc.firstline.var_name = NULL;
10235 fc.lastline.var_type = VAR_NUMBER;
10236 fc.lastline.var_val.var_number = lastline;
10237 fc.lastline.var_name = NULL;
10238 /* Check if this function has a breakpoint. */
10239 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
10240 fc.dbg_tick = debug_tick;
10241
10242 /* Don't redraw while executing the function. */
10243 ++RedrawingDisabled;
10244 save_sourcing_name = sourcing_name;
10245 save_sourcing_lnum = sourcing_lnum;
10246 sourcing_lnum = 1;
10247 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
10248 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
10249 if (sourcing_name != NULL)
10250 {
10251 if (save_sourcing_name != NULL
10252 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
10253 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
10254 else
10255 STRCPY(sourcing_name, "function ");
10256 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
10257
10258 if (p_verbose >= 12)
10259 {
10260 ++no_wait_return;
10261 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10262 msg_str((char_u *)_("calling %s"), sourcing_name);
10263 if (p_verbose >= 14)
10264 {
10265 int i;
10266 char_u buf[MSG_BUF_LEN];
10267
10268 msg_puts((char_u *)"(");
10269 for (i = 0; i < argcount; ++i)
10270 {
10271 if (i > 0)
10272 msg_puts((char_u *)", ");
10273 if (argvars[i].var_type == VAR_NUMBER)
10274 msg_outnum((long)argvars[i].var_val.var_number);
10275 else
10276 {
10277 trunc_string(get_var_string(&argvars[i]),
10278 buf, MSG_BUF_LEN);
10279 msg_puts((char_u *)"\"");
10280 msg_puts(buf);
10281 msg_puts((char_u *)"\"");
10282 }
10283 }
10284 msg_puts((char_u *)")");
10285 }
10286 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10287 cmdline_row = msg_row;
10288 --no_wait_return;
10289 }
10290 }
10291 save_current_SID = current_SID;
10292 current_SID = fp->script_ID;
10293 save_did_emsg = did_emsg;
10294 did_emsg = FALSE;
10295
10296 /* call do_cmdline() to execute the lines */
10297 do_cmdline(NULL, get_func_line, (void *)&fc,
10298 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
10299
10300 --RedrawingDisabled;
10301
10302 /* when the function was aborted because of an error, return -1 */
10303 if ((did_emsg && (fp->flags & FC_ABORT)) || retvar->var_type == VAR_UNKNOWN)
10304 {
10305 clear_var(retvar);
10306 retvar->var_type = VAR_NUMBER;
10307 retvar->var_val.var_number = -1;
10308 }
10309
10310 /* when being verbose, mention the return value */
10311 if (p_verbose >= 12)
10312 {
10313 char_u *sn, *val;
10314
10315 ++no_wait_return;
10316 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10317
10318 /* Make sure the output fits in IObuff. */
10319 sn = sourcing_name;
10320 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
10321 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
10322
10323 if (aborting())
10324 smsg((char_u *)_("%s aborted"), sn);
10325 else if (fc.retvar->var_type == VAR_NUMBER)
10326 smsg((char_u *)_("%s returning #%ld"), sn,
10327 (long)fc.retvar->var_val.var_number);
10328 else if (fc.retvar->var_type == VAR_STRING)
10329 {
10330 val = get_var_string(fc.retvar);
10331 if (STRLEN(val) > IOSIZE / 2 - 50)
10332 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
10333 smsg((char_u *)_("%s returning \"%s\""), sn, val);
10334 }
10335 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10336 cmdline_row = msg_row;
10337 --no_wait_return;
10338 }
10339
10340 vim_free(sourcing_name);
10341 sourcing_name = save_sourcing_name;
10342 sourcing_lnum = save_sourcing_lnum;
10343 current_SID = save_current_SID;
10344
10345 if (p_verbose >= 12 && sourcing_name != NULL)
10346 {
10347 ++no_wait_return;
10348 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10349 msg_str((char_u *)_("continuing in %s"), sourcing_name);
10350 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10351 cmdline_row = msg_row;
10352 --no_wait_return;
10353 }
10354
10355 did_emsg |= save_did_emsg;
10356 current_funccal = save_fcp;
10357
10358 var_clear(&fc.l_vars); /* free all local variables */
10359 --depth;
10360}
10361
10362/*
10363 * ":return [expr]"
10364 */
10365 void
10366ex_return(eap)
10367 exarg_T *eap;
10368{
10369 char_u *arg = eap->arg;
10370 var retvar;
10371 int returning = FALSE;
10372
10373 if (current_funccal == NULL)
10374 {
10375 EMSG(_("E133: :return not inside a function"));
10376 return;
10377 }
10378
10379 if (eap->skip)
10380 ++emsg_skip;
10381
10382 eap->nextcmd = NULL;
10383 if ((*arg != NUL && *arg != '|' && *arg != '\n')
10384 && eval0(arg, &retvar, &eap->nextcmd, !eap->skip) != FAIL)
10385 {
10386 if (!eap->skip)
10387 returning = do_return(eap, FALSE, TRUE, &retvar);
10388 else
10389 clear_var(&retvar);
10390 }
10391 /* It's safer to return also on error. */
10392 else if (!eap->skip)
10393 {
10394 /*
10395 * Return unless the expression evaluation has been cancelled due to an
10396 * aborting error, an interrupt, or an exception.
10397 */
10398 if (!aborting())
10399 returning = do_return(eap, FALSE, TRUE, NULL);
10400 }
10401
10402 /* When skipping or the return gets pending, advance to the next command
10403 * in this line (!returning). Otherwise, ignore the rest of the line.
10404 * Following lines will be ignored by get_func_line(). */
10405 if (returning)
10406 eap->nextcmd = NULL;
10407 else if (eap->nextcmd == NULL) /* no argument */
10408 eap->nextcmd = check_nextcmd(arg);
10409
10410 if (eap->skip)
10411 --emsg_skip;
10412}
10413
10414/*
10415 * Return from a function. Possibly makes the return pending. Also called
10416 * for a pending return at the ":endtry" or after returning from an extra
10417 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
10418 * when called due to a ":return" command. "value" may point to a variable
10419 * with the return value. Returns TRUE when the return can be carried out,
10420 * FALSE when the return gets pending.
10421 */
10422 int
10423do_return(eap, reanimate, is_cmd, value)
10424 exarg_T *eap;
10425 int reanimate;
10426 int is_cmd;
10427 void *value;
10428{
10429 int idx;
10430 struct condstack *cstack = eap->cstack;
10431
10432 if (reanimate)
10433 /* Undo the return. */
10434 current_funccal->returned = FALSE;
10435
10436 /*
10437 * Cleanup (and inactivate) conditionals, but stop when a try conditional
10438 * not in its finally clause (which then is to be executed next) is found.
10439 * In this case, make the ":return" pending for execution at the ":endtry".
10440 * Otherwise, return normally.
10441 */
10442 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
10443 if (idx >= 0)
10444 {
10445 cstack->cs_pending[idx] = CSTP_RETURN;
10446
10447 if (!is_cmd && !reanimate)
10448 /* A pending return again gets pending. "value" points to an
10449 * allocated variable with the value of the original ":return"'s
10450 * argument if present or is NULL else. */
10451 cstack->cs_retvar[idx] = value;
10452 else
10453 {
10454 /* When undoing a return in order to make it pending, get the stored
10455 * return value. */
10456 if (reanimate)
10457 value = current_funccal->retvar;
10458
10459 if (value != NULL)
10460 {
10461 /* Store the value of the pending return. */
10462 if ((cstack->cs_retvar[idx] = alloc_var()) != NULL)
10463 *(VAR)cstack->cs_retvar[idx] = *(VAR)value;
10464 else
10465 EMSG(_(e_outofmem));
10466 }
10467 else
10468 cstack->cs_retvar[idx] = NULL;
10469
10470 if (reanimate)
10471 {
10472 /* The pending return value could be overwritten by a ":return"
10473 * without argument in a finally clause; reset the default
10474 * return value. */
10475 current_funccal->retvar->var_type = VAR_NUMBER;
10476 current_funccal->retvar->var_val.var_number = 0;
10477 }
10478 }
10479 report_make_pending(CSTP_RETURN, value);
10480 }
10481 else
10482 {
10483 current_funccal->returned = TRUE;
10484
10485 /* If the return is carried out now, store the return value. For
10486 * a return immediately after reanimation, the value is already
10487 * there. */
10488 if (!reanimate && value != NULL)
10489 {
10490 clear_var(current_funccal->retvar);
10491 *current_funccal->retvar = *(VAR)value;
10492 if (!is_cmd)
10493 vim_free(value);
10494 }
10495 }
10496
10497 return idx < 0;
10498}
10499
10500/*
10501 * Free the variable with a pending return value.
10502 */
10503 void
10504discard_pending_return(retvar)
10505 void *retvar;
10506{
10507 /* The variable was copied from one with an undefined var_name. So we can't
10508 * use free_var() to clear and free it. */
10509 clear_var((VAR)retvar);
10510 vim_free(retvar);
10511}
10512
10513/*
10514 * Generate a return command for producing the value of "retvar". The result
10515 * is an allocated string. Used by report_pending() for verbose messages.
10516 */
10517 char_u *
10518get_return_cmd(retvar)
10519 void *retvar;
10520{
10521 char_u *s = IObuff;
10522
10523 if (retvar == NULL || ((VAR)retvar)->var_type == VAR_UNKNOWN)
10524 s = (char_u *)":return";
10525 else if (((VAR)retvar)->var_type == VAR_STRING)
10526 sprintf((char *)IObuff, ":return \"%s\"",
10527 ((VAR)retvar)->var_val.var_string);
10528 else
10529 sprintf((char *)IObuff, ":return %ld",
10530 (long)(((VAR)retvar)->var_val.var_number));
10531 return vim_strsave(s);
10532}
10533
10534/*
10535 * Get next function line.
10536 * Called by do_cmdline() to get the next line.
10537 * Returns allocated string, or NULL for end of function.
10538 */
10539/* ARGSUSED */
10540 char_u *
10541get_func_line(c, cookie, indent)
10542 int c; /* not used */
10543 void *cookie;
10544 int indent; /* not used */
10545{
10546 struct funccall *fcp = (struct funccall *)cookie;
10547 char_u *retval;
10548 garray_T *gap; /* growarray with function lines */
10549
10550 /* If breakpoints have been added/deleted need to check for it. */
10551 if (fcp->dbg_tick != debug_tick)
10552 {
10553 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10554 sourcing_lnum);
10555 fcp->dbg_tick = debug_tick;
10556 }
10557
10558 gap = &fcp->func->lines;
10559 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10560 retval = NULL;
10561 else if (fcp->returned || fcp->linenr >= gap->ga_len)
10562 retval = NULL;
10563 else
10564 {
10565 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
10566 sourcing_lnum = fcp->linenr;
10567 }
10568
10569 /* Did we encounter a breakpoint? */
10570 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
10571 {
10572 dbg_breakpoint(fcp->func->name, sourcing_lnum);
10573 /* Find next breakpoint. */
10574 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10575 sourcing_lnum);
10576 fcp->dbg_tick = debug_tick;
10577 }
10578
10579 return retval;
10580}
10581
10582/*
10583 * Return TRUE if the currently active function should be ended, because a
10584 * return was encountered or an error occured. Used inside a ":while".
10585 */
10586 int
10587func_has_ended(cookie)
10588 void *cookie;
10589{
10590 struct funccall *fcp = (struct funccall *)cookie;
10591
10592 /* Ignore the "abort" flag if the abortion behavior has been changed due to
10593 * an error inside a try conditional. */
10594 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10595 || fcp->returned);
10596}
10597
10598/*
10599 * return TRUE if cookie indicates a function which "abort"s on errors.
10600 */
10601 int
10602func_has_abort(cookie)
10603 void *cookie;
10604{
10605 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
10606}
10607
10608#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
10609typedef enum
10610{
10611 VAR_FLAVOUR_DEFAULT,
10612 VAR_FLAVOUR_SESSION,
10613 VAR_FLAVOUR_VIMINFO
10614} var_flavour_T;
10615
10616static var_flavour_T var_flavour __ARGS((char_u *varname));
10617
10618 static var_flavour_T
10619var_flavour(varname)
10620 char_u *varname;
10621{
10622 char_u *p = varname;
10623
10624 if (ASCII_ISUPPER(*p))
10625 {
10626 while (*(++p))
10627 if (ASCII_ISLOWER(*p))
10628 return VAR_FLAVOUR_SESSION;
10629 return VAR_FLAVOUR_VIMINFO;
10630 }
10631 else
10632 return VAR_FLAVOUR_DEFAULT;
10633}
10634#endif
10635
10636#if defined(FEAT_VIMINFO) || defined(PROTO)
10637/*
10638 * Restore global vars that start with a capital from the viminfo file
10639 */
10640 int
10641read_viminfo_varlist(virp, writing)
10642 vir_T *virp;
10643 int writing;
10644{
10645 char_u *tab;
10646 int is_string = FALSE;
10647 VAR varp = NULL;
10648 char_u *val;
10649
10650 if (!writing && (find_viminfo_parameter('!') != NULL))
10651 {
10652 tab = vim_strchr(virp->vir_line + 1, '\t');
10653 if (tab != NULL)
10654 {
10655 *tab++ = '\0'; /* isolate the variable name */
10656 if (*tab == 'S') /* string var */
10657 is_string = TRUE;
10658
10659 tab = vim_strchr(tab, '\t');
10660 if (tab != NULL)
10661 {
10662 /* create a nameless variable to hold the value */
10663 if (is_string)
10664 {
10665 val = viminfo_readstring(virp,
10666 (int)(tab - virp->vir_line + 1), TRUE);
10667 if (val != NULL)
10668 varp = alloc_string_var(val);
10669 }
10670 else
10671 {
10672 varp = alloc_var();
10673 if (varp != NULL)
10674 {
10675 varp->var_type = VAR_NUMBER;
10676 varp->var_val.var_number = atol((char *)tab + 1);
10677 }
10678 }
10679 /* assign the value to the variable */
10680 if (varp != NULL)
10681 {
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010682 set_var(virp->vir_line + 1, varp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 free_var(varp);
10684 }
10685 }
10686 }
10687 }
10688
10689 return viminfo_readline(virp);
10690}
10691
10692/*
10693 * Write global vars that start with a capital to the viminfo file
10694 */
10695 void
10696write_viminfo_varlist(fp)
10697 FILE *fp;
10698{
10699 garray_T *gap = &variables; /* global variable */
10700 VAR this_var;
10701 int i;
10702
10703 if (find_viminfo_parameter('!') == NULL)
10704 return;
10705
10706 fprintf(fp, _("\n# global variables:\n"));
10707 for (i = gap->ga_len; --i >= 0; )
10708 {
10709 this_var = &VAR_GAP_ENTRY(i, gap);
10710 if (this_var->var_name != NULL
10711 && var_flavour(this_var->var_name) == VAR_FLAVOUR_VIMINFO)
10712 {
10713 fprintf(fp, "!%s\t%s\t", this_var->var_name,
10714 (this_var->var_type == VAR_STRING) ? "STR" : "NUM");
10715 viminfo_writestring(fp, get_var_string(this_var));
10716 }
10717 }
10718}
10719#endif
10720
10721#if defined(FEAT_SESSION) || defined(PROTO)
10722 int
10723store_session_globals(fd)
10724 FILE *fd;
10725{
10726 garray_T *gap = &variables; /* global variable */
10727 VAR this_var;
10728 int i;
10729 char_u *p, *t;
10730
10731 for (i = gap->ga_len; --i >= 0; )
10732 {
10733 this_var = &VAR_GAP_ENTRY(i, gap);
10734 if (this_var->var_name != NULL)
10735 {
10736 if (var_flavour(this_var->var_name) == VAR_FLAVOUR_SESSION)
10737 {
10738 /* Escapse special characters with a backslash. Turn a LF and
10739 * CR into \n and \r. */
10740 p = vim_strsave_escaped(get_var_string(this_var),
10741 (char_u *)"\\\"\n\r");
10742 if (p == NULL) /* out of memory */
10743 continue;
10744 for (t = p; *t != NUL; ++t)
10745 if (*t == '\n')
10746 *t = 'n';
10747 else if (*t == '\r')
10748 *t = 'r';
10749 if ((fprintf(fd, "let %s = %c%s%c",
10750 this_var->var_name,
10751 (this_var->var_type == VAR_STRING) ? '"' : ' ',
10752 p,
10753 (this_var->var_type == VAR_STRING) ? '"' : ' ') < 0)
10754 || put_eol(fd) == FAIL)
10755 {
10756 vim_free(p);
10757 return FAIL;
10758 }
10759 vim_free(p);
10760 }
10761
10762 }
10763 }
10764 return OK;
10765}
10766#endif
10767
10768#endif /* FEAT_EVAL */
10769
10770#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
10771
10772
10773#ifdef WIN3264
10774/*
10775 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10776 */
10777static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10778static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
10779static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10780
10781/*
10782 * Get the short pathname of a file.
10783 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
10784 */
10785 static int
10786get_short_pathname(fnamep, bufp, fnamelen)
10787 char_u **fnamep;
10788 char_u **bufp;
10789 int *fnamelen;
10790{
10791 int l,len;
10792 char_u *newbuf;
10793
10794 len = *fnamelen;
10795
10796 l = GetShortPathName(*fnamep, *fnamep, len);
10797 if (l > len - 1)
10798 {
10799 /* If that doesn't work (not enough space), then save the string
10800 * and try again with a new buffer big enough
10801 */
10802 newbuf = vim_strnsave(*fnamep, l);
10803 if (newbuf == NULL)
10804 return 0;
10805
10806 vim_free(*bufp);
10807 *fnamep = *bufp = newbuf;
10808
10809 l = GetShortPathName(*fnamep,*fnamep,l+1);
10810
10811 /* Really should always succeed, as the buffer is big enough */
10812 }
10813
10814 *fnamelen = l;
10815 return 1;
10816}
10817
10818/*
10819 * Create a short path name. Returns the length of the buffer it needs.
10820 * Doesn't copy over the end of the buffer passed in.
10821 */
10822 static int
10823shortpath_for_invalid_fname(fname, bufp, fnamelen)
10824 char_u **fname;
10825 char_u **bufp;
10826 int *fnamelen;
10827{
10828 char_u *s, *p, *pbuf2, *pbuf3;
10829 char_u ch;
10830 int l,len,len2,plen,slen;
10831
10832 /* Make a copy */
10833 len2 = *fnamelen;
10834 pbuf2 = vim_strnsave(*fname, len2);
10835 pbuf3 = NULL;
10836
10837 s = pbuf2 + len2 - 1; /* Find the end */
10838 slen = 1;
10839 plen = len2;
10840
10841 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010842 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 {
10844 --s;
10845 ++slen;
10846 --plen;
10847 }
10848
10849 do
10850 {
10851 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010852 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 {
10854 --s;
10855 ++slen;
10856 --plen;
10857 }
10858 if (s <= pbuf2)
10859 break;
10860
10861 /* Remeber the character that is about to be blatted */
10862 ch = *s;
10863 *s = 0; /* get_short_pathname requires a null-terminated string */
10864
10865 /* Try it in situ */
10866 p = pbuf2;
10867 if (!get_short_pathname(&p, &pbuf3, &plen))
10868 {
10869 vim_free(pbuf2);
10870 return -1;
10871 }
10872 *s = ch; /* Preserve the string */
10873 } while (plen == 0);
10874
10875 if (plen > 0)
10876 {
10877 /* Remeber the length of the new string. */
10878 *fnamelen = len = plen + slen;
10879 vim_free(*bufp);
10880 if (len > len2)
10881 {
10882 /* If there's not enough space in the currently allocated string,
10883 * then copy it to a buffer big enough.
10884 */
10885 *fname= *bufp = vim_strnsave(p, len);
10886 if (*fname == NULL)
10887 return -1;
10888 }
10889 else
10890 {
10891 /* Transfer pbuf2 to being the main buffer (it's big enough) */
10892 *fname = *bufp = pbuf2;
10893 if (p != pbuf2)
10894 strncpy(*fname, p, plen);
10895 pbuf2 = NULL;
10896 }
10897 /* Concat the next bit */
10898 strncpy(*fname + plen, s, slen);
10899 (*fname)[len] = '\0';
10900 }
10901 vim_free(pbuf3);
10902 vim_free(pbuf2);
10903 return 0;
10904}
10905
10906/*
10907 * Get a pathname for a partial path.
10908 */
10909 static int
10910shortpath_for_partial(fnamep, bufp, fnamelen)
10911 char_u **fnamep;
10912 char_u **bufp;
10913 int *fnamelen;
10914{
10915 int sepcount, len, tflen;
10916 char_u *p;
10917 char_u *pbuf, *tfname;
10918 int hasTilde;
10919
10920 /* Count up the path seperators from the RHS.. so we know which part
10921 * of the path to return.
10922 */
10923 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010924 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 if (vim_ispathsep(*p))
10926 ++sepcount;
10927
10928 /* Need full path first (use expand_env() to remove a "~/") */
10929 hasTilde = (**fnamep == '~');
10930 if (hasTilde)
10931 pbuf = tfname = expand_env_save(*fnamep);
10932 else
10933 pbuf = tfname = FullName_save(*fnamep, FALSE);
10934
10935 len = tflen = STRLEN(tfname);
10936
10937 if (!get_short_pathname(&tfname, &pbuf, &len))
10938 return -1;
10939
10940 if (len == 0)
10941 {
10942 /* Don't have a valid filename, so shorten the rest of the
10943 * path if we can. This CAN give us invalid 8.3 filenames, but
10944 * there's not a lot of point in guessing what it might be.
10945 */
10946 len = tflen;
10947 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
10948 return -1;
10949 }
10950
10951 /* Count the paths backward to find the beginning of the desired string. */
10952 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010953 {
10954#ifdef FEAT_MBYTE
10955 if (has_mbyte)
10956 p -= mb_head_off(tfname, p);
10957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (vim_ispathsep(*p))
10959 {
10960 if (sepcount == 0 || (hasTilde && sepcount == 1))
10961 break;
10962 else
10963 sepcount --;
10964 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 if (hasTilde)
10967 {
10968 --p;
10969 if (p >= tfname)
10970 *p = '~';
10971 else
10972 return -1;
10973 }
10974 else
10975 ++p;
10976
10977 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10978 vim_free(*bufp);
10979 *fnamelen = (int)STRLEN(p);
10980 *bufp = pbuf;
10981 *fnamep = p;
10982
10983 return 0;
10984}
10985#endif /* WIN3264 */
10986
10987/*
10988 * Adjust a filename, according to a string of modifiers.
10989 * *fnamep must be NUL terminated when called. When returning, the length is
10990 * determined by *fnamelen.
10991 * Returns valid flags.
10992 * When there is an error, *fnamep is set to NULL.
10993 */
10994 int
10995modify_fname(src, usedlen, fnamep, bufp, fnamelen)
10996 char_u *src; /* string with modifiers */
10997 int *usedlen; /* characters after src that are used */
10998 char_u **fnamep; /* file name so far */
10999 char_u **bufp; /* buffer for allocated file name or NULL */
11000 int *fnamelen; /* length of fnamep */
11001{
11002 int valid = 0;
11003 char_u *tail;
11004 char_u *s, *p, *pbuf;
11005 char_u dirname[MAXPATHL];
11006 int c;
11007 int has_fullname = 0;
11008#ifdef WIN3264
11009 int has_shortname = 0;
11010#endif
11011
11012repeat:
11013 /* ":p" - full path/file_name */
11014 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
11015 {
11016 has_fullname = 1;
11017
11018 valid |= VALID_PATH;
11019 *usedlen += 2;
11020
11021 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
11022 if ((*fnamep)[0] == '~'
11023#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
11024 && ((*fnamep)[1] == '/'
11025# ifdef BACKSLASH_IN_FILENAME
11026 || (*fnamep)[1] == '\\'
11027# endif
11028 || (*fnamep)[1] == NUL)
11029
11030#endif
11031 )
11032 {
11033 *fnamep = expand_env_save(*fnamep);
11034 vim_free(*bufp); /* free any allocated file name */
11035 *bufp = *fnamep;
11036 if (*fnamep == NULL)
11037 return -1;
11038 }
11039
11040 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011041 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 {
11043 if (vim_ispathsep(*p)
11044 && p[1] == '.'
11045 && (p[2] == NUL
11046 || vim_ispathsep(p[2])
11047 || (p[2] == '.'
11048 && (p[3] == NUL || vim_ispathsep(p[3])))))
11049 break;
11050 }
11051
11052 /* FullName_save() is slow, don't use it when not needed. */
11053 if (*p != NUL || !vim_isAbsName(*fnamep))
11054 {
11055 *fnamep = FullName_save(*fnamep, *p != NUL);
11056 vim_free(*bufp); /* free any allocated file name */
11057 *bufp = *fnamep;
11058 if (*fnamep == NULL)
11059 return -1;
11060 }
11061
11062 /* Append a path separator to a directory. */
11063 if (mch_isdir(*fnamep))
11064 {
11065 /* Make room for one or two extra characters. */
11066 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
11067 vim_free(*bufp); /* free any allocated file name */
11068 *bufp = *fnamep;
11069 if (*fnamep == NULL)
11070 return -1;
11071 add_pathsep(*fnamep);
11072 }
11073 }
11074
11075 /* ":." - path relative to the current directory */
11076 /* ":~" - path relative to the home directory */
11077 /* ":8" - shortname path - postponed till after */
11078 while (src[*usedlen] == ':'
11079 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
11080 {
11081 *usedlen += 2;
11082 if (c == '8')
11083 {
11084#ifdef WIN3264
11085 has_shortname = 1; /* Postpone this. */
11086#endif
11087 continue;
11088 }
11089 pbuf = NULL;
11090 /* Need full path first (use expand_env() to remove a "~/") */
11091 if (!has_fullname)
11092 {
11093 if (c == '.' && **fnamep == '~')
11094 p = pbuf = expand_env_save(*fnamep);
11095 else
11096 p = pbuf = FullName_save(*fnamep, FALSE);
11097 }
11098 else
11099 p = *fnamep;
11100
11101 has_fullname = 0;
11102
11103 if (p != NULL)
11104 {
11105 if (c == '.')
11106 {
11107 mch_dirname(dirname, MAXPATHL);
11108 s = shorten_fname(p, dirname);
11109 if (s != NULL)
11110 {
11111 *fnamep = s;
11112 if (pbuf != NULL)
11113 {
11114 vim_free(*bufp); /* free any allocated file name */
11115 *bufp = pbuf;
11116 pbuf = NULL;
11117 }
11118 }
11119 }
11120 else
11121 {
11122 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
11123 /* Only replace it when it starts with '~' */
11124 if (*dirname == '~')
11125 {
11126 s = vim_strsave(dirname);
11127 if (s != NULL)
11128 {
11129 *fnamep = s;
11130 vim_free(*bufp);
11131 *bufp = s;
11132 }
11133 }
11134 }
11135 vim_free(pbuf);
11136 }
11137 }
11138
11139 tail = gettail(*fnamep);
11140 *fnamelen = (int)STRLEN(*fnamep);
11141
11142 /* ":h" - head, remove "/file_name", can be repeated */
11143 /* Don't remove the first "/" or "c:\" */
11144 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
11145 {
11146 valid |= VALID_HEAD;
11147 *usedlen += 2;
11148 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011149 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 --tail;
11151 *fnamelen = (int)(tail - *fnamep);
11152#ifdef VMS
11153 if (*fnamelen > 0)
11154 *fnamelen += 1; /* the path separator is part of the path */
11155#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011156 while (tail > s && !after_pathsep(s, tail))
11157 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 }
11159
11160 /* ":8" - shortname */
11161 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
11162 {
11163 *usedlen += 2;
11164#ifdef WIN3264
11165 has_shortname = 1;
11166#endif
11167 }
11168
11169#ifdef WIN3264
11170 /* Check shortname after we have done 'heads' and before we do 'tails'
11171 */
11172 if (has_shortname)
11173 {
11174 pbuf = NULL;
11175 /* Copy the string if it is shortened by :h */
11176 if (*fnamelen < (int)STRLEN(*fnamep))
11177 {
11178 p = vim_strnsave(*fnamep, *fnamelen);
11179 if (p == 0)
11180 return -1;
11181 vim_free(*bufp);
11182 *bufp = *fnamep = p;
11183 }
11184
11185 /* Split into two implementations - makes it easier. First is where
11186 * there isn't a full name already, second is where there is.
11187 */
11188 if (!has_fullname && !vim_isAbsName(*fnamep))
11189 {
11190 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
11191 return -1;
11192 }
11193 else
11194 {
11195 int l;
11196
11197 /* Simple case, already have the full-name
11198 * Nearly always shorter, so try first time. */
11199 l = *fnamelen;
11200 if (!get_short_pathname(fnamep, bufp, &l))
11201 return -1;
11202
11203 if (l == 0)
11204 {
11205 /* Couldn't find the filename.. search the paths.
11206 */
11207 l = *fnamelen;
11208 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
11209 return -1;
11210 }
11211 *fnamelen = l;
11212 }
11213 }
11214#endif /* WIN3264 */
11215
11216 /* ":t" - tail, just the basename */
11217 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
11218 {
11219 *usedlen += 2;
11220 *fnamelen -= (int)(tail - *fnamep);
11221 *fnamep = tail;
11222 }
11223
11224 /* ":e" - extension, can be repeated */
11225 /* ":r" - root, without extension, can be repeated */
11226 while (src[*usedlen] == ':'
11227 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
11228 {
11229 /* find a '.' in the tail:
11230 * - for second :e: before the current fname
11231 * - otherwise: The last '.'
11232 */
11233 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
11234 s = *fnamep - 2;
11235 else
11236 s = *fnamep + *fnamelen - 1;
11237 for ( ; s > tail; --s)
11238 if (s[0] == '.')
11239 break;
11240 if (src[*usedlen + 1] == 'e') /* :e */
11241 {
11242 if (s > tail)
11243 {
11244 *fnamelen += (int)(*fnamep - (s + 1));
11245 *fnamep = s + 1;
11246#ifdef VMS
11247 /* cut version from the extension */
11248 s = *fnamep + *fnamelen - 1;
11249 for ( ; s > *fnamep; --s)
11250 if (s[0] == ';')
11251 break;
11252 if (s > *fnamep)
11253 *fnamelen = s - *fnamep;
11254#endif
11255 }
11256 else if (*fnamep <= tail)
11257 *fnamelen = 0;
11258 }
11259 else /* :r */
11260 {
11261 if (s > tail) /* remove one extension */
11262 *fnamelen = (int)(s - *fnamep);
11263 }
11264 *usedlen += 2;
11265 }
11266
11267 /* ":s?pat?foo?" - substitute */
11268 /* ":gs?pat?foo?" - global substitute */
11269 if (src[*usedlen] == ':'
11270 && (src[*usedlen + 1] == 's'
11271 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
11272 {
11273 char_u *str;
11274 char_u *pat;
11275 char_u *sub;
11276 int sep;
11277 char_u *flags;
11278 int didit = FALSE;
11279
11280 flags = (char_u *)"";
11281 s = src + *usedlen + 2;
11282 if (src[*usedlen + 1] == 'g')
11283 {
11284 flags = (char_u *)"g";
11285 ++s;
11286 }
11287
11288 sep = *s++;
11289 if (sep)
11290 {
11291 /* find end of pattern */
11292 p = vim_strchr(s, sep);
11293 if (p != NULL)
11294 {
11295 pat = vim_strnsave(s, (int)(p - s));
11296 if (pat != NULL)
11297 {
11298 s = p + 1;
11299 /* find end of substitution */
11300 p = vim_strchr(s, sep);
11301 if (p != NULL)
11302 {
11303 sub = vim_strnsave(s, (int)(p - s));
11304 str = vim_strnsave(*fnamep, *fnamelen);
11305 if (sub != NULL && str != NULL)
11306 {
11307 *usedlen = (int)(p + 1 - src);
11308 s = do_string_sub(str, pat, sub, flags);
11309 if (s != NULL)
11310 {
11311 *fnamep = s;
11312 *fnamelen = (int)STRLEN(s);
11313 vim_free(*bufp);
11314 *bufp = s;
11315 didit = TRUE;
11316 }
11317 }
11318 vim_free(sub);
11319 vim_free(str);
11320 }
11321 vim_free(pat);
11322 }
11323 }
11324 /* after using ":s", repeat all the modifiers */
11325 if (didit)
11326 goto repeat;
11327 }
11328 }
11329
11330 return valid;
11331}
11332
11333/*
11334 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
11335 * "flags" can be "g" to do a global substitute.
11336 * Returns an allocated string, NULL for error.
11337 */
11338 char_u *
11339do_string_sub(str, pat, sub, flags)
11340 char_u *str;
11341 char_u *pat;
11342 char_u *sub;
11343 char_u *flags;
11344{
11345 int sublen;
11346 regmatch_T regmatch;
11347 int i;
11348 int do_all;
11349 char_u *tail;
11350 garray_T ga;
11351 char_u *ret;
11352 char_u *save_cpo;
11353
11354 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
11355 save_cpo = p_cpo;
11356 p_cpo = (char_u *)"";
11357
11358 ga_init2(&ga, 1, 200);
11359
11360 do_all = (flags[0] == 'g');
11361
11362 regmatch.rm_ic = p_ic;
11363 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11364 if (regmatch.regprog != NULL)
11365 {
11366 tail = str;
11367 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
11368 {
11369 /*
11370 * Get some space for a temporary buffer to do the substitution
11371 * into. It will contain:
11372 * - The text up to where the match is.
11373 * - The substituted text.
11374 * - The text after the match.
11375 */
11376 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
11377 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
11378 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11379 {
11380 ga_clear(&ga);
11381 break;
11382 }
11383
11384 /* copy the text up to where the match is */
11385 i = (int)(regmatch.startp[0] - tail);
11386 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11387 /* add the substituted text */
11388 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
11389 + ga.ga_len + i, TRUE, TRUE, FALSE);
11390 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 /* avoid getting stuck on a match with an empty string */
11392 if (tail == regmatch.endp[0])
11393 {
11394 if (*tail == NUL)
11395 break;
11396 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
11397 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 }
11399 else
11400 {
11401 tail = regmatch.endp[0];
11402 if (*tail == NUL)
11403 break;
11404 }
11405 if (!do_all)
11406 break;
11407 }
11408
11409 if (ga.ga_data != NULL)
11410 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11411
11412 vim_free(regmatch.regprog);
11413 }
11414
11415 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11416 ga_clear(&ga);
11417 p_cpo = save_cpo;
11418
11419 return ret;
11420}
11421
11422#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */