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