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