blob: 13a5b1bf6b37e13e3d3b5dfe8b71188b1692df27 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
33#if SIZEOF_INT <= 3 /* use long if int is smaller than 32 bits */
34typedef long varnumber_T;
35#else
36typedef int varnumber_T;
37#endif
38
39/*
40 * Structure to hold an internal variable.
41 */
42typedef struct
43{
44 char_u *var_name; /* name of variable */
45 char var_type; /* VAR_NUMBER or VAR_STRING */
46 union
47 {
48 varnumber_T var_number; /* number value */
49 char_u *var_string; /* string value (Careful: can be NULL!) */
50 } var_val;
51} var;
52
53#define VAR_UNKNOWN 0
54#define VAR_NUMBER 1
55#define VAR_STRING 2
56
57typedef var * VAR;
58
59/*
60 * All user-defined global variables are stored in "variables".
61 */
62garray_T variables = {0, 0, sizeof(var), 4, NULL};
63
64/*
65 * Array to hold an array with variables local to each sourced script.
66 */
67static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
68#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
69
70
71#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
72#define VAR_GAP_ENTRY(idx, gap) (((VAR)(gap->ga_data))[idx])
73#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
74#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
75
76static int echo_attr = 0; /* attributes used for ":echo" */
77
78/*
79 * Structure to hold info for a user function.
80 */
81typedef struct ufunc ufunc_T;
82
83struct ufunc
84{
85 ufunc_T *next; /* next function in list */
86 char_u *name; /* name of function; can start with <SNR>123_
87 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
88 int varargs; /* variable nr of arguments */
89 int flags;
90 int calls; /* nr of active calls */
91 garray_T args; /* arguments */
92 garray_T lines; /* function lines */
93 scid_T script_ID; /* ID of script where function was defined,
94 used for s: variables */
95};
96
97/* function flags */
98#define FC_ABORT 1 /* abort function on error */
99#define FC_RANGE 2 /* function accepts range */
100
101/*
102 * All user-defined functions are found in the forward-linked function list.
103 * The first function is pointed at by firstfunc.
104 */
105ufunc_T *firstfunc = NULL;
106
107#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
108#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
109
110/* structure to hold info for a function that is currently being executed. */
111struct funccall
112{
113 ufunc_T *func; /* function being called */
114 int linenr; /* next line to be executed */
115 int returned; /* ":return" used */
116 int argcount; /* nr of arguments */
117 VAR argvars; /* arguments */
118 var a0_var; /* "a:0" variable */
119 var firstline; /* "a:firstline" variable */
120 var lastline; /* "a:lastline" variable */
121 garray_T l_vars; /* local function variables */
122 VAR retvar; /* return value variable */
123 linenr_T breakpoint; /* next line with breakpoint or zero */
124 int dbg_tick; /* debug_tick when breakpoint was set */
125 int level; /* top nesting level of executed function */
126};
127
128/*
129 * Return the name of the executed function.
130 */
131 char_u *
132func_name(cookie)
133 void *cookie;
134{
135 return ((struct funccall *)cookie)->func->name;
136}
137
138/*
139 * Return the address holding the next breakpoint line for a funccall cookie.
140 */
141 linenr_T *
142func_breakpoint(cookie)
143 void *cookie;
144{
145 return &((struct funccall *)cookie)->breakpoint;
146}
147
148/*
149 * Return the address holding the debug tick for a funccall cookie.
150 */
151 int *
152func_dbg_tick(cookie)
153 void *cookie;
154{
155 return &((struct funccall *)cookie)->dbg_tick;
156}
157
158/*
159 * Return the nesting level for a funccall cookie.
160 */
161 int
162func_level(cookie)
163 void *cookie;
164{
165 return ((struct funccall *)cookie)->level;
166}
167
168/* pointer to funccal for currently active function */
169struct funccall *current_funccal = NULL;
170
171/*
172 * Return TRUE when a function was ended by a ":return" command.
173 */
174 int
175current_func_returned()
176{
177 return current_funccal->returned;
178}
179
180
181/*
182 * Array to hold the value of v: variables.
183 */
184#include "version.h"
185
186/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000187#define VV_COMPAT 1 /* compatible, also used without "v:" */
188#define VV_RO 2 /* read-only */
189#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
191struct vimvar
192{
193 char *name; /* name of variable, without v: */
194 int len; /* length of name */
195 char_u *val; /* current value (can also be a number!) */
196 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000197 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198} vimvars[VV_LEN] =
199{ /* The order here must match the VV_ defines in vim.h! */
200 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
201 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
202 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
203 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
204 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
205 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
206 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
207 VV_COMPAT+VV_RO},
208 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
209 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
210 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000211 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
213 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
214 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
215 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
216 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
217 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
218 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
219 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
220 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
221 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
222 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
223 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000224 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
225 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
226 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
227 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
229 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
230 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
231 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
232 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
233 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
234 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000235 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236};
237
238static int eval0 __ARGS((char_u *arg, VAR retvar, char_u **nextcmd, int evaluate));
239static int eval1 __ARGS((char_u **arg, VAR retvar, int evaluate));
240static int eval2 __ARGS((char_u **arg, VAR retvar, int evaluate));
241static int eval3 __ARGS((char_u **arg, VAR retvar, int evaluate));
242static int eval4 __ARGS((char_u **arg, VAR retvar, int evaluate));
243static int eval5 __ARGS((char_u **arg, VAR retvar, int evaluate));
244static int eval6 __ARGS((char_u **arg, VAR retvar, int evaluate));
245static int eval7 __ARGS((char_u **arg, VAR retvar, int evaluate));
246static int get_option_var __ARGS((char_u **arg, VAR retvar, int evaluate));
247static int get_string_var __ARGS((char_u **arg, VAR retvar, int evaluate));
248static int get_lit_string_var __ARGS((char_u **arg, VAR retvar, int evaluate));
249static int get_env_var __ARGS((char_u **arg, VAR retvar, int evaluate));
250static int find_internal_func __ARGS((char_u *name));
251static int get_func_var __ARGS((char_u *name, int len, VAR retvar, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
252static int call_func __ARGS((char_u *name, int len, VAR retvar, int argcount, VAR argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
253static void f_append __ARGS((VAR argvars, VAR retvar));
254static void f_argc __ARGS((VAR argvars, VAR retvar));
255static void f_argidx __ARGS((VAR argvars, VAR retvar));
256static void f_argv __ARGS((VAR argvars, VAR retvar));
257static void f_browse __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000258static void f_browsedir __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259static buf_T *find_buffer __ARGS((VAR avar));
260static void f_bufexists __ARGS((VAR argvars, VAR retvar));
261static void f_buflisted __ARGS((VAR argvars, VAR retvar));
262static void f_bufloaded __ARGS((VAR argvars, VAR retvar));
263static buf_T *get_buf_var __ARGS((VAR avar));
264static void f_bufname __ARGS((VAR argvars, VAR retvar));
265static void f_bufnr __ARGS((VAR argvars, VAR retvar));
266static void f_bufwinnr __ARGS((VAR argvars, VAR retvar));
267static void f_byte2line __ARGS((VAR argvars, VAR retvar));
Bram Moolenaarab79bcb2004-07-18 21:34:53 +0000268static void f_byteidx __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269static void f_char2nr __ARGS((VAR argvars, VAR retvar));
270static void f_cindent __ARGS((VAR argvars, VAR retvar));
271static void f_col __ARGS((VAR argvars, VAR retvar));
272static void f_confirm __ARGS((VAR argvars, VAR retvar));
273static void f_cscope_connection __ARGS((VAR argvars, VAR retvar));
274static void f_cursor __ARGS((VAR argsvars, VAR retvar));
275static void f_delete __ARGS((VAR argvars, VAR retvar));
276static void f_did_filetype __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar47136d72004-10-12 20:02:24 +0000277static void f_diff_filler __ARGS((VAR argvars, VAR retvar));
278static void f_diff_hlID __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static void f_escape __ARGS((VAR argvars, VAR retvar));
280static void f_eventhandler __ARGS((VAR argvars, VAR retvar));
281static void f_executable __ARGS((VAR argvars, VAR retvar));
282static void f_exists __ARGS((VAR argvars, VAR retvar));
283static void f_expand __ARGS((VAR argvars, VAR retvar));
284static void f_filereadable __ARGS((VAR argvars, VAR retvar));
285static void f_filewritable __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +0000286static void f_finddir __ARGS((VAR argvars, VAR retvar));
287static void f_findfile __ARGS((VAR argvars, VAR retvar));
288static void f_findfilendir __ARGS((VAR argvars, VAR retvar, int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static void f_fnamemodify __ARGS((VAR argvars, VAR retvar));
290static void f_foldclosed __ARGS((VAR argvars, VAR retvar));
291static void f_foldclosedend __ARGS((VAR argvars, VAR retvar));
292static void foldclosed_both __ARGS((VAR argvars, VAR retvar, int end));
293static void f_foldlevel __ARGS((VAR argvars, VAR retvar));
294static void f_foldtext __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000295static void f_foldtextresult __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void f_foreground __ARGS((VAR argvars, VAR retvar));
297static void f_getbufvar __ARGS((VAR argvars, VAR retvar));
298static void f_getchar __ARGS((VAR argvars, VAR retvar));
299static void f_getcharmod __ARGS((VAR argvars, VAR retvar));
300static void f_getcmdline __ARGS((VAR argvars, VAR retvar));
301static void f_getcmdpos __ARGS((VAR argvars, VAR retvar));
302static void f_getcwd __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar46c9c732004-12-12 11:37:09 +0000303static void f_getfontname __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000304static void f_getfperm __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static void f_getfsize __ARGS((VAR argvars, VAR retvar));
306static void f_getftime __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000307static void f_getftype __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static void f_getline __ARGS((VAR argvars, VAR retvar));
309static void f_getreg __ARGS((VAR argvars, VAR retvar));
310static void f_getregtype __ARGS((VAR argvars, VAR retvar));
311static void f_getwinposx __ARGS((VAR argvars, VAR retvar));
312static void f_getwinposy __ARGS((VAR argvars, VAR retvar));
313static void f_getwinvar __ARGS((VAR argvars, VAR retvar));
314static void f_glob __ARGS((VAR argvars, VAR retvar));
315static void f_globpath __ARGS((VAR argvars, VAR retvar));
316static void f_has __ARGS((VAR argvars, VAR retvar));
317static void f_hasmapto __ARGS((VAR argvars, VAR retvar));
318static void f_histadd __ARGS((VAR argvars, VAR retvar));
319static void f_histdel __ARGS((VAR argvars, VAR retvar));
320static void f_histget __ARGS((VAR argvars, VAR retvar));
321static void f_histnr __ARGS((VAR argvars, VAR retvar));
322static void f_hlexists __ARGS((VAR argvars, VAR retvar));
323static void f_hlID __ARGS((VAR argvars, VAR retvar));
324static void f_hostname __ARGS((VAR argvars, VAR retvar));
325static void f_iconv __ARGS((VAR argvars, VAR retvar));
326static void f_indent __ARGS((VAR argvars, VAR retvar));
327static void f_isdirectory __ARGS((VAR argvars, VAR retvar));
328static void f_input __ARGS((VAR argvars, VAR retvar));
329static void f_inputdialog __ARGS((VAR argvars, VAR retvar));
330static void f_inputrestore __ARGS((VAR argvars, VAR retvar));
331static void f_inputsave __ARGS((VAR argvars, VAR retvar));
332static void f_inputsecret __ARGS((VAR argvars, VAR retvar));
333static void f_last_buffer_nr __ARGS((VAR argvars, VAR retvar));
334static void f_libcall __ARGS((VAR argvars, VAR retvar));
335static void f_libcallnr __ARGS((VAR argvars, VAR retvar));
336static void libcall_common __ARGS((VAR argvars, VAR retvar, int type));
337static void f_line __ARGS((VAR argvars, VAR retvar));
338static void f_line2byte __ARGS((VAR argvars, VAR retvar));
339static void f_lispindent __ARGS((VAR argvars, VAR retvar));
340static void f_localtime __ARGS((VAR argvars, VAR retvar));
341static void f_maparg __ARGS((VAR argvars, VAR retvar));
342static void f_mapcheck __ARGS((VAR argvars, VAR retvar));
343static void get_maparg __ARGS((VAR argvars, VAR retvar, int exact));
344static void f_match __ARGS((VAR argvars, VAR retvar));
345static void f_matchend __ARGS((VAR argvars, VAR retvar));
346static void f_matchstr __ARGS((VAR argvars, VAR retvar));
347static void f_mode __ARGS((VAR argvars, VAR retvar));
348static void f_nextnonblank __ARGS((VAR argvars, VAR retvar));
349static void f_nr2char __ARGS((VAR argvars, VAR retvar));
350static void f_prevnonblank __ARGS((VAR argvars, VAR retvar));
351static void f_setbufvar __ARGS((VAR argvars, VAR retvar));
352static void f_setcmdpos __ARGS((VAR argvars, VAR retvar));
353static void f_setwinvar __ARGS((VAR argvars, VAR retvar));
354static void f_rename __ARGS((VAR argvars, VAR retvar));
355static void f_resolve __ARGS((VAR argvars, VAR retvar));
356static void f_search __ARGS((VAR argvars, VAR retvar));
357static void f_searchpair __ARGS((VAR argvars, VAR retvar));
358static int get_search_arg __ARGS((VAR varp, int *flagsp));
359static void f_remote_expr __ARGS((VAR argvars, VAR retvar));
360static void f_remote_foreground __ARGS((VAR argvars, VAR retvar));
361static void f_remote_peek __ARGS((VAR argvars, VAR retvar));
362static void f_remote_read __ARGS((VAR argvars, VAR retvar));
363static void f_remote_send __ARGS((VAR argvars, VAR retvar));
Bram Moolenaarab79bcb2004-07-18 21:34:53 +0000364static void f_repeat __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365static void f_server2client __ARGS((VAR argvars, VAR retvar));
366static void f_serverlist __ARGS((VAR argvars, VAR retvar));
367static void f_setline __ARGS((VAR argvars, VAR retvar));
368static void f_setreg __ARGS((VAR argvars, VAR retvar));
369static void f_simplify __ARGS((VAR argvars, VAR retvar));
370static void find_some_match __ARGS((VAR argvars, VAR retvar, int start));
371static void f_strftime __ARGS((VAR argvars, VAR retvar));
372static void f_stridx __ARGS((VAR argvars, VAR retvar));
373static void f_strlen __ARGS((VAR argvars, VAR retvar));
374static void f_strpart __ARGS((VAR argvars, VAR retvar));
375static void f_strridx __ARGS((VAR argvars, VAR retvar));
376static void f_strtrans __ARGS((VAR argvars, VAR retvar));
377static void f_synID __ARGS((VAR argvars, VAR retvar));
378static void f_synIDattr __ARGS((VAR argvars, VAR retvar));
379static void f_synIDtrans __ARGS((VAR argvars, VAR retvar));
380static void f_system __ARGS((VAR argvars, VAR retvar));
381static void f_submatch __ARGS((VAR argvars, VAR retvar));
382static void f_substitute __ARGS((VAR argvars, VAR retvar));
383static void f_tempname __ARGS((VAR argvars, VAR retvar));
384static void f_tolower __ARGS((VAR argvars, VAR retvar));
385static void f_toupper __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar8299df92004-07-10 09:47:34 +0000386static void f_tr __ARGS((VAR argvars, VAR retvar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static void f_type __ARGS((VAR argvars, VAR retvar));
388static void f_virtcol __ARGS((VAR argvars, VAR retvar));
389static void f_visualmode __ARGS((VAR argvars, VAR retvar));
390static void f_winbufnr __ARGS((VAR argvars, VAR retvar));
391static void f_wincol __ARGS((VAR argvars, VAR retvar));
392static void f_winheight __ARGS((VAR argvars, VAR retvar));
393static void f_winline __ARGS((VAR argvars, VAR retvar));
394static void f_winnr __ARGS((VAR argvars, VAR retvar));
395static void f_winrestcmd __ARGS((VAR argvars, VAR retvar));
396static void f_winwidth __ARGS((VAR argvars, VAR retvar));
397static win_T *find_win_by_nr __ARGS((VAR vp));
398static pos_T *var2fpos __ARGS((VAR varp, int lnum));
399static int get_env_len __ARGS((char_u **arg));
400static int get_id_len __ARGS((char_u **arg));
401static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
402static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end));
403static int eval_isnamec __ARGS((int c));
404static int find_vim_var __ARGS((char_u *name, int len));
405static int get_var_var __ARGS((char_u *name, int len, VAR retvar));
406static VAR alloc_var __ARGS((void));
407static VAR alloc_string_var __ARGS((char_u *string));
408static void free_var __ARGS((VAR varp));
409static void clear_var __ARGS((VAR varp));
410static long get_var_number __ARGS((VAR varp));
411static linenr_T get_var_lnum __ARGS((VAR argvars));
412static char_u *get_var_string __ARGS((VAR varp));
413static char_u *get_var_string_buf __ARGS((VAR varp, char_u *buf));
414static VAR find_var __ARGS((char_u *name, int writing));
415static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
416static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
417static void var_free_one __ARGS((VAR v));
418static void list_one_var __ARGS((VAR v, char_u *prefix));
419static void list_vim_var __ARGS((int i));
420static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
421static void set_var __ARGS((char_u *name, VAR varp));
422static void copy_var __ARGS((VAR from, VAR to));
423static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
424static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
425static int eval_fname_script __ARGS((char_u *p));
426static int eval_fname_sid __ARGS((char_u *p));
427static void list_func_head __ARGS((ufunc_T *fp, int indent));
428static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
429static ufunc_T *find_func __ARGS((char_u *name));
430static void call_user_func __ARGS((ufunc_T *fp, int argcount, VAR argvars, VAR retvar, linenr_T firstline, linenr_T lastline));
431
432/* Magic braces are always enabled, otherwise Vim scripts would not be
433 * portable. */
434#define FEAT_MAGIC_BRACES
435
436#ifdef FEAT_MAGIC_BRACES
437static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
438#endif
439
440/*
441 * Set an internal variable to a string value. Creates the variable if it does
442 * not already exist.
443 */
444 void
445set_internal_string_var(name, value)
446 char_u *name;
447 char_u *value;
448{
449 char_u *val;
450 VAR varp;
451
452 val = vim_strsave(value);
453 if (val != NULL)
454 {
455 varp = alloc_string_var(val);
456 if (varp != NULL)
457 {
458 set_var(name, varp);
459 free_var(varp);
460 }
461 }
462}
463
464# if defined(FEAT_MBYTE) || defined(PROTO)
465 int
466eval_charconvert(enc_from, enc_to, fname_from, fname_to)
467 char_u *enc_from;
468 char_u *enc_to;
469 char_u *fname_from;
470 char_u *fname_to;
471{
472 int err = FALSE;
473
474 set_vim_var_string(VV_CC_FROM, enc_from, -1);
475 set_vim_var_string(VV_CC_TO, enc_to, -1);
476 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
477 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
478 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
479 err = TRUE;
480 set_vim_var_string(VV_CC_FROM, NULL, -1);
481 set_vim_var_string(VV_CC_TO, NULL, -1);
482 set_vim_var_string(VV_FNAME_IN, NULL, -1);
483 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
484
485 if (err)
486 return FAIL;
487 return OK;
488}
489# endif
490
491# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
492 int
493eval_printexpr(fname, args)
494 char_u *fname;
495 char_u *args;
496{
497 int err = FALSE;
498
499 set_vim_var_string(VV_FNAME_IN, fname, -1);
500 set_vim_var_string(VV_CMDARG, args, -1);
501 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
502 err = TRUE;
503 set_vim_var_string(VV_FNAME_IN, NULL, -1);
504 set_vim_var_string(VV_CMDARG, NULL, -1);
505
506 if (err)
507 {
508 mch_remove(fname);
509 return FAIL;
510 }
511 return OK;
512}
513# endif
514
515# if defined(FEAT_DIFF) || defined(PROTO)
516 void
517eval_diff(origfile, newfile, outfile)
518 char_u *origfile;
519 char_u *newfile;
520 char_u *outfile;
521{
522 int err = FALSE;
523
524 set_vim_var_string(VV_FNAME_IN, origfile, -1);
525 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
526 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
527 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
528 set_vim_var_string(VV_FNAME_IN, NULL, -1);
529 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
530 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
531}
532
533 void
534eval_patch(origfile, difffile, outfile)
535 char_u *origfile;
536 char_u *difffile;
537 char_u *outfile;
538{
539 int err;
540
541 set_vim_var_string(VV_FNAME_IN, origfile, -1);
542 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
543 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
544 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
545 set_vim_var_string(VV_FNAME_IN, NULL, -1);
546 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
547 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
548}
549# endif
550
551/*
552 * Top level evaluation function, returning a boolean.
553 * Sets "error" to TRUE if there was an error.
554 * Return TRUE or FALSE.
555 */
556 int
557eval_to_bool(arg, error, nextcmd, skip)
558 char_u *arg;
559 int *error;
560 char_u **nextcmd;
561 int skip; /* only parse, don't execute */
562{
563 var retvar;
564 int retval = FALSE;
565
566 if (skip)
567 ++emsg_skip;
568 if (eval0(arg, &retvar, nextcmd, !skip) == FAIL)
569 {
570 *error = TRUE;
571 }
572 else
573 {
574 *error = FALSE;
575 if (!skip)
576 {
577 retval = (get_var_number(&retvar) != 0);
578 clear_var(&retvar);
579 }
580 }
581 if (skip)
582 --emsg_skip;
583
584 return retval;
585}
586
587/*
588 * Top level evaluation function, returning a string. If "skip" is TRUE,
589 * only parsing to "nextcmd" is done, without reporting errors. Return
590 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
591 */
592 char_u *
593eval_to_string_skip(arg, nextcmd, skip)
594 char_u *arg;
595 char_u **nextcmd;
596 int skip; /* only parse, don't execute */
597{
598 var retvar;
599 char_u *retval;
600
601 if (skip)
602 ++emsg_skip;
603 if (eval0(arg, &retvar, nextcmd, !skip) == FAIL || skip)
604 retval = NULL;
605 else
606 {
607 retval = vim_strsave(get_var_string(&retvar));
608 clear_var(&retvar);
609 }
610 if (skip)
611 --emsg_skip;
612
613 return retval;
614}
615
616/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000617 * Skip over an expression at "*pp".
618 * Return FAIL for an error, OK otherwise.
619 */
620 int
621skip_expr(pp)
622 char_u **pp;
623{
624 var retvar;
625
626 *pp = skipwhite(*pp);
627 return eval1(pp, &retvar, FALSE);
628}
629
630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 * Top level evaluation function, returning a string.
632 * Return pointer to allocated memory, or NULL for failure.
633 */
634 char_u *
635eval_to_string(arg, nextcmd)
636 char_u *arg;
637 char_u **nextcmd;
638{
639 var retvar;
640 char_u *retval;
641
642 if (eval0(arg, &retvar, nextcmd, TRUE) == FAIL)
643 retval = NULL;
644 else
645 {
646 retval = vim_strsave(get_var_string(&retvar));
647 clear_var(&retvar);
648 }
649
650 return retval;
651}
652
653/*
654 * Call eval_to_string() with "sandbox" set and not using local variables.
655 */
656 char_u *
657eval_to_string_safe(arg, nextcmd)
658 char_u *arg;
659 char_u **nextcmd;
660{
661 char_u *retval;
662 void *save_funccalp;
663
664 save_funccalp = save_funccal();
665 ++sandbox;
666 retval = eval_to_string(arg, nextcmd);
667 --sandbox;
668 restore_funccal(save_funccalp);
669 return retval;
670}
671
672#if 0 /* not used */
673/*
674 * Top level evaluation function, returning a string.
675 * Advances "arg" to the first non-blank after the evaluated expression.
676 * Return pointer to allocated memory, or NULL for failure.
677 * Doesn't give error messages.
678 */
679 char_u *
680eval_arg_to_string(arg)
681 char_u **arg;
682{
683 var retvar;
684 char_u *retval;
685 int ret;
686
687 ++emsg_off;
688
689 ret = eval1(arg, &retvar, TRUE);
690 if (ret == FAIL)
691 retval = NULL;
692 else
693 {
694 retval = vim_strsave(get_var_string(&retvar));
695 clear_var(&retvar);
696 }
697
698 --emsg_off;
699
700 return retval;
701}
702#endif
703
704/*
705 * Top level evaluation function, returning a number.
706 * Evaluates "expr" silently.
707 * Returns -1 for an error.
708 */
709 int
710eval_to_number(expr)
711 char_u *expr;
712{
713 var retvar;
714 int retval;
715 char_u *p = expr;
716
717 ++emsg_off;
718
719 if (eval1(&p, &retvar, TRUE) == FAIL)
720 retval = -1;
721 else
722 {
723 retval = get_var_number(&retvar);
724 clear_var(&retvar);
725 }
726 --emsg_off;
727
728 return retval;
729}
730
731#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
732/*
733 * Call some vimL function and return the result as a string
734 * Uses argv[argc] for the function arguments.
735 */
736 char_u *
737call_vim_function(func, argc, argv, safe)
738 char_u *func;
739 int argc;
740 char_u **argv;
741 int safe; /* use the sandbox */
742{
743 char_u *retval = NULL;
744 var retvar;
745 VAR argvars;
746 long n;
747 int len;
748 int i;
749 int doesrange;
750 void *save_funccalp = NULL;
751
752 argvars = (VAR)alloc((unsigned)(argc * sizeof(var)));
753 if (argvars == NULL)
754 return NULL;
755
756 for (i = 0; i < argc; i++)
757 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000758 /* Pass a NULL or empty argument as an empty string */
759 if (argv[i] == NULL || *argv[i] == NUL)
760 {
761 argvars[i].var_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000762 argvars[i].var_val.var_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000763 continue;
764 }
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 /* Recognize a number argument, the others must be strings. */
767 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
768 if (len != 0 && len == (int)STRLEN(argv[i]))
769 {
770 argvars[i].var_type = VAR_NUMBER;
771 argvars[i].var_val.var_number = n;
772 }
773 else
774 {
775 argvars[i].var_type = VAR_STRING;
776 argvars[i].var_val.var_string = argv[i];
777 }
778 }
779
780 if (safe)
781 {
782 save_funccalp = save_funccal();
783 ++sandbox;
784 }
785
786 retvar.var_type = VAR_UNKNOWN; /* clear_var() uses this */
787 if (call_func(func, (int)STRLEN(func), &retvar, argc, argvars,
788 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
789 &doesrange, TRUE) == OK)
790 retval = vim_strsave(get_var_string(&retvar));
791
792 clear_var(&retvar);
793 vim_free(argvars);
794
795 if (safe)
796 {
797 --sandbox;
798 restore_funccal(save_funccalp);
799 }
800 return retval;
801}
802#endif
803
804/*
805 * Save the current function call pointer, and set it to NULL.
806 * Used when executing autocommands and for ":source".
807 */
808 void *
809save_funccal()
810{
811 struct funccall *fc;
812
813 fc = current_funccal;
814 current_funccal = NULL;
815 return (void *)fc;
816}
817
818 void
819restore_funccal(fc)
820 void *fc;
821{
822 current_funccal = (struct funccall *)fc;
823}
824
825#ifdef FEAT_FOLDING
826/*
827 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
828 * it in "*cp". Doesn't give error messages.
829 */
830 int
831eval_foldexpr(arg, cp)
832 char_u *arg;
833 int *cp;
834{
835 var retvar;
836 int retval;
837 char_u *s;
838
839 ++emsg_off;
840 ++sandbox;
841 *cp = NUL;
842 if (eval0(arg, &retvar, NULL, TRUE) == FAIL)
843 retval = 0;
844 else
845 {
846 /* If the result is a number, just return the number. */
847 if (retvar.var_type == VAR_NUMBER)
848 retval = retvar.var_val.var_number;
849 else if (retvar.var_type == VAR_UNKNOWN
850 || retvar.var_val.var_string == NULL)
851 retval = 0;
852 else
853 {
854 /* If the result is a string, check if there is a non-digit before
855 * the number. */
856 s = retvar.var_val.var_string;
857 if (!VIM_ISDIGIT(*s) && *s != '-')
858 *cp = *s++;
859 retval = atol((char *)s);
860 }
861 clear_var(&retvar);
862 }
863 --emsg_off;
864 --sandbox;
865
866 return retval;
867}
868#endif
869
870#ifdef FEAT_MAGIC_BRACES
871/*
872 * Expands out the 'magic' {}'s in a variable/function name.
873 * Note that this can call itself recursively, to deal with
874 * constructs like foo{bar}{baz}{bam}
875 * The four pointer arguments point to "foo{expre}ss{ion}bar"
876 * "in_start" ^
877 * "expr_start" ^
878 * "expr_end" ^
879 * "in_end" ^
880 *
881 * Returns a new allocated string, which the caller must free.
882 * Returns NULL for failure.
883 */
884 static char_u *
885make_expanded_name(in_start, expr_start, expr_end, in_end)
886 char_u *in_start;
887 char_u *expr_start;
888 char_u *expr_end;
889 char_u *in_end;
890{
891 char_u c1;
892 char_u *retval = NULL;
893 char_u *temp_result;
894 char_u *nextcmd = NULL;
895
896 if (expr_end == NULL || in_end == NULL)
897 return NULL;
898 *expr_start = NUL;
899 *expr_end = NUL;
900 c1 = *in_end;
901 *in_end = NUL;
902
903 temp_result = eval_to_string(expr_start + 1, &nextcmd);
904 if (temp_result != NULL && nextcmd == NULL)
905 {
906 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
907 + (in_end - expr_end) + 1));
908
909 if (retval != NULL)
910 {
911 STRCPY(retval, in_start);
912 STRCAT(retval, temp_result);
913 STRCAT(retval, expr_end + 1);
914 }
915 }
916 vim_free(temp_result);
917
918 *in_end = c1; /* put char back for error messages */
919 *expr_start = '{';
920 *expr_end = '}';
921
922 if (retval != NULL)
923 {
924 temp_result = find_name_end(retval, &expr_start, &expr_end);
925 if (expr_start != NULL)
926 {
927 /* Further expansion! */
928 temp_result = make_expanded_name(retval, expr_start,
929 expr_end, temp_result);
930 vim_free(retval);
931 retval = temp_result;
932 }
933 }
934
935 return retval;
936
937}
938#endif /* FEAT_MAGIC_BRACES */
939
940/*
941 * ":let var = expr" assignment command.
942 * ":let var" list one variable value
943 * ":let" list all variable values
944 */
945 void
946ex_let(eap)
947 exarg_T *eap;
948{
949 char_u *arg = eap->arg;
950 char_u *expr;
951 char_u *name;
952 VAR varp;
953 var retvar;
954 char_u *p;
955 int c1 = 0, c2;
956 int i;
957 char_u *expr_start;
958 char_u *expr_end;
959 char_u *name_end;
960
961 name_end = find_name_end(arg, &expr_start, &expr_end);
962 expr = vim_strchr(name_end, '=');
963 if (expr == NULL)
964 {
965 if (ends_excmd(*arg))
966 {
967 if (!eap->skip)
968 {
969 /*
970 * List all variables.
971 */
972 for (i = 0; i < variables.ga_len && !got_int; ++i)
973 if (VAR_ENTRY(i).var_name != NULL)
974 list_one_var(&VAR_ENTRY(i), (char_u *)"");
975 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
976 if (BVAR_ENTRY(i).var_name != NULL)
977 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
978 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
979 if (WVAR_ENTRY(i).var_name != NULL)
980 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
981 for (i = 0; i < VV_LEN && !got_int; ++i)
982 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
983 list_vim_var(i);
984 }
985 }
986 else
987 {
988 int error = FALSE;
989
990 /*
991 * List variables.
992 */
993 while (!ends_excmd(*arg) && !got_int)
994 {
995 char_u *temp_string = NULL;
996 int arg_len;
997
998 /* Find the end of the name. */
999 name_end = find_name_end(arg, &expr_start, &expr_end);
1000
1001 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1002 {
1003 emsg_severe = TRUE;
1004 EMSG(_(e_trailing));
1005 break;
1006 }
1007 if (!error && !eap->skip)
1008 {
1009#ifdef FEAT_MAGIC_BRACES
1010 if (expr_start != NULL)
1011 {
1012 temp_string = make_expanded_name(arg, expr_start,
1013 expr_end, name_end);
1014 if (temp_string == NULL)
1015 {
1016 /*
1017 * Report an invalid expression in braces, unless
1018 * the expression evaluation has been cancelled due
1019 * to an aborting error, an interrupt, or an
1020 * exception.
1021 */
1022 if (!aborting())
1023 {
1024 emsg_severe = TRUE;
1025 EMSG2(_(e_invarg2), arg);
1026 break;
1027 }
1028 error = TRUE;
1029 arg = skipwhite(name_end);
1030 continue;
1031 }
1032 arg = temp_string;
1033 arg_len = STRLEN(temp_string);
1034 }
1035 else
1036#endif
1037 {
1038 c1 = *name_end;
1039 *name_end = NUL;
1040 arg_len = (int)(name_end - arg);
1041 }
1042 i = find_vim_var(arg, arg_len);
1043 if (i >= 0)
1044 list_vim_var(i);
1045 else if (STRCMP("b:changedtick", arg) == 0)
1046 {
1047 char_u numbuf[NUMBUFLEN];
1048
1049 sprintf((char *)numbuf, "%ld",
1050 (long)curbuf->b_changedtick);
1051 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1052 VAR_NUMBER, numbuf);
1053 }
1054 else
1055 {
1056 varp = find_var(arg, FALSE);
1057 if (varp == NULL)
1058 {
1059 /* Skip further arguments but do continue to
1060 * search for a trailing command. */
1061 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1062 error = TRUE;
1063 }
1064 else
1065 {
1066 name = vim_strchr(arg, ':');
1067 if (name != NULL)
1068 {
1069 /* "a:" vars have no name stored, use whole
1070 * arg */
1071 if (arg[0] == 'a' && arg[1] == ':')
1072 c2 = NUL;
1073 else
1074 {
1075 c2 = *++name;
1076 *name = NUL;
1077 }
1078 list_one_var(varp, arg);
1079 if (c2 != NUL)
1080 *name = c2;
1081 }
1082 else
1083 list_one_var(varp, (char_u *)"");
1084 }
1085 }
1086#ifdef FEAT_MAGIC_BRACES
1087 if (expr_start != NULL)
1088 vim_free(temp_string);
1089 else
1090#endif
1091 *name_end = c1;
1092 }
1093 arg = skipwhite(name_end);
1094 }
1095 }
1096 eap->nextcmd = check_nextcmd(arg);
1097 }
1098 else
1099 {
1100 if (eap->skip)
1101 ++emsg_skip;
1102 i = eval0(expr + 1, &retvar, &eap->nextcmd, !eap->skip);
1103 if (eap->skip)
1104 {
1105 if (i != FAIL)
1106 clear_var(&retvar);
1107 --emsg_skip;
1108 }
1109 else if (i != FAIL)
1110 {
1111 /*
1112 * ":let $VAR = expr": Set environment variable.
1113 */
1114 if (*arg == '$')
1115 {
1116 int len;
1117 int cc;
1118
1119 /* Find the end of the name. */
1120 ++arg;
1121 name = arg;
1122 len = get_env_len(&arg);
1123 if (len == 0)
1124 EMSG2(_(e_invarg2), name - 1);
1125 else
1126 {
1127 if (*skipwhite(arg) != '=')
1128 EMSG(_(e_letunexp));
1129 else
1130 {
1131 cc = name[len];
1132 name[len] = NUL;
1133 p = get_var_string(&retvar);
1134 vim_setenv(name, p);
1135 if (STRICMP(name, "HOME") == 0)
1136 init_homedir();
1137 else if (didset_vim && STRICMP(name, "VIM") == 0)
1138 didset_vim = FALSE;
1139 else if (didset_vimruntime
1140 && STRICMP(name, "VIMRUNTIME") == 0)
1141 didset_vimruntime = FALSE;
1142 name[len] = cc;
1143 }
1144 }
1145 }
1146
1147 /*
1148 * ":let &option = expr": Set option value.
1149 * ":let &l:option = expr": Set local option value.
1150 * ":let &g:option = expr": Set global option value.
1151 */
1152 else if (*arg == '&')
1153 {
1154 int opt_flags;
1155
1156 /*
1157 * Find the end of the name;
1158 */
1159 p = find_option_end(&arg, &opt_flags);
1160 if (p == NULL || *skipwhite(p) != '=')
1161 EMSG(_(e_letunexp));
1162 else
1163 {
1164 c1 = *p;
1165 *p = NUL;
1166 set_option_value(arg, get_var_number(&retvar),
1167 get_var_string(&retvar), opt_flags);
1168 *p = c1; /* put back for error messages */
1169 }
1170 }
1171
1172 /*
1173 * ":let @r = expr": Set register contents.
1174 */
1175 else if (*arg == '@')
1176 {
1177 ++arg;
1178 if (*skipwhite(arg + 1) != '=')
1179 EMSG(_(e_letunexp));
1180 else
1181 write_reg_contents(*arg == '@' ? '"' : *arg,
1182 get_var_string(&retvar), -1, FALSE);
1183 }
1184
1185 /*
1186 * ":let var = expr": Set internal variable.
1187 */
1188 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1189 {
1190 /* Find the end of the name. */
1191 p = find_name_end(arg, &expr_start, &expr_end);
1192
1193 if (*skipwhite(p) != '=')
1194 EMSG(_(e_letunexp));
1195 else if (p - arg == 13
1196 && STRNCMP(arg, "b:changedtick", 13) == 0)
1197 EMSG2(_(e_readonlyvar), arg);
1198#ifdef FEAT_MAGIC_BRACES
1199 else if (expr_start != NULL)
1200 {
1201 char_u *temp_string;
1202
1203 temp_string = make_expanded_name(arg, expr_start,
1204 expr_end, p);
1205 if (temp_string == NULL)
1206 {
1207 /*
1208 * Report an invalid expression in braces, unless the
1209 * expression evaluation has been cancelled due to an
1210 * aborting error, an interrupt, or an exception.
1211 */
1212 if (!aborting())
1213 EMSG2(_(e_invarg2), arg);
1214 }
1215 else
1216 {
1217 set_var(temp_string, &retvar);
1218 vim_free(temp_string);
1219 }
1220 }
1221#endif
1222 else
1223 {
1224 c1 = *p;
1225 *p = NUL;
1226 set_var(arg, &retvar);
1227 *p = c1; /* put char back for error messages */
1228 }
1229 }
1230
1231 else
1232 {
1233 EMSG2(_(e_invarg2), arg);
1234 }
1235
1236 clear_var(&retvar);
1237 }
1238 }
1239}
1240
1241#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1242
1243 void
1244set_context_for_expression(xp, arg, cmdidx)
1245 expand_T *xp;
1246 char_u *arg;
1247 cmdidx_T cmdidx;
1248{
1249 int got_eq = FALSE;
1250 int c;
1251
1252 xp->xp_context = cmdidx == CMD_let ? EXPAND_USER_VARS
1253 : cmdidx == CMD_call ? EXPAND_FUNCTIONS
1254 : EXPAND_EXPRESSION;
1255 while ((xp->xp_pattern = vim_strpbrk(arg,
1256 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1257 {
1258 c = *xp->xp_pattern;
1259 if (c == '&')
1260 {
1261 c = xp->xp_pattern[1];
1262 if (c == '&')
1263 {
1264 ++xp->xp_pattern;
1265 xp->xp_context = cmdidx != CMD_let || got_eq
1266 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1267 }
1268 else if (c != ' ')
1269 xp->xp_context = EXPAND_SETTINGS;
1270 }
1271 else if (c == '$')
1272 {
1273 /* environment variable */
1274 xp->xp_context = EXPAND_ENV_VARS;
1275 }
1276 else if (c == '=')
1277 {
1278 got_eq = TRUE;
1279 xp->xp_context = EXPAND_EXPRESSION;
1280 }
1281 else if (c == '<'
1282 && xp->xp_context == EXPAND_FUNCTIONS
1283 && vim_strchr(xp->xp_pattern, '(') == NULL)
1284 {
1285 /* Function name can start with "<SNR>" */
1286 break;
1287 }
1288 else if (cmdidx != CMD_let || got_eq)
1289 {
1290 if (c == '"') /* string */
1291 {
1292 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1293 if (c == '\\' && xp->xp_pattern[1] != NUL)
1294 ++xp->xp_pattern;
1295 xp->xp_context = EXPAND_NOTHING;
1296 }
1297 else if (c == '\'') /* literal string */
1298 {
1299 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1300 /* skip */ ;
1301 xp->xp_context = EXPAND_NOTHING;
1302 }
1303 else if (c == '|')
1304 {
1305 if (xp->xp_pattern[1] == '|')
1306 {
1307 ++xp->xp_pattern;
1308 xp->xp_context = EXPAND_EXPRESSION;
1309 }
1310 else
1311 xp->xp_context = EXPAND_COMMANDS;
1312 }
1313 else
1314 xp->xp_context = EXPAND_EXPRESSION;
1315 }
1316 else
1317 xp->xp_context = EXPAND_NOTHING;
1318 arg = xp->xp_pattern;
1319 if (*arg != NUL)
1320 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1321 /* skip */ ;
1322 }
1323 xp->xp_pattern = arg;
1324}
1325
1326#endif /* FEAT_CMDL_COMPL */
1327
1328/*
1329 * ":1,25call func(arg1, arg2)" function call.
1330 */
1331 void
1332ex_call(eap)
1333 exarg_T *eap;
1334{
1335 char_u *arg = eap->arg;
1336 char_u *startarg;
1337 char_u *alias;
1338 char_u *name;
1339 var retvar;
1340 int len;
1341 linenr_T lnum;
1342 int doesrange;
1343 int failed = FALSE;
1344
1345 name = arg;
1346 len = get_func_len(&arg, &alias, !eap->skip);
1347 if (len == 0)
1348 goto end;
1349 if (alias != NULL)
1350 name = alias;
1351
1352 startarg = arg;
1353 retvar.var_type = VAR_UNKNOWN; /* clear_var() uses this */
1354
1355 if (*startarg != '(')
1356 {
1357 EMSG2(_("E107: Missing braces: %s"), name);
1358 goto end;
1359 }
1360
1361 /*
1362 * When skipping, evaluate the function once, to find the end of the
1363 * arguments.
1364 * When the function takes a range, this is discovered after the first
1365 * call, and the loop is broken.
1366 */
1367 if (eap->skip)
1368 {
1369 ++emsg_skip;
1370 lnum = eap->line2; /* do it once, also with an invalid range */
1371 }
1372 else
1373 lnum = eap->line1;
1374 for ( ; lnum <= eap->line2; ++lnum)
1375 {
1376 if (!eap->skip && eap->addr_count > 0)
1377 {
1378 curwin->w_cursor.lnum = lnum;
1379 curwin->w_cursor.col = 0;
1380 }
1381 arg = startarg;
1382 if (get_func_var(name, len, &retvar, &arg,
1383 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
1384 {
1385 failed = TRUE;
1386 break;
1387 }
1388 clear_var(&retvar);
1389 if (doesrange || eap->skip)
1390 break;
1391 /* Stop when immediately aborting on error, or when an interrupt
1392 * occurred or an exception was thrown but not caught. get_func_var()
1393 * returned OK, so that the check for trailing characters below is
1394 * executed. */
1395 if (aborting())
1396 break;
1397 }
1398 if (eap->skip)
1399 --emsg_skip;
1400
1401 if (!failed)
1402 {
1403 /* Check for trailing illegal characters and a following command. */
1404 if (!ends_excmd(*arg))
1405 {
1406 emsg_severe = TRUE;
1407 EMSG(_(e_trailing));
1408 }
1409 else
1410 eap->nextcmd = check_nextcmd(arg);
1411 }
1412
1413end:
1414 if (alias != NULL)
1415 vim_free(alias);
1416}
1417
1418/*
1419 * ":unlet[!] var1 ... " command.
1420 */
1421 void
1422ex_unlet(eap)
1423 exarg_T *eap;
1424{
1425 char_u *arg = eap->arg;
1426 char_u *name_end;
1427 char_u cc;
1428 char_u *expr_start;
1429 char_u *expr_end;
1430 int error = FALSE;
1431
1432 do
1433 {
1434 /* Find the end of the name. */
1435 name_end = find_name_end(arg, &expr_start, &expr_end);
1436
1437 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1438 {
1439 emsg_severe = TRUE;
1440 EMSG(_(e_trailing));
1441 break;
1442 }
1443
1444 if (!error && !eap->skip)
1445 {
1446#ifdef FEAT_MAGIC_BRACES
1447 if (expr_start != NULL)
1448 {
1449 char_u *temp_string;
1450
1451 temp_string = make_expanded_name(arg, expr_start,
1452 expr_end, name_end);
1453 if (temp_string == NULL)
1454 {
1455 /*
1456 * Report an invalid expression in braces, unless the
1457 * expression evaluation has been cancelled due to an
1458 * aborting error, an interrupt, or an exception.
1459 */
1460 if (!aborting())
1461 {
1462 emsg_severe = TRUE;
1463 EMSG2(_(e_invarg2), arg);
1464 break;
1465 }
1466 error = TRUE;
1467 }
1468 else
1469 {
1470 if (do_unlet(temp_string) == FAIL && !eap->forceit)
1471 {
1472 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
1473 error = TRUE;
1474 }
1475 vim_free(temp_string);
1476 }
1477 }
1478 else
1479#endif
1480 {
1481 cc = *name_end;
1482 *name_end = NUL;
1483
1484 if (do_unlet(arg) == FAIL && !eap->forceit)
1485 {
1486 EMSG2(_("E108: No such variable: \"%s\""), arg);
1487 error = TRUE;
1488 }
1489
1490 *name_end = cc;
1491 }
1492 }
1493 arg = skipwhite(name_end);
1494 } while (!ends_excmd(*arg));
1495
1496 eap->nextcmd = check_nextcmd(arg);
1497}
1498
1499/*
1500 * "unlet" a variable. Return OK if it existed, FAIL if not.
1501 */
1502 int
1503do_unlet(name)
1504 char_u *name;
1505{
1506 VAR v;
1507
1508 v = find_var(name, TRUE);
1509 if (v != NULL)
1510 {
1511 var_free_one(v);
1512 return OK;
1513 }
1514 return FAIL;
1515}
1516
1517#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1518/*
1519 * Delete all "menutrans_" variables.
1520 */
1521 void
1522del_menutrans_vars()
1523{
1524 int i;
1525
1526 for (i = 0; i < variables.ga_len; ++i)
1527 if (VAR_ENTRY(i).var_name != NULL
1528 && STRNCMP(VAR_ENTRY(i).var_name, "menutrans_", 10) == 0)
1529 var_free_one(&VAR_ENTRY(i));
1530}
1531#endif
1532
1533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1534
1535/*
1536 * Local string buffer for the next two functions to store a variable name
1537 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1538 * get_user_var_name().
1539 */
1540
1541static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
1542
1543static char_u *varnamebuf = NULL;
1544static int varnamebuflen = 0;
1545
1546/*
1547 * Function to concatenate a prefix and a variable name.
1548 */
1549 static char_u *
1550cat_prefix_varname(prefix, name)
1551 int prefix;
1552 char_u *name;
1553{
1554 int len;
1555
1556 len = (int)STRLEN(name) + 3;
1557 if (len > varnamebuflen)
1558 {
1559 vim_free(varnamebuf);
1560 len += 10; /* some additional space */
1561 varnamebuf = alloc(len);
1562 if (varnamebuf == NULL)
1563 {
1564 varnamebuflen = 0;
1565 return NULL;
1566 }
1567 varnamebuflen = len;
1568 }
1569 *varnamebuf = prefix;
1570 varnamebuf[1] = ':';
1571 STRCPY(varnamebuf + 2, name);
1572 return varnamebuf;
1573}
1574
1575/*
1576 * Function given to ExpandGeneric() to obtain the list of user defined
1577 * (global/buffer/window/built-in) variable names.
1578 */
1579/*ARGSUSED*/
1580 char_u *
1581get_user_var_name(xp, idx)
1582 expand_T *xp;
1583 int idx;
1584{
1585 static int gidx;
1586 static int bidx;
1587 static int widx;
1588 static int vidx;
1589 char_u *name;
1590
1591 if (idx == 0)
1592 gidx = bidx = widx = vidx = 0;
1593 if (gidx < variables.ga_len) /* Global variables */
1594 {
1595 while ((name = VAR_ENTRY(gidx++).var_name) == NULL
1596 && gidx < variables.ga_len)
1597 /* skip */;
1598 if (name != NULL)
1599 {
1600 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1601 return cat_prefix_varname('g', name);
1602 else
1603 return name;
1604 }
1605 }
1606 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
1607 {
1608 while ((name = BVAR_ENTRY(bidx++).var_name) == NULL
1609 && bidx < curbuf->b_vars.ga_len)
1610 /* skip */;
1611 if (name != NULL)
1612 return cat_prefix_varname('b', name);
1613 }
1614 if (bidx == curbuf->b_vars.ga_len)
1615 {
1616 ++bidx;
1617 return (char_u *)"b:changedtick";
1618 }
1619 if (widx < curwin->w_vars.ga_len) /* Current window variables */
1620 {
1621 while ((name = WVAR_ENTRY(widx++).var_name) == NULL
1622 && widx < curwin->w_vars.ga_len)
1623 /* skip */;
1624 if (name != NULL)
1625 return cat_prefix_varname('w', name);
1626 }
1627 if (vidx < VV_LEN) /* Built-in variables */
1628 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
1629
1630 vim_free(varnamebuf);
1631 varnamebuf = NULL;
1632 varnamebuflen = 0;
1633 return NULL;
1634}
1635
1636#endif /* FEAT_CMDL_COMPL */
1637
1638/*
1639 * types for expressions.
1640 */
1641typedef enum
1642{
1643 TYPE_UNKNOWN = 0
1644 , TYPE_EQUAL /* == */
1645 , TYPE_NEQUAL /* != */
1646 , TYPE_GREATER /* > */
1647 , TYPE_GEQUAL /* >= */
1648 , TYPE_SMALLER /* < */
1649 , TYPE_SEQUAL /* <= */
1650 , TYPE_MATCH /* =~ */
1651 , TYPE_NOMATCH /* !~ */
1652} exptype_T;
1653
1654/*
1655 * The "evaluate" argument: When FALSE, the argument is only parsed but not
1656 * executed. The function may return OK, but the retvar will be of type
1657 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1658 */
1659
1660/*
1661 * Handle zero level expression.
1662 * This calls eval1() and handles error message and nextcmd.
1663 * Return OK or FAIL.
1664 */
1665 static int
1666eval0(arg, retvar, nextcmd, evaluate)
1667 char_u *arg;
1668 VAR retvar;
1669 char_u **nextcmd;
1670 int evaluate;
1671{
1672 int ret;
1673 char_u *p;
1674
1675 p = skipwhite(arg);
1676 ret = eval1(&p, retvar, evaluate);
1677 if (ret == FAIL || !ends_excmd(*p))
1678 {
1679 if (ret != FAIL)
1680 clear_var(retvar);
1681 /*
1682 * Report the invalid expression unless the expression evaluation has
1683 * been cancelled due to an aborting error, an interrupt, or an
1684 * exception.
1685 */
1686 if (!aborting())
1687 EMSG2(_(e_invexpr2), arg);
1688 ret = FAIL;
1689 }
1690 if (nextcmd != NULL)
1691 *nextcmd = check_nextcmd(p);
1692
1693 return ret;
1694}
1695
1696/*
1697 * Handle top level expression:
1698 * expr1 ? expr0 : expr0
1699 *
1700 * "arg" must point to the first non-white of the expression.
1701 * "arg" is advanced to the next non-white after the recognized expression.
1702 *
1703 * Return OK or FAIL.
1704 */
1705 static int
1706eval1(arg, retvar, evaluate)
1707 char_u **arg;
1708 VAR retvar;
1709 int evaluate;
1710{
1711 int result;
1712 var var2;
1713
1714 /*
1715 * Get the first variable.
1716 */
1717 if (eval2(arg, retvar, evaluate) == FAIL)
1718 return FAIL;
1719
1720 if ((*arg)[0] == '?')
1721 {
1722 result = FALSE;
1723 if (evaluate)
1724 {
1725 if (get_var_number(retvar) != 0)
1726 result = TRUE;
1727 clear_var(retvar);
1728 }
1729
1730 /*
1731 * Get the second variable.
1732 */
1733 *arg = skipwhite(*arg + 1);
1734 if (eval1(arg, retvar, evaluate && result) == FAIL) /* recursive! */
1735 return FAIL;
1736
1737 /*
1738 * Check for the ":".
1739 */
1740 if ((*arg)[0] != ':')
1741 {
1742 EMSG(_("E109: Missing ':' after '?'"));
1743 if (evaluate && result)
1744 clear_var(retvar);
1745 return FAIL;
1746 }
1747
1748 /*
1749 * Get the third variable.
1750 */
1751 *arg = skipwhite(*arg + 1);
1752 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
1753 {
1754 if (evaluate && result)
1755 clear_var(retvar);
1756 return FAIL;
1757 }
1758 if (evaluate && !result)
1759 *retvar = var2;
1760 }
1761
1762 return OK;
1763}
1764
1765/*
1766 * Handle first level expression:
1767 * expr2 || expr2 || expr2 logical OR
1768 *
1769 * "arg" must point to the first non-white of the expression.
1770 * "arg" is advanced to the next non-white after the recognized expression.
1771 *
1772 * Return OK or FAIL.
1773 */
1774 static int
1775eval2(arg, retvar, evaluate)
1776 char_u **arg;
1777 VAR retvar;
1778 int evaluate;
1779{
1780 var var2;
1781 long result;
1782 int first;
1783
1784 /*
1785 * Get the first variable.
1786 */
1787 if (eval3(arg, retvar, evaluate) == FAIL)
1788 return FAIL;
1789
1790 /*
1791 * Repeat until there is no following "||".
1792 */
1793 first = TRUE;
1794 result = FALSE;
1795 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1796 {
1797 if (evaluate && first)
1798 {
1799 if (get_var_number(retvar) != 0)
1800 result = TRUE;
1801 clear_var(retvar);
1802 first = FALSE;
1803 }
1804
1805 /*
1806 * Get the second variable.
1807 */
1808 *arg = skipwhite(*arg + 2);
1809 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1810 return FAIL;
1811
1812 /*
1813 * Compute the result.
1814 */
1815 if (evaluate && !result)
1816 {
1817 if (get_var_number(&var2) != 0)
1818 result = TRUE;
1819 clear_var(&var2);
1820 }
1821 if (evaluate)
1822 {
1823 retvar->var_type = VAR_NUMBER;
1824 retvar->var_val.var_number = result;
1825 }
1826 }
1827
1828 return OK;
1829}
1830
1831/*
1832 * Handle second level expression:
1833 * expr3 && expr3 && expr3 logical AND
1834 *
1835 * "arg" must point to the first non-white of the expression.
1836 * "arg" is advanced to the next non-white after the recognized expression.
1837 *
1838 * Return OK or FAIL.
1839 */
1840 static int
1841eval3(arg, retvar, evaluate)
1842 char_u **arg;
1843 VAR retvar;
1844 int evaluate;
1845{
1846 var var2;
1847 long result;
1848 int first;
1849
1850 /*
1851 * Get the first variable.
1852 */
1853 if (eval4(arg, retvar, evaluate) == FAIL)
1854 return FAIL;
1855
1856 /*
1857 * Repeat until there is no following "&&".
1858 */
1859 first = TRUE;
1860 result = TRUE;
1861 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1862 {
1863 if (evaluate && first)
1864 {
1865 if (get_var_number(retvar) == 0)
1866 result = FALSE;
1867 clear_var(retvar);
1868 first = FALSE;
1869 }
1870
1871 /*
1872 * Get the second variable.
1873 */
1874 *arg = skipwhite(*arg + 2);
1875 if (eval4(arg, &var2, evaluate && result) == FAIL)
1876 return FAIL;
1877
1878 /*
1879 * Compute the result.
1880 */
1881 if (evaluate && result)
1882 {
1883 if (get_var_number(&var2) == 0)
1884 result = FALSE;
1885 clear_var(&var2);
1886 }
1887 if (evaluate)
1888 {
1889 retvar->var_type = VAR_NUMBER;
1890 retvar->var_val.var_number = result;
1891 }
1892 }
1893
1894 return OK;
1895}
1896
1897/*
1898 * Handle third level expression:
1899 * var1 == var2
1900 * var1 =~ var2
1901 * var1 != var2
1902 * var1 !~ var2
1903 * var1 > var2
1904 * var1 >= var2
1905 * var1 < var2
1906 * var1 <= var2
1907 *
1908 * "arg" must point to the first non-white of the expression.
1909 * "arg" is advanced to the next non-white after the recognized expression.
1910 *
1911 * Return OK or FAIL.
1912 */
1913 static int
1914eval4(arg, retvar, evaluate)
1915 char_u **arg;
1916 VAR retvar;
1917 int evaluate;
1918{
1919 var var2;
1920 char_u *p;
1921 int i;
1922 exptype_T type = TYPE_UNKNOWN;
1923 int len = 2;
1924 long n1, n2;
1925 char_u *s1, *s2;
1926 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1927 regmatch_T regmatch;
1928 int ic;
1929 char_u *save_cpo;
1930
1931 /*
1932 * Get the first variable.
1933 */
1934 if (eval5(arg, retvar, evaluate) == FAIL)
1935 return FAIL;
1936
1937 p = *arg;
1938 switch (p[0])
1939 {
1940 case '=': if (p[1] == '=')
1941 type = TYPE_EQUAL;
1942 else if (p[1] == '~')
1943 type = TYPE_MATCH;
1944 break;
1945 case '!': if (p[1] == '=')
1946 type = TYPE_NEQUAL;
1947 else if (p[1] == '~')
1948 type = TYPE_NOMATCH;
1949 break;
1950 case '>': if (p[1] != '=')
1951 {
1952 type = TYPE_GREATER;
1953 len = 1;
1954 }
1955 else
1956 type = TYPE_GEQUAL;
1957 break;
1958 case '<': if (p[1] != '=')
1959 {
1960 type = TYPE_SMALLER;
1961 len = 1;
1962 }
1963 else
1964 type = TYPE_SEQUAL;
1965 break;
1966 }
1967
1968 /*
1969 * If there is a comparitive operator, use it.
1970 */
1971 if (type != TYPE_UNKNOWN)
1972 {
1973 /* extra question mark appended: ignore case */
1974 if (p[len] == '?')
1975 {
1976 ic = TRUE;
1977 ++len;
1978 }
1979 /* extra '#' appended: match case */
1980 else if (p[len] == '#')
1981 {
1982 ic = FALSE;
1983 ++len;
1984 }
1985 /* nothing appened: use 'ignorecase' */
1986 else
1987 ic = p_ic;
1988
1989 /*
1990 * Get the second variable.
1991 */
1992 *arg = skipwhite(p + len);
1993 if (eval5(arg, &var2, evaluate) == FAIL)
1994 {
1995 clear_var(retvar);
1996 return FAIL;
1997 }
1998
1999 if (evaluate)
2000 {
2001 /*
2002 * If one of the two variables is a number, compare as a number.
2003 * When using "=~" or "!~", always compare as string.
2004 */
2005 if ((retvar->var_type == VAR_NUMBER || var2.var_type == VAR_NUMBER)
2006 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2007 {
2008 n1 = get_var_number(retvar);
2009 n2 = get_var_number(&var2);
2010 switch (type)
2011 {
2012 case TYPE_EQUAL: n1 = (n1 == n2); break;
2013 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2014 case TYPE_GREATER: n1 = (n1 > n2); break;
2015 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2016 case TYPE_SMALLER: n1 = (n1 < n2); break;
2017 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2018 case TYPE_UNKNOWN:
2019 case TYPE_MATCH:
2020 case TYPE_NOMATCH: break; /* avoid gcc warning */
2021 }
2022 }
2023 else
2024 {
2025 s1 = get_var_string_buf(retvar, buf1);
2026 s2 = get_var_string_buf(&var2, buf2);
2027 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2028 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2029 else
2030 i = 0;
2031 n1 = FALSE;
2032 switch (type)
2033 {
2034 case TYPE_EQUAL: n1 = (i == 0); break;
2035 case TYPE_NEQUAL: n1 = (i != 0); break;
2036 case TYPE_GREATER: n1 = (i > 0); break;
2037 case TYPE_GEQUAL: n1 = (i >= 0); break;
2038 case TYPE_SMALLER: n1 = (i < 0); break;
2039 case TYPE_SEQUAL: n1 = (i <= 0); break;
2040
2041 case TYPE_MATCH:
2042 case TYPE_NOMATCH:
2043 /* avoid 'l' flag in 'cpoptions' */
2044 save_cpo = p_cpo;
2045 p_cpo = (char_u *)"";
2046 regmatch.regprog = vim_regcomp(s2,
2047 RE_MAGIC + RE_STRING);
2048 regmatch.rm_ic = ic;
2049 if (regmatch.regprog != NULL)
2050 {
2051 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2052 vim_free(regmatch.regprog);
2053 if (type == TYPE_NOMATCH)
2054 n1 = !n1;
2055 }
2056 p_cpo = save_cpo;
2057 break;
2058
2059 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2060 }
2061 }
2062 clear_var(retvar);
2063 clear_var(&var2);
2064 retvar->var_type = VAR_NUMBER;
2065 retvar->var_val.var_number = n1;
2066 }
2067 }
2068
2069 return OK;
2070}
2071
2072/*
2073 * Handle fourth level expression:
2074 * + number addition
2075 * - number subtraction
2076 * . string concatenation
2077 *
2078 * "arg" must point to the first non-white of the expression.
2079 * "arg" is advanced to the next non-white after the recognized expression.
2080 *
2081 * Return OK or FAIL.
2082 */
2083 static int
2084eval5(arg, retvar, evaluate)
2085 char_u **arg;
2086 VAR retvar;
2087 int evaluate;
2088{
2089 var var2;
2090 int op;
2091 long n1, n2;
2092 char_u *s1, *s2;
2093 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2094 char_u *p;
2095
2096 /*
2097 * Get the first variable.
2098 */
2099 if (eval6(arg, retvar, evaluate) == FAIL)
2100 return FAIL;
2101
2102 /*
2103 * Repeat computing, until no '+', '-' or '.' is following.
2104 */
2105 for (;;)
2106 {
2107 op = **arg;
2108 if (op != '+' && op != '-' && op != '.')
2109 break;
2110
2111 /*
2112 * Get the second variable.
2113 */
2114 *arg = skipwhite(*arg + 1);
2115 if (eval6(arg, &var2, evaluate) == FAIL)
2116 {
2117 clear_var(retvar);
2118 return FAIL;
2119 }
2120
2121 if (evaluate)
2122 {
2123 /*
2124 * Compute the result.
2125 */
2126 if (op == '.')
2127 {
2128 s1 = get_var_string_buf(retvar, buf1);
2129 s2 = get_var_string_buf(&var2, buf2);
2130 op = (int)STRLEN(s1);
2131 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2132 if (p != NULL)
2133 {
2134 STRCPY(p, s1);
2135 STRCPY(p + op, s2);
2136 }
2137 clear_var(retvar);
2138 retvar->var_type = VAR_STRING;
2139 retvar->var_val.var_string = p;
2140 }
2141 else
2142 {
2143 n1 = get_var_number(retvar);
2144 n2 = get_var_number(&var2);
2145 clear_var(retvar);
2146 if (op == '+')
2147 n1 = n1 + n2;
2148 else
2149 n1 = n1 - n2;
2150 retvar->var_type = VAR_NUMBER;
2151 retvar->var_val.var_number = n1;
2152 }
2153 clear_var(&var2);
2154 }
2155 }
2156 return OK;
2157}
2158
2159/*
2160 * Handle fifth level expression:
2161 * * number multiplication
2162 * / number division
2163 * % number modulo
2164 *
2165 * "arg" must point to the first non-white of the expression.
2166 * "arg" is advanced to the next non-white after the recognized expression.
2167 *
2168 * Return OK or FAIL.
2169 */
2170 static int
2171eval6(arg, retvar, evaluate)
2172 char_u **arg;
2173 VAR retvar;
2174 int evaluate;
2175{
2176 var var2;
2177 int op;
2178 long n1, n2;
2179
2180 /*
2181 * Get the first variable.
2182 */
2183 if (eval7(arg, retvar, evaluate) == FAIL)
2184 return FAIL;
2185
2186 /*
2187 * Repeat computing, until no '*', '/' or '%' is following.
2188 */
2189 for (;;)
2190 {
2191 op = **arg;
2192 if (op != '*' && op != '/' && op != '%')
2193 break;
2194
2195 if (evaluate)
2196 {
2197 n1 = get_var_number(retvar);
2198 clear_var(retvar);
2199 }
2200 else
2201 n1 = 0;
2202
2203 /*
2204 * Get the second variable.
2205 */
2206 *arg = skipwhite(*arg + 1);
2207 if (eval7(arg, &var2, evaluate) == FAIL)
2208 return FAIL;
2209
2210 if (evaluate)
2211 {
2212 n2 = get_var_number(&var2);
2213 clear_var(&var2);
2214
2215 /*
2216 * Compute the result.
2217 */
2218 if (op == '*')
2219 n1 = n1 * n2;
2220 else if (op == '/')
2221 {
2222 if (n2 == 0) /* give an error message? */
2223 n1 = 0x7fffffffL;
2224 else
2225 n1 = n1 / n2;
2226 }
2227 else
2228 {
2229 if (n2 == 0) /* give an error message? */
2230 n1 = 0;
2231 else
2232 n1 = n1 % n2;
2233 }
2234 retvar->var_type = VAR_NUMBER;
2235 retvar->var_val.var_number = n1;
2236 }
2237 }
2238
2239 return OK;
2240}
2241
2242/*
2243 * Handle sixth level expression:
2244 * number number constant
2245 * "string" string contstant
2246 * 'string' literal string contstant
2247 * &option-name option value
2248 * @r register contents
2249 * identifier variable value
2250 * function() function call
2251 * $VAR environment variable
2252 * (expression) nested expression
2253 *
2254 * Also handle:
2255 * ! in front logical NOT
2256 * - in front unary minus
2257 * + in front unary plus (ignored)
2258 * trailing [] subscript in String
2259 *
2260 * "arg" must point to the first non-white of the expression.
2261 * "arg" is advanced to the next non-white after the recognized expression.
2262 *
2263 * Return OK or FAIL.
2264 */
2265 static int
2266eval7(arg, retvar, evaluate)
2267 char_u **arg;
2268 VAR retvar;
2269 int evaluate;
2270{
2271 var var2;
2272 long n;
2273 int len;
2274 char_u *s;
2275 int val;
2276 char_u *start_leader, *end_leader;
2277 int ret = OK;
2278 char_u *alias;
2279
2280 /*
2281 * Initialise variable so that clear_var() can't mistake this for a string
2282 * and free a string that isn't there.
2283 */
2284 retvar->var_type = VAR_UNKNOWN;
2285
2286 /*
2287 * Skip '!' and '-' characters. They are handled later.
2288 */
2289 start_leader = *arg;
2290 while (**arg == '!' || **arg == '-' || **arg == '+')
2291 *arg = skipwhite(*arg + 1);
2292 end_leader = *arg;
2293
2294 switch (**arg)
2295 {
2296 /*
2297 * Number constant.
2298 */
2299 case '0':
2300 case '1':
2301 case '2':
2302 case '3':
2303 case '4':
2304 case '5':
2305 case '6':
2306 case '7':
2307 case '8':
2308 case '9':
2309 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
2310 *arg += len;
2311 if (evaluate)
2312 {
2313 retvar->var_type = VAR_NUMBER;
2314 retvar->var_val.var_number = n;
2315 }
2316 break;
2317
2318 /*
2319 * String constant: "string".
2320 */
2321 case '"': ret = get_string_var(arg, retvar, evaluate);
2322 break;
2323
2324 /*
2325 * Literal string constant: 'string'.
2326 */
2327 case '\'': ret = get_lit_string_var(arg, retvar, evaluate);
2328 break;
2329
2330 /*
2331 * Option value: &name
2332 */
2333 case '&': ret = get_option_var(arg, retvar, evaluate);
2334 break;
2335
2336 /*
2337 * Environment variable: $VAR.
2338 */
2339 case '$': ret = get_env_var(arg, retvar, evaluate);
2340 break;
2341
2342 /*
2343 * Register contents: @r.
2344 */
2345 case '@': ++*arg;
2346 if (evaluate)
2347 {
2348 retvar->var_type = VAR_STRING;
2349 retvar->var_val.var_string = get_reg_contents(**arg, FALSE);
2350 }
2351 if (**arg != NUL)
2352 ++*arg;
2353 break;
2354
2355 /*
2356 * nested expression: (expression).
2357 */
2358 case '(': *arg = skipwhite(*arg + 1);
2359 ret = eval1(arg, retvar, evaluate); /* recursive! */
2360 if (**arg == ')')
2361 ++*arg;
2362 else if (ret == OK)
2363 {
2364 EMSG(_("E110: Missing ')'"));
2365 clear_var(retvar);
2366 ret = FAIL;
2367 }
2368 break;
2369
2370 /*
2371 * Must be a variable or function name then.
2372 */
2373 default: s = *arg;
2374 len = get_func_len(arg, &alias, evaluate);
2375 if (alias != NULL)
2376 s = alias;
2377
2378 if (len == 0)
2379 ret = FAIL;
2380 else
2381 {
2382 if (**arg == '(') /* recursive! */
2383 {
2384 ret = get_func_var(s, len, retvar, arg,
2385 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
2386 &len, evaluate);
2387 /* Stop the expression evaluation when immediately
2388 * aborting on error, or when an interrupt occurred or
2389 * an exception was thrown but not caught. */
2390 if (aborting())
2391 {
2392 if (ret == OK)
2393 clear_var(retvar);
2394 ret = FAIL;
2395 }
2396 }
2397 else if (evaluate)
2398 ret = get_var_var(s, len, retvar);
2399 }
2400
2401 if (alias != NULL)
2402 vim_free(alias);
2403
2404 break;
2405 }
2406 *arg = skipwhite(*arg);
2407
2408 /*
2409 * Handle expr[expr] subscript.
2410 */
2411 if (**arg == '[' && ret == OK)
2412 {
2413 /*
2414 * Get the variable from inside the [].
2415 */
2416 *arg = skipwhite(*arg + 1);
2417 if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
2418 {
2419 clear_var(retvar);
2420 return FAIL;
2421 }
2422
2423 /* Check for the ']'. */
2424 if (**arg != ']')
2425 {
2426 EMSG(_("E111: Missing ']'"));
2427 clear_var(retvar);
2428 clear_var(&var2);
2429 return FAIL;
2430 }
2431
2432 if (evaluate)
2433 {
2434 n = get_var_number(&var2);
2435 clear_var(&var2);
2436
2437 /*
2438 * The resulting variable is a string of a single character.
2439 * If the index is too big or negative, the result is empty.
2440 */
2441 s = get_var_string(retvar);
2442 if (n >= (long)STRLEN(s) || n < 0)
2443 s = NULL;
2444 else
2445 s = vim_strnsave(s + n, 1);
2446 clear_var(retvar);
2447 retvar->var_type = VAR_STRING;
2448 retvar->var_val.var_string = s;
2449 }
2450 *arg = skipwhite(*arg + 1); /* skip the ']' */
2451 }
2452
2453 /*
2454 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2455 */
2456 if (ret == OK && evaluate && end_leader > start_leader)
2457 {
2458 val = get_var_number(retvar);
2459 while (end_leader > start_leader)
2460 {
2461 --end_leader;
2462 if (*end_leader == '!')
2463 val = !val;
2464 else if (*end_leader == '-')
2465 val = -val;
2466 }
2467 clear_var(retvar);
2468 retvar->var_type = VAR_NUMBER;
2469 retvar->var_val.var_number = val;
2470 }
2471
2472 return ret;
2473}
2474
2475/*
2476 * Get an option value.
2477 * "arg" points to the '&' or '+' before the option name.
2478 * "arg" is advanced to character after the option name.
2479 * Return OK or FAIL.
2480 */
2481 static int
2482get_option_var(arg, retvar, evaluate)
2483 char_u **arg;
2484 VAR retvar; /* when NULL, only check if option exists */
2485 int evaluate;
2486{
2487 char_u *option_end;
2488 long numval;
2489 char_u *stringval;
2490 int opt_type;
2491 int c;
2492 int working = (**arg == '+'); /* has("+option") */
2493 int ret = OK;
2494 int opt_flags;
2495
2496 /*
2497 * Isolate the option name and find its value.
2498 */
2499 option_end = find_option_end(arg, &opt_flags);
2500 if (option_end == NULL)
2501 {
2502 if (retvar != NULL)
2503 EMSG2(_("E112: Option name missing: %s"), *arg);
2504 return FAIL;
2505 }
2506
2507 if (!evaluate)
2508 {
2509 *arg = option_end;
2510 return OK;
2511 }
2512
2513 c = *option_end;
2514 *option_end = NUL;
2515 opt_type = get_option_value(*arg, &numval,
2516 retvar == NULL ? NULL : &stringval, opt_flags);
2517
2518 if (opt_type == -3) /* invalid name */
2519 {
2520 if (retvar != NULL)
2521 EMSG2(_("E113: Unknown option: %s"), *arg);
2522 ret = FAIL;
2523 }
2524 else if (retvar != NULL)
2525 {
2526 if (opt_type == -2) /* hidden string option */
2527 {
2528 retvar->var_type = VAR_STRING;
2529 retvar->var_val.var_string = NULL;
2530 }
2531 else if (opt_type == -1) /* hidden number option */
2532 {
2533 retvar->var_type = VAR_NUMBER;
2534 retvar->var_val.var_number = 0;
2535 }
2536 else if (opt_type == 1) /* number option */
2537 {
2538 retvar->var_type = VAR_NUMBER;
2539 retvar->var_val.var_number = numval;
2540 }
2541 else /* string option */
2542 {
2543 retvar->var_type = VAR_STRING;
2544 retvar->var_val.var_string = stringval;
2545 }
2546 }
2547 else if (working && (opt_type == -2 || opt_type == -1))
2548 ret = FAIL;
2549
2550 *option_end = c; /* put back for error messages */
2551 *arg = option_end;
2552
2553 return ret;
2554}
2555
2556/*
2557 * Allocate a variable for a string constant.
2558 * Return OK or FAIL.
2559 */
2560 static int
2561get_string_var(arg, retvar, evaluate)
2562 char_u **arg;
2563 VAR retvar;
2564 int evaluate;
2565{
2566 char_u *p;
2567 char_u *name;
2568 int i;
2569 int extra = 0;
2570
2571 /*
2572 * Find the end of the string, skipping backslashed characters.
2573 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002574 for (p = *arg + 1; *p && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 if (*p == '\\' && p[1] != NUL)
2577 {
2578 ++p;
2579 /* A "\<x>" form occupies at least 4 characters, and produces up
2580 * to 6 characters: reserve space for 2 extra */
2581 if (*p == '<')
2582 extra += 2;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586 if (*p != '"')
2587 {
2588 EMSG2(_("E114: Missing quote: %s"), *arg);
2589 return FAIL;
2590 }
2591
2592 /* If only parsing, set *arg and return here */
2593 if (!evaluate)
2594 {
2595 *arg = p + 1;
2596 return OK;
2597 }
2598
2599 /*
2600 * Copy the string into allocated memory, handling backslashed
2601 * characters.
2602 */
2603 name = alloc((unsigned)(p - *arg + extra));
2604 if (name == NULL)
2605 return FAIL;
2606
2607 i = 0;
2608 for (p = *arg + 1; *p && *p != '"'; ++p)
2609 {
2610 if (*p == '\\')
2611 {
2612 switch (*++p)
2613 {
2614 case 'b': name[i++] = BS; break;
2615 case 'e': name[i++] = ESC; break;
2616 case 'f': name[i++] = FF; break;
2617 case 'n': name[i++] = NL; break;
2618 case 'r': name[i++] = CAR; break;
2619 case 't': name[i++] = TAB; break;
2620
2621 case 'X': /* hex: "\x1", "\x12" */
2622 case 'x':
2623 case 'u': /* Unicode: "\u0023" */
2624 case 'U':
2625 if (vim_isxdigit(p[1]))
2626 {
2627 int n, nr;
2628 int c = toupper(*p);
2629
2630 if (c == 'X')
2631 n = 2;
2632 else
2633 n = 4;
2634 nr = 0;
2635 while (--n >= 0 && vim_isxdigit(p[1]))
2636 {
2637 ++p;
2638 nr = (nr << 4) + hex2nr(*p);
2639 }
2640#ifdef FEAT_MBYTE
2641 /* For "\u" store the number according to
2642 * 'encoding'. */
2643 if (c != 'X')
2644 i += (*mb_char2bytes)(nr, name + i);
2645 else
2646#endif
2647 name[i++] = nr;
2648 }
2649 else
2650 name[i++] = *p;
2651 break;
2652
2653 /* octal: "\1", "\12", "\123" */
2654 case '0':
2655 case '1':
2656 case '2':
2657 case '3':
2658 case '4':
2659 case '5':
2660 case '6':
2661 case '7': name[i] = *p - '0';
2662 if (p[1] >= '0' && p[1] <= '7')
2663 {
2664 ++p;
2665 name[i] = (name[i] << 3) + *p - '0';
2666 if (p[1] >= '0' && p[1] <= '7')
2667 {
2668 ++p;
2669 name[i] = (name[i] << 3) + *p - '0';
2670 }
2671 }
2672 ++i;
2673 break;
2674
2675 /* Special key, e.g.: "\<C-W>" */
2676 case '<': extra = trans_special(&p, name + i, TRUE);
2677 if (extra != 0)
2678 {
2679 i += extra;
2680 --p;
2681 break;
2682 }
2683 /* FALLTHROUGH */
2684
2685 default: name[i++] = *p;
2686 break;
2687 }
2688 }
2689 else
2690 name[i++] = *p;
2691
2692#ifdef FEAT_MBYTE
2693 /* For a multi-byte character copy the bytes after the first one. */
2694 if (has_mbyte)
2695 {
2696 int l = (*mb_ptr2len_check)(p);
2697
2698 while (--l > 0)
2699 name[i++] = *++p;
2700 }
2701#endif
2702 }
2703 name[i] = NUL;
2704 *arg = p + 1;
2705
2706 retvar->var_type = VAR_STRING;
2707 retvar->var_val.var_string = name;
2708
2709 return OK;
2710}
2711
2712/*
2713 * Allocate a variable for an backtick-string constant.
2714 * Return OK or FAIL.
2715 */
2716 static int
2717get_lit_string_var(arg, retvar, evaluate)
2718 char_u **arg;
2719 VAR retvar;
2720 int evaluate;
2721{
2722 char_u *p;
2723 char_u *name;
2724
2725 /*
2726 * Find the end of the string.
2727 */
2728 p = vim_strchr(*arg + 1, '\'');
2729 if (p == NULL)
2730 {
2731 EMSG2(_("E115: Missing quote: %s"), *arg);
2732 return FAIL;
2733 }
2734
2735 if (evaluate)
2736 {
2737 /*
2738 * Copy the string into allocated memory.
2739 */
2740 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
2741 if (name == NULL)
2742 return FAIL;
2743
2744 retvar->var_type = VAR_STRING;
2745 retvar->var_val.var_string = name;
2746 }
2747
2748 *arg = p + 1;
2749
2750 return OK;
2751}
2752
2753/*
2754 * Get the value of an environment variable.
2755 * "arg" is pointing to the '$'. It is advanced to after the name.
2756 * If the environment variable was not set, silently assume it is empty.
2757 * Always return OK.
2758 */
2759 static int
2760get_env_var(arg, retvar, evaluate)
2761 char_u **arg;
2762 VAR retvar;
2763 int evaluate;
2764{
2765 char_u *string = NULL;
2766 int len;
2767 int cc;
2768 char_u *name;
2769
2770 ++*arg;
2771 name = *arg;
2772 len = get_env_len(arg);
2773 if (evaluate)
2774 {
2775 if (len != 0)
2776 {
2777 cc = name[len];
2778 name[len] = NUL;
2779 /* first try mch_getenv(), fast for normal environment vars */
2780 string = mch_getenv(name);
2781 if (string != NULL && *string != NUL)
2782 string = vim_strsave(string);
2783 else
2784 {
2785 /* next try expanding things like $VIM and ${HOME} */
2786 string = expand_env_save(name - 1);
2787 if (string != NULL && *string == '$')
2788 {
2789 vim_free(string);
2790 string = NULL;
2791 }
2792 }
2793 name[len] = cc;
2794 }
2795 retvar->var_type = VAR_STRING;
2796 retvar->var_val.var_string = string;
2797 }
2798
2799 return OK;
2800}
2801
2802/*
2803 * Array with names and number of arguments of all internal functions
2804 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
2805 */
2806static struct fst
2807{
2808 char *f_name; /* function name */
2809 char f_min_argc; /* minimal number of arguments */
2810 char f_max_argc; /* maximal number of arguments */
2811 void (*f_func) __ARGS((VAR args, VAR rvar)); /* impl. function */
2812} functions[] =
2813{
2814 {"append", 2, 2, f_append},
2815 {"argc", 0, 0, f_argc},
2816 {"argidx", 0, 0, f_argidx},
2817 {"argv", 1, 1, f_argv},
2818 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002819 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {"bufexists", 1, 1, f_bufexists},
2821 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
2822 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
2823 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
2824 {"buflisted", 1, 1, f_buflisted},
2825 {"bufloaded", 1, 1, f_bufloaded},
2826 {"bufname", 1, 1, f_bufname},
2827 {"bufnr", 1, 1, f_bufnr},
2828 {"bufwinnr", 1, 1, f_bufwinnr},
2829 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00002830 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {"char2nr", 1, 1, f_char2nr},
2832 {"cindent", 1, 1, f_cindent},
2833 {"col", 1, 1, f_col},
2834 {"confirm", 1, 4, f_confirm},
2835 {"cscope_connection",0,3, f_cscope_connection},
2836 {"cursor", 2, 2, f_cursor},
2837 {"delete", 1, 1, f_delete},
2838 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00002839 {"diff_filler", 1, 1, f_diff_filler},
2840 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {"escape", 2, 2, f_escape},
2842 {"eventhandler", 0, 0, f_eventhandler},
2843 {"executable", 1, 1, f_executable},
2844 {"exists", 1, 1, f_exists},
2845 {"expand", 1, 2, f_expand},
2846 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
2847 {"filereadable", 1, 1, f_filereadable},
2848 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002849 {"finddir", 1, 3, f_finddir},
2850 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {"fnamemodify", 2, 2, f_fnamemodify},
2852 {"foldclosed", 1, 1, f_foldclosed},
2853 {"foldclosedend", 1, 1, f_foldclosedend},
2854 {"foldlevel", 1, 1, f_foldlevel},
2855 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002856 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {"foreground", 0, 0, f_foreground},
2858 {"getbufvar", 2, 2, f_getbufvar},
2859 {"getchar", 0, 1, f_getchar},
2860 {"getcharmod", 0, 0, f_getcharmod},
2861 {"getcmdline", 0, 0, f_getcmdline},
2862 {"getcmdpos", 0, 0, f_getcmdpos},
2863 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002864 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002865 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {"getfsize", 1, 1, f_getfsize},
2867 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002868 {"getftype", 1, 1, f_getftype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {"getline", 1, 1, f_getline},
2870 {"getreg", 0, 1, f_getreg},
2871 {"getregtype", 0, 1, f_getregtype},
2872 {"getwinposx", 0, 0, f_getwinposx},
2873 {"getwinposy", 0, 0, f_getwinposy},
2874 {"getwinvar", 2, 2, f_getwinvar},
2875 {"glob", 1, 1, f_glob},
2876 {"globpath", 2, 2, f_globpath},
2877 {"has", 1, 1, f_has},
2878 {"hasmapto", 1, 2, f_hasmapto},
2879 {"highlightID", 1, 1, f_hlID}, /* obsolete */
2880 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
2881 {"histadd", 2, 2, f_histadd},
2882 {"histdel", 1, 2, f_histdel},
2883 {"histget", 1, 2, f_histget},
2884 {"histnr", 1, 1, f_histnr},
2885 {"hlID", 1, 1, f_hlID},
2886 {"hlexists", 1, 1, f_hlexists},
2887 {"hostname", 0, 0, f_hostname},
2888 {"iconv", 3, 3, f_iconv},
2889 {"indent", 1, 1, f_indent},
2890 {"input", 1, 2, f_input},
2891 {"inputdialog", 1, 3, f_inputdialog},
2892 {"inputrestore", 0, 0, f_inputrestore},
2893 {"inputsave", 0, 0, f_inputsave},
2894 {"inputsecret", 1, 2, f_inputsecret},
2895 {"isdirectory", 1, 1, f_isdirectory},
2896 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
2897 {"libcall", 3, 3, f_libcall},
2898 {"libcallnr", 3, 3, f_libcallnr},
2899 {"line", 1, 1, f_line},
2900 {"line2byte", 1, 1, f_line2byte},
2901 {"lispindent", 1, 1, f_lispindent},
2902 {"localtime", 0, 0, f_localtime},
2903 {"maparg", 1, 2, f_maparg},
2904 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00002905 {"match", 2, 4, f_match},
2906 {"matchend", 2, 4, f_matchend},
2907 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {"mode", 0, 0, f_mode},
2909 {"nextnonblank", 1, 1, f_nextnonblank},
2910 {"nr2char", 1, 1, f_nr2char},
2911 {"prevnonblank", 1, 1, f_prevnonblank},
2912 {"remote_expr", 2, 3, f_remote_expr},
2913 {"remote_foreground", 1, 1, f_remote_foreground},
2914 {"remote_peek", 1, 2, f_remote_peek},
2915 {"remote_read", 1, 1, f_remote_read},
2916 {"remote_send", 2, 3, f_remote_send},
2917 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00002918 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {"resolve", 1, 1, f_resolve},
2920 {"search", 1, 2, f_search},
2921 {"searchpair", 3, 5, f_searchpair},
2922 {"server2client", 2, 2, f_server2client},
2923 {"serverlist", 0, 0, f_serverlist},
2924 {"setbufvar", 3, 3, f_setbufvar},
2925 {"setcmdpos", 1, 1, f_setcmdpos},
2926 {"setline", 2, 2, f_setline},
2927 {"setreg", 2, 3, f_setreg},
2928 {"setwinvar", 3, 3, f_setwinvar},
2929 {"simplify", 1, 1, f_simplify},
2930#ifdef HAVE_STRFTIME
2931 {"strftime", 1, 2, f_strftime},
2932#endif
2933 {"stridx", 2, 2, f_stridx},
2934 {"strlen", 1, 1, f_strlen},
2935 {"strpart", 2, 3, f_strpart},
2936 {"strridx", 2, 2, f_strridx},
2937 {"strtrans", 1, 1, f_strtrans},
2938 {"submatch", 1, 1, f_submatch},
2939 {"substitute", 4, 4, f_substitute},
2940 {"synID", 3, 3, f_synID},
2941 {"synIDattr", 2, 3, f_synIDattr},
2942 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002943 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {"tempname", 0, 0, f_tempname},
2945 {"tolower", 1, 1, f_tolower},
2946 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00002947 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {"type", 1, 1, f_type},
2949 {"virtcol", 1, 1, f_virtcol},
2950 {"visualmode", 0, 1, f_visualmode},
2951 {"winbufnr", 1, 1, f_winbufnr},
2952 {"wincol", 0, 0, f_wincol},
2953 {"winheight", 1, 1, f_winheight},
2954 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002955 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {"winrestcmd", 0, 0, f_winrestcmd},
2957 {"winwidth", 1, 1, f_winwidth},
2958};
2959
2960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2961
2962/*
2963 * Function given to ExpandGeneric() to obtain the list of internal
2964 * or user defined function names.
2965 */
2966 char_u *
2967get_function_name(xp, idx)
2968 expand_T *xp;
2969 int idx;
2970{
2971 static int intidx = -1;
2972 char_u *name;
2973
2974 if (idx == 0)
2975 intidx = -1;
2976 if (intidx < 0)
2977 {
2978 name = get_user_func_name(xp, idx);
2979 if (name != NULL)
2980 return name;
2981 }
2982 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
2983 {
2984 STRCPY(IObuff, functions[intidx].f_name);
2985 STRCAT(IObuff, "(");
2986 if (functions[intidx].f_max_argc == 0)
2987 STRCAT(IObuff, ")");
2988 return IObuff;
2989 }
2990
2991 return NULL;
2992}
2993
2994/*
2995 * Function given to ExpandGeneric() to obtain the list of internal or
2996 * user defined variable or function names.
2997 */
2998/*ARGSUSED*/
2999 char_u *
3000get_expr_name(xp, idx)
3001 expand_T *xp;
3002 int idx;
3003{
3004 static int intidx = -1;
3005 char_u *name;
3006
3007 if (idx == 0)
3008 intidx = -1;
3009 if (intidx < 0)
3010 {
3011 name = get_function_name(xp, idx);
3012 if (name != NULL)
3013 return name;
3014 }
3015 return get_user_var_name(xp, ++intidx);
3016}
3017
3018#endif /* FEAT_CMDL_COMPL */
3019
3020/*
3021 * Find internal function in table above.
3022 * Return index, or -1 if not found
3023 */
3024 static int
3025find_internal_func(name)
3026 char_u *name; /* name of the function */
3027{
3028 int first = 0;
3029 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
3030 int cmp;
3031 int x;
3032
3033 /*
3034 * Find the function name in the table. Binary search.
3035 */
3036 while (first <= last)
3037 {
3038 x = first + ((unsigned)(last - first) >> 1);
3039 cmp = STRCMP(name, functions[x].f_name);
3040 if (cmp < 0)
3041 last = x - 1;
3042 else if (cmp > 0)
3043 first = x + 1;
3044 else
3045 return x;
3046 }
3047 return -1;
3048}
3049
3050/*
3051 * Allocate a variable for the result of a function.
3052 * Return OK or FAIL.
3053 */
3054 static int
3055get_func_var(name, len, retvar, arg, firstline, lastline, doesrange, evaluate)
3056 char_u *name; /* name of the function */
3057 int len; /* length of "name" */
3058 VAR retvar;
3059 char_u **arg; /* argument, pointing to the '(' */
3060 linenr_T firstline; /* first line of range */
3061 linenr_T lastline; /* last line of range */
3062 int *doesrange; /* return: function handled range */
3063 int evaluate;
3064{
3065 char_u *argp;
3066 int ret = OK;
3067#define MAX_FUNC_ARGS 20
3068 var argvars[MAX_FUNC_ARGS]; /* vars for arguments */
3069 int argcount = 0; /* number of arguments found */
3070
3071 /*
3072 * Get the arguments.
3073 */
3074 argp = *arg;
3075 while (argcount < MAX_FUNC_ARGS)
3076 {
3077 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
3078 if (*argp == ')' || *argp == ',' || *argp == NUL)
3079 break;
3080 argvars[argcount].var_name = NULL; /* the name is not stored */
3081 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
3082 {
3083 ret = FAIL;
3084 break;
3085 }
3086 ++argcount;
3087 if (*argp != ',')
3088 break;
3089 }
3090 if (*argp == ')')
3091 ++argp;
3092 else
3093 ret = FAIL;
3094
3095 if (ret == OK)
3096 ret = call_func(name, len, retvar, argcount, argvars,
3097 firstline, lastline, doesrange, evaluate);
3098 else if (!aborting())
3099 EMSG2(_("E116: Invalid arguments for function %s"), name);
3100
3101 while (--argcount >= 0)
3102 clear_var(&argvars[argcount]);
3103
3104 *arg = skipwhite(argp);
3105 return ret;
3106}
3107
3108
3109/*
3110 * Call a function with its resolved parameters
3111 * Return OK or FAIL.
3112 */
3113 static int
3114call_func(name, len, retvar, argcount, argvars, firstline, lastline,
3115 doesrange, evaluate)
3116 char_u *name; /* name of the function */
3117 int len; /* length of "name" */
3118 VAR retvar; /* return value goes here */
3119 int argcount; /* number of "argvars" */
3120 VAR argvars; /* vars for arguments */
3121 linenr_T firstline; /* first line of range */
3122 linenr_T lastline; /* last line of range */
3123 int *doesrange; /* return: function handled range */
3124 int evaluate;
3125{
3126 int ret = FAIL;
3127 static char *errors[] =
3128 {N_("E117: Unknown function: %s"),
3129 N_("E118: Too many arguments for function: %s"),
3130 N_("E119: Not enough arguments for function: %s"),
3131 N_("E120: Using <SID> not in a script context: %s"),
3132 };
3133#define ERROR_UNKNOWN 0
3134#define ERROR_TOOMANY 1
3135#define ERROR_TOOFEW 2
3136#define ERROR_SCRIPT 3
3137#define ERROR_NONE 4
3138#define ERROR_OTHER 5
3139 int error = ERROR_NONE;
3140 int i;
3141 int llen;
3142 ufunc_T *fp;
3143 int cc;
3144#define FLEN_FIXED 40
3145 char_u fname_buf[FLEN_FIXED + 1];
3146 char_u *fname;
3147
3148 /*
3149 * In a script change <SID>name() and s:name() to K_SNR 123_name().
3150 * Change <SNR>123_name() to K_SNR 123_name().
3151 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
3152 */
3153 cc = name[len];
3154 name[len] = NUL;
3155 llen = eval_fname_script(name);
3156 if (llen > 0)
3157 {
3158 fname_buf[0] = K_SPECIAL;
3159 fname_buf[1] = KS_EXTRA;
3160 fname_buf[2] = (int)KE_SNR;
3161 i = 3;
3162 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
3163 {
3164 if (current_SID <= 0)
3165 error = ERROR_SCRIPT;
3166 else
3167 {
3168 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
3169 i = (int)STRLEN(fname_buf);
3170 }
3171 }
3172 if (i + STRLEN(name + llen) < FLEN_FIXED)
3173 {
3174 STRCPY(fname_buf + i, name + llen);
3175 fname = fname_buf;
3176 }
3177 else
3178 {
3179 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
3180 if (fname == NULL)
3181 error = ERROR_OTHER;
3182 else
3183 {
3184 mch_memmove(fname, fname_buf, (size_t)i);
3185 STRCPY(fname + i, name + llen);
3186 }
3187 }
3188 }
3189 else
3190 fname = name;
3191
3192 *doesrange = FALSE;
3193
3194
3195 /* execute the function if no errors detected and executing */
3196 if (evaluate && error == ERROR_NONE)
3197 {
3198 retvar->var_type = VAR_NUMBER; /* default is number retvar */
3199 error = ERROR_UNKNOWN;
3200
3201 if (!ASCII_ISLOWER(fname[0]))
3202 {
3203 /*
3204 * User defined function.
3205 */
3206 fp = find_func(fname);
3207#ifdef FEAT_AUTOCMD
3208 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
3209 fname, fname, TRUE, NULL)
3210#ifdef FEAT_EVAL
3211 && !aborting()
3212#endif
3213 )
3214 {
3215 /* executed an autocommand, search for function again */
3216 fp = find_func(fname);
3217 }
3218#endif
3219 if (fp != NULL)
3220 {
3221 if (fp->flags & FC_RANGE)
3222 *doesrange = TRUE;
3223 if (argcount < fp->args.ga_len)
3224 error = ERROR_TOOFEW;
3225 else if (!fp->varargs && argcount > fp->args.ga_len)
3226 error = ERROR_TOOMANY;
3227 else
3228 {
3229 /*
3230 * Call the user function.
3231 * Save and restore search patterns, script variables and
3232 * redo buffer.
3233 */
3234 save_search_patterns();
3235 saveRedobuff();
3236 ++fp->calls;
3237 call_user_func(fp, argcount, argvars, retvar,
3238 firstline, lastline);
3239 --fp->calls;
3240 restoreRedobuff();
3241 restore_search_patterns();
3242 error = ERROR_NONE;
3243 }
3244 }
3245 }
3246 else
3247 {
3248 /*
3249 * Find the function name in the table, call its implementation.
3250 */
3251 i = find_internal_func(fname);
3252 if (i >= 0)
3253 {
3254 if (argcount < functions[i].f_min_argc)
3255 error = ERROR_TOOFEW;
3256 else if (argcount > functions[i].f_max_argc)
3257 error = ERROR_TOOMANY;
3258 else
3259 {
3260 argvars[argcount].var_type = VAR_UNKNOWN;
3261 functions[i].f_func(argvars, retvar);
3262 error = ERROR_NONE;
3263 }
3264 }
3265 }
3266 /*
3267 * The function call (or "FuncUndefined" autocommand sequence) might
3268 * have been aborted by an error, an interrupt, or an explicitly thrown
3269 * exception that has not been caught so far. This situation can be
3270 * tested for by calling aborting(). For an error in an internal
3271 * function or for the "E132" error in call_user_func(), however, the
3272 * throw point at which the "force_abort" flag (temporarily reset by
3273 * emsg()) is normally updated has not been reached yet. We need to
3274 * update that flag first to make aborting() reliable.
3275 */
3276 update_force_abort();
3277 }
3278 if (error == ERROR_NONE)
3279 ret = OK;
3280
3281 /*
3282 * Report an error unless the argument evaluation or function call has been
3283 * cancelled due to an aborting error, an interrupt, or an exception.
3284 */
3285 if (error < ERROR_NONE && !aborting())
3286 EMSG2((char_u *)_(errors[error]), name);
3287
3288 name[len] = cc;
3289 if (fname != name && fname != fname_buf)
3290 vim_free(fname);
3291
3292 return ret;
3293}
3294
3295/*********************************************
3296 * Implementation of the built-in functions
3297 */
3298
3299/*
3300 * "append(lnum, string)" function
3301 */
3302 static void
3303f_append(argvars, retvar)
3304 VAR argvars;
3305 VAR retvar;
3306{
3307 long lnum;
3308
3309 lnum = get_var_lnum(argvars);
3310 retvar->var_val.var_number = 1; /* Default: Failed */
3311
3312 if (lnum >= 0
3313 && lnum <= curbuf->b_ml.ml_line_count
3314 && u_save(lnum, lnum + 1) == OK)
3315 {
3316 ml_append(lnum, get_var_string(&argvars[1]), (colnr_T)0, FALSE);
3317 if (curwin->w_cursor.lnum > lnum)
3318 ++curwin->w_cursor.lnum;
3319 appended_lines_mark(lnum, 1L);
3320 retvar->var_val.var_number = 0;
3321 }
3322}
3323
3324/*
3325 * "argc()" function
3326 */
3327/* ARGSUSED */
3328 static void
3329f_argc(argvars, retvar)
3330 VAR argvars;
3331 VAR retvar;
3332{
3333 retvar->var_val.var_number = ARGCOUNT;
3334}
3335
3336/*
3337 * "argidx()" function
3338 */
3339/* ARGSUSED */
3340 static void
3341f_argidx(argvars, retvar)
3342 VAR argvars;
3343 VAR retvar;
3344{
3345 retvar->var_val.var_number = curwin->w_arg_idx;
3346}
3347
3348/*
3349 * "argv(nr)" function
3350 */
3351 static void
3352f_argv(argvars, retvar)
3353 VAR argvars;
3354 VAR retvar;
3355{
3356 int idx;
3357
3358 idx = get_var_number(&argvars[0]);
3359 if (idx >= 0 && idx < ARGCOUNT)
3360 retvar->var_val.var_string = vim_strsave(alist_name(&ARGLIST[idx]));
3361 else
3362 retvar->var_val.var_string = NULL;
3363 retvar->var_type = VAR_STRING;
3364}
3365
3366/*
3367 * "browse(save, title, initdir, default)" function
3368 */
3369/* ARGSUSED */
3370 static void
3371f_browse(argvars, retvar)
3372 VAR argvars;
3373 VAR retvar;
3374{
3375#ifdef FEAT_BROWSE
3376 int save;
3377 char_u *title;
3378 char_u *initdir;
3379 char_u *defname;
3380 char_u buf[NUMBUFLEN];
3381 char_u buf2[NUMBUFLEN];
3382
3383 save = get_var_number(&argvars[0]);
3384 title = get_var_string(&argvars[1]);
3385 initdir = get_var_string_buf(&argvars[2], buf);
3386 defname = get_var_string_buf(&argvars[3], buf2);
3387
3388 retvar->var_val.var_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003389 do_browse(save ? BROWSE_SAVE : 0,
3390 title, defname, NULL, initdir, NULL, curbuf);
3391#else
3392 retvar->var_val.var_string = NULL;
3393#endif
3394 retvar->var_type = VAR_STRING;
3395}
3396
3397/*
3398 * "browsedir(title, initdir)" function
3399 */
3400/* ARGSUSED */
3401 static void
3402f_browsedir(argvars, retvar)
3403 VAR argvars;
3404 VAR retvar;
3405{
3406#ifdef FEAT_BROWSE
3407 char_u *title;
3408 char_u *initdir;
3409 char_u buf[NUMBUFLEN];
3410
3411 title = get_var_string(&argvars[0]);
3412 initdir = get_var_string_buf(&argvars[1], buf);
3413
3414 retvar->var_val.var_string = do_browse(BROWSE_DIR,
3415 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#else
3417 retvar->var_val.var_string = NULL;
3418#endif
3419 retvar->var_type = VAR_STRING;
3420}
3421
3422/*
3423 * Find a buffer by number or exact name.
3424 */
3425 static buf_T *
3426find_buffer(avar)
3427 VAR avar;
3428{
3429 buf_T *buf = NULL;
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);
6882 set_var(bufvarname, varp);
6883 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);
7043 set_var(winvarname, varp);
7044 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);
7196 set_var(get_var_string(&argvars[1]), &v);
7197 }
7198#else
7199 retvar->var_val.var_number = -1;
7200#endif
7201}
7202
7203/*ARGSUSED*/
7204 static void
7205f_remote_read(argvars, retvar)
7206 VAR argvars;
7207 VAR retvar;
7208{
7209 char_u *r = NULL;
7210
7211#ifdef FEAT_CLIENTSERVER
7212 if (!check_restricted() && !check_secure())
7213 {
7214# ifdef WIN32
7215 /* The server's HWND is encoded in the 'id' parameter */
7216 int n = 0;
7217
7218 sscanf(get_var_string(&argvars[0]), "%x", &n);
7219 if (n != 0)
7220 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
7221 if (r == NULL)
7222# else
7223 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
7224 serverStrToWin(get_var_string(&argvars[0])), &r, FALSE) < 0)
7225# endif
7226 EMSG(_("E277: Unable to read a server reply"));
7227 }
7228#endif
7229 retvar->var_type = VAR_STRING;
7230 retvar->var_val.var_string = r;
7231}
7232
7233/*ARGSUSED*/
7234 static void
7235f_server2client(argvars, retvar)
7236 VAR argvars;
7237 VAR retvar;
7238{
7239#ifdef FEAT_CLIENTSERVER
7240 char_u buf[NUMBUFLEN];
7241 char_u *server = get_var_string(&argvars[0]);
7242 char_u *reply = get_var_string_buf(&argvars[1], buf);
7243
7244 retvar->var_val.var_number = -1;
7245 if (check_restricted() || check_secure())
7246 return;
7247# ifdef FEAT_X11
7248 if (check_connection() == FAIL)
7249 return;
7250# endif
7251
7252 if (serverSendReply(server, reply) < 0)
7253 {
7254 EMSG(_("E258: Unable to send to client"));
7255 return;
7256 }
7257 retvar->var_val.var_number = 0;
7258#else
7259 retvar->var_val.var_number = -1;
7260#endif
7261}
7262
7263#ifdef FEAT_CLIENTSERVER
7264static void remote_common __ARGS((VAR argvars, VAR retvar, int expr));
7265
7266 static void
7267remote_common(argvars, retvar, expr)
7268 VAR argvars;
7269 VAR retvar;
7270 int expr;
7271{
7272 char_u *server_name;
7273 char_u *keys;
7274 char_u *r = NULL;
7275 char_u buf[NUMBUFLEN];
7276# ifdef WIN32
7277 HWND w;
7278# else
7279 Window w;
7280# endif
7281
7282 if (check_restricted() || check_secure())
7283 return;
7284
7285# ifdef FEAT_X11
7286 if (check_connection() == FAIL)
7287 return;
7288# endif
7289
7290 server_name = get_var_string(&argvars[0]);
7291 keys = get_var_string_buf(&argvars[1], buf);
7292# ifdef WIN32
7293 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
7294# else
7295 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
7296 < 0)
7297# endif
7298 {
7299 if (r != NULL)
7300 EMSG(r); /* sending worked but evaluation failed */
7301 else
7302 EMSG2(_("E241: Unable to send to %s"), server_name);
7303 return;
7304 }
7305
7306 retvar->var_val.var_string = r;
7307
7308 if (argvars[2].var_type != VAR_UNKNOWN)
7309 {
7310 var v;
7311 char_u str[30];
7312
7313 sprintf((char *)str, "0x%x", (unsigned int)w);
7314 v.var_type = VAR_STRING;
7315 v.var_val.var_string = vim_strsave(str);
7316 set_var(get_var_string(&argvars[2]), &v);
7317 }
7318}
7319#endif
7320
7321/*
7322 * "remote_expr()" function
7323 */
7324/*ARGSUSED*/
7325 static void
7326f_remote_expr(argvars, retvar)
7327 VAR argvars;
7328 VAR retvar;
7329{
7330 retvar->var_type = VAR_STRING;
7331 retvar->var_val.var_string = NULL;
7332#ifdef FEAT_CLIENTSERVER
7333 remote_common(argvars, retvar, TRUE);
7334#endif
7335}
7336
7337/*
7338 * "remote_send()" function
7339 */
7340/*ARGSUSED*/
7341 static void
7342f_remote_send(argvars, retvar)
7343 VAR argvars;
7344 VAR retvar;
7345{
7346 retvar->var_type = VAR_STRING;
7347 retvar->var_val.var_string = NULL;
7348#ifdef FEAT_CLIENTSERVER
7349 remote_common(argvars, retvar, FALSE);
7350#endif
7351}
7352
7353/*
7354 * "remote_foreground()" function
7355 */
7356/*ARGSUSED*/
7357 static void
7358f_remote_foreground(argvars, retvar)
7359 VAR argvars;
7360 VAR retvar;
7361{
7362 retvar->var_val.var_number = 0;
7363#ifdef FEAT_CLIENTSERVER
7364# ifdef WIN32
7365 /* On Win32 it's done in this application. */
7366 serverForeground(get_var_string(&argvars[0]));
7367# else
7368 /* Send a foreground() expression to the server. */
7369 argvars[1].var_type = VAR_STRING;
7370 argvars[1].var_val.var_string = vim_strsave((char_u *)"foreground()");
7371 argvars[2].var_type = VAR_UNKNOWN;
7372 remote_common(argvars, retvar, TRUE);
7373 vim_free(argvars[1].var_val.var_string);
7374# endif
7375#endif
7376}
7377
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007378/*
7379 * "repeat()" function
7380 */
7381/*ARGSUSED*/
7382 static void
7383f_repeat(argvars, retvar)
7384 VAR argvars;
7385 VAR retvar;
7386{
7387 char_u *p;
7388 int n;
7389 int slen;
7390 int len;
7391 char_u *r;
7392 int i;
7393
7394 p = get_var_string(&argvars[0]);
7395 n = get_var_number(&argvars[1]);
7396
7397 retvar->var_type = VAR_STRING;
7398 retvar->var_val.var_string = NULL;
7399
7400 slen = (int)STRLEN(p);
7401 len = slen * n;
7402
7403 if (len <= 0)
7404 return;
7405
7406 r = alloc(len + 1);
7407 if (r != NULL)
7408 {
7409 for (i = 0; i < n; i++)
7410 mch_memmove(r + i * slen, p, (size_t)slen);
7411 r[len] = NUL;
7412 }
7413
7414 retvar->var_val.var_string = r;
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#ifdef HAVE_STRFTIME
7418/*
7419 * "strftime({format}[, {time}])" function
7420 */
7421 static void
7422f_strftime(argvars, retvar)
7423 VAR argvars;
7424 VAR retvar;
7425{
7426 char_u result_buf[256];
7427 struct tm *curtime;
7428 time_t seconds;
7429 char_u *p;
7430
7431 retvar->var_type = VAR_STRING;
7432
7433 p = get_var_string(&argvars[0]);
7434 if (argvars[1].var_type == VAR_UNKNOWN)
7435 seconds = time(NULL);
7436 else
7437 seconds = (time_t)get_var_number(&argvars[1]);
7438 curtime = localtime(&seconds);
7439 /* MSVC returns NULL for an invalid value of seconds. */
7440 if (curtime == NULL)
7441 retvar->var_val.var_string = vim_strsave((char_u *)_("(Invalid)"));
7442 else
7443 {
7444# ifdef FEAT_MBYTE
7445 vimconv_T conv;
7446 char_u *enc;
7447
7448 conv.vc_type = CONV_NONE;
7449 enc = enc_locale();
7450 convert_setup(&conv, p_enc, enc);
7451 if (conv.vc_type != CONV_NONE)
7452 p = string_convert(&conv, p, NULL);
7453# endif
7454 if (p != NULL)
7455 (void)strftime((char *)result_buf, sizeof(result_buf),
7456 (char *)p, curtime);
7457 else
7458 result_buf[0] = NUL;
7459
7460# ifdef FEAT_MBYTE
7461 if (conv.vc_type != CONV_NONE)
7462 vim_free(p);
7463 convert_setup(&conv, enc, p_enc);
7464 if (conv.vc_type != CONV_NONE)
7465 retvar->var_val.var_string =
7466 string_convert(&conv, result_buf, NULL);
7467 else
7468# endif
7469 retvar->var_val.var_string = vim_strsave(result_buf);
7470
7471# ifdef FEAT_MBYTE
7472 /* Release conversion descriptors */
7473 convert_setup(&conv, NULL, NULL);
7474 vim_free(enc);
7475# endif
7476 }
7477}
7478#endif
7479
7480/*
7481 * "stridx()" function
7482 */
7483 static void
7484f_stridx(argvars, retvar)
7485 VAR argvars;
7486 VAR retvar;
7487{
7488 char_u buf[NUMBUFLEN];
7489 char_u *needle;
7490 char_u *haystack;
7491 char_u *pos;
7492
7493 needle = get_var_string(&argvars[1]);
7494 haystack = get_var_string_buf(&argvars[0], buf);
7495 pos = (char_u *)strstr((char *)haystack, (char *)needle);
7496
7497 if (pos == NULL)
7498 retvar->var_val.var_number = -1;
7499 else
7500 retvar->var_val.var_number = (varnumber_T) (pos - haystack);
7501}
7502
7503/*
7504 * "strridx()" function
7505 */
7506 static void
7507f_strridx(argvars, retvar)
7508 VAR argvars;
7509 VAR retvar;
7510{
7511 char_u buf[NUMBUFLEN];
7512 char_u *needle;
7513 char_u *haystack;
7514 char_u *rest;
7515 char_u *lastmatch = NULL;
7516
7517 needle = get_var_string(&argvars[1]);
7518 haystack = get_var_string_buf(&argvars[0], buf);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007519 if (*needle == NUL)
7520 /* Empty string matches past the end. */
7521 lastmatch = haystack + STRLEN(haystack);
7522 else
7523 for (rest = haystack; *rest != '\0'; ++rest)
7524 {
7525 rest = (char_u *)strstr((char *)rest, (char *)needle);
7526 if (rest == NULL)
7527 break;
7528 lastmatch = rest;
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
7531 if (lastmatch == NULL)
7532 retvar->var_val.var_number = -1;
7533 else
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007534 retvar->var_val.var_number = (varnumber_T)(lastmatch - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535}
7536
7537/*
7538 * "strlen()" function
7539 */
7540 static void
7541f_strlen(argvars, retvar)
7542 VAR argvars;
7543 VAR retvar;
7544{
7545 retvar->var_val.var_number = (varnumber_T) (STRLEN(get_var_string(&argvars[0])));
7546}
7547
7548/*
7549 * "strpart()" function
7550 */
7551 static void
7552f_strpart(argvars, retvar)
7553 VAR argvars;
7554 VAR retvar;
7555{
7556 char_u *p;
7557 int n;
7558 int len;
7559 int slen;
7560
7561 p = get_var_string(&argvars[0]);
7562 slen = (int)STRLEN(p);
7563
7564 n = get_var_number(&argvars[1]);
7565 if (argvars[2].var_type != VAR_UNKNOWN)
7566 len = get_var_number(&argvars[2]);
7567 else
7568 len = slen - n; /* default len: all bytes that are available. */
7569
7570 /*
7571 * Only return the overlap between the specified part and the actual
7572 * string.
7573 */
7574 if (n < 0)
7575 {
7576 len += n;
7577 n = 0;
7578 }
7579 else if (n > slen)
7580 n = slen;
7581 if (len < 0)
7582 len = 0;
7583 else if (n + len > slen)
7584 len = slen - n;
7585
7586 retvar->var_type = VAR_STRING;
7587 retvar->var_val.var_string = vim_strnsave(p + n, len);
7588}
7589
7590/*
7591 * "strtrans()" function
7592 */
7593 static void
7594f_strtrans(argvars, retvar)
7595 VAR argvars;
7596 VAR retvar;
7597{
7598 retvar->var_type = VAR_STRING;
7599 retvar->var_val.var_string = transstr(get_var_string(&argvars[0]));
7600}
7601
7602/*
7603 * "synID(line, col, trans)" function
7604 */
7605/*ARGSUSED*/
7606 static void
7607f_synID(argvars, retvar)
7608 VAR argvars;
7609 VAR retvar;
7610{
7611 int id = 0;
7612#ifdef FEAT_SYN_HL
7613 long line;
7614 long col;
7615 int trans;
7616
7617 line = get_var_lnum(argvars);
7618 col = get_var_number(&argvars[1]) - 1;
7619 trans = get_var_number(&argvars[2]);
7620
7621 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
7622 && col >= 0 && col < (long)STRLEN(ml_get(line)))
7623 id = syn_get_id(line, col, trans);
7624#endif
7625
7626 retvar->var_val.var_number = id;
7627}
7628
7629/*
7630 * "synIDattr(id, what [, mode])" function
7631 */
7632/*ARGSUSED*/
7633 static void
7634f_synIDattr(argvars, retvar)
7635 VAR argvars;
7636 VAR retvar;
7637{
7638 char_u *p = NULL;
7639#ifdef FEAT_SYN_HL
7640 int id;
7641 char_u *what;
7642 char_u *mode;
7643 char_u modebuf[NUMBUFLEN];
7644 int modec;
7645
7646 id = get_var_number(&argvars[0]);
7647 what = get_var_string(&argvars[1]);
7648 if (argvars[2].var_type != VAR_UNKNOWN)
7649 {
7650 mode = get_var_string_buf(&argvars[2], modebuf);
7651 modec = TOLOWER_ASC(mode[0]);
7652 if (modec != 't' && modec != 'c'
7653#ifdef FEAT_GUI
7654 && modec != 'g'
7655#endif
7656 )
7657 modec = 0; /* replace invalid with current */
7658 }
7659 else
7660 {
7661#ifdef FEAT_GUI
7662 if (gui.in_use)
7663 modec = 'g';
7664 else
7665#endif
7666 if (t_colors > 1)
7667 modec = 'c';
7668 else
7669 modec = 't';
7670 }
7671
7672
7673 switch (TOLOWER_ASC(what[0]))
7674 {
7675 case 'b':
7676 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
7677 p = highlight_color(id, what, modec);
7678 else /* bold */
7679 p = highlight_has_attr(id, HL_BOLD, modec);
7680 break;
7681
7682 case 'f': /* fg[#] */
7683 p = highlight_color(id, what, modec);
7684 break;
7685
7686 case 'i':
7687 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
7688 p = highlight_has_attr(id, HL_INVERSE, modec);
7689 else /* italic */
7690 p = highlight_has_attr(id, HL_ITALIC, modec);
7691 break;
7692
7693 case 'n': /* name */
7694 p = get_highlight_name(NULL, id - 1);
7695 break;
7696
7697 case 'r': /* reverse */
7698 p = highlight_has_attr(id, HL_INVERSE, modec);
7699 break;
7700
7701 case 's': /* standout */
7702 p = highlight_has_attr(id, HL_STANDOUT, modec);
7703 break;
7704
7705 case 'u': /* underline */
7706 p = highlight_has_attr(id, HL_UNDERLINE, modec);
7707 break;
7708 }
7709
7710 if (p != NULL)
7711 p = vim_strsave(p);
7712#endif
7713 retvar->var_type = VAR_STRING;
7714 retvar->var_val.var_string = p;
7715}
7716
7717/*
7718 * "synIDtrans(id)" function
7719 */
7720/*ARGSUSED*/
7721 static void
7722f_synIDtrans(argvars, retvar)
7723 VAR argvars;
7724 VAR retvar;
7725{
7726 int id;
7727
7728#ifdef FEAT_SYN_HL
7729 id = get_var_number(&argvars[0]);
7730
7731 if (id > 0)
7732 id = syn_get_final_id(id);
7733 else
7734#endif
7735 id = 0;
7736
7737 retvar->var_val.var_number = id;
7738}
7739
7740/*
7741 * "system()" function
7742 */
7743 static void
7744f_system(argvars, retvar)
7745 VAR argvars;
7746 VAR retvar;
7747{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007748 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007750 char_u *infile = NULL;
7751 char_u buf[NUMBUFLEN];
7752 int err = FALSE;
7753 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007755 if (argvars[1].var_type != VAR_UNKNOWN)
7756 {
7757 /*
7758 * Write the string to a temp file, to be used for input of the shell
7759 * command.
7760 */
7761 if ((infile = vim_tempname('i')) == NULL)
7762 {
7763 EMSG(_(e_notmp));
7764 return;
7765 }
7766
7767 fd = mch_fopen((char *)infile, WRITEBIN);
7768 if (fd == NULL)
7769 {
7770 EMSG2(_(e_notopen), infile);
7771 goto done;
7772 }
7773 p = get_var_string_buf(&argvars[1], buf);
7774 if (fwrite(p, STRLEN(p), 1, fd) != 1)
7775 err = TRUE;
7776 if (fclose(fd) != 0)
7777 err = TRUE;
7778 if (err)
7779 {
7780 EMSG(_("E677: Error writing temp file"));
7781 goto done;
7782 }
7783 }
7784
7785 res = get_cmd_output(get_var_string(&argvars[0]), infile, SHELL_SILENT);
7786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787#ifdef USE_CR
7788 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007789 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 {
7791 char_u *s;
7792
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007793 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {
7795 if (*s == CAR)
7796 *s = NL;
7797 }
7798 }
7799#else
7800# ifdef USE_CRNL
7801 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007802 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {
7804 char_u *s, *d;
7805
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007806 d = res;
7807 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {
7809 if (s[0] == CAR && s[1] == NL)
7810 ++s;
7811 *d++ = *s;
7812 }
7813 *d = NUL;
7814 }
7815# endif
7816#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007817
7818done:
7819 if (infile != NULL)
7820 {
7821 mch_remove(infile);
7822 vim_free(infile);
7823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 retvar->var_type = VAR_STRING;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007825 retvar->var_val.var_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826}
7827
7828/*
7829 * "submatch()" function
7830 */
7831 static void
7832f_submatch(argvars, retvar)
7833 VAR argvars;
7834 VAR retvar;
7835{
7836 retvar->var_type = VAR_STRING;
7837 retvar->var_val.var_string = reg_submatch((int)get_var_number(&argvars[0]));
7838}
7839
7840/*
7841 * "substitute()" function
7842 */
7843 static void
7844f_substitute(argvars, retvar)
7845 VAR argvars;
7846 VAR retvar;
7847{
7848 char_u patbuf[NUMBUFLEN];
7849 char_u subbuf[NUMBUFLEN];
7850 char_u flagsbuf[NUMBUFLEN];
7851
7852 retvar->var_type = VAR_STRING;
7853 retvar->var_val.var_string = do_string_sub(
7854 get_var_string(&argvars[0]),
7855 get_var_string_buf(&argvars[1], patbuf),
7856 get_var_string_buf(&argvars[2], subbuf),
7857 get_var_string_buf(&argvars[3], flagsbuf));
7858}
7859
7860/*
7861 * "tempname()" function
7862 */
7863/*ARGSUSED*/
7864 static void
7865f_tempname(argvars, retvar)
7866 VAR argvars;
7867 VAR retvar;
7868{
7869 static int x = 'A';
7870
7871 retvar->var_type = VAR_STRING;
7872 retvar->var_val.var_string = vim_tempname(x);
7873
7874 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
7875 * names. Skip 'I' and 'O', they are used for shell redirection. */
7876 do
7877 {
7878 if (x == 'Z')
7879 x = '0';
7880 else if (x == '9')
7881 x = 'A';
7882 else
7883 {
7884#ifdef EBCDIC
7885 if (x == 'I')
7886 x = 'J';
7887 else if (x == 'R')
7888 x = 'S';
7889 else
7890#endif
7891 ++x;
7892 }
7893 } while (x == 'I' || x == 'O');
7894}
7895
7896/*
7897 * "tolower(string)" function
7898 */
7899 static void
7900f_tolower(argvars, retvar)
7901 VAR argvars;
7902 VAR retvar;
7903{
7904 char_u *p;
7905
7906 p = vim_strsave(get_var_string(&argvars[0]));
7907 retvar->var_type = VAR_STRING;
7908 retvar->var_val.var_string = p;
7909
7910 if (p != NULL)
7911 while (*p != NUL)
7912 {
7913#ifdef FEAT_MBYTE
7914 int l;
7915
7916 if (enc_utf8)
7917 {
7918 int c, lc;
7919
7920 c = utf_ptr2char(p);
7921 lc = utf_tolower(c);
7922 l = utf_ptr2len_check(p);
7923 /* TODO: reallocate string when byte count changes. */
7924 if (utf_char2len(lc) == l)
7925 utf_char2bytes(lc, p);
7926 p += l;
7927 }
7928 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7929 p += l; /* skip multi-byte character */
7930 else
7931#endif
7932 {
7933 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
7934 ++p;
7935 }
7936 }
7937}
7938
7939/*
7940 * "toupper(string)" function
7941 */
7942 static void
7943f_toupper(argvars, retvar)
7944 VAR argvars;
7945 VAR retvar;
7946{
7947 char_u *p;
7948
7949 p = vim_strsave(get_var_string(&argvars[0]));
7950 retvar->var_type = VAR_STRING;
7951 retvar->var_val.var_string = p;
7952
7953 if (p != NULL)
7954 while (*p != NUL)
7955 {
7956#ifdef FEAT_MBYTE
7957 int l;
7958
7959 if (enc_utf8)
7960 {
7961 int c, uc;
7962
7963 c = utf_ptr2char(p);
7964 uc = utf_toupper(c);
7965 l = utf_ptr2len_check(p);
7966 /* TODO: reallocate string when byte count changes. */
7967 if (utf_char2len(uc) == l)
7968 utf_char2bytes(uc, p);
7969 p += l;
7970 }
7971 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7972 p += l; /* skip multi-byte character */
7973 else
7974#endif
7975 {
7976 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
7977 p++;
7978 }
7979 }
7980}
7981
7982/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00007983 * "tr(string, fromstr, tostr)" function
7984 */
7985 static void
7986f_tr(argvars, retvar)
7987 VAR argvars;
7988 VAR retvar;
7989{
7990 char_u *instr;
7991 char_u *fromstr;
7992 char_u *tostr;
7993 char_u *p;
7994#ifdef FEAT_MBYTE
7995 int inlen;
7996 int fromlen;
7997 int tolen;
7998 int idx;
7999 char_u *cpstr;
8000 int cplen;
8001 int first = TRUE;
8002#endif
8003 char_u buf[NUMBUFLEN];
8004 char_u buf2[NUMBUFLEN];
8005 garray_T ga;
8006
8007 instr = get_var_string(&argvars[0]);
8008 fromstr = get_var_string_buf(&argvars[1], buf);
8009 tostr = get_var_string_buf(&argvars[2], buf2);
8010
8011 /* Default return value: empty string. */
8012 retvar->var_type = VAR_STRING;
8013 retvar->var_val.var_string = NULL;
8014 ga_init2(&ga, (int)sizeof(char), 80);
8015
8016#ifdef FEAT_MBYTE
8017 if (!has_mbyte)
8018#endif
8019 /* not multi-byte: fromstr and tostr must be the same length */
8020 if (STRLEN(fromstr) != STRLEN(tostr))
8021 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008022#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +00008023error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008024#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +00008025 EMSG2(_(e_invarg2), fromstr);
8026 ga_clear(&ga);
8027 return;
8028 }
8029
8030 /* fromstr and tostr have to contain the same number of chars */
8031 while (*instr != NUL)
8032 {
8033#ifdef FEAT_MBYTE
8034 if (has_mbyte)
8035 {
8036 inlen = mb_ptr2len_check(instr);
8037 cpstr = instr;
8038 cplen = inlen;
8039 idx = 0;
8040 for (p = fromstr; *p != NUL; p += fromlen)
8041 {
8042 fromlen = mb_ptr2len_check(p);
8043 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
8044 {
8045 for (p = tostr; *p != NUL; p += tolen)
8046 {
8047 tolen = mb_ptr2len_check(p);
8048 if (idx-- == 0)
8049 {
8050 cplen = tolen;
8051 cpstr = p;
8052 break;
8053 }
8054 }
8055 if (*p == NUL) /* tostr is shorter than fromstr */
8056 goto error;
8057 break;
8058 }
8059 ++idx;
8060 }
8061
8062 if (first && cpstr == instr)
8063 {
8064 /* Check that fromstr and tostr have the same number of
8065 * (multi-byte) characters. Done only once when a character
8066 * of instr doesn't appear in fromstr. */
8067 first = FALSE;
8068 for (p = tostr; *p != NUL; p += tolen)
8069 {
8070 tolen = mb_ptr2len_check(p);
8071 --idx;
8072 }
8073 if (idx != 0)
8074 goto error;
8075 }
8076
8077 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00008078 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +00008079 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +00008080
8081 instr += inlen;
8082 }
8083 else
8084#endif
8085 {
8086 /* When not using multi-byte chars we can do it faster. */
8087 p = vim_strchr(fromstr, *instr);
8088 if (p != NULL)
8089 ga_append(&ga, tostr[p - fromstr]);
8090 else
8091 ga_append(&ga, *instr);
8092 ++instr;
8093 }
8094 }
8095
8096 retvar->var_val.var_string = ga.ga_data;
8097}
8098
8099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 * "type(expr)" function
8101 */
8102 static void
8103f_type(argvars, retvar)
8104 VAR argvars;
8105 VAR retvar;
8106{
8107 if (argvars[0].var_type == VAR_NUMBER)
8108 retvar->var_val.var_number = 0;
8109 else
8110 retvar->var_val.var_number = 1;
8111}
8112
8113/*
8114 * "virtcol(string)" function
8115 */
8116 static void
8117f_virtcol(argvars, retvar)
8118 VAR argvars;
8119 VAR retvar;
8120{
8121 colnr_T vcol = 0;
8122 pos_T *fp;
8123
8124 fp = var2fpos(&argvars[0], FALSE);
8125 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
8126 {
8127 getvvcol(curwin, fp, NULL, NULL, &vcol);
8128 ++vcol;
8129 }
8130
8131 retvar->var_val.var_number = vcol;
8132}
8133
8134/*
8135 * "visualmode()" function
8136 */
8137/*ARGSUSED*/
8138 static void
8139f_visualmode(argvars, retvar)
8140 VAR argvars;
8141 VAR retvar;
8142{
8143#ifdef FEAT_VISUAL
8144 char_u str[2];
8145
8146 retvar->var_type = VAR_STRING;
8147 str[0] = curbuf->b_visual_mode_eval;
8148 str[1] = NUL;
8149 retvar->var_val.var_string = vim_strsave(str);
8150
8151 /* A non-zero number or non-empty string argument: reset mode. */
8152 if ((argvars[0].var_type == VAR_NUMBER
8153 && argvars[0].var_val.var_number != 0)
8154 || (argvars[0].var_type == VAR_STRING
8155 && *get_var_string(&argvars[0]) != NUL))
8156 curbuf->b_visual_mode_eval = NUL;
8157#else
8158 retvar->var_val.var_number = 0; /* return anything, it won't work anyway */
8159#endif
8160}
8161
8162/*
8163 * "winbufnr(nr)" function
8164 */
8165 static void
8166f_winbufnr(argvars, retvar)
8167 VAR argvars;
8168 VAR retvar;
8169{
8170 win_T *wp;
8171
8172 wp = find_win_by_nr(&argvars[0]);
8173 if (wp == NULL)
8174 retvar->var_val.var_number = -1;
8175 else
8176 retvar->var_val.var_number = wp->w_buffer->b_fnum;
8177}
8178
8179/*
8180 * "wincol()" function
8181 */
8182/*ARGSUSED*/
8183 static void
8184f_wincol(argvars, retvar)
8185 VAR argvars;
8186 VAR retvar;
8187{
8188 validate_cursor();
8189 retvar->var_val.var_number = curwin->w_wcol + 1;
8190}
8191
8192/*
8193 * "winheight(nr)" function
8194 */
8195 static void
8196f_winheight(argvars, retvar)
8197 VAR argvars;
8198 VAR retvar;
8199{
8200 win_T *wp;
8201
8202 wp = find_win_by_nr(&argvars[0]);
8203 if (wp == NULL)
8204 retvar->var_val.var_number = -1;
8205 else
8206 retvar->var_val.var_number = wp->w_height;
8207}
8208
8209/*
8210 * "winline()" function
8211 */
8212/*ARGSUSED*/
8213 static void
8214f_winline(argvars, retvar)
8215 VAR argvars;
8216 VAR retvar;
8217{
8218 validate_cursor();
8219 retvar->var_val.var_number = curwin->w_wrow + 1;
8220}
8221
8222/*
8223 * "winnr()" function
8224 */
8225/* ARGSUSED */
8226 static void
8227f_winnr(argvars, retvar)
8228 VAR argvars;
8229 VAR retvar;
8230{
8231 int nr = 1;
8232#ifdef FEAT_WINDOWS
8233 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008234 win_T *twin = curwin;
8235 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008237 if (argvars[0].var_type != VAR_UNKNOWN)
8238 {
8239 arg = get_var_string(&argvars[0]);
8240 if (STRCMP(arg, "$") == 0)
8241 twin = lastwin;
8242 else if (STRCMP(arg, "#") == 0)
8243 {
8244 twin = prevwin;
8245 if (prevwin == NULL)
8246 nr = 0;
8247 }
8248 else
8249 {
8250 EMSG2(_(e_invexpr2), arg);
8251 nr = 0;
8252 }
8253 }
8254
8255 if (nr > 0)
8256 for (wp = firstwin; wp != twin; wp = wp->w_next)
8257 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258#endif
8259 retvar->var_val.var_number = nr;
8260}
8261
8262/*
8263 * "winrestcmd()" function
8264 */
8265/* ARGSUSED */
8266 static void
8267f_winrestcmd(argvars, retvar)
8268 VAR argvars;
8269 VAR retvar;
8270{
8271#ifdef FEAT_WINDOWS
8272 win_T *wp;
8273 int winnr = 1;
8274 garray_T ga;
8275 char_u buf[50];
8276
8277 ga_init2(&ga, (int)sizeof(char), 70);
8278 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8279 {
8280 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
8281 ga_concat(&ga, buf);
8282# ifdef FEAT_VERTSPLIT
8283 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
8284 ga_concat(&ga, buf);
8285# endif
8286 ++winnr;
8287 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00008288 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 retvar->var_val.var_string = ga.ga_data;
8291#else
8292 retvar->var_val.var_string = NULL;
8293#endif
8294 retvar->var_type = VAR_STRING;
8295}
8296
8297/*
8298 * "winwidth(nr)" function
8299 */
8300 static void
8301f_winwidth(argvars, retvar)
8302 VAR argvars;
8303 VAR retvar;
8304{
8305 win_T *wp;
8306
8307 wp = find_win_by_nr(&argvars[0]);
8308 if (wp == NULL)
8309 retvar->var_val.var_number = -1;
8310 else
8311#ifdef FEAT_VERTSPLIT
8312 retvar->var_val.var_number = wp->w_width;
8313#else
8314 retvar->var_val.var_number = Columns;
8315#endif
8316}
8317
8318 static win_T *
8319find_win_by_nr(vp)
8320 VAR vp;
8321{
8322#ifdef FEAT_WINDOWS
8323 win_T *wp;
8324#endif
8325 int nr;
8326
8327 nr = get_var_number(vp);
8328
8329#ifdef FEAT_WINDOWS
8330 if (nr == 0)
8331 return curwin;
8332
8333 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8334 if (--nr <= 0)
8335 break;
8336 return wp;
8337#else
8338 if (nr == 0 || nr == 1)
8339 return curwin;
8340 return NULL;
8341#endif
8342}
8343
8344/*
8345 * Translate a String variable into a position.
8346 */
8347 static pos_T *
8348var2fpos(varp, lnum)
8349 VAR varp;
8350 int lnum; /* TRUE when $ is last line */
8351{
8352 char_u *name;
8353 static pos_T pos;
8354 pos_T *pp;
8355
8356 name = get_var_string(varp);
8357 if (name[0] == '.') /* cursor */
8358 return &curwin->w_cursor;
8359 if (name[0] == '\'') /* mark */
8360 {
8361 pp = getmark(name[1], FALSE);
8362 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
8363 return NULL;
8364 return pp;
8365 }
8366 if (name[0] == '$') /* last column or line */
8367 {
8368 if (lnum)
8369 {
8370 pos.lnum = curbuf->b_ml.ml_line_count;
8371 pos.col = 0;
8372 }
8373 else
8374 {
8375 pos.lnum = curwin->w_cursor.lnum;
8376 pos.col = (colnr_T)STRLEN(ml_get_curline());
8377 }
8378 return &pos;
8379 }
8380 return NULL;
8381}
8382
8383/*
8384 * Get the length of an environment variable name.
8385 * Advance "arg" to the first character after the name.
8386 * Return 0 for error.
8387 */
8388 static int
8389get_env_len(arg)
8390 char_u **arg;
8391{
8392 char_u *p;
8393 int len;
8394
8395 for (p = *arg; vim_isIDc(*p); ++p)
8396 ;
8397 if (p == *arg) /* no name found */
8398 return 0;
8399
8400 len = (int)(p - *arg);
8401 *arg = p;
8402 return len;
8403}
8404
8405/*
8406 * Get the length of the name of a function or internal variable.
8407 * "arg" is advanced to the first non-white character after the name.
8408 * Return 0 if something is wrong.
8409 */
8410 static int
8411get_id_len(arg)
8412 char_u **arg;
8413{
8414 char_u *p;
8415 int len;
8416
8417 /* Find the end of the name. */
8418 for (p = *arg; eval_isnamec(*p); ++p)
8419 ;
8420 if (p == *arg) /* no name found */
8421 return 0;
8422
8423 len = (int)(p - *arg);
8424 *arg = skipwhite(p);
8425
8426 return len;
8427}
8428
8429/*
8430 * Get the length of the name of a function.
8431 * "arg" is advanced to the first non-white character after the name.
8432 * Return 0 if something is wrong.
8433 * If the name contains 'magic' {}'s, expand them and return the
8434 * expanded name in an allocated string via 'alias' - caller must free.
8435 */
8436 static int
8437get_func_len(arg, alias, evaluate)
8438 char_u **arg;
8439 char_u **alias;
8440 int evaluate;
8441{
8442 int len;
8443#ifdef FEAT_MAGIC_BRACES
8444 char_u *p;
8445 char_u *expr_start;
8446 char_u *expr_end;
8447#endif
8448
8449 *alias = NULL; /* default to no alias */
8450
8451 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
8452 && (*arg)[2] == (int)KE_SNR)
8453 {
8454 /* hard coded <SNR>, already translated */
8455 *arg += 3;
8456 return get_id_len(arg) + 3;
8457 }
8458 len = eval_fname_script(*arg);
8459 if (len > 0)
8460 {
8461 /* literal "<SID>", "s:" or "<SNR>" */
8462 *arg += len;
8463 }
8464
8465#ifdef FEAT_MAGIC_BRACES
8466 /*
8467 * Find the end of the name;
8468 */
8469 p = find_name_end(*arg, &expr_start, &expr_end);
8470 /* check for {} construction */
8471 if (expr_start != NULL)
8472 {
8473 char_u *temp_string;
8474
8475 if (!evaluate)
8476 {
8477 len += (int)(p - *arg);
8478 *arg = skipwhite(p);
8479 return len;
8480 }
8481
8482 /*
8483 * Include any <SID> etc in the expanded string:
8484 * Thus the -len here.
8485 */
8486 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
8487 if (temp_string == NULL)
8488 return 0;
8489 *alias = temp_string;
8490 *arg = skipwhite(p);
8491 return (int)STRLEN(temp_string);
8492 }
8493#endif
8494
8495 len += get_id_len(arg);
8496 if (len == 0)
8497 EMSG2(_(e_invexpr2), *arg);
8498
8499 return len;
8500}
8501
8502 static char_u *
8503find_name_end(arg, expr_start, expr_end)
8504 char_u *arg;
8505 char_u **expr_start;
8506 char_u **expr_end;
8507{
8508 int nesting = 0;
8509 char_u *p;
8510
8511 *expr_start = NULL;
8512 *expr_end = NULL;
8513
8514 for (p = arg; (*p != NUL && (eval_isnamec(*p) || nesting != 0)); ++p)
8515 {
8516#ifdef FEAT_MAGIC_BRACES
8517 if (*p == '{')
8518 {
8519 nesting++;
8520 if (*expr_start == NULL)
8521 *expr_start = p;
8522 }
8523 else if (*p == '}')
8524 {
8525 nesting--;
8526 if (nesting == 0 && *expr_end == NULL)
8527 *expr_end = p;
8528 }
8529#endif
8530 }
8531
8532 return p;
8533}
8534
8535/*
8536 * Return TRUE if character "c" can be used in a variable or function name.
8537 */
8538 static int
8539eval_isnamec(c)
8540 int c;
8541{
8542 return (ASCII_ISALNUM(c) || c == '_' || c == ':'
8543#ifdef FEAT_MAGIC_BRACES
8544 || c == '{' || c == '}'
8545#endif
8546 );
8547}
8548
8549/*
8550 * Find a v: variable.
8551 * Return it's index, or -1 if not found.
8552 */
8553 static int
8554find_vim_var(name, len)
8555 char_u *name;
8556 int len; /* length of "name" */
8557{
8558 char_u *vname;
8559 int vlen;
8560 int i;
8561
8562 /*
8563 * Ignore "v:" for old built-in variables, require it for new ones.
8564 */
8565 if (name[0] == 'v' && name[1] == ':')
8566 {
8567 vname = name + 2;
8568 vlen = len - 2;
8569 }
8570 else
8571 {
8572 vname = name;
8573 vlen = len;
8574 }
8575 for (i = 0; i < VV_LEN; ++i)
8576 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
8577 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
8578 return i;
8579 return -1;
8580}
8581
8582/*
8583 * Set number v: variable to "val".
8584 */
8585 void
8586set_vim_var_nr(idx, val)
8587 int idx;
8588 long val;
8589{
8590 vimvars[idx].val = (char_u *)val;
8591}
8592
8593/*
8594 * Get number v: variable value;
8595 */
8596 long
8597get_vim_var_nr(idx)
8598 int idx;
8599{
8600 return (long)vimvars[idx].val;
8601}
8602
8603/*
8604 * Set v:count, v:count1 and v:prevcount.
8605 */
8606 void
8607set_vcount(count, count1)
8608 long count;
8609 long count1;
8610{
8611 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
8612 vimvars[VV_COUNT].val = (char_u *)count;
8613 vimvars[VV_COUNT1].val = (char_u *)count1;
8614}
8615
8616/*
8617 * Set string v: variable to a copy of "val".
8618 */
8619 void
8620set_vim_var_string(idx, val, len)
8621 int idx;
8622 char_u *val;
8623 int len; /* length of "val" to use or -1 (whole string) */
8624{
8625 vim_free(vimvars[idx].val);
8626 if (val == NULL)
8627 vimvars[idx].val = NULL;
8628 else if (len == -1)
8629 vimvars[idx].val = vim_strsave(val);
8630 else
8631 vimvars[idx].val = vim_strnsave(val, len);
8632}
8633
8634/*
8635 * Set v:register if needed.
8636 */
8637 void
8638set_reg_var(c)
8639 int c;
8640{
8641 char_u regname;
8642
8643 if (c == 0 || c == ' ')
8644 regname = '"';
8645 else
8646 regname = c;
8647 /* Avoid free/alloc when the value is already right. */
8648 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
8649 set_vim_var_string(VV_REG, &regname, 1);
8650}
8651
8652/*
8653 * Get or set v:exception. If "oldval" == NULL, return the current value.
8654 * Otherwise, restore the value to "oldval" and return NULL.
8655 * Must always be called in pairs to save and restore v:exception! Does not
8656 * take care of memory allocations.
8657 */
8658 char_u *
8659v_exception(oldval)
8660 char_u *oldval;
8661{
8662 if (oldval == NULL)
8663 return vimvars[VV_EXCEPTION].val;
8664
8665 vimvars[VV_EXCEPTION].val = oldval;
8666 return NULL;
8667}
8668
8669/*
8670 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
8671 * Otherwise, restore the value to "oldval" and return NULL.
8672 * Must always be called in pairs to save and restore v:throwpoint! Does not
8673 * take care of memory allocations.
8674 */
8675 char_u *
8676v_throwpoint(oldval)
8677 char_u *oldval;
8678{
8679 if (oldval == NULL)
8680 return vimvars[VV_THROWPOINT].val;
8681
8682 vimvars[VV_THROWPOINT].val = oldval;
8683 return NULL;
8684}
8685
8686#if defined(FEAT_AUTOCMD) || defined(PROTO)
8687/*
8688 * Set v:cmdarg.
8689 * If "eap" != NULL, use "eap" to generate the value and return the old value.
8690 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
8691 * Must always be called in pairs!
8692 */
8693 char_u *
8694set_cmdarg(eap, oldarg)
8695 exarg_T *eap;
8696 char_u *oldarg;
8697{
8698 char_u *oldval;
8699 char_u *newval;
8700 unsigned len;
8701
8702 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008703 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008705 vim_free(oldval);
8706 vimvars[VV_CMDARG].val = oldarg;
8707 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008710 if (eap->force_bin == FORCE_BIN)
8711 len = 6;
8712 else if (eap->force_bin == FORCE_NOBIN)
8713 len = 8;
8714 else
8715 len = 0;
8716 if (eap->force_ff != 0)
8717 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
8718# ifdef FEAT_MBYTE
8719 if (eap->force_enc != 0)
8720 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
8721# endif
8722
8723 newval = alloc(len + 1);
8724 if (newval == NULL)
8725 return NULL;
8726
8727 if (eap->force_bin == FORCE_BIN)
8728 sprintf((char *)newval, " ++bin");
8729 else if (eap->force_bin == FORCE_NOBIN)
8730 sprintf((char *)newval, " ++nobin");
8731 else
8732 *newval = NUL;
8733 if (eap->force_ff != 0)
8734 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
8735 eap->cmd + eap->force_ff);
8736# ifdef FEAT_MBYTE
8737 if (eap->force_enc != 0)
8738 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
8739 eap->cmd + eap->force_enc);
8740# endif
8741 vimvars[VV_CMDARG].val = newval;
8742 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744#endif
8745
8746/*
8747 * Get the value of internal variable "name".
8748 * Return OK or FAIL.
8749 */
8750 static int
8751get_var_var(name, len, retvar)
8752 char_u *name;
8753 int len; /* length of "name" */
8754 VAR retvar; /* NULL when only checking existence */
8755{
8756 int ret = OK;
8757 int type = VAR_UNKNOWN;
8758 long number = 1;
8759 char_u *string = NULL;
8760 VAR v;
8761 int cc;
8762 int i;
8763
8764 /* truncate the name, so that we can use strcmp() */
8765 cc = name[len];
8766 name[len] = NUL;
8767
8768 /*
8769 * Check for "b:changedtick".
8770 */
8771 if (STRCMP(name, "b:changedtick") == 0)
8772 {
8773 type = VAR_NUMBER;
8774 number = curbuf->b_changedtick;
8775 }
8776
8777 /*
8778 * Check for built-in v: variables.
8779 */
8780 else if ((i = find_vim_var(name, len)) >= 0)
8781 {
8782 type = vimvars[i].type;
8783 number = (long)vimvars[i].val;
8784 string = vimvars[i].val;
8785 }
8786
8787 /*
8788 * Check for user-defined variables.
8789 */
8790 else
8791 {
8792 v = find_var(name, FALSE);
8793 if (v != NULL)
8794 {
8795 type = v->var_type;
8796 number = v->var_val.var_number;
8797 string = v->var_val.var_string;
8798 }
8799 }
8800
8801 if (type == VAR_UNKNOWN)
8802 {
8803 if (retvar != NULL)
8804 EMSG2(_("E121: Undefined variable: %s"), name);
8805 ret = FAIL;
8806 }
8807 else if (retvar != NULL)
8808 {
8809 retvar->var_type = type;
8810 if (type == VAR_NUMBER)
8811 retvar->var_val.var_number = number;
8812 else if (type == VAR_STRING)
8813 {
8814 if (string != NULL)
8815 string = vim_strsave(string);
8816 retvar->var_val.var_string = string;
8817 }
8818 }
8819
8820 name[len] = cc;
8821
8822 return ret;
8823}
8824
8825/*
8826 * Allocate memory for a variable, and make it emtpy (0 or NULL value).
8827 */
8828 static VAR
8829alloc_var()
8830{
8831 return (VAR)alloc_clear((unsigned)sizeof(var));
8832}
8833
8834/*
8835 * Allocate memory for a variable, and assign a string to it.
8836 * The string "s" must have been allocated, it is consumed.
8837 * Return NULL for out of memory, the variable otherwise.
8838 */
8839 static VAR
8840alloc_string_var(s)
8841 char_u *s;
8842{
8843 VAR retvar;
8844
8845 retvar = alloc_var();
8846 if (retvar != NULL)
8847 {
8848 retvar->var_type = VAR_STRING;
8849 retvar->var_val.var_string = s;
8850 }
8851 else
8852 vim_free(s);
8853 return retvar;
8854}
8855
8856/*
8857 * Free the memory for a variable.
8858 */
8859 static void
8860free_var(varp)
8861 VAR varp;
8862{
8863 if (varp != NULL)
8864 {
8865 if (varp->var_type == VAR_STRING)
8866 vim_free(varp->var_val.var_string);
8867 vim_free(varp->var_name);
8868 vim_free(varp);
8869 }
8870}
8871
8872/*
8873 * Free the memory for a variable value and set the value to NULL or 0.
8874 */
8875 static void
8876clear_var(varp)
8877 VAR varp;
8878{
8879 if (varp != NULL)
8880 {
8881 if (varp->var_type == VAR_STRING)
8882 {
8883 vim_free(varp->var_val.var_string);
8884 varp->var_val.var_string = NULL;
8885 }
8886 else
8887 varp->var_val.var_number = 0;
8888 }
8889}
8890
8891/*
8892 * Get the number value of a variable.
8893 * If it is a String variable, uses vim_str2nr().
8894 */
8895 static long
8896get_var_number(varp)
8897 VAR varp;
8898{
8899 long n;
8900
8901 if (varp->var_type == VAR_NUMBER)
8902 return (long)(varp->var_val.var_number);
8903 else if (varp->var_type == VAR_UNKNOWN || varp->var_val.var_string == NULL)
8904 return 0L;
8905 else
8906 {
8907 vim_str2nr(varp->var_val.var_string, NULL, NULL, TRUE, TRUE, &n, NULL);
8908 return n;
8909 }
8910}
8911
8912/*
8913 * Get the lnum from the first argument. Also accepts ".", "$", etc.
8914 */
8915 static linenr_T
8916get_var_lnum(argvars)
8917 VAR argvars;
8918{
8919 var retvar;
8920 linenr_T lnum;
8921
8922 lnum = get_var_number(&argvars[0]);
8923 if (lnum == 0) /* no valid number, try using line() */
8924 {
8925 retvar.var_type = VAR_NUMBER;
8926 f_line(argvars, &retvar);
8927 lnum = retvar.var_val.var_number;
8928 clear_var(&retvar);
8929 }
8930 return lnum;
8931}
8932
8933/*
8934 * Get the string value of a variable.
8935 * If it is a Number variable, the number is converted into a string.
8936 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
8937 * get_var_string_buf() uses a given buffer.
8938 * If the String variable has never been set, return an empty string.
8939 * Never returns NULL;
8940 */
8941 static char_u *
8942get_var_string(varp)
8943 VAR varp;
8944{
8945 static char_u mybuf[NUMBUFLEN];
8946
8947 return get_var_string_buf(varp, mybuf);
8948}
8949
8950 static char_u *
8951get_var_string_buf(varp, buf)
8952 VAR varp;
8953 char_u *buf;
8954{
8955 if (varp->var_type == VAR_NUMBER)
8956 {
8957 sprintf((char *)buf, "%ld", (long)varp->var_val.var_number);
8958 return buf;
8959 }
8960 else if (varp->var_val.var_string == NULL)
8961 return (char_u *)"";
8962 else
8963 return varp->var_val.var_string;
8964}
8965
8966/*
8967 * Find variable "name" in the list of variables.
8968 * Return a pointer to it if found, NULL if not found.
8969 */
8970 static VAR
8971find_var(name, writing)
8972 char_u *name;
8973 int writing;
8974{
8975 int i;
8976 char_u *varname;
8977 garray_T *gap;
8978
8979 /* Check for function arguments "a:" */
8980 if (name[0] == 'a' && name[1] == ':')
8981 {
8982 if (writing)
8983 {
8984 EMSG2(_(e_readonlyvar), name);
8985 return NULL;
8986 }
8987 name += 2;
8988 if (current_funccal == NULL)
8989 return NULL;
8990 if (VIM_ISDIGIT(*name))
8991 {
8992 i = atol((char *)name);
8993 if (i == 0) /* a:0 */
8994 return &current_funccal->a0_var;
8995 i += current_funccal->func->args.ga_len;
8996 if (i > current_funccal->argcount) /* a:999 */
8997 return NULL;
8998 return &(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
8999 }
9000 if (STRCMP(name, "firstline") == 0)
9001 return &(current_funccal->firstline);
9002 if (STRCMP(name, "lastline") == 0)
9003 return &(current_funccal->lastline);
9004 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
9005 if (STRCMP(name, ((char_u **)
9006 (current_funccal->func->args.ga_data))[i]) == 0)
9007 return &(current_funccal->argvars[i]); /* a:name */
9008 return NULL;
9009 }
9010
9011 gap = find_var_ga(name, &varname);
9012 if (gap == NULL)
9013 return NULL;
9014 return find_var_in_ga(gap, varname);
9015}
9016
9017 static VAR
9018find_var_in_ga(gap, varname)
9019 garray_T *gap;
9020 char_u *varname;
9021{
9022 int i;
9023
9024 for (i = gap->ga_len; --i >= 0; )
9025 if (VAR_GAP_ENTRY(i, gap).var_name != NULL
9026 && STRCMP(VAR_GAP_ENTRY(i, gap).var_name, varname) == 0)
9027 break;
9028 if (i < 0)
9029 return NULL;
9030 return &VAR_GAP_ENTRY(i, gap);
9031}
9032
9033/*
9034 * Find the growarray and start of name without ':' for a variable name.
9035 */
9036 static garray_T *
9037find_var_ga(name, varname)
9038 char_u *name;
9039 char_u **varname;
9040{
9041 if (name[1] != ':')
9042 {
9043 /* If not "x:name" there must not be any ":" in the name. */
9044 if (vim_strchr(name, ':') != NULL)
9045 return NULL;
9046 *varname = name;
9047 if (current_funccal == NULL)
9048 return &variables; /* global variable */
9049 return &current_funccal->l_vars; /* local function variable */
9050 }
9051 *varname = name + 2;
9052 if (*name == 'b') /* buffer variable */
9053 return &curbuf->b_vars;
9054 if (*name == 'w') /* window variable */
9055 return &curwin->w_vars;
9056 if (*name == 'g') /* global variable */
9057 return &variables;
9058 if (*name == 'l' && current_funccal != NULL)/* local function variable */
9059 return &current_funccal->l_vars;
9060 if (*name == 's' /* script variable */
9061 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
9062 return &SCRIPT_VARS(current_SID);
9063 return NULL;
9064}
9065
9066/*
9067 * Get the string value of a (global/local) variable.
9068 * Returns NULL when it doesn't exist.
9069 */
9070 char_u *
9071get_var_value(name)
9072 char_u *name;
9073{
9074 VAR v;
9075
9076 v = find_var(name, FALSE);
9077 if (v == NULL)
9078 return NULL;
9079 return get_var_string(v);
9080}
9081
9082/*
9083 * Allocate a new growarry for a sourced script. It will be used while
9084 * sourcing this script and when executing functions defined in the script.
9085 */
9086 void
9087new_script_vars(id)
9088 scid_T id;
9089{
9090 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
9091 {
9092 while (ga_scripts.ga_len < id)
9093 {
9094 var_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
9095 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 }
9097 }
9098}
9099
9100/*
9101 * Initialize internal variables for use.
9102 */
9103 void
9104var_init(gap)
9105 garray_T *gap;
9106{
9107 ga_init2(gap, (int)sizeof(var), 4);
9108}
9109
9110/*
9111 * Clean up a list of internal variables.
9112 */
9113 void
9114var_clear(gap)
9115 garray_T *gap;
9116{
9117 int i;
9118
9119 for (i = gap->ga_len; --i >= 0; )
9120 var_free_one(&VAR_GAP_ENTRY(i, gap));
9121 ga_clear(gap);
9122}
9123
9124 static void
9125var_free_one(v)
9126 VAR v;
9127{
9128 vim_free(v->var_name);
9129 v->var_name = NULL;
9130 if (v->var_type == VAR_STRING)
9131 vim_free(v->var_val.var_string);
9132 v->var_val.var_string = NULL;
9133}
9134
9135/*
9136 * List the value of one internal variable.
9137 */
9138 static void
9139list_one_var(v, prefix)
9140 VAR v;
9141 char_u *prefix;
9142{
9143 list_one_var_a(prefix, v->var_name, v->var_type, get_var_string(v));
9144}
9145
9146/*
9147 * List the value of one "v:" variable.
9148 */
9149 static void
9150list_vim_var(i)
9151 int i; /* index in vimvars[] */
9152{
9153 char_u *p;
9154 char_u numbuf[NUMBUFLEN];
9155
9156 if (vimvars[i].type == VAR_NUMBER)
9157 {
9158 p = numbuf;
9159 sprintf((char *)p, "%ld", (long)vimvars[i].val);
9160 }
9161 else if (vimvars[i].val == NULL)
9162 p = (char_u *)"";
9163 else
9164 p = vimvars[i].val;
9165 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
9166 vimvars[i].type, p);
9167}
9168
9169 static void
9170list_one_var_a(prefix, name, type, string)
9171 char_u *prefix;
9172 char_u *name;
9173 int type;
9174 char_u *string;
9175{
9176 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
9177 if (name != NULL) /* "a:" vars don't have a name stored */
9178 msg_puts(name);
9179 msg_putchar(' ');
9180 msg_advance(22);
9181 if (type == VAR_NUMBER)
9182 msg_putchar('#');
9183 else
9184 msg_putchar(' ');
9185 msg_outtrans(string);
9186}
9187
9188/*
9189 * Set variable "name" to value in "varp".
9190 * If the variable already exists, the value is updated.
9191 * Otherwise the variable is created.
9192 */
9193 static void
9194set_var(name, varp)
9195 char_u *name;
9196 VAR varp;
9197{
9198 int i;
9199 VAR v;
9200 char_u *varname;
9201 garray_T *gap;
9202
9203 /*
9204 * Handle setting internal v: variables.
9205 */
9206 i = find_vim_var(name, (int)STRLEN(name));
9207 if (i >= 0)
9208 {
9209 if (vimvars[i].flags & VV_RO)
9210 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009211 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
9212 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 else
9214 {
9215 if (vimvars[i].type == VAR_STRING)
9216 {
9217 vim_free(vimvars[i].val);
9218 vimvars[i].val = vim_strsave(get_var_string(varp));
9219 }
9220 else
9221 vimvars[i].val = (char_u *)(long)varp->var_val.var_number;
9222 }
9223 return;
9224 }
9225
9226 v = find_var(name, TRUE);
9227 if (v != NULL) /* existing variable, only need to free string */
9228 {
9229 if (v->var_type == VAR_STRING)
9230 vim_free(v->var_val.var_string);
9231 }
9232 else /* add a new variable */
9233 {
9234 gap = find_var_ga(name, &varname);
9235 if (gap == NULL) /* illegal name */
9236 {
9237 EMSG2(_("E461: Illegal variable name: %s"), name);
9238 return;
9239 }
9240
9241 /* Try to use an empty entry */
9242 for (i = gap->ga_len; --i >= 0; )
9243 if (VAR_GAP_ENTRY(i, gap).var_name == NULL)
9244 break;
9245 if (i < 0) /* need to allocate more room */
9246 {
9247 if (ga_grow(gap, 1) == FAIL)
9248 return;
9249 i = gap->ga_len;
9250 }
9251 v = &VAR_GAP_ENTRY(i, gap);
9252 if ((v->var_name = vim_strsave(varname)) == NULL)
9253 return;
9254 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 }
9257 copy_var(varp, v);
9258}
9259
9260 static void
9261copy_var(from, to)
9262 VAR from;
9263 VAR to;
9264{
9265 to->var_type = from->var_type;
9266 if (from->var_type == VAR_STRING)
9267 to->var_val.var_string = vim_strsave(get_var_string(from));
9268 else
9269 to->var_val.var_number = from->var_val.var_number;
9270}
9271
9272/*
9273 * ":echo expr1 ..." print each argument separated with a space, add a
9274 * newline at the end.
9275 * ":echon expr1 ..." print each argument plain.
9276 */
9277 void
9278ex_echo(eap)
9279 exarg_T *eap;
9280{
9281 char_u *arg = eap->arg;
9282 var retvar;
9283 char_u *p;
9284 int needclr = TRUE;
9285 int atstart = TRUE;
9286
9287 if (eap->skip)
9288 ++emsg_skip;
9289 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
9290 {
9291 p = arg;
9292 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9293 {
9294 /*
9295 * Report the invalid expression unless the expression evaluation
9296 * has been cancelled due to an aborting error, an interrupt, or an
9297 * exception.
9298 */
9299 if (!aborting())
9300 EMSG2(_(e_invexpr2), p);
9301 break;
9302 }
9303 if (!eap->skip)
9304 {
9305 if (atstart)
9306 {
9307 atstart = FALSE;
9308 /* Call msg_start() after eval1(), evaluating the expression
9309 * may cause a message to appear. */
9310 if (eap->cmdidx == CMD_echo)
9311 msg_start();
9312 }
9313 else if (eap->cmdidx == CMD_echo)
9314 msg_puts_attr((char_u *)" ", echo_attr);
9315 for (p = get_var_string(&retvar); *p != NUL && !got_int; ++p)
9316 if (*p == '\n' || *p == '\r' || *p == TAB)
9317 {
9318 if (*p != TAB && needclr)
9319 {
9320 /* remove any text still there from the command */
9321 msg_clr_eos();
9322 needclr = FALSE;
9323 }
9324 msg_putchar_attr(*p, echo_attr);
9325 }
9326 else
9327 {
9328#ifdef FEAT_MBYTE
9329 if (has_mbyte)
9330 {
9331 int i = (*mb_ptr2len_check)(p);
9332
9333 (void)msg_outtrans_len_attr(p, i, echo_attr);
9334 p += i - 1;
9335 }
9336 else
9337#endif
9338 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9339 }
9340 }
9341 clear_var(&retvar);
9342 arg = skipwhite(arg);
9343 }
9344 eap->nextcmd = check_nextcmd(arg);
9345
9346 if (eap->skip)
9347 --emsg_skip;
9348 else
9349 {
9350 /* remove text that may still be there from the command */
9351 if (needclr)
9352 msg_clr_eos();
9353 if (eap->cmdidx == CMD_echo)
9354 msg_end();
9355 }
9356}
9357
9358/*
9359 * ":echohl {name}".
9360 */
9361 void
9362ex_echohl(eap)
9363 exarg_T *eap;
9364{
9365 int id;
9366
9367 id = syn_name2id(eap->arg);
9368 if (id == 0)
9369 echo_attr = 0;
9370 else
9371 echo_attr = syn_id2attr(id);
9372}
9373
9374/*
9375 * ":execute expr1 ..." execute the result of an expression.
9376 * ":echomsg expr1 ..." Print a message
9377 * ":echoerr expr1 ..." Print an error
9378 * Each gets spaces around each argument and a newline at the end for
9379 * echo commands
9380 */
9381 void
9382ex_execute(eap)
9383 exarg_T *eap;
9384{
9385 char_u *arg = eap->arg;
9386 var retvar;
9387 int ret = OK;
9388 char_u *p;
9389 garray_T ga;
9390 int len;
9391 int save_did_emsg;
9392
9393 ga_init2(&ga, 1, 80);
9394
9395 if (eap->skip)
9396 ++emsg_skip;
9397 while (*arg != NUL && *arg != '|' && *arg != '\n')
9398 {
9399 p = arg;
9400 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9401 {
9402 /*
9403 * Report the invalid expression unless the expression evaluation
9404 * has been cancelled due to an aborting error, an interrupt, or an
9405 * exception.
9406 */
9407 if (!aborting())
9408 EMSG2(_(e_invexpr2), p);
9409 ret = FAIL;
9410 break;
9411 }
9412
9413 if (!eap->skip)
9414 {
9415 p = get_var_string(&retvar);
9416 len = (int)STRLEN(p);
9417 if (ga_grow(&ga, len + 2) == FAIL)
9418 {
9419 clear_var(&retvar);
9420 ret = FAIL;
9421 break;
9422 }
9423 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 ga.ga_len += len;
9427 }
9428
9429 clear_var(&retvar);
9430 arg = skipwhite(arg);
9431 }
9432
9433 if (ret != FAIL && ga.ga_data != NULL)
9434 {
9435 if (eap->cmdidx == CMD_echomsg)
9436 MSG_ATTR(ga.ga_data, echo_attr);
9437 else if (eap->cmdidx == CMD_echoerr)
9438 {
9439 /* We don't want to abort following commands, restore did_emsg. */
9440 save_did_emsg = did_emsg;
9441 EMSG((char_u *)ga.ga_data);
9442 if (!force_abort)
9443 did_emsg = save_did_emsg;
9444 }
9445 else if (eap->cmdidx == CMD_execute)
9446 do_cmdline((char_u *)ga.ga_data,
9447 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9448 }
9449
9450 ga_clear(&ga);
9451
9452 if (eap->skip)
9453 --emsg_skip;
9454
9455 eap->nextcmd = check_nextcmd(arg);
9456}
9457
9458/*
9459 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9460 * "arg" points to the "&" or '+' when called, to "option" when returning.
9461 * Returns NULL when no option name found. Otherwise pointer to the char
9462 * after the option name.
9463 */
9464 static char_u *
9465find_option_end(arg, opt_flags)
9466 char_u **arg;
9467 int *opt_flags;
9468{
9469 char_u *p = *arg;
9470
9471 ++p;
9472 if (*p == 'g' && p[1] == ':')
9473 {
9474 *opt_flags = OPT_GLOBAL;
9475 p += 2;
9476 }
9477 else if (*p == 'l' && p[1] == ':')
9478 {
9479 *opt_flags = OPT_LOCAL;
9480 p += 2;
9481 }
9482 else
9483 *opt_flags = 0;
9484
9485 if (!ASCII_ISALPHA(*p))
9486 return NULL;
9487 *arg = p;
9488
9489 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9490 p += 4; /* termcap option */
9491 else
9492 while (ASCII_ISALPHA(*p))
9493 ++p;
9494 return p;
9495}
9496
9497/*
9498 * ":function"
9499 */
9500 void
9501ex_function(eap)
9502 exarg_T *eap;
9503{
9504 char_u *theline;
9505 int j;
9506 int c;
9507#ifdef FEAT_MAGIC_BRACES
9508 int saved_did_emsg;
9509#endif
9510 char_u *name = NULL;
9511 char_u *p;
9512 char_u *arg;
9513 garray_T newargs;
9514 garray_T newlines;
9515 int varargs = FALSE;
9516 int mustend = FALSE;
9517 int flags = 0;
9518 ufunc_T *fp;
9519 int indent;
9520 int nesting;
9521 char_u *skip_until = NULL;
9522 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
9523
9524 /*
9525 * ":function" without argument: list functions.
9526 */
9527 if (ends_excmd(*eap->arg))
9528 {
9529 if (!eap->skip)
9530 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
9531 list_func_head(fp, FALSE);
9532 eap->nextcmd = check_nextcmd(eap->arg);
9533 return;
9534 }
9535
9536 p = eap->arg;
9537 name = trans_function_name(&p, eap->skip, FALSE);
9538 if (name == NULL && !eap->skip)
9539 {
9540 /*
9541 * Return on an invalid expression in braces, unless the expression
9542 * evaluation has been cancelled due to an aborting error, an
9543 * interrupt, or an exception.
9544 */
9545 if (!aborting())
9546 return;
9547 else
9548 eap->skip = TRUE;
9549 }
9550#ifdef FEAT_MAGIC_BRACES
9551 /* An error in a function call during evaluation of an expression in magic
9552 * braces should not cause the function not to be defined. */
9553 saved_did_emsg = did_emsg;
9554 did_emsg = FALSE;
9555#endif
9556
9557 /*
9558 * ":function func" with only function name: list function.
9559 */
9560 if (vim_strchr(p, '(') == NULL)
9561 {
9562 if (!ends_excmd(*skipwhite(p)))
9563 {
9564 EMSG(_(e_trailing));
9565 goto erret_name;
9566 }
9567 eap->nextcmd = check_nextcmd(p);
9568 if (eap->nextcmd != NULL)
9569 *p = NUL;
9570 if (!eap->skip && !got_int)
9571 {
9572 fp = find_func(name);
9573 if (fp != NULL)
9574 {
9575 list_func_head(fp, TRUE);
9576 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
9577 {
9578 msg_putchar('\n');
9579 msg_outnum((long)(j + 1));
9580 if (j < 9)
9581 msg_putchar(' ');
9582 if (j < 99)
9583 msg_putchar(' ');
9584 msg_prt_line(FUNCLINE(fp, j));
9585 out_flush(); /* show a line at a time */
9586 ui_breakcheck();
9587 }
9588 if (!got_int)
9589 {
9590 msg_putchar('\n');
9591 msg_puts((char_u *)" endfunction");
9592 }
9593 }
9594 else
9595 EMSG2(_("E123: Undefined function: %s"), eap->arg);
9596 }
9597 goto erret_name;
9598 }
9599
9600 /*
9601 * ":function name(arg1, arg2)" Define function.
9602 */
9603 p = skipwhite(p);
9604 if (*p != '(')
9605 {
9606 if (!eap->skip)
9607 {
9608 EMSG2(_("E124: Missing '(': %s"), eap->arg);
9609 goto erret_name;
9610 }
9611 /* attempt to continue by skipping some text */
9612 if (vim_strchr(p, '(') != NULL)
9613 p = vim_strchr(p, '(');
9614 }
9615 p = skipwhite(p + 1);
9616
9617 ga_init2(&newargs, (int)sizeof(char_u *), 3);
9618 ga_init2(&newlines, (int)sizeof(char_u *), 3);
9619
9620 /*
9621 * Isolate the arguments: "arg1, arg2, ...)"
9622 */
9623 while (*p != ')')
9624 {
9625 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
9626 {
9627 varargs = TRUE;
9628 p += 3;
9629 mustend = TRUE;
9630 }
9631 else
9632 {
9633 arg = p;
9634 while (ASCII_ISALNUM(*p) || *p == '_')
9635 ++p;
9636 if (arg == p || isdigit(*arg)
9637 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
9638 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
9639 {
9640 if (!eap->skip)
9641 EMSG2(_("E125: Illegal argument: %s"), arg);
9642 break;
9643 }
9644 if (ga_grow(&newargs, 1) == FAIL)
9645 goto erret;
9646 c = *p;
9647 *p = NUL;
9648 arg = vim_strsave(arg);
9649 if (arg == NULL)
9650 goto erret;
9651 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
9652 *p = c;
9653 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (*p == ',')
9655 ++p;
9656 else
9657 mustend = TRUE;
9658 }
9659 p = skipwhite(p);
9660 if (mustend && *p != ')')
9661 {
9662 if (!eap->skip)
9663 EMSG2(_(e_invarg2), eap->arg);
9664 break;
9665 }
9666 }
9667 ++p; /* skip the ')' */
9668
9669 /* find extra arguments "range" and "abort" */
9670 for (;;)
9671 {
9672 p = skipwhite(p);
9673 if (STRNCMP(p, "range", 5) == 0)
9674 {
9675 flags |= FC_RANGE;
9676 p += 5;
9677 }
9678 else if (STRNCMP(p, "abort", 5) == 0)
9679 {
9680 flags |= FC_ABORT;
9681 p += 5;
9682 }
9683 else
9684 break;
9685 }
9686
9687 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
9688 EMSG(_(e_trailing));
9689
9690 /*
9691 * Read the body of the function, until ":endfunction" is found.
9692 */
9693 if (KeyTyped)
9694 {
9695 /* Check if the function already exists, don't let the user type the
9696 * whole function before telling him it doesn't work! For a script we
9697 * need to skip the body to be able to find what follows. */
9698 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
9699 EMSG2(_(e_funcexts), name);
9700
9701 msg_putchar('\n'); /* don't overwrite the function name */
9702 cmdline_row = msg_row;
9703 }
9704
9705 indent = 2;
9706 nesting = 0;
9707 for (;;)
9708 {
9709 msg_scroll = TRUE;
9710 need_wait_return = FALSE;
9711 if (eap->getline == NULL)
9712 theline = getcmdline(':', 0L, indent);
9713 else
9714 theline = eap->getline(':', eap->cookie, indent);
9715 if (KeyTyped)
9716 lines_left = Rows - 1;
9717 if (theline == NULL)
9718 {
9719 EMSG(_("E126: Missing :endfunction"));
9720 goto erret;
9721 }
9722
9723 if (skip_until != NULL)
9724 {
9725 /* between ":append" and "." and between ":python <<EOF" and "EOF"
9726 * don't check for ":endfunc". */
9727 if (STRCMP(theline, skip_until) == 0)
9728 {
9729 vim_free(skip_until);
9730 skip_until = NULL;
9731 }
9732 }
9733 else
9734 {
9735 /* skip ':' and blanks*/
9736 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
9737 ;
9738
9739 /* Check for "endfunction" (should be more strict...). */
9740 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
9741 {
9742 vim_free(theline);
9743 break;
9744 }
9745
9746 /* Increase indent inside "if", "while", and "try", decrease
9747 * at "end". */
9748 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
9749 indent -= 2;
9750 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
9751 || STRNCMP(p, "try", 3) == 0)
9752 indent += 2;
9753
9754 /* Check for defining a function inside this function. */
9755 if (STRNCMP(p, "fu", 2) == 0)
9756 {
9757 p = skipwhite(skiptowhite(p));
9758 p += eval_fname_script(p);
9759 if (ASCII_ISALPHA(*p))
9760 {
9761 vim_free(trans_function_name(&p, TRUE, FALSE));
9762 if (*skipwhite(p) == '(')
9763 {
9764 ++nesting;
9765 indent += 2;
9766 }
9767 }
9768 }
9769
9770 /* Check for ":append" or ":insert". */
9771 p = skip_range(p, NULL);
9772 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
9773 || (p[0] == 'i'
9774 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
9775 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
9776 skip_until = vim_strsave((char_u *)".");
9777
9778 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
9779 arg = skipwhite(skiptowhite(p));
9780 if (arg[0] == '<' && arg[1] =='<'
9781 && ((p[0] == 'p' && p[1] == 'y'
9782 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
9783 || (p[0] == 'p' && p[1] == 'e'
9784 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
9785 || (p[0] == 't' && p[1] == 'c'
9786 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
9787 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
9788 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009789 || (p[0] == 'm' && p[1] == 'z'
9790 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 ))
9792 {
9793 /* ":python <<" continues until a dot, like ":append" */
9794 p = skipwhite(arg + 2);
9795 if (*p == NUL)
9796 skip_until = vim_strsave((char_u *)".");
9797 else
9798 skip_until = vim_strsave(p);
9799 }
9800 }
9801
9802 /* Add the line to the function. */
9803 if (ga_grow(&newlines, 1) == FAIL)
9804 goto erret;
9805 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
9806 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808
9809 /* Don't define the function when skipping commands or when an error was
9810 * detected. */
9811 if (eap->skip || did_emsg)
9812 goto erret;
9813
9814 /*
9815 * If there are no errors, add the function
9816 */
9817 fp = find_func(name);
9818 if (fp != NULL)
9819 {
9820 if (!eap->forceit)
9821 {
9822 EMSG2(_(e_funcexts), name);
9823 goto erret;
9824 }
9825 if (fp->calls)
9826 {
9827 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
9828 goto erret;
9829 }
9830 /* redefine existing function */
9831 ga_clear_strings(&(fp->args));
9832 ga_clear_strings(&(fp->lines));
9833 vim_free(name);
9834 }
9835 else
9836 {
9837 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
9838 if (fp == NULL)
9839 goto erret;
9840 /* insert the new function in the function list */
9841 fp->next = firstfunc;
9842 firstfunc = fp;
9843 fp->name = name;
9844 }
9845 fp->args = newargs;
9846 fp->lines = newlines;
9847 fp->varargs = varargs;
9848 fp->flags = flags;
9849 fp->calls = 0;
9850 fp->script_ID = current_SID;
9851#ifdef FEAT_MAGIC_BRACES
9852 did_emsg |= saved_did_emsg;
9853#endif
9854 vim_free(skip_until);
9855 return;
9856
9857erret:
9858 vim_free(skip_until);
9859 ga_clear_strings(&newargs);
9860 ga_clear_strings(&newlines);
9861erret_name:
9862 vim_free(name);
9863#ifdef FEAT_MAGIC_BRACES
9864 did_emsg |= saved_did_emsg;
9865#endif
9866}
9867
9868/*
9869 * Get a function name, translating "<SID>" and "<SNR>".
9870 * Returns the function name in allocated memory, or NULL for failure.
9871 * Advances "pp" to just after the function name (if no error).
9872 */
9873 static char_u *
9874trans_function_name(pp, skip, internal)
9875 char_u **pp;
9876 int skip; /* only find the end, don't evaluate */
9877 int internal; /* TRUE if internal function name OK */
9878{
9879 char_u *name;
9880 char_u *start;
9881 char_u *end;
9882 int lead;
9883 char_u sid_buf[20];
9884 char_u *temp_string = NULL;
9885 char_u *expr_start, *expr_end;
9886 int len;
9887
9888 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
9889 start = *pp;
9890 lead = eval_fname_script(start);
9891 if (lead > 0)
9892 start += lead;
9893 end = find_name_end(start, &expr_start, &expr_end);
9894 if (end == start)
9895 {
9896 if (!skip)
9897 EMSG(_("E129: Function name required"));
9898 return NULL;
9899 }
9900#ifdef FEAT_MAGIC_BRACES
9901 if (expr_start != NULL && !skip)
9902 {
9903 /* expand magic curlies */
9904 temp_string = make_expanded_name(start, expr_start, expr_end, end);
9905 if (temp_string == NULL)
9906 {
9907 /*
9908 * Report an invalid expression in braces, unless the expression
9909 * evaluation has been cancelled due to an aborting error, an
9910 * interrupt, or an exception.
9911 */
9912 if (!aborting())
9913 EMSG2(_(e_invarg2), start);
9914 else
9915 *pp = end;
9916 return NULL;
9917 }
9918 start = temp_string;
9919 len = (int)STRLEN(temp_string);
9920 }
9921 else
9922#endif
9923 len = (int)(end - start);
9924
9925 /*
9926 * Copy the function name to allocated memory.
9927 * Accept <SID>name() inside a script, translate into <SNR>123_name().
9928 * Accept <SNR>123_name() outside a script.
9929 */
9930 if (skip)
9931 lead = 0; /* do nothing */
9932 else if (lead > 0)
9933 {
9934 lead = 3;
9935 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9936 {
9937 if (current_SID <= 0)
9938 {
9939 EMSG(_(e_usingsid));
9940 return NULL;
9941 }
9942 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
9943 lead += (int)STRLEN(sid_buf);
9944 }
9945 }
9946 else if (!internal && !ASCII_ISUPPER(*start))
9947 {
9948 EMSG2(_("E128: Function name must start with a capital: %s"), start);
9949 return NULL;
9950 }
9951 name = alloc((unsigned)(len + lead + 1));
9952 if (name != NULL)
9953 {
9954 if (lead > 0)
9955 {
9956 name[0] = K_SPECIAL;
9957 name[1] = KS_EXTRA;
9958 name[2] = (int)KE_SNR;
9959 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9960 STRCPY(name + 3, sid_buf);
9961 }
9962 mch_memmove(name + lead, start, (size_t)len);
9963 name[len + lead] = NUL;
9964 }
9965 *pp = end;
9966
9967 vim_free(temp_string);
9968 return name;
9969}
9970
9971/*
9972 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
9973 * Return 2 if "p" starts with "s:".
9974 * Return 0 otherwise.
9975 */
9976 static int
9977eval_fname_script(p)
9978 char_u *p;
9979{
9980 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
9981 || STRNICMP(p + 1, "SNR>", 4) == 0))
9982 return 5;
9983 if (p[0] == 's' && p[1] == ':')
9984 return 2;
9985 return 0;
9986}
9987
9988/*
9989 * Return TRUE if "p" starts with "<SID>" or "s:".
9990 * Only works if eval_fname_script() returned non-zero for "p"!
9991 */
9992 static int
9993eval_fname_sid(p)
9994 char_u *p;
9995{
9996 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
9997}
9998
9999/*
10000 * List the head of the function: "name(arg1, arg2)".
10001 */
10002 static void
10003list_func_head(fp, indent)
10004 ufunc_T *fp;
10005 int indent;
10006{
10007 int j;
10008
10009 msg_start();
10010 if (indent)
10011 MSG_PUTS(" ");
10012 MSG_PUTS("function ");
10013 if (fp->name[0] == K_SPECIAL)
10014 {
10015 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
10016 msg_puts(fp->name + 3);
10017 }
10018 else
10019 msg_puts(fp->name);
10020 msg_putchar('(');
10021 for (j = 0; j < fp->args.ga_len; ++j)
10022 {
10023 if (j)
10024 MSG_PUTS(", ");
10025 msg_puts(FUNCARG(fp, j));
10026 }
10027 if (fp->varargs)
10028 {
10029 if (j)
10030 MSG_PUTS(", ");
10031 MSG_PUTS("...");
10032 }
10033 msg_putchar(')');
10034}
10035
10036/*
10037 * Find a function by name, return pointer to it in ufuncs.
10038 * Return NULL for unknown function.
10039 */
10040 static ufunc_T *
10041find_func(name)
10042 char_u *name;
10043{
10044 ufunc_T *fp;
10045
10046 for (fp = firstfunc; fp != NULL; fp = fp->next)
10047 if (STRCMP(name, fp->name) == 0)
10048 break;
10049 return fp;
10050}
10051
10052#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10053
10054/*
10055 * Function given to ExpandGeneric() to obtain the list of user defined
10056 * function names.
10057 */
10058 char_u *
10059get_user_func_name(xp, idx)
10060 expand_T *xp;
10061 int idx;
10062{
10063 static ufunc_T *fp = NULL;
10064
10065 if (idx == 0)
10066 fp = firstfunc;
10067 if (fp != NULL)
10068 {
10069 if (STRLEN(fp->name) + 4 >= IOSIZE)
10070 return fp->name; /* prevents overflow */
10071
10072 cat_func_name(IObuff, fp);
10073 if (xp->xp_context != EXPAND_USER_FUNC)
10074 {
10075 STRCAT(IObuff, "(");
10076 if (!fp->varargs && fp->args.ga_len == 0)
10077 STRCAT(IObuff, ")");
10078 }
10079
10080 fp = fp->next;
10081 return IObuff;
10082 }
10083 return NULL;
10084}
10085
10086#endif /* FEAT_CMDL_COMPL */
10087
10088/*
10089 * Copy the function name of "fp" to buffer "buf".
10090 * "buf" must be able to hold the function name plus three bytes.
10091 * Takes care of script-local function names.
10092 */
10093 static void
10094cat_func_name(buf, fp)
10095 char_u *buf;
10096 ufunc_T *fp;
10097{
10098 if (fp->name[0] == K_SPECIAL)
10099 {
10100 STRCPY(buf, "<SNR>");
10101 STRCAT(buf, fp->name + 3);
10102 }
10103 else
10104 STRCPY(buf, fp->name);
10105}
10106
10107/*
10108 * ":delfunction {name}"
10109 */
10110 void
10111ex_delfunction(eap)
10112 exarg_T *eap;
10113{
10114 ufunc_T *fp = NULL, *pfp;
10115 char_u *p;
10116 char_u *name;
10117
10118 p = eap->arg;
10119 name = trans_function_name(&p, eap->skip, FALSE);
10120 if (name == NULL)
10121 return;
10122 if (!ends_excmd(*skipwhite(p)))
10123 {
10124 vim_free(name);
10125 EMSG(_(e_trailing));
10126 return;
10127 }
10128 eap->nextcmd = check_nextcmd(p);
10129 if (eap->nextcmd != NULL)
10130 *p = NUL;
10131
10132 if (!eap->skip)
10133 fp = find_func(name);
10134 vim_free(name);
10135
10136 if (!eap->skip)
10137 {
10138 if (fp == NULL)
10139 {
10140 EMSG2(_("E130: Undefined function: %s"), eap->arg);
10141 return;
10142 }
10143 if (fp->calls)
10144 {
10145 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
10146 return;
10147 }
10148
10149 /* clear this function */
10150 vim_free(fp->name);
10151 ga_clear_strings(&(fp->args));
10152 ga_clear_strings(&(fp->lines));
10153
10154 /* remove the function from the function list */
10155 if (firstfunc == fp)
10156 firstfunc = fp->next;
10157 else
10158 {
10159 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
10160 if (pfp->next == fp)
10161 {
10162 pfp->next = fp->next;
10163 break;
10164 }
10165 }
10166 vim_free(fp);
10167 }
10168}
10169
10170/*
10171 * Call a user function.
10172 */
10173 static void
10174call_user_func(fp, argcount, argvars, retvar, firstline, lastline)
10175 ufunc_T *fp; /* pointer to function */
10176 int argcount; /* nr of args */
10177 VAR argvars; /* arguments */
10178 VAR retvar; /* return value */
10179 linenr_T firstline; /* first line of range */
10180 linenr_T lastline; /* last line of range */
10181{
10182 char_u *save_sourcing_name;
10183 linenr_T save_sourcing_lnum;
10184 scid_T save_current_SID;
10185 struct funccall fc;
10186 struct funccall *save_fcp = current_funccal;
10187 int save_did_emsg;
10188 static int depth = 0;
10189
10190 /* If depth of calling is getting too high, don't execute the function */
10191 if (depth >= p_mfd)
10192 {
10193 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
10194 retvar->var_type = VAR_NUMBER;
10195 retvar->var_val.var_number = -1;
10196 return;
10197 }
10198 ++depth;
10199
10200 line_breakcheck(); /* check for CTRL-C hit */
10201
10202 /* set local variables */
10203 var_init(&fc.l_vars);
10204 fc.func = fp;
10205 fc.argcount = argcount;
10206 fc.argvars = argvars;
10207 fc.retvar = retvar;
10208 retvar->var_val.var_number = 0;
10209 fc.linenr = 0;
10210 fc.returned = FALSE;
10211 fc.level = ex_nesting_level;
10212 fc.a0_var.var_type = VAR_NUMBER;
10213 fc.a0_var.var_val.var_number = argcount - fp->args.ga_len;
10214 fc.a0_var.var_name = NULL;
10215 current_funccal = &fc;
10216 fc.firstline.var_type = VAR_NUMBER;
10217 fc.firstline.var_val.var_number = firstline;
10218 fc.firstline.var_name = NULL;
10219 fc.lastline.var_type = VAR_NUMBER;
10220 fc.lastline.var_val.var_number = lastline;
10221 fc.lastline.var_name = NULL;
10222 /* Check if this function has a breakpoint. */
10223 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
10224 fc.dbg_tick = debug_tick;
10225
10226 /* Don't redraw while executing the function. */
10227 ++RedrawingDisabled;
10228 save_sourcing_name = sourcing_name;
10229 save_sourcing_lnum = sourcing_lnum;
10230 sourcing_lnum = 1;
10231 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
10232 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
10233 if (sourcing_name != NULL)
10234 {
10235 if (save_sourcing_name != NULL
10236 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
10237 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
10238 else
10239 STRCPY(sourcing_name, "function ");
10240 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
10241
10242 if (p_verbose >= 12)
10243 {
10244 ++no_wait_return;
10245 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10246 msg_str((char_u *)_("calling %s"), sourcing_name);
10247 if (p_verbose >= 14)
10248 {
10249 int i;
10250 char_u buf[MSG_BUF_LEN];
10251
10252 msg_puts((char_u *)"(");
10253 for (i = 0; i < argcount; ++i)
10254 {
10255 if (i > 0)
10256 msg_puts((char_u *)", ");
10257 if (argvars[i].var_type == VAR_NUMBER)
10258 msg_outnum((long)argvars[i].var_val.var_number);
10259 else
10260 {
10261 trunc_string(get_var_string(&argvars[i]),
10262 buf, MSG_BUF_LEN);
10263 msg_puts((char_u *)"\"");
10264 msg_puts(buf);
10265 msg_puts((char_u *)"\"");
10266 }
10267 }
10268 msg_puts((char_u *)")");
10269 }
10270 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10271 cmdline_row = msg_row;
10272 --no_wait_return;
10273 }
10274 }
10275 save_current_SID = current_SID;
10276 current_SID = fp->script_ID;
10277 save_did_emsg = did_emsg;
10278 did_emsg = FALSE;
10279
10280 /* call do_cmdline() to execute the lines */
10281 do_cmdline(NULL, get_func_line, (void *)&fc,
10282 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
10283
10284 --RedrawingDisabled;
10285
10286 /* when the function was aborted because of an error, return -1 */
10287 if ((did_emsg && (fp->flags & FC_ABORT)) || retvar->var_type == VAR_UNKNOWN)
10288 {
10289 clear_var(retvar);
10290 retvar->var_type = VAR_NUMBER;
10291 retvar->var_val.var_number = -1;
10292 }
10293
10294 /* when being verbose, mention the return value */
10295 if (p_verbose >= 12)
10296 {
10297 char_u *sn, *val;
10298
10299 ++no_wait_return;
10300 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10301
10302 /* Make sure the output fits in IObuff. */
10303 sn = sourcing_name;
10304 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
10305 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
10306
10307 if (aborting())
10308 smsg((char_u *)_("%s aborted"), sn);
10309 else if (fc.retvar->var_type == VAR_NUMBER)
10310 smsg((char_u *)_("%s returning #%ld"), sn,
10311 (long)fc.retvar->var_val.var_number);
10312 else if (fc.retvar->var_type == VAR_STRING)
10313 {
10314 val = get_var_string(fc.retvar);
10315 if (STRLEN(val) > IOSIZE / 2 - 50)
10316 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
10317 smsg((char_u *)_("%s returning \"%s\""), sn, val);
10318 }
10319 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10320 cmdline_row = msg_row;
10321 --no_wait_return;
10322 }
10323
10324 vim_free(sourcing_name);
10325 sourcing_name = save_sourcing_name;
10326 sourcing_lnum = save_sourcing_lnum;
10327 current_SID = save_current_SID;
10328
10329 if (p_verbose >= 12 && sourcing_name != NULL)
10330 {
10331 ++no_wait_return;
10332 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10333 msg_str((char_u *)_("continuing in %s"), sourcing_name);
10334 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10335 cmdline_row = msg_row;
10336 --no_wait_return;
10337 }
10338
10339 did_emsg |= save_did_emsg;
10340 current_funccal = save_fcp;
10341
10342 var_clear(&fc.l_vars); /* free all local variables */
10343 --depth;
10344}
10345
10346/*
10347 * ":return [expr]"
10348 */
10349 void
10350ex_return(eap)
10351 exarg_T *eap;
10352{
10353 char_u *arg = eap->arg;
10354 var retvar;
10355 int returning = FALSE;
10356
10357 if (current_funccal == NULL)
10358 {
10359 EMSG(_("E133: :return not inside a function"));
10360 return;
10361 }
10362
10363 if (eap->skip)
10364 ++emsg_skip;
10365
10366 eap->nextcmd = NULL;
10367 if ((*arg != NUL && *arg != '|' && *arg != '\n')
10368 && eval0(arg, &retvar, &eap->nextcmd, !eap->skip) != FAIL)
10369 {
10370 if (!eap->skip)
10371 returning = do_return(eap, FALSE, TRUE, &retvar);
10372 else
10373 clear_var(&retvar);
10374 }
10375 /* It's safer to return also on error. */
10376 else if (!eap->skip)
10377 {
10378 /*
10379 * Return unless the expression evaluation has been cancelled due to an
10380 * aborting error, an interrupt, or an exception.
10381 */
10382 if (!aborting())
10383 returning = do_return(eap, FALSE, TRUE, NULL);
10384 }
10385
10386 /* When skipping or the return gets pending, advance to the next command
10387 * in this line (!returning). Otherwise, ignore the rest of the line.
10388 * Following lines will be ignored by get_func_line(). */
10389 if (returning)
10390 eap->nextcmd = NULL;
10391 else if (eap->nextcmd == NULL) /* no argument */
10392 eap->nextcmd = check_nextcmd(arg);
10393
10394 if (eap->skip)
10395 --emsg_skip;
10396}
10397
10398/*
10399 * Return from a function. Possibly makes the return pending. Also called
10400 * for a pending return at the ":endtry" or after returning from an extra
10401 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
10402 * when called due to a ":return" command. "value" may point to a variable
10403 * with the return value. Returns TRUE when the return can be carried out,
10404 * FALSE when the return gets pending.
10405 */
10406 int
10407do_return(eap, reanimate, is_cmd, value)
10408 exarg_T *eap;
10409 int reanimate;
10410 int is_cmd;
10411 void *value;
10412{
10413 int idx;
10414 struct condstack *cstack = eap->cstack;
10415
10416 if (reanimate)
10417 /* Undo the return. */
10418 current_funccal->returned = FALSE;
10419
10420 /*
10421 * Cleanup (and inactivate) conditionals, but stop when a try conditional
10422 * not in its finally clause (which then is to be executed next) is found.
10423 * In this case, make the ":return" pending for execution at the ":endtry".
10424 * Otherwise, return normally.
10425 */
10426 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
10427 if (idx >= 0)
10428 {
10429 cstack->cs_pending[idx] = CSTP_RETURN;
10430
10431 if (!is_cmd && !reanimate)
10432 /* A pending return again gets pending. "value" points to an
10433 * allocated variable with the value of the original ":return"'s
10434 * argument if present or is NULL else. */
10435 cstack->cs_retvar[idx] = value;
10436 else
10437 {
10438 /* When undoing a return in order to make it pending, get the stored
10439 * return value. */
10440 if (reanimate)
10441 value = current_funccal->retvar;
10442
10443 if (value != NULL)
10444 {
10445 /* Store the value of the pending return. */
10446 if ((cstack->cs_retvar[idx] = alloc_var()) != NULL)
10447 *(VAR)cstack->cs_retvar[idx] = *(VAR)value;
10448 else
10449 EMSG(_(e_outofmem));
10450 }
10451 else
10452 cstack->cs_retvar[idx] = NULL;
10453
10454 if (reanimate)
10455 {
10456 /* The pending return value could be overwritten by a ":return"
10457 * without argument in a finally clause; reset the default
10458 * return value. */
10459 current_funccal->retvar->var_type = VAR_NUMBER;
10460 current_funccal->retvar->var_val.var_number = 0;
10461 }
10462 }
10463 report_make_pending(CSTP_RETURN, value);
10464 }
10465 else
10466 {
10467 current_funccal->returned = TRUE;
10468
10469 /* If the return is carried out now, store the return value. For
10470 * a return immediately after reanimation, the value is already
10471 * there. */
10472 if (!reanimate && value != NULL)
10473 {
10474 clear_var(current_funccal->retvar);
10475 *current_funccal->retvar = *(VAR)value;
10476 if (!is_cmd)
10477 vim_free(value);
10478 }
10479 }
10480
10481 return idx < 0;
10482}
10483
10484/*
10485 * Free the variable with a pending return value.
10486 */
10487 void
10488discard_pending_return(retvar)
10489 void *retvar;
10490{
10491 /* The variable was copied from one with an undefined var_name. So we can't
10492 * use free_var() to clear and free it. */
10493 clear_var((VAR)retvar);
10494 vim_free(retvar);
10495}
10496
10497/*
10498 * Generate a return command for producing the value of "retvar". The result
10499 * is an allocated string. Used by report_pending() for verbose messages.
10500 */
10501 char_u *
10502get_return_cmd(retvar)
10503 void *retvar;
10504{
10505 char_u *s = IObuff;
10506
10507 if (retvar == NULL || ((VAR)retvar)->var_type == VAR_UNKNOWN)
10508 s = (char_u *)":return";
10509 else if (((VAR)retvar)->var_type == VAR_STRING)
10510 sprintf((char *)IObuff, ":return \"%s\"",
10511 ((VAR)retvar)->var_val.var_string);
10512 else
10513 sprintf((char *)IObuff, ":return %ld",
10514 (long)(((VAR)retvar)->var_val.var_number));
10515 return vim_strsave(s);
10516}
10517
10518/*
10519 * Get next function line.
10520 * Called by do_cmdline() to get the next line.
10521 * Returns allocated string, or NULL for end of function.
10522 */
10523/* ARGSUSED */
10524 char_u *
10525get_func_line(c, cookie, indent)
10526 int c; /* not used */
10527 void *cookie;
10528 int indent; /* not used */
10529{
10530 struct funccall *fcp = (struct funccall *)cookie;
10531 char_u *retval;
10532 garray_T *gap; /* growarray with function lines */
10533
10534 /* If breakpoints have been added/deleted need to check for it. */
10535 if (fcp->dbg_tick != debug_tick)
10536 {
10537 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10538 sourcing_lnum);
10539 fcp->dbg_tick = debug_tick;
10540 }
10541
10542 gap = &fcp->func->lines;
10543 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10544 retval = NULL;
10545 else if (fcp->returned || fcp->linenr >= gap->ga_len)
10546 retval = NULL;
10547 else
10548 {
10549 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
10550 sourcing_lnum = fcp->linenr;
10551 }
10552
10553 /* Did we encounter a breakpoint? */
10554 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
10555 {
10556 dbg_breakpoint(fcp->func->name, sourcing_lnum);
10557 /* Find next breakpoint. */
10558 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10559 sourcing_lnum);
10560 fcp->dbg_tick = debug_tick;
10561 }
10562
10563 return retval;
10564}
10565
10566/*
10567 * Return TRUE if the currently active function should be ended, because a
10568 * return was encountered or an error occured. Used inside a ":while".
10569 */
10570 int
10571func_has_ended(cookie)
10572 void *cookie;
10573{
10574 struct funccall *fcp = (struct funccall *)cookie;
10575
10576 /* Ignore the "abort" flag if the abortion behavior has been changed due to
10577 * an error inside a try conditional. */
10578 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10579 || fcp->returned);
10580}
10581
10582/*
10583 * return TRUE if cookie indicates a function which "abort"s on errors.
10584 */
10585 int
10586func_has_abort(cookie)
10587 void *cookie;
10588{
10589 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
10590}
10591
10592#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
10593typedef enum
10594{
10595 VAR_FLAVOUR_DEFAULT,
10596 VAR_FLAVOUR_SESSION,
10597 VAR_FLAVOUR_VIMINFO
10598} var_flavour_T;
10599
10600static var_flavour_T var_flavour __ARGS((char_u *varname));
10601
10602 static var_flavour_T
10603var_flavour(varname)
10604 char_u *varname;
10605{
10606 char_u *p = varname;
10607
10608 if (ASCII_ISUPPER(*p))
10609 {
10610 while (*(++p))
10611 if (ASCII_ISLOWER(*p))
10612 return VAR_FLAVOUR_SESSION;
10613 return VAR_FLAVOUR_VIMINFO;
10614 }
10615 else
10616 return VAR_FLAVOUR_DEFAULT;
10617}
10618#endif
10619
10620#if defined(FEAT_VIMINFO) || defined(PROTO)
10621/*
10622 * Restore global vars that start with a capital from the viminfo file
10623 */
10624 int
10625read_viminfo_varlist(virp, writing)
10626 vir_T *virp;
10627 int writing;
10628{
10629 char_u *tab;
10630 int is_string = FALSE;
10631 VAR varp = NULL;
10632 char_u *val;
10633
10634 if (!writing && (find_viminfo_parameter('!') != NULL))
10635 {
10636 tab = vim_strchr(virp->vir_line + 1, '\t');
10637 if (tab != NULL)
10638 {
10639 *tab++ = '\0'; /* isolate the variable name */
10640 if (*tab == 'S') /* string var */
10641 is_string = TRUE;
10642
10643 tab = vim_strchr(tab, '\t');
10644 if (tab != NULL)
10645 {
10646 /* create a nameless variable to hold the value */
10647 if (is_string)
10648 {
10649 val = viminfo_readstring(virp,
10650 (int)(tab - virp->vir_line + 1), TRUE);
10651 if (val != NULL)
10652 varp = alloc_string_var(val);
10653 }
10654 else
10655 {
10656 varp = alloc_var();
10657 if (varp != NULL)
10658 {
10659 varp->var_type = VAR_NUMBER;
10660 varp->var_val.var_number = atol((char *)tab + 1);
10661 }
10662 }
10663 /* assign the value to the variable */
10664 if (varp != NULL)
10665 {
10666 set_var(virp->vir_line + 1, varp);
10667 free_var(varp);
10668 }
10669 }
10670 }
10671 }
10672
10673 return viminfo_readline(virp);
10674}
10675
10676/*
10677 * Write global vars that start with a capital to the viminfo file
10678 */
10679 void
10680write_viminfo_varlist(fp)
10681 FILE *fp;
10682{
10683 garray_T *gap = &variables; /* global variable */
10684 VAR this_var;
10685 int i;
10686
10687 if (find_viminfo_parameter('!') == NULL)
10688 return;
10689
10690 fprintf(fp, _("\n# global variables:\n"));
10691 for (i = gap->ga_len; --i >= 0; )
10692 {
10693 this_var = &VAR_GAP_ENTRY(i, gap);
10694 if (this_var->var_name != NULL
10695 && var_flavour(this_var->var_name) == VAR_FLAVOUR_VIMINFO)
10696 {
10697 fprintf(fp, "!%s\t%s\t", this_var->var_name,
10698 (this_var->var_type == VAR_STRING) ? "STR" : "NUM");
10699 viminfo_writestring(fp, get_var_string(this_var));
10700 }
10701 }
10702}
10703#endif
10704
10705#if defined(FEAT_SESSION) || defined(PROTO)
10706 int
10707store_session_globals(fd)
10708 FILE *fd;
10709{
10710 garray_T *gap = &variables; /* global variable */
10711 VAR this_var;
10712 int i;
10713 char_u *p, *t;
10714
10715 for (i = gap->ga_len; --i >= 0; )
10716 {
10717 this_var = &VAR_GAP_ENTRY(i, gap);
10718 if (this_var->var_name != NULL)
10719 {
10720 if (var_flavour(this_var->var_name) == VAR_FLAVOUR_SESSION)
10721 {
10722 /* Escapse special characters with a backslash. Turn a LF and
10723 * CR into \n and \r. */
10724 p = vim_strsave_escaped(get_var_string(this_var),
10725 (char_u *)"\\\"\n\r");
10726 if (p == NULL) /* out of memory */
10727 continue;
10728 for (t = p; *t != NUL; ++t)
10729 if (*t == '\n')
10730 *t = 'n';
10731 else if (*t == '\r')
10732 *t = 'r';
10733 if ((fprintf(fd, "let %s = %c%s%c",
10734 this_var->var_name,
10735 (this_var->var_type == VAR_STRING) ? '"' : ' ',
10736 p,
10737 (this_var->var_type == VAR_STRING) ? '"' : ' ') < 0)
10738 || put_eol(fd) == FAIL)
10739 {
10740 vim_free(p);
10741 return FAIL;
10742 }
10743 vim_free(p);
10744 }
10745
10746 }
10747 }
10748 return OK;
10749}
10750#endif
10751
10752#endif /* FEAT_EVAL */
10753
10754#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
10755
10756
10757#ifdef WIN3264
10758/*
10759 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10760 */
10761static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10762static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
10763static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10764
10765/*
10766 * Get the short pathname of a file.
10767 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
10768 */
10769 static int
10770get_short_pathname(fnamep, bufp, fnamelen)
10771 char_u **fnamep;
10772 char_u **bufp;
10773 int *fnamelen;
10774{
10775 int l,len;
10776 char_u *newbuf;
10777
10778 len = *fnamelen;
10779
10780 l = GetShortPathName(*fnamep, *fnamep, len);
10781 if (l > len - 1)
10782 {
10783 /* If that doesn't work (not enough space), then save the string
10784 * and try again with a new buffer big enough
10785 */
10786 newbuf = vim_strnsave(*fnamep, l);
10787 if (newbuf == NULL)
10788 return 0;
10789
10790 vim_free(*bufp);
10791 *fnamep = *bufp = newbuf;
10792
10793 l = GetShortPathName(*fnamep,*fnamep,l+1);
10794
10795 /* Really should always succeed, as the buffer is big enough */
10796 }
10797
10798 *fnamelen = l;
10799 return 1;
10800}
10801
10802/*
10803 * Create a short path name. Returns the length of the buffer it needs.
10804 * Doesn't copy over the end of the buffer passed in.
10805 */
10806 static int
10807shortpath_for_invalid_fname(fname, bufp, fnamelen)
10808 char_u **fname;
10809 char_u **bufp;
10810 int *fnamelen;
10811{
10812 char_u *s, *p, *pbuf2, *pbuf3;
10813 char_u ch;
10814 int l,len,len2,plen,slen;
10815
10816 /* Make a copy */
10817 len2 = *fnamelen;
10818 pbuf2 = vim_strnsave(*fname, len2);
10819 pbuf3 = NULL;
10820
10821 s = pbuf2 + len2 - 1; /* Find the end */
10822 slen = 1;
10823 plen = len2;
10824
10825 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010826 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 {
10828 --s;
10829 ++slen;
10830 --plen;
10831 }
10832
10833 do
10834 {
10835 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010836 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 {
10838 --s;
10839 ++slen;
10840 --plen;
10841 }
10842 if (s <= pbuf2)
10843 break;
10844
10845 /* Remeber the character that is about to be blatted */
10846 ch = *s;
10847 *s = 0; /* get_short_pathname requires a null-terminated string */
10848
10849 /* Try it in situ */
10850 p = pbuf2;
10851 if (!get_short_pathname(&p, &pbuf3, &plen))
10852 {
10853 vim_free(pbuf2);
10854 return -1;
10855 }
10856 *s = ch; /* Preserve the string */
10857 } while (plen == 0);
10858
10859 if (plen > 0)
10860 {
10861 /* Remeber the length of the new string. */
10862 *fnamelen = len = plen + slen;
10863 vim_free(*bufp);
10864 if (len > len2)
10865 {
10866 /* If there's not enough space in the currently allocated string,
10867 * then copy it to a buffer big enough.
10868 */
10869 *fname= *bufp = vim_strnsave(p, len);
10870 if (*fname == NULL)
10871 return -1;
10872 }
10873 else
10874 {
10875 /* Transfer pbuf2 to being the main buffer (it's big enough) */
10876 *fname = *bufp = pbuf2;
10877 if (p != pbuf2)
10878 strncpy(*fname, p, plen);
10879 pbuf2 = NULL;
10880 }
10881 /* Concat the next bit */
10882 strncpy(*fname + plen, s, slen);
10883 (*fname)[len] = '\0';
10884 }
10885 vim_free(pbuf3);
10886 vim_free(pbuf2);
10887 return 0;
10888}
10889
10890/*
10891 * Get a pathname for a partial path.
10892 */
10893 static int
10894shortpath_for_partial(fnamep, bufp, fnamelen)
10895 char_u **fnamep;
10896 char_u **bufp;
10897 int *fnamelen;
10898{
10899 int sepcount, len, tflen;
10900 char_u *p;
10901 char_u *pbuf, *tfname;
10902 int hasTilde;
10903
10904 /* Count up the path seperators from the RHS.. so we know which part
10905 * of the path to return.
10906 */
10907 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010908 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 if (vim_ispathsep(*p))
10910 ++sepcount;
10911
10912 /* Need full path first (use expand_env() to remove a "~/") */
10913 hasTilde = (**fnamep == '~');
10914 if (hasTilde)
10915 pbuf = tfname = expand_env_save(*fnamep);
10916 else
10917 pbuf = tfname = FullName_save(*fnamep, FALSE);
10918
10919 len = tflen = STRLEN(tfname);
10920
10921 if (!get_short_pathname(&tfname, &pbuf, &len))
10922 return -1;
10923
10924 if (len == 0)
10925 {
10926 /* Don't have a valid filename, so shorten the rest of the
10927 * path if we can. This CAN give us invalid 8.3 filenames, but
10928 * there's not a lot of point in guessing what it might be.
10929 */
10930 len = tflen;
10931 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
10932 return -1;
10933 }
10934
10935 /* Count the paths backward to find the beginning of the desired string. */
10936 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010937 {
10938#ifdef FEAT_MBYTE
10939 if (has_mbyte)
10940 p -= mb_head_off(tfname, p);
10941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (vim_ispathsep(*p))
10943 {
10944 if (sepcount == 0 || (hasTilde && sepcount == 1))
10945 break;
10946 else
10947 sepcount --;
10948 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 if (hasTilde)
10951 {
10952 --p;
10953 if (p >= tfname)
10954 *p = '~';
10955 else
10956 return -1;
10957 }
10958 else
10959 ++p;
10960
10961 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10962 vim_free(*bufp);
10963 *fnamelen = (int)STRLEN(p);
10964 *bufp = pbuf;
10965 *fnamep = p;
10966
10967 return 0;
10968}
10969#endif /* WIN3264 */
10970
10971/*
10972 * Adjust a filename, according to a string of modifiers.
10973 * *fnamep must be NUL terminated when called. When returning, the length is
10974 * determined by *fnamelen.
10975 * Returns valid flags.
10976 * When there is an error, *fnamep is set to NULL.
10977 */
10978 int
10979modify_fname(src, usedlen, fnamep, bufp, fnamelen)
10980 char_u *src; /* string with modifiers */
10981 int *usedlen; /* characters after src that are used */
10982 char_u **fnamep; /* file name so far */
10983 char_u **bufp; /* buffer for allocated file name or NULL */
10984 int *fnamelen; /* length of fnamep */
10985{
10986 int valid = 0;
10987 char_u *tail;
10988 char_u *s, *p, *pbuf;
10989 char_u dirname[MAXPATHL];
10990 int c;
10991 int has_fullname = 0;
10992#ifdef WIN3264
10993 int has_shortname = 0;
10994#endif
10995
10996repeat:
10997 /* ":p" - full path/file_name */
10998 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10999 {
11000 has_fullname = 1;
11001
11002 valid |= VALID_PATH;
11003 *usedlen += 2;
11004
11005 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
11006 if ((*fnamep)[0] == '~'
11007#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
11008 && ((*fnamep)[1] == '/'
11009# ifdef BACKSLASH_IN_FILENAME
11010 || (*fnamep)[1] == '\\'
11011# endif
11012 || (*fnamep)[1] == NUL)
11013
11014#endif
11015 )
11016 {
11017 *fnamep = expand_env_save(*fnamep);
11018 vim_free(*bufp); /* free any allocated file name */
11019 *bufp = *fnamep;
11020 if (*fnamep == NULL)
11021 return -1;
11022 }
11023
11024 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011025 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 {
11027 if (vim_ispathsep(*p)
11028 && p[1] == '.'
11029 && (p[2] == NUL
11030 || vim_ispathsep(p[2])
11031 || (p[2] == '.'
11032 && (p[3] == NUL || vim_ispathsep(p[3])))))
11033 break;
11034 }
11035
11036 /* FullName_save() is slow, don't use it when not needed. */
11037 if (*p != NUL || !vim_isAbsName(*fnamep))
11038 {
11039 *fnamep = FullName_save(*fnamep, *p != NUL);
11040 vim_free(*bufp); /* free any allocated file name */
11041 *bufp = *fnamep;
11042 if (*fnamep == NULL)
11043 return -1;
11044 }
11045
11046 /* Append a path separator to a directory. */
11047 if (mch_isdir(*fnamep))
11048 {
11049 /* Make room for one or two extra characters. */
11050 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
11051 vim_free(*bufp); /* free any allocated file name */
11052 *bufp = *fnamep;
11053 if (*fnamep == NULL)
11054 return -1;
11055 add_pathsep(*fnamep);
11056 }
11057 }
11058
11059 /* ":." - path relative to the current directory */
11060 /* ":~" - path relative to the home directory */
11061 /* ":8" - shortname path - postponed till after */
11062 while (src[*usedlen] == ':'
11063 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
11064 {
11065 *usedlen += 2;
11066 if (c == '8')
11067 {
11068#ifdef WIN3264
11069 has_shortname = 1; /* Postpone this. */
11070#endif
11071 continue;
11072 }
11073 pbuf = NULL;
11074 /* Need full path first (use expand_env() to remove a "~/") */
11075 if (!has_fullname)
11076 {
11077 if (c == '.' && **fnamep == '~')
11078 p = pbuf = expand_env_save(*fnamep);
11079 else
11080 p = pbuf = FullName_save(*fnamep, FALSE);
11081 }
11082 else
11083 p = *fnamep;
11084
11085 has_fullname = 0;
11086
11087 if (p != NULL)
11088 {
11089 if (c == '.')
11090 {
11091 mch_dirname(dirname, MAXPATHL);
11092 s = shorten_fname(p, dirname);
11093 if (s != NULL)
11094 {
11095 *fnamep = s;
11096 if (pbuf != NULL)
11097 {
11098 vim_free(*bufp); /* free any allocated file name */
11099 *bufp = pbuf;
11100 pbuf = NULL;
11101 }
11102 }
11103 }
11104 else
11105 {
11106 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
11107 /* Only replace it when it starts with '~' */
11108 if (*dirname == '~')
11109 {
11110 s = vim_strsave(dirname);
11111 if (s != NULL)
11112 {
11113 *fnamep = s;
11114 vim_free(*bufp);
11115 *bufp = s;
11116 }
11117 }
11118 }
11119 vim_free(pbuf);
11120 }
11121 }
11122
11123 tail = gettail(*fnamep);
11124 *fnamelen = (int)STRLEN(*fnamep);
11125
11126 /* ":h" - head, remove "/file_name", can be repeated */
11127 /* Don't remove the first "/" or "c:\" */
11128 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
11129 {
11130 valid |= VALID_HEAD;
11131 *usedlen += 2;
11132 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011133 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 --tail;
11135 *fnamelen = (int)(tail - *fnamep);
11136#ifdef VMS
11137 if (*fnamelen > 0)
11138 *fnamelen += 1; /* the path separator is part of the path */
11139#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011140 while (tail > s && !after_pathsep(s, tail))
11141 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 }
11143
11144 /* ":8" - shortname */
11145 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
11146 {
11147 *usedlen += 2;
11148#ifdef WIN3264
11149 has_shortname = 1;
11150#endif
11151 }
11152
11153#ifdef WIN3264
11154 /* Check shortname after we have done 'heads' and before we do 'tails'
11155 */
11156 if (has_shortname)
11157 {
11158 pbuf = NULL;
11159 /* Copy the string if it is shortened by :h */
11160 if (*fnamelen < (int)STRLEN(*fnamep))
11161 {
11162 p = vim_strnsave(*fnamep, *fnamelen);
11163 if (p == 0)
11164 return -1;
11165 vim_free(*bufp);
11166 *bufp = *fnamep = p;
11167 }
11168
11169 /* Split into two implementations - makes it easier. First is where
11170 * there isn't a full name already, second is where there is.
11171 */
11172 if (!has_fullname && !vim_isAbsName(*fnamep))
11173 {
11174 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
11175 return -1;
11176 }
11177 else
11178 {
11179 int l;
11180
11181 /* Simple case, already have the full-name
11182 * Nearly always shorter, so try first time. */
11183 l = *fnamelen;
11184 if (!get_short_pathname(fnamep, bufp, &l))
11185 return -1;
11186
11187 if (l == 0)
11188 {
11189 /* Couldn't find the filename.. search the paths.
11190 */
11191 l = *fnamelen;
11192 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
11193 return -1;
11194 }
11195 *fnamelen = l;
11196 }
11197 }
11198#endif /* WIN3264 */
11199
11200 /* ":t" - tail, just the basename */
11201 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
11202 {
11203 *usedlen += 2;
11204 *fnamelen -= (int)(tail - *fnamep);
11205 *fnamep = tail;
11206 }
11207
11208 /* ":e" - extension, can be repeated */
11209 /* ":r" - root, without extension, can be repeated */
11210 while (src[*usedlen] == ':'
11211 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
11212 {
11213 /* find a '.' in the tail:
11214 * - for second :e: before the current fname
11215 * - otherwise: The last '.'
11216 */
11217 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
11218 s = *fnamep - 2;
11219 else
11220 s = *fnamep + *fnamelen - 1;
11221 for ( ; s > tail; --s)
11222 if (s[0] == '.')
11223 break;
11224 if (src[*usedlen + 1] == 'e') /* :e */
11225 {
11226 if (s > tail)
11227 {
11228 *fnamelen += (int)(*fnamep - (s + 1));
11229 *fnamep = s + 1;
11230#ifdef VMS
11231 /* cut version from the extension */
11232 s = *fnamep + *fnamelen - 1;
11233 for ( ; s > *fnamep; --s)
11234 if (s[0] == ';')
11235 break;
11236 if (s > *fnamep)
11237 *fnamelen = s - *fnamep;
11238#endif
11239 }
11240 else if (*fnamep <= tail)
11241 *fnamelen = 0;
11242 }
11243 else /* :r */
11244 {
11245 if (s > tail) /* remove one extension */
11246 *fnamelen = (int)(s - *fnamep);
11247 }
11248 *usedlen += 2;
11249 }
11250
11251 /* ":s?pat?foo?" - substitute */
11252 /* ":gs?pat?foo?" - global substitute */
11253 if (src[*usedlen] == ':'
11254 && (src[*usedlen + 1] == 's'
11255 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
11256 {
11257 char_u *str;
11258 char_u *pat;
11259 char_u *sub;
11260 int sep;
11261 char_u *flags;
11262 int didit = FALSE;
11263
11264 flags = (char_u *)"";
11265 s = src + *usedlen + 2;
11266 if (src[*usedlen + 1] == 'g')
11267 {
11268 flags = (char_u *)"g";
11269 ++s;
11270 }
11271
11272 sep = *s++;
11273 if (sep)
11274 {
11275 /* find end of pattern */
11276 p = vim_strchr(s, sep);
11277 if (p != NULL)
11278 {
11279 pat = vim_strnsave(s, (int)(p - s));
11280 if (pat != NULL)
11281 {
11282 s = p + 1;
11283 /* find end of substitution */
11284 p = vim_strchr(s, sep);
11285 if (p != NULL)
11286 {
11287 sub = vim_strnsave(s, (int)(p - s));
11288 str = vim_strnsave(*fnamep, *fnamelen);
11289 if (sub != NULL && str != NULL)
11290 {
11291 *usedlen = (int)(p + 1 - src);
11292 s = do_string_sub(str, pat, sub, flags);
11293 if (s != NULL)
11294 {
11295 *fnamep = s;
11296 *fnamelen = (int)STRLEN(s);
11297 vim_free(*bufp);
11298 *bufp = s;
11299 didit = TRUE;
11300 }
11301 }
11302 vim_free(sub);
11303 vim_free(str);
11304 }
11305 vim_free(pat);
11306 }
11307 }
11308 /* after using ":s", repeat all the modifiers */
11309 if (didit)
11310 goto repeat;
11311 }
11312 }
11313
11314 return valid;
11315}
11316
11317/*
11318 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
11319 * "flags" can be "g" to do a global substitute.
11320 * Returns an allocated string, NULL for error.
11321 */
11322 char_u *
11323do_string_sub(str, pat, sub, flags)
11324 char_u *str;
11325 char_u *pat;
11326 char_u *sub;
11327 char_u *flags;
11328{
11329 int sublen;
11330 regmatch_T regmatch;
11331 int i;
11332 int do_all;
11333 char_u *tail;
11334 garray_T ga;
11335 char_u *ret;
11336 char_u *save_cpo;
11337
11338 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
11339 save_cpo = p_cpo;
11340 p_cpo = (char_u *)"";
11341
11342 ga_init2(&ga, 1, 200);
11343
11344 do_all = (flags[0] == 'g');
11345
11346 regmatch.rm_ic = p_ic;
11347 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11348 if (regmatch.regprog != NULL)
11349 {
11350 tail = str;
11351 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
11352 {
11353 /*
11354 * Get some space for a temporary buffer to do the substitution
11355 * into. It will contain:
11356 * - The text up to where the match is.
11357 * - The substituted text.
11358 * - The text after the match.
11359 */
11360 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
11361 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
11362 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11363 {
11364 ga_clear(&ga);
11365 break;
11366 }
11367
11368 /* copy the text up to where the match is */
11369 i = (int)(regmatch.startp[0] - tail);
11370 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11371 /* add the substituted text */
11372 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
11373 + ga.ga_len + i, TRUE, TRUE, FALSE);
11374 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 /* avoid getting stuck on a match with an empty string */
11376 if (tail == regmatch.endp[0])
11377 {
11378 if (*tail == NUL)
11379 break;
11380 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
11381 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383 else
11384 {
11385 tail = regmatch.endp[0];
11386 if (*tail == NUL)
11387 break;
11388 }
11389 if (!do_all)
11390 break;
11391 }
11392
11393 if (ga.ga_data != NULL)
11394 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11395
11396 vim_free(regmatch.regprog);
11397 }
11398
11399 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11400 ga_clear(&ga);
11401 p_cpo = save_cpo;
11402
11403 return ret;
11404}
11405
11406#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */