blob: 88b954971d5f50704f04564b0da0e47665b3bcc2 [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
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00005574#ifdef FEAT_NETBEANS_INTG
5575 else if (STRICMP(name, "netbeans_enabled") == 0)
5576 n = usingNetbeans;
5577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
5579
5580 retvar->var_val.var_number = n;
5581}
5582
5583/*
5584 * "hasmapto()" function
5585 */
5586 static void
5587f_hasmapto(argvars, retvar)
5588 VAR argvars;
5589 VAR retvar;
5590{
5591 char_u *name;
5592 char_u *mode;
5593 char_u buf[NUMBUFLEN];
5594
5595 name = get_var_string(&argvars[0]);
5596 if (argvars[1].var_type == VAR_UNKNOWN)
5597 mode = (char_u *)"nvo";
5598 else
5599 mode = get_var_string_buf(&argvars[1], buf);
5600
5601 if (map_to_exists(name, mode))
5602 retvar->var_val.var_number = TRUE;
5603 else
5604 retvar->var_val.var_number = FALSE;
5605}
5606
5607/*
5608 * "histadd()" function
5609 */
5610/*ARGSUSED*/
5611 static void
5612f_histadd(argvars, retvar)
5613 VAR argvars;
5614 VAR retvar;
5615{
5616#ifdef FEAT_CMDHIST
5617 int histype;
5618 char_u *str;
5619 char_u buf[NUMBUFLEN];
5620#endif
5621
5622 retvar->var_val.var_number = FALSE;
5623 if (check_restricted() || check_secure())
5624 return;
5625#ifdef FEAT_CMDHIST
5626 histype = get_histtype(get_var_string(&argvars[0]));
5627 if (histype >= 0)
5628 {
5629 str = get_var_string_buf(&argvars[1], buf);
5630 if (*str != NUL)
5631 {
5632 add_to_history(histype, str, FALSE, NUL);
5633 retvar->var_val.var_number = TRUE;
5634 return;
5635 }
5636 }
5637#endif
5638}
5639
5640/*
5641 * "histdel()" function
5642 */
5643/*ARGSUSED*/
5644 static void
5645f_histdel(argvars, retvar)
5646 VAR argvars;
5647 VAR retvar;
5648{
5649#ifdef FEAT_CMDHIST
5650 int n;
5651 char_u buf[NUMBUFLEN];
5652
5653 if (argvars[1].var_type == VAR_UNKNOWN)
5654 /* only one argument: clear entire history */
5655 n = clr_history(get_histtype(get_var_string(&argvars[0])));
5656 else if (argvars[1].var_type == VAR_NUMBER)
5657 /* index given: remove that entry */
5658 n = del_history_idx(get_histtype(get_var_string(&argvars[0])),
5659 (int)get_var_number(&argvars[1]));
5660 else
5661 /* string given: remove all matching entries */
5662 n = del_history_entry(get_histtype(get_var_string(&argvars[0])),
5663 get_var_string_buf(&argvars[1], buf));
5664 retvar->var_val.var_number = n;
5665#else
5666 retvar->var_val.var_number = 0;
5667#endif
5668}
5669
5670/*
5671 * "histget()" function
5672 */
5673/*ARGSUSED*/
5674 static void
5675f_histget(argvars, retvar)
5676 VAR argvars;
5677 VAR retvar;
5678{
5679#ifdef FEAT_CMDHIST
5680 int type;
5681 int idx;
5682
5683 type = get_histtype(get_var_string(&argvars[0]));
5684 if (argvars[1].var_type == VAR_UNKNOWN)
5685 idx = get_history_idx(type);
5686 else
5687 idx = (int)get_var_number(&argvars[1]);
5688 retvar->var_val.var_string = vim_strsave(get_history_entry(type, idx));
5689#else
5690 retvar->var_val.var_string = NULL;
5691#endif
5692 retvar->var_type = VAR_STRING;
5693}
5694
5695/*
5696 * "histnr()" function
5697 */
5698/*ARGSUSED*/
5699 static void
5700f_histnr(argvars, retvar)
5701 VAR argvars;
5702 VAR retvar;
5703{
5704 int i;
5705
5706#ifdef FEAT_CMDHIST
5707 i = get_histtype(get_var_string(&argvars[0]));
5708 if (i >= HIST_CMD && i < HIST_COUNT)
5709 i = get_history_idx(i);
5710 else
5711#endif
5712 i = -1;
5713 retvar->var_val.var_number = i;
5714}
5715
5716/*
5717 * "highlight_exists()" function
5718 */
5719 static void
5720f_hlexists(argvars, retvar)
5721 VAR argvars;
5722 VAR retvar;
5723{
5724 retvar->var_val.var_number = highlight_exists(get_var_string(&argvars[0]));
5725}
5726
5727/*
5728 * "highlightID(name)" function
5729 */
5730 static void
5731f_hlID(argvars, retvar)
5732 VAR argvars;
5733 VAR retvar;
5734{
5735 retvar->var_val.var_number = syn_name2id(get_var_string(&argvars[0]));
5736}
5737
5738/*
5739 * "hostname()" function
5740 */
5741/*ARGSUSED*/
5742 static void
5743f_hostname(argvars, retvar)
5744 VAR argvars;
5745 VAR retvar;
5746{
5747 char_u hostname[256];
5748
5749 mch_get_host_name(hostname, 256);
5750 retvar->var_type = VAR_STRING;
5751 retvar->var_val.var_string = vim_strsave(hostname);
5752}
5753
5754/*
5755 * iconv() function
5756 */
5757/*ARGSUSED*/
5758 static void
5759f_iconv(argvars, retvar)
5760 VAR argvars;
5761 VAR retvar;
5762{
5763#ifdef FEAT_MBYTE
5764 char_u buf1[NUMBUFLEN];
5765 char_u buf2[NUMBUFLEN];
5766 char_u *from, *to, *str;
5767 vimconv_T vimconv;
5768#endif
5769
5770 retvar->var_type = VAR_STRING;
5771 retvar->var_val.var_string = NULL;
5772
5773#ifdef FEAT_MBYTE
5774 str = get_var_string(&argvars[0]);
5775 from = enc_canonize(enc_skip(get_var_string_buf(&argvars[1], buf1)));
5776 to = enc_canonize(enc_skip(get_var_string_buf(&argvars[2], buf2)));
5777 vimconv.vc_type = CONV_NONE;
5778 convert_setup(&vimconv, from, to);
5779
5780 /* If the encodings are equal, no conversion needed. */
5781 if (vimconv.vc_type == CONV_NONE)
5782 retvar->var_val.var_string = vim_strsave(str);
5783 else
5784 retvar->var_val.var_string = string_convert(&vimconv, str, NULL);
5785
5786 convert_setup(&vimconv, NULL, NULL);
5787 vim_free(from);
5788 vim_free(to);
5789#endif
5790}
5791
5792/*
5793 * "indent()" function
5794 */
5795 static void
5796f_indent(argvars, retvar)
5797 VAR argvars;
5798 VAR retvar;
5799{
5800 linenr_T lnum;
5801
5802 lnum = get_var_lnum(argvars);
5803 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5804 retvar->var_val.var_number = get_indent_lnum(lnum);
5805 else
5806 retvar->var_val.var_number = -1;
5807}
5808
5809static int inputsecret_flag = 0;
5810
5811/*
5812 * "input()" function
5813 * Also handles inputsecret() when inputsecret is set.
5814 */
5815 static void
5816f_input(argvars, retvar)
5817 VAR argvars;
5818 VAR retvar;
5819{
5820 char_u *prompt = get_var_string(&argvars[0]);
5821 char_u *p = NULL;
5822 int c;
5823 char_u buf[NUMBUFLEN];
5824 int cmd_silent_save = cmd_silent;
5825
5826 retvar->var_type = VAR_STRING;
5827
5828#ifdef NO_CONSOLE_INPUT
5829 /* While starting up, there is no place to enter text. */
5830 if (no_console_input())
5831 {
5832 retvar->var_val.var_string = NULL;
5833 return;
5834 }
5835#endif
5836
5837 cmd_silent = FALSE; /* Want to see the prompt. */
5838 if (prompt != NULL)
5839 {
5840 /* Only the part of the message after the last NL is considered as
5841 * prompt for the command line */
5842 p = vim_strrchr(prompt, '\n');
5843 if (p == NULL)
5844 p = prompt;
5845 else
5846 {
5847 ++p;
5848 c = *p;
5849 *p = NUL;
5850 msg_start();
5851 msg_clr_eos();
5852 msg_puts_attr(prompt, echo_attr);
5853 msg_didout = FALSE;
5854 msg_starthere();
5855 *p = c;
5856 }
5857 cmdline_row = msg_row;
5858 }
5859
5860 if (argvars[1].var_type != VAR_UNKNOWN)
5861 stuffReadbuffSpec(get_var_string_buf(&argvars[1], buf));
5862
5863 retvar->var_val.var_string =
5864 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
5865
5866 /* since the user typed this, no need to wait for return */
5867 need_wait_return = FALSE;
5868 msg_didout = FALSE;
5869 cmd_silent = cmd_silent_save;
5870}
5871
5872/*
5873 * "inputdialog()" function
5874 */
5875 static void
5876f_inputdialog(argvars, retvar)
5877 VAR argvars;
5878 VAR retvar;
5879{
5880#if defined(FEAT_GUI_TEXTDIALOG)
5881 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
5882 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5883 {
5884 char_u *message;
5885 char_u buf[NUMBUFLEN];
5886
5887 message = get_var_string(&argvars[0]);
5888 if (argvars[1].var_type != VAR_UNKNOWN)
5889 {
5890 STRNCPY(IObuff, get_var_string_buf(&argvars[1], buf), IOSIZE);
5891 IObuff[IOSIZE - 1] = NUL;
5892 }
5893 else
5894 IObuff[0] = NUL;
5895 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
5896 1, IObuff) == 1)
5897 retvar->var_val.var_string = vim_strsave(IObuff);
5898 else
5899 {
5900 if (argvars[1].var_type != VAR_UNKNOWN
5901 && argvars[2].var_type != VAR_UNKNOWN)
5902 retvar->var_val.var_string = vim_strsave(
5903 get_var_string_buf(&argvars[2], buf));
5904 else
5905 retvar->var_val.var_string = NULL;
5906 }
5907 retvar->var_type = VAR_STRING;
5908 }
5909 else
5910#endif
5911 f_input(argvars, retvar);
5912}
5913
5914static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5915
5916/*
5917 * "inputrestore()" function
5918 */
5919/*ARGSUSED*/
5920 static void
5921f_inputrestore(argvars, retvar)
5922 VAR argvars;
5923 VAR retvar;
5924{
5925 if (ga_userinput.ga_len > 0)
5926 {
5927 --ga_userinput.ga_len;
5928 ++ga_userinput.ga_room;
5929 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5930 + ga_userinput.ga_len);
5931 retvar->var_val.var_number = 0; /* OK */
5932 }
5933 else if (p_verbose > 1)
5934 {
5935 msg((char_u *)_("called inputrestore() more often than inputsave()"));
5936 retvar->var_val.var_number = 1; /* Failed */
5937 }
5938}
5939
5940/*
5941 * "inputsave()" function
5942 */
5943/*ARGSUSED*/
5944 static void
5945f_inputsave(argvars, retvar)
5946 VAR argvars;
5947 VAR retvar;
5948{
5949 /* Add an entry to the stack of typehead storage. */
5950 if (ga_grow(&ga_userinput, 1) == OK)
5951 {
5952 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5953 + ga_userinput.ga_len);
5954 ++ga_userinput.ga_len;
5955 --ga_userinput.ga_room;
5956 retvar->var_val.var_number = 0; /* OK */
5957 }
5958 else
5959 retvar->var_val.var_number = 1; /* Failed */
5960}
5961
5962/*
5963 * "inputsecret()" function
5964 */
5965 static void
5966f_inputsecret(argvars, retvar)
5967 VAR argvars;
5968 VAR retvar;
5969{
5970 ++cmdline_star;
5971 ++inputsecret_flag;
5972 f_input(argvars, retvar);
5973 --cmdline_star;
5974 --inputsecret_flag;
5975}
5976
5977/*
5978 * "isdirectory()" function
5979 */
5980 static void
5981f_isdirectory(argvars, retvar)
5982 VAR argvars;
5983 VAR retvar;
5984{
5985 retvar->var_val.var_number = mch_isdir(get_var_string(&argvars[0]));
5986}
5987
5988/*
5989 * "last_buffer_nr()" function.
5990 */
5991/*ARGSUSED*/
5992 static void
5993f_last_buffer_nr(argvars, retvar)
5994 VAR argvars;
5995 VAR retvar;
5996{
5997 int n = 0;
5998 buf_T *buf;
5999
6000 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6001 if (n < buf->b_fnum)
6002 n = buf->b_fnum;
6003
6004 retvar->var_val.var_number = n;
6005}
6006
6007/*
6008 * "line(string)" function
6009 */
6010 static void
6011f_line(argvars, retvar)
6012 VAR argvars;
6013 VAR retvar;
6014{
6015 linenr_T lnum = 0;
6016 pos_T *fp;
6017
6018 fp = var2fpos(&argvars[0], TRUE);
6019 if (fp != NULL)
6020 lnum = fp->lnum;
6021 retvar->var_val.var_number = lnum;
6022}
6023
6024/*
6025 * "line2byte(lnum)" function
6026 */
6027/*ARGSUSED*/
6028 static void
6029f_line2byte(argvars, retvar)
6030 VAR argvars;
6031 VAR retvar;
6032{
6033#ifndef FEAT_BYTEOFF
6034 retvar->var_val.var_number = -1;
6035#else
6036 linenr_T lnum;
6037
6038 lnum = get_var_lnum(argvars);
6039 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6040 retvar->var_val.var_number = -1;
6041 else
6042 retvar->var_val.var_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6043 if (retvar->var_val.var_number >= 0)
6044 ++retvar->var_val.var_number;
6045#endif
6046}
6047
6048/*
6049 * "lispindent(lnum)" function
6050 */
6051 static void
6052f_lispindent(argvars, retvar)
6053 VAR argvars;
6054 VAR retvar;
6055{
6056#ifdef FEAT_LISP
6057 pos_T pos;
6058 linenr_T lnum;
6059
6060 pos = curwin->w_cursor;
6061 lnum = get_var_lnum(argvars);
6062 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6063 {
6064 curwin->w_cursor.lnum = lnum;
6065 retvar->var_val.var_number = get_lisp_indent();
6066 curwin->w_cursor = pos;
6067 }
6068 else
6069#endif
6070 retvar->var_val.var_number = -1;
6071}
6072
6073/*
6074 * "localtime()" function
6075 */
6076/*ARGSUSED*/
6077 static void
6078f_localtime(argvars, retvar)
6079 VAR argvars;
6080 VAR retvar;
6081{
6082 retvar->var_val.var_number = (varnumber_T)time(NULL);
6083}
6084
6085/*
6086 * "maparg()" function
6087 */
6088 static void
6089f_maparg(argvars, retvar)
6090 VAR argvars;
6091 VAR retvar;
6092{
6093 get_maparg(argvars, retvar, TRUE);
6094}
6095
6096/*
6097 * "mapcheck()" function
6098 */
6099 static void
6100f_mapcheck(argvars, retvar)
6101 VAR argvars;
6102 VAR retvar;
6103{
6104 get_maparg(argvars, retvar, FALSE);
6105}
6106
6107 static void
6108get_maparg(argvars, retvar, exact)
6109 VAR argvars;
6110 VAR retvar;
6111 int exact;
6112{
6113 char_u *keys;
6114 char_u *which;
6115 char_u buf[NUMBUFLEN];
6116 char_u *keys_buf = NULL;
6117 char_u *rhs;
6118 int mode;
6119 garray_T ga;
6120
6121 /* return empty string for failure */
6122 retvar->var_type = VAR_STRING;
6123 retvar->var_val.var_string = NULL;
6124
6125 keys = get_var_string(&argvars[0]);
6126 if (*keys == NUL)
6127 return;
6128
6129 if (argvars[1].var_type != VAR_UNKNOWN)
6130 which = get_var_string_buf(&argvars[1], buf);
6131 else
6132 which = (char_u *)"";
6133 mode = get_map_mode(&which, 0);
6134
6135 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
6136 rhs = check_map(keys, mode, exact);
6137 vim_free(keys_buf);
6138 if (rhs != NULL)
6139 {
6140 ga_init(&ga);
6141 ga.ga_itemsize = 1;
6142 ga.ga_growsize = 40;
6143
6144 while (*rhs != NUL)
6145 ga_concat(&ga, str2special(&rhs, FALSE));
6146
6147 ga_append(&ga, NUL);
6148 retvar->var_val.var_string = (char_u *)ga.ga_data;
6149 }
6150}
6151
6152/*
6153 * "match()" function
6154 */
6155 static void
6156f_match(argvars, retvar)
6157 VAR argvars;
6158 VAR retvar;
6159{
6160 find_some_match(argvars, retvar, 1);
6161}
6162
6163/*
6164 * "matchend()" function
6165 */
6166 static void
6167f_matchend(argvars, retvar)
6168 VAR argvars;
6169 VAR retvar;
6170{
6171 find_some_match(argvars, retvar, 0);
6172}
6173
6174/*
6175 * "matchstr()" function
6176 */
6177 static void
6178f_matchstr(argvars, retvar)
6179 VAR argvars;
6180 VAR retvar;
6181{
6182 find_some_match(argvars, retvar, 2);
6183}
6184
6185 static void
6186find_some_match(argvars, retvar, type)
6187 VAR argvars;
6188 VAR retvar;
6189 int type;
6190{
6191 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006192 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 char_u *pat;
6194 regmatch_T regmatch;
6195 char_u patbuf[NUMBUFLEN];
6196 char_u *save_cpo;
6197 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006198 long nth = 1;
6199 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6202 save_cpo = p_cpo;
6203 p_cpo = (char_u *)"";
6204
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006205 expr = str = get_var_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 pat = get_var_string_buf(&argvars[1], patbuf);
6207
6208 if (type == 2)
6209 {
6210 retvar->var_type = VAR_STRING;
6211 retvar->var_val.var_string = NULL;
6212 }
6213 else
6214 retvar->var_val.var_number = -1;
6215
6216 if (argvars[2].var_type != VAR_UNKNOWN)
6217 {
6218 start = get_var_number(&argvars[2]);
6219 if (start < 0)
6220 start = 0;
6221 if (start > (long)STRLEN(str))
6222 goto theend;
6223 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006224
6225 if (argvars[3].var_type != VAR_UNKNOWN)
6226 nth = get_var_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228
6229 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6230 if (regmatch.regprog != NULL)
6231 {
6232 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006233
6234 while (1)
6235 {
6236 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
6237 if (!match || --nth <= 0)
6238 break;
6239 /* Advance to just after the match. */
6240#ifdef FEAT_MBYTE
6241 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
6242#else
6243 str = regmatch.startp[0] + 1;
6244#endif
6245 }
6246
6247 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
6249 if (type == 2)
6250 retvar->var_val.var_string = vim_strnsave(regmatch.startp[0],
6251 (int)(regmatch.endp[0] - regmatch.startp[0]));
6252 else
6253 {
6254 if (type != 0)
6255 retvar->var_val.var_number =
6256 (varnumber_T)(regmatch.startp[0] - str);
6257 else
6258 retvar->var_val.var_number =
6259 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006260 retvar->var_val.var_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262 }
6263 vim_free(regmatch.regprog);
6264 }
6265
6266theend:
6267 p_cpo = save_cpo;
6268}
6269
6270/*
6271 * "mode()" function
6272 */
6273/*ARGSUSED*/
6274 static void
6275f_mode(argvars, retvar)
6276 VAR argvars;
6277 VAR retvar;
6278{
6279 char_u buf[2];
6280
6281#ifdef FEAT_VISUAL
6282 if (VIsual_active)
6283 {
6284 if (VIsual_select)
6285 buf[0] = VIsual_mode + 's' - 'v';
6286 else
6287 buf[0] = VIsual_mode;
6288 }
6289 else
6290#endif
6291 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
6292 buf[0] = 'r';
6293 else if (State & INSERT)
6294 {
6295 if (State & REPLACE_FLAG)
6296 buf[0] = 'R';
6297 else
6298 buf[0] = 'i';
6299 }
6300 else if (State & CMDLINE)
6301 buf[0] = 'c';
6302 else
6303 buf[0] = 'n';
6304
6305 buf[1] = NUL;
6306 retvar->var_val.var_string = vim_strsave(buf);
6307 retvar->var_type = VAR_STRING;
6308}
6309
6310/*
6311 * "nr2char()" function
6312 */
6313 static void
6314f_nr2char(argvars, retvar)
6315 VAR argvars;
6316 VAR retvar;
6317{
6318 char_u buf[NUMBUFLEN];
6319
6320#ifdef FEAT_MBYTE
6321 if (has_mbyte)
6322 buf[(*mb_char2bytes)((int)get_var_number(&argvars[0]), buf)] = NUL;
6323 else
6324#endif
6325 {
6326 buf[0] = (char_u)get_var_number(&argvars[0]);
6327 buf[1] = NUL;
6328 }
6329 retvar->var_type = VAR_STRING;
6330 retvar->var_val.var_string = vim_strsave(buf);
6331}
6332
6333/*
6334 * "rename({from}, {to})" function
6335 */
6336 static void
6337f_rename(argvars, retvar)
6338 VAR argvars;
6339 VAR retvar;
6340{
6341 char_u buf[NUMBUFLEN];
6342
6343 if (check_restricted() || check_secure())
6344 retvar->var_val.var_number = -1;
6345 else
6346 retvar->var_val.var_number = vim_rename(get_var_string(&argvars[0]),
6347 get_var_string_buf(&argvars[1], buf));
6348}
6349
6350/*
6351 * "resolve()" function
6352 */
6353 static void
6354f_resolve(argvars, retvar)
6355 VAR argvars;
6356 VAR retvar;
6357{
6358 char_u *p;
6359
6360 p = get_var_string(&argvars[0]);
6361#ifdef FEAT_SHORTCUT
6362 {
6363 char_u *v = NULL;
6364
6365 v = mch_resolve_shortcut(p);
6366 if (v != NULL)
6367 retvar->var_val.var_string = v;
6368 else
6369 retvar->var_val.var_string = vim_strsave(p);
6370 }
6371#else
6372# ifdef HAVE_READLINK
6373 {
6374 char_u buf[MAXPATHL + 1];
6375 char_u *cpy;
6376 int len;
6377 char_u *remain = NULL;
6378 char_u *q;
6379 int is_relative_to_current = FALSE;
6380 int has_trailing_pathsep = FALSE;
6381 int limit = 100;
6382
6383 p = vim_strsave(p);
6384
6385 if (p[0] == '.' && (vim_ispathsep(p[1])
6386 || (p[1] == '.' && (vim_ispathsep(p[2])))))
6387 is_relative_to_current = TRUE;
6388
6389 len = STRLEN(p);
6390 if (len > 0 && vim_ispathsep(p[len-1]))
6391 has_trailing_pathsep = TRUE;
6392
6393 q = getnextcomp(p);
6394 if (*q != NUL)
6395 {
6396 /* Separate the first path component in "p", and keep the
6397 * remainder (beginning with the path separator). */
6398 remain = vim_strsave(q - 1);
6399 q[-1] = NUL;
6400 }
6401
6402 for (;;)
6403 {
6404 for (;;)
6405 {
6406 len = readlink((char *)p, (char *)buf, MAXPATHL);
6407 if (len <= 0)
6408 break;
6409 buf[len] = NUL;
6410
6411 if (limit-- == 0)
6412 {
6413 vim_free(p);
6414 vim_free(remain);
6415 EMSG(_("E655: Too many symbolic links (cycle?)"));
6416 retvar->var_val.var_string = NULL;
6417 goto fail;
6418 }
6419
6420 /* Ensure that the result will have a trailing path separator
6421 * if the argument has one. */
6422 if (remain == NULL && has_trailing_pathsep)
6423 add_pathsep(buf);
6424
6425 /* Separate the first path component in the link value and
6426 * concatenate the remainders. */
6427 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
6428 if (*q != NUL)
6429 {
6430 if (remain == NULL)
6431 remain = vim_strsave(q - 1);
6432 else
6433 {
6434 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
6435 if (cpy != NULL)
6436 {
6437 STRCAT(cpy, remain);
6438 vim_free(remain);
6439 remain = cpy;
6440 }
6441 }
6442 q[-1] = NUL;
6443 }
6444
6445 q = gettail(p);
6446 if (q > p && *q == NUL)
6447 {
6448 /* Ignore trailing path separator. */
6449 q[-1] = NUL;
6450 q = gettail(p);
6451 }
6452 if (q > p && !mch_isFullName(buf))
6453 {
6454 /* symlink is relative to directory of argument */
6455 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
6456 if (cpy != NULL)
6457 {
6458 STRCPY(cpy, p);
6459 STRCPY(gettail(cpy), buf);
6460 vim_free(p);
6461 p = cpy;
6462 }
6463 }
6464 else
6465 {
6466 vim_free(p);
6467 p = vim_strsave(buf);
6468 }
6469 }
6470
6471 if (remain == NULL)
6472 break;
6473
6474 /* Append the first path component of "remain" to "p". */
6475 q = getnextcomp(remain + 1);
6476 len = q - remain - (*q != NUL);
6477 cpy = vim_strnsave(p, STRLEN(p) + len);
6478 if (cpy != NULL)
6479 {
6480 STRNCAT(cpy, remain, len);
6481 vim_free(p);
6482 p = cpy;
6483 }
6484 /* Shorten "remain". */
6485 if (*q != NUL)
6486 STRCPY(remain, q - 1);
6487 else
6488 {
6489 vim_free(remain);
6490 remain = NULL;
6491 }
6492 }
6493
6494 /* If the result is a relative path name, make it explicitly relative to
6495 * the current directory if and only if the argument had this form. */
6496 if (!vim_ispathsep(*p))
6497 {
6498 if (is_relative_to_current
6499 && *p != NUL
6500 && !(p[0] == '.'
6501 && (p[1] == NUL
6502 || vim_ispathsep(p[1])
6503 || (p[1] == '.'
6504 && (p[2] == NUL
6505 || vim_ispathsep(p[2]))))))
6506 {
6507 /* Prepend "./". */
6508 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
6509 if (cpy != NULL)
6510 {
6511 STRCAT(cpy, p);
6512 vim_free(p);
6513 p = cpy;
6514 }
6515 }
6516 else if (!is_relative_to_current)
6517 {
6518 /* Strip leading "./". */
6519 q = p;
6520 while (q[0] == '.' && vim_ispathsep(q[1]))
6521 q += 2;
6522 if (q > p)
6523 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
6524 }
6525 }
6526
6527 /* Ensure that the result will have no trailing path separator
6528 * if the argument had none. But keep "/" or "//". */
6529 if (!has_trailing_pathsep)
6530 {
6531 q = p + STRLEN(p);
6532 while ((q > p + 2 || (q == p + 2 && !vim_ispathsep(*p)))
6533 && vim_ispathsep(q[-1]))
6534 --q;
6535 *q = NUL;
6536 }
6537
6538 retvar->var_val.var_string = p;
6539 }
6540# else
6541 retvar->var_val.var_string = vim_strsave(p);
6542# endif
6543#endif
6544
6545 simplify_filename(retvar->var_val.var_string);
6546
6547#ifdef HAVE_READLINK
6548fail:
6549#endif
6550 retvar->var_type = VAR_STRING;
6551}
6552
6553/*
6554 * "simplify()" function
6555 */
6556 static void
6557f_simplify(argvars, retvar)
6558 VAR argvars;
6559 VAR retvar;
6560{
6561 char_u *p;
6562
6563 p = get_var_string(&argvars[0]);
6564 retvar->var_val.var_string = vim_strsave(p);
6565 simplify_filename(retvar->var_val.var_string); /* simplify in place */
6566 retvar->var_type = VAR_STRING;
6567}
6568
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006569#define SP_NOMOVE 1 /* don't move cursor */
6570#define SP_REPEAT 2 /* repeat to find outer pair */
6571#define SP_RETCOUNT 4 /* return matchcount */
6572
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573/*
6574 * "search()" function
6575 */
6576 static void
6577f_search(argvars, retvar)
6578 VAR argvars;
6579 VAR retvar;
6580{
6581 char_u *pat;
6582 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006583 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 int save_p_ws = p_ws;
6585 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006586 int flags = 0;
6587
6588 retvar->var_val.var_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
6590 pat = get_var_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006591 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
6592 if (dir == 0)
6593 goto theend;
6594 if ((flags & ~SP_NOMOVE) != 0)
6595 {
6596 EMSG2(_(e_invarg2), get_var_string(&argvars[1]));
6597 goto theend;
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006600 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
6602 SEARCH_KEEP, RE_SEARCH) != FAIL)
6603 {
6604 retvar->var_val.var_number = pos.lnum;
6605 curwin->w_cursor = pos;
6606 /* "/$" will put the cursor after the end of the line, may need to
6607 * correct that here */
6608 check_cursor();
6609 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006610
6611 /* If 'n' flag is used: restore cursor position. */
6612 if (flags & SP_NOMOVE)
6613 curwin->w_cursor = save_cursor;
6614theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 p_ws = save_p_ws;
6616}
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618/*
6619 * "searchpair()" function
6620 */
6621 static void
6622f_searchpair(argvars, retvar)
6623 VAR argvars;
6624 VAR retvar;
6625{
6626 char_u *spat, *mpat, *epat;
6627 char_u *skip;
6628 char_u *pat, *pat2, *pat3;
6629 pos_T pos;
6630 pos_T firstpos;
6631 pos_T save_cursor;
6632 pos_T save_pos;
6633 int save_p_ws = p_ws;
6634 char_u *save_cpo;
6635 int dir;
6636 int flags = 0;
6637 char_u nbuf1[NUMBUFLEN];
6638 char_u nbuf2[NUMBUFLEN];
6639 char_u nbuf3[NUMBUFLEN];
6640 int n;
6641 int r;
6642 int nest = 1;
6643 int err;
6644
6645 retvar->var_val.var_number = 0; /* default: FAIL */
6646
6647 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6648 save_cpo = p_cpo;
6649 p_cpo = (char_u *)"";
6650
6651 /* Get the three pattern arguments: start, middle, end. */
6652 spat = get_var_string(&argvars[0]);
6653 mpat = get_var_string_buf(&argvars[1], nbuf1);
6654 epat = get_var_string_buf(&argvars[2], nbuf2);
6655
6656 /* Make two search patterns: start/end (pat2, for in nested pairs) and
6657 * start/middle/end (pat3, for the top pair). */
6658 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
6659 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
6660 if (pat2 == NULL || pat3 == NULL)
6661 goto theend;
6662 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
6663 if (*mpat == NUL)
6664 STRCPY(pat3, pat2);
6665 else
6666 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
6667 spat, epat, mpat);
6668
6669 /* Handle the optional fourth argument: flags */
6670 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006671 if (dir == 0)
6672 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 /* Optional fifth argument: skip expresion */
6675 if (argvars[3].var_type == VAR_UNKNOWN
6676 || argvars[4].var_type == VAR_UNKNOWN)
6677 skip = (char_u *)"";
6678 else
6679 skip = get_var_string_buf(&argvars[4], nbuf3);
6680
6681 save_cursor = curwin->w_cursor;
6682 pos = curwin->w_cursor;
6683 firstpos.lnum = 0;
6684 pat = pat3;
6685 for (;;)
6686 {
6687 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
6688 SEARCH_KEEP, RE_SEARCH);
6689 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
6690 /* didn't find it or found the first match again: FAIL */
6691 break;
6692
6693 if (firstpos.lnum == 0)
6694 firstpos = pos;
6695
6696 /* If the skip pattern matches, ignore this match. */
6697 if (*skip != NUL)
6698 {
6699 save_pos = curwin->w_cursor;
6700 curwin->w_cursor = pos;
6701 r = eval_to_bool(skip, &err, NULL, FALSE);
6702 curwin->w_cursor = save_pos;
6703 if (err)
6704 {
6705 /* Evaluating {skip} caused an error, break here. */
6706 curwin->w_cursor = save_cursor;
6707 retvar->var_val.var_number = -1;
6708 break;
6709 }
6710 if (r)
6711 continue;
6712 }
6713
6714 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
6715 {
6716 /* Found end when searching backwards or start when searching
6717 * forward: nested pair. */
6718 ++nest;
6719 pat = pat2; /* nested, don't search for middle */
6720 }
6721 else
6722 {
6723 /* Found end when searching forward or start when searching
6724 * backward: end of (nested) pair; or found middle in outer pair. */
6725 if (--nest == 1)
6726 pat = pat3; /* outer level, search for middle */
6727 }
6728
6729 if (nest == 0)
6730 {
6731 /* Found the match: return matchcount or line number. */
6732 if (flags & SP_RETCOUNT)
6733 ++retvar->var_val.var_number;
6734 else
6735 retvar->var_val.var_number = pos.lnum;
6736 curwin->w_cursor = pos;
6737 if (!(flags & SP_REPEAT))
6738 break;
6739 nest = 1; /* search for next unmatched */
6740 }
6741 }
6742
6743 /* If 'n' flag is used or search failed: restore cursor position. */
6744 if ((flags & SP_NOMOVE) || retvar->var_val.var_number == 0)
6745 curwin->w_cursor = save_cursor;
6746
6747theend:
6748 vim_free(pat2);
6749 vim_free(pat3);
6750 p_ws = save_p_ws;
6751 p_cpo = save_cpo;
6752}
6753
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006754/*
6755 * Get flags for a search function.
6756 * Possibly sets "p_ws".
6757 * Returns BACKWARD, FORWARD or zero (for an error).
6758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 static int
6760get_search_arg(varp, flagsp)
6761 VAR varp;
6762 int *flagsp;
6763{
6764 int dir = FORWARD;
6765 char_u *flags;
6766 char_u nbuf[NUMBUFLEN];
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006767 int mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
6769 if (varp->var_type != VAR_UNKNOWN)
6770 {
6771 flags = get_var_string_buf(varp, nbuf);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006772 while (*flags != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006774 switch (*flags)
6775 {
6776 case 'b': dir = BACKWARD; break;
6777 case 'w': p_ws = TRUE; break;
6778 case 'W': p_ws = FALSE; break;
6779 default: mask = 0;
6780 if (flagsp != NULL)
6781 switch (*flags)
6782 {
6783 case 'n': mask = SP_NOMOVE; break;
6784 case 'r': mask = SP_REPEAT; break;
6785 case 'm': mask = SP_RETCOUNT; break;
6786 }
6787 if (mask == 0)
6788 {
6789 EMSG2(_(e_invarg2), flags);
6790 dir = 0;
6791 }
6792 else
6793 *flagsp |= mask;
6794 }
6795 if (dir == 0)
6796 break;
6797 ++flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
6799 }
6800 return dir;
6801}
6802
6803/*
6804 * "setbufvar()" function
6805 */
6806/*ARGSUSED*/
6807 static void
6808f_setbufvar(argvars, retvar)
6809 VAR argvars;
6810 VAR retvar;
6811{
6812 buf_T *buf;
6813#ifdef FEAT_AUTOCMD
6814 aco_save_T aco;
6815#else
6816 buf_T *save_curbuf;
6817#endif
6818 char_u *varname, *bufvarname;
6819 VAR varp;
6820 char_u nbuf[NUMBUFLEN];
6821
6822 if (check_restricted() || check_secure())
6823 return;
6824 ++emsg_off;
6825 buf = get_buf_var(&argvars[0]);
6826 varname = get_var_string(&argvars[1]);
6827 varp = &argvars[2];
6828
6829 if (buf != NULL && varname != NULL && varp != NULL)
6830 {
6831 /* set curbuf to be our buf, temporarily */
6832#ifdef FEAT_AUTOCMD
6833 aucmd_prepbuf(&aco, buf);
6834#else
6835 save_curbuf = curbuf;
6836 curbuf = buf;
6837#endif
6838
6839 if (*varname == '&')
6840 {
6841 ++varname;
6842 set_option_value(varname, get_var_number(varp),
6843 get_var_string_buf(varp, nbuf), OPT_LOCAL);
6844 }
6845 else
6846 {
6847 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
6848 if (bufvarname != NULL)
6849 {
6850 STRCPY(bufvarname, "b:");
6851 STRCPY(bufvarname + 2, varname);
6852 set_var(bufvarname, varp);
6853 vim_free(bufvarname);
6854 }
6855 }
6856
6857 /* reset notion of buffer */
6858#ifdef FEAT_AUTOCMD
6859 aucmd_restbuf(&aco);
6860#else
6861 curbuf = save_curbuf;
6862#endif
6863 }
6864 --emsg_off;
6865}
6866
6867/*
6868 * "setcmdpos()" function
6869 */
6870 static void
6871f_setcmdpos(argvars, retvar)
6872 VAR argvars;
6873 VAR retvar;
6874{
6875 retvar->var_val.var_number = set_cmdline_pos(
6876 (int)get_var_number(&argvars[0]) - 1);
6877}
6878
6879/*
6880 * "setline()" function
6881 */
6882 static void
6883f_setline(argvars, retvar)
6884 VAR argvars;
6885 VAR retvar;
6886{
6887 linenr_T lnum;
6888 char_u *line;
6889
6890 lnum = get_var_lnum(argvars);
6891 line = get_var_string(&argvars[1]);
6892 retvar->var_val.var_number = 1; /* FAIL is default */
6893
6894 if (lnum >= 1
6895 && lnum <= curbuf->b_ml.ml_line_count
6896 && u_savesub(lnum) == OK
6897 && ml_replace(lnum, line, TRUE) == OK)
6898 {
6899 changed_bytes(lnum, 0);
6900 check_cursor_col();
6901 retvar->var_val.var_number = 0;
6902 }
6903}
6904
6905/*
6906 * "setreg()" function
6907 */
6908 static void
6909f_setreg(argvars, retvar)
6910 VAR argvars;
6911 VAR retvar;
6912{
6913 int regname;
6914 char_u *strregname;
6915 char_u *stropt;
6916 int append;
6917 char_u yank_type;
6918 long block_len;
6919
6920 block_len = -1;
6921 yank_type = MAUTO;
6922 append = FALSE;
6923
6924 strregname = get_var_string(argvars);
6925 retvar->var_val.var_number = 1; /* FAIL is default */
6926
6927 regname = (strregname == NULL ? '"' : *strregname);
6928 if (regname == 0 || regname == '@')
6929 regname = '"';
6930 else if (regname == '=')
6931 return;
6932
6933 if (argvars[2].var_type != VAR_UNKNOWN)
6934 {
6935 for (stropt = get_var_string(&argvars[2]); *stropt != NUL; ++stropt)
6936 switch (*stropt)
6937 {
6938 case 'a': case 'A': /* append */
6939 append = TRUE;
6940 break;
6941 case 'v': case 'c': /* character-wise selection */
6942 yank_type = MCHAR;
6943 break;
6944 case 'V': case 'l': /* line-wise selection */
6945 yank_type = MLINE;
6946 break;
6947#ifdef FEAT_VISUAL
6948 case 'b': case Ctrl_V: /* block-wise selection */
6949 yank_type = MBLOCK;
6950 if (VIM_ISDIGIT(stropt[1]))
6951 {
6952 ++stropt;
6953 block_len = getdigits(&stropt) - 1;
6954 --stropt;
6955 }
6956 break;
6957#endif
6958 }
6959 }
6960
6961 write_reg_contents_ex(regname, get_var_string(&argvars[1]), -1,
6962 append, yank_type, block_len);
6963 retvar->var_val.var_number = 0;
6964}
6965
6966
6967/*
6968 * "setwinvar(expr)" function
6969 */
6970/*ARGSUSED*/
6971 static void
6972f_setwinvar(argvars, retvar)
6973 VAR argvars;
6974 VAR retvar;
6975{
6976 win_T *win;
6977#ifdef FEAT_WINDOWS
6978 win_T *save_curwin;
6979#endif
6980 char_u *varname, *winvarname;
6981 VAR varp;
6982 char_u nbuf[NUMBUFLEN];
6983
6984 if (check_restricted() || check_secure())
6985 return;
6986 ++emsg_off;
6987 win = find_win_by_nr(&argvars[0]);
6988 varname = get_var_string(&argvars[1]);
6989 varp = &argvars[2];
6990
6991 if (win != NULL && varname != NULL && varp != NULL)
6992 {
6993#ifdef FEAT_WINDOWS
6994 /* set curwin to be our win, temporarily */
6995 save_curwin = curwin;
6996 curwin = win;
6997 curbuf = curwin->w_buffer;
6998#endif
6999
7000 if (*varname == '&')
7001 {
7002 ++varname;
7003 set_option_value(varname, get_var_number(varp),
7004 get_var_string_buf(varp, nbuf), OPT_LOCAL);
7005 }
7006 else
7007 {
7008 winvarname = alloc((unsigned)STRLEN(varname) + 3);
7009 if (winvarname != NULL)
7010 {
7011 STRCPY(winvarname, "w:");
7012 STRCPY(winvarname + 2, varname);
7013 set_var(winvarname, varp);
7014 vim_free(winvarname);
7015 }
7016 }
7017
7018#ifdef FEAT_WINDOWS
7019 /* Restore current window, if it's still valid (autocomands can make
7020 * it invalid). */
7021 if (win_valid(save_curwin))
7022 {
7023 curwin = save_curwin;
7024 curbuf = curwin->w_buffer;
7025 }
7026#endif
7027 }
7028 --emsg_off;
7029}
7030
7031/*
7032 * "nextnonblank()" function
7033 */
7034 static void
7035f_nextnonblank(argvars, retvar)
7036 VAR argvars;
7037 VAR retvar;
7038{
7039 linenr_T lnum;
7040
7041 for (lnum = get_var_lnum(argvars); ; ++lnum)
7042 {
7043 if (lnum > curbuf->b_ml.ml_line_count)
7044 {
7045 lnum = 0;
7046 break;
7047 }
7048 if (*skipwhite(ml_get(lnum)) != NUL)
7049 break;
7050 }
7051 retvar->var_val.var_number = lnum;
7052}
7053
7054/*
7055 * "prevnonblank()" function
7056 */
7057 static void
7058f_prevnonblank(argvars, retvar)
7059 VAR argvars;
7060 VAR retvar;
7061{
7062 linenr_T lnum;
7063
7064 lnum = get_var_lnum(argvars);
7065 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7066 lnum = 0;
7067 else
7068 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7069 --lnum;
7070 retvar->var_val.var_number = lnum;
7071}
7072
7073#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
7074static void make_connection __ARGS((void));
7075static int check_connection __ARGS((void));
7076
7077 static void
7078make_connection()
7079{
7080 if (X_DISPLAY == NULL
7081# ifdef FEAT_GUI
7082 && !gui.in_use
7083# endif
7084 )
7085 {
7086 x_force_connect = TRUE;
7087 setup_term_clip();
7088 x_force_connect = FALSE;
7089 }
7090}
7091
7092 static int
7093check_connection()
7094{
7095 make_connection();
7096 if (X_DISPLAY == NULL)
7097 {
7098 EMSG(_("E240: No connection to Vim server"));
7099 return FAIL;
7100 }
7101 return OK;
7102}
7103#endif
7104
7105/*ARGSUSED*/
7106 static void
7107f_serverlist(argvars, retvar)
7108 VAR argvars;
7109 VAR retvar;
7110{
7111 char_u *r = NULL;
7112
7113#ifdef FEAT_CLIENTSERVER
7114# ifdef WIN32
7115 r = serverGetVimNames();
7116# else
7117 make_connection();
7118 if (X_DISPLAY != NULL)
7119 r = serverGetVimNames(X_DISPLAY);
7120# endif
7121#endif
7122 retvar->var_type = VAR_STRING;
7123 retvar->var_val.var_string = r;
7124}
7125
7126/*ARGSUSED*/
7127 static void
7128f_remote_peek(argvars, retvar)
7129 VAR argvars;
7130 VAR retvar;
7131{
7132#ifdef FEAT_CLIENTSERVER
7133 var v;
7134 char_u *s = NULL;
7135# ifdef WIN32
7136 int n = 0;
7137# endif
7138
7139 if (check_restricted() || check_secure())
7140 {
7141 retvar->var_val.var_number = -1;
7142 return;
7143 }
7144# ifdef WIN32
7145 sscanf(get_var_string(&argvars[0]), "%x", &n);
7146 if (n == 0)
7147 retvar->var_val.var_number = -1;
7148 else
7149 {
7150 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
7151 retvar->var_val.var_number = (s != NULL);
7152 }
7153# else
7154 retvar->var_val.var_number = 0;
7155 if (check_connection() == FAIL)
7156 return;
7157
7158 retvar->var_val.var_number = serverPeekReply(X_DISPLAY,
7159 serverStrToWin(get_var_string(&argvars[0])), &s);
7160# endif
7161
7162 if (argvars[1].var_type != VAR_UNKNOWN && retvar->var_val.var_number > 0)
7163 {
7164 v.var_type = VAR_STRING;
7165 v.var_val.var_string = vim_strsave(s);
7166 set_var(get_var_string(&argvars[1]), &v);
7167 }
7168#else
7169 retvar->var_val.var_number = -1;
7170#endif
7171}
7172
7173/*ARGSUSED*/
7174 static void
7175f_remote_read(argvars, retvar)
7176 VAR argvars;
7177 VAR retvar;
7178{
7179 char_u *r = NULL;
7180
7181#ifdef FEAT_CLIENTSERVER
7182 if (!check_restricted() && !check_secure())
7183 {
7184# ifdef WIN32
7185 /* The server's HWND is encoded in the 'id' parameter */
7186 int n = 0;
7187
7188 sscanf(get_var_string(&argvars[0]), "%x", &n);
7189 if (n != 0)
7190 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
7191 if (r == NULL)
7192# else
7193 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
7194 serverStrToWin(get_var_string(&argvars[0])), &r, FALSE) < 0)
7195# endif
7196 EMSG(_("E277: Unable to read a server reply"));
7197 }
7198#endif
7199 retvar->var_type = VAR_STRING;
7200 retvar->var_val.var_string = r;
7201}
7202
7203/*ARGSUSED*/
7204 static void
7205f_server2client(argvars, retvar)
7206 VAR argvars;
7207 VAR retvar;
7208{
7209#ifdef FEAT_CLIENTSERVER
7210 char_u buf[NUMBUFLEN];
7211 char_u *server = get_var_string(&argvars[0]);
7212 char_u *reply = get_var_string_buf(&argvars[1], buf);
7213
7214 retvar->var_val.var_number = -1;
7215 if (check_restricted() || check_secure())
7216 return;
7217# ifdef FEAT_X11
7218 if (check_connection() == FAIL)
7219 return;
7220# endif
7221
7222 if (serverSendReply(server, reply) < 0)
7223 {
7224 EMSG(_("E258: Unable to send to client"));
7225 return;
7226 }
7227 retvar->var_val.var_number = 0;
7228#else
7229 retvar->var_val.var_number = -1;
7230#endif
7231}
7232
7233#ifdef FEAT_CLIENTSERVER
7234static void remote_common __ARGS((VAR argvars, VAR retvar, int expr));
7235
7236 static void
7237remote_common(argvars, retvar, expr)
7238 VAR argvars;
7239 VAR retvar;
7240 int expr;
7241{
7242 char_u *server_name;
7243 char_u *keys;
7244 char_u *r = NULL;
7245 char_u buf[NUMBUFLEN];
7246# ifdef WIN32
7247 HWND w;
7248# else
7249 Window w;
7250# endif
7251
7252 if (check_restricted() || check_secure())
7253 return;
7254
7255# ifdef FEAT_X11
7256 if (check_connection() == FAIL)
7257 return;
7258# endif
7259
7260 server_name = get_var_string(&argvars[0]);
7261 keys = get_var_string_buf(&argvars[1], buf);
7262# ifdef WIN32
7263 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
7264# else
7265 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
7266 < 0)
7267# endif
7268 {
7269 if (r != NULL)
7270 EMSG(r); /* sending worked but evaluation failed */
7271 else
7272 EMSG2(_("E241: Unable to send to %s"), server_name);
7273 return;
7274 }
7275
7276 retvar->var_val.var_string = r;
7277
7278 if (argvars[2].var_type != VAR_UNKNOWN)
7279 {
7280 var v;
7281 char_u str[30];
7282
7283 sprintf((char *)str, "0x%x", (unsigned int)w);
7284 v.var_type = VAR_STRING;
7285 v.var_val.var_string = vim_strsave(str);
7286 set_var(get_var_string(&argvars[2]), &v);
7287 }
7288}
7289#endif
7290
7291/*
7292 * "remote_expr()" function
7293 */
7294/*ARGSUSED*/
7295 static void
7296f_remote_expr(argvars, retvar)
7297 VAR argvars;
7298 VAR retvar;
7299{
7300 retvar->var_type = VAR_STRING;
7301 retvar->var_val.var_string = NULL;
7302#ifdef FEAT_CLIENTSERVER
7303 remote_common(argvars, retvar, TRUE);
7304#endif
7305}
7306
7307/*
7308 * "remote_send()" function
7309 */
7310/*ARGSUSED*/
7311 static void
7312f_remote_send(argvars, retvar)
7313 VAR argvars;
7314 VAR retvar;
7315{
7316 retvar->var_type = VAR_STRING;
7317 retvar->var_val.var_string = NULL;
7318#ifdef FEAT_CLIENTSERVER
7319 remote_common(argvars, retvar, FALSE);
7320#endif
7321}
7322
7323/*
7324 * "remote_foreground()" function
7325 */
7326/*ARGSUSED*/
7327 static void
7328f_remote_foreground(argvars, retvar)
7329 VAR argvars;
7330 VAR retvar;
7331{
7332 retvar->var_val.var_number = 0;
7333#ifdef FEAT_CLIENTSERVER
7334# ifdef WIN32
7335 /* On Win32 it's done in this application. */
7336 serverForeground(get_var_string(&argvars[0]));
7337# else
7338 /* Send a foreground() expression to the server. */
7339 argvars[1].var_type = VAR_STRING;
7340 argvars[1].var_val.var_string = vim_strsave((char_u *)"foreground()");
7341 argvars[2].var_type = VAR_UNKNOWN;
7342 remote_common(argvars, retvar, TRUE);
7343 vim_free(argvars[1].var_val.var_string);
7344# endif
7345#endif
7346}
7347
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007348/*
7349 * "repeat()" function
7350 */
7351/*ARGSUSED*/
7352 static void
7353f_repeat(argvars, retvar)
7354 VAR argvars;
7355 VAR retvar;
7356{
7357 char_u *p;
7358 int n;
7359 int slen;
7360 int len;
7361 char_u *r;
7362 int i;
7363
7364 p = get_var_string(&argvars[0]);
7365 n = get_var_number(&argvars[1]);
7366
7367 retvar->var_type = VAR_STRING;
7368 retvar->var_val.var_string = NULL;
7369
7370 slen = (int)STRLEN(p);
7371 len = slen * n;
7372
7373 if (len <= 0)
7374 return;
7375
7376 r = alloc(len + 1);
7377 if (r != NULL)
7378 {
7379 for (i = 0; i < n; i++)
7380 mch_memmove(r + i * slen, p, (size_t)slen);
7381 r[len] = NUL;
7382 }
7383
7384 retvar->var_val.var_string = r;
7385}
7386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387#ifdef HAVE_STRFTIME
7388/*
7389 * "strftime({format}[, {time}])" function
7390 */
7391 static void
7392f_strftime(argvars, retvar)
7393 VAR argvars;
7394 VAR retvar;
7395{
7396 char_u result_buf[256];
7397 struct tm *curtime;
7398 time_t seconds;
7399 char_u *p;
7400
7401 retvar->var_type = VAR_STRING;
7402
7403 p = get_var_string(&argvars[0]);
7404 if (argvars[1].var_type == VAR_UNKNOWN)
7405 seconds = time(NULL);
7406 else
7407 seconds = (time_t)get_var_number(&argvars[1]);
7408 curtime = localtime(&seconds);
7409 /* MSVC returns NULL for an invalid value of seconds. */
7410 if (curtime == NULL)
7411 retvar->var_val.var_string = vim_strsave((char_u *)_("(Invalid)"));
7412 else
7413 {
7414# ifdef FEAT_MBYTE
7415 vimconv_T conv;
7416 char_u *enc;
7417
7418 conv.vc_type = CONV_NONE;
7419 enc = enc_locale();
7420 convert_setup(&conv, p_enc, enc);
7421 if (conv.vc_type != CONV_NONE)
7422 p = string_convert(&conv, p, NULL);
7423# endif
7424 if (p != NULL)
7425 (void)strftime((char *)result_buf, sizeof(result_buf),
7426 (char *)p, curtime);
7427 else
7428 result_buf[0] = NUL;
7429
7430# ifdef FEAT_MBYTE
7431 if (conv.vc_type != CONV_NONE)
7432 vim_free(p);
7433 convert_setup(&conv, enc, p_enc);
7434 if (conv.vc_type != CONV_NONE)
7435 retvar->var_val.var_string =
7436 string_convert(&conv, result_buf, NULL);
7437 else
7438# endif
7439 retvar->var_val.var_string = vim_strsave(result_buf);
7440
7441# ifdef FEAT_MBYTE
7442 /* Release conversion descriptors */
7443 convert_setup(&conv, NULL, NULL);
7444 vim_free(enc);
7445# endif
7446 }
7447}
7448#endif
7449
7450/*
7451 * "stridx()" function
7452 */
7453 static void
7454f_stridx(argvars, retvar)
7455 VAR argvars;
7456 VAR retvar;
7457{
7458 char_u buf[NUMBUFLEN];
7459 char_u *needle;
7460 char_u *haystack;
7461 char_u *pos;
7462
7463 needle = get_var_string(&argvars[1]);
7464 haystack = get_var_string_buf(&argvars[0], buf);
7465 pos = (char_u *)strstr((char *)haystack, (char *)needle);
7466
7467 if (pos == NULL)
7468 retvar->var_val.var_number = -1;
7469 else
7470 retvar->var_val.var_number = (varnumber_T) (pos - haystack);
7471}
7472
7473/*
7474 * "strridx()" function
7475 */
7476 static void
7477f_strridx(argvars, retvar)
7478 VAR argvars;
7479 VAR retvar;
7480{
7481 char_u buf[NUMBUFLEN];
7482 char_u *needle;
7483 char_u *haystack;
7484 char_u *rest;
7485 char_u *lastmatch = NULL;
7486
7487 needle = get_var_string(&argvars[1]);
7488 haystack = get_var_string_buf(&argvars[0], buf);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007489 if (*needle == NUL)
7490 /* Empty string matches past the end. */
7491 lastmatch = haystack + STRLEN(haystack);
7492 else
7493 for (rest = haystack; *rest != '\0'; ++rest)
7494 {
7495 rest = (char_u *)strstr((char *)rest, (char *)needle);
7496 if (rest == NULL)
7497 break;
7498 lastmatch = rest;
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 if (lastmatch == NULL)
7502 retvar->var_val.var_number = -1;
7503 else
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007504 retvar->var_val.var_number = (varnumber_T)(lastmatch - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505}
7506
7507/*
7508 * "strlen()" function
7509 */
7510 static void
7511f_strlen(argvars, retvar)
7512 VAR argvars;
7513 VAR retvar;
7514{
7515 retvar->var_val.var_number = (varnumber_T) (STRLEN(get_var_string(&argvars[0])));
7516}
7517
7518/*
7519 * "strpart()" function
7520 */
7521 static void
7522f_strpart(argvars, retvar)
7523 VAR argvars;
7524 VAR retvar;
7525{
7526 char_u *p;
7527 int n;
7528 int len;
7529 int slen;
7530
7531 p = get_var_string(&argvars[0]);
7532 slen = (int)STRLEN(p);
7533
7534 n = get_var_number(&argvars[1]);
7535 if (argvars[2].var_type != VAR_UNKNOWN)
7536 len = get_var_number(&argvars[2]);
7537 else
7538 len = slen - n; /* default len: all bytes that are available. */
7539
7540 /*
7541 * Only return the overlap between the specified part and the actual
7542 * string.
7543 */
7544 if (n < 0)
7545 {
7546 len += n;
7547 n = 0;
7548 }
7549 else if (n > slen)
7550 n = slen;
7551 if (len < 0)
7552 len = 0;
7553 else if (n + len > slen)
7554 len = slen - n;
7555
7556 retvar->var_type = VAR_STRING;
7557 retvar->var_val.var_string = vim_strnsave(p + n, len);
7558}
7559
7560/*
7561 * "strtrans()" function
7562 */
7563 static void
7564f_strtrans(argvars, retvar)
7565 VAR argvars;
7566 VAR retvar;
7567{
7568 retvar->var_type = VAR_STRING;
7569 retvar->var_val.var_string = transstr(get_var_string(&argvars[0]));
7570}
7571
7572/*
7573 * "synID(line, col, trans)" function
7574 */
7575/*ARGSUSED*/
7576 static void
7577f_synID(argvars, retvar)
7578 VAR argvars;
7579 VAR retvar;
7580{
7581 int id = 0;
7582#ifdef FEAT_SYN_HL
7583 long line;
7584 long col;
7585 int trans;
7586
7587 line = get_var_lnum(argvars);
7588 col = get_var_number(&argvars[1]) - 1;
7589 trans = get_var_number(&argvars[2]);
7590
7591 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
7592 && col >= 0 && col < (long)STRLEN(ml_get(line)))
7593 id = syn_get_id(line, col, trans);
7594#endif
7595
7596 retvar->var_val.var_number = id;
7597}
7598
7599/*
7600 * "synIDattr(id, what [, mode])" function
7601 */
7602/*ARGSUSED*/
7603 static void
7604f_synIDattr(argvars, retvar)
7605 VAR argvars;
7606 VAR retvar;
7607{
7608 char_u *p = NULL;
7609#ifdef FEAT_SYN_HL
7610 int id;
7611 char_u *what;
7612 char_u *mode;
7613 char_u modebuf[NUMBUFLEN];
7614 int modec;
7615
7616 id = get_var_number(&argvars[0]);
7617 what = get_var_string(&argvars[1]);
7618 if (argvars[2].var_type != VAR_UNKNOWN)
7619 {
7620 mode = get_var_string_buf(&argvars[2], modebuf);
7621 modec = TOLOWER_ASC(mode[0]);
7622 if (modec != 't' && modec != 'c'
7623#ifdef FEAT_GUI
7624 && modec != 'g'
7625#endif
7626 )
7627 modec = 0; /* replace invalid with current */
7628 }
7629 else
7630 {
7631#ifdef FEAT_GUI
7632 if (gui.in_use)
7633 modec = 'g';
7634 else
7635#endif
7636 if (t_colors > 1)
7637 modec = 'c';
7638 else
7639 modec = 't';
7640 }
7641
7642
7643 switch (TOLOWER_ASC(what[0]))
7644 {
7645 case 'b':
7646 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
7647 p = highlight_color(id, what, modec);
7648 else /* bold */
7649 p = highlight_has_attr(id, HL_BOLD, modec);
7650 break;
7651
7652 case 'f': /* fg[#] */
7653 p = highlight_color(id, what, modec);
7654 break;
7655
7656 case 'i':
7657 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
7658 p = highlight_has_attr(id, HL_INVERSE, modec);
7659 else /* italic */
7660 p = highlight_has_attr(id, HL_ITALIC, modec);
7661 break;
7662
7663 case 'n': /* name */
7664 p = get_highlight_name(NULL, id - 1);
7665 break;
7666
7667 case 'r': /* reverse */
7668 p = highlight_has_attr(id, HL_INVERSE, modec);
7669 break;
7670
7671 case 's': /* standout */
7672 p = highlight_has_attr(id, HL_STANDOUT, modec);
7673 break;
7674
7675 case 'u': /* underline */
7676 p = highlight_has_attr(id, HL_UNDERLINE, modec);
7677 break;
7678 }
7679
7680 if (p != NULL)
7681 p = vim_strsave(p);
7682#endif
7683 retvar->var_type = VAR_STRING;
7684 retvar->var_val.var_string = p;
7685}
7686
7687/*
7688 * "synIDtrans(id)" function
7689 */
7690/*ARGSUSED*/
7691 static void
7692f_synIDtrans(argvars, retvar)
7693 VAR argvars;
7694 VAR retvar;
7695{
7696 int id;
7697
7698#ifdef FEAT_SYN_HL
7699 id = get_var_number(&argvars[0]);
7700
7701 if (id > 0)
7702 id = syn_get_final_id(id);
7703 else
7704#endif
7705 id = 0;
7706
7707 retvar->var_val.var_number = id;
7708}
7709
7710/*
7711 * "system()" function
7712 */
7713 static void
7714f_system(argvars, retvar)
7715 VAR argvars;
7716 VAR retvar;
7717{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007718 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007720 char_u *infile = NULL;
7721 char_u buf[NUMBUFLEN];
7722 int err = FALSE;
7723 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007725 if (argvars[1].var_type != VAR_UNKNOWN)
7726 {
7727 /*
7728 * Write the string to a temp file, to be used for input of the shell
7729 * command.
7730 */
7731 if ((infile = vim_tempname('i')) == NULL)
7732 {
7733 EMSG(_(e_notmp));
7734 return;
7735 }
7736
7737 fd = mch_fopen((char *)infile, WRITEBIN);
7738 if (fd == NULL)
7739 {
7740 EMSG2(_(e_notopen), infile);
7741 goto done;
7742 }
7743 p = get_var_string_buf(&argvars[1], buf);
7744 if (fwrite(p, STRLEN(p), 1, fd) != 1)
7745 err = TRUE;
7746 if (fclose(fd) != 0)
7747 err = TRUE;
7748 if (err)
7749 {
7750 EMSG(_("E677: Error writing temp file"));
7751 goto done;
7752 }
7753 }
7754
7755 res = get_cmd_output(get_var_string(&argvars[0]), infile, SHELL_SILENT);
7756
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757#ifdef USE_CR
7758 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007759 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
7761 char_u *s;
7762
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007763 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
7765 if (*s == CAR)
7766 *s = NL;
7767 }
7768 }
7769#else
7770# ifdef USE_CRNL
7771 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007772 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {
7774 char_u *s, *d;
7775
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007776 d = res;
7777 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
7779 if (s[0] == CAR && s[1] == NL)
7780 ++s;
7781 *d++ = *s;
7782 }
7783 *d = NUL;
7784 }
7785# endif
7786#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007787
7788done:
7789 if (infile != NULL)
7790 {
7791 mch_remove(infile);
7792 vim_free(infile);
7793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 retvar->var_type = VAR_STRING;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007795 retvar->var_val.var_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
7798/*
7799 * "submatch()" function
7800 */
7801 static void
7802f_submatch(argvars, retvar)
7803 VAR argvars;
7804 VAR retvar;
7805{
7806 retvar->var_type = VAR_STRING;
7807 retvar->var_val.var_string = reg_submatch((int)get_var_number(&argvars[0]));
7808}
7809
7810/*
7811 * "substitute()" function
7812 */
7813 static void
7814f_substitute(argvars, retvar)
7815 VAR argvars;
7816 VAR retvar;
7817{
7818 char_u patbuf[NUMBUFLEN];
7819 char_u subbuf[NUMBUFLEN];
7820 char_u flagsbuf[NUMBUFLEN];
7821
7822 retvar->var_type = VAR_STRING;
7823 retvar->var_val.var_string = do_string_sub(
7824 get_var_string(&argvars[0]),
7825 get_var_string_buf(&argvars[1], patbuf),
7826 get_var_string_buf(&argvars[2], subbuf),
7827 get_var_string_buf(&argvars[3], flagsbuf));
7828}
7829
7830/*
7831 * "tempname()" function
7832 */
7833/*ARGSUSED*/
7834 static void
7835f_tempname(argvars, retvar)
7836 VAR argvars;
7837 VAR retvar;
7838{
7839 static int x = 'A';
7840
7841 retvar->var_type = VAR_STRING;
7842 retvar->var_val.var_string = vim_tempname(x);
7843
7844 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
7845 * names. Skip 'I' and 'O', they are used for shell redirection. */
7846 do
7847 {
7848 if (x == 'Z')
7849 x = '0';
7850 else if (x == '9')
7851 x = 'A';
7852 else
7853 {
7854#ifdef EBCDIC
7855 if (x == 'I')
7856 x = 'J';
7857 else if (x == 'R')
7858 x = 'S';
7859 else
7860#endif
7861 ++x;
7862 }
7863 } while (x == 'I' || x == 'O');
7864}
7865
7866/*
7867 * "tolower(string)" function
7868 */
7869 static void
7870f_tolower(argvars, retvar)
7871 VAR argvars;
7872 VAR retvar;
7873{
7874 char_u *p;
7875
7876 p = vim_strsave(get_var_string(&argvars[0]));
7877 retvar->var_type = VAR_STRING;
7878 retvar->var_val.var_string = p;
7879
7880 if (p != NULL)
7881 while (*p != NUL)
7882 {
7883#ifdef FEAT_MBYTE
7884 int l;
7885
7886 if (enc_utf8)
7887 {
7888 int c, lc;
7889
7890 c = utf_ptr2char(p);
7891 lc = utf_tolower(c);
7892 l = utf_ptr2len_check(p);
7893 /* TODO: reallocate string when byte count changes. */
7894 if (utf_char2len(lc) == l)
7895 utf_char2bytes(lc, p);
7896 p += l;
7897 }
7898 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7899 p += l; /* skip multi-byte character */
7900 else
7901#endif
7902 {
7903 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
7904 ++p;
7905 }
7906 }
7907}
7908
7909/*
7910 * "toupper(string)" function
7911 */
7912 static void
7913f_toupper(argvars, retvar)
7914 VAR argvars;
7915 VAR retvar;
7916{
7917 char_u *p;
7918
7919 p = vim_strsave(get_var_string(&argvars[0]));
7920 retvar->var_type = VAR_STRING;
7921 retvar->var_val.var_string = p;
7922
7923 if (p != NULL)
7924 while (*p != NUL)
7925 {
7926#ifdef FEAT_MBYTE
7927 int l;
7928
7929 if (enc_utf8)
7930 {
7931 int c, uc;
7932
7933 c = utf_ptr2char(p);
7934 uc = utf_toupper(c);
7935 l = utf_ptr2len_check(p);
7936 /* TODO: reallocate string when byte count changes. */
7937 if (utf_char2len(uc) == l)
7938 utf_char2bytes(uc, p);
7939 p += l;
7940 }
7941 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
7942 p += l; /* skip multi-byte character */
7943 else
7944#endif
7945 {
7946 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
7947 p++;
7948 }
7949 }
7950}
7951
7952/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00007953 * "tr(string, fromstr, tostr)" function
7954 */
7955 static void
7956f_tr(argvars, retvar)
7957 VAR argvars;
7958 VAR retvar;
7959{
7960 char_u *instr;
7961 char_u *fromstr;
7962 char_u *tostr;
7963 char_u *p;
7964#ifdef FEAT_MBYTE
7965 int inlen;
7966 int fromlen;
7967 int tolen;
7968 int idx;
7969 char_u *cpstr;
7970 int cplen;
7971 int first = TRUE;
7972#endif
7973 char_u buf[NUMBUFLEN];
7974 char_u buf2[NUMBUFLEN];
7975 garray_T ga;
7976
7977 instr = get_var_string(&argvars[0]);
7978 fromstr = get_var_string_buf(&argvars[1], buf);
7979 tostr = get_var_string_buf(&argvars[2], buf2);
7980
7981 /* Default return value: empty string. */
7982 retvar->var_type = VAR_STRING;
7983 retvar->var_val.var_string = NULL;
7984 ga_init2(&ga, (int)sizeof(char), 80);
7985
7986#ifdef FEAT_MBYTE
7987 if (!has_mbyte)
7988#endif
7989 /* not multi-byte: fromstr and tostr must be the same length */
7990 if (STRLEN(fromstr) != STRLEN(tostr))
7991 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007992#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +00007993error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007994#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +00007995 EMSG2(_(e_invarg2), fromstr);
7996 ga_clear(&ga);
7997 return;
7998 }
7999
8000 /* fromstr and tostr have to contain the same number of chars */
8001 while (*instr != NUL)
8002 {
8003#ifdef FEAT_MBYTE
8004 if (has_mbyte)
8005 {
8006 inlen = mb_ptr2len_check(instr);
8007 cpstr = instr;
8008 cplen = inlen;
8009 idx = 0;
8010 for (p = fromstr; *p != NUL; p += fromlen)
8011 {
8012 fromlen = mb_ptr2len_check(p);
8013 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
8014 {
8015 for (p = tostr; *p != NUL; p += tolen)
8016 {
8017 tolen = mb_ptr2len_check(p);
8018 if (idx-- == 0)
8019 {
8020 cplen = tolen;
8021 cpstr = p;
8022 break;
8023 }
8024 }
8025 if (*p == NUL) /* tostr is shorter than fromstr */
8026 goto error;
8027 break;
8028 }
8029 ++idx;
8030 }
8031
8032 if (first && cpstr == instr)
8033 {
8034 /* Check that fromstr and tostr have the same number of
8035 * (multi-byte) characters. Done only once when a character
8036 * of instr doesn't appear in fromstr. */
8037 first = FALSE;
8038 for (p = tostr; *p != NUL; p += tolen)
8039 {
8040 tolen = mb_ptr2len_check(p);
8041 --idx;
8042 }
8043 if (idx != 0)
8044 goto error;
8045 }
8046
8047 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00008048 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +00008049 ga.ga_len += cplen;
8050 ga.ga_room -= cplen;
8051
8052 instr += inlen;
8053 }
8054 else
8055#endif
8056 {
8057 /* When not using multi-byte chars we can do it faster. */
8058 p = vim_strchr(fromstr, *instr);
8059 if (p != NULL)
8060 ga_append(&ga, tostr[p - fromstr]);
8061 else
8062 ga_append(&ga, *instr);
8063 ++instr;
8064 }
8065 }
8066
8067 retvar->var_val.var_string = ga.ga_data;
8068}
8069
8070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * "type(expr)" function
8072 */
8073 static void
8074f_type(argvars, retvar)
8075 VAR argvars;
8076 VAR retvar;
8077{
8078 if (argvars[0].var_type == VAR_NUMBER)
8079 retvar->var_val.var_number = 0;
8080 else
8081 retvar->var_val.var_number = 1;
8082}
8083
8084/*
8085 * "virtcol(string)" function
8086 */
8087 static void
8088f_virtcol(argvars, retvar)
8089 VAR argvars;
8090 VAR retvar;
8091{
8092 colnr_T vcol = 0;
8093 pos_T *fp;
8094
8095 fp = var2fpos(&argvars[0], FALSE);
8096 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
8097 {
8098 getvvcol(curwin, fp, NULL, NULL, &vcol);
8099 ++vcol;
8100 }
8101
8102 retvar->var_val.var_number = vcol;
8103}
8104
8105/*
8106 * "visualmode()" function
8107 */
8108/*ARGSUSED*/
8109 static void
8110f_visualmode(argvars, retvar)
8111 VAR argvars;
8112 VAR retvar;
8113{
8114#ifdef FEAT_VISUAL
8115 char_u str[2];
8116
8117 retvar->var_type = VAR_STRING;
8118 str[0] = curbuf->b_visual_mode_eval;
8119 str[1] = NUL;
8120 retvar->var_val.var_string = vim_strsave(str);
8121
8122 /* A non-zero number or non-empty string argument: reset mode. */
8123 if ((argvars[0].var_type == VAR_NUMBER
8124 && argvars[0].var_val.var_number != 0)
8125 || (argvars[0].var_type == VAR_STRING
8126 && *get_var_string(&argvars[0]) != NUL))
8127 curbuf->b_visual_mode_eval = NUL;
8128#else
8129 retvar->var_val.var_number = 0; /* return anything, it won't work anyway */
8130#endif
8131}
8132
8133/*
8134 * "winbufnr(nr)" function
8135 */
8136 static void
8137f_winbufnr(argvars, retvar)
8138 VAR argvars;
8139 VAR retvar;
8140{
8141 win_T *wp;
8142
8143 wp = find_win_by_nr(&argvars[0]);
8144 if (wp == NULL)
8145 retvar->var_val.var_number = -1;
8146 else
8147 retvar->var_val.var_number = wp->w_buffer->b_fnum;
8148}
8149
8150/*
8151 * "wincol()" function
8152 */
8153/*ARGSUSED*/
8154 static void
8155f_wincol(argvars, retvar)
8156 VAR argvars;
8157 VAR retvar;
8158{
8159 validate_cursor();
8160 retvar->var_val.var_number = curwin->w_wcol + 1;
8161}
8162
8163/*
8164 * "winheight(nr)" function
8165 */
8166 static void
8167f_winheight(argvars, retvar)
8168 VAR argvars;
8169 VAR retvar;
8170{
8171 win_T *wp;
8172
8173 wp = find_win_by_nr(&argvars[0]);
8174 if (wp == NULL)
8175 retvar->var_val.var_number = -1;
8176 else
8177 retvar->var_val.var_number = wp->w_height;
8178}
8179
8180/*
8181 * "winline()" function
8182 */
8183/*ARGSUSED*/
8184 static void
8185f_winline(argvars, retvar)
8186 VAR argvars;
8187 VAR retvar;
8188{
8189 validate_cursor();
8190 retvar->var_val.var_number = curwin->w_wrow + 1;
8191}
8192
8193/*
8194 * "winnr()" function
8195 */
8196/* ARGSUSED */
8197 static void
8198f_winnr(argvars, retvar)
8199 VAR argvars;
8200 VAR retvar;
8201{
8202 int nr = 1;
8203#ifdef FEAT_WINDOWS
8204 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008205 win_T *twin = curwin;
8206 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008208 if (argvars[0].var_type != VAR_UNKNOWN)
8209 {
8210 arg = get_var_string(&argvars[0]);
8211 if (STRCMP(arg, "$") == 0)
8212 twin = lastwin;
8213 else if (STRCMP(arg, "#") == 0)
8214 {
8215 twin = prevwin;
8216 if (prevwin == NULL)
8217 nr = 0;
8218 }
8219 else
8220 {
8221 EMSG2(_(e_invexpr2), arg);
8222 nr = 0;
8223 }
8224 }
8225
8226 if (nr > 0)
8227 for (wp = firstwin; wp != twin; wp = wp->w_next)
8228 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229#endif
8230 retvar->var_val.var_number = nr;
8231}
8232
8233/*
8234 * "winrestcmd()" function
8235 */
8236/* ARGSUSED */
8237 static void
8238f_winrestcmd(argvars, retvar)
8239 VAR argvars;
8240 VAR retvar;
8241{
8242#ifdef FEAT_WINDOWS
8243 win_T *wp;
8244 int winnr = 1;
8245 garray_T ga;
8246 char_u buf[50];
8247
8248 ga_init2(&ga, (int)sizeof(char), 70);
8249 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8250 {
8251 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
8252 ga_concat(&ga, buf);
8253# ifdef FEAT_VERTSPLIT
8254 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
8255 ga_concat(&ga, buf);
8256# endif
8257 ++winnr;
8258 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00008259 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260
8261 retvar->var_val.var_string = ga.ga_data;
8262#else
8263 retvar->var_val.var_string = NULL;
8264#endif
8265 retvar->var_type = VAR_STRING;
8266}
8267
8268/*
8269 * "winwidth(nr)" function
8270 */
8271 static void
8272f_winwidth(argvars, retvar)
8273 VAR argvars;
8274 VAR retvar;
8275{
8276 win_T *wp;
8277
8278 wp = find_win_by_nr(&argvars[0]);
8279 if (wp == NULL)
8280 retvar->var_val.var_number = -1;
8281 else
8282#ifdef FEAT_VERTSPLIT
8283 retvar->var_val.var_number = wp->w_width;
8284#else
8285 retvar->var_val.var_number = Columns;
8286#endif
8287}
8288
8289 static win_T *
8290find_win_by_nr(vp)
8291 VAR vp;
8292{
8293#ifdef FEAT_WINDOWS
8294 win_T *wp;
8295#endif
8296 int nr;
8297
8298 nr = get_var_number(vp);
8299
8300#ifdef FEAT_WINDOWS
8301 if (nr == 0)
8302 return curwin;
8303
8304 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8305 if (--nr <= 0)
8306 break;
8307 return wp;
8308#else
8309 if (nr == 0 || nr == 1)
8310 return curwin;
8311 return NULL;
8312#endif
8313}
8314
8315/*
8316 * Translate a String variable into a position.
8317 */
8318 static pos_T *
8319var2fpos(varp, lnum)
8320 VAR varp;
8321 int lnum; /* TRUE when $ is last line */
8322{
8323 char_u *name;
8324 static pos_T pos;
8325 pos_T *pp;
8326
8327 name = get_var_string(varp);
8328 if (name[0] == '.') /* cursor */
8329 return &curwin->w_cursor;
8330 if (name[0] == '\'') /* mark */
8331 {
8332 pp = getmark(name[1], FALSE);
8333 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
8334 return NULL;
8335 return pp;
8336 }
8337 if (name[0] == '$') /* last column or line */
8338 {
8339 if (lnum)
8340 {
8341 pos.lnum = curbuf->b_ml.ml_line_count;
8342 pos.col = 0;
8343 }
8344 else
8345 {
8346 pos.lnum = curwin->w_cursor.lnum;
8347 pos.col = (colnr_T)STRLEN(ml_get_curline());
8348 }
8349 return &pos;
8350 }
8351 return NULL;
8352}
8353
8354/*
8355 * Get the length of an environment variable name.
8356 * Advance "arg" to the first character after the name.
8357 * Return 0 for error.
8358 */
8359 static int
8360get_env_len(arg)
8361 char_u **arg;
8362{
8363 char_u *p;
8364 int len;
8365
8366 for (p = *arg; vim_isIDc(*p); ++p)
8367 ;
8368 if (p == *arg) /* no name found */
8369 return 0;
8370
8371 len = (int)(p - *arg);
8372 *arg = p;
8373 return len;
8374}
8375
8376/*
8377 * Get the length of the name of a function or internal variable.
8378 * "arg" is advanced to the first non-white character after the name.
8379 * Return 0 if something is wrong.
8380 */
8381 static int
8382get_id_len(arg)
8383 char_u **arg;
8384{
8385 char_u *p;
8386 int len;
8387
8388 /* Find the end of the name. */
8389 for (p = *arg; eval_isnamec(*p); ++p)
8390 ;
8391 if (p == *arg) /* no name found */
8392 return 0;
8393
8394 len = (int)(p - *arg);
8395 *arg = skipwhite(p);
8396
8397 return len;
8398}
8399
8400/*
8401 * Get the length of the name of a function.
8402 * "arg" is advanced to the first non-white character after the name.
8403 * Return 0 if something is wrong.
8404 * If the name contains 'magic' {}'s, expand them and return the
8405 * expanded name in an allocated string via 'alias' - caller must free.
8406 */
8407 static int
8408get_func_len(arg, alias, evaluate)
8409 char_u **arg;
8410 char_u **alias;
8411 int evaluate;
8412{
8413 int len;
8414#ifdef FEAT_MAGIC_BRACES
8415 char_u *p;
8416 char_u *expr_start;
8417 char_u *expr_end;
8418#endif
8419
8420 *alias = NULL; /* default to no alias */
8421
8422 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
8423 && (*arg)[2] == (int)KE_SNR)
8424 {
8425 /* hard coded <SNR>, already translated */
8426 *arg += 3;
8427 return get_id_len(arg) + 3;
8428 }
8429 len = eval_fname_script(*arg);
8430 if (len > 0)
8431 {
8432 /* literal "<SID>", "s:" or "<SNR>" */
8433 *arg += len;
8434 }
8435
8436#ifdef FEAT_MAGIC_BRACES
8437 /*
8438 * Find the end of the name;
8439 */
8440 p = find_name_end(*arg, &expr_start, &expr_end);
8441 /* check for {} construction */
8442 if (expr_start != NULL)
8443 {
8444 char_u *temp_string;
8445
8446 if (!evaluate)
8447 {
8448 len += (int)(p - *arg);
8449 *arg = skipwhite(p);
8450 return len;
8451 }
8452
8453 /*
8454 * Include any <SID> etc in the expanded string:
8455 * Thus the -len here.
8456 */
8457 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
8458 if (temp_string == NULL)
8459 return 0;
8460 *alias = temp_string;
8461 *arg = skipwhite(p);
8462 return (int)STRLEN(temp_string);
8463 }
8464#endif
8465
8466 len += get_id_len(arg);
8467 if (len == 0)
8468 EMSG2(_(e_invexpr2), *arg);
8469
8470 return len;
8471}
8472
8473 static char_u *
8474find_name_end(arg, expr_start, expr_end)
8475 char_u *arg;
8476 char_u **expr_start;
8477 char_u **expr_end;
8478{
8479 int nesting = 0;
8480 char_u *p;
8481
8482 *expr_start = NULL;
8483 *expr_end = NULL;
8484
8485 for (p = arg; (*p != NUL && (eval_isnamec(*p) || nesting != 0)); ++p)
8486 {
8487#ifdef FEAT_MAGIC_BRACES
8488 if (*p == '{')
8489 {
8490 nesting++;
8491 if (*expr_start == NULL)
8492 *expr_start = p;
8493 }
8494 else if (*p == '}')
8495 {
8496 nesting--;
8497 if (nesting == 0 && *expr_end == NULL)
8498 *expr_end = p;
8499 }
8500#endif
8501 }
8502
8503 return p;
8504}
8505
8506/*
8507 * Return TRUE if character "c" can be used in a variable or function name.
8508 */
8509 static int
8510eval_isnamec(c)
8511 int c;
8512{
8513 return (ASCII_ISALNUM(c) || c == '_' || c == ':'
8514#ifdef FEAT_MAGIC_BRACES
8515 || c == '{' || c == '}'
8516#endif
8517 );
8518}
8519
8520/*
8521 * Find a v: variable.
8522 * Return it's index, or -1 if not found.
8523 */
8524 static int
8525find_vim_var(name, len)
8526 char_u *name;
8527 int len; /* length of "name" */
8528{
8529 char_u *vname;
8530 int vlen;
8531 int i;
8532
8533 /*
8534 * Ignore "v:" for old built-in variables, require it for new ones.
8535 */
8536 if (name[0] == 'v' && name[1] == ':')
8537 {
8538 vname = name + 2;
8539 vlen = len - 2;
8540 }
8541 else
8542 {
8543 vname = name;
8544 vlen = len;
8545 }
8546 for (i = 0; i < VV_LEN; ++i)
8547 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
8548 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
8549 return i;
8550 return -1;
8551}
8552
8553/*
8554 * Set number v: variable to "val".
8555 */
8556 void
8557set_vim_var_nr(idx, val)
8558 int idx;
8559 long val;
8560{
8561 vimvars[idx].val = (char_u *)val;
8562}
8563
8564/*
8565 * Get number v: variable value;
8566 */
8567 long
8568get_vim_var_nr(idx)
8569 int idx;
8570{
8571 return (long)vimvars[idx].val;
8572}
8573
8574/*
8575 * Set v:count, v:count1 and v:prevcount.
8576 */
8577 void
8578set_vcount(count, count1)
8579 long count;
8580 long count1;
8581{
8582 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
8583 vimvars[VV_COUNT].val = (char_u *)count;
8584 vimvars[VV_COUNT1].val = (char_u *)count1;
8585}
8586
8587/*
8588 * Set string v: variable to a copy of "val".
8589 */
8590 void
8591set_vim_var_string(idx, val, len)
8592 int idx;
8593 char_u *val;
8594 int len; /* length of "val" to use or -1 (whole string) */
8595{
8596 vim_free(vimvars[idx].val);
8597 if (val == NULL)
8598 vimvars[idx].val = NULL;
8599 else if (len == -1)
8600 vimvars[idx].val = vim_strsave(val);
8601 else
8602 vimvars[idx].val = vim_strnsave(val, len);
8603}
8604
8605/*
8606 * Set v:register if needed.
8607 */
8608 void
8609set_reg_var(c)
8610 int c;
8611{
8612 char_u regname;
8613
8614 if (c == 0 || c == ' ')
8615 regname = '"';
8616 else
8617 regname = c;
8618 /* Avoid free/alloc when the value is already right. */
8619 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
8620 set_vim_var_string(VV_REG, &regname, 1);
8621}
8622
8623/*
8624 * Get or set v:exception. If "oldval" == NULL, return the current value.
8625 * Otherwise, restore the value to "oldval" and return NULL.
8626 * Must always be called in pairs to save and restore v:exception! Does not
8627 * take care of memory allocations.
8628 */
8629 char_u *
8630v_exception(oldval)
8631 char_u *oldval;
8632{
8633 if (oldval == NULL)
8634 return vimvars[VV_EXCEPTION].val;
8635
8636 vimvars[VV_EXCEPTION].val = oldval;
8637 return NULL;
8638}
8639
8640/*
8641 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
8642 * Otherwise, restore the value to "oldval" and return NULL.
8643 * Must always be called in pairs to save and restore v:throwpoint! Does not
8644 * take care of memory allocations.
8645 */
8646 char_u *
8647v_throwpoint(oldval)
8648 char_u *oldval;
8649{
8650 if (oldval == NULL)
8651 return vimvars[VV_THROWPOINT].val;
8652
8653 vimvars[VV_THROWPOINT].val = oldval;
8654 return NULL;
8655}
8656
8657#if defined(FEAT_AUTOCMD) || defined(PROTO)
8658/*
8659 * Set v:cmdarg.
8660 * If "eap" != NULL, use "eap" to generate the value and return the old value.
8661 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
8662 * Must always be called in pairs!
8663 */
8664 char_u *
8665set_cmdarg(eap, oldarg)
8666 exarg_T *eap;
8667 char_u *oldarg;
8668{
8669 char_u *oldval;
8670 char_u *newval;
8671 unsigned len;
8672
8673 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008674 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008676 vim_free(oldval);
8677 vimvars[VV_CMDARG].val = oldarg;
8678 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 }
8680
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008681 if (eap->force_bin == FORCE_BIN)
8682 len = 6;
8683 else if (eap->force_bin == FORCE_NOBIN)
8684 len = 8;
8685 else
8686 len = 0;
8687 if (eap->force_ff != 0)
8688 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
8689# ifdef FEAT_MBYTE
8690 if (eap->force_enc != 0)
8691 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
8692# endif
8693
8694 newval = alloc(len + 1);
8695 if (newval == NULL)
8696 return NULL;
8697
8698 if (eap->force_bin == FORCE_BIN)
8699 sprintf((char *)newval, " ++bin");
8700 else if (eap->force_bin == FORCE_NOBIN)
8701 sprintf((char *)newval, " ++nobin");
8702 else
8703 *newval = NUL;
8704 if (eap->force_ff != 0)
8705 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
8706 eap->cmd + eap->force_ff);
8707# ifdef FEAT_MBYTE
8708 if (eap->force_enc != 0)
8709 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
8710 eap->cmd + eap->force_enc);
8711# endif
8712 vimvars[VV_CMDARG].val = newval;
8713 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714}
8715#endif
8716
8717/*
8718 * Get the value of internal variable "name".
8719 * Return OK or FAIL.
8720 */
8721 static int
8722get_var_var(name, len, retvar)
8723 char_u *name;
8724 int len; /* length of "name" */
8725 VAR retvar; /* NULL when only checking existence */
8726{
8727 int ret = OK;
8728 int type = VAR_UNKNOWN;
8729 long number = 1;
8730 char_u *string = NULL;
8731 VAR v;
8732 int cc;
8733 int i;
8734
8735 /* truncate the name, so that we can use strcmp() */
8736 cc = name[len];
8737 name[len] = NUL;
8738
8739 /*
8740 * Check for "b:changedtick".
8741 */
8742 if (STRCMP(name, "b:changedtick") == 0)
8743 {
8744 type = VAR_NUMBER;
8745 number = curbuf->b_changedtick;
8746 }
8747
8748 /*
8749 * Check for built-in v: variables.
8750 */
8751 else if ((i = find_vim_var(name, len)) >= 0)
8752 {
8753 type = vimvars[i].type;
8754 number = (long)vimvars[i].val;
8755 string = vimvars[i].val;
8756 }
8757
8758 /*
8759 * Check for user-defined variables.
8760 */
8761 else
8762 {
8763 v = find_var(name, FALSE);
8764 if (v != NULL)
8765 {
8766 type = v->var_type;
8767 number = v->var_val.var_number;
8768 string = v->var_val.var_string;
8769 }
8770 }
8771
8772 if (type == VAR_UNKNOWN)
8773 {
8774 if (retvar != NULL)
8775 EMSG2(_("E121: Undefined variable: %s"), name);
8776 ret = FAIL;
8777 }
8778 else if (retvar != NULL)
8779 {
8780 retvar->var_type = type;
8781 if (type == VAR_NUMBER)
8782 retvar->var_val.var_number = number;
8783 else if (type == VAR_STRING)
8784 {
8785 if (string != NULL)
8786 string = vim_strsave(string);
8787 retvar->var_val.var_string = string;
8788 }
8789 }
8790
8791 name[len] = cc;
8792
8793 return ret;
8794}
8795
8796/*
8797 * Allocate memory for a variable, and make it emtpy (0 or NULL value).
8798 */
8799 static VAR
8800alloc_var()
8801{
8802 return (VAR)alloc_clear((unsigned)sizeof(var));
8803}
8804
8805/*
8806 * Allocate memory for a variable, and assign a string to it.
8807 * The string "s" must have been allocated, it is consumed.
8808 * Return NULL for out of memory, the variable otherwise.
8809 */
8810 static VAR
8811alloc_string_var(s)
8812 char_u *s;
8813{
8814 VAR retvar;
8815
8816 retvar = alloc_var();
8817 if (retvar != NULL)
8818 {
8819 retvar->var_type = VAR_STRING;
8820 retvar->var_val.var_string = s;
8821 }
8822 else
8823 vim_free(s);
8824 return retvar;
8825}
8826
8827/*
8828 * Free the memory for a variable.
8829 */
8830 static void
8831free_var(varp)
8832 VAR varp;
8833{
8834 if (varp != NULL)
8835 {
8836 if (varp->var_type == VAR_STRING)
8837 vim_free(varp->var_val.var_string);
8838 vim_free(varp->var_name);
8839 vim_free(varp);
8840 }
8841}
8842
8843/*
8844 * Free the memory for a variable value and set the value to NULL or 0.
8845 */
8846 static void
8847clear_var(varp)
8848 VAR varp;
8849{
8850 if (varp != NULL)
8851 {
8852 if (varp->var_type == VAR_STRING)
8853 {
8854 vim_free(varp->var_val.var_string);
8855 varp->var_val.var_string = NULL;
8856 }
8857 else
8858 varp->var_val.var_number = 0;
8859 }
8860}
8861
8862/*
8863 * Get the number value of a variable.
8864 * If it is a String variable, uses vim_str2nr().
8865 */
8866 static long
8867get_var_number(varp)
8868 VAR varp;
8869{
8870 long n;
8871
8872 if (varp->var_type == VAR_NUMBER)
8873 return (long)(varp->var_val.var_number);
8874 else if (varp->var_type == VAR_UNKNOWN || varp->var_val.var_string == NULL)
8875 return 0L;
8876 else
8877 {
8878 vim_str2nr(varp->var_val.var_string, NULL, NULL, TRUE, TRUE, &n, NULL);
8879 return n;
8880 }
8881}
8882
8883/*
8884 * Get the lnum from the first argument. Also accepts ".", "$", etc.
8885 */
8886 static linenr_T
8887get_var_lnum(argvars)
8888 VAR argvars;
8889{
8890 var retvar;
8891 linenr_T lnum;
8892
8893 lnum = get_var_number(&argvars[0]);
8894 if (lnum == 0) /* no valid number, try using line() */
8895 {
8896 retvar.var_type = VAR_NUMBER;
8897 f_line(argvars, &retvar);
8898 lnum = retvar.var_val.var_number;
8899 clear_var(&retvar);
8900 }
8901 return lnum;
8902}
8903
8904/*
8905 * Get the string value of a variable.
8906 * If it is a Number variable, the number is converted into a string.
8907 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
8908 * get_var_string_buf() uses a given buffer.
8909 * If the String variable has never been set, return an empty string.
8910 * Never returns NULL;
8911 */
8912 static char_u *
8913get_var_string(varp)
8914 VAR varp;
8915{
8916 static char_u mybuf[NUMBUFLEN];
8917
8918 return get_var_string_buf(varp, mybuf);
8919}
8920
8921 static char_u *
8922get_var_string_buf(varp, buf)
8923 VAR varp;
8924 char_u *buf;
8925{
8926 if (varp->var_type == VAR_NUMBER)
8927 {
8928 sprintf((char *)buf, "%ld", (long)varp->var_val.var_number);
8929 return buf;
8930 }
8931 else if (varp->var_val.var_string == NULL)
8932 return (char_u *)"";
8933 else
8934 return varp->var_val.var_string;
8935}
8936
8937/*
8938 * Find variable "name" in the list of variables.
8939 * Return a pointer to it if found, NULL if not found.
8940 */
8941 static VAR
8942find_var(name, writing)
8943 char_u *name;
8944 int writing;
8945{
8946 int i;
8947 char_u *varname;
8948 garray_T *gap;
8949
8950 /* Check for function arguments "a:" */
8951 if (name[0] == 'a' && name[1] == ':')
8952 {
8953 if (writing)
8954 {
8955 EMSG2(_(e_readonlyvar), name);
8956 return NULL;
8957 }
8958 name += 2;
8959 if (current_funccal == NULL)
8960 return NULL;
8961 if (VIM_ISDIGIT(*name))
8962 {
8963 i = atol((char *)name);
8964 if (i == 0) /* a:0 */
8965 return &current_funccal->a0_var;
8966 i += current_funccal->func->args.ga_len;
8967 if (i > current_funccal->argcount) /* a:999 */
8968 return NULL;
8969 return &(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
8970 }
8971 if (STRCMP(name, "firstline") == 0)
8972 return &(current_funccal->firstline);
8973 if (STRCMP(name, "lastline") == 0)
8974 return &(current_funccal->lastline);
8975 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
8976 if (STRCMP(name, ((char_u **)
8977 (current_funccal->func->args.ga_data))[i]) == 0)
8978 return &(current_funccal->argvars[i]); /* a:name */
8979 return NULL;
8980 }
8981
8982 gap = find_var_ga(name, &varname);
8983 if (gap == NULL)
8984 return NULL;
8985 return find_var_in_ga(gap, varname);
8986}
8987
8988 static VAR
8989find_var_in_ga(gap, varname)
8990 garray_T *gap;
8991 char_u *varname;
8992{
8993 int i;
8994
8995 for (i = gap->ga_len; --i >= 0; )
8996 if (VAR_GAP_ENTRY(i, gap).var_name != NULL
8997 && STRCMP(VAR_GAP_ENTRY(i, gap).var_name, varname) == 0)
8998 break;
8999 if (i < 0)
9000 return NULL;
9001 return &VAR_GAP_ENTRY(i, gap);
9002}
9003
9004/*
9005 * Find the growarray and start of name without ':' for a variable name.
9006 */
9007 static garray_T *
9008find_var_ga(name, varname)
9009 char_u *name;
9010 char_u **varname;
9011{
9012 if (name[1] != ':')
9013 {
9014 /* If not "x:name" there must not be any ":" in the name. */
9015 if (vim_strchr(name, ':') != NULL)
9016 return NULL;
9017 *varname = name;
9018 if (current_funccal == NULL)
9019 return &variables; /* global variable */
9020 return &current_funccal->l_vars; /* local function variable */
9021 }
9022 *varname = name + 2;
9023 if (*name == 'b') /* buffer variable */
9024 return &curbuf->b_vars;
9025 if (*name == 'w') /* window variable */
9026 return &curwin->w_vars;
9027 if (*name == 'g') /* global variable */
9028 return &variables;
9029 if (*name == 'l' && current_funccal != NULL)/* local function variable */
9030 return &current_funccal->l_vars;
9031 if (*name == 's' /* script variable */
9032 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
9033 return &SCRIPT_VARS(current_SID);
9034 return NULL;
9035}
9036
9037/*
9038 * Get the string value of a (global/local) variable.
9039 * Returns NULL when it doesn't exist.
9040 */
9041 char_u *
9042get_var_value(name)
9043 char_u *name;
9044{
9045 VAR v;
9046
9047 v = find_var(name, FALSE);
9048 if (v == NULL)
9049 return NULL;
9050 return get_var_string(v);
9051}
9052
9053/*
9054 * Allocate a new growarry for a sourced script. It will be used while
9055 * sourcing this script and when executing functions defined in the script.
9056 */
9057 void
9058new_script_vars(id)
9059 scid_T id;
9060{
9061 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
9062 {
9063 while (ga_scripts.ga_len < id)
9064 {
9065 var_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
9066 ++ga_scripts.ga_len;
9067 --ga_scripts.ga_room;
9068 }
9069 }
9070}
9071
9072/*
9073 * Initialize internal variables for use.
9074 */
9075 void
9076var_init(gap)
9077 garray_T *gap;
9078{
9079 ga_init2(gap, (int)sizeof(var), 4);
9080}
9081
9082/*
9083 * Clean up a list of internal variables.
9084 */
9085 void
9086var_clear(gap)
9087 garray_T *gap;
9088{
9089 int i;
9090
9091 for (i = gap->ga_len; --i >= 0; )
9092 var_free_one(&VAR_GAP_ENTRY(i, gap));
9093 ga_clear(gap);
9094}
9095
9096 static void
9097var_free_one(v)
9098 VAR v;
9099{
9100 vim_free(v->var_name);
9101 v->var_name = NULL;
9102 if (v->var_type == VAR_STRING)
9103 vim_free(v->var_val.var_string);
9104 v->var_val.var_string = NULL;
9105}
9106
9107/*
9108 * List the value of one internal variable.
9109 */
9110 static void
9111list_one_var(v, prefix)
9112 VAR v;
9113 char_u *prefix;
9114{
9115 list_one_var_a(prefix, v->var_name, v->var_type, get_var_string(v));
9116}
9117
9118/*
9119 * List the value of one "v:" variable.
9120 */
9121 static void
9122list_vim_var(i)
9123 int i; /* index in vimvars[] */
9124{
9125 char_u *p;
9126 char_u numbuf[NUMBUFLEN];
9127
9128 if (vimvars[i].type == VAR_NUMBER)
9129 {
9130 p = numbuf;
9131 sprintf((char *)p, "%ld", (long)vimvars[i].val);
9132 }
9133 else if (vimvars[i].val == NULL)
9134 p = (char_u *)"";
9135 else
9136 p = vimvars[i].val;
9137 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
9138 vimvars[i].type, p);
9139}
9140
9141 static void
9142list_one_var_a(prefix, name, type, string)
9143 char_u *prefix;
9144 char_u *name;
9145 int type;
9146 char_u *string;
9147{
9148 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
9149 if (name != NULL) /* "a:" vars don't have a name stored */
9150 msg_puts(name);
9151 msg_putchar(' ');
9152 msg_advance(22);
9153 if (type == VAR_NUMBER)
9154 msg_putchar('#');
9155 else
9156 msg_putchar(' ');
9157 msg_outtrans(string);
9158}
9159
9160/*
9161 * Set variable "name" to value in "varp".
9162 * If the variable already exists, the value is updated.
9163 * Otherwise the variable is created.
9164 */
9165 static void
9166set_var(name, varp)
9167 char_u *name;
9168 VAR varp;
9169{
9170 int i;
9171 VAR v;
9172 char_u *varname;
9173 garray_T *gap;
9174
9175 /*
9176 * Handle setting internal v: variables.
9177 */
9178 i = find_vim_var(name, (int)STRLEN(name));
9179 if (i >= 0)
9180 {
9181 if (vimvars[i].flags & VV_RO)
9182 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009183 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
9184 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 else
9186 {
9187 if (vimvars[i].type == VAR_STRING)
9188 {
9189 vim_free(vimvars[i].val);
9190 vimvars[i].val = vim_strsave(get_var_string(varp));
9191 }
9192 else
9193 vimvars[i].val = (char_u *)(long)varp->var_val.var_number;
9194 }
9195 return;
9196 }
9197
9198 v = find_var(name, TRUE);
9199 if (v != NULL) /* existing variable, only need to free string */
9200 {
9201 if (v->var_type == VAR_STRING)
9202 vim_free(v->var_val.var_string);
9203 }
9204 else /* add a new variable */
9205 {
9206 gap = find_var_ga(name, &varname);
9207 if (gap == NULL) /* illegal name */
9208 {
9209 EMSG2(_("E461: Illegal variable name: %s"), name);
9210 return;
9211 }
9212
9213 /* Try to use an empty entry */
9214 for (i = gap->ga_len; --i >= 0; )
9215 if (VAR_GAP_ENTRY(i, gap).var_name == NULL)
9216 break;
9217 if (i < 0) /* need to allocate more room */
9218 {
9219 if (ga_grow(gap, 1) == FAIL)
9220 return;
9221 i = gap->ga_len;
9222 }
9223 v = &VAR_GAP_ENTRY(i, gap);
9224 if ((v->var_name = vim_strsave(varname)) == NULL)
9225 return;
9226 if (i == gap->ga_len)
9227 {
9228 ++gap->ga_len;
9229 --gap->ga_room;
9230 }
9231 }
9232 copy_var(varp, v);
9233}
9234
9235 static void
9236copy_var(from, to)
9237 VAR from;
9238 VAR to;
9239{
9240 to->var_type = from->var_type;
9241 if (from->var_type == VAR_STRING)
9242 to->var_val.var_string = vim_strsave(get_var_string(from));
9243 else
9244 to->var_val.var_number = from->var_val.var_number;
9245}
9246
9247/*
9248 * ":echo expr1 ..." print each argument separated with a space, add a
9249 * newline at the end.
9250 * ":echon expr1 ..." print each argument plain.
9251 */
9252 void
9253ex_echo(eap)
9254 exarg_T *eap;
9255{
9256 char_u *arg = eap->arg;
9257 var retvar;
9258 char_u *p;
9259 int needclr = TRUE;
9260 int atstart = TRUE;
9261
9262 if (eap->skip)
9263 ++emsg_skip;
9264 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
9265 {
9266 p = arg;
9267 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9268 {
9269 /*
9270 * Report the invalid expression unless the expression evaluation
9271 * has been cancelled due to an aborting error, an interrupt, or an
9272 * exception.
9273 */
9274 if (!aborting())
9275 EMSG2(_(e_invexpr2), p);
9276 break;
9277 }
9278 if (!eap->skip)
9279 {
9280 if (atstart)
9281 {
9282 atstart = FALSE;
9283 /* Call msg_start() after eval1(), evaluating the expression
9284 * may cause a message to appear. */
9285 if (eap->cmdidx == CMD_echo)
9286 msg_start();
9287 }
9288 else if (eap->cmdidx == CMD_echo)
9289 msg_puts_attr((char_u *)" ", echo_attr);
9290 for (p = get_var_string(&retvar); *p != NUL && !got_int; ++p)
9291 if (*p == '\n' || *p == '\r' || *p == TAB)
9292 {
9293 if (*p != TAB && needclr)
9294 {
9295 /* remove any text still there from the command */
9296 msg_clr_eos();
9297 needclr = FALSE;
9298 }
9299 msg_putchar_attr(*p, echo_attr);
9300 }
9301 else
9302 {
9303#ifdef FEAT_MBYTE
9304 if (has_mbyte)
9305 {
9306 int i = (*mb_ptr2len_check)(p);
9307
9308 (void)msg_outtrans_len_attr(p, i, echo_attr);
9309 p += i - 1;
9310 }
9311 else
9312#endif
9313 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9314 }
9315 }
9316 clear_var(&retvar);
9317 arg = skipwhite(arg);
9318 }
9319 eap->nextcmd = check_nextcmd(arg);
9320
9321 if (eap->skip)
9322 --emsg_skip;
9323 else
9324 {
9325 /* remove text that may still be there from the command */
9326 if (needclr)
9327 msg_clr_eos();
9328 if (eap->cmdidx == CMD_echo)
9329 msg_end();
9330 }
9331}
9332
9333/*
9334 * ":echohl {name}".
9335 */
9336 void
9337ex_echohl(eap)
9338 exarg_T *eap;
9339{
9340 int id;
9341
9342 id = syn_name2id(eap->arg);
9343 if (id == 0)
9344 echo_attr = 0;
9345 else
9346 echo_attr = syn_id2attr(id);
9347}
9348
9349/*
9350 * ":execute expr1 ..." execute the result of an expression.
9351 * ":echomsg expr1 ..." Print a message
9352 * ":echoerr expr1 ..." Print an error
9353 * Each gets spaces around each argument and a newline at the end for
9354 * echo commands
9355 */
9356 void
9357ex_execute(eap)
9358 exarg_T *eap;
9359{
9360 char_u *arg = eap->arg;
9361 var retvar;
9362 int ret = OK;
9363 char_u *p;
9364 garray_T ga;
9365 int len;
9366 int save_did_emsg;
9367
9368 ga_init2(&ga, 1, 80);
9369
9370 if (eap->skip)
9371 ++emsg_skip;
9372 while (*arg != NUL && *arg != '|' && *arg != '\n')
9373 {
9374 p = arg;
9375 if (eval1(&arg, &retvar, !eap->skip) == FAIL)
9376 {
9377 /*
9378 * Report the invalid expression unless the expression evaluation
9379 * has been cancelled due to an aborting error, an interrupt, or an
9380 * exception.
9381 */
9382 if (!aborting())
9383 EMSG2(_(e_invexpr2), p);
9384 ret = FAIL;
9385 break;
9386 }
9387
9388 if (!eap->skip)
9389 {
9390 p = get_var_string(&retvar);
9391 len = (int)STRLEN(p);
9392 if (ga_grow(&ga, len + 2) == FAIL)
9393 {
9394 clear_var(&retvar);
9395 ret = FAIL;
9396 break;
9397 }
9398 if (ga.ga_len)
9399 {
9400 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
9401 --ga.ga_room;
9402 }
9403 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
9404 ga.ga_room -= len;
9405 ga.ga_len += len;
9406 }
9407
9408 clear_var(&retvar);
9409 arg = skipwhite(arg);
9410 }
9411
9412 if (ret != FAIL && ga.ga_data != NULL)
9413 {
9414 if (eap->cmdidx == CMD_echomsg)
9415 MSG_ATTR(ga.ga_data, echo_attr);
9416 else if (eap->cmdidx == CMD_echoerr)
9417 {
9418 /* We don't want to abort following commands, restore did_emsg. */
9419 save_did_emsg = did_emsg;
9420 EMSG((char_u *)ga.ga_data);
9421 if (!force_abort)
9422 did_emsg = save_did_emsg;
9423 }
9424 else if (eap->cmdidx == CMD_execute)
9425 do_cmdline((char_u *)ga.ga_data,
9426 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9427 }
9428
9429 ga_clear(&ga);
9430
9431 if (eap->skip)
9432 --emsg_skip;
9433
9434 eap->nextcmd = check_nextcmd(arg);
9435}
9436
9437/*
9438 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9439 * "arg" points to the "&" or '+' when called, to "option" when returning.
9440 * Returns NULL when no option name found. Otherwise pointer to the char
9441 * after the option name.
9442 */
9443 static char_u *
9444find_option_end(arg, opt_flags)
9445 char_u **arg;
9446 int *opt_flags;
9447{
9448 char_u *p = *arg;
9449
9450 ++p;
9451 if (*p == 'g' && p[1] == ':')
9452 {
9453 *opt_flags = OPT_GLOBAL;
9454 p += 2;
9455 }
9456 else if (*p == 'l' && p[1] == ':')
9457 {
9458 *opt_flags = OPT_LOCAL;
9459 p += 2;
9460 }
9461 else
9462 *opt_flags = 0;
9463
9464 if (!ASCII_ISALPHA(*p))
9465 return NULL;
9466 *arg = p;
9467
9468 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9469 p += 4; /* termcap option */
9470 else
9471 while (ASCII_ISALPHA(*p))
9472 ++p;
9473 return p;
9474}
9475
9476/*
9477 * ":function"
9478 */
9479 void
9480ex_function(eap)
9481 exarg_T *eap;
9482{
9483 char_u *theline;
9484 int j;
9485 int c;
9486#ifdef FEAT_MAGIC_BRACES
9487 int saved_did_emsg;
9488#endif
9489 char_u *name = NULL;
9490 char_u *p;
9491 char_u *arg;
9492 garray_T newargs;
9493 garray_T newlines;
9494 int varargs = FALSE;
9495 int mustend = FALSE;
9496 int flags = 0;
9497 ufunc_T *fp;
9498 int indent;
9499 int nesting;
9500 char_u *skip_until = NULL;
9501 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
9502
9503 /*
9504 * ":function" without argument: list functions.
9505 */
9506 if (ends_excmd(*eap->arg))
9507 {
9508 if (!eap->skip)
9509 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
9510 list_func_head(fp, FALSE);
9511 eap->nextcmd = check_nextcmd(eap->arg);
9512 return;
9513 }
9514
9515 p = eap->arg;
9516 name = trans_function_name(&p, eap->skip, FALSE);
9517 if (name == NULL && !eap->skip)
9518 {
9519 /*
9520 * Return on an invalid expression in braces, unless the expression
9521 * evaluation has been cancelled due to an aborting error, an
9522 * interrupt, or an exception.
9523 */
9524 if (!aborting())
9525 return;
9526 else
9527 eap->skip = TRUE;
9528 }
9529#ifdef FEAT_MAGIC_BRACES
9530 /* An error in a function call during evaluation of an expression in magic
9531 * braces should not cause the function not to be defined. */
9532 saved_did_emsg = did_emsg;
9533 did_emsg = FALSE;
9534#endif
9535
9536 /*
9537 * ":function func" with only function name: list function.
9538 */
9539 if (vim_strchr(p, '(') == NULL)
9540 {
9541 if (!ends_excmd(*skipwhite(p)))
9542 {
9543 EMSG(_(e_trailing));
9544 goto erret_name;
9545 }
9546 eap->nextcmd = check_nextcmd(p);
9547 if (eap->nextcmd != NULL)
9548 *p = NUL;
9549 if (!eap->skip && !got_int)
9550 {
9551 fp = find_func(name);
9552 if (fp != NULL)
9553 {
9554 list_func_head(fp, TRUE);
9555 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
9556 {
9557 msg_putchar('\n');
9558 msg_outnum((long)(j + 1));
9559 if (j < 9)
9560 msg_putchar(' ');
9561 if (j < 99)
9562 msg_putchar(' ');
9563 msg_prt_line(FUNCLINE(fp, j));
9564 out_flush(); /* show a line at a time */
9565 ui_breakcheck();
9566 }
9567 if (!got_int)
9568 {
9569 msg_putchar('\n');
9570 msg_puts((char_u *)" endfunction");
9571 }
9572 }
9573 else
9574 EMSG2(_("E123: Undefined function: %s"), eap->arg);
9575 }
9576 goto erret_name;
9577 }
9578
9579 /*
9580 * ":function name(arg1, arg2)" Define function.
9581 */
9582 p = skipwhite(p);
9583 if (*p != '(')
9584 {
9585 if (!eap->skip)
9586 {
9587 EMSG2(_("E124: Missing '(': %s"), eap->arg);
9588 goto erret_name;
9589 }
9590 /* attempt to continue by skipping some text */
9591 if (vim_strchr(p, '(') != NULL)
9592 p = vim_strchr(p, '(');
9593 }
9594 p = skipwhite(p + 1);
9595
9596 ga_init2(&newargs, (int)sizeof(char_u *), 3);
9597 ga_init2(&newlines, (int)sizeof(char_u *), 3);
9598
9599 /*
9600 * Isolate the arguments: "arg1, arg2, ...)"
9601 */
9602 while (*p != ')')
9603 {
9604 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
9605 {
9606 varargs = TRUE;
9607 p += 3;
9608 mustend = TRUE;
9609 }
9610 else
9611 {
9612 arg = p;
9613 while (ASCII_ISALNUM(*p) || *p == '_')
9614 ++p;
9615 if (arg == p || isdigit(*arg)
9616 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
9617 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
9618 {
9619 if (!eap->skip)
9620 EMSG2(_("E125: Illegal argument: %s"), arg);
9621 break;
9622 }
9623 if (ga_grow(&newargs, 1) == FAIL)
9624 goto erret;
9625 c = *p;
9626 *p = NUL;
9627 arg = vim_strsave(arg);
9628 if (arg == NULL)
9629 goto erret;
9630 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
9631 *p = c;
9632 newargs.ga_len++;
9633 newargs.ga_room--;
9634 if (*p == ',')
9635 ++p;
9636 else
9637 mustend = TRUE;
9638 }
9639 p = skipwhite(p);
9640 if (mustend && *p != ')')
9641 {
9642 if (!eap->skip)
9643 EMSG2(_(e_invarg2), eap->arg);
9644 break;
9645 }
9646 }
9647 ++p; /* skip the ')' */
9648
9649 /* find extra arguments "range" and "abort" */
9650 for (;;)
9651 {
9652 p = skipwhite(p);
9653 if (STRNCMP(p, "range", 5) == 0)
9654 {
9655 flags |= FC_RANGE;
9656 p += 5;
9657 }
9658 else if (STRNCMP(p, "abort", 5) == 0)
9659 {
9660 flags |= FC_ABORT;
9661 p += 5;
9662 }
9663 else
9664 break;
9665 }
9666
9667 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
9668 EMSG(_(e_trailing));
9669
9670 /*
9671 * Read the body of the function, until ":endfunction" is found.
9672 */
9673 if (KeyTyped)
9674 {
9675 /* Check if the function already exists, don't let the user type the
9676 * whole function before telling him it doesn't work! For a script we
9677 * need to skip the body to be able to find what follows. */
9678 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
9679 EMSG2(_(e_funcexts), name);
9680
9681 msg_putchar('\n'); /* don't overwrite the function name */
9682 cmdline_row = msg_row;
9683 }
9684
9685 indent = 2;
9686 nesting = 0;
9687 for (;;)
9688 {
9689 msg_scroll = TRUE;
9690 need_wait_return = FALSE;
9691 if (eap->getline == NULL)
9692 theline = getcmdline(':', 0L, indent);
9693 else
9694 theline = eap->getline(':', eap->cookie, indent);
9695 if (KeyTyped)
9696 lines_left = Rows - 1;
9697 if (theline == NULL)
9698 {
9699 EMSG(_("E126: Missing :endfunction"));
9700 goto erret;
9701 }
9702
9703 if (skip_until != NULL)
9704 {
9705 /* between ":append" and "." and between ":python <<EOF" and "EOF"
9706 * don't check for ":endfunc". */
9707 if (STRCMP(theline, skip_until) == 0)
9708 {
9709 vim_free(skip_until);
9710 skip_until = NULL;
9711 }
9712 }
9713 else
9714 {
9715 /* skip ':' and blanks*/
9716 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
9717 ;
9718
9719 /* Check for "endfunction" (should be more strict...). */
9720 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
9721 {
9722 vim_free(theline);
9723 break;
9724 }
9725
9726 /* Increase indent inside "if", "while", and "try", decrease
9727 * at "end". */
9728 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
9729 indent -= 2;
9730 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
9731 || STRNCMP(p, "try", 3) == 0)
9732 indent += 2;
9733
9734 /* Check for defining a function inside this function. */
9735 if (STRNCMP(p, "fu", 2) == 0)
9736 {
9737 p = skipwhite(skiptowhite(p));
9738 p += eval_fname_script(p);
9739 if (ASCII_ISALPHA(*p))
9740 {
9741 vim_free(trans_function_name(&p, TRUE, FALSE));
9742 if (*skipwhite(p) == '(')
9743 {
9744 ++nesting;
9745 indent += 2;
9746 }
9747 }
9748 }
9749
9750 /* Check for ":append" or ":insert". */
9751 p = skip_range(p, NULL);
9752 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
9753 || (p[0] == 'i'
9754 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
9755 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
9756 skip_until = vim_strsave((char_u *)".");
9757
9758 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
9759 arg = skipwhite(skiptowhite(p));
9760 if (arg[0] == '<' && arg[1] =='<'
9761 && ((p[0] == 'p' && p[1] == 'y'
9762 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
9763 || (p[0] == 'p' && p[1] == 'e'
9764 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
9765 || (p[0] == 't' && p[1] == 'c'
9766 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
9767 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
9768 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009769 || (p[0] == 'm' && p[1] == 'z'
9770 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 ))
9772 {
9773 /* ":python <<" continues until a dot, like ":append" */
9774 p = skipwhite(arg + 2);
9775 if (*p == NUL)
9776 skip_until = vim_strsave((char_u *)".");
9777 else
9778 skip_until = vim_strsave(p);
9779 }
9780 }
9781
9782 /* Add the line to the function. */
9783 if (ga_grow(&newlines, 1) == FAIL)
9784 goto erret;
9785 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
9786 newlines.ga_len++;
9787 newlines.ga_room--;
9788 }
9789
9790 /* Don't define the function when skipping commands or when an error was
9791 * detected. */
9792 if (eap->skip || did_emsg)
9793 goto erret;
9794
9795 /*
9796 * If there are no errors, add the function
9797 */
9798 fp = find_func(name);
9799 if (fp != NULL)
9800 {
9801 if (!eap->forceit)
9802 {
9803 EMSG2(_(e_funcexts), name);
9804 goto erret;
9805 }
9806 if (fp->calls)
9807 {
9808 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
9809 goto erret;
9810 }
9811 /* redefine existing function */
9812 ga_clear_strings(&(fp->args));
9813 ga_clear_strings(&(fp->lines));
9814 vim_free(name);
9815 }
9816 else
9817 {
9818 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
9819 if (fp == NULL)
9820 goto erret;
9821 /* insert the new function in the function list */
9822 fp->next = firstfunc;
9823 firstfunc = fp;
9824 fp->name = name;
9825 }
9826 fp->args = newargs;
9827 fp->lines = newlines;
9828 fp->varargs = varargs;
9829 fp->flags = flags;
9830 fp->calls = 0;
9831 fp->script_ID = current_SID;
9832#ifdef FEAT_MAGIC_BRACES
9833 did_emsg |= saved_did_emsg;
9834#endif
9835 vim_free(skip_until);
9836 return;
9837
9838erret:
9839 vim_free(skip_until);
9840 ga_clear_strings(&newargs);
9841 ga_clear_strings(&newlines);
9842erret_name:
9843 vim_free(name);
9844#ifdef FEAT_MAGIC_BRACES
9845 did_emsg |= saved_did_emsg;
9846#endif
9847}
9848
9849/*
9850 * Get a function name, translating "<SID>" and "<SNR>".
9851 * Returns the function name in allocated memory, or NULL for failure.
9852 * Advances "pp" to just after the function name (if no error).
9853 */
9854 static char_u *
9855trans_function_name(pp, skip, internal)
9856 char_u **pp;
9857 int skip; /* only find the end, don't evaluate */
9858 int internal; /* TRUE if internal function name OK */
9859{
9860 char_u *name;
9861 char_u *start;
9862 char_u *end;
9863 int lead;
9864 char_u sid_buf[20];
9865 char_u *temp_string = NULL;
9866 char_u *expr_start, *expr_end;
9867 int len;
9868
9869 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
9870 start = *pp;
9871 lead = eval_fname_script(start);
9872 if (lead > 0)
9873 start += lead;
9874 end = find_name_end(start, &expr_start, &expr_end);
9875 if (end == start)
9876 {
9877 if (!skip)
9878 EMSG(_("E129: Function name required"));
9879 return NULL;
9880 }
9881#ifdef FEAT_MAGIC_BRACES
9882 if (expr_start != NULL && !skip)
9883 {
9884 /* expand magic curlies */
9885 temp_string = make_expanded_name(start, expr_start, expr_end, end);
9886 if (temp_string == NULL)
9887 {
9888 /*
9889 * Report an invalid expression in braces, unless the expression
9890 * evaluation has been cancelled due to an aborting error, an
9891 * interrupt, or an exception.
9892 */
9893 if (!aborting())
9894 EMSG2(_(e_invarg2), start);
9895 else
9896 *pp = end;
9897 return NULL;
9898 }
9899 start = temp_string;
9900 len = (int)STRLEN(temp_string);
9901 }
9902 else
9903#endif
9904 len = (int)(end - start);
9905
9906 /*
9907 * Copy the function name to allocated memory.
9908 * Accept <SID>name() inside a script, translate into <SNR>123_name().
9909 * Accept <SNR>123_name() outside a script.
9910 */
9911 if (skip)
9912 lead = 0; /* do nothing */
9913 else if (lead > 0)
9914 {
9915 lead = 3;
9916 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9917 {
9918 if (current_SID <= 0)
9919 {
9920 EMSG(_(e_usingsid));
9921 return NULL;
9922 }
9923 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
9924 lead += (int)STRLEN(sid_buf);
9925 }
9926 }
9927 else if (!internal && !ASCII_ISUPPER(*start))
9928 {
9929 EMSG2(_("E128: Function name must start with a capital: %s"), start);
9930 return NULL;
9931 }
9932 name = alloc((unsigned)(len + lead + 1));
9933 if (name != NULL)
9934 {
9935 if (lead > 0)
9936 {
9937 name[0] = K_SPECIAL;
9938 name[1] = KS_EXTRA;
9939 name[2] = (int)KE_SNR;
9940 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
9941 STRCPY(name + 3, sid_buf);
9942 }
9943 mch_memmove(name + lead, start, (size_t)len);
9944 name[len + lead] = NUL;
9945 }
9946 *pp = end;
9947
9948 vim_free(temp_string);
9949 return name;
9950}
9951
9952/*
9953 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
9954 * Return 2 if "p" starts with "s:".
9955 * Return 0 otherwise.
9956 */
9957 static int
9958eval_fname_script(p)
9959 char_u *p;
9960{
9961 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
9962 || STRNICMP(p + 1, "SNR>", 4) == 0))
9963 return 5;
9964 if (p[0] == 's' && p[1] == ':')
9965 return 2;
9966 return 0;
9967}
9968
9969/*
9970 * Return TRUE if "p" starts with "<SID>" or "s:".
9971 * Only works if eval_fname_script() returned non-zero for "p"!
9972 */
9973 static int
9974eval_fname_sid(p)
9975 char_u *p;
9976{
9977 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
9978}
9979
9980/*
9981 * List the head of the function: "name(arg1, arg2)".
9982 */
9983 static void
9984list_func_head(fp, indent)
9985 ufunc_T *fp;
9986 int indent;
9987{
9988 int j;
9989
9990 msg_start();
9991 if (indent)
9992 MSG_PUTS(" ");
9993 MSG_PUTS("function ");
9994 if (fp->name[0] == K_SPECIAL)
9995 {
9996 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
9997 msg_puts(fp->name + 3);
9998 }
9999 else
10000 msg_puts(fp->name);
10001 msg_putchar('(');
10002 for (j = 0; j < fp->args.ga_len; ++j)
10003 {
10004 if (j)
10005 MSG_PUTS(", ");
10006 msg_puts(FUNCARG(fp, j));
10007 }
10008 if (fp->varargs)
10009 {
10010 if (j)
10011 MSG_PUTS(", ");
10012 MSG_PUTS("...");
10013 }
10014 msg_putchar(')');
10015}
10016
10017/*
10018 * Find a function by name, return pointer to it in ufuncs.
10019 * Return NULL for unknown function.
10020 */
10021 static ufunc_T *
10022find_func(name)
10023 char_u *name;
10024{
10025 ufunc_T *fp;
10026
10027 for (fp = firstfunc; fp != NULL; fp = fp->next)
10028 if (STRCMP(name, fp->name) == 0)
10029 break;
10030 return fp;
10031}
10032
10033#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10034
10035/*
10036 * Function given to ExpandGeneric() to obtain the list of user defined
10037 * function names.
10038 */
10039 char_u *
10040get_user_func_name(xp, idx)
10041 expand_T *xp;
10042 int idx;
10043{
10044 static ufunc_T *fp = NULL;
10045
10046 if (idx == 0)
10047 fp = firstfunc;
10048 if (fp != NULL)
10049 {
10050 if (STRLEN(fp->name) + 4 >= IOSIZE)
10051 return fp->name; /* prevents overflow */
10052
10053 cat_func_name(IObuff, fp);
10054 if (xp->xp_context != EXPAND_USER_FUNC)
10055 {
10056 STRCAT(IObuff, "(");
10057 if (!fp->varargs && fp->args.ga_len == 0)
10058 STRCAT(IObuff, ")");
10059 }
10060
10061 fp = fp->next;
10062 return IObuff;
10063 }
10064 return NULL;
10065}
10066
10067#endif /* FEAT_CMDL_COMPL */
10068
10069/*
10070 * Copy the function name of "fp" to buffer "buf".
10071 * "buf" must be able to hold the function name plus three bytes.
10072 * Takes care of script-local function names.
10073 */
10074 static void
10075cat_func_name(buf, fp)
10076 char_u *buf;
10077 ufunc_T *fp;
10078{
10079 if (fp->name[0] == K_SPECIAL)
10080 {
10081 STRCPY(buf, "<SNR>");
10082 STRCAT(buf, fp->name + 3);
10083 }
10084 else
10085 STRCPY(buf, fp->name);
10086}
10087
10088/*
10089 * ":delfunction {name}"
10090 */
10091 void
10092ex_delfunction(eap)
10093 exarg_T *eap;
10094{
10095 ufunc_T *fp = NULL, *pfp;
10096 char_u *p;
10097 char_u *name;
10098
10099 p = eap->arg;
10100 name = trans_function_name(&p, eap->skip, FALSE);
10101 if (name == NULL)
10102 return;
10103 if (!ends_excmd(*skipwhite(p)))
10104 {
10105 vim_free(name);
10106 EMSG(_(e_trailing));
10107 return;
10108 }
10109 eap->nextcmd = check_nextcmd(p);
10110 if (eap->nextcmd != NULL)
10111 *p = NUL;
10112
10113 if (!eap->skip)
10114 fp = find_func(name);
10115 vim_free(name);
10116
10117 if (!eap->skip)
10118 {
10119 if (fp == NULL)
10120 {
10121 EMSG2(_("E130: Undefined function: %s"), eap->arg);
10122 return;
10123 }
10124 if (fp->calls)
10125 {
10126 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
10127 return;
10128 }
10129
10130 /* clear this function */
10131 vim_free(fp->name);
10132 ga_clear_strings(&(fp->args));
10133 ga_clear_strings(&(fp->lines));
10134
10135 /* remove the function from the function list */
10136 if (firstfunc == fp)
10137 firstfunc = fp->next;
10138 else
10139 {
10140 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
10141 if (pfp->next == fp)
10142 {
10143 pfp->next = fp->next;
10144 break;
10145 }
10146 }
10147 vim_free(fp);
10148 }
10149}
10150
10151/*
10152 * Call a user function.
10153 */
10154 static void
10155call_user_func(fp, argcount, argvars, retvar, firstline, lastline)
10156 ufunc_T *fp; /* pointer to function */
10157 int argcount; /* nr of args */
10158 VAR argvars; /* arguments */
10159 VAR retvar; /* return value */
10160 linenr_T firstline; /* first line of range */
10161 linenr_T lastline; /* last line of range */
10162{
10163 char_u *save_sourcing_name;
10164 linenr_T save_sourcing_lnum;
10165 scid_T save_current_SID;
10166 struct funccall fc;
10167 struct funccall *save_fcp = current_funccal;
10168 int save_did_emsg;
10169 static int depth = 0;
10170
10171 /* If depth of calling is getting too high, don't execute the function */
10172 if (depth >= p_mfd)
10173 {
10174 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
10175 retvar->var_type = VAR_NUMBER;
10176 retvar->var_val.var_number = -1;
10177 return;
10178 }
10179 ++depth;
10180
10181 line_breakcheck(); /* check for CTRL-C hit */
10182
10183 /* set local variables */
10184 var_init(&fc.l_vars);
10185 fc.func = fp;
10186 fc.argcount = argcount;
10187 fc.argvars = argvars;
10188 fc.retvar = retvar;
10189 retvar->var_val.var_number = 0;
10190 fc.linenr = 0;
10191 fc.returned = FALSE;
10192 fc.level = ex_nesting_level;
10193 fc.a0_var.var_type = VAR_NUMBER;
10194 fc.a0_var.var_val.var_number = argcount - fp->args.ga_len;
10195 fc.a0_var.var_name = NULL;
10196 current_funccal = &fc;
10197 fc.firstline.var_type = VAR_NUMBER;
10198 fc.firstline.var_val.var_number = firstline;
10199 fc.firstline.var_name = NULL;
10200 fc.lastline.var_type = VAR_NUMBER;
10201 fc.lastline.var_val.var_number = lastline;
10202 fc.lastline.var_name = NULL;
10203 /* Check if this function has a breakpoint. */
10204 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
10205 fc.dbg_tick = debug_tick;
10206
10207 /* Don't redraw while executing the function. */
10208 ++RedrawingDisabled;
10209 save_sourcing_name = sourcing_name;
10210 save_sourcing_lnum = sourcing_lnum;
10211 sourcing_lnum = 1;
10212 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
10213 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
10214 if (sourcing_name != NULL)
10215 {
10216 if (save_sourcing_name != NULL
10217 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
10218 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
10219 else
10220 STRCPY(sourcing_name, "function ");
10221 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
10222
10223 if (p_verbose >= 12)
10224 {
10225 ++no_wait_return;
10226 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10227 msg_str((char_u *)_("calling %s"), sourcing_name);
10228 if (p_verbose >= 14)
10229 {
10230 int i;
10231 char_u buf[MSG_BUF_LEN];
10232
10233 msg_puts((char_u *)"(");
10234 for (i = 0; i < argcount; ++i)
10235 {
10236 if (i > 0)
10237 msg_puts((char_u *)", ");
10238 if (argvars[i].var_type == VAR_NUMBER)
10239 msg_outnum((long)argvars[i].var_val.var_number);
10240 else
10241 {
10242 trunc_string(get_var_string(&argvars[i]),
10243 buf, MSG_BUF_LEN);
10244 msg_puts((char_u *)"\"");
10245 msg_puts(buf);
10246 msg_puts((char_u *)"\"");
10247 }
10248 }
10249 msg_puts((char_u *)")");
10250 }
10251 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10252 cmdline_row = msg_row;
10253 --no_wait_return;
10254 }
10255 }
10256 save_current_SID = current_SID;
10257 current_SID = fp->script_ID;
10258 save_did_emsg = did_emsg;
10259 did_emsg = FALSE;
10260
10261 /* call do_cmdline() to execute the lines */
10262 do_cmdline(NULL, get_func_line, (void *)&fc,
10263 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
10264
10265 --RedrawingDisabled;
10266
10267 /* when the function was aborted because of an error, return -1 */
10268 if ((did_emsg && (fp->flags & FC_ABORT)) || retvar->var_type == VAR_UNKNOWN)
10269 {
10270 clear_var(retvar);
10271 retvar->var_type = VAR_NUMBER;
10272 retvar->var_val.var_number = -1;
10273 }
10274
10275 /* when being verbose, mention the return value */
10276 if (p_verbose >= 12)
10277 {
10278 char_u *sn, *val;
10279
10280 ++no_wait_return;
10281 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10282
10283 /* Make sure the output fits in IObuff. */
10284 sn = sourcing_name;
10285 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
10286 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
10287
10288 if (aborting())
10289 smsg((char_u *)_("%s aborted"), sn);
10290 else if (fc.retvar->var_type == VAR_NUMBER)
10291 smsg((char_u *)_("%s returning #%ld"), sn,
10292 (long)fc.retvar->var_val.var_number);
10293 else if (fc.retvar->var_type == VAR_STRING)
10294 {
10295 val = get_var_string(fc.retvar);
10296 if (STRLEN(val) > IOSIZE / 2 - 50)
10297 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
10298 smsg((char_u *)_("%s returning \"%s\""), sn, val);
10299 }
10300 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10301 cmdline_row = msg_row;
10302 --no_wait_return;
10303 }
10304
10305 vim_free(sourcing_name);
10306 sourcing_name = save_sourcing_name;
10307 sourcing_lnum = save_sourcing_lnum;
10308 current_SID = save_current_SID;
10309
10310 if (p_verbose >= 12 && sourcing_name != NULL)
10311 {
10312 ++no_wait_return;
10313 msg_scroll = TRUE; /* always scroll up, don't overwrite */
10314 msg_str((char_u *)_("continuing in %s"), sourcing_name);
10315 msg_puts((char_u *)"\n"); /* don't overwrite this either */
10316 cmdline_row = msg_row;
10317 --no_wait_return;
10318 }
10319
10320 did_emsg |= save_did_emsg;
10321 current_funccal = save_fcp;
10322
10323 var_clear(&fc.l_vars); /* free all local variables */
10324 --depth;
10325}
10326
10327/*
10328 * ":return [expr]"
10329 */
10330 void
10331ex_return(eap)
10332 exarg_T *eap;
10333{
10334 char_u *arg = eap->arg;
10335 var retvar;
10336 int returning = FALSE;
10337
10338 if (current_funccal == NULL)
10339 {
10340 EMSG(_("E133: :return not inside a function"));
10341 return;
10342 }
10343
10344 if (eap->skip)
10345 ++emsg_skip;
10346
10347 eap->nextcmd = NULL;
10348 if ((*arg != NUL && *arg != '|' && *arg != '\n')
10349 && eval0(arg, &retvar, &eap->nextcmd, !eap->skip) != FAIL)
10350 {
10351 if (!eap->skip)
10352 returning = do_return(eap, FALSE, TRUE, &retvar);
10353 else
10354 clear_var(&retvar);
10355 }
10356 /* It's safer to return also on error. */
10357 else if (!eap->skip)
10358 {
10359 /*
10360 * Return unless the expression evaluation has been cancelled due to an
10361 * aborting error, an interrupt, or an exception.
10362 */
10363 if (!aborting())
10364 returning = do_return(eap, FALSE, TRUE, NULL);
10365 }
10366
10367 /* When skipping or the return gets pending, advance to the next command
10368 * in this line (!returning). Otherwise, ignore the rest of the line.
10369 * Following lines will be ignored by get_func_line(). */
10370 if (returning)
10371 eap->nextcmd = NULL;
10372 else if (eap->nextcmd == NULL) /* no argument */
10373 eap->nextcmd = check_nextcmd(arg);
10374
10375 if (eap->skip)
10376 --emsg_skip;
10377}
10378
10379/*
10380 * Return from a function. Possibly makes the return pending. Also called
10381 * for a pending return at the ":endtry" or after returning from an extra
10382 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
10383 * when called due to a ":return" command. "value" may point to a variable
10384 * with the return value. Returns TRUE when the return can be carried out,
10385 * FALSE when the return gets pending.
10386 */
10387 int
10388do_return(eap, reanimate, is_cmd, value)
10389 exarg_T *eap;
10390 int reanimate;
10391 int is_cmd;
10392 void *value;
10393{
10394 int idx;
10395 struct condstack *cstack = eap->cstack;
10396
10397 if (reanimate)
10398 /* Undo the return. */
10399 current_funccal->returned = FALSE;
10400
10401 /*
10402 * Cleanup (and inactivate) conditionals, but stop when a try conditional
10403 * not in its finally clause (which then is to be executed next) is found.
10404 * In this case, make the ":return" pending for execution at the ":endtry".
10405 * Otherwise, return normally.
10406 */
10407 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
10408 if (idx >= 0)
10409 {
10410 cstack->cs_pending[idx] = CSTP_RETURN;
10411
10412 if (!is_cmd && !reanimate)
10413 /* A pending return again gets pending. "value" points to an
10414 * allocated variable with the value of the original ":return"'s
10415 * argument if present or is NULL else. */
10416 cstack->cs_retvar[idx] = value;
10417 else
10418 {
10419 /* When undoing a return in order to make it pending, get the stored
10420 * return value. */
10421 if (reanimate)
10422 value = current_funccal->retvar;
10423
10424 if (value != NULL)
10425 {
10426 /* Store the value of the pending return. */
10427 if ((cstack->cs_retvar[idx] = alloc_var()) != NULL)
10428 *(VAR)cstack->cs_retvar[idx] = *(VAR)value;
10429 else
10430 EMSG(_(e_outofmem));
10431 }
10432 else
10433 cstack->cs_retvar[idx] = NULL;
10434
10435 if (reanimate)
10436 {
10437 /* The pending return value could be overwritten by a ":return"
10438 * without argument in a finally clause; reset the default
10439 * return value. */
10440 current_funccal->retvar->var_type = VAR_NUMBER;
10441 current_funccal->retvar->var_val.var_number = 0;
10442 }
10443 }
10444 report_make_pending(CSTP_RETURN, value);
10445 }
10446 else
10447 {
10448 current_funccal->returned = TRUE;
10449
10450 /* If the return is carried out now, store the return value. For
10451 * a return immediately after reanimation, the value is already
10452 * there. */
10453 if (!reanimate && value != NULL)
10454 {
10455 clear_var(current_funccal->retvar);
10456 *current_funccal->retvar = *(VAR)value;
10457 if (!is_cmd)
10458 vim_free(value);
10459 }
10460 }
10461
10462 return idx < 0;
10463}
10464
10465/*
10466 * Free the variable with a pending return value.
10467 */
10468 void
10469discard_pending_return(retvar)
10470 void *retvar;
10471{
10472 /* The variable was copied from one with an undefined var_name. So we can't
10473 * use free_var() to clear and free it. */
10474 clear_var((VAR)retvar);
10475 vim_free(retvar);
10476}
10477
10478/*
10479 * Generate a return command for producing the value of "retvar". The result
10480 * is an allocated string. Used by report_pending() for verbose messages.
10481 */
10482 char_u *
10483get_return_cmd(retvar)
10484 void *retvar;
10485{
10486 char_u *s = IObuff;
10487
10488 if (retvar == NULL || ((VAR)retvar)->var_type == VAR_UNKNOWN)
10489 s = (char_u *)":return";
10490 else if (((VAR)retvar)->var_type == VAR_STRING)
10491 sprintf((char *)IObuff, ":return \"%s\"",
10492 ((VAR)retvar)->var_val.var_string);
10493 else
10494 sprintf((char *)IObuff, ":return %ld",
10495 (long)(((VAR)retvar)->var_val.var_number));
10496 return vim_strsave(s);
10497}
10498
10499/*
10500 * Get next function line.
10501 * Called by do_cmdline() to get the next line.
10502 * Returns allocated string, or NULL for end of function.
10503 */
10504/* ARGSUSED */
10505 char_u *
10506get_func_line(c, cookie, indent)
10507 int c; /* not used */
10508 void *cookie;
10509 int indent; /* not used */
10510{
10511 struct funccall *fcp = (struct funccall *)cookie;
10512 char_u *retval;
10513 garray_T *gap; /* growarray with function lines */
10514
10515 /* If breakpoints have been added/deleted need to check for it. */
10516 if (fcp->dbg_tick != debug_tick)
10517 {
10518 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10519 sourcing_lnum);
10520 fcp->dbg_tick = debug_tick;
10521 }
10522
10523 gap = &fcp->func->lines;
10524 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10525 retval = NULL;
10526 else if (fcp->returned || fcp->linenr >= gap->ga_len)
10527 retval = NULL;
10528 else
10529 {
10530 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
10531 sourcing_lnum = fcp->linenr;
10532 }
10533
10534 /* Did we encounter a breakpoint? */
10535 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
10536 {
10537 dbg_breakpoint(fcp->func->name, sourcing_lnum);
10538 /* Find next breakpoint. */
10539 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
10540 sourcing_lnum);
10541 fcp->dbg_tick = debug_tick;
10542 }
10543
10544 return retval;
10545}
10546
10547/*
10548 * Return TRUE if the currently active function should be ended, because a
10549 * return was encountered or an error occured. Used inside a ":while".
10550 */
10551 int
10552func_has_ended(cookie)
10553 void *cookie;
10554{
10555 struct funccall *fcp = (struct funccall *)cookie;
10556
10557 /* Ignore the "abort" flag if the abortion behavior has been changed due to
10558 * an error inside a try conditional. */
10559 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
10560 || fcp->returned);
10561}
10562
10563/*
10564 * return TRUE if cookie indicates a function which "abort"s on errors.
10565 */
10566 int
10567func_has_abort(cookie)
10568 void *cookie;
10569{
10570 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
10571}
10572
10573#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
10574typedef enum
10575{
10576 VAR_FLAVOUR_DEFAULT,
10577 VAR_FLAVOUR_SESSION,
10578 VAR_FLAVOUR_VIMINFO
10579} var_flavour_T;
10580
10581static var_flavour_T var_flavour __ARGS((char_u *varname));
10582
10583 static var_flavour_T
10584var_flavour(varname)
10585 char_u *varname;
10586{
10587 char_u *p = varname;
10588
10589 if (ASCII_ISUPPER(*p))
10590 {
10591 while (*(++p))
10592 if (ASCII_ISLOWER(*p))
10593 return VAR_FLAVOUR_SESSION;
10594 return VAR_FLAVOUR_VIMINFO;
10595 }
10596 else
10597 return VAR_FLAVOUR_DEFAULT;
10598}
10599#endif
10600
10601#if defined(FEAT_VIMINFO) || defined(PROTO)
10602/*
10603 * Restore global vars that start with a capital from the viminfo file
10604 */
10605 int
10606read_viminfo_varlist(virp, writing)
10607 vir_T *virp;
10608 int writing;
10609{
10610 char_u *tab;
10611 int is_string = FALSE;
10612 VAR varp = NULL;
10613 char_u *val;
10614
10615 if (!writing && (find_viminfo_parameter('!') != NULL))
10616 {
10617 tab = vim_strchr(virp->vir_line + 1, '\t');
10618 if (tab != NULL)
10619 {
10620 *tab++ = '\0'; /* isolate the variable name */
10621 if (*tab == 'S') /* string var */
10622 is_string = TRUE;
10623
10624 tab = vim_strchr(tab, '\t');
10625 if (tab != NULL)
10626 {
10627 /* create a nameless variable to hold the value */
10628 if (is_string)
10629 {
10630 val = viminfo_readstring(virp,
10631 (int)(tab - virp->vir_line + 1), TRUE);
10632 if (val != NULL)
10633 varp = alloc_string_var(val);
10634 }
10635 else
10636 {
10637 varp = alloc_var();
10638 if (varp != NULL)
10639 {
10640 varp->var_type = VAR_NUMBER;
10641 varp->var_val.var_number = atol((char *)tab + 1);
10642 }
10643 }
10644 /* assign the value to the variable */
10645 if (varp != NULL)
10646 {
10647 set_var(virp->vir_line + 1, varp);
10648 free_var(varp);
10649 }
10650 }
10651 }
10652 }
10653
10654 return viminfo_readline(virp);
10655}
10656
10657/*
10658 * Write global vars that start with a capital to the viminfo file
10659 */
10660 void
10661write_viminfo_varlist(fp)
10662 FILE *fp;
10663{
10664 garray_T *gap = &variables; /* global variable */
10665 VAR this_var;
10666 int i;
10667
10668 if (find_viminfo_parameter('!') == NULL)
10669 return;
10670
10671 fprintf(fp, _("\n# global variables:\n"));
10672 for (i = gap->ga_len; --i >= 0; )
10673 {
10674 this_var = &VAR_GAP_ENTRY(i, gap);
10675 if (this_var->var_name != NULL
10676 && var_flavour(this_var->var_name) == VAR_FLAVOUR_VIMINFO)
10677 {
10678 fprintf(fp, "!%s\t%s\t", this_var->var_name,
10679 (this_var->var_type == VAR_STRING) ? "STR" : "NUM");
10680 viminfo_writestring(fp, get_var_string(this_var));
10681 }
10682 }
10683}
10684#endif
10685
10686#if defined(FEAT_SESSION) || defined(PROTO)
10687 int
10688store_session_globals(fd)
10689 FILE *fd;
10690{
10691 garray_T *gap = &variables; /* global variable */
10692 VAR this_var;
10693 int i;
10694 char_u *p, *t;
10695
10696 for (i = gap->ga_len; --i >= 0; )
10697 {
10698 this_var = &VAR_GAP_ENTRY(i, gap);
10699 if (this_var->var_name != NULL)
10700 {
10701 if (var_flavour(this_var->var_name) == VAR_FLAVOUR_SESSION)
10702 {
10703 /* Escapse special characters with a backslash. Turn a LF and
10704 * CR into \n and \r. */
10705 p = vim_strsave_escaped(get_var_string(this_var),
10706 (char_u *)"\\\"\n\r");
10707 if (p == NULL) /* out of memory */
10708 continue;
10709 for (t = p; *t != NUL; ++t)
10710 if (*t == '\n')
10711 *t = 'n';
10712 else if (*t == '\r')
10713 *t = 'r';
10714 if ((fprintf(fd, "let %s = %c%s%c",
10715 this_var->var_name,
10716 (this_var->var_type == VAR_STRING) ? '"' : ' ',
10717 p,
10718 (this_var->var_type == VAR_STRING) ? '"' : ' ') < 0)
10719 || put_eol(fd) == FAIL)
10720 {
10721 vim_free(p);
10722 return FAIL;
10723 }
10724 vim_free(p);
10725 }
10726
10727 }
10728 }
10729 return OK;
10730}
10731#endif
10732
10733#endif /* FEAT_EVAL */
10734
10735#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
10736
10737
10738#ifdef WIN3264
10739/*
10740 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10741 */
10742static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10743static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
10744static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
10745
10746/*
10747 * Get the short pathname of a file.
10748 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
10749 */
10750 static int
10751get_short_pathname(fnamep, bufp, fnamelen)
10752 char_u **fnamep;
10753 char_u **bufp;
10754 int *fnamelen;
10755{
10756 int l,len;
10757 char_u *newbuf;
10758
10759 len = *fnamelen;
10760
10761 l = GetShortPathName(*fnamep, *fnamep, len);
10762 if (l > len - 1)
10763 {
10764 /* If that doesn't work (not enough space), then save the string
10765 * and try again with a new buffer big enough
10766 */
10767 newbuf = vim_strnsave(*fnamep, l);
10768 if (newbuf == NULL)
10769 return 0;
10770
10771 vim_free(*bufp);
10772 *fnamep = *bufp = newbuf;
10773
10774 l = GetShortPathName(*fnamep,*fnamep,l+1);
10775
10776 /* Really should always succeed, as the buffer is big enough */
10777 }
10778
10779 *fnamelen = l;
10780 return 1;
10781}
10782
10783/*
10784 * Create a short path name. Returns the length of the buffer it needs.
10785 * Doesn't copy over the end of the buffer passed in.
10786 */
10787 static int
10788shortpath_for_invalid_fname(fname, bufp, fnamelen)
10789 char_u **fname;
10790 char_u **bufp;
10791 int *fnamelen;
10792{
10793 char_u *s, *p, *pbuf2, *pbuf3;
10794 char_u ch;
10795 int l,len,len2,plen,slen;
10796
10797 /* Make a copy */
10798 len2 = *fnamelen;
10799 pbuf2 = vim_strnsave(*fname, len2);
10800 pbuf3 = NULL;
10801
10802 s = pbuf2 + len2 - 1; /* Find the end */
10803 slen = 1;
10804 plen = len2;
10805
10806 l = 0;
10807 if (vim_ispathsep(*s))
10808 {
10809 --s;
10810 ++slen;
10811 --plen;
10812 }
10813
10814 do
10815 {
10816 /* Go back one path-seperator */
10817 while (s > pbuf2 && !vim_ispathsep(*s))
10818 {
10819 --s;
10820 ++slen;
10821 --plen;
10822 }
10823 if (s <= pbuf2)
10824 break;
10825
10826 /* Remeber the character that is about to be blatted */
10827 ch = *s;
10828 *s = 0; /* get_short_pathname requires a null-terminated string */
10829
10830 /* Try it in situ */
10831 p = pbuf2;
10832 if (!get_short_pathname(&p, &pbuf3, &plen))
10833 {
10834 vim_free(pbuf2);
10835 return -1;
10836 }
10837 *s = ch; /* Preserve the string */
10838 } while (plen == 0);
10839
10840 if (plen > 0)
10841 {
10842 /* Remeber the length of the new string. */
10843 *fnamelen = len = plen + slen;
10844 vim_free(*bufp);
10845 if (len > len2)
10846 {
10847 /* If there's not enough space in the currently allocated string,
10848 * then copy it to a buffer big enough.
10849 */
10850 *fname= *bufp = vim_strnsave(p, len);
10851 if (*fname == NULL)
10852 return -1;
10853 }
10854 else
10855 {
10856 /* Transfer pbuf2 to being the main buffer (it's big enough) */
10857 *fname = *bufp = pbuf2;
10858 if (p != pbuf2)
10859 strncpy(*fname, p, plen);
10860 pbuf2 = NULL;
10861 }
10862 /* Concat the next bit */
10863 strncpy(*fname + plen, s, slen);
10864 (*fname)[len] = '\0';
10865 }
10866 vim_free(pbuf3);
10867 vim_free(pbuf2);
10868 return 0;
10869}
10870
10871/*
10872 * Get a pathname for a partial path.
10873 */
10874 static int
10875shortpath_for_partial(fnamep, bufp, fnamelen)
10876 char_u **fnamep;
10877 char_u **bufp;
10878 int *fnamelen;
10879{
10880 int sepcount, len, tflen;
10881 char_u *p;
10882 char_u *pbuf, *tfname;
10883 int hasTilde;
10884
10885 /* Count up the path seperators from the RHS.. so we know which part
10886 * of the path to return.
10887 */
10888 sepcount = 0;
10889 for (p = *fnamep + *fnamelen - 1; p >= *fnamep; --p)
10890 if (vim_ispathsep(*p))
10891 ++sepcount;
10892
10893 /* Need full path first (use expand_env() to remove a "~/") */
10894 hasTilde = (**fnamep == '~');
10895 if (hasTilde)
10896 pbuf = tfname = expand_env_save(*fnamep);
10897 else
10898 pbuf = tfname = FullName_save(*fnamep, FALSE);
10899
10900 len = tflen = STRLEN(tfname);
10901
10902 if (!get_short_pathname(&tfname, &pbuf, &len))
10903 return -1;
10904
10905 if (len == 0)
10906 {
10907 /* Don't have a valid filename, so shorten the rest of the
10908 * path if we can. This CAN give us invalid 8.3 filenames, but
10909 * there's not a lot of point in guessing what it might be.
10910 */
10911 len = tflen;
10912 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
10913 return -1;
10914 }
10915
10916 /* Count the paths backward to find the beginning of the desired string. */
10917 for (p = tfname + len - 1; p >= tfname; --p)
10918 if (vim_ispathsep(*p))
10919 {
10920 if (sepcount == 0 || (hasTilde && sepcount == 1))
10921 break;
10922 else
10923 sepcount --;
10924 }
10925 if (hasTilde)
10926 {
10927 --p;
10928 if (p >= tfname)
10929 *p = '~';
10930 else
10931 return -1;
10932 }
10933 else
10934 ++p;
10935
10936 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10937 vim_free(*bufp);
10938 *fnamelen = (int)STRLEN(p);
10939 *bufp = pbuf;
10940 *fnamep = p;
10941
10942 return 0;
10943}
10944#endif /* WIN3264 */
10945
10946/*
10947 * Adjust a filename, according to a string of modifiers.
10948 * *fnamep must be NUL terminated when called. When returning, the length is
10949 * determined by *fnamelen.
10950 * Returns valid flags.
10951 * When there is an error, *fnamep is set to NULL.
10952 */
10953 int
10954modify_fname(src, usedlen, fnamep, bufp, fnamelen)
10955 char_u *src; /* string with modifiers */
10956 int *usedlen; /* characters after src that are used */
10957 char_u **fnamep; /* file name so far */
10958 char_u **bufp; /* buffer for allocated file name or NULL */
10959 int *fnamelen; /* length of fnamep */
10960{
10961 int valid = 0;
10962 char_u *tail;
10963 char_u *s, *p, *pbuf;
10964 char_u dirname[MAXPATHL];
10965 int c;
10966 int has_fullname = 0;
10967#ifdef WIN3264
10968 int has_shortname = 0;
10969#endif
10970
10971repeat:
10972 /* ":p" - full path/file_name */
10973 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10974 {
10975 has_fullname = 1;
10976
10977 valid |= VALID_PATH;
10978 *usedlen += 2;
10979
10980 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10981 if ((*fnamep)[0] == '~'
10982#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10983 && ((*fnamep)[1] == '/'
10984# ifdef BACKSLASH_IN_FILENAME
10985 || (*fnamep)[1] == '\\'
10986# endif
10987 || (*fnamep)[1] == NUL)
10988
10989#endif
10990 )
10991 {
10992 *fnamep = expand_env_save(*fnamep);
10993 vim_free(*bufp); /* free any allocated file name */
10994 *bufp = *fnamep;
10995 if (*fnamep == NULL)
10996 return -1;
10997 }
10998
10999 /* When "/." or "/.." is used: force expansion to get rid of it. */
11000 for (p = *fnamep; *p != NUL; ++p)
11001 {
11002 if (vim_ispathsep(*p)
11003 && p[1] == '.'
11004 && (p[2] == NUL
11005 || vim_ispathsep(p[2])
11006 || (p[2] == '.'
11007 && (p[3] == NUL || vim_ispathsep(p[3])))))
11008 break;
11009 }
11010
11011 /* FullName_save() is slow, don't use it when not needed. */
11012 if (*p != NUL || !vim_isAbsName(*fnamep))
11013 {
11014 *fnamep = FullName_save(*fnamep, *p != NUL);
11015 vim_free(*bufp); /* free any allocated file name */
11016 *bufp = *fnamep;
11017 if (*fnamep == NULL)
11018 return -1;
11019 }
11020
11021 /* Append a path separator to a directory. */
11022 if (mch_isdir(*fnamep))
11023 {
11024 /* Make room for one or two extra characters. */
11025 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
11026 vim_free(*bufp); /* free any allocated file name */
11027 *bufp = *fnamep;
11028 if (*fnamep == NULL)
11029 return -1;
11030 add_pathsep(*fnamep);
11031 }
11032 }
11033
11034 /* ":." - path relative to the current directory */
11035 /* ":~" - path relative to the home directory */
11036 /* ":8" - shortname path - postponed till after */
11037 while (src[*usedlen] == ':'
11038 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
11039 {
11040 *usedlen += 2;
11041 if (c == '8')
11042 {
11043#ifdef WIN3264
11044 has_shortname = 1; /* Postpone this. */
11045#endif
11046 continue;
11047 }
11048 pbuf = NULL;
11049 /* Need full path first (use expand_env() to remove a "~/") */
11050 if (!has_fullname)
11051 {
11052 if (c == '.' && **fnamep == '~')
11053 p = pbuf = expand_env_save(*fnamep);
11054 else
11055 p = pbuf = FullName_save(*fnamep, FALSE);
11056 }
11057 else
11058 p = *fnamep;
11059
11060 has_fullname = 0;
11061
11062 if (p != NULL)
11063 {
11064 if (c == '.')
11065 {
11066 mch_dirname(dirname, MAXPATHL);
11067 s = shorten_fname(p, dirname);
11068 if (s != NULL)
11069 {
11070 *fnamep = s;
11071 if (pbuf != NULL)
11072 {
11073 vim_free(*bufp); /* free any allocated file name */
11074 *bufp = pbuf;
11075 pbuf = NULL;
11076 }
11077 }
11078 }
11079 else
11080 {
11081 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
11082 /* Only replace it when it starts with '~' */
11083 if (*dirname == '~')
11084 {
11085 s = vim_strsave(dirname);
11086 if (s != NULL)
11087 {
11088 *fnamep = s;
11089 vim_free(*bufp);
11090 *bufp = s;
11091 }
11092 }
11093 }
11094 vim_free(pbuf);
11095 }
11096 }
11097
11098 tail = gettail(*fnamep);
11099 *fnamelen = (int)STRLEN(*fnamep);
11100
11101 /* ":h" - head, remove "/file_name", can be repeated */
11102 /* Don't remove the first "/" or "c:\" */
11103 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
11104 {
11105 valid |= VALID_HEAD;
11106 *usedlen += 2;
11107 s = get_past_head(*fnamep);
11108 while (tail > s && vim_ispathsep(tail[-1]))
11109 --tail;
11110 *fnamelen = (int)(tail - *fnamep);
11111#ifdef VMS
11112 if (*fnamelen > 0)
11113 *fnamelen += 1; /* the path separator is part of the path */
11114#endif
11115 while (tail > s && !vim_ispathsep(tail[-1]))
11116 --tail;
11117 }
11118
11119 /* ":8" - shortname */
11120 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
11121 {
11122 *usedlen += 2;
11123#ifdef WIN3264
11124 has_shortname = 1;
11125#endif
11126 }
11127
11128#ifdef WIN3264
11129 /* Check shortname after we have done 'heads' and before we do 'tails'
11130 */
11131 if (has_shortname)
11132 {
11133 pbuf = NULL;
11134 /* Copy the string if it is shortened by :h */
11135 if (*fnamelen < (int)STRLEN(*fnamep))
11136 {
11137 p = vim_strnsave(*fnamep, *fnamelen);
11138 if (p == 0)
11139 return -1;
11140 vim_free(*bufp);
11141 *bufp = *fnamep = p;
11142 }
11143
11144 /* Split into two implementations - makes it easier. First is where
11145 * there isn't a full name already, second is where there is.
11146 */
11147 if (!has_fullname && !vim_isAbsName(*fnamep))
11148 {
11149 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
11150 return -1;
11151 }
11152 else
11153 {
11154 int l;
11155
11156 /* Simple case, already have the full-name
11157 * Nearly always shorter, so try first time. */
11158 l = *fnamelen;
11159 if (!get_short_pathname(fnamep, bufp, &l))
11160 return -1;
11161
11162 if (l == 0)
11163 {
11164 /* Couldn't find the filename.. search the paths.
11165 */
11166 l = *fnamelen;
11167 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
11168 return -1;
11169 }
11170 *fnamelen = l;
11171 }
11172 }
11173#endif /* WIN3264 */
11174
11175 /* ":t" - tail, just the basename */
11176 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
11177 {
11178 *usedlen += 2;
11179 *fnamelen -= (int)(tail - *fnamep);
11180 *fnamep = tail;
11181 }
11182
11183 /* ":e" - extension, can be repeated */
11184 /* ":r" - root, without extension, can be repeated */
11185 while (src[*usedlen] == ':'
11186 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
11187 {
11188 /* find a '.' in the tail:
11189 * - for second :e: before the current fname
11190 * - otherwise: The last '.'
11191 */
11192 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
11193 s = *fnamep - 2;
11194 else
11195 s = *fnamep + *fnamelen - 1;
11196 for ( ; s > tail; --s)
11197 if (s[0] == '.')
11198 break;
11199 if (src[*usedlen + 1] == 'e') /* :e */
11200 {
11201 if (s > tail)
11202 {
11203 *fnamelen += (int)(*fnamep - (s + 1));
11204 *fnamep = s + 1;
11205#ifdef VMS
11206 /* cut version from the extension */
11207 s = *fnamep + *fnamelen - 1;
11208 for ( ; s > *fnamep; --s)
11209 if (s[0] == ';')
11210 break;
11211 if (s > *fnamep)
11212 *fnamelen = s - *fnamep;
11213#endif
11214 }
11215 else if (*fnamep <= tail)
11216 *fnamelen = 0;
11217 }
11218 else /* :r */
11219 {
11220 if (s > tail) /* remove one extension */
11221 *fnamelen = (int)(s - *fnamep);
11222 }
11223 *usedlen += 2;
11224 }
11225
11226 /* ":s?pat?foo?" - substitute */
11227 /* ":gs?pat?foo?" - global substitute */
11228 if (src[*usedlen] == ':'
11229 && (src[*usedlen + 1] == 's'
11230 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
11231 {
11232 char_u *str;
11233 char_u *pat;
11234 char_u *sub;
11235 int sep;
11236 char_u *flags;
11237 int didit = FALSE;
11238
11239 flags = (char_u *)"";
11240 s = src + *usedlen + 2;
11241 if (src[*usedlen + 1] == 'g')
11242 {
11243 flags = (char_u *)"g";
11244 ++s;
11245 }
11246
11247 sep = *s++;
11248 if (sep)
11249 {
11250 /* find end of pattern */
11251 p = vim_strchr(s, sep);
11252 if (p != NULL)
11253 {
11254 pat = vim_strnsave(s, (int)(p - s));
11255 if (pat != NULL)
11256 {
11257 s = p + 1;
11258 /* find end of substitution */
11259 p = vim_strchr(s, sep);
11260 if (p != NULL)
11261 {
11262 sub = vim_strnsave(s, (int)(p - s));
11263 str = vim_strnsave(*fnamep, *fnamelen);
11264 if (sub != NULL && str != NULL)
11265 {
11266 *usedlen = (int)(p + 1 - src);
11267 s = do_string_sub(str, pat, sub, flags);
11268 if (s != NULL)
11269 {
11270 *fnamep = s;
11271 *fnamelen = (int)STRLEN(s);
11272 vim_free(*bufp);
11273 *bufp = s;
11274 didit = TRUE;
11275 }
11276 }
11277 vim_free(sub);
11278 vim_free(str);
11279 }
11280 vim_free(pat);
11281 }
11282 }
11283 /* after using ":s", repeat all the modifiers */
11284 if (didit)
11285 goto repeat;
11286 }
11287 }
11288
11289 return valid;
11290}
11291
11292/*
11293 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
11294 * "flags" can be "g" to do a global substitute.
11295 * Returns an allocated string, NULL for error.
11296 */
11297 char_u *
11298do_string_sub(str, pat, sub, flags)
11299 char_u *str;
11300 char_u *pat;
11301 char_u *sub;
11302 char_u *flags;
11303{
11304 int sublen;
11305 regmatch_T regmatch;
11306 int i;
11307 int do_all;
11308 char_u *tail;
11309 garray_T ga;
11310 char_u *ret;
11311 char_u *save_cpo;
11312
11313 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
11314 save_cpo = p_cpo;
11315 p_cpo = (char_u *)"";
11316
11317 ga_init2(&ga, 1, 200);
11318
11319 do_all = (flags[0] == 'g');
11320
11321 regmatch.rm_ic = p_ic;
11322 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11323 if (regmatch.regprog != NULL)
11324 {
11325 tail = str;
11326 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
11327 {
11328 /*
11329 * Get some space for a temporary buffer to do the substitution
11330 * into. It will contain:
11331 * - The text up to where the match is.
11332 * - The substituted text.
11333 * - The text after the match.
11334 */
11335 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
11336 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
11337 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11338 {
11339 ga_clear(&ga);
11340 break;
11341 }
11342
11343 /* copy the text up to where the match is */
11344 i = (int)(regmatch.startp[0] - tail);
11345 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11346 /* add the substituted text */
11347 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
11348 + ga.ga_len + i, TRUE, TRUE, FALSE);
11349 ga.ga_len += i + sublen - 1;
11350 ga.ga_room -= i + sublen - 1;
11351 /* avoid getting stuck on a match with an empty string */
11352 if (tail == regmatch.endp[0])
11353 {
11354 if (*tail == NUL)
11355 break;
11356 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
11357 ++ga.ga_len;
11358 --ga.ga_room;
11359 }
11360 else
11361 {
11362 tail = regmatch.endp[0];
11363 if (*tail == NUL)
11364 break;
11365 }
11366 if (!do_all)
11367 break;
11368 }
11369
11370 if (ga.ga_data != NULL)
11371 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11372
11373 vim_free(regmatch.regprog);
11374 }
11375
11376 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11377 ga_clear(&ga);
11378 p_cpo = save_cpo;
11379
11380 return ret;
11381}
11382
11383#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */