blob: c25cb124ea499467495ae53416c72b1a3f960529 [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/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000040 * Structure to hold an internal variable without a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 */
42typedef struct
43{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000044 char v_type; /* see below: VAR_NUMBER, VAR_STRING, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 union
46 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000047 varnumber_T v_number; /* number value */
48 char_u *v_string; /* string value (can be NULL!) */
49 struct listvar_S *v_list; /* list value (can be NULL!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +000050 struct dictvar_S *v_dict; /* dict value (can be NULL!) */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000051 } vval;
52} typeval;
53
54/* Values for "v_type". */
55#define VAR_UNKNOWN 0
56#define VAR_NUMBER 1 /* "v_number" is used */
57#define VAR_STRING 2 /* "v_string" is used */
58#define VAR_FUNC 3 /* "v_string" is function name */
59#define VAR_LIST 4 /* "v_list" is used */
Bram Moolenaar8c711452005-01-14 21:53:12 +000060#define VAR_DICT 5 /* "v_dict" is used */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000061
62/*
63 * Structure to hold an internal variable with a name.
64 * The "tv" must come first, so that this can be used as a "typeval" as well.
65 */
66typedef struct
67{
68 typeval tv; /* type and value of the variable */
69 char_u *v_name; /* name of variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} var;
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072typedef var * VAR;
73
74/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000075 * Structure to hold an item of a list: an internal variable without a name.
76 */
77struct listitem_S
78{
79 struct listitem_S *li_next; /* next item in list */
80 struct listitem_S *li_prev; /* previous item in list */
81 typeval li_tv; /* type and value of the variable */
82};
83
84typedef struct listitem_S listitem;
85
86/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000087 * Struct used by those that are using an item in a list.
88 */
89typedef struct listwatch_S
90{
91 listitem *lw_item; /* item being watched */
92 struct listwatch_S *lw_next; /* next watcher */
93} listwatch;
94
95/*
96 * Structure to hold info about a list.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000097 */
98struct listvar_S
99{
100 int lv_refcount; /* reference count */
101 listitem *lv_first; /* first item, NULL if none */
102 listitem *lv_last; /* last item, NULL if none */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000103 listwatch *lv_watch; /* first watcher, NULL if none */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000104};
105
106typedef struct listvar_S listvar;
107
Bram Moolenaare9a41262005-01-15 22:18:47 +0000108#define VAR_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000109
110/*
111 * Structure to hold an item of a Dictionary.
112 */
113struct dictitem_S
114{
115 struct dictitem_S *di_next; /* next item in list */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000116 char_u *di_key; /* key (never NULL!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000117 typeval di_tv; /* type and value of the variable */
118};
119
120typedef struct dictitem_S dictitem;
121
122/*
123 * Structure to hold info about a Dictionary.
124 */
125struct dictvar_S
126{
127 int dv_refcount; /* reference count */
128 dictitem *dv_first; /* first item, NULL if none */
129};
130
131typedef struct dictvar_S dictvar;
132
133
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000134static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000135static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000136static char *e_undefvar = N_("E121: Undefined variable: %s");
137static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000138static char *e_intern2 = N_("E685: Internal error: %s");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000139static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +0000140static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000141static char *e_emptykey = N_("E999: Empty key in Dictionary");
142static char *e_listreq = N_("E999: List required");
143static char *e_dictreq = N_("E999: Dictionary required");
144static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
145static char *e_dictkey = N_("E999: key not found in Dictionary: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000146
147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 * All user-defined global variables are stored in "variables".
149 */
150garray_T variables = {0, 0, sizeof(var), 4, NULL};
151
152/*
153 * Array to hold an array with variables local to each sourced script.
154 */
155static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
156#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
157
158
159#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
Bram Moolenaare9a41262005-01-15 22:18:47 +0000160#define VAR_GAP_ENTRY(idx, gap) (((VAR)((gap)->ga_data))[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
162#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
163
164static int echo_attr = 0; /* attributes used for ":echo" */
165
166/*
167 * Structure to hold info for a user function.
168 */
169typedef struct ufunc ufunc_T;
170
171struct ufunc
172{
173 ufunc_T *next; /* next function in list */
174 char_u *name; /* name of function; can start with <SNR>123_
175 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
176 int varargs; /* variable nr of arguments */
177 int flags;
178 int calls; /* nr of active calls */
179 garray_T args; /* arguments */
180 garray_T lines; /* function lines */
181 scid_T script_ID; /* ID of script where function was defined,
182 used for s: variables */
183};
184
185/* function flags */
186#define FC_ABORT 1 /* abort function on error */
187#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000188#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190/*
191 * All user-defined functions are found in the forward-linked function list.
192 * The first function is pointed at by firstfunc.
193 */
194ufunc_T *firstfunc = NULL;
195
196#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
197#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
198
199/* structure to hold info for a function that is currently being executed. */
200struct funccall
201{
202 ufunc_T *func; /* function being called */
203 int linenr; /* next line to be executed */
204 int returned; /* ":return" used */
205 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000206 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 var a0_var; /* "a:0" variable */
208 var firstline; /* "a:firstline" variable */
209 var lastline; /* "a:lastline" variable */
210 garray_T l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000211 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 linenr_T breakpoint; /* next line with breakpoint or zero */
213 int dbg_tick; /* debug_tick when breakpoint was set */
214 int level; /* top nesting level of executed function */
215};
216
217/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000218 * Info used by a ":for" loop.
219 */
220typedef struct forinfo_S
221{
222 int fi_semicolon; /* TRUE if ending in '; var]' */
223 int fi_varcount; /* nr of variables in the list */
224 listwatch fi_lw; /* keep an eye on the item used. */
225 listvar *fi_list; /* list being used */
226} forinfo;
227
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 * Return the name of the executed function.
230 */
231 char_u *
232func_name(cookie)
233 void *cookie;
234{
235 return ((struct funccall *)cookie)->func->name;
236}
237
238/*
239 * Return the address holding the next breakpoint line for a funccall cookie.
240 */
241 linenr_T *
242func_breakpoint(cookie)
243 void *cookie;
244{
245 return &((struct funccall *)cookie)->breakpoint;
246}
247
248/*
249 * Return the address holding the debug tick for a funccall cookie.
250 */
251 int *
252func_dbg_tick(cookie)
253 void *cookie;
254{
255 return &((struct funccall *)cookie)->dbg_tick;
256}
257
258/*
259 * Return the nesting level for a funccall cookie.
260 */
261 int
262func_level(cookie)
263 void *cookie;
264{
265 return ((struct funccall *)cookie)->level;
266}
267
268/* pointer to funccal for currently active function */
269struct funccall *current_funccal = NULL;
270
271/*
272 * Return TRUE when a function was ended by a ":return" command.
273 */
274 int
275current_func_returned()
276{
277 return current_funccal->returned;
278}
279
280
281/*
282 * Array to hold the value of v: variables.
283 */
284#include "version.h"
285
286/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000287#define VV_COMPAT 1 /* compatible, also used without "v:" */
288#define VV_RO 2 /* read-only */
289#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Bram Moolenaare9a41262005-01-15 22:18:47 +0000291#define VV_NAME(s) s, sizeof(s) - 1
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293struct vimvar
294{
295 char *name; /* name of variable, without v: */
296 int len; /* length of name */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000297 typeval tv; /* type and value */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000298 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299} vimvars[VV_LEN] =
Bram Moolenaare9a41262005-01-15 22:18:47 +0000300{
301 /*
302 * The order here must match the VV_ defines in vim.h!
303 */
304 {VV_NAME("count"), {VAR_NUMBER, {NULL}}, VV_COMPAT+VV_RO},
305 {VV_NAME("count1"), {VAR_NUMBER, {NULL}}, VV_RO},
306 {VV_NAME("prevcount"), {VAR_NUMBER, {NULL}}, VV_RO},
307 {VV_NAME("errmsg"), {VAR_STRING, {NULL}}, VV_COMPAT},
308 {VV_NAME("warningmsg"), {VAR_STRING, {NULL}}, 0},
309 {VV_NAME("statusmsg"), {VAR_STRING, {NULL}}, 0},
310 {VV_NAME("shell_error"), {VAR_NUMBER, {NULL}}, VV_COMPAT+VV_RO},
311 {VV_NAME("this_session"), {VAR_STRING, {NULL}}, VV_COMPAT},
312 {VV_NAME("version"), {VAR_NUMBER, {VIM_VERSION_100}}, VV_COMPAT+VV_RO},
313 {VV_NAME("lnum"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
314 {VV_NAME("termresponse"), {VAR_STRING, {NULL}}, VV_RO},
315 {VV_NAME("fname"), {VAR_STRING, {NULL}}, VV_RO},
316 {VV_NAME("lang"), {VAR_STRING, {NULL}}, VV_RO},
317 {VV_NAME("lc_time"), {VAR_STRING, {NULL}}, VV_RO},
318 {VV_NAME("ctype"), {VAR_STRING, {NULL}}, VV_RO},
319 {VV_NAME("charconvert_from"), {VAR_STRING, {NULL}}, VV_RO},
320 {VV_NAME("charconvert_to"), {VAR_STRING, {NULL}}, VV_RO},
321 {VV_NAME("fname_in"), {VAR_STRING, {NULL}}, VV_RO},
322 {VV_NAME("fname_out"), {VAR_STRING, {NULL}}, VV_RO},
323 {VV_NAME("fname_new"), {VAR_STRING, {NULL}}, VV_RO},
324 {VV_NAME("fname_diff"), {VAR_STRING, {NULL}}, VV_RO},
325 {VV_NAME("cmdarg"), {VAR_STRING, {NULL}}, VV_RO},
326 {VV_NAME("foldstart"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
327 {VV_NAME("foldend"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
328 {VV_NAME("folddashes"), {VAR_STRING, {NULL}}, VV_RO_SBX},
329 {VV_NAME("foldlevel"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
330 {VV_NAME("progname"), {VAR_STRING, {NULL}}, VV_RO},
331 {VV_NAME("servername"), {VAR_STRING, {NULL}}, VV_RO},
332 {VV_NAME("dying"), {VAR_NUMBER, {NULL}}, VV_RO},
333 {VV_NAME("exception"), {VAR_STRING, {NULL}}, VV_RO},
334 {VV_NAME("throwpoint"), {VAR_STRING, {NULL}}, VV_RO},
335 {VV_NAME("register"), {VAR_STRING, {NULL}}, VV_RO},
336 {VV_NAME("cmdbang"), {VAR_NUMBER, {NULL}}, VV_RO},
337 {VV_NAME("insertmode"), {VAR_STRING, {NULL}}, VV_RO},
338 {VV_NAME("val"), {VAR_UNKNOWN, {NULL}}, VV_RO},
339 {VV_NAME("key"), {VAR_UNKNOWN, {NULL}}, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340};
341
Bram Moolenaare9a41262005-01-15 22:18:47 +0000342/* shorthand */
343#define vv_nr tv.vval.v_number
344#define vv_str tv.vval.v_string
345
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
347static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
348static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
349static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
350static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
351static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
352static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
353static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
354static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
355static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
356static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
357static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
358static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000359static listvar *list_alloc __ARGS((void));
360static void list_unref __ARGS((listvar *l));
361static void list_free __ARGS((listvar *l));
362static listitem *listitem_alloc __ARGS((void));
363static void listitem_free __ARGS((listitem *item));
364static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000365static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
366static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000367static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000368static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000369static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000370static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000371static void list_append __ARGS((listvar *l, listitem *item));
372static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000373static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
374static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
375static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000376static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000377static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000378static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000379static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
380
Bram Moolenaar8c711452005-01-14 21:53:12 +0000381static dictvar *dict_alloc __ARGS((void));
382static void dict_unref __ARGS((dictvar *d));
383static void dict_free __ARGS((dictvar *d));
384static dictitem *dictitem_alloc __ARGS((void));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000385static dictitem *dictitem_copy __ARGS((dictitem *org));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000386static void dictitem_free __ARGS((dictitem *item));
387static void dict_add __ARGS((dictvar *d, dictitem *item));
388static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len));
389static char_u *dict2string __ARGS((typeval *tv));
390static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
391
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000392static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000393static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000394static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000395static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000397static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000398static int get_func_tv __ARGS((char_u *name, int len, typeval *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dictvar *selfdict));
399static int call_func __ARGS((char_u *name, int len, typeval *rettv, int argcount, typeval *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dictvar *selfdict));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000400
401static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000402static void f_append __ARGS((typeval *argvars, typeval *rettv));
403static void f_argc __ARGS((typeval *argvars, typeval *rettv));
404static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
405static void f_argv __ARGS((typeval *argvars, typeval *rettv));
406static void f_browse __ARGS((typeval *argvars, typeval *rettv));
407static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000408static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
409static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
410static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000411static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
412static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
413static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
414static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
415static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000416static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
418static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
419static void f_col __ARGS((typeval *argvars, typeval *rettv));
420static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
421static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000422static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000423static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
424static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
425static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
426static void f_delete __ARGS((typeval *argvars, typeval *rettv));
427static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
428static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
429static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000430static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000431static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000432static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000433static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
434static void f_executable __ARGS((typeval *argvars, typeval *rettv));
435static void f_exists __ARGS((typeval *argvars, typeval *rettv));
436static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000437static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000438static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
439static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000440static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000441static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
442static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000443static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
444static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
445static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000446static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
447static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
448static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
449static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
450static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000451static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000452static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
453static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
454static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
455static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
456static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
457static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
458static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
459static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
460static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
461static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
462static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
463static void f_getline __ARGS((typeval *argvars, typeval *rettv));
464static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
465static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
466static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
467static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
468static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
469static void f_glob __ARGS((typeval *argvars, typeval *rettv));
470static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
471static void f_has __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000472static void f_has_key __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000473static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
474static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
475static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
476static void f_histget __ARGS((typeval *argvars, typeval *rettv));
477static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000478static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000479static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000480static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
481static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
482static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000483static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000484static void f_input __ARGS((typeval *argvars, typeval *rettv));
485static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
486static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
487static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
488static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000489static void f_insert __ARGS((typeval *argvars, typeval *rettv));
490static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000491static void f_items __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000492static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000493static void f_keys __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000494static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
495static void f_len __ARGS((typeval *argvars, typeval *rettv));
496static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
497static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000498static void f_line __ARGS((typeval *argvars, typeval *rettv));
499static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
500static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
501static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000502static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000503static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
504static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000505static void f_match __ARGS((typeval *argvars, typeval *rettv));
506static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
507static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000508static void f_max __ARGS((typeval *argvars, typeval *rettv));
509static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000510static void f_mode __ARGS((typeval *argvars, typeval *rettv));
511static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
512static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
513static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000514static void f_range __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000515static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
516static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
517static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
518static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
519static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000520static void f_remove __ARGS((typeval *argvars, typeval *rettv));
521static void f_rename __ARGS((typeval *argvars, typeval *rettv));
522static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
523static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
524static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
525static void f_search __ARGS((typeval *argvars, typeval *rettv));
526static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000527static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
528static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000529static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
530static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000531static void f_setline __ARGS((typeval *argvars, typeval *rettv));
532static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000533static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000534static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000535static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000536static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000537#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000538static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000539#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000540static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
541static void f_string __ARGS((typeval *argvars, typeval *rettv));
542static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
543static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
544static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
545static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000546static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
547static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000548static void f_synID __ARGS((typeval *argvars, typeval *rettv));
549static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
550static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
551static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000552static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
553static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
554static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
555static void f_tr __ARGS((typeval *argvars, typeval *rettv));
556static void f_type __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000557static void f_values __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000558static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
559static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
560static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
561static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
562static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
563static void f_winline __ARGS((typeval *argvars, typeval *rettv));
564static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
565static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
566static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000567
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000568static win_T *find_win_by_nr __ARGS((typeval *vp));
569static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570static int get_env_len __ARGS((char_u **arg));
571static int get_id_len __ARGS((char_u **arg));
572static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000573static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574static int eval_isnamec __ARGS((int c));
575static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000576static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
577static typeval *alloc_tv __ARGS((void));
578static typeval *alloc_string_tv __ARGS((char_u *string));
579static void free_tv __ARGS((typeval *varp));
580static void clear_tv __ARGS((typeval *varp));
581static void init_tv __ARGS((typeval *varp));
582static long get_tv_number __ARGS((typeval *varp));
583static linenr_T get_tv_lnum __ARGS((typeval *argvars));
584static char_u *get_tv_string __ARGS((typeval *varp));
585static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586static VAR find_var __ARGS((char_u *name, int writing));
587static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
588static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000589static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590static void list_one_var __ARGS((VAR v, char_u *prefix));
591static void list_vim_var __ARGS((int i));
592static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000593static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000595static void item_copy __ARGS((typeval *from, typeval *to, int deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
597static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
598static int eval_fname_script __ARGS((char_u *p));
599static int eval_fname_sid __ARGS((char_u *p));
600static void list_func_head __ARGS((ufunc_T *fp, int indent));
601static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
602static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000603static int function_exists __ARGS((char_u *name));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000604static void call_user_func __ARGS((ufunc_T *fp, int argcount, typeval *argvars, typeval *rettv, linenr_T firstline, linenr_T lastline, dictvar *selfdict));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000605
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000606#define get_var_string(p) get_tv_string(&(p)->tv)
607#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
608#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000611
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000612static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
613static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
614static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000615static void list_all_vars __ARGS((void));
616static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
617static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000618static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000619static char_u *set_var_idx __ARGS((char_u *name, char_u *ip, typeval *rettv, int copy, char_u *endchars));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000620static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000621static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
622static void list_fix_watch __ARGS((listvar *l, listitem *item));
623static int do_unlet_var __ARGS((char_u *name, int forceit));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625/*
626 * Set an internal variable to a string value. Creates the variable if it does
627 * not already exist.
628 */
629 void
630set_internal_string_var(name, value)
631 char_u *name;
632 char_u *value;
633{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000634 char_u *val;
635 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 val = vim_strsave(value);
638 if (val != NULL)
639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000640 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000641 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000643 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000644 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
646 }
647}
648
649# if defined(FEAT_MBYTE) || defined(PROTO)
650 int
651eval_charconvert(enc_from, enc_to, fname_from, fname_to)
652 char_u *enc_from;
653 char_u *enc_to;
654 char_u *fname_from;
655 char_u *fname_to;
656{
657 int err = FALSE;
658
659 set_vim_var_string(VV_CC_FROM, enc_from, -1);
660 set_vim_var_string(VV_CC_TO, enc_to, -1);
661 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
662 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
663 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
664 err = TRUE;
665 set_vim_var_string(VV_CC_FROM, NULL, -1);
666 set_vim_var_string(VV_CC_TO, NULL, -1);
667 set_vim_var_string(VV_FNAME_IN, NULL, -1);
668 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
669
670 if (err)
671 return FAIL;
672 return OK;
673}
674# endif
675
676# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
677 int
678eval_printexpr(fname, args)
679 char_u *fname;
680 char_u *args;
681{
682 int err = FALSE;
683
684 set_vim_var_string(VV_FNAME_IN, fname, -1);
685 set_vim_var_string(VV_CMDARG, args, -1);
686 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
687 err = TRUE;
688 set_vim_var_string(VV_FNAME_IN, NULL, -1);
689 set_vim_var_string(VV_CMDARG, NULL, -1);
690
691 if (err)
692 {
693 mch_remove(fname);
694 return FAIL;
695 }
696 return OK;
697}
698# endif
699
700# if defined(FEAT_DIFF) || defined(PROTO)
701 void
702eval_diff(origfile, newfile, outfile)
703 char_u *origfile;
704 char_u *newfile;
705 char_u *outfile;
706{
707 int err = FALSE;
708
709 set_vim_var_string(VV_FNAME_IN, origfile, -1);
710 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
711 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
712 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
713 set_vim_var_string(VV_FNAME_IN, NULL, -1);
714 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
715 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
716}
717
718 void
719eval_patch(origfile, difffile, outfile)
720 char_u *origfile;
721 char_u *difffile;
722 char_u *outfile;
723{
724 int err;
725
726 set_vim_var_string(VV_FNAME_IN, origfile, -1);
727 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
728 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
729 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
730 set_vim_var_string(VV_FNAME_IN, NULL, -1);
731 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
732 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
733}
734# endif
735
736/*
737 * Top level evaluation function, returning a boolean.
738 * Sets "error" to TRUE if there was an error.
739 * Return TRUE or FALSE.
740 */
741 int
742eval_to_bool(arg, error, nextcmd, skip)
743 char_u *arg;
744 int *error;
745 char_u **nextcmd;
746 int skip; /* only parse, don't execute */
747{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000748 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 int retval = FALSE;
750
751 if (skip)
752 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000753 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 else
756 {
757 *error = FALSE;
758 if (!skip)
759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000760 retval = (get_tv_number(&tv) != 0);
761 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763 }
764 if (skip)
765 --emsg_skip;
766
767 return retval;
768}
769
770/*
771 * Top level evaluation function, returning a string. If "skip" is TRUE,
772 * only parsing to "nextcmd" is done, without reporting errors. Return
773 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
774 */
775 char_u *
776eval_to_string_skip(arg, nextcmd, skip)
777 char_u *arg;
778 char_u **nextcmd;
779 int skip; /* only parse, don't execute */
780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 char_u *retval;
783
784 if (skip)
785 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000786 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 retval = NULL;
788 else
789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000790 retval = vim_strsave(get_tv_string(&tv));
791 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 if (skip)
794 --emsg_skip;
795
796 return retval;
797}
798
799/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800 * Skip over an expression at "*pp".
801 * Return FAIL for an error, OK otherwise.
802 */
803 int
804skip_expr(pp)
805 char_u **pp;
806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000808
809 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000810 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000811}
812
813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 * Top level evaluation function, returning a string.
815 * Return pointer to allocated memory, or NULL for failure.
816 */
817 char_u *
818eval_to_string(arg, nextcmd)
819 char_u *arg;
820 char_u **nextcmd;
821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 char_u *retval;
824
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 retval = NULL;
827 else
828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 retval = vim_strsave(get_tv_string(&tv));
830 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832
833 return retval;
834}
835
836/*
837 * Call eval_to_string() with "sandbox" set and not using local variables.
838 */
839 char_u *
840eval_to_string_safe(arg, nextcmd)
841 char_u *arg;
842 char_u **nextcmd;
843{
844 char_u *retval;
845 void *save_funccalp;
846
847 save_funccalp = save_funccal();
848 ++sandbox;
849 retval = eval_to_string(arg, nextcmd);
850 --sandbox;
851 restore_funccal(save_funccalp);
852 return retval;
853}
854
855#if 0 /* not used */
856/*
857 * Top level evaluation function, returning a string.
858 * Advances "arg" to the first non-blank after the evaluated expression.
859 * Return pointer to allocated memory, or NULL for failure.
860 * Doesn't give error messages.
861 */
862 char_u *
863eval_arg_to_string(arg)
864 char_u **arg;
865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000866 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 char_u *retval;
868 int ret;
869
870 ++emsg_off;
871
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (ret == FAIL)
874 retval = NULL;
875 else
876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000877 retval = vim_strsave(get_tv_string(&rettv));
878 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880
881 --emsg_off;
882
883 return retval;
884}
885#endif
886
887/*
888 * Top level evaluation function, returning a number.
889 * Evaluates "expr" silently.
890 * Returns -1 for an error.
891 */
892 int
893eval_to_number(expr)
894 char_u *expr;
895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000898 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900 ++emsg_off;
901
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = -1;
904 else
905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000906 retval = get_tv_number(&rettv);
907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909 --emsg_off;
910
911 return retval;
912}
913
914#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
915/*
916 * Call some vimL function and return the result as a string
917 * Uses argv[argc] for the function arguments.
918 */
919 char_u *
920call_vim_function(func, argc, argv, safe)
921 char_u *func;
922 int argc;
923 char_u **argv;
924 int safe; /* use the sandbox */
925{
926 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000927 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000928 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 long n;
930 int len;
931 int i;
932 int doesrange;
933 void *save_funccalp = NULL;
934
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000935 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (argvars == NULL)
937 return NULL;
938
939 for (i = 0; i < argc; i++)
940 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000941 /* Pass a NULL or empty argument as an empty string */
942 if (argv[i] == NULL || *argv[i] == NUL)
943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000944 argvars[i].v_type = VAR_STRING;
945 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000946 continue;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 /* Recognize a number argument, the others must be strings. */
950 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
951 if (len != 0 && len == (int)STRLEN(argv[i]))
952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000953 argvars[i].v_type = VAR_NUMBER;
954 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 else
957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000958 argvars[i].v_type = VAR_STRING;
959 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961 }
962
963 if (safe)
964 {
965 save_funccalp = save_funccal();
966 ++sandbox;
967 }
968
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000969 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
970 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +0000972 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 vim_free(argvars);
977
978 if (safe)
979 {
980 --sandbox;
981 restore_funccal(save_funccalp);
982 }
983 return retval;
984}
985#endif
986
987/*
988 * Save the current function call pointer, and set it to NULL.
989 * Used when executing autocommands and for ":source".
990 */
991 void *
992save_funccal()
993{
994 struct funccall *fc;
995
996 fc = current_funccal;
997 current_funccal = NULL;
998 return (void *)fc;
999}
1000
1001 void
1002restore_funccal(fc)
1003 void *fc;
1004{
1005 current_funccal = (struct funccall *)fc;
1006}
1007
1008#ifdef FEAT_FOLDING
1009/*
1010 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1011 * it in "*cp". Doesn't give error messages.
1012 */
1013 int
1014eval_foldexpr(arg, cp)
1015 char_u *arg;
1016 int *cp;
1017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 int retval;
1020 char_u *s;
1021
1022 ++emsg_off;
1023 ++sandbox;
1024 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001025 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 retval = 0;
1027 else
1028 {
1029 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030 if (tv.v_type == VAR_NUMBER)
1031 retval = tv.vval.v_number;
1032 else if (tv.v_type == VAR_UNKNOWN
1033 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 retval = 0;
1035 else
1036 {
1037 /* If the result is a string, check if there is a non-digit before
1038 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (!VIM_ISDIGIT(*s) && *s != '-')
1041 *cp = *s++;
1042 retval = atol((char *)s);
1043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001044 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
1046 --emsg_off;
1047 --sandbox;
1048
1049 return retval;
1050}
1051#endif
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053/*
1054 * Expands out the 'magic' {}'s in a variable/function name.
1055 * Note that this can call itself recursively, to deal with
1056 * constructs like foo{bar}{baz}{bam}
1057 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1058 * "in_start" ^
1059 * "expr_start" ^
1060 * "expr_end" ^
1061 * "in_end" ^
1062 *
1063 * Returns a new allocated string, which the caller must free.
1064 * Returns NULL for failure.
1065 */
1066 static char_u *
1067make_expanded_name(in_start, expr_start, expr_end, in_end)
1068 char_u *in_start;
1069 char_u *expr_start;
1070 char_u *expr_end;
1071 char_u *in_end;
1072{
1073 char_u c1;
1074 char_u *retval = NULL;
1075 char_u *temp_result;
1076 char_u *nextcmd = NULL;
1077
1078 if (expr_end == NULL || in_end == NULL)
1079 return NULL;
1080 *expr_start = NUL;
1081 *expr_end = NUL;
1082 c1 = *in_end;
1083 *in_end = NUL;
1084
1085 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1086 if (temp_result != NULL && nextcmd == NULL)
1087 {
1088 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1089 + (in_end - expr_end) + 1));
1090
1091 if (retval != NULL)
1092 {
1093 STRCPY(retval, in_start);
1094 STRCAT(retval, temp_result);
1095 STRCAT(retval, expr_end + 1);
1096 }
1097 }
1098 vim_free(temp_result);
1099
1100 *in_end = c1; /* put char back for error messages */
1101 *expr_start = '{';
1102 *expr_end = '}';
1103
1104 if (retval != NULL)
1105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001106 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (expr_start != NULL)
1108 {
1109 /* Further expansion! */
1110 temp_result = make_expanded_name(retval, expr_start,
1111 expr_end, temp_result);
1112 vim_free(retval);
1113 retval = temp_result;
1114 }
1115 }
1116
1117 return retval;
1118
1119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 * ":let" list all variable values
1123 * ":let var1 var2" list variable values
1124 * ":let var = expr" assignment command.
1125 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
1127 void
1128ex_let(eap)
1129 exarg_T *eap;
1130{
1131 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 char_u *expr = NULL;
1133 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 int var_count = 0;
1136 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001138 expr = skip_var_list(arg, &var_count, &semicolon);
1139 if (expr == NULL)
1140 return;
1141 expr = vim_strchr(expr, '=');
1142 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001144 if (*arg == '[')
1145 EMSG(_(e_invarg));
1146 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 /* ":let var1 var2" */
1148 arg = list_arg_vars(eap, arg);
1149 else if (!eap->skip)
1150 /* ":let" */
1151 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 eap->nextcmd = check_nextcmd(arg);
1153 }
1154 else
1155 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001156 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (eap->skip)
1159 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001160 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (eap->skip)
1162 {
1163 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 --emsg_skip;
1166 }
1167 else if (i != FAIL)
1168 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001169 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1170 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 }
1174}
1175
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001176/*
1177 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1178 * Handles both "var" with any type and "[var, var; var]" with a list type.
1179 * Returns OK or FAIL;
1180 */
1181 static int
1182ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1183 char_u *arg_start;
1184 typeval *tv;
1185 int copy; /* copy values from "tv", don't move */
1186 int semicolon; /* from skip_var_list() */
1187 int var_count; /* from skip_var_list() */
1188 char_u *nextchars; /* characters that must follow or NULL */
1189{
1190 char_u *arg = arg_start;
1191 listvar *l;
1192 int i;
1193 listitem *item;
1194 typeval ltv;
1195
1196 if (*arg != '[')
1197 {
1198 /*
1199 * ":let var = expr" or ":for var in list"
1200 */
1201 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1202 return FAIL;
1203 return OK;
1204 }
1205
1206 /*
1207 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1208 */
1209 l = tv->vval.v_list;
1210 if (tv->v_type != VAR_LIST || l == NULL)
1211 {
1212 EMSG(_(e_listreq));
1213 return FAIL;
1214 }
1215
1216 i = list_len(l);
1217 if (semicolon == 0 && var_count < i)
1218 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001219 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001220 return FAIL;
1221 }
1222 if (var_count - semicolon > i)
1223 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001224 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001225 return FAIL;
1226 }
1227
1228 item = l->lv_first;
1229 while (*arg != ']')
1230 {
1231 arg = skipwhite(arg + 1);
1232 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1233 item = item->li_next;
1234 if (arg == NULL)
1235 return FAIL;
1236
1237 arg = skipwhite(arg);
1238 if (*arg == ';')
1239 {
1240 /* Put the rest of the list (may be empty) in the var after ';'.
1241 * Create a new list for this. */
1242 l = list_alloc();
1243 if (l == NULL)
1244 return FAIL;
1245 while (item != NULL)
1246 {
1247 list_append_tv(l, &item->li_tv);
1248 item = item->li_next;
1249 }
1250
1251 ltv.v_type = VAR_LIST;
1252 ltv.vval.v_list = l;
1253 l->lv_refcount = 1;
1254
1255 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1256 clear_tv(&ltv);
1257 if (arg == NULL)
1258 return FAIL;
1259 break;
1260 }
1261 else if (*arg != ',' && *arg != ']')
1262 {
1263 EMSG2(_(e_intern2), "ex_let_vars()");
1264 return FAIL;
1265 }
1266 }
1267
1268 return OK;
1269}
1270
1271/*
1272 * Skip over assignable variable "var" or list of variables "[var, var]".
1273 * Used for ":let varvar = expr" and ":for varvar in expr".
1274 * For "[var, var]" increment "*var_count" for each variable.
1275 * for "[var, var; var]" set "semicolon".
1276 * Return NULL for an error.
1277 */
1278 static char_u *
1279skip_var_list(arg, var_count, semicolon)
1280 char_u *arg;
1281 int *var_count;
1282 int *semicolon;
1283{
1284 char_u *p, *s;
1285
1286 if (*arg == '[')
1287 {
1288 /* "[var, var]": find the matching ']'. */
1289 p = arg;
1290 while (1)
1291 {
1292 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1293 s = skip_var_one(p);
1294 if (s == p)
1295 {
1296 EMSG2(_(e_invarg2), p);
1297 return NULL;
1298 }
1299 ++*var_count;
1300
1301 p = skipwhite(s);
1302 if (*p == ']')
1303 break;
1304 else if (*p == ';')
1305 {
1306 if (*semicolon == 1)
1307 {
1308 EMSG(_("Double ; in list of variables"));
1309 return NULL;
1310 }
1311 *semicolon = 1;
1312 }
1313 else if (*p != ',')
1314 {
1315 EMSG2(_(e_invarg2), p);
1316 return NULL;
1317 }
1318 }
1319 return p + 1;
1320 }
1321 else
1322 return skip_var_one(arg);
1323}
1324
1325 static char_u *
1326skip_var_one(arg)
1327 char_u *arg;
1328{
1329 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1330 ++arg;
1331 return find_name_end(arg, NULL, NULL, TRUE);
1332}
1333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 static void
1335list_all_vars()
1336{
1337 int i;
1338
1339 /*
1340 * List all variables.
1341 */
1342 for (i = 0; i < variables.ga_len && !got_int; ++i)
1343 if (VAR_ENTRY(i).v_name != NULL)
1344 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1345 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1346 if (BVAR_ENTRY(i).v_name != NULL)
1347 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1348 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1349 if (WVAR_ENTRY(i).v_name != NULL)
1350 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1351 for (i = 0; i < VV_LEN && !got_int; ++i)
Bram Moolenaare9a41262005-01-15 22:18:47 +00001352 if (vimvars[i].tv.v_type == VAR_NUMBER || vimvars[i].vv_str != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 list_vim_var(i);
1354}
1355
1356/*
1357 * List variables in "arg".
1358 */
1359 static char_u *
1360list_arg_vars(eap, arg)
1361 exarg_T *eap;
1362 char_u *arg;
1363{
1364 int error = FALSE;
1365 char_u *temp_string = NULL;
1366 int arg_len;
1367 char_u *expr_start;
1368 char_u *expr_end;
1369 char_u *name_end;
1370 int c1 = 0, c2;
1371 int i;
1372 VAR varp;
1373 char_u *name;
1374
1375 while (!ends_excmd(*arg) && !got_int)
1376 {
1377 /* Find the end of the name. */
1378 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1379
1380 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1381 {
1382 emsg_severe = TRUE;
1383 EMSG(_(e_trailing));
1384 break;
1385 }
1386 if (!error && !eap->skip)
1387 {
1388 if (expr_start != NULL)
1389 {
1390 temp_string = make_expanded_name(arg, expr_start,
1391 expr_end, name_end);
1392 if (temp_string == NULL)
1393 {
1394 /*
1395 * Report an invalid expression in braces, unless
1396 * the expression evaluation has been cancelled due
1397 * to an aborting error, an interrupt, or an
1398 * exception.
1399 */
1400 if (!aborting())
1401 {
1402 emsg_severe = TRUE;
1403 EMSG2(_(e_invarg2), arg);
1404 break;
1405 }
1406 error = TRUE;
1407 arg = skipwhite(name_end);
1408 continue;
1409 }
1410 arg = temp_string;
1411 arg_len = STRLEN(temp_string);
1412 }
1413 else
1414 {
1415 c1 = *name_end;
1416 *name_end = NUL;
1417 arg_len = (int)(name_end - arg);
1418 }
1419 i = find_vim_var(arg, arg_len);
1420 if (i >= 0)
1421 list_vim_var(i);
1422 else if (STRCMP("b:changedtick", arg) == 0)
1423 {
1424 char_u numbuf[NUMBUFLEN];
1425
1426 sprintf((char *)numbuf, "%ld",
1427 (long)curbuf->b_changedtick);
1428 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1429 VAR_NUMBER, numbuf);
1430 }
1431 else
1432 {
1433 varp = find_var(arg, FALSE);
1434 if (varp == NULL)
1435 {
1436 /* Skip further arguments but do continue to
1437 * search for a trailing command. */
1438 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1439 error = TRUE;
1440 }
1441 else
1442 {
1443 name = vim_strchr(arg, ':');
1444 if (name != NULL)
1445 {
1446 /* "a:" vars have no name stored, use whole arg */
1447 if (arg[0] == 'a' && arg[1] == ':')
1448 c2 = NUL;
1449 else
1450 {
1451 c2 = *++name;
1452 *name = NUL;
1453 }
1454 list_one_var(varp, arg);
1455 if (c2 != NUL)
1456 *name = c2;
1457 }
1458 else
1459 list_one_var(varp, (char_u *)"");
1460 }
1461 }
1462 if (expr_start != NULL)
1463 vim_free(temp_string);
1464 else
1465 *name_end = c1;
1466 }
1467 arg = skipwhite(name_end);
1468 }
1469
1470 return arg;
1471}
1472
1473/*
1474 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1475 * Returns a pointer to the char just after the var name.
1476 * Returns NULL if there is an error.
1477 */
1478 static char_u *
1479ex_let_one(arg, tv, copy, endchars)
1480 char_u *arg; /* points to variable name */
1481 typeval *tv; /* value to assign to variable */
1482 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484{
1485 int c1;
1486 char_u *name;
1487 char_u *p;
1488 char_u *arg_end = NULL;
1489 int len;
1490 int opt_flags;
1491
1492 /*
1493 * ":let $VAR = expr": Set environment variable.
1494 */
1495 if (*arg == '$')
1496 {
1497 /* Find the end of the name. */
1498 ++arg;
1499 name = arg;
1500 len = get_env_len(&arg);
1501 if (len == 0)
1502 EMSG2(_(e_invarg2), name - 1);
1503 else
1504 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505 if (endchars != NULL
1506 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507 EMSG(_(e_letunexp));
1508 else
1509 {
1510 c1 = name[len];
1511 name[len] = NUL;
1512 p = get_tv_string(tv);
1513 vim_setenv(name, p);
1514 if (STRICMP(name, "HOME") == 0)
1515 init_homedir();
1516 else if (didset_vim && STRICMP(name, "VIM") == 0)
1517 didset_vim = FALSE;
1518 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1519 didset_vimruntime = FALSE;
1520 name[len] = c1;
1521 arg_end = arg;
1522 }
1523 }
1524 }
1525
1526 /*
1527 * ":let &option = expr": Set option value.
1528 * ":let &l:option = expr": Set local option value.
1529 * ":let &g:option = expr": Set global option value.
1530 */
1531 else if (*arg == '&')
1532 {
1533 /* Find the end of the name. */
1534 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 if (p == NULL || (endchars != NULL
1536 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 EMSG(_(e_letunexp));
1538 else
1539 {
1540 c1 = *p;
1541 *p = NUL;
1542 set_option_value(arg, get_tv_number(tv),
1543 get_tv_string(tv), opt_flags);
1544 *p = c1;
1545 arg_end = p;
1546 }
1547 }
1548
1549 /*
1550 * ":let @r = expr": Set register contents.
1551 */
1552 else if (*arg == '@')
1553 {
1554 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 if (endchars != NULL
1556 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 EMSG(_(e_letunexp));
1558 else
1559 {
1560 write_reg_contents(*arg == '@' ? '"' : *arg,
1561 get_tv_string(tv), -1, FALSE);
1562 arg_end = arg + 1;
1563 }
1564 }
1565
1566 /*
1567 * ":let var = expr": Set internal variable.
1568 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001569 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 {
1571 char_u *exp_name = NULL;
1572 char_u *expr_start, *expr_end;
1573
1574 /* Find the end of the name. */
1575 p = find_name_end(arg, &expr_start, &expr_end, FALSE);
1576 if (expr_start != NULL)
1577 {
1578 exp_name = make_expanded_name(arg, expr_start, expr_end, p);
1579 arg = exp_name;
1580 }
1581
1582 if (arg == NULL)
1583 {
1584 /* Report an invalid expression in braces, unless the
1585 * expression evaluation has been cancelled due to an
1586 * aborting error, an interrupt, or an exception. */
1587 if (!aborting())
1588 EMSG2(_(e_invarg2), arg);
1589 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001590 else if (*p == '[' || *p == '.')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 arg_end = set_var_idx(arg, p, tv, copy, endchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592 else if (endchars != NULL
1593 && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 EMSG(_(e_letunexp));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001595 else if (!check_changedtick(arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596 {
1597 c1 = *p;
1598 *p = NUL;
1599 set_var(arg, tv, copy);
1600 *p = c1;
1601 arg_end = p;
1602 }
1603
1604 vim_free(exp_name);
1605 }
1606
1607 else
1608 EMSG2(_(e_invarg2), arg);
1609
1610 return arg_end;
1611}
1612
1613/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001614 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1615 */
1616 static int
1617check_changedtick(arg)
1618 char_u *arg;
1619{
1620 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1621 {
1622 EMSG2(_(e_readonlyvar), arg);
1623 return TRUE;
1624 }
1625 return FALSE;
1626}
1627
1628/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001629 * Set a variable with an index: "name[expr]", "name[expr:expr]",
Bram Moolenaar8c711452005-01-14 21:53:12 +00001630 * "name[expr][expr]", "name.key", "name.key[expr]" etc.
1631 * Only works if "name" is an existing List or Dictionary.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 * "ip" points to the first '['.
1633 * Returns a pointer to just after the last used ']'; NULL for error.
1634 */
1635 static char_u *
1636set_var_idx(name, ip, rettv, copy, endchars)
1637 char_u *name;
1638 char_u *ip;
1639 typeval *rettv;
1640 int copy;
1641 char_u *endchars;
1642{
1643 VAR v;
1644 int c1;
1645 char_u *p;
1646 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001647 typeval var2;
1648 int range = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 typeval *tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001650 long n1 = 0, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001651 int empty1 = FALSE, empty2 = FALSE;
1652 listitem *li = NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001653 listitem *ni;
1654 listitem *ri;
1655 listvar *l = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001656 dictitem *di;
1657 char_u *key = NULL;
1658 char_u *newkey = NULL;
1659 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660
1661 c1 = *ip;
1662 *ip = NUL;
1663 v = find_var(name, TRUE);
1664 if (v == NULL)
1665 EMSG2(_(e_undefvar), name);
1666 *ip = c1;
1667 if (v == NULL)
1668 return NULL;
1669
1670 tv = &v->tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001671 for (p = ip; *p == '[' || (*p == '.' && tv->v_type == VAR_DICT); )
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001673 if (!(tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
1674 && !(tv->v_type == VAR_DICT && tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001676 EMSG(_("E689: Can only index a List or Dictionary"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 p = NULL;
1678 break;
1679 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001680 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001682 EMSG(_("E708: [:] must come last"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 p = NULL;
1684 break;
1685 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001686
Bram Moolenaar8c711452005-01-14 21:53:12 +00001687 len = -1;
1688 if (*p == '.')
1689 {
1690 key = p + 1;
1691 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1692 ;
1693 if (len == 0)
1694 {
1695 EMSG(_(e_emptykey));
1696 p = NULL;
1697 break;
1698 }
1699 p = key + len;
1700 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001701 else
1702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001703 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001704 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001705 if (*p == ':')
1706 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001707 else
1708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001709 empty1 = FALSE;
1710 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001711 {
1712 p = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001713 break;
1714 }
1715 }
1716
1717 /* Optionally get the second index [ :expr]. */
1718 if (*p == ':')
1719 {
1720 if (tv->v_type == VAR_DICT)
1721 {
1722 EMSG(_("E999: Cannot use [:] with a Dictionary"));
1723 p = NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001724 if (!empty1)
1725 clear_tv(&var1);
1726 break;
1727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001728 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL)
1729 {
1730 EMSG(_("E709: [:] requires a List value"));
1731 p = NULL;
1732 if (!empty1)
1733 clear_tv(&var1);
1734 break;
1735 }
1736 p = skipwhite(p + 1);
1737 if (*p == ']')
1738 empty2 = TRUE;
1739 else
1740 {
1741 empty2 = FALSE;
1742 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1743 {
1744 p = NULL;
1745 if (!empty1)
1746 clear_tv(&var1);
1747 break;
1748 }
1749 }
1750 range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001752 else
1753 range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001754
Bram Moolenaar8c711452005-01-14 21:53:12 +00001755 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001757 EMSG(_(e_missbrac));
1758 if (!empty1)
1759 clear_tv(&var1);
1760 if (range && !empty2)
1761 clear_tv(&var2);
1762 p = NULL;
1763 break;
1764 }
1765
1766 /* Skip to past ']'. */
1767 ++p;
1768 }
1769
1770 if (tv->v_type == VAR_DICT)
1771 {
1772 if (len == -1)
1773 {
1774 key = get_tv_string(&var1);
1775 if (*key == NUL)
1776 {
1777 EMSG(_(e_emptykey));
1778 clear_tv(&var1);
1779 p = NULL;
1780 break;
1781 }
1782 }
1783 di = dict_find(tv->vval.v_dict, key, len);
1784 if (di == NULL)
1785 {
1786 /* Key does not exist in dict: may need toadd it. */
1787 if (*p == '[' || *p == '.')
1788 {
1789 EMSG2(_("E999: Key does not exist in Dictionary: %s"), key);
1790 p = NULL;
1791 if (len == -1)
1792 clear_tv(&var1);
1793 break;
1794 }
1795 if (len == -1)
1796 newkey = vim_strsave(key);
1797 else
1798 newkey = vim_strnsave(key, len);
1799 if (len == -1)
1800 clear_tv(&var1);
1801 if (newkey == NULL)
1802 p = NULL;
1803 break;
1804 }
1805 if (len == -1)
1806 clear_tv(&var1);
1807 tv = &di->di_tv;
1808 }
1809 else
1810 {
1811 /*
1812 * Get the number and item for the only or first index of the List.
1813 */
1814 if (empty1)
1815 n1 = 0;
1816 else
1817 {
1818 n1 = get_tv_number(&var1);
1819 clear_tv(&var1);
1820 }
1821 l = tv->vval.v_list;
1822 li = list_find(l, n1);
1823 if (li == NULL)
1824 {
1825 EMSGN(_(e_listidx), n1);
1826 p = NULL;
1827 if (range && !empty2)
1828 clear_tv(&var2);
1829 break;
1830 }
1831
1832 /*
1833 * May need to find the item or absolute index for the second
1834 * index of a range.
1835 * When no index given: "empty2" is TRUE.
1836 * Otherwise "n2" is set to the second index.
1837 */
1838 if (range && !empty2)
1839 {
1840 n2 = get_tv_number(&var2);
1841 clear_tv(&var2);
1842 if (n2 < 0)
1843 {
1844 ni = list_find(l, n2);
1845 if (ni == NULL)
1846 {
1847 EMSGN(_(e_listidx), n2);
1848 p = NULL;
1849 break;
1850 }
1851 n2 = list_idx_of_item(l, ni);
1852 }
1853
1854 /* Check that n2 isn't before n1. */
1855 if (n1 < 0)
1856 n1 = list_idx_of_item(l, li);
1857 if (n2 < n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001858 {
1859 EMSGN(_(e_listidx), n2);
1860 p = NULL;
1861 break;
1862 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001863 }
1864
Bram Moolenaar8c711452005-01-14 21:53:12 +00001865 tv = &li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 }
1868
1869 if (p != NULL)
1870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001871 p = skipwhite(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 {
1874 EMSG(_(e_letunexp));
1875 p = NULL;
1876 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001877 else if (range)
1878 {
1879 /*
1880 * Assign the List values to the list items.
1881 */
1882 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001884 clear_tv(&li->li_tv);
1885 copy_tv(&ri->li_tv, &li->li_tv);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001886 ri = ri->li_next;
1887 if (ri == NULL || (!empty2 && n2 == n1))
1888 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001889 if (li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001890 {
1891 /* Need to add an empty item. */
1892 ni = listitem_alloc();
1893 if (ni == NULL)
1894 {
1895 ri = NULL;
1896 break;
1897 }
1898 ni->li_tv.v_type = VAR_NUMBER;
1899 ni->li_tv.vval.v_number = 0;
1900 list_append(l, ni);
1901 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001902 li = li->li_next;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001903 ++n1;
1904 }
1905 if (ri != NULL)
1906 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001907 else if (empty2 ? li != NULL && li->li_next != NULL : n1 != n2)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001908 EMSG(_("E711: List value has not enough items"));
1909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 else
1911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001912 if (newkey != NULL)
1913 {
1914 /* Need to add the item to the dictionary. */
1915 di = dictitem_alloc();
1916 if (di == NULL)
1917 p = NULL;
1918 else
1919 {
1920 di->di_key = newkey;
1921 newkey = NULL;
1922 dict_add(tv->vval.v_dict, di);
1923 tv = &di->di_tv;
1924 }
1925 }
1926 else
1927 clear_tv(tv);
1928
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001929 /*
1930 * Assign the value to the variable or list item.
1931 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001932 if (p != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001934 if (copy)
1935 copy_tv(rettv, tv);
1936 else
1937 {
1938 *tv = *rettv;
1939 init_tv(rettv);
1940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 }
1942 }
1943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 vim_free(newkey);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 return p;
1946}
1947
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948/*
1949 * Add a watcher to a list.
1950 */
1951 static void
1952list_add_watch(l, lw)
1953 listvar *l;
1954 listwatch *lw;
1955{
1956 lw->lw_next = l->lv_watch;
1957 l->lv_watch = lw;
1958}
1959
1960/*
1961 * Remove a watches from a list.
1962 * No warning when it isn't found...
1963 */
1964 static void
1965list_rem_watch(l, lwrem)
1966 listvar *l;
1967 listwatch *lwrem;
1968{
1969 listwatch *lw, **lwp;
1970
1971 lwp = &l->lv_watch;
1972 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1973 {
1974 if (lw == lwrem)
1975 {
1976 *lwp = lw->lw_next;
1977 break;
1978 }
1979 lwp = &lw->lw_next;
1980 }
1981}
1982
1983/*
1984 * Just before removing an item from a list: advance watchers to the next
1985 * item.
1986 */
1987 static void
1988list_fix_watch(l, item)
1989 listvar *l;
1990 listitem *item;
1991{
1992 listwatch *lw;
1993
1994 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1995 if (lw->lw_item == item)
1996 lw->lw_item = item->li_next;
1997}
1998
1999/*
2000 * Evaluate the expression used in a ":for var in expr" command.
2001 * "arg" points to "var".
2002 * Set "*errp" to TRUE for an error, FALSE otherwise;
2003 * Return a pointer that holds the info. Null when there is an error.
2004 */
2005 void *
2006eval_for_line(arg, errp, nextcmdp, skip)
2007 char_u *arg;
2008 int *errp;
2009 char_u **nextcmdp;
2010 int skip;
2011{
2012 forinfo *fi;
2013 char_u *expr;
2014 typeval tv;
2015 listvar *l;
2016
2017 *errp = TRUE; /* default: there is an error */
2018
2019 fi = (forinfo *)alloc_clear(sizeof(forinfo));
2020 if (fi == NULL)
2021 return NULL;
2022
2023 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2024 if (expr == NULL)
2025 return fi;
2026
2027 expr = skipwhite(expr);
2028 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2029 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002030 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return fi;
2032 }
2033
2034 if (skip)
2035 ++emsg_skip;
2036 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2037 {
2038 *errp = FALSE;
2039 if (!skip)
2040 {
2041 l = tv.vval.v_list;
2042 if (tv.v_type != VAR_LIST || l == NULL)
2043 EMSG(_(e_listreq));
2044 else
2045 {
2046 fi->fi_list = l;
2047 list_add_watch(l, &fi->fi_lw);
2048 fi->fi_lw.lw_item = l->lv_first;
2049 }
2050 }
2051 }
2052 if (skip)
2053 --emsg_skip;
2054
2055 return fi;
2056}
2057
2058/*
2059 * Use the first item in a ":for" list. Advance to the next.
2060 * Assign the values to the variable (list). "arg" points to the first one.
2061 * Return TRUE when a valid item was found, FALSE when at end of list or
2062 * something wrong.
2063 */
2064 int
2065next_for_item(fi_void, arg)
2066 void *fi_void;
2067 char_u *arg;
2068{
2069 forinfo *fi = (forinfo *)fi_void;
2070 int result;
2071 listitem *item;
2072
2073 item = fi->fi_lw.lw_item;
2074 if (item == NULL)
2075 result = FALSE;
2076 else
2077 {
2078 fi->fi_lw.lw_item = item->li_next;
2079 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2080 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2081 }
2082 return result;
2083}
2084
2085/*
2086 * Free the structure used to store info used by ":for".
2087 */
2088 void
2089free_for_info(fi_void)
2090 void *fi_void;
2091{
2092 forinfo *fi = (forinfo *)fi_void;
2093
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002094 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002095 list_rem_watch(fi->fi_list, &fi->fi_lw);
2096 vim_free(fi);
2097}
2098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2100
2101 void
2102set_context_for_expression(xp, arg, cmdidx)
2103 expand_T *xp;
2104 char_u *arg;
2105 cmdidx_T cmdidx;
2106{
2107 int got_eq = FALSE;
2108 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002109 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111 if (cmdidx == CMD_let)
2112 {
2113 xp->xp_context = EXPAND_USER_VARS;
2114 if (vim_strchr(arg, '=') == NULL)
2115 {
2116 /* ":let var1 var2 ...": find last space. */
2117 for (p = arg + STRLEN(arg); p > arg; )
2118 {
2119 xp->xp_pattern = p;
2120 p = mb_ptr_back(arg, p);
2121 if (vim_iswhite(*p))
2122 break;
2123 }
2124 return;
2125 }
2126 }
2127 else
2128 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2129 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 while ((xp->xp_pattern = vim_strpbrk(arg,
2131 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2132 {
2133 c = *xp->xp_pattern;
2134 if (c == '&')
2135 {
2136 c = xp->xp_pattern[1];
2137 if (c == '&')
2138 {
2139 ++xp->xp_pattern;
2140 xp->xp_context = cmdidx != CMD_let || got_eq
2141 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2142 }
2143 else if (c != ' ')
2144 xp->xp_context = EXPAND_SETTINGS;
2145 }
2146 else if (c == '$')
2147 {
2148 /* environment variable */
2149 xp->xp_context = EXPAND_ENV_VARS;
2150 }
2151 else if (c == '=')
2152 {
2153 got_eq = TRUE;
2154 xp->xp_context = EXPAND_EXPRESSION;
2155 }
2156 else if (c == '<'
2157 && xp->xp_context == EXPAND_FUNCTIONS
2158 && vim_strchr(xp->xp_pattern, '(') == NULL)
2159 {
2160 /* Function name can start with "<SNR>" */
2161 break;
2162 }
2163 else if (cmdidx != CMD_let || got_eq)
2164 {
2165 if (c == '"') /* string */
2166 {
2167 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2168 if (c == '\\' && xp->xp_pattern[1] != NUL)
2169 ++xp->xp_pattern;
2170 xp->xp_context = EXPAND_NOTHING;
2171 }
2172 else if (c == '\'') /* literal string */
2173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002174 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2176 /* skip */ ;
2177 xp->xp_context = EXPAND_NOTHING;
2178 }
2179 else if (c == '|')
2180 {
2181 if (xp->xp_pattern[1] == '|')
2182 {
2183 ++xp->xp_pattern;
2184 xp->xp_context = EXPAND_EXPRESSION;
2185 }
2186 else
2187 xp->xp_context = EXPAND_COMMANDS;
2188 }
2189 else
2190 xp->xp_context = EXPAND_EXPRESSION;
2191 }
2192 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002193 /* Doesn't look like something valid, expand as an expression
2194 * anyway. */
2195 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 arg = xp->xp_pattern;
2197 if (*arg != NUL)
2198 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2199 /* skip */ ;
2200 }
2201 xp->xp_pattern = arg;
2202}
2203
2204#endif /* FEAT_CMDL_COMPL */
2205
2206/*
2207 * ":1,25call func(arg1, arg2)" function call.
2208 */
2209 void
2210ex_call(eap)
2211 exarg_T *eap;
2212{
2213 char_u *arg = eap->arg;
2214 char_u *startarg;
2215 char_u *alias;
2216 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 int len;
2219 linenr_T lnum;
2220 int doesrange;
2221 int failed = FALSE;
2222
2223 name = arg;
2224 len = get_func_len(&arg, &alias, !eap->skip);
2225 if (len == 0)
2226 goto end;
2227 if (alias != NULL)
2228 name = alias;
2229
2230 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 if (*startarg != '(')
2234 {
2235 EMSG2(_("E107: Missing braces: %s"), name);
2236 goto end;
2237 }
2238
2239 /*
2240 * When skipping, evaluate the function once, to find the end of the
2241 * arguments.
2242 * When the function takes a range, this is discovered after the first
2243 * call, and the loop is broken.
2244 */
2245 if (eap->skip)
2246 {
2247 ++emsg_skip;
2248 lnum = eap->line2; /* do it once, also with an invalid range */
2249 }
2250 else
2251 lnum = eap->line1;
2252 for ( ; lnum <= eap->line2; ++lnum)
2253 {
2254 if (!eap->skip && eap->addr_count > 0)
2255 {
2256 curwin->w_cursor.lnum = lnum;
2257 curwin->w_cursor.col = 0;
2258 }
2259 arg = startarg;
Bram Moolenaare9a41262005-01-15 22:18:47 +00002260 if (get_func_tv(name, len, &rettv, &arg, eap->line1, eap->line2,
2261 &doesrange, !eap->skip, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
2263 failed = TRUE;
2264 break;
2265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (doesrange || eap->skip)
2268 break;
2269 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002270 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002272 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (aborting())
2274 break;
2275 }
2276 if (eap->skip)
2277 --emsg_skip;
2278
2279 if (!failed)
2280 {
2281 /* Check for trailing illegal characters and a following command. */
2282 if (!ends_excmd(*arg))
2283 {
2284 emsg_severe = TRUE;
2285 EMSG(_(e_trailing));
2286 }
2287 else
2288 eap->nextcmd = check_nextcmd(arg);
2289 }
2290
2291end:
2292 if (alias != NULL)
2293 vim_free(alias);
2294}
2295
2296/*
2297 * ":unlet[!] var1 ... " command.
2298 */
2299 void
2300ex_unlet(eap)
2301 exarg_T *eap;
2302{
2303 char_u *arg = eap->arg;
2304 char_u *name_end;
2305 char_u cc;
2306 char_u *expr_start;
2307 char_u *expr_end;
2308 int error = FALSE;
2309
2310 do
2311 {
2312 /* Find the end of the name. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002313 name_end = find_name_end(arg, &expr_start, &expr_end, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
2316 {
2317 emsg_severe = TRUE;
2318 EMSG(_(e_trailing));
2319 break;
2320 }
2321
2322 if (!error && !eap->skip)
2323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (expr_start != NULL)
2325 {
2326 char_u *temp_string;
2327
2328 temp_string = make_expanded_name(arg, expr_start,
2329 expr_end, name_end);
2330 if (temp_string == NULL)
2331 {
2332 /*
2333 * Report an invalid expression in braces, unless the
2334 * expression evaluation has been cancelled due to an
2335 * aborting error, an interrupt, or an exception.
2336 */
2337 if (!aborting())
2338 {
2339 emsg_severe = TRUE;
2340 EMSG2(_(e_invarg2), arg);
2341 break;
2342 }
2343 error = TRUE;
2344 }
2345 else
2346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 if (do_unlet_var(temp_string, eap->forceit) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 vim_free(temp_string);
2350 }
2351 }
2352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 cc = *name_end;
2355 *name_end = NUL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 if (do_unlet_var(arg, eap->forceit) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 *name_end = cc;
2359 }
2360 }
2361 arg = skipwhite(name_end);
2362 } while (!ends_excmd(*arg));
2363
2364 eap->nextcmd = check_nextcmd(arg);
2365}
2366
Bram Moolenaar8c711452005-01-14 21:53:12 +00002367 static int
2368do_unlet_var(name, forceit)
2369 char_u *name;
2370 int forceit;
2371{
2372 if (check_changedtick(name))
2373 return FAIL;
2374 if (do_unlet(name) == FAIL && !forceit)
2375 {
2376 EMSG2(_("E108: No such variable: \"%s\""), name);
2377 return FAIL;
2378 }
2379 return OK;
2380}
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382/*
2383 * "unlet" a variable. Return OK if it existed, FAIL if not.
2384 */
2385 int
2386do_unlet(name)
2387 char_u *name;
2388{
2389 VAR v;
2390
2391 v = find_var(name, TRUE);
2392 if (v != NULL)
2393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002394 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 return OK;
2396 }
2397 return FAIL;
2398}
2399
2400#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2401/*
2402 * Delete all "menutrans_" variables.
2403 */
2404 void
2405del_menutrans_vars()
2406{
2407 int i;
2408
2409 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002410 if (VAR_ENTRY(i).v_name != NULL
2411 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2412 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414#endif
2415
2416#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2417
2418/*
2419 * Local string buffer for the next two functions to store a variable name
2420 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2421 * get_user_var_name().
2422 */
2423
2424static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2425
2426static char_u *varnamebuf = NULL;
2427static int varnamebuflen = 0;
2428
2429/*
2430 * Function to concatenate a prefix and a variable name.
2431 */
2432 static char_u *
2433cat_prefix_varname(prefix, name)
2434 int prefix;
2435 char_u *name;
2436{
2437 int len;
2438
2439 len = (int)STRLEN(name) + 3;
2440 if (len > varnamebuflen)
2441 {
2442 vim_free(varnamebuf);
2443 len += 10; /* some additional space */
2444 varnamebuf = alloc(len);
2445 if (varnamebuf == NULL)
2446 {
2447 varnamebuflen = 0;
2448 return NULL;
2449 }
2450 varnamebuflen = len;
2451 }
2452 *varnamebuf = prefix;
2453 varnamebuf[1] = ':';
2454 STRCPY(varnamebuf + 2, name);
2455 return varnamebuf;
2456}
2457
2458/*
2459 * Function given to ExpandGeneric() to obtain the list of user defined
2460 * (global/buffer/window/built-in) variable names.
2461 */
2462/*ARGSUSED*/
2463 char_u *
2464get_user_var_name(xp, idx)
2465 expand_T *xp;
2466 int idx;
2467{
2468 static int gidx;
2469 static int bidx;
2470 static int widx;
2471 static int vidx;
2472 char_u *name;
2473
2474 if (idx == 0)
2475 gidx = bidx = widx = vidx = 0;
2476 if (gidx < variables.ga_len) /* Global variables */
2477 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002478 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 && gidx < variables.ga_len)
2480 /* skip */;
2481 if (name != NULL)
2482 {
2483 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2484 return cat_prefix_varname('g', name);
2485 else
2486 return name;
2487 }
2488 }
2489 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2490 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002491 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 && bidx < curbuf->b_vars.ga_len)
2493 /* skip */;
2494 if (name != NULL)
2495 return cat_prefix_varname('b', name);
2496 }
2497 if (bidx == curbuf->b_vars.ga_len)
2498 {
2499 ++bidx;
2500 return (char_u *)"b:changedtick";
2501 }
2502 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2503 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002504 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 && widx < curwin->w_vars.ga_len)
2506 /* skip */;
2507 if (name != NULL)
2508 return cat_prefix_varname('w', name);
2509 }
2510 if (vidx < VV_LEN) /* Built-in variables */
2511 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2512
2513 vim_free(varnamebuf);
2514 varnamebuf = NULL;
2515 varnamebuflen = 0;
2516 return NULL;
2517}
2518
2519#endif /* FEAT_CMDL_COMPL */
2520
2521/*
2522 * types for expressions.
2523 */
2524typedef enum
2525{
2526 TYPE_UNKNOWN = 0
2527 , TYPE_EQUAL /* == */
2528 , TYPE_NEQUAL /* != */
2529 , TYPE_GREATER /* > */
2530 , TYPE_GEQUAL /* >= */
2531 , TYPE_SMALLER /* < */
2532 , TYPE_SEQUAL /* <= */
2533 , TYPE_MATCH /* =~ */
2534 , TYPE_NOMATCH /* !~ */
2535} exptype_T;
2536
2537/*
2538 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2541 */
2542
2543/*
2544 * Handle zero level expression.
2545 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 * Return OK or FAIL.
2548 */
2549 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 char_u **nextcmd;
2554 int evaluate;
2555{
2556 int ret;
2557 char_u *p;
2558
2559 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (ret == FAIL || !ends_excmd(*p))
2562 {
2563 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /*
2566 * Report the invalid expression unless the expression evaluation has
2567 * been cancelled due to an aborting error, an interrupt, or an
2568 * exception.
2569 */
2570 if (!aborting())
2571 EMSG2(_(e_invexpr2), arg);
2572 ret = FAIL;
2573 }
2574 if (nextcmd != NULL)
2575 *nextcmd = check_nextcmd(p);
2576
2577 return ret;
2578}
2579
2580/*
2581 * Handle top level expression:
2582 * expr1 ? expr0 : expr0
2583 *
2584 * "arg" must point to the first non-white of the expression.
2585 * "arg" is advanced to the next non-white after the recognized expression.
2586 *
2587 * Return OK or FAIL.
2588 */
2589 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 int evaluate;
2594{
2595 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002596 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 /*
2599 * Get the first variable.
2600 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return FAIL;
2603
2604 if ((*arg)[0] == '?')
2605 {
2606 result = FALSE;
2607 if (evaluate)
2608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613
2614 /*
2615 * Get the second variable.
2616 */
2617 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 return FAIL;
2620
2621 /*
2622 * Check for the ":".
2623 */
2624 if ((*arg)[0] != ':')
2625 {
2626 EMSG(_("E109: Missing ':' after '?'"));
2627 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 return FAIL;
2630 }
2631
2632 /*
2633 * Get the third variable.
2634 */
2635 *arg = skipwhite(*arg + 1);
2636 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2637 {
2638 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 return FAIL;
2641 }
2642 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645
2646 return OK;
2647}
2648
2649/*
2650 * Handle first level expression:
2651 * expr2 || expr2 || expr2 logical OR
2652 *
2653 * "arg" must point to the first non-white of the expression.
2654 * "arg" is advanced to the next non-white after the recognized expression.
2655 *
2656 * Return OK or FAIL.
2657 */
2658 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int evaluate;
2663{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002664 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 long result;
2666 int first;
2667
2668 /*
2669 * Get the first variable.
2670 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 return FAIL;
2673
2674 /*
2675 * Repeat until there is no following "||".
2676 */
2677 first = TRUE;
2678 result = FALSE;
2679 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2680 {
2681 if (evaluate && first)
2682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 first = FALSE;
2687 }
2688
2689 /*
2690 * Get the second variable.
2691 */
2692 *arg = skipwhite(*arg + 2);
2693 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2694 return FAIL;
2695
2696 /*
2697 * Compute the result.
2698 */
2699 if (evaluate && !result)
2700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
2705 if (evaluate)
2706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 rettv->v_type = VAR_NUMBER;
2708 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 }
2711
2712 return OK;
2713}
2714
2715/*
2716 * Handle second level expression:
2717 * expr3 && expr3 && expr3 logical AND
2718 *
2719 * "arg" must point to the first non-white of the expression.
2720 * "arg" is advanced to the next non-white after the recognized expression.
2721 *
2722 * Return OK or FAIL.
2723 */
2724 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002725eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002727 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 int evaluate;
2729{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002730 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 long result;
2732 int first;
2733
2734 /*
2735 * Get the first variable.
2736 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002737 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return FAIL;
2739
2740 /*
2741 * Repeat until there is no following "&&".
2742 */
2743 first = TRUE;
2744 result = TRUE;
2745 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2746 {
2747 if (evaluate && first)
2748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002749 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 first = FALSE;
2753 }
2754
2755 /*
2756 * Get the second variable.
2757 */
2758 *arg = skipwhite(*arg + 2);
2759 if (eval4(arg, &var2, evaluate && result) == FAIL)
2760 return FAIL;
2761
2762 /*
2763 * Compute the result.
2764 */
2765 if (evaluate && result)
2766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002767 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 if (evaluate)
2772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773 rettv->v_type = VAR_NUMBER;
2774 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776 }
2777
2778 return OK;
2779}
2780
2781/*
2782 * Handle third level expression:
2783 * var1 == var2
2784 * var1 =~ var2
2785 * var1 != var2
2786 * var1 !~ var2
2787 * var1 > var2
2788 * var1 >= var2
2789 * var1 < var2
2790 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002791 * var1 is var2
2792 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 *
2794 * "arg" must point to the first non-white of the expression.
2795 * "arg" is advanced to the next non-white after the recognized expression.
2796 *
2797 * Return OK or FAIL.
2798 */
2799 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002802 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 int evaluate;
2804{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002805 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 char_u *p;
2807 int i;
2808 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002809 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int len = 2;
2811 long n1, n2;
2812 char_u *s1, *s2;
2813 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2814 regmatch_T regmatch;
2815 int ic;
2816 char_u *save_cpo;
2817
2818 /*
2819 * Get the first variable.
2820 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002821 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 return FAIL;
2823
2824 p = *arg;
2825 switch (p[0])
2826 {
2827 case '=': if (p[1] == '=')
2828 type = TYPE_EQUAL;
2829 else if (p[1] == '~')
2830 type = TYPE_MATCH;
2831 break;
2832 case '!': if (p[1] == '=')
2833 type = TYPE_NEQUAL;
2834 else if (p[1] == '~')
2835 type = TYPE_NOMATCH;
2836 break;
2837 case '>': if (p[1] != '=')
2838 {
2839 type = TYPE_GREATER;
2840 len = 1;
2841 }
2842 else
2843 type = TYPE_GEQUAL;
2844 break;
2845 case '<': if (p[1] != '=')
2846 {
2847 type = TYPE_SMALLER;
2848 len = 1;
2849 }
2850 else
2851 type = TYPE_SEQUAL;
2852 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002853 case 'i': if (p[1] == 's')
2854 {
2855 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2856 len = 5;
2857 if (!vim_isIDc(p[len]))
2858 {
2859 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2860 type_is = TRUE;
2861 }
2862 }
2863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865
2866 /*
2867 * If there is a comparitive operator, use it.
2868 */
2869 if (type != TYPE_UNKNOWN)
2870 {
2871 /* extra question mark appended: ignore case */
2872 if (p[len] == '?')
2873 {
2874 ic = TRUE;
2875 ++len;
2876 }
2877 /* extra '#' appended: match case */
2878 else if (p[len] == '#')
2879 {
2880 ic = FALSE;
2881 ++len;
2882 }
2883 /* nothing appened: use 'ignorecase' */
2884 else
2885 ic = p_ic;
2886
2887 /*
2888 * Get the second variable.
2889 */
2890 *arg = skipwhite(p + len);
2891 if (eval5(arg, &var2, evaluate) == FAIL)
2892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 return FAIL;
2895 }
2896
2897 if (evaluate)
2898 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002899 if (type_is && rettv->v_type != var2.v_type)
2900 {
2901 /* For "is" a different type always means FALSE, for "notis"
2902 * it means TRUE. */
2903 n1 = (type == TYPE_NEQUAL);
2904 }
2905 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
2906 {
2907 if (type_is)
2908 {
2909 n1 = (rettv->v_type == var2.v_type
2910 && rettv->vval.v_list == var2.vval.v_list);
2911 if (type == TYPE_NEQUAL)
2912 n1 = !n1;
2913 }
2914 else if (rettv->v_type != var2.v_type
2915 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2916 {
2917 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002918 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002919 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002920 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002921 clear_tv(rettv);
2922 clear_tv(&var2);
2923 return FAIL;
2924 }
2925 else
2926 {
2927 /* Compare two Lists for being equal or unequal. */
2928 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
2929 if (type == TYPE_NEQUAL)
2930 n1 = !n1;
2931 }
2932 }
2933
2934 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
2935 {
2936 if (rettv->v_type != var2.v_type
2937 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2938 {
2939 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002940 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002941 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002942 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002943 clear_tv(rettv);
2944 clear_tv(&var2);
2945 return FAIL;
2946 }
2947 else
2948 {
2949 /* Compare two Funcrefs for being equal or unequal. */
2950 if (rettv->vval.v_string == NULL
2951 || var2.vval.v_string == NULL)
2952 n1 = FALSE;
2953 else
2954 n1 = STRCMP(rettv->vval.v_string,
2955 var2.vval.v_string) == 0;
2956 if (type == TYPE_NEQUAL)
2957 n1 = !n1;
2958 }
2959 }
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 /*
2962 * If one of the two variables is a number, compare as a number.
2963 * When using "=~" or "!~", always compare as string.
2964 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002965 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 n1 = get_tv_number(rettv);
2969 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 switch (type)
2971 {
2972 case TYPE_EQUAL: n1 = (n1 == n2); break;
2973 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2974 case TYPE_GREATER: n1 = (n1 > n2); break;
2975 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2976 case TYPE_SMALLER: n1 = (n1 < n2); break;
2977 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2978 case TYPE_UNKNOWN:
2979 case TYPE_MATCH:
2980 case TYPE_NOMATCH: break; /* avoid gcc warning */
2981 }
2982 }
2983 else
2984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002985 s1 = get_tv_string_buf(rettv, buf1);
2986 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2988 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2989 else
2990 i = 0;
2991 n1 = FALSE;
2992 switch (type)
2993 {
2994 case TYPE_EQUAL: n1 = (i == 0); break;
2995 case TYPE_NEQUAL: n1 = (i != 0); break;
2996 case TYPE_GREATER: n1 = (i > 0); break;
2997 case TYPE_GEQUAL: n1 = (i >= 0); break;
2998 case TYPE_SMALLER: n1 = (i < 0); break;
2999 case TYPE_SEQUAL: n1 = (i <= 0); break;
3000
3001 case TYPE_MATCH:
3002 case TYPE_NOMATCH:
3003 /* avoid 'l' flag in 'cpoptions' */
3004 save_cpo = p_cpo;
3005 p_cpo = (char_u *)"";
3006 regmatch.regprog = vim_regcomp(s2,
3007 RE_MAGIC + RE_STRING);
3008 regmatch.rm_ic = ic;
3009 if (regmatch.regprog != NULL)
3010 {
3011 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3012 vim_free(regmatch.regprog);
3013 if (type == TYPE_NOMATCH)
3014 n1 = !n1;
3015 }
3016 p_cpo = save_cpo;
3017 break;
3018
3019 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3020 }
3021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003022 clear_tv(rettv);
3023 clear_tv(&var2);
3024 rettv->v_type = VAR_NUMBER;
3025 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
3027 }
3028
3029 return OK;
3030}
3031
3032/*
3033 * Handle fourth level expression:
3034 * + number addition
3035 * - number subtraction
3036 * . string concatenation
3037 *
3038 * "arg" must point to the first non-white of the expression.
3039 * "arg" is advanced to the next non-white after the recognized expression.
3040 *
3041 * Return OK or FAIL.
3042 */
3043 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 int evaluate;
3048{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003049 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003050 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int op;
3052 long n1, n2;
3053 char_u *s1, *s2;
3054 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3055 char_u *p;
3056
3057 /*
3058 * Get the first variable.
3059 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 return FAIL;
3062
3063 /*
3064 * Repeat computing, until no '+', '-' or '.' is following.
3065 */
3066 for (;;)
3067 {
3068 op = **arg;
3069 if (op != '+' && op != '-' && op != '.')
3070 break;
3071
3072 /*
3073 * Get the second variable.
3074 */
3075 *arg = skipwhite(*arg + 1);
3076 if (eval6(arg, &var2, evaluate) == FAIL)
3077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 return FAIL;
3080 }
3081
3082 if (evaluate)
3083 {
3084 /*
3085 * Compute the result.
3086 */
3087 if (op == '.')
3088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003089 s1 = get_tv_string_buf(rettv, buf1);
3090 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 op = (int)STRLEN(s1);
3092 p = alloc((unsigned)(op + STRLEN(s2) + 1));
3093 if (p != NULL)
3094 {
3095 STRCPY(p, s1);
3096 STRCPY(p + op, s2);
3097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 clear_tv(rettv);
3099 rettv->v_type = VAR_STRING;
3100 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003102 else if (op == '+' && rettv->v_type == VAR_LIST
3103 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003104 {
3105 /* concatenate Lists */
3106 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3107 &var3) == FAIL)
3108 {
3109 clear_tv(rettv);
3110 clear_tv(&var2);
3111 return FAIL;
3112 }
3113 clear_tv(rettv);
3114 *rettv = var3;
3115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 else
3117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003118 n1 = get_tv_number(rettv);
3119 n2 = get_tv_number(&var2);
3120 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (op == '+')
3122 n1 = n1 + n2;
3123 else
3124 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125 rettv->v_type = VAR_NUMBER;
3126 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003128 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130 }
3131 return OK;
3132}
3133
3134/*
3135 * Handle fifth level expression:
3136 * * number multiplication
3137 * / number division
3138 * % number modulo
3139 *
3140 * "arg" must point to the first non-white of the expression.
3141 * "arg" is advanced to the next non-white after the recognized expression.
3142 *
3143 * Return OK or FAIL.
3144 */
3145 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003148 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 int evaluate;
3150{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003151 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int op;
3153 long n1, n2;
3154
3155 /*
3156 * Get the first variable.
3157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003158 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return FAIL;
3160
3161 /*
3162 * Repeat computing, until no '*', '/' or '%' is following.
3163 */
3164 for (;;)
3165 {
3166 op = **arg;
3167 if (op != '*' && op != '/' && op != '%')
3168 break;
3169
3170 if (evaluate)
3171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003172 n1 = get_tv_number(rettv);
3173 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 else
3176 n1 = 0;
3177
3178 /*
3179 * Get the second variable.
3180 */
3181 *arg = skipwhite(*arg + 1);
3182 if (eval7(arg, &var2, evaluate) == FAIL)
3183 return FAIL;
3184
3185 if (evaluate)
3186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 n2 = get_tv_number(&var2);
3188 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 /*
3191 * Compute the result.
3192 */
3193 if (op == '*')
3194 n1 = n1 * n2;
3195 else if (op == '/')
3196 {
3197 if (n2 == 0) /* give an error message? */
3198 n1 = 0x7fffffffL;
3199 else
3200 n1 = n1 / n2;
3201 }
3202 else
3203 {
3204 if (n2 == 0) /* give an error message? */
3205 n1 = 0;
3206 else
3207 n1 = n1 % n2;
3208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003209 rettv->v_type = VAR_NUMBER;
3210 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212 }
3213
3214 return OK;
3215}
3216
3217/*
3218 * Handle sixth level expression:
3219 * number number constant
3220 * "string" string contstant
3221 * 'string' literal string contstant
3222 * &option-name option value
3223 * @r register contents
3224 * identifier variable value
3225 * function() function call
3226 * $VAR environment variable
3227 * (expression) nested expression
3228 *
3229 * Also handle:
3230 * ! in front logical NOT
3231 * - in front unary minus
3232 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003233 * trailing [] subscript in String or List
3234 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 *
3236 * "arg" must point to the first non-white of the expression.
3237 * "arg" is advanced to the next non-white after the recognized expression.
3238 *
3239 * Return OK or FAIL.
3240 */
3241 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003242eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003244 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 int evaluate;
3246{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 long n;
3248 int len;
3249 char_u *s;
3250 int val;
3251 char_u *start_leader, *end_leader;
3252 int ret = OK;
3253 char_u *alias;
Bram Moolenaare9a41262005-01-15 22:18:47 +00003254 dictvar *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003257 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003258 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003260 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 /*
3263 * Skip '!' and '-' characters. They are handled later.
3264 */
3265 start_leader = *arg;
3266 while (**arg == '!' || **arg == '-' || **arg == '+')
3267 *arg = skipwhite(*arg + 1);
3268 end_leader = *arg;
3269
3270 switch (**arg)
3271 {
3272 /*
3273 * Number constant.
3274 */
3275 case '0':
3276 case '1':
3277 case '2':
3278 case '3':
3279 case '4':
3280 case '5':
3281 case '6':
3282 case '7':
3283 case '8':
3284 case '9':
3285 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3286 *arg += len;
3287 if (evaluate)
3288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003289 rettv->v_type = VAR_NUMBER;
3290 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292 break;
3293
3294 /*
3295 * String constant: "string".
3296 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 break;
3299
3300 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003301 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003303 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003304 break;
3305
3306 /*
3307 * List: [expr, expr]
3308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003309 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 break;
3311
3312 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003313 * Dictionary: {key: val, key: val}
3314 */
3315 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3316 break;
3317
3318 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003319 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003321 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 break;
3323
3324 /*
3325 * Environment variable: $VAR.
3326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 break;
3329
3330 /*
3331 * Register contents: @r.
3332 */
3333 case '@': ++*arg;
3334 if (evaluate)
3335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 rettv->v_type = VAR_STRING;
3337 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 if (**arg != NUL)
3340 ++*arg;
3341 break;
3342
3343 /*
3344 * nested expression: (expression).
3345 */
3346 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (**arg == ')')
3349 ++*arg;
3350 else if (ret == OK)
3351 {
3352 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003353 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 ret = FAIL;
3355 }
3356 break;
3357
Bram Moolenaar8c711452005-01-14 21:53:12 +00003358 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 break;
3360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003361
3362 if (ret == NOTDONE)
3363 {
3364 /*
3365 * Must be a variable or function name.
3366 * Can also be a curly-braces kind of name: {expr}.
3367 */
3368 s = *arg;
3369 len = get_func_len(arg, &alias, evaluate);
3370 if (alias != NULL)
3371 s = alias;
3372
3373 if (len == 0)
3374 ret = FAIL;
3375 else
3376 {
3377 if (**arg == '(') /* recursive! */
3378 {
3379 /* If "s" is the name of a variable of type VAR_FUNC
3380 * use its contents. */
3381 s = deref_func_name(s, &len);
3382
3383 /* Invoke the function. */
3384 ret = get_func_tv(s, len, rettv, arg,
3385 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003386 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 /* Stop the expression evaluation when immediately
3388 * aborting on error, or when an interrupt occurred or
3389 * an exception was thrown but not caught. */
3390 if (aborting())
3391 {
3392 if (ret == OK)
3393 clear_tv(rettv);
3394 ret = FAIL;
3395 }
3396 }
3397 else if (evaluate)
3398 ret = get_var_tv(s, len, rettv);
3399 }
3400
3401 if (alias != NULL)
3402 vim_free(alias);
3403 }
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 *arg = skipwhite(*arg);
3406
3407 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003408 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00003409 * Also handle function call with Funcref variable: func(expr)
3410 * Can all be combined: dict.func(expr)[idx].func(expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003412 selfdict = NULL;
3413 while (ret == OK
3414 && (**arg == '['
3415 || (**arg == '.' && rettv->v_type == VAR_DICT)
3416 || (**arg == '(' && rettv->v_type == VAR_FUNC))
3417 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003419 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003421 s = rettv->vval.v_string;
3422
3423 /* Invoke the function. Recursive! */
3424 ret = get_func_tv(s, STRLEN(s), rettv, arg,
3425 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3426 &len, evaluate, selfdict);
3427
3428 /* Stop the expression evaluation when immediately
3429 * aborting on error, or when an interrupt occurred or
3430 * an exception was thrown but not caught. */
3431 if (aborting())
3432 {
3433 if (ret == OK)
3434 clear_tv(rettv);
3435 ret = FAIL;
3436 }
3437 selfdict = NULL;
3438 }
3439 else
3440 {
3441 if (rettv->v_type == VAR_DICT)
3442 selfdict = rettv->vval.v_dict;
3443 else
3444 selfdict = NULL;
3445 if (eval_index(arg, rettv, evaluate) == FAIL)
3446 {
3447 clear_tv(rettv);
3448 ret = FAIL;
3449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452
3453 /*
3454 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3455 */
3456 if (ret == OK && evaluate && end_leader > start_leader)
3457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003458 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 while (end_leader > start_leader)
3460 {
3461 --end_leader;
3462 if (*end_leader == '!')
3463 val = !val;
3464 else if (*end_leader == '-')
3465 val = -val;
3466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 clear_tv(rettv);
3468 rettv->v_type = VAR_NUMBER;
3469 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471
3472 return ret;
3473}
3474
3475/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003476 * Evaluate an "[expr]" or "[expr:expr]" index.
3477 * "*arg" points to the '['.
3478 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3479 */
3480 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003484 int evaluate;
3485{
3486 int empty1 = FALSE, empty2 = FALSE;
3487 typeval var1, var2;
3488 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003489 long len = -1;
3490 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003492 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003495 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003496 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003497 return FAIL;
3498 }
3499
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003502 /*
3503 * dict.name
3504 */
3505 key = *arg + 1;
3506 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3507 ;
3508 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003509 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003510 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003511 }
3512 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003513 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003514 /*
3515 * something[idx]
3516 *
3517 * Get the (first) variable from inside the [].
3518 */
3519 *arg = skipwhite(*arg + 1);
3520 if (**arg == ':')
3521 empty1 = TRUE;
3522 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3523 return FAIL;
3524
3525 /*
3526 * Get the second variable from inside the [:].
3527 */
3528 if (**arg == ':')
3529 {
3530 range = TRUE;
3531 *arg = skipwhite(*arg + 1);
3532 if (**arg == ']')
3533 empty2 = TRUE;
3534 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3535 {
3536 clear_tv(&var1);
3537 return FAIL;
3538 }
3539 }
3540
3541 /* Check for the ']'. */
3542 if (**arg != ']')
3543 {
3544 EMSG(_(e_missbrac));
3545 clear_tv(&var1);
3546 if (range)
3547 clear_tv(&var2);
3548 return FAIL;
3549 }
3550 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003551 }
3552
3553 if (evaluate)
3554 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003555 n1 = 0;
3556 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 n1 = get_tv_number(&var1);
3559 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 }
3561 if (range)
3562 {
3563 if (empty2)
3564 n2 = -1;
3565 else
3566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003567 n2 = get_tv_number(&var2);
3568 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003569 }
3570 }
3571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003572 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003573 {
3574 case VAR_NUMBER:
3575 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003576 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003577 len = (long)STRLEN(s);
3578 if (range)
3579 {
3580 /* The resulting variable is a substring. If the indexes
3581 * are out of range the result is empty. */
3582 if (n1 < 0)
3583 {
3584 n1 = len + n1;
3585 if (n1 < 0)
3586 n1 = 0;
3587 }
3588 if (n2 < 0)
3589 n2 = len + n2;
3590 else if (n2 >= len)
3591 n2 = len;
3592 if (n1 >= len || n2 < 0 || n1 > n2)
3593 s = NULL;
3594 else
3595 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3596 }
3597 else
3598 {
3599 /* The resulting variable is a string of a single
3600 * character. If the index is too big or negative the
3601 * result is empty. */
3602 if (n1 >= len || n1 < 0)
3603 s = NULL;
3604 else
3605 s = vim_strnsave(s + n1, 1);
3606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003607 clear_tv(rettv);
3608 rettv->v_type = VAR_STRING;
3609 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003610 break;
3611
3612 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003614 if (n1 < 0)
3615 n1 = len + n1;
3616 if (!empty1 && (n1 < 0 || n1 >= len))
3617 {
3618 EMSGN(_(e_listidx), n1);
3619 return FAIL;
3620 }
3621 if (range)
3622 {
3623 listvar *l;
3624 listitem *item;
3625
3626 if (n2 < 0)
3627 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003629 {
3630 EMSGN(_(e_listidx), n2);
3631 return FAIL;
3632 }
3633 l = list_alloc();
3634 if (l == NULL)
3635 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003636 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003637 n1 <= n2; ++n1)
3638 {
3639 if (list_append_tv(l, &item->li_tv) == FAIL)
3640 {
3641 list_free(l);
3642 return FAIL;
3643 }
3644 item = item->li_next;
3645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003646 clear_tv(rettv);
3647 rettv->v_type = VAR_LIST;
3648 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003649 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003650 }
3651 else
3652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003654 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 clear_tv(rettv);
3656 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003657 }
3658 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659
3660 case VAR_DICT:
3661 if (range)
3662 {
3663 EMSG(_("E999: Using range with Dictionary"));
3664 if (len == -1)
3665 clear_tv(&var1);
3666 return FAIL;
3667 }
3668 {
3669 dictitem *item;
3670
3671 if (len == -1)
3672 {
3673 key = get_tv_string(&var1);
3674 if (*key == NUL)
3675 {
3676 EMSG(_("E999: Empty key for Dictionary"));
3677 clear_tv(&var1);
3678 return FAIL;
3679 }
3680 }
3681
3682 item = dict_find(rettv->vval.v_dict, key, (int)len);
3683
3684 if (item == NULL)
3685 EMSG2(_("E999: Key not found in Dictionary: %s"), key);
3686 if (len == -1)
3687 clear_tv(&var1);
3688 if (item == NULL)
3689 return FAIL;
3690
3691 copy_tv(&item->di_tv, &var1);
3692 clear_tv(rettv);
3693 *rettv = var1;
3694 }
3695 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003696 }
3697 }
3698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003699 return OK;
3700}
3701
3702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * Get an option value.
3704 * "arg" points to the '&' or '+' before the option name.
3705 * "arg" is advanced to character after the option name.
3706 * Return OK or FAIL.
3707 */
3708 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003709get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 int evaluate;
3713{
3714 char_u *option_end;
3715 long numval;
3716 char_u *stringval;
3717 int opt_type;
3718 int c;
3719 int working = (**arg == '+'); /* has("+option") */
3720 int ret = OK;
3721 int opt_flags;
3722
3723 /*
3724 * Isolate the option name and find its value.
3725 */
3726 option_end = find_option_end(arg, &opt_flags);
3727 if (option_end == NULL)
3728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 EMSG2(_("E112: Option name missing: %s"), *arg);
3731 return FAIL;
3732 }
3733
3734 if (!evaluate)
3735 {
3736 *arg = option_end;
3737 return OK;
3738 }
3739
3740 c = *option_end;
3741 *option_end = NUL;
3742 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 if (opt_type == -3) /* invalid name */
3746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003747 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 EMSG2(_("E113: Unknown option: %s"), *arg);
3749 ret = FAIL;
3750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 if (opt_type == -2) /* hidden string option */
3754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 rettv->v_type = VAR_STRING;
3756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 else if (opt_type == -1) /* hidden number option */
3759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003760 rettv->v_type = VAR_NUMBER;
3761 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 else if (opt_type == 1) /* number option */
3764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765 rettv->v_type = VAR_NUMBER;
3766 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768 else /* string option */
3769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 rettv->v_type = VAR_STRING;
3771 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 }
3774 else if (working && (opt_type == -2 || opt_type == -1))
3775 ret = FAIL;
3776
3777 *option_end = c; /* put back for error messages */
3778 *arg = option_end;
3779
3780 return ret;
3781}
3782
3783/*
3784 * Allocate a variable for a string constant.
3785 * Return OK or FAIL.
3786 */
3787 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003788get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 int evaluate;
3792{
3793 char_u *p;
3794 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 int extra = 0;
3796
3797 /*
3798 * Find the end of the string, skipping backslashed characters.
3799 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003800 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
3802 if (*p == '\\' && p[1] != NUL)
3803 {
3804 ++p;
3805 /* A "\<x>" form occupies at least 4 characters, and produces up
3806 * to 6 characters: reserve space for 2 extra */
3807 if (*p == '<')
3808 extra += 2;
3809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812 if (*p != '"')
3813 {
3814 EMSG2(_("E114: Missing quote: %s"), *arg);
3815 return FAIL;
3816 }
3817
3818 /* If only parsing, set *arg and return here */
3819 if (!evaluate)
3820 {
3821 *arg = p + 1;
3822 return OK;
3823 }
3824
3825 /*
3826 * Copy the string into allocated memory, handling backslashed
3827 * characters.
3828 */
3829 name = alloc((unsigned)(p - *arg + extra));
3830 if (name == NULL)
3831 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003832 rettv->v_type = VAR_STRING;
3833 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Bram Moolenaar8c711452005-01-14 21:53:12 +00003835 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 if (*p == '\\')
3838 {
3839 switch (*++p)
3840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003841 case 'b': *name++ = BS; ++p; break;
3842 case 'e': *name++ = ESC; ++p; break;
3843 case 'f': *name++ = FF; ++p; break;
3844 case 'n': *name++ = NL; ++p; break;
3845 case 'r': *name++ = CAR; ++p; break;
3846 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 case 'X': /* hex: "\x1", "\x12" */
3849 case 'x':
3850 case 'u': /* Unicode: "\u0023" */
3851 case 'U':
3852 if (vim_isxdigit(p[1]))
3853 {
3854 int n, nr;
3855 int c = toupper(*p);
3856
3857 if (c == 'X')
3858 n = 2;
3859 else
3860 n = 4;
3861 nr = 0;
3862 while (--n >= 0 && vim_isxdigit(p[1]))
3863 {
3864 ++p;
3865 nr = (nr << 4) + hex2nr(*p);
3866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003867 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868#ifdef FEAT_MBYTE
3869 /* For "\u" store the number according to
3870 * 'encoding'. */
3871 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003872 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 else
3874#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00003875 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 break;
3878
3879 /* octal: "\1", "\12", "\123" */
3880 case '0':
3881 case '1':
3882 case '2':
3883 case '3':
3884 case '4':
3885 case '5':
3886 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003887 case '7': *name = *p++ - '0';
3888 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003890 *name = (*name << 3) + *p++ - '0';
3891 if (*p >= '0' && *p <= '7')
3892 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003894 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 break;
3896
3897 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00003898 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (extra != 0)
3900 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003901 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 break;
3903 }
3904 /* FALLTHROUGH */
3905
Bram Moolenaar8c711452005-01-14 21:53:12 +00003906 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908 }
3909 }
3910 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003911 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003914 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 *arg = p + 1;
3916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 return OK;
3918}
3919
3920/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003921 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 * Return OK or FAIL.
3923 */
3924 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003927 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int evaluate;
3929{
3930 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003931 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003932 int reduce = 0;
3933
3934 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003935 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003936 */
3937 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
3938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003939 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003941 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003942 break;
3943 ++reduce;
3944 ++p;
3945 }
3946 }
3947
Bram Moolenaar8c711452005-01-14 21:53:12 +00003948 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003950 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003951 return FAIL;
3952 }
3953
Bram Moolenaar8c711452005-01-14 21:53:12 +00003954 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003955 if (!evaluate)
3956 {
3957 *arg = p + 1;
3958 return OK;
3959 }
3960
3961 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003962 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003963 */
3964 str = alloc((unsigned)((p - *arg) - reduce));
3965 if (str == NULL)
3966 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003967 rettv->v_type = VAR_STRING;
3968 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003969
Bram Moolenaar8c711452005-01-14 21:53:12 +00003970 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003972 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003974 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003975 break;
3976 ++p;
3977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003980 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003981 *arg = p + 1;
3982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003983 return OK;
3984}
3985
3986/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003987 * Allocate a variable for a List and fill it from "*arg".
3988 * Return OK or FAIL.
3989 */
3990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003992 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003994 int evaluate;
3995{
3996 listvar *l = NULL;
3997 typeval tv;
3998 listitem *item;
3999
4000 if (evaluate)
4001 {
4002 l = list_alloc();
4003 if (l == NULL)
4004 return FAIL;
4005 }
4006
4007 *arg = skipwhite(*arg + 1);
4008 while (**arg != ']' && **arg != NUL)
4009 {
4010 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4011 goto failret;
4012 if (evaluate)
4013 {
4014 item = listitem_alloc();
4015 if (item != NULL)
4016 {
4017 item->li_tv = tv;
4018 list_append(l, item);
4019 }
4020 }
4021
4022 if (**arg == ']')
4023 break;
4024 if (**arg != ',')
4025 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004026 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004027 goto failret;
4028 }
4029 *arg = skipwhite(*arg + 1);
4030 }
4031
4032 if (**arg != ']')
4033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004034 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004035failret:
4036 if (evaluate)
4037 list_free(l);
4038 return FAIL;
4039 }
4040
4041 *arg = skipwhite(*arg + 1);
4042 if (evaluate)
4043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 rettv->v_type = VAR_LIST;
4045 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004046 ++l->lv_refcount;
4047 }
4048
4049 return OK;
4050}
4051
4052/*
4053 * Allocate an empty header for a list.
4054 */
4055 static listvar *
4056list_alloc()
4057{
4058 return (listvar *)alloc_clear(sizeof(listvar));
4059}
4060
4061/*
4062 * Unreference a list: decrement the reference count and free it when it
4063 * becomes zero.
4064 */
4065 static void
4066list_unref(l)
4067 listvar *l;
4068{
4069 if (l != NULL && --l->lv_refcount <= 0)
4070 list_free(l);
4071}
4072
4073/*
4074 * Free a list, including all items it points to.
4075 * Ignores the reference count.
4076 */
4077 static void
4078list_free(l)
4079 listvar *l;
4080{
4081 listitem *item;
4082 listitem *next;
4083
4084 for (item = l->lv_first; item != NULL; item = next)
4085 {
4086 next = item->li_next;
4087 listitem_free(item);
4088 }
4089 vim_free(l);
4090}
4091
4092/*
4093 * Allocate a list item.
4094 */
4095 static listitem *
4096listitem_alloc()
4097{
4098 return (listitem *)alloc(sizeof(listitem));
4099}
4100
4101/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004102 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004103 */
4104 static void
4105listitem_free(item)
4106 listitem *item;
4107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 vim_free(item);
4110}
4111
4112/*
4113 * Get the number of items in a list.
4114 */
4115 static long
4116list_len(l)
4117 listvar *l;
4118{
4119 listitem *item;
4120 long len = 0;
4121
4122 if (l == NULL)
4123 return 0L;
4124 for (item = l->lv_first; item != NULL; item = item->li_next)
4125 ++len;
4126 return len;
4127}
4128
4129/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004130 * Return TRUE when two lists have exactly the same values.
4131 */
4132 static int
4133list_equal(l1, l2, ic)
4134 listvar *l1;
4135 listvar *l2;
4136 int ic; /* ignore case for strings */
4137{
4138 listitem *item1, *item2;
4139
4140 for (item1 = l1->lv_first, item2 = l2->lv_first;
4141 item1 != NULL && item2 != NULL;
4142 item1 = item1->li_next, item2 = item2->li_next)
4143 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4144 return FALSE;
4145 return item1 == NULL && item2 == NULL;
4146}
4147
4148/*
4149 * Return TRUE if "tv1" and "tv2" have the same value.
4150 * Compares the items just like "==" would compare them.
4151 */
4152 static int
4153tv_equal(tv1, tv2, ic)
4154 typeval *tv1;
4155 typeval *tv2;
4156 int ic; /* ignore case */
4157{
4158 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4159
4160 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4161 {
4162 /* recursive! */
4163 if (tv1->v_type != tv2->v_type
4164 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4165 return FALSE;
4166 }
4167 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4168 {
4169 if (tv1->v_type != tv2->v_type
4170 || tv1->vval.v_string == NULL
4171 || tv2->vval.v_string == NULL
4172 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4173 return FALSE;
4174 }
4175 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4176 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004177 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4178 * Don't consider "4x" to be equal to 4. */
4179 if ((tv1->v_type == VAR_STRING
4180 && !string_isa_number(tv1->vval.v_string))
4181 || (tv2->v_type == VAR_STRING
4182 && !string_isa_number(tv2->vval.v_string)))
4183 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004184 if (get_tv_number(tv1) != get_tv_number(tv2))
4185 return FALSE;
4186 }
4187 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4188 get_tv_string_buf(tv2, buf2)) != 0)
4189 return FALSE;
4190 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4191 get_tv_string_buf(tv2, buf2)) != 0)
4192 return FALSE;
4193 return TRUE;
4194}
4195
4196/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004197 * Return TRUE if "tv" is a number without other non-white characters.
4198 */
4199 static int
4200string_isa_number(s)
4201 char_u *s;
4202{
4203 int len;
4204
4205 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4206 return len > 0 && *skipwhite(s + len) == NUL;
4207}
4208
4209/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004210 * Locate item with index "n" in list "l" and return it.
4211 * A negative index is counted from the end; -1 is the last item.
4212 * Returns NULL when "n" is out of range.
4213 */
4214 static listitem *
4215list_find(l, n)
4216 listvar *l;
4217 long n;
4218{
4219 listitem *item;
4220 long idx;
4221
4222 if (l == NULL)
4223 return NULL;
4224 if (n < 0)
4225 {
4226 idx = -1; /* search from the end */
4227 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4228 --idx;
4229 }
4230 else
4231 {
4232 idx = 0; /* search from the start */
4233 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4234 ++idx;
4235 }
4236 if (idx != n)
4237 return NULL;
4238 return item;
4239}
4240
4241/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004242 * Locate "item" list "l" and return its index.
4243 * Returns -1 when "item" is not in the list.
4244 */
4245 static long
4246list_idx_of_item(l, item)
4247 listvar *l;
4248 listitem *item;
4249{
4250 long idx = 0;
4251 listitem *li;
4252
4253 if (l == NULL)
4254 return -1;
4255 idx = 0;
4256 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4257 ++idx;
4258 if (li == NULL)
4259 return -1;
4260 return idx;;
4261}
4262
4263/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004264 * Like list_find(), but also find an item just past the end.
4265 * "*ip" is the item to find.
4266 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4267 * Returns NULL when item not found or item is just past the end.
4268 */
4269 static listitem *
4270list_find_ext(l, ip)
4271 listvar *l;
4272 long *ip;
4273{
4274 long n;
4275 listitem *item;
4276
4277 if (*ip < 0)
4278 {
4279 /* Count from the end: -1 is before last item. */
4280 item = l->lv_last;
4281 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4282 item = item->li_prev;
4283 if (item == NULL)
4284 n = 1; /* error! */
4285 }
4286 else
4287 {
4288 item = l->lv_first;
4289 for (n = *ip; n > 0 && item != NULL; --n)
4290 item = item->li_next;
4291 }
4292 *ip = n;
4293 return item;
4294}
4295
4296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004297 * Append item "item" to the end of list "l".
4298 */
4299 static void
4300list_append(l, item)
4301 listvar *l;
4302 listitem *item;
4303{
4304 if (l->lv_last == NULL)
4305 {
4306 /* empty list */
4307 l->lv_first = item;
4308 l->lv_last = item;
4309 item->li_prev = NULL;
4310 }
4311 else
4312 {
4313 l->lv_last->li_next = item;
4314 item->li_prev = l->lv_last;
4315 l->lv_last = item;
4316 }
4317 item->li_next = NULL;
4318}
4319
4320/*
4321 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004322 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004323 */
4324 static int
4325list_append_tv(l, tv)
4326 listvar *l;
4327 typeval *tv;
4328{
4329 listitem *ni = listitem_alloc();
4330
4331 if (ni == NULL)
4332 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004334 list_append(l, ni);
4335 return OK;
4336}
4337
4338/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 * Insert typeval "tv" in list "l" before "item".
4340 * If "item" is NULL append at the end.
4341 * Return FAIL when out of memory.
4342 */
4343 static int
4344list_insert_tv(l, tv, item)
4345 listvar *l;
4346 typeval *tv;
4347 listitem *item;
4348{
4349 listitem *ni = listitem_alloc();
4350
4351 if (ni == NULL)
4352 return FAIL;
4353 copy_tv(tv, &ni->li_tv);
4354 if (item == NULL)
4355 /* Append new item at end of list. */
4356 list_append(l, ni);
4357 else
4358 {
4359 /* Insert new item before existing item. */
4360 ni->li_prev = item->li_prev;
4361 ni->li_next = item;
4362 if (item->li_prev == NULL)
4363 l->lv_first = ni;
4364 else
4365 item->li_prev->li_next = ni;
4366 item->li_prev = ni;
4367 }
4368 return OK;
4369}
4370
4371/*
4372 * Extend "l1" with "l2".
4373 * If "bef" is NULL append at the end, otherwise insert before this item.
4374 * Returns FAIL when out of memory.
4375 */
4376 static int
4377list_extend(l1, l2, bef)
4378 listvar *l1;
4379 listvar *l2;
4380 listitem *bef;
4381{
4382 listitem *item;
4383
4384 for (item = l2->lv_first; item != NULL; item = item->li_next)
4385 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4386 return FAIL;
4387 return OK;
4388}
4389
4390/*
4391 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4392 * Return FAIL when out of memory.
4393 */
4394 static int
4395list_concat(l1, l2, tv)
4396 listvar *l1;
4397 listvar *l2;
4398 typeval *tv;
4399{
4400 listvar *l;
4401
4402 /* make a copy of the first list. */
4403 l = list_copy(l1, FALSE);
4404 if (l == NULL)
4405 return FAIL;
4406 tv->v_type = VAR_LIST;
4407 tv->vval.v_list = l;
4408
4409 /* append all items from the second list */
4410 return list_extend(l, l2, NULL);
4411}
4412
4413/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004414 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004415 * The refcount of the new list is set to 1.
4416 * Returns NULL when out of memory.
4417 */
4418 static listvar *
4419list_copy(orig, deep)
4420 listvar *orig;
4421 int deep;
4422{
4423 listvar *copy;
4424 listitem *item;
4425 listitem *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426
4427 if (orig == NULL)
4428 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004429
4430 copy = list_alloc();
4431 if (copy != NULL)
4432 {
4433 for (item = orig->lv_first; item != NULL; item = item->li_next)
4434 {
4435 ni = listitem_alloc();
4436 if (ni == NULL)
4437 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004438 if (deep)
4439 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 list_append(copy, ni);
4443 }
4444 ++copy->lv_refcount;
4445 }
4446
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004447 return copy;
4448}
4449
4450/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 * Remove items "item" to "item2" from list "l".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 static void
4454list_getrem(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004457 listitem *item2;
4458{
4459 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 /* notify watchers */
4462 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 list_fix_watch(l, ip);
4465 if (ip == item2)
4466 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468
4469 if (item2->li_next == NULL)
4470 l->lv_last = item->li_prev;
4471 else
4472 item2->li_next->li_prev = item->li_prev;
4473 if (item->li_prev == NULL)
4474 l->lv_first = item2->li_next;
4475 else
4476 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477}
4478
4479/*
4480 * Return an allocated string with the string representation of a list.
4481 * May return NULL.
4482 */
4483 static char_u *
4484list2string(tv)
4485 typeval *tv;
4486{
4487 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488
4489 if (tv->vval.v_list == NULL)
4490 return NULL;
4491 ga_init2(&ga, (int)sizeof(char), 80);
4492 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004493 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004494 ga_append(&ga, ']');
4495 ga_append(&ga, NUL);
4496 return (char_u *)ga.ga_data;
4497}
4498
4499/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004500 * Join list "l" into a string in "*gap", using separator "sep".
4501 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4502 */
4503 static void
4504list_join(gap, l, sep, echo)
4505 garray_T *gap;
4506 listvar *l;
4507 char_u *sep;
4508 int echo;
4509{
4510 int first = TRUE;
4511 char_u *tofree;
4512 char_u numbuf[NUMBUFLEN];
4513 listitem *item;
4514 char_u *s;
4515
4516 for (item = l->lv_first; item != NULL; item = item->li_next)
4517 {
4518 if (first)
4519 first = FALSE;
4520 else
4521 ga_concat(gap, sep);
4522
4523 if (echo)
4524 s = echo_string(&item->li_tv, &tofree, numbuf);
4525 else
4526 s = tv2string(&item->li_tv, &tofree, numbuf);
4527 if (s != NULL)
4528 ga_concat(gap, s);
4529 vim_free(tofree);
4530 }
4531}
4532
4533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 * Allocate an empty header for a dictionary.
4535 */
4536 static dictvar *
4537dict_alloc()
4538{
4539 return (dictvar *)alloc_clear(sizeof(dictvar));
4540}
4541
4542/*
4543 * Unreference a Dictionary: decrement the reference count and free it when it
4544 * becomes zero.
4545 */
4546 static void
4547dict_unref(d)
4548 dictvar *d;
4549{
4550 if (d != NULL && --d->dv_refcount <= 0)
4551 dict_free(d);
4552}
4553
4554/*
4555 * Free a Dictionary, including all items it contains.
4556 * Ignores the reference count.
4557 */
4558 static void
4559dict_free(d)
4560 dictvar *d;
4561{
4562 dictitem *item;
4563 dictitem *next;
4564
4565 for (item = d->dv_first; item != NULL; item = next)
4566 {
4567 next = item->di_next;
4568 dictitem_free(item);
4569 }
4570 vim_free(d);
4571}
4572
4573/*
4574 * Allocate a Dictionary item.
4575 */
4576 static dictitem *
4577dictitem_alloc()
4578{
4579 return (dictitem *)alloc(sizeof(dictitem));
4580}
4581
4582/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004583 * Make a copy of a Dictionary item.
4584 */
4585 static dictitem *
4586dictitem_copy(org)
4587 dictitem *org;
4588{
4589 dictitem *di;
4590
4591 di = (dictitem *)alloc(sizeof(dictitem));
4592 if (di != NULL)
4593 {
4594 di->di_key = vim_strsave(org->di_key);
4595 if (di->di_key == NULL)
4596 {
4597 vim_free(di);
4598 return NULL;
4599 }
4600 copy_tv(&org->di_tv, &di->di_tv);
4601 }
4602 return di;
4603}
4604
4605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 * Free a dict item. Also clears the value.
4607 */
4608 static void
4609dictitem_free(item)
4610 dictitem *item;
4611{
4612 vim_free(item->di_key);
4613 clear_tv(&item->di_tv);
4614 vim_free(item);
4615}
4616
4617/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004618 * Make a copy of dict "d". Shallow if "deep" is FALSE.
4619 * The refcount of the new dict is set to 1.
4620 * Returns NULL when out of memory.
4621 */
4622 static dictvar *
4623dict_copy(orig, deep)
4624 dictvar *orig;
4625 int deep;
4626{
4627 dictvar *copy;
4628 dictitem *item;
4629 dictitem *di;
4630
4631 if (orig == NULL)
4632 return NULL;
4633
4634 copy = dict_alloc();
4635 if (copy != NULL)
4636 {
4637 for (item = orig->dv_first; item != NULL; item = item->di_next)
4638 {
4639 di = dictitem_alloc();
4640 if (di == NULL)
4641 break;
4642 di->di_key = vim_strsave(item->di_key);
4643 if (di->di_key == NULL)
4644 {
4645 vim_free(di);
4646 break;
4647 }
4648 if (deep)
4649 item_copy(&item->di_tv, &di->di_tv, deep);
4650 else
4651 copy_tv(&item->di_tv, &di->di_tv);
4652 dict_add(copy, di);
4653 }
4654 ++copy->dv_refcount;
4655 }
4656
4657 return copy;
4658}
4659
4660/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 * Add item "item" to Dictionary "d".
4662 */
4663 static void
4664dict_add(d, item)
4665 dictvar *d;
4666 dictitem *item;
4667{
4668 item->di_next = d->dv_first;
4669 d->dv_first = item;
4670}
4671
4672#if 0 /* not currently used */
4673static void dict_set_item __ARGS((dictvar *d, int type, char *key, void *val));
4674
4675/*
4676 * Add an item to Dictionary "d" with type "type", key "key" and value "val".
4677 * If it already exists it is overwritten.
4678 * The key and value are copied to allocated memory.
4679 */
4680 static void
4681dict_set_item(d, type, key, val)
4682 dictvar *d;
4683 int type;
4684 char *key;
4685 void *val;
4686{
4687 dictitem *di;
4688 char_u *dkey;
4689
4690 di = dict_find(d, (char_u *)key, -1);
4691 if (di == NULL)
4692 {
4693 dkey = vim_strsave((char_u *)key);
4694 if (dkey != NULL)
4695 {
4696 di = dictitem_alloc();
4697 if (di == NULL)
4698 vim_free(dkey);
4699 else
4700 di->di_key = dkey;
4701 }
4702 }
4703 else
4704 clear_tv(&di->di_tv);
4705
4706 if (di != NULL)
4707 {
4708 di->di_tv.v_type = type;
4709 switch (type)
4710 {
4711 case VAR_NUMBER:
4712 di->di_tv.vval.v_number = (varnumber_T)val;
4713 break;
4714 case VAR_FUNC:
4715 case VAR_STRING:
4716 di->di_tv.vval.v_string = vim_strsave((char_u *)val);
4717 break;
4718 default:
4719 EMSG2(_(e_intern2), "dict_set_item()");
4720 }
4721 dict_add(d, di);
4722 }
4723}
4724#endif
4725
4726/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004727 * Get the number of items in a Dictionary.
4728 */
4729 static long
4730dict_len(d)
4731 dictvar *d;
4732{
4733 dictitem *item;
4734 long len = 0;
4735
4736 if (d == NULL)
4737 return 0L;
4738 for (item = d->dv_first; item != NULL; item = item->di_next)
4739 ++len;
4740 return len;
4741}
4742
4743/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 * Find item "key[len]" in Dictionary "d".
4745 * If "len" is negative use strlen(key).
4746 * Returns NULL when not found.
4747 */
4748 static dictitem *
4749dict_find(d, key, len)
4750 dictvar *d;
4751 char_u *key;
4752 int len;
4753{
4754 static dictitem *di;
4755
4756 for (di = d->dv_first; di != NULL; di = di->di_next)
4757 if (len < 0
4758 ? STRCMP(di->di_key, key) == 0
4759 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL)
4760 return di;
4761 return NULL;
4762}
4763
4764/*
4765 * Return an allocated string with the string representation of a Dictionary.
4766 * May return NULL.
4767 */
4768 static char_u *
4769dict2string(tv)
4770 typeval *tv;
4771{
4772 garray_T ga;
4773 int first = TRUE;
4774 char_u *tofree;
4775 char_u numbuf[NUMBUFLEN];
4776 dictitem *item;
4777 char_u *s;
4778
4779 if (tv->vval.v_dict == NULL)
4780 return NULL;
4781 ga_init2(&ga, (int)sizeof(char), 80);
4782 ga_append(&ga, '{');
4783
4784 for (item = tv->vval.v_dict->dv_first; item != NULL; item = item->di_next)
4785 {
4786 if (first)
4787 first = FALSE;
4788 else
4789 ga_concat(&ga, (char_u *)", ");
4790
4791 tofree = string_quote(item->di_key, FALSE);
4792 if (tofree != NULL)
4793 {
4794 ga_concat(&ga, tofree);
4795 vim_free(tofree);
4796 }
4797 ga_concat(&ga, (char_u *)": ");
4798 s = tv2string(&item->di_tv, &tofree, numbuf);
4799 if (s != NULL)
4800 ga_concat(&ga, s);
4801 vim_free(tofree);
4802 }
4803
4804 ga_append(&ga, '}');
4805 ga_append(&ga, NUL);
4806 return (char_u *)ga.ga_data;
4807}
4808
4809/*
4810 * Allocate a variable for a Dictionary and fill it from "*arg".
4811 * Return OK or FAIL. Returns NOTDONE for {expr}.
4812 */
4813 static int
4814get_dict_tv(arg, rettv, evaluate)
4815 char_u **arg;
4816 typeval *rettv;
4817 int evaluate;
4818{
4819 dictvar *d = NULL;
4820 typeval tv;
4821 char_u *key;
4822 dictitem *item;
4823 char_u *start = skipwhite(*arg + 1);
4824
4825 /*
4826 * First check if it's not a curly-braces thing: {expr}.
4827 * Must do this without evaluating, otherwise a function may be called
4828 * twice. Unfortunately this means we need to call eval1() twice for the
4829 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004830 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004832 if (*start != '}')
4833 {
4834 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
4835 return FAIL;
4836 if (*start == '}')
4837 return NOTDONE;
4838 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004839
4840 if (evaluate)
4841 {
4842 d = dict_alloc();
4843 if (d == NULL)
4844 return FAIL;
4845 }
4846
4847 *arg = skipwhite(*arg + 1);
4848 while (**arg != '}' && **arg != NUL)
4849 {
4850 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4851 goto failret;
4852 if (**arg != ':')
4853 {
4854 EMSG2(_("E999: Missing colon in Dictionary: %s"), *arg);
4855 clear_tv(&tv);
4856 goto failret;
4857 }
4858 key = get_tv_string(&tv);
4859 if (*key == NUL)
4860 {
4861 EMSG(_(e_emptykey));
4862 clear_tv(&tv);
4863 goto failret;
4864 }
4865 key = vim_strsave(key);
4866 clear_tv(&tv);
4867 if (key == NULL)
4868 goto failret;
4869
4870 *arg = skipwhite(*arg + 1);
4871 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4872 {
4873 vim_free(key);
4874 goto failret;
4875 }
4876 if (evaluate)
4877 {
4878 item = dict_find(d, key, -1);
4879 if (item != NULL)
4880 {
4881 EMSG(_("E999: Duplicate key in Dictionary"));
4882 vim_free(key);
4883 clear_tv(&tv);
4884 goto failret;
4885 }
4886 item = dictitem_alloc();
4887 if (item == NULL)
4888 vim_free(key);
4889 else
4890 {
4891 item->di_key = key;
4892 item->di_tv = tv;
4893 dict_add(d, item);
4894 }
4895 }
4896
4897 if (**arg == '}')
4898 break;
4899 if (**arg != ',')
4900 {
4901 EMSG2(_("E999: Missing comma in Dictionary: %s"), *arg);
4902 goto failret;
4903 }
4904 *arg = skipwhite(*arg + 1);
4905 }
4906
4907 if (**arg != '}')
4908 {
4909 EMSG2(_("E999: Missing end of Dictionary '}': %s"), *arg);
4910failret:
4911 if (evaluate)
4912 dict_free(d);
4913 return FAIL;
4914 }
4915
4916 *arg = skipwhite(*arg + 1);
4917 if (evaluate)
4918 {
4919 rettv->v_type = VAR_DICT;
4920 rettv->vval.v_dict = d;
4921 ++d->dv_refcount;
4922 }
4923
4924 return OK;
4925}
4926
4927
4928/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929 * Return a string with the string representation of a variable.
4930 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004931 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004932 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933 * May return NULL;
4934 */
4935 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004936echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 typeval *tv;
4938 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004939 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004941 static int recurse = 0;
4942 char_u *r = NULL;
4943
4944 if (recurse >= VAR_MAXNEST)
4945 {
4946 EMSG(_("E999: variable nested too deep for displaying"));
4947 *tofree = NULL;
4948 return NULL;
4949 }
4950 ++recurse;
4951
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 switch (tv->v_type)
4953 {
4954 case VAR_FUNC:
4955 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004956 r = tv->vval.v_string;
4957 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 case VAR_LIST:
4959 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004960 r = *tofree;
4961 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 case VAR_DICT:
4963 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004964 r = *tofree;
4965 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 case VAR_STRING:
4967 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004968 *tofree = NULL;
4969 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004972 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00004973 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004975
4976 --recurse;
4977 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004978}
4979
4980/*
4981 * Return a string with the string representation of a variable.
4982 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4983 * "numbuf" is used for a number.
4984 * Puts quotes around strings, so that they can be parsed back by eval().
4985 * May return NULL;
4986 */
4987 static char_u *
4988tv2string(tv, tofree, numbuf)
4989 typeval *tv;
4990 char_u **tofree;
4991 char_u *numbuf;
4992{
4993 switch (tv->v_type)
4994 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 case VAR_FUNC:
4996 *tofree = string_quote(tv->vval.v_string, TRUE);
4997 return *tofree;
4998 case VAR_STRING:
4999 *tofree = string_quote(tv->vval.v_string, FALSE);
5000 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005001 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005002 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005004 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005005 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005006 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005008 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005009}
5010
5011/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012 * Return a string in ' quotes, doubling ' characters.
5013 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005014 */
5015 static char_u *
5016string_quote(str, function)
5017 char_u *str;
5018 int function;
5019{
5020 unsigned len = function ? 13 : 3;
5021 char_u *p, *r, *s;
5022
5023 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005025 ++len;
5026 s = r = alloc(len);
5027 if (r != NULL)
5028 {
5029 if (function)
5030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005032 r += 10;
5033 }
5034 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 *r++ = '\'';
5036 for (p = str; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 if (*p == '\'')
5039 *r++ = '\'';
5040 MB_COPY_CHAR(p, r);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 if (function)
5044 *r++ = ')';
5045 *r++ = NUL;
5046 }
5047 return s;
5048}
5049
5050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * Get the value of an environment variable.
5052 * "arg" is pointing to the '$'. It is advanced to after the name.
5053 * If the environment variable was not set, silently assume it is empty.
5054 * Always return OK.
5055 */
5056 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 int evaluate;
5061{
5062 char_u *string = NULL;
5063 int len;
5064 int cc;
5065 char_u *name;
5066
5067 ++*arg;
5068 name = *arg;
5069 len = get_env_len(arg);
5070 if (evaluate)
5071 {
5072 if (len != 0)
5073 {
5074 cc = name[len];
5075 name[len] = NUL;
5076 /* first try mch_getenv(), fast for normal environment vars */
5077 string = mch_getenv(name);
5078 if (string != NULL && *string != NUL)
5079 string = vim_strsave(string);
5080 else
5081 {
5082 /* next try expanding things like $VIM and ${HOME} */
5083 string = expand_env_save(name - 1);
5084 if (string != NULL && *string == '$')
5085 {
5086 vim_free(string);
5087 string = NULL;
5088 }
5089 }
5090 name[len] = cc;
5091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005092 rettv->v_type = VAR_STRING;
5093 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095
5096 return OK;
5097}
5098
5099/*
5100 * Array with names and number of arguments of all internal functions
5101 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5102 */
5103static struct fst
5104{
5105 char *f_name; /* function name */
5106 char f_min_argc; /* minimal number of arguments */
5107 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 void (*f_func) __ARGS((typeval *args, typeval *rvar));
5109 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110} functions[] =
5111{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005112 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {"append", 2, 2, f_append},
5114 {"argc", 0, 0, f_argc},
5115 {"argidx", 0, 0, f_argidx},
5116 {"argv", 1, 1, f_argv},
5117 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005118 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {"bufexists", 1, 1, f_bufexists},
5120 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5121 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5122 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5123 {"buflisted", 1, 1, f_buflisted},
5124 {"bufloaded", 1, 1, f_bufloaded},
5125 {"bufname", 1, 1, f_bufname},
5126 {"bufnr", 1, 1, f_bufnr},
5127 {"bufwinnr", 1, 1, f_bufwinnr},
5128 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005129 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005130 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {"char2nr", 1, 1, f_char2nr},
5132 {"cindent", 1, 1, f_cindent},
5133 {"col", 1, 1, f_col},
5134 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {"cscope_connection",0,3, f_cscope_connection},
5138 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {"delete", 1, 1, f_delete},
5141 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005142 {"diff_filler", 1, 1, f_diff_filler},
5143 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005144 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005146 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {"eventhandler", 0, 0, f_eventhandler},
5148 {"executable", 1, 1, f_executable},
5149 {"exists", 1, 1, f_exists},
5150 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005151 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5153 {"filereadable", 1, 1, f_filereadable},
5154 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005155 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005156 {"finddir", 1, 3, f_finddir},
5157 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {"fnamemodify", 2, 2, f_fnamemodify},
5159 {"foldclosed", 1, 1, f_foldclosed},
5160 {"foldclosedend", 1, 1, f_foldclosedend},
5161 {"foldlevel", 1, 1, f_foldlevel},
5162 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005163 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005166 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {"getbufvar", 2, 2, f_getbufvar},
5168 {"getchar", 0, 1, f_getchar},
5169 {"getcharmod", 0, 0, f_getcharmod},
5170 {"getcmdline", 0, 0, f_getcmdline},
5171 {"getcmdpos", 0, 0, f_getcmdpos},
5172 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005173 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005174 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {"getfsize", 1, 1, f_getfsize},
5176 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005177 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005178 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {"getreg", 0, 1, f_getreg},
5180 {"getregtype", 0, 1, f_getregtype},
5181 {"getwinposx", 0, 0, f_getwinposx},
5182 {"getwinposy", 0, 0, f_getwinposy},
5183 {"getwinvar", 2, 2, f_getwinvar},
5184 {"glob", 1, 1, f_glob},
5185 {"globpath", 2, 2, f_globpath},
5186 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005187 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {"hasmapto", 1, 2, f_hasmapto},
5189 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5190 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5191 {"histadd", 2, 2, f_histadd},
5192 {"histdel", 1, 2, f_histdel},
5193 {"histget", 1, 2, f_histget},
5194 {"histnr", 1, 1, f_histnr},
5195 {"hlID", 1, 1, f_hlID},
5196 {"hlexists", 1, 1, f_hlexists},
5197 {"hostname", 0, 0, f_hostname},
5198 {"iconv", 3, 3, f_iconv},
5199 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005200 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {"input", 1, 2, f_input},
5202 {"inputdialog", 1, 3, f_inputdialog},
5203 {"inputrestore", 0, 0, f_inputrestore},
5204 {"inputsave", 0, 0, f_inputsave},
5205 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005209 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {"libcall", 3, 3, f_libcall},
5214 {"libcallnr", 3, 3, f_libcallnr},
5215 {"line", 1, 1, f_line},
5216 {"line2byte", 1, 1, f_line2byte},
5217 {"lispindent", 1, 1, f_lispindent},
5218 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005219 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {"maparg", 1, 2, f_maparg},
5221 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005222 {"match", 2, 4, f_match},
5223 {"matchend", 2, 4, f_matchend},
5224 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005225 {"max", 1, 1, f_max},
5226 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {"mode", 0, 0, f_mode},
5228 {"nextnonblank", 1, 1, f_nextnonblank},
5229 {"nr2char", 1, 1, f_nr2char},
5230 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {"remote_expr", 2, 3, f_remote_expr},
5233 {"remote_foreground", 1, 1, f_remote_foreground},
5234 {"remote_peek", 1, 2, f_remote_peek},
5235 {"remote_read", 1, 1, f_remote_read},
5236 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005237 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005239 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005241 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {"search", 1, 2, f_search},
5243 {"searchpair", 3, 5, f_searchpair},
5244 {"server2client", 2, 2, f_server2client},
5245 {"serverlist", 0, 0, f_serverlist},
5246 {"setbufvar", 3, 3, f_setbufvar},
5247 {"setcmdpos", 1, 1, f_setcmdpos},
5248 {"setline", 2, 2, f_setline},
5249 {"setreg", 2, 3, f_setreg},
5250 {"setwinvar", 3, 3, f_setwinvar},
5251 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005252 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005253 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#ifdef HAVE_STRFTIME
5255 {"strftime", 1, 2, f_strftime},
5256#endif
5257 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {"strlen", 1, 1, f_strlen},
5260 {"strpart", 2, 3, f_strpart},
5261 {"strridx", 2, 2, f_strridx},
5262 {"strtrans", 1, 1, f_strtrans},
5263 {"submatch", 1, 1, f_submatch},
5264 {"substitute", 4, 4, f_substitute},
5265 {"synID", 3, 3, f_synID},
5266 {"synIDattr", 2, 3, f_synIDattr},
5267 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005268 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {"tempname", 0, 0, f_tempname},
5270 {"tolower", 1, 1, f_tolower},
5271 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005272 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {"virtcol", 1, 1, f_virtcol},
5276 {"visualmode", 0, 1, f_visualmode},
5277 {"winbufnr", 1, 1, f_winbufnr},
5278 {"wincol", 0, 0, f_wincol},
5279 {"winheight", 1, 1, f_winheight},
5280 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005281 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {"winrestcmd", 0, 0, f_winrestcmd},
5283 {"winwidth", 1, 1, f_winwidth},
5284};
5285
5286#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5287
5288/*
5289 * Function given to ExpandGeneric() to obtain the list of internal
5290 * or user defined function names.
5291 */
5292 char_u *
5293get_function_name(xp, idx)
5294 expand_T *xp;
5295 int idx;
5296{
5297 static int intidx = -1;
5298 char_u *name;
5299
5300 if (idx == 0)
5301 intidx = -1;
5302 if (intidx < 0)
5303 {
5304 name = get_user_func_name(xp, idx);
5305 if (name != NULL)
5306 return name;
5307 }
5308 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5309 {
5310 STRCPY(IObuff, functions[intidx].f_name);
5311 STRCAT(IObuff, "(");
5312 if (functions[intidx].f_max_argc == 0)
5313 STRCAT(IObuff, ")");
5314 return IObuff;
5315 }
5316
5317 return NULL;
5318}
5319
5320/*
5321 * Function given to ExpandGeneric() to obtain the list of internal or
5322 * user defined variable or function names.
5323 */
5324/*ARGSUSED*/
5325 char_u *
5326get_expr_name(xp, idx)
5327 expand_T *xp;
5328 int idx;
5329{
5330 static int intidx = -1;
5331 char_u *name;
5332
5333 if (idx == 0)
5334 intidx = -1;
5335 if (intidx < 0)
5336 {
5337 name = get_function_name(xp, idx);
5338 if (name != NULL)
5339 return name;
5340 }
5341 return get_user_var_name(xp, ++intidx);
5342}
5343
5344#endif /* FEAT_CMDL_COMPL */
5345
5346/*
5347 * Find internal function in table above.
5348 * Return index, or -1 if not found
5349 */
5350 static int
5351find_internal_func(name)
5352 char_u *name; /* name of the function */
5353{
5354 int first = 0;
5355 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5356 int cmp;
5357 int x;
5358
5359 /*
5360 * Find the function name in the table. Binary search.
5361 */
5362 while (first <= last)
5363 {
5364 x = first + ((unsigned)(last - first) >> 1);
5365 cmp = STRCMP(name, functions[x].f_name);
5366 if (cmp < 0)
5367 last = x - 1;
5368 else if (cmp > 0)
5369 first = x + 1;
5370 else
5371 return x;
5372 }
5373 return -1;
5374}
5375
5376/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5378 * name it contains, otherwise return "name".
5379 */
5380 static char_u *
5381deref_func_name(name, lenp)
5382 char_u *name;
5383 int *lenp;
5384{
5385 VAR v;
5386 int cc;
5387
5388 cc = name[*lenp];
5389 name[*lenp] = NUL;
5390 v = find_var(name, FALSE);
5391 name[*lenp] = cc;
5392 if (v != NULL && v->tv.v_type == VAR_FUNC)
5393 {
5394 if (v->tv.vval.v_string == NULL)
5395 {
5396 *lenp = 0;
5397 return (char_u *)""; /* just in case */
5398 }
5399 *lenp = STRLEN(v->tv.vval.v_string);
5400 return v->tv.vval.v_string;
5401 }
5402
5403 return name;
5404}
5405
5406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 * Allocate a variable for the result of a function.
5408 * Return OK or FAIL.
5409 */
5410 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005411get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5412 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 char_u *name; /* name of the function */
5414 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 char_u **arg; /* argument, pointing to the '(' */
5417 linenr_T firstline; /* first line of range */
5418 linenr_T lastline; /* last line of range */
5419 int *doesrange; /* return: function handled range */
5420 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005421 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 char_u *argp;
5424 int ret = OK;
5425#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 int argcount = 0; /* number of arguments found */
5428
5429 /*
5430 * Get the arguments.
5431 */
5432 argp = *arg;
5433 while (argcount < MAX_FUNC_ARGS)
5434 {
5435 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5436 if (*argp == ')' || *argp == ',' || *argp == NUL)
5437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5439 {
5440 ret = FAIL;
5441 break;
5442 }
5443 ++argcount;
5444 if (*argp != ',')
5445 break;
5446 }
5447 if (*argp == ')')
5448 ++argp;
5449 else
5450 ret = FAIL;
5451
5452 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005454 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 else if (!aborting())
5456 EMSG2(_("E116: Invalid arguments for function %s"), name);
5457
5458 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461 *arg = skipwhite(argp);
5462 return ret;
5463}
5464
5465
5466/*
5467 * Call a function with its resolved parameters
5468 * Return OK or FAIL.
5469 */
5470 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005472 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 char_u *name; /* name of the function */
5474 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 linenr_T firstline; /* first line of range */
5479 linenr_T lastline; /* last line of range */
5480 int *doesrange; /* return: function handled range */
5481 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005482 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485#define ERROR_UNKNOWN 0
5486#define ERROR_TOOMANY 1
5487#define ERROR_TOOFEW 2
5488#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00005489#define ERROR_DICT 4
5490#define ERROR_NONE 5
5491#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int error = ERROR_NONE;
5493 int i;
5494 int llen;
5495 ufunc_T *fp;
5496 int cc;
5497#define FLEN_FIXED 40
5498 char_u fname_buf[FLEN_FIXED + 1];
5499 char_u *fname;
5500
5501 /*
5502 * In a script change <SID>name() and s:name() to K_SNR 123_name().
5503 * Change <SNR>123_name() to K_SNR 123_name().
5504 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
5505 */
5506 cc = name[len];
5507 name[len] = NUL;
5508 llen = eval_fname_script(name);
5509 if (llen > 0)
5510 {
5511 fname_buf[0] = K_SPECIAL;
5512 fname_buf[1] = KS_EXTRA;
5513 fname_buf[2] = (int)KE_SNR;
5514 i = 3;
5515 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
5516 {
5517 if (current_SID <= 0)
5518 error = ERROR_SCRIPT;
5519 else
5520 {
5521 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
5522 i = (int)STRLEN(fname_buf);
5523 }
5524 }
5525 if (i + STRLEN(name + llen) < FLEN_FIXED)
5526 {
5527 STRCPY(fname_buf + i, name + llen);
5528 fname = fname_buf;
5529 }
5530 else
5531 {
5532 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
5533 if (fname == NULL)
5534 error = ERROR_OTHER;
5535 else
5536 {
5537 mch_memmove(fname, fname_buf, (size_t)i);
5538 STRCPY(fname + i, name + llen);
5539 }
5540 }
5541 }
5542 else
5543 fname = name;
5544
5545 *doesrange = FALSE;
5546
5547
5548 /* execute the function if no errors detected and executing */
5549 if (evaluate && error == ERROR_NONE)
5550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 error = ERROR_UNKNOWN;
5553
5554 if (!ASCII_ISLOWER(fname[0]))
5555 {
5556 /*
5557 * User defined function.
5558 */
5559 fp = find_func(fname);
5560#ifdef FEAT_AUTOCMD
5561 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
5562 fname, fname, TRUE, NULL)
5563#ifdef FEAT_EVAL
5564 && !aborting()
5565#endif
5566 )
5567 {
5568 /* executed an autocommand, search for function again */
5569 fp = find_func(fname);
5570 }
5571#endif
5572 if (fp != NULL)
5573 {
5574 if (fp->flags & FC_RANGE)
5575 *doesrange = TRUE;
5576 if (argcount < fp->args.ga_len)
5577 error = ERROR_TOOFEW;
5578 else if (!fp->varargs && argcount > fp->args.ga_len)
5579 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005580 else if ((fp->flags & FC_DICT) && selfdict == NULL)
5581 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 else
5583 {
5584 /*
5585 * Call the user function.
5586 * Save and restore search patterns, script variables and
5587 * redo buffer.
5588 */
5589 save_search_patterns();
5590 saveRedobuff();
5591 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005593 firstline, lastline,
5594 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 --fp->calls;
5596 restoreRedobuff();
5597 restore_search_patterns();
5598 error = ERROR_NONE;
5599 }
5600 }
5601 }
5602 else
5603 {
5604 /*
5605 * Find the function name in the table, call its implementation.
5606 */
5607 i = find_internal_func(fname);
5608 if (i >= 0)
5609 {
5610 if (argcount < functions[i].f_min_argc)
5611 error = ERROR_TOOFEW;
5612 else if (argcount > functions[i].f_max_argc)
5613 error = ERROR_TOOMANY;
5614 else
5615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 error = ERROR_NONE;
5619 }
5620 }
5621 }
5622 /*
5623 * The function call (or "FuncUndefined" autocommand sequence) might
5624 * have been aborted by an error, an interrupt, or an explicitly thrown
5625 * exception that has not been caught so far. This situation can be
5626 * tested for by calling aborting(). For an error in an internal
5627 * function or for the "E132" error in call_user_func(), however, the
5628 * throw point at which the "force_abort" flag (temporarily reset by
5629 * emsg()) is normally updated has not been reached yet. We need to
5630 * update that flag first to make aborting() reliable.
5631 */
5632 update_force_abort();
5633 }
5634 if (error == ERROR_NONE)
5635 ret = OK;
5636
5637 /*
5638 * Report an error unless the argument evaluation or function call has been
5639 * cancelled due to an aborting error, an interrupt, or an exception.
5640 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 if (!aborting())
5642 {
5643 switch (error)
5644 {
5645 case ERROR_UNKNOWN:
5646 EMSG2(_("E117: Unknown function: %s"), name);
5647 break;
5648 case ERROR_TOOMANY:
5649 EMSG2(_(e_toomanyarg), name);
5650 break;
5651 case ERROR_TOOFEW:
5652 EMSG2(_("E119: Not enough arguments for function: %s"),
5653 name);
5654 break;
5655 case ERROR_SCRIPT:
5656 EMSG2(_("E120: Using <SID> not in a script context: %s"),
5657 name);
5658 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005659 case ERROR_DICT:
5660 EMSG2(_("E999: Calling dict function without Dictionary: %s"),
5661 name);
5662 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 }
5664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 name[len] = cc;
5667 if (fname != name && fname != fname_buf)
5668 vim_free(fname);
5669
5670 return ret;
5671}
5672
5673/*********************************************
5674 * Implementation of the built-in functions
5675 */
5676
5677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005678 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 */
5680 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005681f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005685 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005690 l = argvars[0].vval.v_list;
5691 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 }
5694 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00005695 EMSG(_(e_listreq));
5696}
5697
5698/*
5699 * "append(lnum, string/list)" function
5700 */
5701 static void
5702f_append(argvars, rettv)
5703 typeval *argvars;
5704 typeval *rettv;
5705{
5706 long lnum;
5707 listvar *l = NULL;
5708 listitem *li = NULL;
5709 typeval *tv;
5710 long added = 0;
5711
5712 rettv->vval.v_number = 1; /* Default: Failed */
5713 lnum = get_tv_lnum(argvars);
5714 if (lnum >= 0
5715 && lnum <= curbuf->b_ml.ml_line_count
5716 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005718 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005720 l = argvars[1].vval.v_list;
5721 if (l == NULL)
5722 return;
5723 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00005725 for (;;)
5726 {
5727 if (l == NULL)
5728 tv = &argvars[1]; /* append a string */
5729 else if (li == NULL)
5730 break; /* end of list */
5731 else
5732 tv = &li->li_tv; /* append item from list */
5733 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
5734 ++added;
5735 if (l == NULL)
5736 break;
5737 li = li->li_next;
5738 }
5739
5740 appended_lines_mark(lnum, added);
5741 if (curwin->w_cursor.lnum > lnum)
5742 curwin->w_cursor.lnum += added;
5743 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745}
5746
5747/*
5748 * "argc()" function
5749 */
5750/* ARGSUSED */
5751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758
5759/*
5760 * "argidx()" function
5761 */
5762/* ARGSUSED */
5763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005764f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005766 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005768 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769}
5770
5771/*
5772 * "argv(nr)" function
5773 */
5774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 int idx;
5780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005783 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005785 rettv->vval.v_string = NULL;
5786 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787}
5788
5789/*
5790 * "browse(save, title, initdir, default)" function
5791 */
5792/* ARGSUSED */
5793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005794f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798#ifdef FEAT_BROWSE
5799 int save;
5800 char_u *title;
5801 char_u *initdir;
5802 char_u *defname;
5803 char_u buf[NUMBUFLEN];
5804 char_u buf2[NUMBUFLEN];
5805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005806 save = get_tv_number(&argvars[0]);
5807 title = get_tv_string(&argvars[1]);
5808 initdir = get_tv_string_buf(&argvars[2], buf);
5809 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005811 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005812 do_browse(save ? BROWSE_SAVE : 0,
5813 title, defname, NULL, initdir, NULL, curbuf);
5814#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005817 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005818}
5819
5820/*
5821 * "browsedir(title, initdir)" function
5822 */
5823/* ARGSUSED */
5824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005827 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005828{
5829#ifdef FEAT_BROWSE
5830 char_u *title;
5831 char_u *initdir;
5832 char_u buf[NUMBUFLEN];
5833
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005834 title = get_tv_string(&argvars[0]);
5835 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005837 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005838 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843}
5844
Bram Moolenaar0d660222005-01-07 21:51:51 +00005845static buf_T *find_buffer __ARGS((typeval *avar));
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847/*
5848 * Find a buffer by number or exact name.
5849 */
5850 static buf_T *
5851find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
5854 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 if (avar->v_type == VAR_NUMBER)
5857 buf = buflist_findnr((int)avar->vval.v_number);
5858 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005861 if (buf == NULL)
5862 {
5863 /* No full path name match, try a match with a URL or a "nofile"
5864 * buffer, these don't use the full path. */
5865 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5866 if (buf->b_fname != NULL
5867 && (path_with_url(buf->b_fname)
5868#ifdef FEAT_QUICKFIX
5869 || bt_nofile(buf)
5870#endif
5871 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005873 break;
5874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 }
5876 return buf;
5877}
5878
5879/*
5880 * "bufexists(expr)" function
5881 */
5882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005883f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005885 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005887 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888}
5889
5890/*
5891 * "buflisted(expr)" function
5892 */
5893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005894f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005896 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 buf_T *buf;
5899
5900 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005901 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902}
5903
5904/*
5905 * "bufloaded(expr)" function
5906 */
5907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 buf_T *buf;
5913
5914 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005915 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916}
5917
Bram Moolenaar0d660222005-01-07 21:51:51 +00005918static buf_T *get_buf_tv __ARGS((typeval *tv));
5919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920/*
5921 * Get buffer by number or pattern.
5922 */
5923 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005924get_buf_tv(tv)
5925 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005927 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 int save_magic;
5929 char_u *save_cpo;
5930 buf_T *buf;
5931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005932 if (tv->v_type == VAR_NUMBER)
5933 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 if (name == NULL || *name == NUL)
5935 return curbuf;
5936 if (name[0] == '$' && name[1] == NUL)
5937 return lastbuf;
5938
5939 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
5940 save_magic = p_magic;
5941 p_magic = TRUE;
5942 save_cpo = p_cpo;
5943 p_cpo = (char_u *)"";
5944
5945 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
5946 TRUE, FALSE));
5947
5948 p_magic = save_magic;
5949 p_cpo = save_cpo;
5950
5951 /* If not found, try expanding the name, like done for bufexists(). */
5952 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005953 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
5955 return buf;
5956}
5957
5958/*
5959 * "bufname(expr)" function
5960 */
5961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
5966 buf_T *buf;
5967
5968 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 buf = get_buf_tv(&argvars[0]);
5970 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005972 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 --emsg_off;
5976}
5977
5978/*
5979 * "bufnr(expr)" function
5980 */
5981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005982f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005984 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 buf_T *buf;
5987
5988 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005991 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 --emsg_off;
5995}
5996
5997/*
5998 * "bufwinnr(nr)" function
5999 */
6000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006001f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006003 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005#ifdef FEAT_WINDOWS
6006 win_T *wp;
6007 int winnr = 0;
6008#endif
6009 buf_T *buf;
6010
6011 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006012 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#ifdef FEAT_WINDOWS
6014 for (wp = firstwin; wp; wp = wp->w_next)
6015 {
6016 ++winnr;
6017 if (wp->w_buffer == buf)
6018 break;
6019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006020 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006022 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023#endif
6024 --emsg_off;
6025}
6026
6027/*
6028 * "byte2line(byte)" function
6029 */
6030/*ARGSUSED*/
6031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006032f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006034 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038#else
6039 long boff = 0;
6040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006041 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006045 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 (linenr_T)0, &boff);
6047#endif
6048}
6049
6050/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006051 * "byteidx()" function
6052 */
6053/*ARGSUSED*/
6054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006057 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006058{
6059#ifdef FEAT_MBYTE
6060 char_u *t;
6061#endif
6062 char_u *str;
6063 long idx;
6064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006065 str = get_tv_string(&argvars[0]);
6066 idx = get_tv_number(&argvars[1]);
6067 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006068 if (idx < 0)
6069 return;
6070
6071#ifdef FEAT_MBYTE
6072 t = str;
6073 for ( ; idx > 0; idx--)
6074 {
6075 if (*t == NUL) /* EOL reached */
6076 return;
6077 t += mb_ptr2len_check(t);
6078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006079 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006080#else
6081 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006082 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006083#endif
6084}
6085
6086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * "call(func, arglist)" function
6088 */
6089 static void
6090f_call(argvars, rettv)
6091 typeval *argvars;
6092 typeval *rettv;
6093{
6094 char_u *func;
6095 typeval argv[MAX_FUNC_ARGS];
6096 int argc = 0;
6097 listitem *item;
6098 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 dictvar *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100
6101 rettv->vval.v_number = 0;
6102 if (argvars[1].v_type != VAR_LIST)
6103 {
6104 EMSG(_(e_listreq));
6105 return;
6106 }
6107 if (argvars[1].vval.v_list == NULL)
6108 return;
6109
6110 if (argvars[0].v_type == VAR_FUNC)
6111 func = argvars[0].vval.v_string;
6112 else
6113 func = get_tv_string(&argvars[0]);
6114
Bram Moolenaare9a41262005-01-15 22:18:47 +00006115 if (argvars[2].v_type != VAR_UNKNOWN)
6116 {
6117 if (argvars[2].v_type != VAR_DICT)
6118 {
6119 EMSG(_(e_dictreq));
6120 return;
6121 }
6122 selfdict = argvars[2].vval.v_dict;
6123 }
6124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6126 item = item->li_next)
6127 {
6128 if (argc == MAX_FUNC_ARGS)
6129 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006130 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 break;
6132 }
6133 /* Make a copy of each argument (is this really needed?) */
6134 copy_tv(&item->li_tv, &argv[argc++]);
6135 }
6136
6137 if (item == NULL)
6138 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6140 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006141
6142 /* Free the arguments. */
6143 while (argc > 0)
6144 clear_tv(&argv[--argc]);
6145}
6146
6147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 * "char2nr(string)" function
6149 */
6150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006151f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155#ifdef FEAT_MBYTE
6156 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157 rettv->vval.v_number =
6158 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 else
6160#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
6163
6164/*
6165 * "cindent(lnum)" function
6166 */
6167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006168f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006170 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172#ifdef FEAT_CINDENT
6173 pos_T pos;
6174 linenr_T lnum;
6175
6176 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6179 {
6180 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 curwin->w_cursor = pos;
6183 }
6184 else
6185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006186 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187}
6188
6189/*
6190 * "col(string)" function
6191 */
6192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006195 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 colnr_T col = 0;
6198 pos_T *fp;
6199
6200 fp = var2fpos(&argvars[0], FALSE);
6201 if (fp != NULL)
6202 {
6203 if (fp->col == MAXCOL)
6204 {
6205 /* '> can be MAXCOL, get the length of the line then */
6206 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6207 col = STRLEN(ml_get(fp->lnum)) + 1;
6208 else
6209 col = MAXCOL;
6210 }
6211 else
6212 {
6213 col = fp->col + 1;
6214#ifdef FEAT_VIRTUALEDIT
6215 /* col(".") when the cursor is on the NUL at the end of the line
6216 * because of "coladd" can be seen as an extra column. */
6217 if (virtual_active() && fp == &curwin->w_cursor)
6218 {
6219 char_u *p = ml_get_cursor();
6220
6221 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6222 curwin->w_virtcol - curwin->w_cursor.coladd))
6223 {
6224# ifdef FEAT_MBYTE
6225 int l;
6226
6227 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6228 col += l;
6229# else
6230 if (*p != NUL && p[1] == NUL)
6231 ++col;
6232# endif
6233 }
6234 }
6235#endif
6236 }
6237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006238 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239}
6240
6241/*
6242 * "confirm(message, buttons[, default [, type]])" function
6243 */
6244/*ARGSUSED*/
6245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006246f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006248 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6251 char_u *message;
6252 char_u *buttons = NULL;
6253 char_u buf[NUMBUFLEN];
6254 char_u buf2[NUMBUFLEN];
6255 int def = 1;
6256 int type = VIM_GENERIC;
6257 int c;
6258
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006259 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006262 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006265 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
6268 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006269 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 switch (TOUPPER_ASC(c))
6271 {
6272 case 'E': type = VIM_ERROR; break;
6273 case 'Q': type = VIM_QUESTION; break;
6274 case 'I': type = VIM_INFO; break;
6275 case 'W': type = VIM_WARNING; break;
6276 case 'G': type = VIM_GENERIC; break;
6277 }
6278 }
6279 }
6280 }
6281
6282 if (buttons == NULL || *buttons == NUL)
6283 buttons = (char_u *)_("&Ok");
6284
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006285 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 def, NULL);
6287#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006288 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289#endif
6290}
6291
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292/*
6293 * "copy()" function
6294 */
6295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006296f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006298 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 * "count()" function
6305 */
6306 static void
6307f_count(argvars, rettv)
6308 typeval *argvars;
6309 typeval *rettv;
6310{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 long n = 0;
6312 int ic = FALSE;
6313
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006315 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316 listitem *li;
6317 listvar *l;
6318 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006319
Bram Moolenaare9a41262005-01-15 22:18:47 +00006320 if ((l = argvars[0].vval.v_list) != NULL)
6321 {
6322 li = l->lv_first;
6323 if (argvars[2].v_type != VAR_UNKNOWN)
6324 {
6325 ic = get_tv_number(&argvars[2]);
6326 if (argvars[3].v_type != VAR_UNKNOWN)
6327 {
6328 idx = get_tv_number(&argvars[3]);
6329 li = list_find(l, idx);
6330 if (li == NULL)
6331 EMSGN(_(e_listidx), idx);
6332 }
6333 }
6334
6335 for ( ; li != NULL; li = li->li_next)
6336 if (tv_equal(&li->li_tv, &argvars[1], ic))
6337 ++n;
6338 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006340 else if (argvars[0].v_type == VAR_DICT)
6341 {
6342 if (argvars[0].vval.v_dict != NULL)
6343 {
6344 dictitem *di;
6345
6346 di = argvars[0].vval.v_dict->dv_first;
6347 if (argvars[2].v_type != VAR_UNKNOWN)
6348 {
6349 ic = get_tv_number(&argvars[2]);
6350 if (argvars[3].v_type != VAR_UNKNOWN)
6351 EMSG(_(e_invarg));
6352 }
6353
6354 for ( ; di != NULL; di = di->di_next)
6355 if (tv_equal(&di->di_tv, &argvars[1], ic))
6356 ++n;
6357 }
6358 }
6359 else
6360 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006361 rettv->vval.v_number = n;
6362}
6363
6364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6366 *
6367 * Checks the existence of a cscope connection.
6368 */
6369/*ARGSUSED*/
6370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006371f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006373 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375#ifdef FEAT_CSCOPE
6376 int num = 0;
6377 char_u *dbpath = NULL;
6378 char_u *prepend = NULL;
6379 char_u buf[NUMBUFLEN];
6380
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 if (argvars[0].v_type != VAR_UNKNOWN
6382 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 num = (int)get_tv_number(&argvars[0]);
6385 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006387 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
6389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006390 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006392 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#endif
6394}
6395
6396/*
6397 * "cursor(lnum, col)" function
6398 *
6399 * Moves the cursor to the specified line and column
6400 */
6401/*ARGSUSED*/
6402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006403f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006405 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 long line, col;
6408
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006409 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (line > 0)
6411 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006412 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (col > 0)
6414 curwin->w_cursor.col = col - 1;
6415#ifdef FEAT_VIRTUALEDIT
6416 curwin->w_cursor.coladd = 0;
6417#endif
6418
6419 /* Make sure the cursor is in a valid position. */
6420 check_cursor();
6421#ifdef FEAT_MBYTE
6422 /* Correct cursor for multi-byte character. */
6423 if (has_mbyte)
6424 mb_adjust_cursor();
6425#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006426
6427 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428}
6429
6430/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 */
6433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006434f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006436 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439}
6440
6441/*
6442 * "delete()" function
6443 */
6444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006445f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006447 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006450 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006452 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453}
6454
6455/*
6456 * "did_filetype()" function
6457 */
6458/*ARGSUSED*/
6459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006465 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468#endif
6469}
6470
6471/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00006472 * "diff_filler()" function
6473 */
6474/*ARGSUSED*/
6475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006478 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006479{
6480#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00006482#endif
6483}
6484
6485/*
6486 * "diff_hlID()" function
6487 */
6488/*ARGSUSED*/
6489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006493{
6494#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00006496 static linenr_T prev_lnum = 0;
6497 static int changedtick = 0;
6498 static int fnum = 0;
6499 static int change_start = 0;
6500 static int change_end = 0;
6501 static enum hlf_value hlID = 0;
6502 int filler_lines;
6503 int col;
6504
6505 if (lnum != prev_lnum
6506 || changedtick != curbuf->b_changedtick
6507 || fnum != curbuf->b_fnum)
6508 {
6509 /* New line, buffer, change: need to get the values. */
6510 filler_lines = diff_check(curwin, lnum);
6511 if (filler_lines < 0)
6512 {
6513 if (filler_lines == -1)
6514 {
6515 change_start = MAXCOL;
6516 change_end = -1;
6517 if (diff_find_change(curwin, lnum, &change_start, &change_end))
6518 hlID = HLF_ADD; /* added line */
6519 else
6520 hlID = HLF_CHD; /* changed line */
6521 }
6522 else
6523 hlID = HLF_ADD; /* added line */
6524 }
6525 else
6526 hlID = (enum hlf_value)0;
6527 prev_lnum = lnum;
6528 changedtick = curbuf->b_changedtick;
6529 fnum = curbuf->b_fnum;
6530 }
6531
6532 if (hlID == HLF_CHD || hlID == HLF_TXD)
6533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006534 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006535 if (col >= change_start && col <= change_end)
6536 hlID = HLF_TXD; /* changed text */
6537 else
6538 hlID = HLF_CHD; /* changed line */
6539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006540 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006541#endif
6542}
6543
6544/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006545 * "empty({expr})" function
6546 */
6547 static void
6548f_empty(argvars, rettv)
6549 typeval *argvars;
6550 typeval *rettv;
6551{
6552 int n;
6553
6554 switch (argvars[0].v_type)
6555 {
6556 case VAR_STRING:
6557 case VAR_FUNC:
6558 n = argvars[0].vval.v_string == NULL
6559 || *argvars[0].vval.v_string == NUL;
6560 break;
6561 case VAR_NUMBER:
6562 n = argvars[0].vval.v_number == 0;
6563 break;
6564 case VAR_LIST:
6565 n = argvars[0].vval.v_list == NULL
6566 || argvars[0].vval.v_list->lv_first == NULL;
6567 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006568 case VAR_DICT:
6569 n = argvars[0].vval.v_dict == NULL
6570 || argvars[0].vval.v_dict->dv_first == NULL;
6571 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006572 default:
6573 EMSG2(_(e_intern2), "f_empty()");
6574 n = 0;
6575 }
6576
6577 rettv->vval.v_number = n;
6578}
6579
6580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 * "escape({string}, {chars})" function
6582 */
6583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006584f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 char_u buf[NUMBUFLEN];
6589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590 rettv->vval.v_string =
6591 vim_strsave_escaped(get_tv_string(&argvars[0]),
6592 get_tv_string_buf(&argvars[1], buf));
6593 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
6596/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006597 * "eval()" function
6598 */
6599/*ARGSUSED*/
6600 static void
6601f_eval(argvars, rettv)
6602 typeval *argvars;
6603 typeval *rettv;
6604{
6605 char_u *s;
6606
6607 s = get_tv_string(&argvars[0]);
6608 s = skipwhite(s);
6609
6610 if (eval1(&s, rettv, TRUE) == FAIL)
6611 rettv->vval.v_number = 0;
6612 else if (*s != NUL)
6613 EMSG(_(e_trailing));
6614}
6615
6616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 * "eventhandler()" function
6618 */
6619/*ARGSUSED*/
6620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006621f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006623 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006625 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626}
6627
6628/*
6629 * "executable()" function
6630 */
6631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006634 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637}
6638
6639/*
6640 * "exists()" function
6641 */
6642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006643f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006645 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 char_u *p;
6648 char_u *name;
6649 int n = FALSE;
6650 int len = 0;
6651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006652 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (*p == '$') /* environment variable */
6654 {
6655 /* first try "normal" environment variables (fast) */
6656 if (mch_getenv(p + 1) != NULL)
6657 n = TRUE;
6658 else
6659 {
6660 /* try expanding things like $VIM and ${HOME} */
6661 p = expand_env_save(p);
6662 if (p != NULL && *p != '$')
6663 n = TRUE;
6664 vim_free(p);
6665 }
6666 }
6667 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006668 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 else if (*p == '*') /* internal or user defined function */
6670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 else if (*p == ':')
6674 {
6675 n = cmd_exists(p + 1);
6676 }
6677 else if (*p == '#')
6678 {
6679#ifdef FEAT_AUTOCMD
6680 name = p + 1;
6681 p = vim_strchr(name, '#');
6682 if (p != NULL)
6683 n = au_exists(name, p, p + 1);
6684 else
6685 n = au_exists(name, name + STRLEN(name), NULL);
6686#endif
6687 }
6688 else /* internal variable */
6689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 char_u *expr_start;
6691 char_u *expr_end;
6692 char_u *temp_string = NULL;
6693 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 name = p;
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006697 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (expr_start != NULL)
6699 {
6700 temp_string = make_expanded_name(name, expr_start, expr_end, s);
6701 if (temp_string != NULL)
6702 {
6703 len = STRLEN(temp_string);
6704 name = temp_string;
6705 }
6706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (len == 0)
6708 len = get_id_len(&p);
6709 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006710 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006715 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716}
6717
6718/*
6719 * "expand()" function
6720 */
6721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006722f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006723 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006724 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 char_u *s;
6727 int len;
6728 char_u *errormsg;
6729 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
6730 expand_T xpc;
6731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732 rettv->v_type = VAR_STRING;
6733 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (*s == '%' || *s == '#' || *s == '<')
6735 {
6736 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 --emsg_off;
6739 }
6740 else
6741 {
6742 /* When the optional second argument is non-zero, don't remove matches
6743 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 flags |= WILD_KEEP_ALL;
6746 ExpandInit(&xpc);
6747 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006748 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 ExpandCleanup(&xpc);
6750 }
6751}
6752
6753/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006754 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756 */
6757 static void
6758f_extend(argvars, rettv)
6759 typeval *argvars;
6760 typeval *rettv;
6761{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006763 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006764 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006765 listvar *l1, *l2;
6766 listitem *item;
6767 long before;
6768 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006769
Bram Moolenaare9a41262005-01-15 22:18:47 +00006770 l1 = argvars[0].vval.v_list;
6771 l2 = argvars[1].vval.v_list;
6772 if (l1 != NULL && l2 != NULL)
6773 {
6774 if (argvars[2].v_type != VAR_UNKNOWN)
6775 {
6776 n = before = get_tv_number(&argvars[2]);
6777 item = list_find_ext(l1, &n);
6778 if (n != 0)
6779 {
6780 EMSGN(_(e_listidx), before);
6781 return;
6782 }
6783 }
6784 else
6785 item = NULL;
6786 list_extend(l1, l2, item);
6787
6788 ++l1->lv_refcount;
6789 copy_tv(&argvars[0], rettv);
6790 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006791 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006792 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
6793 {
6794 dictvar *d1, *d2;
6795 dictitem *d1i, *d2i;
6796 char_u *action;
6797 int i;
6798
6799 d1 = argvars[0].vval.v_dict;
6800 d2 = argvars[1].vval.v_dict;
6801 if (d1 != NULL && d2 != NULL)
6802 {
6803 /* Check the third argument. */
6804 if (argvars[2].v_type != VAR_UNKNOWN)
6805 {
6806 static char *(av[]) = {"keep", "force", "error"};
6807
6808 action = get_tv_string(&argvars[2]);
6809 for (i = 0; i < 3; ++i)
6810 if (STRCMP(action, av[i]) == 0)
6811 break;
6812 if (i == 3)
6813 {
6814 EMSGN(_(e_invarg2), action);
6815 return;
6816 }
6817 }
6818 else
6819 action = (char_u *)"force";
6820
6821 /* Go over all entries in the second dict and add them to the
6822 * first dict. */
6823 for (d2i = d2->dv_first; d2i != NULL; d2i = d2i->di_next)
6824 {
6825 d1i = dict_find(d1, d2i->di_key, -1);
6826 if (d1i == NULL)
6827 {
6828 d1i = dictitem_copy(d2i);
6829 if (d1i != NULL)
6830 dict_add(d1, d1i);
6831 }
6832 else if (*action == 'e')
6833 {
6834 EMSG2(_("Key already exists: %s"), d2i->di_key);
6835 break;
6836 }
6837 else if (*action == 'f')
6838 {
6839 clear_tv(&d1i->di_tv);
6840 copy_tv(&d2i->di_tv, &d1i->di_tv);
6841 }
6842 }
6843
6844 ++d1->dv_refcount;
6845 copy_tv(&argvars[0], rettv);
6846 }
6847 }
6848 else
6849 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006850}
6851
6852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 * "filereadable()" function
6854 */
6855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006856f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 FILE *fd;
6861 char_u *p;
6862 int n;
6863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006864 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
6866 {
6867 n = TRUE;
6868 fclose(fd);
6869 }
6870 else
6871 n = FALSE;
6872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006873 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874}
6875
6876/*
6877 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
6878 * rights to write into.
6879 */
6880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 char_u *p;
6886 int retval = 0;
6887#if defined(UNIX) || defined(VMS)
6888 int perm = 0;
6889#endif
6890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006891 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892#if defined(UNIX) || defined(VMS)
6893 perm = mch_getperm(p);
6894#endif
6895#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6896 if (
6897# ifdef WIN3264
6898 mch_writable(p) &&
6899# else
6900# if defined(UNIX) || defined(VMS)
6901 (perm & 0222) &&
6902# endif
6903# endif
6904 mch_access((char *)p, W_OK) == 0
6905 )
6906#endif
6907 {
6908 ++retval;
6909 if (mch_isdir(p))
6910 ++retval;
6911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006912 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913}
6914
Bram Moolenaar0d660222005-01-07 21:51:51 +00006915static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006916
6917 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006918findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006919 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006921 int dir;
6922{
6923#ifdef FEAT_SEARCHPATH
6924 char_u *fname;
6925 char_u *fresult = NULL;
6926 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
6927 char_u *p;
6928 char_u pathbuf[NUMBUFLEN];
6929 int count = 1;
6930 int first = TRUE;
6931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006932 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006933
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006934 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006936 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006937 if (*p != NUL)
6938 path = p;
6939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006942 }
6943
6944 do
6945 {
6946 vim_free(fresult);
6947 fresult = find_file_in_path_option(first ? fname : NULL,
6948 first ? (int)STRLEN(fname) : 0,
6949 0, first, path, dir, NULL);
6950 first = FALSE;
6951 } while (--count > 0 && fresult != NULL);
6952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006953 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006954#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006955 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006957 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006958}
6959
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006960static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961static int filter_map_one __ARGS((typeval *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006962
6963/*
6964 * Implementation of map() and filter().
6965 */
6966 static void
6967filter_map(argvars, rettv, map)
6968 typeval *argvars;
6969 typeval *rettv;
6970 int map;
6971{
6972 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 char_u *expr;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006974 listitem *li, *nli;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 listvar *l = NULL;
6976 dictitem *di, **pdi;
6977 dictvar *d = NULL;
6978 typeval save_val;
6979 typeval save_key;
6980 int rem;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006981
6982 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006983 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006984 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985 if ((l = argvars[0].vval.v_list) == NULL)
6986 return;
6987 }
6988 else if (argvars[0].v_type == VAR_DICT)
6989 {
6990 if ((d = argvars[0].vval.v_dict) == NULL)
6991 return;
6992 }
6993 else
6994 {
6995 EMSG2(_(e_listdictarg), map ? "map()" : "filter()");
6996 return;
6997 }
6998
6999 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
7000 save_val = vimvars[VV_VAL].tv;
7001
7002 if (argvars[0].v_type == VAR_DICT)
7003 {
7004 save_key = vimvars[VV_KEY].tv;
7005 vimvars[VV_KEY].tv.v_type = VAR_STRING;
7006 pdi = &d->dv_first;
7007 for (di = d->dv_first; di != NULL; di = *pdi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007008 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007009 vimvars[VV_KEY].tv.vval.v_string = vim_strsave(di->di_key);
7010 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007011 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 if (!map && rem)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007013 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007014 *pdi = di->di_next;
7015 dictitem_free(di);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007016 }
7017 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007018 pdi = &di->di_next;
7019 clear_tv(&vimvars[VV_KEY].tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007020 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007021 clear_tv(&vimvars[VV_KEY].tv);
7022 vimvars[VV_KEY].tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 else
7025 {
7026 for (li = l->lv_first; li != NULL; li = nli)
7027 {
7028 nli = li->li_next;
7029 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7030 break;
7031 if (!map && rem)
7032 {
7033 clear_tv(&li->li_tv);
7034 list_getrem(l, li, li);
7035 }
7036 }
7037 }
7038
7039 clear_tv(&vimvars[VV_VAL].tv);
7040 vimvars[VV_VAL].tv = save_val;
7041
7042 copy_tv(&argvars[0], rettv);
7043}
7044
7045 static int
7046filter_map_one(tv, expr, map, remp)
7047 typeval *tv;
7048 char_u *expr;
7049 int map;
7050 int *remp;
7051{
7052 typeval rettv;
7053 char_u *s;
7054
7055 copy_tv(tv, &vimvars[VV_VAL].tv);
7056 s = expr;
7057 if (eval1(&s, &rettv, TRUE) == FAIL)
7058 return FAIL;
7059 if (*s != NUL) /* check for trailing chars after expr */
7060 {
7061 EMSG2(_(e_invexpr2), s);
7062 return FAIL;
7063 }
7064 if (map)
7065 {
7066 /* map(): replace the list item value */
7067 clear_tv(tv);
7068 *tv = rettv;
7069 }
7070 else
7071 {
7072 /* filter(): when expr is zero remove the item */
7073 *remp = (get_tv_number(&rettv) == 0);
7074 clear_tv(&rettv);
7075 }
7076 clear_tv(&vimvars[VV_VAL].tv);
7077 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007078}
7079
7080/*
7081 * "filter()" function
7082 */
7083 static void
7084f_filter(argvars, rettv)
7085 typeval *argvars;
7086 typeval *rettv;
7087{
7088 filter_map(argvars, rettv, FALSE);
7089}
7090
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007091/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007092 * "finddir({fname}[, {path}[, {count}]])" function
7093 */
7094 static void
7095f_finddir(argvars, rettv)
7096 typeval *argvars;
7097 typeval *rettv;
7098{
7099 findfilendir(argvars, rettv, TRUE);
7100}
7101
7102/*
7103 * "findfile({fname}[, {path}[, {count}]])" function
7104 */
7105 static void
7106f_findfile(argvars, rettv)
7107 typeval *argvars;
7108 typeval *rettv;
7109{
7110 findfilendir(argvars, rettv, FALSE);
7111}
7112
7113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 * "fnamemodify({fname}, {mods})" function
7115 */
7116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007117f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 char_u *fname;
7122 char_u *mods;
7123 int usedlen = 0;
7124 int len;
7125 char_u *fbuf = NULL;
7126 char_u buf[NUMBUFLEN];
7127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007128 fname = get_tv_string(&argvars[0]);
7129 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 len = (int)STRLEN(fname);
7131
7132 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7133
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007136 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007138 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 vim_free(fbuf);
7140}
7141
Bram Moolenaar0d660222005-01-07 21:51:51 +00007142static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
7144/*
7145 * "foldclosed()" function
7146 */
7147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007148foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007149 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007150 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 int end;
7152{
7153#ifdef FEAT_FOLDING
7154 linenr_T lnum;
7155 linenr_T first, last;
7156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7159 {
7160 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7161 {
7162 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007163 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007165 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 return;
7167 }
7168 }
7169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171}
7172
7173/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007174 * "foldclosed()" function
7175 */
7176 static void
7177f_foldclosed(argvars, rettv)
7178 typeval *argvars;
7179 typeval *rettv;
7180{
7181 foldclosed_both(argvars, rettv, FALSE);
7182}
7183
7184/*
7185 * "foldclosedend()" function
7186 */
7187 static void
7188f_foldclosedend(argvars, rettv)
7189 typeval *argvars;
7190 typeval *rettv;
7191{
7192 foldclosed_both(argvars, rettv, TRUE);
7193}
7194
7195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 * "foldlevel()" function
7197 */
7198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007199f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007200 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007201 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203#ifdef FEAT_FOLDING
7204 linenr_T lnum;
7205
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007206 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007208 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 else
7210#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212}
7213
7214/*
7215 * "foldtext()" function
7216 */
7217/*ARGSUSED*/
7218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007219f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007220 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223#ifdef FEAT_FOLDING
7224 linenr_T lnum;
7225 char_u *s;
7226 char_u *r;
7227 int len;
7228 char *txt;
7229#endif
7230
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007231 rettv->v_type = VAR_STRING;
7232 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7235 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7236 <= curbuf->b_ml.ml_line_count
7237 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
7239 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7241 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 if (!linewhite(lnum))
7244 break;
7245 ++lnum;
7246 }
7247
7248 /* Find interesting text in this line. */
7249 s = skipwhite(ml_get(lnum));
7250 /* skip C comment-start */
7251 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007254 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007255 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007256 {
7257 s = skipwhite(ml_get(lnum + 1));
7258 if (*s == '*')
7259 s = skipwhite(s + 1);
7260 }
7261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 txt = _("+-%s%3ld lines: ");
7263 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007264 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 + 20 /* for %3ld */
7266 + STRLEN(s))); /* concatenated */
7267 if (r != NULL)
7268 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007269 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7270 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7271 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 len = (int)STRLEN(r);
7273 STRCAT(r, s);
7274 /* remove 'foldmarker' and 'commentstring' */
7275 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
7278 }
7279#endif
7280}
7281
7282/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007283 * "foldtextresult(lnum)" function
7284 */
7285/*ARGSUSED*/
7286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007287f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007288 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007289 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007290{
7291#ifdef FEAT_FOLDING
7292 linenr_T lnum;
7293 char_u *text;
7294 char_u buf[51];
7295 foldinfo_T foldinfo;
7296 int fold_count;
7297#endif
7298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007299 rettv->v_type = VAR_STRING;
7300 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007301#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007302 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007303 fold_count = foldedCount(curwin, lnum, &foldinfo);
7304 if (fold_count > 0)
7305 {
7306 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7307 &foldinfo, buf);
7308 if (text == buf)
7309 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007310 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007311 }
7312#endif
7313}
7314
7315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 * "foreground()" function
7317 */
7318/*ARGSUSED*/
7319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007320f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007321 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007322 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007324 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325#ifdef FEAT_GUI
7326 if (gui.in_use)
7327 gui_mch_set_foreground();
7328#else
7329# ifdef WIN32
7330 win32_set_foreground();
7331# endif
7332#endif
7333}
7334
7335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007336 * "function()" function
7337 */
7338/*ARGSUSED*/
7339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007340f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007343{
7344 char_u *s;
7345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007347 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007348 EMSG2(_(e_invarg2), s);
7349 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007350 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007351 else
7352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007353 rettv->vval.v_string = vim_strsave(s);
7354 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007355 }
7356}
7357
7358/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007359 * "get()" function
7360 */
7361 static void
7362f_get(argvars, rettv)
7363 typeval *argvars;
7364 typeval *rettv;
7365{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 listitem *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007367 listvar *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 dictitem *di;
7369 dictvar *d;
7370 typeval *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007371
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007373 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007375 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 li = list_find(l, get_tv_number(&argvars[1]));
7377 if (li != NULL)
7378 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007379 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00007380 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 else if (argvars[0].v_type == VAR_DICT)
7382 {
7383 if ((d = argvars[0].vval.v_dict) != NULL)
7384 {
7385 di = dict_find(d, get_tv_string(&argvars[1]), -1);
7386 if (di != NULL)
7387 tv = &di->di_tv;
7388 }
7389 }
7390 else
7391 EMSG2(_(e_listdictarg), "get()");
7392
7393 if (tv == NULL)
7394 {
7395 if (argvars[2].v_type == VAR_UNKNOWN)
7396 rettv->vval.v_number = 0;
7397 else
7398 copy_tv(&argvars[2], rettv);
7399 }
7400 else
7401 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007402}
7403
7404/*
7405 * "getbufvar()" function
7406 */
7407 static void
7408f_getbufvar(argvars, rettv)
7409 typeval *argvars;
7410 typeval *rettv;
7411{
7412 buf_T *buf;
7413 buf_T *save_curbuf;
7414 char_u *varname;
7415 VAR v;
7416
7417 ++emsg_off;
7418 buf = get_buf_tv(&argvars[0]);
7419 varname = get_tv_string(&argvars[1]);
7420
7421 rettv->v_type = VAR_STRING;
7422 rettv->vval.v_string = NULL;
7423
7424 if (buf != NULL && varname != NULL)
7425 {
7426 if (*varname == '&') /* buffer-local-option */
7427 {
7428 /* set curbuf to be our buf, temporarily */
7429 save_curbuf = curbuf;
7430 curbuf = buf;
7431
7432 get_option_tv(&varname, rettv, TRUE);
7433
7434 /* restore previous notion of curbuf */
7435 curbuf = save_curbuf;
7436 }
7437 else
7438 {
7439 /* look up the variable */
7440 v = find_var_in_ga(&buf->b_vars, varname);
7441 if (v != NULL)
7442 copy_tv(&v->tv, rettv);
7443 }
7444 }
7445
7446 --emsg_off;
7447}
7448
7449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 * "getchar()" function
7451 */
7452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457 varnumber_T n;
7458
7459 ++no_mapping;
7460 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 /* getchar(): blocking wait. */
7463 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 /* getchar(1): only check if char avail */
7466 n = vpeekc();
7467 else if (vpeekc() == NUL)
7468 /* getchar(0) and no char avail: return zero */
7469 n = 0;
7470 else
7471 /* getchar(0) and char avail: return char */
7472 n = safe_vgetc();
7473 --no_mapping;
7474 --allow_keys;
7475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (IS_SPECIAL(n) || mod_mask != 0)
7478 {
7479 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
7480 int i = 0;
7481
7482 /* Turn a special key into three bytes, plus modifier. */
7483 if (mod_mask != 0)
7484 {
7485 temp[i++] = K_SPECIAL;
7486 temp[i++] = KS_MODIFIER;
7487 temp[i++] = mod_mask;
7488 }
7489 if (IS_SPECIAL(n))
7490 {
7491 temp[i++] = K_SPECIAL;
7492 temp[i++] = K_SECOND(n);
7493 temp[i++] = K_THIRD(n);
7494 }
7495#ifdef FEAT_MBYTE
7496 else if (has_mbyte)
7497 i += (*mb_char2bytes)(n, temp + i);
7498#endif
7499 else
7500 temp[i++] = n;
7501 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 rettv->v_type = VAR_STRING;
7503 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505}
7506
7507/*
7508 * "getcharmod()" function
7509 */
7510/*ARGSUSED*/
7511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007513 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007514 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
7519/*
7520 * "getcmdline()" function
7521 */
7522/*ARGSUSED*/
7523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007525 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 rettv->v_type = VAR_STRING;
7529 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531
7532/*
7533 * "getcmdpos()" function
7534 */
7535/*ARGSUSED*/
7536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007537f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007538 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542}
7543
7544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 * "getcwd()" function
7546 */
7547/*ARGSUSED*/
7548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007550 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 char_u cwd[MAXPATHL];
7554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 else
7559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563#endif
7564 }
7565}
7566
7567/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007568 * "getfontname()" function
7569 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007570/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007574 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 rettv->v_type = VAR_STRING;
7577 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007578#ifdef FEAT_GUI
7579 if (gui.in_use)
7580 {
7581 GuiFont font;
7582 char_u *name = NULL;
7583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007585 {
7586 /* Get the "Normal" font. Either the name saved by
7587 * hl_set_font_name() or from the font ID. */
7588 font = gui.norm_font;
7589 name = hl_get_font_name();
7590 }
7591 else
7592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007594 if (STRCMP(name, "*") == 0) /* don't use font dialog */
7595 return;
7596 font = gui_mch_get_font(name, FALSE);
7597 if (font == NOFONT)
7598 return; /* Invalid font name, return empty string. */
7599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007602 gui_mch_free_font(font);
7603 }
7604#endif
7605}
7606
7607/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007608 * "getfperm({fname})" function
7609 */
7610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007614{
7615 char_u *fname;
7616 struct stat st;
7617 char_u *perm = NULL;
7618 char_u flags[] = "rwx";
7619 int i;
7620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007622
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007624 if (mch_stat((char *)fname, &st) >= 0)
7625 {
7626 perm = vim_strsave((char_u *)"---------");
7627 if (perm != NULL)
7628 {
7629 for (i = 0; i < 9; i++)
7630 {
7631 if (st.st_mode & (1 << (8 - i)))
7632 perm[i] = flags[i % 3];
7633 }
7634 }
7635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007636 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007637}
7638
7639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 * "getfsize({fname})" function
7641 */
7642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 char_u *fname;
7648 struct stat st;
7649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
7654 if (mch_stat((char *)fname, &st) >= 0)
7655 {
7656 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 }
7661 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663}
7664
7665/*
7666 * "getftime({fname})" function
7667 */
7668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 char_u *fname;
7674 struct stat st;
7675
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007676 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682}
7683
7684/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007685 * "getftype({fname})" function
7686 */
7687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007691{
7692 char_u *fname;
7693 struct stat st;
7694 char_u *type = NULL;
7695 char *t;
7696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007698
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007699 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007700 if (mch_lstat((char *)fname, &st) >= 0)
7701 {
7702#ifdef S_ISREG
7703 if (S_ISREG(st.st_mode))
7704 t = "file";
7705 else if (S_ISDIR(st.st_mode))
7706 t = "dir";
7707# ifdef S_ISLNK
7708 else if (S_ISLNK(st.st_mode))
7709 t = "link";
7710# endif
7711# ifdef S_ISBLK
7712 else if (S_ISBLK(st.st_mode))
7713 t = "bdev";
7714# endif
7715# ifdef S_ISCHR
7716 else if (S_ISCHR(st.st_mode))
7717 t = "cdev";
7718# endif
7719# ifdef S_ISFIFO
7720 else if (S_ISFIFO(st.st_mode))
7721 t = "fifo";
7722# endif
7723# ifdef S_ISSOCK
7724 else if (S_ISSOCK(st.st_mode))
7725 t = "fifo";
7726# endif
7727 else
7728 t = "other";
7729#else
7730# ifdef S_IFMT
7731 switch (st.st_mode & S_IFMT)
7732 {
7733 case S_IFREG: t = "file"; break;
7734 case S_IFDIR: t = "dir"; break;
7735# ifdef S_IFLNK
7736 case S_IFLNK: t = "link"; break;
7737# endif
7738# ifdef S_IFBLK
7739 case S_IFBLK: t = "bdev"; break;
7740# endif
7741# ifdef S_IFCHR
7742 case S_IFCHR: t = "cdev"; break;
7743# endif
7744# ifdef S_IFIFO
7745 case S_IFIFO: t = "fifo"; break;
7746# endif
7747# ifdef S_IFSOCK
7748 case S_IFSOCK: t = "socket"; break;
7749# endif
7750 default: t = "other";
7751 }
7752# else
7753 if (mch_isdir(fname))
7754 t = "dir";
7755 else
7756 t = "file";
7757# endif
7758#endif
7759 type = vim_strsave((char_u *)t);
7760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007762}
7763
7764/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007765 * "getline(lnum)" function
7766 */
7767 static void
7768f_getline(argvars, rettv)
7769 typeval *argvars;
7770 typeval *rettv;
7771{
7772 linenr_T lnum;
7773 linenr_T end;
7774 char_u *p;
7775 listvar *l;
7776 listitem *li;
7777
7778 lnum = get_tv_lnum(argvars);
7779
7780 if (argvars[1].v_type == VAR_UNKNOWN)
7781 {
7782 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7783 p = ml_get(lnum);
7784 else
7785 p = (char_u *)"";
7786
7787 rettv->v_type = VAR_STRING;
7788 rettv->vval.v_string = vim_strsave(p);
7789 }
7790 else
7791 {
7792 end = get_tv_lnum(&argvars[1]);
7793 if (end < lnum)
7794 {
7795 EMSG(_(e_invrange));
7796 rettv->vval.v_number = 0;
7797 }
7798 else
7799 {
7800 l = list_alloc();
7801 if (l != NULL)
7802 {
7803 if (lnum < 1)
7804 lnum = 1;
7805 if (end > curbuf->b_ml.ml_line_count)
7806 end = curbuf->b_ml.ml_line_count;
7807 while (lnum <= end)
7808 {
7809 li = listitem_alloc();
7810 if (li == NULL)
7811 break;
7812 list_append(l, li);
7813 li->li_tv.v_type = VAR_STRING;
7814 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
7815 }
7816 rettv->vval.v_list = l;
7817 rettv->v_type = VAR_LIST;
7818 ++l->lv_refcount;
7819 }
7820 }
7821 }
7822}
7823
7824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 * "getreg()" function
7826 */
7827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832 char_u *strregname;
7833 int regname;
7834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007838 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 regname = (strregname == NULL ? '"' : *strregname);
7840 if (regname == 0)
7841 regname = '"';
7842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 rettv->v_type = VAR_STRING;
7844 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845}
7846
7847/*
7848 * "getregtype()" function
7849 */
7850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 char_u *strregname;
7856 int regname;
7857 char_u buf[NUMBUFLEN + 2];
7858 long reglen = 0;
7859
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007861 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 else
7863 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
7866 regname = (strregname == NULL ? '"' : *strregname);
7867 if (regname == 0)
7868 regname = '"';
7869
7870 buf[0] = NUL;
7871 buf[1] = NUL;
7872 switch (get_reg_type(regname, &reglen))
7873 {
7874 case MLINE: buf[0] = 'V'; break;
7875 case MCHAR: buf[0] = 'v'; break;
7876#ifdef FEAT_VISUAL
7877 case MBLOCK:
7878 buf[0] = Ctrl_V;
7879 sprintf((char *)buf + 1, "%ld", reglen + 1);
7880 break;
7881#endif
7882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 rettv->v_type = VAR_STRING;
7884 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885}
7886
7887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 * "getwinposx()" function
7889 */
7890/*ARGSUSED*/
7891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897#ifdef FEAT_GUI
7898 if (gui.in_use)
7899 {
7900 int x, y;
7901
7902 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905#endif
7906}
7907
7908/*
7909 * "getwinposy()" function
7910 */
7911/*ARGSUSED*/
7912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#ifdef FEAT_GUI
7919 if (gui.in_use)
7920 {
7921 int x, y;
7922
7923 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926#endif
7927}
7928
7929/*
7930 * "getwinvar()" function
7931 */
7932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007934 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 win_T *win, *oldcurwin;
7938 char_u *varname;
7939 VAR v;
7940
7941 ++emsg_off;
7942 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 rettv->v_type = VAR_STRING;
7946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
7948 if (win != NULL && varname != NULL)
7949 {
7950 if (*varname == '&') /* window-local-option */
7951 {
7952 /* set curwin to be our win, temporarily */
7953 oldcurwin = curwin;
7954 curwin = win;
7955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957
7958 /* restore previous notion of curwin */
7959 curwin = oldcurwin;
7960 }
7961 else
7962 {
7963 /* look up the variable */
7964 v = find_var_in_ga(&win->w_vars, varname);
7965 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 }
7968 }
7969
7970 --emsg_off;
7971}
7972
7973/*
7974 * "glob()" function
7975 */
7976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 expand_T xpc;
7982
7983 ExpandInit(&xpc);
7984 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985 rettv->v_type = VAR_STRING;
7986 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
7988 ExpandCleanup(&xpc);
7989}
7990
7991/*
7992 * "globpath()" function
7993 */
7994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
7999 char_u buf1[NUMBUFLEN];
8000
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 rettv->v_type = VAR_STRING;
8002 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8003 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
8005
8006/*
8007 * "has()" function
8008 */
8009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008011 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
8014 int i;
8015 char_u *name;
8016 int n = FALSE;
8017 static char *(has_list[]) =
8018 {
8019#ifdef AMIGA
8020 "amiga",
8021# ifdef FEAT_ARP
8022 "arp",
8023# endif
8024#endif
8025#ifdef __BEOS__
8026 "beos",
8027#endif
8028#ifdef MSDOS
8029# ifdef DJGPP
8030 "dos32",
8031# else
8032 "dos16",
8033# endif
8034#endif
8035#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8036 "mac",
8037#endif
8038#if defined(MACOS_X_UNIX)
8039 "macunix",
8040#endif
8041#ifdef OS2
8042 "os2",
8043#endif
8044#ifdef __QNX__
8045 "qnx",
8046#endif
8047#ifdef RISCOS
8048 "riscos",
8049#endif
8050#ifdef UNIX
8051 "unix",
8052#endif
8053#ifdef VMS
8054 "vms",
8055#endif
8056#ifdef WIN16
8057 "win16",
8058#endif
8059#ifdef WIN32
8060 "win32",
8061#endif
8062#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8063 "win32unix",
8064#endif
8065#ifdef WIN64
8066 "win64",
8067#endif
8068#ifdef EBCDIC
8069 "ebcdic",
8070#endif
8071#ifndef CASE_INSENSITIVE_FILENAME
8072 "fname_case",
8073#endif
8074#ifdef FEAT_ARABIC
8075 "arabic",
8076#endif
8077#ifdef FEAT_AUTOCMD
8078 "autocmd",
8079#endif
8080#ifdef FEAT_BEVAL
8081 "balloon_eval",
8082#endif
8083#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8084 "builtin_terms",
8085# ifdef ALL_BUILTIN_TCAPS
8086 "all_builtin_terms",
8087# endif
8088#endif
8089#ifdef FEAT_BYTEOFF
8090 "byte_offset",
8091#endif
8092#ifdef FEAT_CINDENT
8093 "cindent",
8094#endif
8095#ifdef FEAT_CLIENTSERVER
8096 "clientserver",
8097#endif
8098#ifdef FEAT_CLIPBOARD
8099 "clipboard",
8100#endif
8101#ifdef FEAT_CMDL_COMPL
8102 "cmdline_compl",
8103#endif
8104#ifdef FEAT_CMDHIST
8105 "cmdline_hist",
8106#endif
8107#ifdef FEAT_COMMENTS
8108 "comments",
8109#endif
8110#ifdef FEAT_CRYPT
8111 "cryptv",
8112#endif
8113#ifdef FEAT_CSCOPE
8114 "cscope",
8115#endif
8116#ifdef DEBUG
8117 "debug",
8118#endif
8119#ifdef FEAT_CON_DIALOG
8120 "dialog_con",
8121#endif
8122#ifdef FEAT_GUI_DIALOG
8123 "dialog_gui",
8124#endif
8125#ifdef FEAT_DIFF
8126 "diff",
8127#endif
8128#ifdef FEAT_DIGRAPHS
8129 "digraphs",
8130#endif
8131#ifdef FEAT_DND
8132 "dnd",
8133#endif
8134#ifdef FEAT_EMACS_TAGS
8135 "emacs_tags",
8136#endif
8137 "eval", /* always present, of course! */
8138#ifdef FEAT_EX_EXTRA
8139 "ex_extra",
8140#endif
8141#ifdef FEAT_SEARCH_EXTRA
8142 "extra_search",
8143#endif
8144#ifdef FEAT_FKMAP
8145 "farsi",
8146#endif
8147#ifdef FEAT_SEARCHPATH
8148 "file_in_path",
8149#endif
8150#ifdef FEAT_FIND_ID
8151 "find_in_path",
8152#endif
8153#ifdef FEAT_FOLDING
8154 "folding",
8155#endif
8156#ifdef FEAT_FOOTER
8157 "footer",
8158#endif
8159#if !defined(USE_SYSTEM) && defined(UNIX)
8160 "fork",
8161#endif
8162#ifdef FEAT_GETTEXT
8163 "gettext",
8164#endif
8165#ifdef FEAT_GUI
8166 "gui",
8167#endif
8168#ifdef FEAT_GUI_ATHENA
8169# ifdef FEAT_GUI_NEXTAW
8170 "gui_neXtaw",
8171# else
8172 "gui_athena",
8173# endif
8174#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008175#ifdef FEAT_GUI_KDE
8176 "gui_kde",
8177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178#ifdef FEAT_GUI_GTK
8179 "gui_gtk",
8180# ifdef HAVE_GTK2
8181 "gui_gtk2",
8182# endif
8183#endif
8184#ifdef FEAT_GUI_MAC
8185 "gui_mac",
8186#endif
8187#ifdef FEAT_GUI_MOTIF
8188 "gui_motif",
8189#endif
8190#ifdef FEAT_GUI_PHOTON
8191 "gui_photon",
8192#endif
8193#ifdef FEAT_GUI_W16
8194 "gui_win16",
8195#endif
8196#ifdef FEAT_GUI_W32
8197 "gui_win32",
8198#endif
8199#ifdef FEAT_HANGULIN
8200 "hangul_input",
8201#endif
8202#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8203 "iconv",
8204#endif
8205#ifdef FEAT_INS_EXPAND
8206 "insert_expand",
8207#endif
8208#ifdef FEAT_JUMPLIST
8209 "jumplist",
8210#endif
8211#ifdef FEAT_KEYMAP
8212 "keymap",
8213#endif
8214#ifdef FEAT_LANGMAP
8215 "langmap",
8216#endif
8217#ifdef FEAT_LIBCALL
8218 "libcall",
8219#endif
8220#ifdef FEAT_LINEBREAK
8221 "linebreak",
8222#endif
8223#ifdef FEAT_LISP
8224 "lispindent",
8225#endif
8226#ifdef FEAT_LISTCMDS
8227 "listcmds",
8228#endif
8229#ifdef FEAT_LOCALMAP
8230 "localmap",
8231#endif
8232#ifdef FEAT_MENU
8233 "menu",
8234#endif
8235#ifdef FEAT_SESSION
8236 "mksession",
8237#endif
8238#ifdef FEAT_MODIFY_FNAME
8239 "modify_fname",
8240#endif
8241#ifdef FEAT_MOUSE
8242 "mouse",
8243#endif
8244#ifdef FEAT_MOUSESHAPE
8245 "mouseshape",
8246#endif
8247#if defined(UNIX) || defined(VMS)
8248# ifdef FEAT_MOUSE_DEC
8249 "mouse_dec",
8250# endif
8251# ifdef FEAT_MOUSE_GPM
8252 "mouse_gpm",
8253# endif
8254# ifdef FEAT_MOUSE_JSB
8255 "mouse_jsbterm",
8256# endif
8257# ifdef FEAT_MOUSE_NET
8258 "mouse_netterm",
8259# endif
8260# ifdef FEAT_MOUSE_PTERM
8261 "mouse_pterm",
8262# endif
8263# ifdef FEAT_MOUSE_XTERM
8264 "mouse_xterm",
8265# endif
8266#endif
8267#ifdef FEAT_MBYTE
8268 "multi_byte",
8269#endif
8270#ifdef FEAT_MBYTE_IME
8271 "multi_byte_ime",
8272#endif
8273#ifdef FEAT_MULTI_LANG
8274 "multi_lang",
8275#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008276#ifdef FEAT_MZSCHEME
8277 "mzscheme",
8278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279#ifdef FEAT_OLE
8280 "ole",
8281#endif
8282#ifdef FEAT_OSFILETYPE
8283 "osfiletype",
8284#endif
8285#ifdef FEAT_PATH_EXTRA
8286 "path_extra",
8287#endif
8288#ifdef FEAT_PERL
8289#ifndef DYNAMIC_PERL
8290 "perl",
8291#endif
8292#endif
8293#ifdef FEAT_PYTHON
8294#ifndef DYNAMIC_PYTHON
8295 "python",
8296#endif
8297#endif
8298#ifdef FEAT_POSTSCRIPT
8299 "postscript",
8300#endif
8301#ifdef FEAT_PRINTER
8302 "printer",
8303#endif
8304#ifdef FEAT_QUICKFIX
8305 "quickfix",
8306#endif
8307#ifdef FEAT_RIGHTLEFT
8308 "rightleft",
8309#endif
8310#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8311 "ruby",
8312#endif
8313#ifdef FEAT_SCROLLBIND
8314 "scrollbind",
8315#endif
8316#ifdef FEAT_CMDL_INFO
8317 "showcmd",
8318 "cmdline_info",
8319#endif
8320#ifdef FEAT_SIGNS
8321 "signs",
8322#endif
8323#ifdef FEAT_SMARTINDENT
8324 "smartindent",
8325#endif
8326#ifdef FEAT_SNIFF
8327 "sniff",
8328#endif
8329#ifdef FEAT_STL_OPT
8330 "statusline",
8331#endif
8332#ifdef FEAT_SUN_WORKSHOP
8333 "sun_workshop",
8334#endif
8335#ifdef FEAT_NETBEANS_INTG
8336 "netbeans_intg",
8337#endif
8338#ifdef FEAT_SYN_HL
8339 "syntax",
8340#endif
8341#if defined(USE_SYSTEM) || !defined(UNIX)
8342 "system",
8343#endif
8344#ifdef FEAT_TAG_BINS
8345 "tag_binary",
8346#endif
8347#ifdef FEAT_TAG_OLDSTATIC
8348 "tag_old_static",
8349#endif
8350#ifdef FEAT_TAG_ANYWHITE
8351 "tag_any_white",
8352#endif
8353#ifdef FEAT_TCL
8354# ifndef DYNAMIC_TCL
8355 "tcl",
8356# endif
8357#endif
8358#ifdef TERMINFO
8359 "terminfo",
8360#endif
8361#ifdef FEAT_TERMRESPONSE
8362 "termresponse",
8363#endif
8364#ifdef FEAT_TEXTOBJ
8365 "textobjects",
8366#endif
8367#ifdef HAVE_TGETENT
8368 "tgetent",
8369#endif
8370#ifdef FEAT_TITLE
8371 "title",
8372#endif
8373#ifdef FEAT_TOOLBAR
8374 "toolbar",
8375#endif
8376#ifdef FEAT_USR_CMDS
8377 "user-commands", /* was accidentally included in 5.4 */
8378 "user_commands",
8379#endif
8380#ifdef FEAT_VIMINFO
8381 "viminfo",
8382#endif
8383#ifdef FEAT_VERTSPLIT
8384 "vertsplit",
8385#endif
8386#ifdef FEAT_VIRTUALEDIT
8387 "virtualedit",
8388#endif
8389#ifdef FEAT_VISUAL
8390 "visual",
8391#endif
8392#ifdef FEAT_VISUALEXTRA
8393 "visualextra",
8394#endif
8395#ifdef FEAT_VREPLACE
8396 "vreplace",
8397#endif
8398#ifdef FEAT_WILDIGN
8399 "wildignore",
8400#endif
8401#ifdef FEAT_WILDMENU
8402 "wildmenu",
8403#endif
8404#ifdef FEAT_WINDOWS
8405 "windows",
8406#endif
8407#ifdef FEAT_WAK
8408 "winaltkeys",
8409#endif
8410#ifdef FEAT_WRITEBACKUP
8411 "writebackup",
8412#endif
8413#ifdef FEAT_XIM
8414 "xim",
8415#endif
8416#ifdef FEAT_XFONTSET
8417 "xfontset",
8418#endif
8419#ifdef USE_XSMP
8420 "xsmp",
8421#endif
8422#ifdef USE_XSMP_INTERACT
8423 "xsmp_interact",
8424#endif
8425#ifdef FEAT_XCLIPBOARD
8426 "xterm_clipboard",
8427#endif
8428#ifdef FEAT_XTERM_SAVE
8429 "xterm_save",
8430#endif
8431#if defined(UNIX) && defined(FEAT_X11)
8432 "X11",
8433#endif
8434 NULL
8435 };
8436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008437 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 for (i = 0; has_list[i] != NULL; ++i)
8439 if (STRICMP(name, has_list[i]) == 0)
8440 {
8441 n = TRUE;
8442 break;
8443 }
8444
8445 if (n == FALSE)
8446 {
8447 if (STRNICMP(name, "patch", 5) == 0)
8448 n = has_patch(atoi((char *)name + 5));
8449 else if (STRICMP(name, "vim_starting") == 0)
8450 n = (starting != 0);
8451#ifdef DYNAMIC_TCL
8452 else if (STRICMP(name, "tcl") == 0)
8453 n = tcl_enabled(FALSE);
8454#endif
8455#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
8456 else if (STRICMP(name, "iconv") == 0)
8457 n = iconv_enabled(FALSE);
8458#endif
8459#ifdef DYNAMIC_RUBY
8460 else if (STRICMP(name, "ruby") == 0)
8461 n = ruby_enabled(FALSE);
8462#endif
8463#ifdef DYNAMIC_PYTHON
8464 else if (STRICMP(name, "python") == 0)
8465 n = python_enabled(FALSE);
8466#endif
8467#ifdef DYNAMIC_PERL
8468 else if (STRICMP(name, "perl") == 0)
8469 n = perl_enabled(FALSE);
8470#endif
8471#ifdef FEAT_GUI
8472 else if (STRICMP(name, "gui_running") == 0)
8473 n = (gui.in_use || gui.starting);
8474# ifdef FEAT_GUI_W32
8475 else if (STRICMP(name, "gui_win32s") == 0)
8476 n = gui_is_win32s();
8477# endif
8478# ifdef FEAT_BROWSE
8479 else if (STRICMP(name, "browse") == 0)
8480 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
8481# endif
8482#endif
8483#ifdef FEAT_SYN_HL
8484 else if (STRICMP(name, "syntax_items") == 0)
8485 n = syntax_present(curbuf);
8486#endif
8487#if defined(WIN3264)
8488 else if (STRICMP(name, "win95") == 0)
8489 n = mch_windows95();
8490#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00008491#ifdef FEAT_NETBEANS_INTG
8492 else if (STRICMP(name, "netbeans_enabled") == 0)
8493 n = usingNetbeans;
8494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 }
8496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498}
8499
8500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 * "has_key()" function
8502 */
8503 static void
8504f_has_key(argvars, rettv)
8505 typeval *argvars;
8506 typeval *rettv;
8507{
8508 rettv->vval.v_number = 0;
8509 if (argvars[0].v_type != VAR_DICT)
8510 {
8511 EMSG(_(e_dictreq));
8512 return;
8513 }
8514 if (argvars[0].vval.v_dict == NULL)
8515 return;
8516
8517 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
8518 get_tv_string(&argvars[1]), -1) != NULL;
8519}
8520
8521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 * "hasmapto()" function
8523 */
8524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 char_u *name;
8530 char_u *mode;
8531 char_u buf[NUMBUFLEN];
8532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 mode = (char_u *)"nvo";
8536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
8539 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008542 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543}
8544
8545/*
8546 * "histadd()" function
8547 */
8548/*ARGSUSED*/
8549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008550f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554#ifdef FEAT_CMDHIST
8555 int histype;
8556 char_u *str;
8557 char_u buf[NUMBUFLEN];
8558#endif
8559
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (check_restricted() || check_secure())
8562 return;
8563#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (histype >= 0)
8566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (*str != NUL)
8569 {
8570 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 return;
8573 }
8574 }
8575#endif
8576}
8577
8578/*
8579 * "histdel()" function
8580 */
8581/*ARGSUSED*/
8582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008584 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587#ifdef FEAT_CMDHIST
8588 int n;
8589 char_u buf[NUMBUFLEN];
8590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008591 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008594 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
8597 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 else
8599 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
8601 get_tv_string_buf(&argvars[1], buf));
8602 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605#endif
8606}
8607
8608/*
8609 * "histget()" function
8610 */
8611/*ARGSUSED*/
8612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617#ifdef FEAT_CMDHIST
8618 int type;
8619 int idx;
8620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008622 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 idx = get_history_idx(type);
8624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 idx = (int)get_tv_number(&argvars[1]);
8626 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631}
8632
8633/*
8634 * "histnr()" function
8635 */
8636/*ARGSUSED*/
8637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008639 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008640 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 int i;
8643
8644#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 if (i >= HIST_CMD && i < HIST_COUNT)
8647 i = get_history_idx(i);
8648 else
8649#endif
8650 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652}
8653
8654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 * "highlightID(name)" function
8656 */
8657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663}
8664
8665/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008666 * "highlight_exists()" function
8667 */
8668 static void
8669f_hlexists(argvars, rettv)
8670 typeval *argvars;
8671 typeval *rettv;
8672{
8673 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
8674}
8675
8676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 * "hostname()" function
8678 */
8679/*ARGSUSED*/
8680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008682 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 char_u hostname[256];
8686
8687 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008688 rettv->v_type = VAR_STRING;
8689 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
8693 * iconv() function
8694 */
8695/*ARGSUSED*/
8696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008698 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701#ifdef FEAT_MBYTE
8702 char_u buf1[NUMBUFLEN];
8703 char_u buf2[NUMBUFLEN];
8704 char_u *from, *to, *str;
8705 vimconv_T vimconv;
8706#endif
8707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 rettv->v_type = VAR_STRING;
8709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
8711#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 str = get_tv_string(&argvars[0]);
8713 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
8714 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 vimconv.vc_type = CONV_NONE;
8716 convert_setup(&vimconv, from, to);
8717
8718 /* If the encodings are equal, no conversion needed. */
8719 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 convert_setup(&vimconv, NULL, NULL);
8725 vim_free(from);
8726 vim_free(to);
8727#endif
8728}
8729
8730/*
8731 * "indent()" function
8732 */
8733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008735 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
8738 linenr_T lnum;
8739
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745}
8746
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008747/*
8748 * "index()" function
8749 */
8750 static void
8751f_index(argvars, rettv)
8752 typeval *argvars;
8753 typeval *rettv;
8754{
8755 listvar *l;
8756 listitem *item;
8757 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008758 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008759 int ic = FALSE;
8760
8761 rettv->vval.v_number = -1;
8762 if (argvars[0].v_type != VAR_LIST)
8763 {
8764 EMSG(_(e_listreq));
8765 return;
8766 }
8767 l = argvars[0].vval.v_list;
8768 if (l != NULL)
8769 {
8770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008771 {
8772 min_idx = get_tv_number(&argvars[2]);
8773 if (argvars[3].v_type != VAR_UNKNOWN)
8774 ic = get_tv_number(&argvars[3]);
8775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008776
8777 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008778 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008779 {
8780 rettv->vval.v_number = idx;
8781 break;
8782 }
8783 }
8784}
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786static int inputsecret_flag = 0;
8787
8788/*
8789 * "input()" function
8790 * Also handles inputsecret() when inputsecret is set.
8791 */
8792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008794 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 char_u *p = NULL;
8799 int c;
8800 char_u buf[NUMBUFLEN];
8801 int cmd_silent_save = cmd_silent;
8802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804
8805#ifdef NO_CONSOLE_INPUT
8806 /* While starting up, there is no place to enter text. */
8807 if (no_console_input())
8808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 return;
8811 }
8812#endif
8813
8814 cmd_silent = FALSE; /* Want to see the prompt. */
8815 if (prompt != NULL)
8816 {
8817 /* Only the part of the message after the last NL is considered as
8818 * prompt for the command line */
8819 p = vim_strrchr(prompt, '\n');
8820 if (p == NULL)
8821 p = prompt;
8822 else
8823 {
8824 ++p;
8825 c = *p;
8826 *p = NUL;
8827 msg_start();
8828 msg_clr_eos();
8829 msg_puts_attr(prompt, echo_attr);
8830 msg_didout = FALSE;
8831 msg_starthere();
8832 *p = c;
8833 }
8834 cmdline_row = msg_row;
8835 }
8836
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008837 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
8842
8843 /* since the user typed this, no need to wait for return */
8844 need_wait_return = FALSE;
8845 msg_didout = FALSE;
8846 cmd_silent = cmd_silent_save;
8847}
8848
8849/*
8850 * "inputdialog()" function
8851 */
8852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857#if defined(FEAT_GUI_TEXTDIALOG)
8858 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
8859 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
8860 {
8861 char_u *message;
8862 char_u buf[NUMBUFLEN];
8863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008865 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 IObuff[IOSIZE - 1] = NUL;
8869 }
8870 else
8871 IObuff[0] = NUL;
8872 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
8873 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 else
8876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008877 if (argvars[1].v_type != VAR_UNKNOWN
8878 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879 rettv->vval.v_string = vim_strsave(
8880 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886 else
8887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889}
8890
8891static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
8892
8893/*
8894 * "inputrestore()" function
8895 */
8896/*ARGSUSED*/
8897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008898f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008899 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 if (ga_userinput.ga_len > 0)
8903 {
8904 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
8906 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 }
8909 else if (p_verbose > 1)
8910 {
8911 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914}
8915
8916/*
8917 * "inputsave()" function
8918 */
8919/*ARGSUSED*/
8920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008922 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 /* Add an entry to the stack of typehead storage. */
8926 if (ga_grow(&ga_userinput, 1) == OK)
8927 {
8928 save_typeahead((tasave_T *)(ga_userinput.ga_data)
8929 + ga_userinput.ga_len);
8930 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 }
8933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
8937/*
8938 * "inputsecret()" function
8939 */
8940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008942 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944{
8945 ++cmdline_star;
8946 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 --cmdline_star;
8949 --inputsecret_flag;
8950}
8951
8952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008953 * "insert()" function
8954 */
8955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008959{
8960 long before = 0;
8961 long n;
8962 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963 listvar *l;
8964
8965 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008966 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 else if ((l = argvars[0].vval.v_list) != NULL)
8968 {
8969 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008972 n = before;
8973 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008974 if (n > 0)
8975 EMSGN(_(e_listidx), before);
8976 else
8977 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008978 list_insert_tv(l, &argvars[1], item);
8979 ++l->lv_refcount;
8980 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 }
8982 }
8983}
8984
8985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 * "isdirectory()" function
8987 */
8988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995
Bram Moolenaar8c711452005-01-14 21:53:12 +00008996static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
8997
8998/*
8999 * Turn a dict into a list:
9000 * "what" == 0: list of keys
9001 * "what" == 1: list of values
9002 * "what" == 2: list of items
9003 */
9004 static void
9005dict_list(argvars, rettv, what)
9006 typeval *argvars;
9007 typeval *rettv;
9008 int what;
9009{
9010 listvar *l;
9011 listvar *l2;
9012 dictitem *di;
9013 listitem *li;
9014 listitem *li2;
9015
9016 rettv->vval.v_number = 0;
9017 if (argvars[0].v_type != VAR_DICT)
9018 {
9019 EMSG(_(e_dictreq));
9020 return;
9021 }
9022 if (argvars[0].vval.v_dict == NULL)
9023 return;
9024
9025 l = list_alloc();
9026 if (l == NULL)
9027 return;
9028 rettv->v_type = VAR_LIST;
9029 rettv->vval.v_list = l;
9030 ++l->lv_refcount;
9031
9032 for (di = argvars[0].vval.v_dict->dv_first; di != NULL; di = di->di_next)
9033 {
9034 li = listitem_alloc();
9035 if (li == NULL)
9036 break;
9037 list_append(l, li);
9038
9039 if (what == 0)
9040 {
9041 /* keys() */
9042 li->li_tv.v_type = VAR_STRING;
9043 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9044 }
9045 else if (what == 1)
9046 {
9047 /* values() */
9048 copy_tv(&di->di_tv, &li->li_tv);
9049 }
9050 else
9051 {
9052 /* items() */
9053 l2 = list_alloc();
9054 li->li_tv.v_type = VAR_LIST;
9055 li->li_tv.vval.v_list = l2;
9056 if (l2 == NULL)
9057 break;
9058 ++l2->lv_refcount;
9059
9060 li2 = listitem_alloc();
9061 if (li2 == NULL)
9062 break;
9063 list_append(l2, li2);
9064 li2->li_tv.v_type = VAR_STRING;
9065 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9066
9067 li2 = listitem_alloc();
9068 if (li2 == NULL)
9069 break;
9070 list_append(l2, li2);
9071 copy_tv(&di->di_tv, &li2->li_tv);
9072 }
9073 }
9074}
9075
9076/*
9077 * "items(dict)" function
9078 */
9079 static void
9080f_items(argvars, rettv)
9081 typeval *argvars;
9082 typeval *rettv;
9083{
9084 dict_list(argvars, rettv, 2);
9085}
9086
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009088 * "join()" function
9089 */
9090 static void
9091f_join(argvars, rettv)
9092 typeval *argvars;
9093 typeval *rettv;
9094{
9095 garray_T ga;
9096 char_u *sep;
9097
9098 rettv->vval.v_number = 0;
9099 if (argvars[0].v_type != VAR_LIST)
9100 {
9101 EMSG(_(e_listreq));
9102 return;
9103 }
9104 if (argvars[0].vval.v_list == NULL)
9105 return;
9106 if (argvars[1].v_type == VAR_UNKNOWN)
9107 sep = (char_u *)" ";
9108 else
9109 sep = get_tv_string(&argvars[1]);
9110
9111 ga_init2(&ga, (int)sizeof(char), 80);
9112 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9113 ga_append(&ga, NUL);
9114
9115 rettv->v_type = VAR_STRING;
9116 rettv->vval.v_string = (char_u *)ga.ga_data;
9117}
9118
9119/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009120 * "keys()" function
9121 */
9122 static void
9123f_keys(argvars, rettv)
9124 typeval *argvars;
9125 typeval *rettv;
9126{
9127 dict_list(argvars, rettv, 0);
9128}
9129
9130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 * "last_buffer_nr()" function.
9132 */
9133/*ARGSUSED*/
9134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009136 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 int n = 0;
9140 buf_T *buf;
9141
9142 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9143 if (n < buf->b_fnum)
9144 n = buf->b_fnum;
9145
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009147}
9148
9149/*
9150 * "len()" function
9151 */
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009154 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156{
9157 switch (argvars[0].v_type)
9158 {
9159 case VAR_STRING:
9160 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = (varnumber_T)STRLEN(
9162 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 break;
9164 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009166 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009167 case VAR_DICT:
9168 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9169 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009171 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009172 break;
9173 }
9174}
9175
Bram Moolenaar0d660222005-01-07 21:51:51 +00009176static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009177
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009180 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009182 int type;
9183{
9184#ifdef FEAT_LIBCALL
9185 char_u *string_in;
9186 char_u **string_result;
9187 int nr_result;
9188#endif
9189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009191 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009195
9196 if (check_restricted() || check_secure())
9197 return;
9198
9199#ifdef FEAT_LIBCALL
9200 /* The first two args must be strings, otherwise its meaningless */
9201 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9202 {
9203 if (argvars[2].v_type == VAR_NUMBER)
9204 string_in = NULL;
9205 else
9206 string_in = argvars[2].vval.v_string;
9207 if (type == VAR_NUMBER)
9208 string_result = NULL;
9209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009211 if (mch_libcall(argvars[0].vval.v_string,
9212 argvars[1].vval.v_string,
9213 string_in,
9214 argvars[2].vval.v_number,
9215 string_result,
9216 &nr_result) == OK
9217 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009219 }
9220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221}
9222
9223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009224 * "libcall()" function
9225 */
9226 static void
9227f_libcall(argvars, rettv)
9228 typeval *argvars;
9229 typeval *rettv;
9230{
9231 libcall_common(argvars, rettv, VAR_STRING);
9232}
9233
9234/*
9235 * "libcallnr()" function
9236 */
9237 static void
9238f_libcallnr(argvars, rettv)
9239 typeval *argvars;
9240 typeval *rettv;
9241{
9242 libcall_common(argvars, rettv, VAR_NUMBER);
9243}
9244
9245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 * "line(string)" function
9247 */
9248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252{
9253 linenr_T lnum = 0;
9254 pos_T *fp;
9255
9256 fp = var2fpos(&argvars[0], TRUE);
9257 if (fp != NULL)
9258 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262/*
9263 * "line2byte(lnum)" function
9264 */
9265/*ARGSUSED*/
9266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009268 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
9271#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#else
9274 linenr_T lnum;
9275
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
9281 if (rettv->vval.v_number >= 0)
9282 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#endif
9284}
9285
9286/*
9287 * "lispindent(lnum)" function
9288 */
9289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009291 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
9294#ifdef FEAT_LISP
9295 pos_T pos;
9296 linenr_T lnum;
9297
9298 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9301 {
9302 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 curwin->w_cursor = pos;
9305 }
9306 else
9307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309}
9310
9311/*
9312 * "localtime()" function
9313 */
9314/*ARGSUSED*/
9315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009317 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321}
9322
Bram Moolenaar0d660222005-01-07 21:51:51 +00009323static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
9325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009327 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 int exact;
9330{
9331 char_u *keys;
9332 char_u *which;
9333 char_u buf[NUMBUFLEN];
9334 char_u *keys_buf = NULL;
9335 char_u *rhs;
9336 int mode;
9337 garray_T ga;
9338
9339 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 rettv->v_type = VAR_STRING;
9341 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 if (*keys == NUL)
9345 return;
9346
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009347 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 else
9350 which = (char_u *)"";
9351 mode = get_map_mode(&which, 0);
9352
9353 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9354 rhs = check_map(keys, mode, exact);
9355 vim_free(keys_buf);
9356 if (rhs != NULL)
9357 {
9358 ga_init(&ga);
9359 ga.ga_itemsize = 1;
9360 ga.ga_growsize = 40;
9361
9362 while (*rhs != NUL)
9363 ga_concat(&ga, str2special(&rhs, FALSE));
9364
9365 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 }
9368}
9369
9370/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009371 * "map()" function
9372 */
9373 static void
9374f_map(argvars, rettv)
9375 typeval *argvars;
9376 typeval *rettv;
9377{
9378 filter_map(argvars, rettv, TRUE);
9379}
9380
9381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009382 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 */
9384 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009385f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009386 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009389 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390}
9391
9392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009393 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 */
9395 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009396f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009397 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
Bram Moolenaar0d660222005-01-07 21:51:51 +00009403static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009407 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 int type;
9410{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009411 char_u *str = NULL;
9412 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 char_u *pat;
9414 regmatch_T regmatch;
9415 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009416 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 char_u *save_cpo;
9418 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009419 long nth = 1;
9420 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009421 listvar *l = NULL;
9422 listitem *li = NULL;
9423 long idx = 0;
9424 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425
9426 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9427 save_cpo = p_cpo;
9428 p_cpo = (char_u *)"";
9429
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 if (type == 2)
9431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->v_type = VAR_STRING;
9433 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
9435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009438 if (argvars[0].v_type == VAR_LIST)
9439 {
9440 if ((l = argvars[0].vval.v_list) == NULL)
9441 goto theend;
9442 li = l->lv_first;
9443 }
9444 else
9445 expr = str = get_tv_string(&argvars[0]);
9446
9447 pat = get_tv_string_buf(&argvars[1], patbuf);
9448
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009452 if (l != NULL)
9453 {
9454 li = list_find(l, start);
9455 if (li == NULL)
9456 goto theend;
9457 if (start < 0)
9458 {
9459 listitem *ni;
9460
9461 /* Need to compute the index. */
9462 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
9463 ++idx;
9464 }
9465 else
9466 idx = start;
9467 }
9468 else
9469 {
9470 if (start < 0)
9471 start = 0;
9472 if (start > (long)STRLEN(str))
9473 goto theend;
9474 str += start;
9475 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009476
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009477 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 }
9480
9481 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9482 if (regmatch.regprog != NULL)
9483 {
9484 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009485
9486 while (1)
9487 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009488 if (l != NULL)
9489 {
9490 if (li == NULL)
9491 {
9492 match = FALSE;
9493 break;
9494 }
9495 str = echo_string(&li->li_tv, &tofree, strbuf);
9496 }
9497
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009498 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009499
9500 if (l != NULL)
9501 vim_free(tofree);
9502 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009503 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009504 if (l == NULL && !match)
9505 break;
9506
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009507 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009508 if (l != NULL)
9509 {
9510 li = li->li_next;
9511 ++idx;
9512 }
9513 else
9514 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009515#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009516 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009517#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009518 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009519#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009520 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009521 }
9522
9523 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
9525 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009526 {
9527 if (l != NULL)
9528 copy_tv(&li->li_tv, rettv);
9529 else
9530 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009532 }
9533 else if (l != NULL)
9534 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 else
9536 {
9537 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 (varnumber_T)(regmatch.startp[0] - str);
9540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 }
9545 }
9546 vim_free(regmatch.regprog);
9547 }
9548
9549theend:
9550 p_cpo = save_cpo;
9551}
9552
9553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009554 * "match()" function
9555 */
9556 static void
9557f_match(argvars, rettv)
9558 typeval *argvars;
9559 typeval *rettv;
9560{
9561 find_some_match(argvars, rettv, 1);
9562}
9563
9564/*
9565 * "matchend()" function
9566 */
9567 static void
9568f_matchend(argvars, rettv)
9569 typeval *argvars;
9570 typeval *rettv;
9571{
9572 find_some_match(argvars, rettv, 0);
9573}
9574
9575/*
9576 * "matchstr()" function
9577 */
9578 static void
9579f_matchstr(argvars, rettv)
9580 typeval *argvars;
9581 typeval *rettv;
9582{
9583 find_some_match(argvars, rettv, 2);
9584}
9585
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009586static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
9587
9588 static void
9589max_min(argvars, rettv, domax)
9590 typeval *argvars;
9591 typeval *rettv;
9592 int domax;
9593{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009594 long n = 0;
9595 long i;
9596
9597 if (argvars[0].v_type == VAR_LIST)
9598 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009599 listvar *l;
9600 listitem *li;
9601
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009602 l = argvars[0].vval.v_list;
9603 if (l != NULL)
9604 {
9605 li = l->lv_first;
9606 if (li != NULL)
9607 {
9608 n = get_tv_number(&li->li_tv);
9609 while (1)
9610 {
9611 li = li->li_next;
9612 if (li == NULL)
9613 break;
9614 i = get_tv_number(&li->li_tv);
9615 if (domax ? i > n : i < n)
9616 n = i;
9617 }
9618 }
9619 }
9620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 else if (argvars[0].v_type == VAR_DICT)
9622 {
9623 dictvar *d;
9624 dictitem *di;
9625
9626 d = argvars[0].vval.v_dict;
9627 if (d != NULL)
9628 {
9629 di = d->dv_first;
9630 if (di != NULL)
9631 {
9632 n = get_tv_number(&di->di_tv);
9633 while (1)
9634 {
9635 di = di->di_next;
9636 if (di == NULL)
9637 break;
9638 i = get_tv_number(&di->di_tv);
9639 if (domax ? i > n : i < n)
9640 n = i;
9641 }
9642 }
9643 }
9644 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009645 else
9646 EMSG(_(e_listreq));
9647 rettv->vval.v_number = n;
9648}
9649
9650/*
9651 * "max()" function
9652 */
9653 static void
9654f_max(argvars, rettv)
9655 typeval *argvars;
9656 typeval *rettv;
9657{
9658 max_min(argvars, rettv, TRUE);
9659}
9660
9661/*
9662 * "min()" function
9663 */
9664 static void
9665f_min(argvars, rettv)
9666 typeval *argvars;
9667 typeval *rettv;
9668{
9669 max_min(argvars, rettv, FALSE);
9670}
9671
Bram Moolenaar0d660222005-01-07 21:51:51 +00009672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 * "mode()" function
9674 */
9675/*ARGSUSED*/
9676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009678 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 char_u buf[2];
9682
9683#ifdef FEAT_VISUAL
9684 if (VIsual_active)
9685 {
9686 if (VIsual_select)
9687 buf[0] = VIsual_mode + 's' - 'v';
9688 else
9689 buf[0] = VIsual_mode;
9690 }
9691 else
9692#endif
9693 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
9694 buf[0] = 'r';
9695 else if (State & INSERT)
9696 {
9697 if (State & REPLACE_FLAG)
9698 buf[0] = 'R';
9699 else
9700 buf[0] = 'i';
9701 }
9702 else if (State & CMDLINE)
9703 buf[0] = 'c';
9704 else
9705 buf[0] = 'n';
9706
9707 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_string = vim_strsave(buf);
9709 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710}
9711
9712/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009713 * "nextnonblank()" function
9714 */
9715 static void
9716f_nextnonblank(argvars, rettv)
9717 typeval *argvars;
9718 typeval *rettv;
9719{
9720 linenr_T lnum;
9721
9722 for (lnum = get_tv_lnum(argvars); ; ++lnum)
9723 {
9724 if (lnum > curbuf->b_ml.ml_line_count)
9725 {
9726 lnum = 0;
9727 break;
9728 }
9729 if (*skipwhite(ml_get(lnum)) != NUL)
9730 break;
9731 }
9732 rettv->vval.v_number = lnum;
9733}
9734
9735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 * "nr2char()" function
9737 */
9738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009740 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742{
9743 char_u buf[NUMBUFLEN];
9744
9745#ifdef FEAT_MBYTE
9746 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 else
9749#endif
9750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 buf[1] = NUL;
9753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->v_type = VAR_STRING;
9755 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009756}
9757
9758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009759 * "prevnonblank()" function
9760 */
9761 static void
9762f_prevnonblank(argvars, rettv)
9763 typeval *argvars;
9764 typeval *rettv;
9765{
9766 linenr_T lnum;
9767
9768 lnum = get_tv_lnum(argvars);
9769 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9770 lnum = 0;
9771 else
9772 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9773 --lnum;
9774 rettv->vval.v_number = lnum;
9775}
9776
Bram Moolenaar8c711452005-01-14 21:53:12 +00009777/*
9778 * "range()" function
9779 */
9780 static void
9781f_range(argvars, rettv)
9782 typeval *argvars;
9783 typeval *rettv;
9784{
9785 long start;
9786 long end;
9787 long stride = 1;
9788 long i;
9789 listvar *l;
9790 listitem *li;
9791
9792 start = get_tv_number(&argvars[0]);
9793 if (argvars[1].v_type == VAR_UNKNOWN)
9794 {
9795 end = start - 1;
9796 start = 0;
9797 }
9798 else
9799 {
9800 end = get_tv_number(&argvars[1]);
9801 if (argvars[2].v_type != VAR_UNKNOWN)
9802 stride = get_tv_number(&argvars[2]);
9803 }
9804
9805 rettv->vval.v_number = 0;
9806 if (stride == 0)
9807 EMSG(_("E999: Stride is zero"));
9808 else if (stride > 0 ? end < start : end > start)
9809 EMSG(_("E999: Start past end"));
9810 else
9811 {
9812 l = list_alloc();
9813 if (l != NULL)
9814 {
9815 rettv->v_type = VAR_LIST;
9816 rettv->vval.v_list = l;
9817 ++l->lv_refcount;
9818
9819 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9820 {
9821 li = listitem_alloc();
9822 if (li == NULL)
9823 break;
9824 li->li_tv.v_type = VAR_NUMBER;
9825 li->li_tv.vval.v_number = i;
9826 list_append(l, li);
9827 }
9828 }
9829 }
9830}
9831
Bram Moolenaar0d660222005-01-07 21:51:51 +00009832#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9833static void make_connection __ARGS((void));
9834static int check_connection __ARGS((void));
9835
9836 static void
9837make_connection()
9838{
9839 if (X_DISPLAY == NULL
9840# ifdef FEAT_GUI
9841 && !gui.in_use
9842# endif
9843 )
9844 {
9845 x_force_connect = TRUE;
9846 setup_term_clip();
9847 x_force_connect = FALSE;
9848 }
9849}
9850
9851 static int
9852check_connection()
9853{
9854 make_connection();
9855 if (X_DISPLAY == NULL)
9856 {
9857 EMSG(_("E240: No connection to Vim server"));
9858 return FAIL;
9859 }
9860 return OK;
9861}
9862#endif
9863
9864#ifdef FEAT_CLIENTSERVER
9865static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
9866
9867 static void
9868remote_common(argvars, rettv, expr)
9869 typeval *argvars;
9870 typeval *rettv;
9871 int expr;
9872{
9873 char_u *server_name;
9874 char_u *keys;
9875 char_u *r = NULL;
9876 char_u buf[NUMBUFLEN];
9877# ifdef WIN32
9878 HWND w;
9879# else
9880 Window w;
9881# endif
9882
9883 if (check_restricted() || check_secure())
9884 return;
9885
9886# ifdef FEAT_X11
9887 if (check_connection() == FAIL)
9888 return;
9889# endif
9890
9891 server_name = get_tv_string(&argvars[0]);
9892 keys = get_tv_string_buf(&argvars[1], buf);
9893# ifdef WIN32
9894 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
9895# else
9896 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
9897 < 0)
9898# endif
9899 {
9900 if (r != NULL)
9901 EMSG(r); /* sending worked but evaluation failed */
9902 else
9903 EMSG2(_("E241: Unable to send to %s"), server_name);
9904 return;
9905 }
9906
9907 rettv->vval.v_string = r;
9908
9909 if (argvars[2].v_type != VAR_UNKNOWN)
9910 {
9911 var v;
9912 char_u str[30];
9913
9914 sprintf((char *)str, "0x%x", (unsigned int)w);
9915 v.tv.v_type = VAR_STRING;
9916 v.tv.vval.v_string = vim_strsave(str);
9917 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
9918 vim_free(v.tv.vval.v_string);
9919 }
9920}
9921#endif
9922
9923/*
9924 * "remote_expr()" function
9925 */
9926/*ARGSUSED*/
9927 static void
9928f_remote_expr(argvars, rettv)
9929 typeval *argvars;
9930 typeval *rettv;
9931{
9932 rettv->v_type = VAR_STRING;
9933 rettv->vval.v_string = NULL;
9934#ifdef FEAT_CLIENTSERVER
9935 remote_common(argvars, rettv, TRUE);
9936#endif
9937}
9938
9939/*
9940 * "remote_foreground()" function
9941 */
9942/*ARGSUSED*/
9943 static void
9944f_remote_foreground(argvars, rettv)
9945 typeval *argvars;
9946 typeval *rettv;
9947{
9948 rettv->vval.v_number = 0;
9949#ifdef FEAT_CLIENTSERVER
9950# ifdef WIN32
9951 /* On Win32 it's done in this application. */
9952 serverForeground(get_tv_string(&argvars[0]));
9953# else
9954 /* Send a foreground() expression to the server. */
9955 argvars[1].v_type = VAR_STRING;
9956 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9957 argvars[2].v_type = VAR_UNKNOWN;
9958 remote_common(argvars, rettv, TRUE);
9959 vim_free(argvars[1].vval.v_string);
9960# endif
9961#endif
9962}
9963
9964/*ARGSUSED*/
9965 static void
9966f_remote_peek(argvars, rettv)
9967 typeval *argvars;
9968 typeval *rettv;
9969{
9970#ifdef FEAT_CLIENTSERVER
9971 var v;
9972 char_u *s = NULL;
9973# ifdef WIN32
9974 int n = 0;
9975# endif
9976
9977 if (check_restricted() || check_secure())
9978 {
9979 rettv->vval.v_number = -1;
9980 return;
9981 }
9982# ifdef WIN32
9983 sscanf(get_tv_string(&argvars[0]), "%x", &n);
9984 if (n == 0)
9985 rettv->vval.v_number = -1;
9986 else
9987 {
9988 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
9989 rettv->vval.v_number = (s != NULL);
9990 }
9991# else
9992 rettv->vval.v_number = 0;
9993 if (check_connection() == FAIL)
9994 return;
9995
9996 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9997 serverStrToWin(get_tv_string(&argvars[0])), &s);
9998# endif
9999
10000 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10001 {
10002 v.tv.v_type = VAR_STRING;
10003 v.tv.vval.v_string = vim_strsave(s);
10004 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
10005 vim_free(v.tv.vval.v_string);
10006 }
10007#else
10008 rettv->vval.v_number = -1;
10009#endif
10010}
10011
10012/*ARGSUSED*/
10013 static void
10014f_remote_read(argvars, rettv)
10015 typeval *argvars;
10016 typeval *rettv;
10017{
10018 char_u *r = NULL;
10019
10020#ifdef FEAT_CLIENTSERVER
10021 if (!check_restricted() && !check_secure())
10022 {
10023# ifdef WIN32
10024 /* The server's HWND is encoded in the 'id' parameter */
10025 int n = 0;
10026
10027 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10028 if (n != 0)
10029 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10030 if (r == NULL)
10031# else
10032 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10033 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10034# endif
10035 EMSG(_("E277: Unable to read a server reply"));
10036 }
10037#endif
10038 rettv->v_type = VAR_STRING;
10039 rettv->vval.v_string = r;
10040}
10041
10042/*
10043 * "remote_send()" function
10044 */
10045/*ARGSUSED*/
10046 static void
10047f_remote_send(argvars, rettv)
10048 typeval *argvars;
10049 typeval *rettv;
10050{
10051 rettv->v_type = VAR_STRING;
10052 rettv->vval.v_string = NULL;
10053#ifdef FEAT_CLIENTSERVER
10054 remote_common(argvars, rettv, FALSE);
10055#endif
10056}
10057
10058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010059 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010060 */
10061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010063 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010065{
10066 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010067 listitem *item, *item2;
10068 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010069 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010070 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010071 char_u *key;
10072 dictvar *d;
10073 dictitem *di, **pdi;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010074
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010075 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010076 if (argvars[0].v_type == VAR_DICT)
10077 {
10078 if (argvars[2].v_type != VAR_UNKNOWN)
10079 EMSG2(_(e_toomanyarg), "remove()");
10080 else if ((d = argvars[0].vval.v_dict) != NULL)
10081 {
10082 key = get_tv_string(&argvars[1]);
10083 pdi = &d->dv_first;
10084 for (di = d->dv_first; di != NULL; pdi = &di->di_next, di = *pdi)
10085 if (STRCMP(di->di_key, key) == 0)
10086 {
10087 *pdi = di->di_next;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010088 *rettv = di->di_tv;
10089 init_tv(&di->di_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010090 dictitem_free(di);
10091 break;
10092 }
10093 if (di == NULL)
10094 EMSG2(_(e_dictkey), key);
10095 }
10096 }
10097 else if (argvars[0].v_type != VAR_LIST)
10098 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010099 else if ((l = argvars[0].vval.v_list) != NULL)
10100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010102 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010103 if (item == NULL)
10104 EMSGN(_(e_listidx), idx);
10105 else
10106 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010107 if (argvars[2].v_type == VAR_UNKNOWN)
10108 {
10109 /* Remove one item, return its value. */
10110 list_getrem(l, item, item);
10111 *rettv = item->li_tv;
10112 vim_free(item);
10113 }
10114 else
10115 {
10116 /* Remove range of items, return list with values. */
10117 end = get_tv_number(&argvars[2]);
10118 item2 = list_find(l, end);
10119 if (item2 == NULL)
10120 EMSGN(_(e_listidx), end);
10121 else
10122 {
10123 for (li = item; li != item2 && li != NULL; li = li->li_next)
10124 ;
10125 if (li == NULL) /* didn't find "item2" after "item" */
10126 EMSG(_(e_invrange));
10127 else
10128 {
10129 list_getrem(l, item, item2);
10130 l = list_alloc();
10131 if (l != NULL)
10132 {
10133 rettv->v_type = VAR_LIST;
10134 rettv->vval.v_list = l;
10135 l->lv_first = item;
10136 l->lv_last = item2;
10137 l->lv_refcount = 1;
10138 item->li_prev = NULL;
10139 item2->li_next = NULL;
10140 }
10141 }
10142 }
10143 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010144 }
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146}
10147
10148/*
10149 * "rename({from}, {to})" function
10150 */
10151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010153 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
10156 char_u buf[NUMBUFLEN];
10157
10158 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10162 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163}
10164
10165/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010166 * "repeat()" function
10167 */
10168/*ARGSUSED*/
10169 static void
10170f_repeat(argvars, rettv)
10171 typeval *argvars;
10172 typeval *rettv;
10173{
10174 char_u *p;
10175 int n;
10176 int slen;
10177 int len;
10178 char_u *r;
10179 int i;
10180 listvar *l;
10181
10182 n = get_tv_number(&argvars[1]);
10183 if (argvars[0].v_type == VAR_LIST)
10184 {
10185 l = list_alloc();
10186 if (l != NULL && argvars[0].vval.v_list != NULL)
10187 {
10188 l->lv_refcount = 1;
10189 while (n-- > 0)
10190 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
10191 break;
10192 }
10193 rettv->v_type = VAR_LIST;
10194 rettv->vval.v_list = l;
10195 }
10196 else
10197 {
10198 p = get_tv_string(&argvars[0]);
10199 rettv->v_type = VAR_STRING;
10200 rettv->vval.v_string = NULL;
10201
10202 slen = (int)STRLEN(p);
10203 len = slen * n;
10204 if (len <= 0)
10205 return;
10206
10207 r = alloc(len + 1);
10208 if (r != NULL)
10209 {
10210 for (i = 0; i < n; i++)
10211 mch_memmove(r + i * slen, p, (size_t)slen);
10212 r[len] = NUL;
10213 }
10214
10215 rettv->vval.v_string = r;
10216 }
10217}
10218
10219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 * "resolve()" function
10221 */
10222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010224 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 char_u *p;
10228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230#ifdef FEAT_SHORTCUT
10231 {
10232 char_u *v = NULL;
10233
10234 v = mch_resolve_shortcut(p);
10235 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010236 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240#else
10241# ifdef HAVE_READLINK
10242 {
10243 char_u buf[MAXPATHL + 1];
10244 char_u *cpy;
10245 int len;
10246 char_u *remain = NULL;
10247 char_u *q;
10248 int is_relative_to_current = FALSE;
10249 int has_trailing_pathsep = FALSE;
10250 int limit = 100;
10251
10252 p = vim_strsave(p);
10253
10254 if (p[0] == '.' && (vim_ispathsep(p[1])
10255 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10256 is_relative_to_current = TRUE;
10257
10258 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010259 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 has_trailing_pathsep = TRUE;
10261
10262 q = getnextcomp(p);
10263 if (*q != NUL)
10264 {
10265 /* Separate the first path component in "p", and keep the
10266 * remainder (beginning with the path separator). */
10267 remain = vim_strsave(q - 1);
10268 q[-1] = NUL;
10269 }
10270
10271 for (;;)
10272 {
10273 for (;;)
10274 {
10275 len = readlink((char *)p, (char *)buf, MAXPATHL);
10276 if (len <= 0)
10277 break;
10278 buf[len] = NUL;
10279
10280 if (limit-- == 0)
10281 {
10282 vim_free(p);
10283 vim_free(remain);
10284 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 goto fail;
10287 }
10288
10289 /* Ensure that the result will have a trailing path separator
10290 * if the argument has one. */
10291 if (remain == NULL && has_trailing_pathsep)
10292 add_pathsep(buf);
10293
10294 /* Separate the first path component in the link value and
10295 * concatenate the remainders. */
10296 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10297 if (*q != NUL)
10298 {
10299 if (remain == NULL)
10300 remain = vim_strsave(q - 1);
10301 else
10302 {
10303 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
10304 if (cpy != NULL)
10305 {
10306 STRCAT(cpy, remain);
10307 vim_free(remain);
10308 remain = cpy;
10309 }
10310 }
10311 q[-1] = NUL;
10312 }
10313
10314 q = gettail(p);
10315 if (q > p && *q == NUL)
10316 {
10317 /* Ignore trailing path separator. */
10318 q[-1] = NUL;
10319 q = gettail(p);
10320 }
10321 if (q > p && !mch_isFullName(buf))
10322 {
10323 /* symlink is relative to directory of argument */
10324 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10325 if (cpy != NULL)
10326 {
10327 STRCPY(cpy, p);
10328 STRCPY(gettail(cpy), buf);
10329 vim_free(p);
10330 p = cpy;
10331 }
10332 }
10333 else
10334 {
10335 vim_free(p);
10336 p = vim_strsave(buf);
10337 }
10338 }
10339
10340 if (remain == NULL)
10341 break;
10342
10343 /* Append the first path component of "remain" to "p". */
10344 q = getnextcomp(remain + 1);
10345 len = q - remain - (*q != NUL);
10346 cpy = vim_strnsave(p, STRLEN(p) + len);
10347 if (cpy != NULL)
10348 {
10349 STRNCAT(cpy, remain, len);
10350 vim_free(p);
10351 p = cpy;
10352 }
10353 /* Shorten "remain". */
10354 if (*q != NUL)
10355 STRCPY(remain, q - 1);
10356 else
10357 {
10358 vim_free(remain);
10359 remain = NULL;
10360 }
10361 }
10362
10363 /* If the result is a relative path name, make it explicitly relative to
10364 * the current directory if and only if the argument had this form. */
10365 if (!vim_ispathsep(*p))
10366 {
10367 if (is_relative_to_current
10368 && *p != NUL
10369 && !(p[0] == '.'
10370 && (p[1] == NUL
10371 || vim_ispathsep(p[1])
10372 || (p[1] == '.'
10373 && (p[2] == NUL
10374 || vim_ispathsep(p[2]))))))
10375 {
10376 /* Prepend "./". */
10377 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
10378 if (cpy != NULL)
10379 {
10380 STRCAT(cpy, p);
10381 vim_free(p);
10382 p = cpy;
10383 }
10384 }
10385 else if (!is_relative_to_current)
10386 {
10387 /* Strip leading "./". */
10388 q = p;
10389 while (q[0] == '.' && vim_ispathsep(q[1]))
10390 q += 2;
10391 if (q > p)
10392 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10393 }
10394 }
10395
10396 /* Ensure that the result will have no trailing path separator
10397 * if the argument had none. But keep "/" or "//". */
10398 if (!has_trailing_pathsep)
10399 {
10400 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010401 if (after_pathsep(p, q))
10402 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 }
10404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
10407# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409# endif
10410#endif
10411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413
10414#ifdef HAVE_READLINK
10415fail:
10416#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
10419
10420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010421 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 */
10423 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010424f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010425 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010428 listvar *l;
10429 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
Bram Moolenaar0d660222005-01-07 21:51:51 +000010431 rettv->vval.v_number = 0;
10432 if (argvars[0].v_type != VAR_LIST)
10433 EMSG2(_(e_listarg), "reverse()");
10434 else if ((l = argvars[0].vval.v_list) != NULL)
10435 {
10436 li = l->lv_last;
10437 l->lv_first = l->lv_last = li;
10438 while (li != NULL)
10439 {
10440 ni = li->li_prev;
10441 list_append(l, li);
10442 li = ni;
10443 }
10444 rettv->vval.v_list = l;
10445 rettv->v_type = VAR_LIST;
10446 ++l->lv_refcount;
10447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448}
10449
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010450#define SP_NOMOVE 1 /* don't move cursor */
10451#define SP_REPEAT 2 /* repeat to find outer pair */
10452#define SP_RETCOUNT 4 /* return matchcount */
10453
Bram Moolenaar0d660222005-01-07 21:51:51 +000010454static int get_search_arg __ARGS((typeval *varp, int *flagsp));
10455
10456/*
10457 * Get flags for a search function.
10458 * Possibly sets "p_ws".
10459 * Returns BACKWARD, FORWARD or zero (for an error).
10460 */
10461 static int
10462get_search_arg(varp, flagsp)
10463 typeval *varp;
10464 int *flagsp;
10465{
10466 int dir = FORWARD;
10467 char_u *flags;
10468 char_u nbuf[NUMBUFLEN];
10469 int mask;
10470
10471 if (varp->v_type != VAR_UNKNOWN)
10472 {
10473 flags = get_tv_string_buf(varp, nbuf);
10474 while (*flags != NUL)
10475 {
10476 switch (*flags)
10477 {
10478 case 'b': dir = BACKWARD; break;
10479 case 'w': p_ws = TRUE; break;
10480 case 'W': p_ws = FALSE; break;
10481 default: mask = 0;
10482 if (flagsp != NULL)
10483 switch (*flags)
10484 {
10485 case 'n': mask = SP_NOMOVE; break;
10486 case 'r': mask = SP_REPEAT; break;
10487 case 'm': mask = SP_RETCOUNT; break;
10488 }
10489 if (mask == 0)
10490 {
10491 EMSG2(_(e_invarg2), flags);
10492 dir = 0;
10493 }
10494 else
10495 *flagsp |= mask;
10496 }
10497 if (dir == 0)
10498 break;
10499 ++flags;
10500 }
10501 }
10502 return dir;
10503}
10504
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505/*
10506 * "search()" function
10507 */
10508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010510 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512{
10513 char_u *pat;
10514 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010515 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 int save_p_ws = p_ws;
10517 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010518 int flags = 0;
10519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010523 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
10524 if (dir == 0)
10525 goto theend;
10526 if ((flags & ~SP_NOMOVE) != 0)
10527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010529 goto theend;
10530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010532 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
10534 SEARCH_KEEP, RE_SEARCH) != FAIL)
10535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 curwin->w_cursor = pos;
10538 /* "/$" will put the cursor after the end of the line, may need to
10539 * correct that here */
10540 check_cursor();
10541 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010542
10543 /* If 'n' flag is used: restore cursor position. */
10544 if (flags & SP_NOMOVE)
10545 curwin->w_cursor = save_cursor;
10546theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 p_ws = save_p_ws;
10548}
10549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550/*
10551 * "searchpair()" function
10552 */
10553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010555 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557{
10558 char_u *spat, *mpat, *epat;
10559 char_u *skip;
10560 char_u *pat, *pat2, *pat3;
10561 pos_T pos;
10562 pos_T firstpos;
10563 pos_T save_cursor;
10564 pos_T save_pos;
10565 int save_p_ws = p_ws;
10566 char_u *save_cpo;
10567 int dir;
10568 int flags = 0;
10569 char_u nbuf1[NUMBUFLEN];
10570 char_u nbuf2[NUMBUFLEN];
10571 char_u nbuf3[NUMBUFLEN];
10572 int n;
10573 int r;
10574 int nest = 1;
10575 int err;
10576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
10579 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10580 save_cpo = p_cpo;
10581 p_cpo = (char_u *)"";
10582
10583 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 spat = get_tv_string(&argvars[0]);
10585 mpat = get_tv_string_buf(&argvars[1], nbuf1);
10586 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587
10588 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10589 * start/middle/end (pat3, for the top pair). */
10590 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
10591 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
10592 if (pat2 == NULL || pat3 == NULL)
10593 goto theend;
10594 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
10595 if (*mpat == NUL)
10596 STRCPY(pat3, pat2);
10597 else
10598 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
10599 spat, epat, mpat);
10600
10601 /* Handle the optional fourth argument: flags */
10602 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010603 if (dir == 0)
10604 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
10606 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010607 if (argvars[3].v_type == VAR_UNKNOWN
10608 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 skip = (char_u *)"";
10610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612
10613 save_cursor = curwin->w_cursor;
10614 pos = curwin->w_cursor;
10615 firstpos.lnum = 0;
10616 pat = pat3;
10617 for (;;)
10618 {
10619 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
10620 SEARCH_KEEP, RE_SEARCH);
10621 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
10622 /* didn't find it or found the first match again: FAIL */
10623 break;
10624
10625 if (firstpos.lnum == 0)
10626 firstpos = pos;
10627
10628 /* If the skip pattern matches, ignore this match. */
10629 if (*skip != NUL)
10630 {
10631 save_pos = curwin->w_cursor;
10632 curwin->w_cursor = pos;
10633 r = eval_to_bool(skip, &err, NULL, FALSE);
10634 curwin->w_cursor = save_pos;
10635 if (err)
10636 {
10637 /* Evaluating {skip} caused an error, break here. */
10638 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 break;
10641 }
10642 if (r)
10643 continue;
10644 }
10645
10646 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10647 {
10648 /* Found end when searching backwards or start when searching
10649 * forward: nested pair. */
10650 ++nest;
10651 pat = pat2; /* nested, don't search for middle */
10652 }
10653 else
10654 {
10655 /* Found end when searching forward or start when searching
10656 * backward: end of (nested) pair; or found middle in outer pair. */
10657 if (--nest == 1)
10658 pat = pat3; /* outer level, search for middle */
10659 }
10660
10661 if (nest == 0)
10662 {
10663 /* Found the match: return matchcount or line number. */
10664 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 curwin->w_cursor = pos;
10669 if (!(flags & SP_REPEAT))
10670 break;
10671 nest = 1; /* search for next unmatched */
10672 }
10673 }
10674
10675 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 curwin->w_cursor = save_cursor;
10678
10679theend:
10680 vim_free(pat2);
10681 vim_free(pat3);
10682 p_ws = save_p_ws;
10683 p_cpo = save_cpo;
10684}
10685
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686/*ARGSUSED*/
10687 static void
10688f_server2client(argvars, rettv)
10689 typeval *argvars;
10690 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010692#ifdef FEAT_CLIENTSERVER
10693 char_u buf[NUMBUFLEN];
10694 char_u *server = get_tv_string(&argvars[0]);
10695 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
Bram Moolenaar0d660222005-01-07 21:51:51 +000010697 rettv->vval.v_number = -1;
10698 if (check_restricted() || check_secure())
10699 return;
10700# ifdef FEAT_X11
10701 if (check_connection() == FAIL)
10702 return;
10703# endif
10704
10705 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707 EMSG(_("E258: Unable to send to client"));
10708 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010710 rettv->vval.v_number = 0;
10711#else
10712 rettv->vval.v_number = -1;
10713#endif
10714}
10715
10716/*ARGSUSED*/
10717 static void
10718f_serverlist(argvars, rettv)
10719 typeval *argvars;
10720 typeval *rettv;
10721{
10722 char_u *r = NULL;
10723
10724#ifdef FEAT_CLIENTSERVER
10725# ifdef WIN32
10726 r = serverGetVimNames();
10727# else
10728 make_connection();
10729 if (X_DISPLAY != NULL)
10730 r = serverGetVimNames(X_DISPLAY);
10731# endif
10732#endif
10733 rettv->v_type = VAR_STRING;
10734 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735}
10736
10737/*
10738 * "setbufvar()" function
10739 */
10740/*ARGSUSED*/
10741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010743 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
10746 buf_T *buf;
10747#ifdef FEAT_AUTOCMD
10748 aco_save_T aco;
10749#else
10750 buf_T *save_curbuf;
10751#endif
10752 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010753 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 char_u nbuf[NUMBUFLEN];
10755
10756 if (check_restricted() || check_secure())
10757 return;
10758 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 buf = get_buf_tv(&argvars[0]);
10760 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 varp = &argvars[2];
10762
10763 if (buf != NULL && varname != NULL && varp != NULL)
10764 {
10765 /* set curbuf to be our buf, temporarily */
10766#ifdef FEAT_AUTOCMD
10767 aucmd_prepbuf(&aco, buf);
10768#else
10769 save_curbuf = curbuf;
10770 curbuf = buf;
10771#endif
10772
10773 if (*varname == '&')
10774 {
10775 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 set_option_value(varname, get_tv_number(varp),
10777 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 }
10779 else
10780 {
10781 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10782 if (bufvarname != NULL)
10783 {
10784 STRCPY(bufvarname, "b:");
10785 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010786 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 vim_free(bufvarname);
10788 }
10789 }
10790
10791 /* reset notion of buffer */
10792#ifdef FEAT_AUTOCMD
10793 aucmd_restbuf(&aco);
10794#else
10795 curbuf = save_curbuf;
10796#endif
10797 }
10798 --emsg_off;
10799}
10800
10801/*
10802 * "setcmdpos()" function
10803 */
10804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010806 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_number = set_cmdline_pos(
10810 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811}
10812
10813/*
10814 * "setline()" function
10815 */
10816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010818 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 linenr_T lnum;
10822 char_u *line;
10823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 lnum = get_tv_lnum(argvars);
10825 line = get_tv_string(&argvars[1]);
10826 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827
10828 if (lnum >= 1
10829 && lnum <= curbuf->b_ml.ml_line_count
10830 && u_savesub(lnum) == OK
10831 && ml_replace(lnum, line, TRUE) == OK)
10832 {
10833 changed_bytes(lnum, 0);
10834 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 }
10837}
10838
10839/*
10840 * "setreg()" function
10841 */
10842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010844 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846{
10847 int regname;
10848 char_u *strregname;
10849 char_u *stropt;
10850 int append;
10851 char_u yank_type;
10852 long block_len;
10853
10854 block_len = -1;
10855 yank_type = MAUTO;
10856 append = FALSE;
10857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 strregname = get_tv_string(argvars);
10859 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860
10861 regname = (strregname == NULL ? '"' : *strregname);
10862 if (regname == 0 || regname == '@')
10863 regname = '"';
10864 else if (regname == '=')
10865 return;
10866
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 switch (*stropt)
10871 {
10872 case 'a': case 'A': /* append */
10873 append = TRUE;
10874 break;
10875 case 'v': case 'c': /* character-wise selection */
10876 yank_type = MCHAR;
10877 break;
10878 case 'V': case 'l': /* line-wise selection */
10879 yank_type = MLINE;
10880 break;
10881#ifdef FEAT_VISUAL
10882 case 'b': case Ctrl_V: /* block-wise selection */
10883 yank_type = MBLOCK;
10884 if (VIM_ISDIGIT(stropt[1]))
10885 {
10886 ++stropt;
10887 block_len = getdigits(&stropt) - 1;
10888 --stropt;
10889 }
10890 break;
10891#endif
10892 }
10893 }
10894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898}
10899
10900
10901/*
10902 * "setwinvar(expr)" function
10903 */
10904/*ARGSUSED*/
10905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010907 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909{
10910 win_T *win;
10911#ifdef FEAT_WINDOWS
10912 win_T *save_curwin;
10913#endif
10914 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010915 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 char_u nbuf[NUMBUFLEN];
10917
10918 if (check_restricted() || check_secure())
10919 return;
10920 ++emsg_off;
10921 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 varp = &argvars[2];
10924
10925 if (win != NULL && varname != NULL && varp != NULL)
10926 {
10927#ifdef FEAT_WINDOWS
10928 /* set curwin to be our win, temporarily */
10929 save_curwin = curwin;
10930 curwin = win;
10931 curbuf = curwin->w_buffer;
10932#endif
10933
10934 if (*varname == '&')
10935 {
10936 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 set_option_value(varname, get_tv_number(varp),
10938 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 }
10940 else
10941 {
10942 winvarname = alloc((unsigned)STRLEN(varname) + 3);
10943 if (winvarname != NULL)
10944 {
10945 STRCPY(winvarname, "w:");
10946 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010947 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 vim_free(winvarname);
10949 }
10950 }
10951
10952#ifdef FEAT_WINDOWS
10953 /* Restore current window, if it's still valid (autocomands can make
10954 * it invalid). */
10955 if (win_valid(save_curwin))
10956 {
10957 curwin = save_curwin;
10958 curbuf = curwin->w_buffer;
10959 }
10960#endif
10961 }
10962 --emsg_off;
10963}
10964
10965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010966 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 */
10968 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010969f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010970 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974
Bram Moolenaar0d660222005-01-07 21:51:51 +000010975 p = get_tv_string(&argvars[0]);
10976 rettv->vval.v_string = vim_strsave(p);
10977 simplify_filename(rettv->vval.v_string); /* simplify in place */
10978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979}
10980
Bram Moolenaar0d660222005-01-07 21:51:51 +000010981static int
10982#ifdef __BORLANDC__
10983 _RTLENTRYF
10984#endif
10985 item_compare __ARGS((const void *s1, const void *s2));
10986static int
10987#ifdef __BORLANDC__
10988 _RTLENTRYF
10989#endif
10990 item_compare2 __ARGS((const void *s1, const void *s2));
10991
10992static int item_compare_ic;
10993static char_u *item_compare_func;
10994#define ITEM_COMPARE_FAIL 999
10995
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010997 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010999 static int
11000#ifdef __BORLANDC__
11001_RTLENTRYF
11002#endif
11003item_compare(s1, s2)
11004 const void *s1;
11005 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007 char_u *p1, *p2;
11008 char_u *tofree1, *tofree2;
11009 int res;
11010 char_u numbuf1[NUMBUFLEN];
11011 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
11014 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
11015 if (item_compare_ic)
11016 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011018 res = STRCMP(p1, p2);
11019 vim_free(tofree1);
11020 vim_free(tofree2);
11021 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022}
11023
11024 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011025#ifdef __BORLANDC__
11026_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011028item_compare2(s1, s2)
11029 const void *s1;
11030 const void *s2;
11031{
11032 int res;
11033 typeval rettv;
11034 typeval argv[2];
11035 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036
Bram Moolenaar0d660222005-01-07 21:51:51 +000011037 /* copy the values (is this really needed?) */
11038 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
11039 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
11040
11041 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11042 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044 clear_tv(&argv[0]);
11045 clear_tv(&argv[1]);
11046
11047 if (res == FAIL)
11048 res = ITEM_COMPARE_FAIL;
11049 else
11050 res = get_tv_number(&rettv);
11051 clear_tv(&rettv);
11052 return res;
11053}
11054
11055/*
11056 * "sort({list})" function
11057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011059f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011063 listvar *l;
11064 listitem *li;
11065 listitem **ptrs;
11066 long len;
11067 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068
Bram Moolenaar0d660222005-01-07 21:51:51 +000011069 rettv->vval.v_number = 0;
11070 if (argvars[0].v_type != VAR_LIST)
11071 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 else
11073 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011074 l = argvars[0].vval.v_list;
11075 if (l == NULL)
11076 return;
11077 rettv->vval.v_list = l;
11078 rettv->v_type = VAR_LIST;
11079 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080
Bram Moolenaar0d660222005-01-07 21:51:51 +000011081 len = list_len(l);
11082 if (len <= 1)
11083 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084
Bram Moolenaar0d660222005-01-07 21:51:51 +000011085 item_compare_ic = FALSE;
11086 item_compare_func = NULL;
11087 if (argvars[1].v_type != VAR_UNKNOWN)
11088 {
11089 if (argvars[1].v_type == VAR_FUNC)
11090 item_compare_func = argvars[0].vval.v_string;
11091 else
11092 {
11093 i = get_tv_number(&argvars[1]);
11094 if (i == 1)
11095 item_compare_ic = TRUE;
11096 else
11097 item_compare_func = get_tv_string(&argvars[1]);
11098 }
11099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100
Bram Moolenaar0d660222005-01-07 21:51:51 +000011101 /* Make an array with each entry pointing to an item in the List. */
11102 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
11103 if (ptrs == NULL)
11104 return;
11105 i = 0;
11106 for (li = l->lv_first; li != NULL; li = li->li_next)
11107 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109 /* test the compare function */
11110 if (item_compare_func != NULL
11111 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11112 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011113 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115 {
11116 /* Sort the array with item pointers. */
11117 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
11118 item_compare_func == NULL ? item_compare : item_compare2);
11119
11120 /* Clear the List and append the items in the sorted order. */
11121 l->lv_first = l->lv_last = NULL;
11122 for (i = 0; i < len; ++i)
11123 list_append(l, ptrs[i]);
11124 }
11125
11126 vim_free(ptrs);
11127 }
11128}
11129
11130 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011131f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132 typeval *argvars;
11133 typeval *rettv;
11134{
11135 char_u *str;
11136 char_u *end;
11137 char_u *pat;
11138 regmatch_T regmatch;
11139 char_u patbuf[NUMBUFLEN];
11140 char_u *save_cpo;
11141 int match;
11142 listitem *ni;
11143 listvar *l;
11144 colnr_T col = 0;
11145
11146 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11147 save_cpo = p_cpo;
11148 p_cpo = (char_u *)"";
11149
11150 str = get_tv_string(&argvars[0]);
11151 if (argvars[1].v_type == VAR_UNKNOWN)
11152 pat = (char_u *)"[\\x01- ]\\+";
11153 else
11154 pat = get_tv_string_buf(&argvars[1], patbuf);
11155
11156 l = list_alloc();
11157 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 rettv->v_type = VAR_LIST;
11160 rettv->vval.v_list = l;
11161 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
Bram Moolenaar0d660222005-01-07 21:51:51 +000011163 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11164 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011166 regmatch.rm_ic = FALSE;
11167 while (*str != NUL)
11168 {
11169 match = vim_regexec_nl(&regmatch, str, col);
11170 if (match)
11171 end = regmatch.startp[0];
11172 else
11173 end = str + STRLEN(str);
11174 if (end > str)
11175 {
11176 ni = listitem_alloc();
11177 if (ni == NULL)
11178 break;
11179 ni->li_tv.v_type = VAR_STRING;
11180 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
11181 list_append(l, ni);
11182 }
11183 if (!match)
11184 break;
11185 /* Advance to just after the match. */
11186 if (regmatch.endp[0] > str)
11187 col = 0;
11188 else
11189 {
11190 /* Don't get stuck at the same match. */
11191#ifdef FEAT_MBYTE
11192 col = mb_ptr2len_check(regmatch.endp[0]);
11193#else
11194 col = 1;
11195#endif
11196 }
11197 str = regmatch.endp[0];
11198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202
Bram Moolenaar0d660222005-01-07 21:51:51 +000011203 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204}
11205
11206#ifdef HAVE_STRFTIME
11207/*
11208 * "strftime({format}[, {time}])" function
11209 */
11210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011212 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214{
11215 char_u result_buf[256];
11216 struct tm *curtime;
11217 time_t seconds;
11218 char_u *p;
11219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011223 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 seconds = time(NULL);
11225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 curtime = localtime(&seconds);
11228 /* MSVC returns NULL for an invalid value of seconds. */
11229 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011230 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 else
11232 {
11233# ifdef FEAT_MBYTE
11234 vimconv_T conv;
11235 char_u *enc;
11236
11237 conv.vc_type = CONV_NONE;
11238 enc = enc_locale();
11239 convert_setup(&conv, p_enc, enc);
11240 if (conv.vc_type != CONV_NONE)
11241 p = string_convert(&conv, p, NULL);
11242# endif
11243 if (p != NULL)
11244 (void)strftime((char *)result_buf, sizeof(result_buf),
11245 (char *)p, curtime);
11246 else
11247 result_buf[0] = NUL;
11248
11249# ifdef FEAT_MBYTE
11250 if (conv.vc_type != CONV_NONE)
11251 vim_free(p);
11252 convert_setup(&conv, enc, p_enc);
11253 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 else
11256# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258
11259# ifdef FEAT_MBYTE
11260 /* Release conversion descriptors */
11261 convert_setup(&conv, NULL, NULL);
11262 vim_free(enc);
11263# endif
11264 }
11265}
11266#endif
11267
11268/*
11269 * "stridx()" function
11270 */
11271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011273 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275{
11276 char_u buf[NUMBUFLEN];
11277 char_u *needle;
11278 char_u *haystack;
11279 char_u *pos;
11280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 needle = get_tv_string(&argvars[1]);
11282 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11284
11285 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289}
11290
11291/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011292 * "string()" function
11293 */
11294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011296 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011298{
11299 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011300 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011303 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011304 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306}
11307
11308/*
11309 * "strlen()" function
11310 */
11311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011313 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = (varnumber_T)(STRLEN(
11317 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318}
11319
11320/*
11321 * "strpart()" function
11322 */
11323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011325 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328 char_u *p;
11329 int n;
11330 int len;
11331 int slen;
11332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 slen = (int)STRLEN(p);
11335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011337 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 else
11340 len = slen - n; /* default len: all bytes that are available. */
11341
11342 /*
11343 * Only return the overlap between the specified part and the actual
11344 * string.
11345 */
11346 if (n < 0)
11347 {
11348 len += n;
11349 n = 0;
11350 }
11351 else if (n > slen)
11352 n = slen;
11353 if (len < 0)
11354 len = 0;
11355 else if (n + len > slen)
11356 len = slen - n;
11357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->v_type = VAR_STRING;
11359 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360}
11361
11362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363 * "strridx()" function
11364 */
11365 static void
11366f_strridx(argvars, rettv)
11367 typeval *argvars;
11368 typeval *rettv;
11369{
11370 char_u buf[NUMBUFLEN];
11371 char_u *needle;
11372 char_u *haystack;
11373 char_u *rest;
11374 char_u *lastmatch = NULL;
11375
11376 needle = get_tv_string(&argvars[1]);
11377 haystack = get_tv_string_buf(&argvars[0], buf);
11378 if (*needle == NUL)
11379 /* Empty string matches past the end. */
11380 lastmatch = haystack + STRLEN(haystack);
11381 else
11382 for (rest = haystack; *rest != '\0'; ++rest)
11383 {
11384 rest = (char_u *)strstr((char *)rest, (char *)needle);
11385 if (rest == NULL)
11386 break;
11387 lastmatch = rest;
11388 }
11389
11390 if (lastmatch == NULL)
11391 rettv->vval.v_number = -1;
11392 else
11393 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11394}
11395
11396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 * "strtrans()" function
11398 */
11399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->v_type = VAR_STRING;
11405 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406}
11407
11408/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011409 * "submatch()" function
11410 */
11411 static void
11412f_submatch(argvars, rettv)
11413 typeval *argvars;
11414 typeval *rettv;
11415{
11416 rettv->v_type = VAR_STRING;
11417 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11418}
11419
11420/*
11421 * "substitute()" function
11422 */
11423 static void
11424f_substitute(argvars, rettv)
11425 typeval *argvars;
11426 typeval *rettv;
11427{
11428 char_u patbuf[NUMBUFLEN];
11429 char_u subbuf[NUMBUFLEN];
11430 char_u flagsbuf[NUMBUFLEN];
11431
11432 rettv->v_type = VAR_STRING;
11433 rettv->vval.v_string = do_string_sub(
11434 get_tv_string(&argvars[0]),
11435 get_tv_string_buf(&argvars[1], patbuf),
11436 get_tv_string_buf(&argvars[2], subbuf),
11437 get_tv_string_buf(&argvars[3], flagsbuf));
11438}
11439
11440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 * "synID(line, col, trans)" function
11442 */
11443/*ARGSUSED*/
11444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011446 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448{
11449 int id = 0;
11450#ifdef FEAT_SYN_HL
11451 long line;
11452 long col;
11453 int trans;
11454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 line = get_tv_lnum(argvars);
11456 col = get_tv_number(&argvars[1]) - 1;
11457 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
11459 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
11460 && col >= 0 && col < (long)STRLEN(ml_get(line)))
11461 id = syn_get_id(line, col, trans);
11462#endif
11463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
11468 * "synIDattr(id, what [, mode])" function
11469 */
11470/*ARGSUSED*/
11471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011473 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476 char_u *p = NULL;
11477#ifdef FEAT_SYN_HL
11478 int id;
11479 char_u *what;
11480 char_u *mode;
11481 char_u modebuf[NUMBUFLEN];
11482 int modec;
11483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 id = get_tv_number(&argvars[0]);
11485 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011486 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 modec = TOLOWER_ASC(mode[0]);
11490 if (modec != 't' && modec != 'c'
11491#ifdef FEAT_GUI
11492 && modec != 'g'
11493#endif
11494 )
11495 modec = 0; /* replace invalid with current */
11496 }
11497 else
11498 {
11499#ifdef FEAT_GUI
11500 if (gui.in_use)
11501 modec = 'g';
11502 else
11503#endif
11504 if (t_colors > 1)
11505 modec = 'c';
11506 else
11507 modec = 't';
11508 }
11509
11510
11511 switch (TOLOWER_ASC(what[0]))
11512 {
11513 case 'b':
11514 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11515 p = highlight_color(id, what, modec);
11516 else /* bold */
11517 p = highlight_has_attr(id, HL_BOLD, modec);
11518 break;
11519
11520 case 'f': /* fg[#] */
11521 p = highlight_color(id, what, modec);
11522 break;
11523
11524 case 'i':
11525 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11526 p = highlight_has_attr(id, HL_INVERSE, modec);
11527 else /* italic */
11528 p = highlight_has_attr(id, HL_ITALIC, modec);
11529 break;
11530
11531 case 'n': /* name */
11532 p = get_highlight_name(NULL, id - 1);
11533 break;
11534
11535 case 'r': /* reverse */
11536 p = highlight_has_attr(id, HL_INVERSE, modec);
11537 break;
11538
11539 case 's': /* standout */
11540 p = highlight_has_attr(id, HL_STANDOUT, modec);
11541 break;
11542
11543 case 'u': /* underline */
11544 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11545 break;
11546 }
11547
11548 if (p != NULL)
11549 p = vim_strsave(p);
11550#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 rettv->v_type = VAR_STRING;
11552 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553}
11554
11555/*
11556 * "synIDtrans(id)" function
11557 */
11558/*ARGSUSED*/
11559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011561 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
11564 int id;
11565
11566#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568
11569 if (id > 0)
11570 id = syn_get_final_id(id);
11571 else
11572#endif
11573 id = 0;
11574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576}
11577
11578/*
11579 * "system()" function
11580 */
11581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011583 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011586 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011588 char_u *infile = NULL;
11589 char_u buf[NUMBUFLEN];
11590 int err = FALSE;
11591 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011594 {
11595 /*
11596 * Write the string to a temp file, to be used for input of the shell
11597 * command.
11598 */
11599 if ((infile = vim_tempname('i')) == NULL)
11600 {
11601 EMSG(_(e_notmp));
11602 return;
11603 }
11604
11605 fd = mch_fopen((char *)infile, WRITEBIN);
11606 if (fd == NULL)
11607 {
11608 EMSG2(_(e_notopen), infile);
11609 goto done;
11610 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011612 if (fwrite(p, STRLEN(p), 1, fd) != 1)
11613 err = TRUE;
11614 if (fclose(fd) != 0)
11615 err = TRUE;
11616 if (err)
11617 {
11618 EMSG(_("E677: Error writing temp file"));
11619 goto done;
11620 }
11621 }
11622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011624
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625#ifdef USE_CR
11626 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011627 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 {
11629 char_u *s;
11630
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011631 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 {
11633 if (*s == CAR)
11634 *s = NL;
11635 }
11636 }
11637#else
11638# ifdef USE_CRNL
11639 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011640 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 {
11642 char_u *s, *d;
11643
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011644 d = res;
11645 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 {
11647 if (s[0] == CAR && s[1] == NL)
11648 ++s;
11649 *d++ = *s;
11650 }
11651 *d = NUL;
11652 }
11653# endif
11654#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011655
11656done:
11657 if (infile != NULL)
11658 {
11659 mch_remove(infile);
11660 vim_free(infile);
11661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662 rettv->v_type = VAR_STRING;
11663 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664}
11665
11666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 * "tempname()" function
11668 */
11669/*ARGSUSED*/
11670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
11675 static int x = 'A';
11676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677 rettv->v_type = VAR_STRING;
11678 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679
11680 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
11681 * names. Skip 'I' and 'O', they are used for shell redirection. */
11682 do
11683 {
11684 if (x == 'Z')
11685 x = '0';
11686 else if (x == '9')
11687 x = 'A';
11688 else
11689 {
11690#ifdef EBCDIC
11691 if (x == 'I')
11692 x = 'J';
11693 else if (x == 'R')
11694 x = 'S';
11695 else
11696#endif
11697 ++x;
11698 }
11699 } while (x == 'I' || x == 'O');
11700}
11701
11702/*
11703 * "tolower(string)" function
11704 */
11705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011707 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709{
11710 char_u *p;
11711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 p = vim_strsave(get_tv_string(&argvars[0]));
11713 rettv->v_type = VAR_STRING;
11714 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715
11716 if (p != NULL)
11717 while (*p != NUL)
11718 {
11719#ifdef FEAT_MBYTE
11720 int l;
11721
11722 if (enc_utf8)
11723 {
11724 int c, lc;
11725
11726 c = utf_ptr2char(p);
11727 lc = utf_tolower(c);
11728 l = utf_ptr2len_check(p);
11729 /* TODO: reallocate string when byte count changes. */
11730 if (utf_char2len(lc) == l)
11731 utf_char2bytes(lc, p);
11732 p += l;
11733 }
11734 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
11735 p += l; /* skip multi-byte character */
11736 else
11737#endif
11738 {
11739 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
11740 ++p;
11741 }
11742 }
11743}
11744
11745/*
11746 * "toupper(string)" function
11747 */
11748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011750 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752{
11753 char_u *p;
11754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755 p = vim_strsave(get_tv_string(&argvars[0]));
11756 rettv->v_type = VAR_STRING;
11757 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758
11759 if (p != NULL)
11760 while (*p != NUL)
11761 {
11762#ifdef FEAT_MBYTE
11763 int l;
11764
11765 if (enc_utf8)
11766 {
11767 int c, uc;
11768
11769 c = utf_ptr2char(p);
11770 uc = utf_toupper(c);
11771 l = utf_ptr2len_check(p);
11772 /* TODO: reallocate string when byte count changes. */
11773 if (utf_char2len(uc) == l)
11774 utf_char2bytes(uc, p);
11775 p += l;
11776 }
11777 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
11778 p += l; /* skip multi-byte character */
11779 else
11780#endif
11781 {
11782 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
11783 p++;
11784 }
11785 }
11786}
11787
11788/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000011789 * "tr(string, fromstr, tostr)" function
11790 */
11791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011793 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011795{
11796 char_u *instr;
11797 char_u *fromstr;
11798 char_u *tostr;
11799 char_u *p;
11800#ifdef FEAT_MBYTE
11801 int inlen;
11802 int fromlen;
11803 int tolen;
11804 int idx;
11805 char_u *cpstr;
11806 int cplen;
11807 int first = TRUE;
11808#endif
11809 char_u buf[NUMBUFLEN];
11810 char_u buf2[NUMBUFLEN];
11811 garray_T ga;
11812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 instr = get_tv_string(&argvars[0]);
11814 fromstr = get_tv_string_buf(&argvars[1], buf);
11815 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000011816
11817 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->v_type = VAR_STRING;
11819 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011820 ga_init2(&ga, (int)sizeof(char), 80);
11821
11822#ifdef FEAT_MBYTE
11823 if (!has_mbyte)
11824#endif
11825 /* not multi-byte: fromstr and tostr must be the same length */
11826 if (STRLEN(fromstr) != STRLEN(tostr))
11827 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011828#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000011829error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011830#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000011831 EMSG2(_(e_invarg2), fromstr);
11832 ga_clear(&ga);
11833 return;
11834 }
11835
11836 /* fromstr and tostr have to contain the same number of chars */
11837 while (*instr != NUL)
11838 {
11839#ifdef FEAT_MBYTE
11840 if (has_mbyte)
11841 {
11842 inlen = mb_ptr2len_check(instr);
11843 cpstr = instr;
11844 cplen = inlen;
11845 idx = 0;
11846 for (p = fromstr; *p != NUL; p += fromlen)
11847 {
11848 fromlen = mb_ptr2len_check(p);
11849 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
11850 {
11851 for (p = tostr; *p != NUL; p += tolen)
11852 {
11853 tolen = mb_ptr2len_check(p);
11854 if (idx-- == 0)
11855 {
11856 cplen = tolen;
11857 cpstr = p;
11858 break;
11859 }
11860 }
11861 if (*p == NUL) /* tostr is shorter than fromstr */
11862 goto error;
11863 break;
11864 }
11865 ++idx;
11866 }
11867
11868 if (first && cpstr == instr)
11869 {
11870 /* Check that fromstr and tostr have the same number of
11871 * (multi-byte) characters. Done only once when a character
11872 * of instr doesn't appear in fromstr. */
11873 first = FALSE;
11874 for (p = tostr; *p != NUL; p += tolen)
11875 {
11876 tolen = mb_ptr2len_check(p);
11877 --idx;
11878 }
11879 if (idx != 0)
11880 goto error;
11881 }
11882
11883 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000011884 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000011885 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011886
11887 instr += inlen;
11888 }
11889 else
11890#endif
11891 {
11892 /* When not using multi-byte chars we can do it faster. */
11893 p = vim_strchr(fromstr, *instr);
11894 if (p != NULL)
11895 ga_append(&ga, tostr[p - fromstr]);
11896 else
11897 ga_append(&ga, *instr);
11898 ++instr;
11899 }
11900 }
11901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011903}
11904
11905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 * "type(expr)" function
11907 */
11908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011910 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011913 int n;
11914
11915 switch (argvars[0].v_type)
11916 {
11917 case VAR_NUMBER: n = 0; break;
11918 case VAR_STRING: n = 1; break;
11919 case VAR_FUNC: n = 2; break;
11920 case VAR_LIST: n = 3; break;
11921 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
11922 }
11923 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924}
11925
11926/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011927 * "values(dict)" function
11928 */
11929 static void
11930f_values(argvars, rettv)
11931 typeval *argvars;
11932 typeval *rettv;
11933{
11934 dict_list(argvars, rettv, 1);
11935}
11936
11937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 * "virtcol(string)" function
11939 */
11940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011942 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944{
11945 colnr_T vcol = 0;
11946 pos_T *fp;
11947
11948 fp = var2fpos(&argvars[0], FALSE);
11949 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
11950 {
11951 getvvcol(curwin, fp, NULL, NULL, &vcol);
11952 ++vcol;
11953 }
11954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956}
11957
11958/*
11959 * "visualmode()" function
11960 */
11961/*ARGSUSED*/
11962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011964 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967#ifdef FEAT_VISUAL
11968 char_u str[2];
11969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 str[0] = curbuf->b_visual_mode_eval;
11972 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974
11975 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011976 if ((argvars[0].v_type == VAR_NUMBER
11977 && argvars[0].vval.v_number != 0)
11978 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 curbuf->b_visual_mode_eval = NUL;
11981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983#endif
11984}
11985
11986/*
11987 * "winbufnr(nr)" function
11988 */
11989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011991 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993{
11994 win_T *wp;
11995
11996 wp = find_win_by_nr(&argvars[0]);
11997 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001}
12002
12003/*
12004 * "wincol()" function
12005 */
12006/*ARGSUSED*/
12007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012009 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
12012 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014}
12015
12016/*
12017 * "winheight(nr)" function
12018 */
12019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012021 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023{
12024 win_T *wp;
12025
12026 wp = find_win_by_nr(&argvars[0]);
12027 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031}
12032
12033/*
12034 * "winline()" function
12035 */
12036/*ARGSUSED*/
12037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012039 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041{
12042 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044}
12045
12046/*
12047 * "winnr()" function
12048 */
12049/* ARGSUSED */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012052 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055 int nr = 1;
12056#ifdef FEAT_WINDOWS
12057 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012058 win_T *twin = curwin;
12059 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012061 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012064 if (STRCMP(arg, "$") == 0)
12065 twin = lastwin;
12066 else if (STRCMP(arg, "#") == 0)
12067 {
12068 twin = prevwin;
12069 if (prevwin == NULL)
12070 nr = 0;
12071 }
12072 else
12073 {
12074 EMSG2(_(e_invexpr2), arg);
12075 nr = 0;
12076 }
12077 }
12078
12079 if (nr > 0)
12080 for (wp = firstwin; wp != twin; wp = wp->w_next)
12081 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084}
12085
12086/*
12087 * "winrestcmd()" function
12088 */
12089/* ARGSUSED */
12090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012092 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094{
12095#ifdef FEAT_WINDOWS
12096 win_T *wp;
12097 int winnr = 1;
12098 garray_T ga;
12099 char_u buf[50];
12100
12101 ga_init2(&ga, (int)sizeof(char), 70);
12102 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12103 {
12104 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12105 ga_concat(&ga, buf);
12106# ifdef FEAT_VERTSPLIT
12107 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12108 ga_concat(&ga, buf);
12109# endif
12110 ++winnr;
12111 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012112 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119}
12120
12121/*
12122 * "winwidth(nr)" function
12123 */
12124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012126 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128{
12129 win_T *wp;
12130
12131 wp = find_win_by_nr(&argvars[0]);
12132 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 else
12135#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139#endif
12140}
12141
12142 static win_T *
12143find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012144 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
12146#ifdef FEAT_WINDOWS
12147 win_T *wp;
12148#endif
12149 int nr;
12150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152
12153#ifdef FEAT_WINDOWS
12154 if (nr == 0)
12155 return curwin;
12156
12157 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12158 if (--nr <= 0)
12159 break;
12160 return wp;
12161#else
12162 if (nr == 0 || nr == 1)
12163 return curwin;
12164 return NULL;
12165#endif
12166}
12167
12168/*
12169 * Translate a String variable into a position.
12170 */
12171 static pos_T *
12172var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 int lnum; /* TRUE when $ is last line */
12175{
12176 char_u *name;
12177 static pos_T pos;
12178 pos_T *pp;
12179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 if (name[0] == '.') /* cursor */
12182 return &curwin->w_cursor;
12183 if (name[0] == '\'') /* mark */
12184 {
12185 pp = getmark(name[1], FALSE);
12186 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
12187 return NULL;
12188 return pp;
12189 }
12190 if (name[0] == '$') /* last column or line */
12191 {
12192 if (lnum)
12193 {
12194 pos.lnum = curbuf->b_ml.ml_line_count;
12195 pos.col = 0;
12196 }
12197 else
12198 {
12199 pos.lnum = curwin->w_cursor.lnum;
12200 pos.col = (colnr_T)STRLEN(ml_get_curline());
12201 }
12202 return &pos;
12203 }
12204 return NULL;
12205}
12206
12207/*
12208 * Get the length of an environment variable name.
12209 * Advance "arg" to the first character after the name.
12210 * Return 0 for error.
12211 */
12212 static int
12213get_env_len(arg)
12214 char_u **arg;
12215{
12216 char_u *p;
12217 int len;
12218
12219 for (p = *arg; vim_isIDc(*p); ++p)
12220 ;
12221 if (p == *arg) /* no name found */
12222 return 0;
12223
12224 len = (int)(p - *arg);
12225 *arg = p;
12226 return len;
12227}
12228
12229/*
12230 * Get the length of the name of a function or internal variable.
12231 * "arg" is advanced to the first non-white character after the name.
12232 * Return 0 if something is wrong.
12233 */
12234 static int
12235get_id_len(arg)
12236 char_u **arg;
12237{
12238 char_u *p;
12239 int len;
12240
12241 /* Find the end of the name. */
12242 for (p = *arg; eval_isnamec(*p); ++p)
12243 ;
12244 if (p == *arg) /* no name found */
12245 return 0;
12246
12247 len = (int)(p - *arg);
12248 *arg = skipwhite(p);
12249
12250 return len;
12251}
12252
12253/*
12254 * Get the length of the name of a function.
12255 * "arg" is advanced to the first non-white character after the name.
12256 * Return 0 if something is wrong.
12257 * If the name contains 'magic' {}'s, expand them and return the
12258 * expanded name in an allocated string via 'alias' - caller must free.
12259 */
12260 static int
12261get_func_len(arg, alias, evaluate)
12262 char_u **arg;
12263 char_u **alias;
12264 int evaluate;
12265{
12266 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 char_u *p;
12268 char_u *expr_start;
12269 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
12271 *alias = NULL; /* default to no alias */
12272
12273 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
12274 && (*arg)[2] == (int)KE_SNR)
12275 {
12276 /* hard coded <SNR>, already translated */
12277 *arg += 3;
12278 return get_id_len(arg) + 3;
12279 }
12280 len = eval_fname_script(*arg);
12281 if (len > 0)
12282 {
12283 /* literal "<SID>", "s:" or "<SNR>" */
12284 *arg += len;
12285 }
12286
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 if (expr_start != NULL)
12292 {
12293 char_u *temp_string;
12294
12295 if (!evaluate)
12296 {
12297 len += (int)(p - *arg);
12298 *arg = skipwhite(p);
12299 return len;
12300 }
12301
12302 /*
12303 * Include any <SID> etc in the expanded string:
12304 * Thus the -len here.
12305 */
12306 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
12307 if (temp_string == NULL)
12308 return 0;
12309 *alias = temp_string;
12310 *arg = skipwhite(p);
12311 return (int)STRLEN(temp_string);
12312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
12314 len += get_id_len(arg);
12315 if (len == 0)
12316 EMSG2(_(e_invexpr2), *arg);
12317
12318 return len;
12319}
12320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321/*
12322 * Find the end of a variable or function name, taking care of magic braces.
12323 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
12324 * start and end of the first magic braces item.
12325 * Return a pointer to just after the name. Equal to "arg" if there is no
12326 * valid name.
12327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 char_u *arg;
12331 char_u **expr_start;
12332 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012333 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 int mb_nest = 0;
12336 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 char_u *p;
12338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 *expr_start = NULL;
12342 *expr_end = NULL;
12343 }
12344
12345 for (p = arg; *p != NUL
12346 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012347 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000012348 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 || mb_nest != 0
12350 || br_nest != 0); ++p)
12351 {
12352 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 if (*p == '[')
12355 ++br_nest;
12356 else if (*p == ']')
12357 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361 if (*p == '{')
12362 {
12363 mb_nest++;
12364 if (expr_start != NULL && *expr_start == NULL)
12365 *expr_start = p;
12366 }
12367 else if (*p == '}')
12368 {
12369 mb_nest--;
12370 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12371 *expr_end = p;
12372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 }
12375
12376 return p;
12377}
12378
12379/*
12380 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000012381 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 */
12383 static int
12384eval_isnamec(c)
12385 int c;
12386{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012387 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388}
12389
12390/*
12391 * Find a v: variable.
12392 * Return it's index, or -1 if not found.
12393 */
12394 static int
12395find_vim_var(name, len)
12396 char_u *name;
12397 int len; /* length of "name" */
12398{
12399 char_u *vname;
12400 int vlen;
12401 int i;
12402
12403 /*
12404 * Ignore "v:" for old built-in variables, require it for new ones.
12405 */
12406 if (name[0] == 'v' && name[1] == ':')
12407 {
12408 vname = name + 2;
12409 vlen = len - 2;
12410 }
12411 else
12412 {
12413 vname = name;
12414 vlen = len;
12415 }
12416 for (i = 0; i < VV_LEN; ++i)
12417 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
12418 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
12419 return i;
12420 return -1;
12421}
12422
12423/*
12424 * Set number v: variable to "val".
12425 */
12426 void
12427set_vim_var_nr(idx, val)
12428 int idx;
12429 long val;
12430{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432}
12433
12434/*
12435 * Get number v: variable value;
12436 */
12437 long
12438get_vim_var_nr(idx)
12439 int idx;
12440{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012441 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
12444/*
12445 * Set v:count, v:count1 and v:prevcount.
12446 */
12447 void
12448set_vcount(count, count1)
12449 long count;
12450 long count1;
12451{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012452 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
12453 vimvars[VV_COUNT].vv_nr = count;
12454 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455}
12456
12457/*
12458 * Set string v: variable to a copy of "val".
12459 */
12460 void
12461set_vim_var_string(idx, val, len)
12462 int idx;
12463 char_u *val;
12464 int len; /* length of "val" to use or -1 (whole string) */
12465{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012466 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012468 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012470 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012472 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473}
12474
12475/*
12476 * Set v:register if needed.
12477 */
12478 void
12479set_reg_var(c)
12480 int c;
12481{
12482 char_u regname;
12483
12484 if (c == 0 || c == ' ')
12485 regname = '"';
12486 else
12487 regname = c;
12488 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012489 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 set_vim_var_string(VV_REG, &regname, 1);
12491}
12492
12493/*
12494 * Get or set v:exception. If "oldval" == NULL, return the current value.
12495 * Otherwise, restore the value to "oldval" and return NULL.
12496 * Must always be called in pairs to save and restore v:exception! Does not
12497 * take care of memory allocations.
12498 */
12499 char_u *
12500v_exception(oldval)
12501 char_u *oldval;
12502{
12503 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012504 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505
Bram Moolenaare9a41262005-01-15 22:18:47 +000012506 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 return NULL;
12508}
12509
12510/*
12511 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
12512 * Otherwise, restore the value to "oldval" and return NULL.
12513 * Must always be called in pairs to save and restore v:throwpoint! Does not
12514 * take care of memory allocations.
12515 */
12516 char_u *
12517v_throwpoint(oldval)
12518 char_u *oldval;
12519{
12520 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012521 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522
Bram Moolenaare9a41262005-01-15 22:18:47 +000012523 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 return NULL;
12525}
12526
12527#if defined(FEAT_AUTOCMD) || defined(PROTO)
12528/*
12529 * Set v:cmdarg.
12530 * If "eap" != NULL, use "eap" to generate the value and return the old value.
12531 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
12532 * Must always be called in pairs!
12533 */
12534 char_u *
12535set_cmdarg(eap, oldarg)
12536 exarg_T *eap;
12537 char_u *oldarg;
12538{
12539 char_u *oldval;
12540 char_u *newval;
12541 unsigned len;
12542
Bram Moolenaare9a41262005-01-15 22:18:47 +000012543 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012544 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012546 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012547 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012548 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 }
12550
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012551 if (eap->force_bin == FORCE_BIN)
12552 len = 6;
12553 else if (eap->force_bin == FORCE_NOBIN)
12554 len = 8;
12555 else
12556 len = 0;
12557 if (eap->force_ff != 0)
12558 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
12559# ifdef FEAT_MBYTE
12560 if (eap->force_enc != 0)
12561 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
12562# endif
12563
12564 newval = alloc(len + 1);
12565 if (newval == NULL)
12566 return NULL;
12567
12568 if (eap->force_bin == FORCE_BIN)
12569 sprintf((char *)newval, " ++bin");
12570 else if (eap->force_bin == FORCE_NOBIN)
12571 sprintf((char *)newval, " ++nobin");
12572 else
12573 *newval = NUL;
12574 if (eap->force_ff != 0)
12575 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
12576 eap->cmd + eap->force_ff);
12577# ifdef FEAT_MBYTE
12578 if (eap->force_enc != 0)
12579 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
12580 eap->cmd + eap->force_enc);
12581# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000012582 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012583 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584}
12585#endif
12586
12587/*
12588 * Get the value of internal variable "name".
12589 * Return OK or FAIL.
12590 */
12591 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593 char_u *name;
12594 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012595 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596{
12597 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012598 typeval *tv = NULL;
12599 typeval atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 VAR v;
12601 int cc;
12602 int i;
12603
12604 /* truncate the name, so that we can use strcmp() */
12605 cc = name[len];
12606 name[len] = NUL;
12607
12608 /*
12609 * Check for "b:changedtick".
12610 */
12611 if (STRCMP(name, "b:changedtick") == 0)
12612 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012613 atv.v_type = VAR_NUMBER;
12614 atv.vval.v_number = curbuf->b_changedtick;
12615 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 }
12617
12618 /*
12619 * Check for built-in v: variables.
12620 */
12621 else if ((i = find_vim_var(name, len)) >= 0)
12622 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012623 tv = &vimvars[i].tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 }
12625
12626 /*
12627 * Check for user-defined variables.
12628 */
12629 else
12630 {
12631 v = find_var(name, FALSE);
12632 if (v != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012633 tv = &v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 }
12635
Bram Moolenaare9a41262005-01-15 22:18:47 +000012636 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 if (rettv != NULL)
12639 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 ret = FAIL;
12641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012643 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644
12645 name[len] = cc;
12646
12647 return ret;
12648}
12649
12650/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012651 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
12652 * value).
12653 */
12654 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012655alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012656{
12657 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
12658}
12659
12660/*
12661 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 * The string "s" must have been allocated, it is consumed.
12663 * Return NULL for out of memory, the variable otherwise.
12664 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012665 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 char_u *s;
12668{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012669 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv = alloc_tv();
12672 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->v_type = VAR_STRING;
12675 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 }
12677 else
12678 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680}
12681
12682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012683 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 */
12685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012687 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688{
12689 if (varp != NULL)
12690 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012691 switch (varp->v_type)
12692 {
12693 case VAR_STRING:
12694 case VAR_FUNC:
12695 vim_free(varp->vval.v_string);
12696 break;
12697 case VAR_LIST:
12698 list_unref(varp->vval.v_list);
12699 break;
12700 default:
12701 break;
12702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 vim_free(varp);
12704 }
12705}
12706
12707/*
12708 * Free the memory for a variable value and set the value to NULL or 0.
12709 */
12710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012712 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
12714 if (varp != NULL)
12715 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012716 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012718 case VAR_STRING:
12719 case VAR_FUNC:
12720 vim_free(varp->vval.v_string);
12721 varp->vval.v_string = NULL;
12722 break;
12723 case VAR_LIST:
12724 list_unref(varp->vval.v_list);
12725 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012726 case VAR_DICT:
12727 dict_unref(varp->vval.v_dict);
12728 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012730 varp->vval.v_number = 0;
12731 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 case VAR_UNKNOWN:
12733 break;
12734 default:
12735 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 }
12738}
12739
12740/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 * Set the value of a variable to NULL without freeing items.
12742 */
12743 static void
12744init_tv(varp)
12745 typeval *varp;
12746{
12747 if (varp != NULL)
12748 vim_memset(varp, 0, sizeof(typeval));
12749}
12750
12751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 * Get the number value of a variable.
12753 * If it is a String variable, uses vim_str2nr().
12754 */
12755 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012757 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012759 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012761 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012763 case VAR_NUMBER:
12764 n = (long)(varp->vval.v_number);
12765 break;
12766 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012767 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012768 break;
12769 case VAR_STRING:
12770 if (varp->vval.v_string != NULL)
12771 vim_str2nr(varp->vval.v_string, NULL, NULL,
12772 TRUE, TRUE, &n, NULL);
12773 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012774 case VAR_LIST:
12775 EMSG(_("E703: Using a List as a number"));
12776 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012777 default:
12778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012780 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781}
12782
12783/*
12784 * Get the lnum from the first argument. Also accepts ".", "$", etc.
12785 */
12786 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012788 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 linenr_T lnum;
12792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 if (lnum == 0) /* no valid number, try using line() */
12795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv.v_type = VAR_NUMBER;
12797 f_line(argvars, &rettv);
12798 lnum = rettv.vval.v_number;
12799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 }
12801 return lnum;
12802}
12803
12804/*
12805 * Get the string value of a variable.
12806 * If it is a Number variable, the number is converted into a string.
12807 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
12808 * get_var_string_buf() uses a given buffer.
12809 * If the String variable has never been set, return an empty string.
12810 * Never returns NULL;
12811 */
12812 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012814 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815{
12816 static char_u mybuf[NUMBUFLEN];
12817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819}
12820
12821 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012823 typeval *varp;
12824 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012826 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012828 case VAR_NUMBER:
12829 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
12830 return buf;
12831 case VAR_FUNC:
Bram Moolenaar8c711452005-01-14 21:53:12 +000012832 EMSG(_("E999: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012833 break;
12834 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +000012835 EMSG(_("E999: using List as a String"));
12836 break;
12837 case VAR_DICT:
12838 EMSG(_("E999: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012839 break;
12840 case VAR_STRING:
12841 if (varp->vval.v_string != NULL)
12842 return varp->vval.v_string;
12843 break;
12844 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012848 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849}
12850
12851/*
12852 * Find variable "name" in the list of variables.
12853 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012854 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 */
12856 static VAR
12857find_var(name, writing)
12858 char_u *name;
12859 int writing;
12860{
12861 int i;
12862 char_u *varname;
12863 garray_T *gap;
12864
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 if (name[0] == 'a' && name[1] == ':')
12866 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012867 /* Function arguments "a:".
12868 * NOTE: We use a typecast, because function arguments don't have a
12869 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 if (writing)
12871 {
12872 EMSG2(_(e_readonlyvar), name);
12873 return NULL;
12874 }
12875 name += 2;
12876 if (current_funccal == NULL)
12877 return NULL;
12878 if (VIM_ISDIGIT(*name))
12879 {
12880 i = atol((char *)name);
12881 if (i == 0) /* a:0 */
12882 return &current_funccal->a0_var;
12883 i += current_funccal->func->args.ga_len;
12884 if (i > current_funccal->argcount) /* a:999 */
12885 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012886 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 }
12888 if (STRCMP(name, "firstline") == 0)
12889 return &(current_funccal->firstline);
12890 if (STRCMP(name, "lastline") == 0)
12891 return &(current_funccal->lastline);
12892 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
12893 if (STRCMP(name, ((char_u **)
12894 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012895 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 return NULL;
12897 }
12898
12899 gap = find_var_ga(name, &varname);
12900 if (gap == NULL)
12901 return NULL;
12902 return find_var_in_ga(gap, varname);
12903}
12904
12905 static VAR
12906find_var_in_ga(gap, varname)
12907 garray_T *gap;
12908 char_u *varname;
12909{
12910 int i;
12911
12912 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012913 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
12914 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 break;
12916 if (i < 0)
12917 return NULL;
12918 return &VAR_GAP_ENTRY(i, gap);
12919}
12920
12921/*
12922 * Find the growarray and start of name without ':' for a variable name.
12923 */
12924 static garray_T *
12925find_var_ga(name, varname)
12926 char_u *name;
12927 char_u **varname;
12928{
12929 if (name[1] != ':')
12930 {
12931 /* If not "x:name" there must not be any ":" in the name. */
12932 if (vim_strchr(name, ':') != NULL)
12933 return NULL;
12934 *varname = name;
12935 if (current_funccal == NULL)
12936 return &variables; /* global variable */
12937 return &current_funccal->l_vars; /* local function variable */
12938 }
12939 *varname = name + 2;
12940 if (*name == 'b') /* buffer variable */
12941 return &curbuf->b_vars;
12942 if (*name == 'w') /* window variable */
12943 return &curwin->w_vars;
12944 if (*name == 'g') /* global variable */
12945 return &variables;
12946 if (*name == 'l' && current_funccal != NULL)/* local function variable */
12947 return &current_funccal->l_vars;
12948 if (*name == 's' /* script variable */
12949 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
12950 return &SCRIPT_VARS(current_SID);
12951 return NULL;
12952}
12953
12954/*
12955 * Get the string value of a (global/local) variable.
12956 * Returns NULL when it doesn't exist.
12957 */
12958 char_u *
12959get_var_value(name)
12960 char_u *name;
12961{
12962 VAR v;
12963
12964 v = find_var(name, FALSE);
12965 if (v == NULL)
12966 return NULL;
12967 return get_var_string(v);
12968}
12969
12970/*
12971 * Allocate a new growarry for a sourced script. It will be used while
12972 * sourcing this script and when executing functions defined in the script.
12973 */
12974 void
12975new_script_vars(id)
12976 scid_T id;
12977{
12978 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
12979 {
12980 while (ga_scripts.ga_len < id)
12981 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012982 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 }
12985 }
12986}
12987
12988/*
12989 * Initialize internal variables for use.
12990 */
12991 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012992vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 garray_T *gap;
12994{
12995 ga_init2(gap, (int)sizeof(var), 4);
12996}
12997
12998/*
12999 * Clean up a list of internal variables.
13000 */
13001 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013002vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 garray_T *gap;
13004{
13005 int i;
13006
13007 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013008 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 ga_clear(gap);
13010}
13011
13012 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013013clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 VAR v;
13015{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013016 vim_free(v->v_name);
13017 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019}
13020
13021/*
13022 * List the value of one internal variable.
13023 */
13024 static void
13025list_one_var(v, prefix)
13026 VAR v;
13027 char_u *prefix;
13028{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013029 char_u *tofree;
13030 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013031 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013032
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013033 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013034 list_one_var_a(prefix, v->v_name, v->tv.v_type,
13035 s == NULL ? (char_u *)"" : s);
13036 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037}
13038
13039/*
13040 * List the value of one "v:" variable.
13041 */
13042 static void
13043list_vim_var(i)
13044 int i; /* index in vimvars[] */
13045{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013046 char_u *tofree;
13047 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 char_u numbuf[NUMBUFLEN];
13049
Bram Moolenaare9a41262005-01-15 22:18:47 +000013050 s = echo_string(&vimvars[i].tv, &tofree, numbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
Bram Moolenaare9a41262005-01-15 22:18:47 +000013052 vimvars[i].tv.v_type, s == NULL ? (char_u *)"" : s);
13053 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054}
13055
13056 static void
13057list_one_var_a(prefix, name, type, string)
13058 char_u *prefix;
13059 char_u *name;
13060 int type;
13061 char_u *string;
13062{
13063 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13064 if (name != NULL) /* "a:" vars don't have a name stored */
13065 msg_puts(name);
13066 msg_putchar(' ');
13067 msg_advance(22);
13068 if (type == VAR_NUMBER)
13069 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013070 else if (type == VAR_FUNC)
13071 msg_putchar('*');
13072 else if (type == VAR_LIST)
13073 {
13074 msg_putchar('[');
13075 if (*string == '[')
13076 ++string;
13077 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013078 else if (type == VAR_DICT)
13079 {
13080 msg_putchar('{');
13081 if (*string == '{')
13082 ++string;
13083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 else
13085 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013086
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013088
13089 if (type == VAR_FUNC)
13090 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091}
13092
13093/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013094 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095 * If the variable already exists, the value is updated.
13096 * Otherwise the variable is created.
13097 */
13098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013099set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 typeval *tv;
13102 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103{
13104 int i;
13105 VAR v;
13106 char_u *varname;
13107 garray_T *gap;
13108
13109 /*
13110 * Handle setting internal v: variables.
13111 */
13112 i = find_vim_var(name, (int)STRLEN(name));
13113 if (i >= 0)
13114 {
13115 if (vimvars[i].flags & VV_RO)
13116 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000013117 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
13118 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 else
13120 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013121 if (vimvars[i].tv.v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013123 vim_free(vimvars[i].vv_str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013124 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013125 vimvars[i].vv_str = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013126 else
13127 {
13128 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013129 vimvars[i].vv_str = tv->vval.v_string;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 }
13133 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013134 vimvars[i].vv_nr = get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 }
13136 return;
13137 }
13138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013140 {
13141 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13142 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13143 ? name[2] : name[0]))
13144 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013145 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013146 return;
13147 }
13148 if (function_exists(name))
13149 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013150 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013151 return;
13152 }
13153 }
13154
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 v = find_var(name, TRUE);
13156 if (v != NULL) /* existing variable, only need to free string */
13157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013159 && !((v->tv.v_type == VAR_STRING
13160 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 && (tv->v_type == VAR_STRING
13162 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013163 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013164 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013165 return;
13166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 }
13169 else /* add a new variable */
13170 {
13171 gap = find_var_ga(name, &varname);
13172 if (gap == NULL) /* illegal name */
13173 {
13174 EMSG2(_("E461: Illegal variable name: %s"), name);
13175 return;
13176 }
13177
13178 /* Try to use an empty entry */
13179 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013180 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 break;
13182 if (i < 0) /* need to allocate more room */
13183 {
13184 if (ga_grow(gap, 1) == FAIL)
13185 return;
13186 i = gap->ga_len;
13187 }
13188 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 return;
13191 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 if (copy || tv->v_type == VAR_NUMBER)
13195 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013196 else
13197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 v->tv = *tv;
13199 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201}
13202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203/*
13204 * Copy the values from typeval "from" to typeval "to".
13205 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013206 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 typeval *from;
13211 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 to->v_type = from->v_type;
13214 switch (from->v_type)
13215 {
13216 case VAR_NUMBER:
13217 to->vval.v_number = from->vval.v_number;
13218 break;
13219 case VAR_STRING:
13220 case VAR_FUNC:
13221 if (from->vval.v_string == NULL)
13222 to->vval.v_string = NULL;
13223 else
13224 to->vval.v_string = vim_strsave(from->vval.v_string);
13225 break;
13226 case VAR_LIST:
13227 if (from->vval.v_list == NULL)
13228 to->vval.v_list = NULL;
13229 else
13230 {
13231 to->vval.v_list = from->vval.v_list;
13232 ++to->vval.v_list->lv_refcount;
13233 }
13234 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013235 case VAR_DICT:
13236 if (from->vval.v_dict == NULL)
13237 to->vval.v_dict = NULL;
13238 else
13239 {
13240 to->vval.v_dict = from->vval.v_dict;
13241 ++to->vval.v_dict->dv_refcount;
13242 }
13243 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013246 break;
13247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248}
13249
13250/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013251 * Make a copy of an item.
13252 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
13253 */
13254 static void
13255item_copy(from, to, deep)
13256 typeval *from;
13257 typeval *to;
13258 int deep;
13259{
13260 static int recurse = 0;
13261
13262 if (recurse >= VAR_MAXNEST)
13263 {
13264 EMSG(_("E698: variable nested too deep for making a copy"));
13265 return;
13266 }
13267 ++recurse;
13268
13269 switch (from->v_type)
13270 {
13271 case VAR_NUMBER:
13272 case VAR_STRING:
13273 case VAR_FUNC:
13274 copy_tv(from, to);
13275 break;
13276 case VAR_LIST:
13277 to->v_type = VAR_LIST;
13278 to->vval.v_list = list_copy(from->vval.v_list, deep);
13279 break;
13280 case VAR_DICT:
13281 to->v_type = VAR_DICT;
13282 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
13283 break;
13284 default:
13285 EMSG2(_(e_intern2), "item_copy()");
13286 }
13287 --recurse;
13288}
13289
13290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 * ":echo expr1 ..." print each argument separated with a space, add a
13292 * newline at the end.
13293 * ":echon expr1 ..." print each argument plain.
13294 */
13295 void
13296ex_echo(eap)
13297 exarg_T *eap;
13298{
13299 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013301 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 char_u *p;
13303 int needclr = TRUE;
13304 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013305 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306
13307 if (eap->skip)
13308 ++emsg_skip;
13309 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
13310 {
13311 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 {
13314 /*
13315 * Report the invalid expression unless the expression evaluation
13316 * has been cancelled due to an aborting error, an interrupt, or an
13317 * exception.
13318 */
13319 if (!aborting())
13320 EMSG2(_(e_invexpr2), p);
13321 break;
13322 }
13323 if (!eap->skip)
13324 {
13325 if (atstart)
13326 {
13327 atstart = FALSE;
13328 /* Call msg_start() after eval1(), evaluating the expression
13329 * may cause a message to appear. */
13330 if (eap->cmdidx == CMD_echo)
13331 msg_start();
13332 }
13333 else if (eap->cmdidx == CMD_echo)
13334 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013335 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013336 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 if (*p == '\n' || *p == '\r' || *p == TAB)
13338 {
13339 if (*p != TAB && needclr)
13340 {
13341 /* remove any text still there from the command */
13342 msg_clr_eos();
13343 needclr = FALSE;
13344 }
13345 msg_putchar_attr(*p, echo_attr);
13346 }
13347 else
13348 {
13349#ifdef FEAT_MBYTE
13350 if (has_mbyte)
13351 {
13352 int i = (*mb_ptr2len_check)(p);
13353
13354 (void)msg_outtrans_len_attr(p, i, echo_attr);
13355 p += i - 1;
13356 }
13357 else
13358#endif
13359 (void)msg_outtrans_len_attr(p, 1, echo_attr);
13360 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 arg = skipwhite(arg);
13365 }
13366 eap->nextcmd = check_nextcmd(arg);
13367
13368 if (eap->skip)
13369 --emsg_skip;
13370 else
13371 {
13372 /* remove text that may still be there from the command */
13373 if (needclr)
13374 msg_clr_eos();
13375 if (eap->cmdidx == CMD_echo)
13376 msg_end();
13377 }
13378}
13379
13380/*
13381 * ":echohl {name}".
13382 */
13383 void
13384ex_echohl(eap)
13385 exarg_T *eap;
13386{
13387 int id;
13388
13389 id = syn_name2id(eap->arg);
13390 if (id == 0)
13391 echo_attr = 0;
13392 else
13393 echo_attr = syn_id2attr(id);
13394}
13395
13396/*
13397 * ":execute expr1 ..." execute the result of an expression.
13398 * ":echomsg expr1 ..." Print a message
13399 * ":echoerr expr1 ..." Print an error
13400 * Each gets spaces around each argument and a newline at the end for
13401 * echo commands
13402 */
13403 void
13404ex_execute(eap)
13405 exarg_T *eap;
13406{
13407 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 int ret = OK;
13410 char_u *p;
13411 garray_T ga;
13412 int len;
13413 int save_did_emsg;
13414
13415 ga_init2(&ga, 1, 80);
13416
13417 if (eap->skip)
13418 ++emsg_skip;
13419 while (*arg != NUL && *arg != '|' && *arg != '\n')
13420 {
13421 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 {
13424 /*
13425 * Report the invalid expression unless the expression evaluation
13426 * has been cancelled due to an aborting error, an interrupt, or an
13427 * exception.
13428 */
13429 if (!aborting())
13430 EMSG2(_(e_invexpr2), p);
13431 ret = FAIL;
13432 break;
13433 }
13434
13435 if (!eap->skip)
13436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013437 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 len = (int)STRLEN(p);
13439 if (ga_grow(&ga, len + 2) == FAIL)
13440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 ret = FAIL;
13443 break;
13444 }
13445 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 ga.ga_len += len;
13449 }
13450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013451 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 arg = skipwhite(arg);
13453 }
13454
13455 if (ret != FAIL && ga.ga_data != NULL)
13456 {
13457 if (eap->cmdidx == CMD_echomsg)
13458 MSG_ATTR(ga.ga_data, echo_attr);
13459 else if (eap->cmdidx == CMD_echoerr)
13460 {
13461 /* We don't want to abort following commands, restore did_emsg. */
13462 save_did_emsg = did_emsg;
13463 EMSG((char_u *)ga.ga_data);
13464 if (!force_abort)
13465 did_emsg = save_did_emsg;
13466 }
13467 else if (eap->cmdidx == CMD_execute)
13468 do_cmdline((char_u *)ga.ga_data,
13469 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
13470 }
13471
13472 ga_clear(&ga);
13473
13474 if (eap->skip)
13475 --emsg_skip;
13476
13477 eap->nextcmd = check_nextcmd(arg);
13478}
13479
13480/*
13481 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
13482 * "arg" points to the "&" or '+' when called, to "option" when returning.
13483 * Returns NULL when no option name found. Otherwise pointer to the char
13484 * after the option name.
13485 */
13486 static char_u *
13487find_option_end(arg, opt_flags)
13488 char_u **arg;
13489 int *opt_flags;
13490{
13491 char_u *p = *arg;
13492
13493 ++p;
13494 if (*p == 'g' && p[1] == ':')
13495 {
13496 *opt_flags = OPT_GLOBAL;
13497 p += 2;
13498 }
13499 else if (*p == 'l' && p[1] == ':')
13500 {
13501 *opt_flags = OPT_LOCAL;
13502 p += 2;
13503 }
13504 else
13505 *opt_flags = 0;
13506
13507 if (!ASCII_ISALPHA(*p))
13508 return NULL;
13509 *arg = p;
13510
13511 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
13512 p += 4; /* termcap option */
13513 else
13514 while (ASCII_ISALPHA(*p))
13515 ++p;
13516 return p;
13517}
13518
13519/*
13520 * ":function"
13521 */
13522 void
13523ex_function(eap)
13524 exarg_T *eap;
13525{
13526 char_u *theline;
13527 int j;
13528 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 char_u *name = NULL;
13531 char_u *p;
13532 char_u *arg;
13533 garray_T newargs;
13534 garray_T newlines;
13535 int varargs = FALSE;
13536 int mustend = FALSE;
13537 int flags = 0;
13538 ufunc_T *fp;
13539 int indent;
13540 int nesting;
13541 char_u *skip_until = NULL;
13542 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013543 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
13545 /*
13546 * ":function" without argument: list functions.
13547 */
13548 if (ends_excmd(*eap->arg))
13549 {
13550 if (!eap->skip)
13551 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
13552 list_func_head(fp, FALSE);
13553 eap->nextcmd = check_nextcmd(eap->arg);
13554 return;
13555 }
13556
13557 p = eap->arg;
13558 name = trans_function_name(&p, eap->skip, FALSE);
13559 if (name == NULL && !eap->skip)
13560 {
13561 /*
13562 * Return on an invalid expression in braces, unless the expression
13563 * evaluation has been cancelled due to an aborting error, an
13564 * interrupt, or an exception.
13565 */
13566 if (!aborting())
13567 return;
13568 else
13569 eap->skip = TRUE;
13570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 /* An error in a function call during evaluation of an expression in magic
13572 * braces should not cause the function not to be defined. */
13573 saved_did_emsg = did_emsg;
13574 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575
13576 /*
13577 * ":function func" with only function name: list function.
13578 */
13579 if (vim_strchr(p, '(') == NULL)
13580 {
13581 if (!ends_excmd(*skipwhite(p)))
13582 {
13583 EMSG(_(e_trailing));
13584 goto erret_name;
13585 }
13586 eap->nextcmd = check_nextcmd(p);
13587 if (eap->nextcmd != NULL)
13588 *p = NUL;
13589 if (!eap->skip && !got_int)
13590 {
13591 fp = find_func(name);
13592 if (fp != NULL)
13593 {
13594 list_func_head(fp, TRUE);
13595 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
13596 {
13597 msg_putchar('\n');
13598 msg_outnum((long)(j + 1));
13599 if (j < 9)
13600 msg_putchar(' ');
13601 if (j < 99)
13602 msg_putchar(' ');
13603 msg_prt_line(FUNCLINE(fp, j));
13604 out_flush(); /* show a line at a time */
13605 ui_breakcheck();
13606 }
13607 if (!got_int)
13608 {
13609 msg_putchar('\n');
13610 msg_puts((char_u *)" endfunction");
13611 }
13612 }
13613 else
13614 EMSG2(_("E123: Undefined function: %s"), eap->arg);
13615 }
13616 goto erret_name;
13617 }
13618
13619 /*
13620 * ":function name(arg1, arg2)" Define function.
13621 */
13622 p = skipwhite(p);
13623 if (*p != '(')
13624 {
13625 if (!eap->skip)
13626 {
13627 EMSG2(_("E124: Missing '(': %s"), eap->arg);
13628 goto erret_name;
13629 }
13630 /* attempt to continue by skipping some text */
13631 if (vim_strchr(p, '(') != NULL)
13632 p = vim_strchr(p, '(');
13633 }
13634 p = skipwhite(p + 1);
13635
13636 ga_init2(&newargs, (int)sizeof(char_u *), 3);
13637 ga_init2(&newlines, (int)sizeof(char_u *), 3);
13638
13639 /*
13640 * Isolate the arguments: "arg1, arg2, ...)"
13641 */
13642 while (*p != ')')
13643 {
13644 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
13645 {
13646 varargs = TRUE;
13647 p += 3;
13648 mustend = TRUE;
13649 }
13650 else
13651 {
13652 arg = p;
13653 while (ASCII_ISALNUM(*p) || *p == '_')
13654 ++p;
13655 if (arg == p || isdigit(*arg)
13656 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
13657 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
13658 {
13659 if (!eap->skip)
13660 EMSG2(_("E125: Illegal argument: %s"), arg);
13661 break;
13662 }
13663 if (ga_grow(&newargs, 1) == FAIL)
13664 goto erret;
13665 c = *p;
13666 *p = NUL;
13667 arg = vim_strsave(arg);
13668 if (arg == NULL)
13669 goto erret;
13670 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
13671 *p = c;
13672 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 if (*p == ',')
13674 ++p;
13675 else
13676 mustend = TRUE;
13677 }
13678 p = skipwhite(p);
13679 if (mustend && *p != ')')
13680 {
13681 if (!eap->skip)
13682 EMSG2(_(e_invarg2), eap->arg);
13683 break;
13684 }
13685 }
13686 ++p; /* skip the ')' */
13687
Bram Moolenaare9a41262005-01-15 22:18:47 +000013688 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 for (;;)
13690 {
13691 p = skipwhite(p);
13692 if (STRNCMP(p, "range", 5) == 0)
13693 {
13694 flags |= FC_RANGE;
13695 p += 5;
13696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013697 else if (STRNCMP(p, "dict", 4) == 0)
13698 {
13699 flags |= FC_DICT;
13700 p += 4;
13701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 else if (STRNCMP(p, "abort", 5) == 0)
13703 {
13704 flags |= FC_ABORT;
13705 p += 5;
13706 }
13707 else
13708 break;
13709 }
13710
13711 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
13712 EMSG(_(e_trailing));
13713
13714 /*
13715 * Read the body of the function, until ":endfunction" is found.
13716 */
13717 if (KeyTyped)
13718 {
13719 /* Check if the function already exists, don't let the user type the
13720 * whole function before telling him it doesn't work! For a script we
13721 * need to skip the body to be able to find what follows. */
13722 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
13723 EMSG2(_(e_funcexts), name);
13724
13725 msg_putchar('\n'); /* don't overwrite the function name */
13726 cmdline_row = msg_row;
13727 }
13728
13729 indent = 2;
13730 nesting = 0;
13731 for (;;)
13732 {
13733 msg_scroll = TRUE;
13734 need_wait_return = FALSE;
13735 if (eap->getline == NULL)
13736 theline = getcmdline(':', 0L, indent);
13737 else
13738 theline = eap->getline(':', eap->cookie, indent);
13739 if (KeyTyped)
13740 lines_left = Rows - 1;
13741 if (theline == NULL)
13742 {
13743 EMSG(_("E126: Missing :endfunction"));
13744 goto erret;
13745 }
13746
13747 if (skip_until != NULL)
13748 {
13749 /* between ":append" and "." and between ":python <<EOF" and "EOF"
13750 * don't check for ":endfunc". */
13751 if (STRCMP(theline, skip_until) == 0)
13752 {
13753 vim_free(skip_until);
13754 skip_until = NULL;
13755 }
13756 }
13757 else
13758 {
13759 /* skip ':' and blanks*/
13760 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
13761 ;
13762
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013763 /* Check for "endfunction". */
13764 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 {
13766 vim_free(theline);
13767 break;
13768 }
13769
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013770 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 * at "end". */
13772 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
13773 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013774 else if (STRNCMP(p, "if", 2) == 0
13775 || STRNCMP(p, "wh", 2) == 0
13776 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 || STRNCMP(p, "try", 3) == 0)
13778 indent += 2;
13779
13780 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013781 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013783 if (*p == '!')
13784 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 p += eval_fname_script(p);
13786 if (ASCII_ISALPHA(*p))
13787 {
13788 vim_free(trans_function_name(&p, TRUE, FALSE));
13789 if (*skipwhite(p) == '(')
13790 {
13791 ++nesting;
13792 indent += 2;
13793 }
13794 }
13795 }
13796
13797 /* Check for ":append" or ":insert". */
13798 p = skip_range(p, NULL);
13799 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
13800 || (p[0] == 'i'
13801 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
13802 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
13803 skip_until = vim_strsave((char_u *)".");
13804
13805 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
13806 arg = skipwhite(skiptowhite(p));
13807 if (arg[0] == '<' && arg[1] =='<'
13808 && ((p[0] == 'p' && p[1] == 'y'
13809 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
13810 || (p[0] == 'p' && p[1] == 'e'
13811 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
13812 || (p[0] == 't' && p[1] == 'c'
13813 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
13814 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
13815 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013816 || (p[0] == 'm' && p[1] == 'z'
13817 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 ))
13819 {
13820 /* ":python <<" continues until a dot, like ":append" */
13821 p = skipwhite(arg + 2);
13822 if (*p == NUL)
13823 skip_until = vim_strsave((char_u *)".");
13824 else
13825 skip_until = vim_strsave(p);
13826 }
13827 }
13828
13829 /* Add the line to the function. */
13830 if (ga_grow(&newlines, 1) == FAIL)
13831 goto erret;
13832 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
13833 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 }
13835
13836 /* Don't define the function when skipping commands or when an error was
13837 * detected. */
13838 if (eap->skip || did_emsg)
13839 goto erret;
13840
13841 /*
13842 * If there are no errors, add the function
13843 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013844 v = find_var(name, FALSE);
13845 if (v != NULL && v->tv.v_type == VAR_FUNC)
13846 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013847 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013848 goto erret;
13849 }
13850
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 fp = find_func(name);
13852 if (fp != NULL)
13853 {
13854 if (!eap->forceit)
13855 {
13856 EMSG2(_(e_funcexts), name);
13857 goto erret;
13858 }
13859 if (fp->calls)
13860 {
13861 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
13862 goto erret;
13863 }
13864 /* redefine existing function */
13865 ga_clear_strings(&(fp->args));
13866 ga_clear_strings(&(fp->lines));
13867 vim_free(name);
13868 }
13869 else
13870 {
13871 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
13872 if (fp == NULL)
13873 goto erret;
13874 /* insert the new function in the function list */
13875 fp->next = firstfunc;
13876 firstfunc = fp;
13877 fp->name = name;
13878 }
13879 fp->args = newargs;
13880 fp->lines = newlines;
13881 fp->varargs = varargs;
13882 fp->flags = flags;
13883 fp->calls = 0;
13884 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 vim_free(skip_until);
13887 return;
13888
13889erret:
13890 vim_free(skip_until);
13891 ga_clear_strings(&newargs);
13892 ga_clear_strings(&newlines);
13893erret_name:
13894 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896}
13897
13898/*
13899 * Get a function name, translating "<SID>" and "<SNR>".
13900 * Returns the function name in allocated memory, or NULL for failure.
13901 * Advances "pp" to just after the function name (if no error).
13902 */
13903 static char_u *
13904trans_function_name(pp, skip, internal)
13905 char_u **pp;
13906 int skip; /* only find the end, don't evaluate */
13907 int internal; /* TRUE if internal function name OK */
13908{
13909 char_u *name;
13910 char_u *start;
13911 char_u *end;
13912 int lead;
13913 char_u sid_buf[20];
13914 char_u *temp_string = NULL;
13915 char_u *expr_start, *expr_end;
13916 int len;
13917
13918 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
13919 start = *pp;
13920 lead = eval_fname_script(start);
13921 if (lead > 0)
13922 start += lead;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 if (end == start)
13925 {
13926 if (!skip)
13927 EMSG(_("E129: Function name required"));
13928 return NULL;
13929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 if (expr_start != NULL && !skip)
13931 {
13932 /* expand magic curlies */
13933 temp_string = make_expanded_name(start, expr_start, expr_end, end);
13934 if (temp_string == NULL)
13935 {
13936 /*
13937 * Report an invalid expression in braces, unless the expression
13938 * evaluation has been cancelled due to an aborting error, an
13939 * interrupt, or an exception.
13940 */
13941 if (!aborting())
13942 EMSG2(_(e_invarg2), start);
13943 else
13944 *pp = end;
13945 return NULL;
13946 }
13947 start = temp_string;
13948 len = (int)STRLEN(temp_string);
13949 }
13950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 len = (int)(end - start);
13952
13953 /*
13954 * Copy the function name to allocated memory.
13955 * Accept <SID>name() inside a script, translate into <SNR>123_name().
13956 * Accept <SNR>123_name() outside a script.
13957 */
13958 if (skip)
13959 lead = 0; /* do nothing */
13960 else if (lead > 0)
13961 {
13962 lead = 3;
13963 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
13964 {
13965 if (current_SID <= 0)
13966 {
13967 EMSG(_(e_usingsid));
13968 return NULL;
13969 }
13970 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
13971 lead += (int)STRLEN(sid_buf);
13972 }
13973 }
13974 else if (!internal && !ASCII_ISUPPER(*start))
13975 {
13976 EMSG2(_("E128: Function name must start with a capital: %s"), start);
13977 return NULL;
13978 }
13979 name = alloc((unsigned)(len + lead + 1));
13980 if (name != NULL)
13981 {
13982 if (lead > 0)
13983 {
13984 name[0] = K_SPECIAL;
13985 name[1] = KS_EXTRA;
13986 name[2] = (int)KE_SNR;
13987 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
13988 STRCPY(name + 3, sid_buf);
13989 }
13990 mch_memmove(name + lead, start, (size_t)len);
13991 name[len + lead] = NUL;
13992 }
13993 *pp = end;
13994
13995 vim_free(temp_string);
13996 return name;
13997}
13998
13999/*
14000 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14001 * Return 2 if "p" starts with "s:".
14002 * Return 0 otherwise.
14003 */
14004 static int
14005eval_fname_script(p)
14006 char_u *p;
14007{
14008 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
14009 || STRNICMP(p + 1, "SNR>", 4) == 0))
14010 return 5;
14011 if (p[0] == 's' && p[1] == ':')
14012 return 2;
14013 return 0;
14014}
14015
14016/*
14017 * Return TRUE if "p" starts with "<SID>" or "s:".
14018 * Only works if eval_fname_script() returned non-zero for "p"!
14019 */
14020 static int
14021eval_fname_sid(p)
14022 char_u *p;
14023{
14024 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
14025}
14026
14027/*
14028 * List the head of the function: "name(arg1, arg2)".
14029 */
14030 static void
14031list_func_head(fp, indent)
14032 ufunc_T *fp;
14033 int indent;
14034{
14035 int j;
14036
14037 msg_start();
14038 if (indent)
14039 MSG_PUTS(" ");
14040 MSG_PUTS("function ");
14041 if (fp->name[0] == K_SPECIAL)
14042 {
14043 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
14044 msg_puts(fp->name + 3);
14045 }
14046 else
14047 msg_puts(fp->name);
14048 msg_putchar('(');
14049 for (j = 0; j < fp->args.ga_len; ++j)
14050 {
14051 if (j)
14052 MSG_PUTS(", ");
14053 msg_puts(FUNCARG(fp, j));
14054 }
14055 if (fp->varargs)
14056 {
14057 if (j)
14058 MSG_PUTS(", ");
14059 MSG_PUTS("...");
14060 }
14061 msg_putchar(')');
14062}
14063
14064/*
14065 * Find a function by name, return pointer to it in ufuncs.
14066 * Return NULL for unknown function.
14067 */
14068 static ufunc_T *
14069find_func(name)
14070 char_u *name;
14071{
14072 ufunc_T *fp;
14073
14074 for (fp = firstfunc; fp != NULL; fp = fp->next)
14075 if (STRCMP(name, fp->name) == 0)
14076 break;
14077 return fp;
14078}
14079
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080/*
14081 * Return TRUE if a function "name" exists.
14082 */
14083 static int
14084function_exists(name)
14085 char_u *name;
14086{
14087 char_u *p = name;
14088 int n = FALSE;
14089
14090 p = trans_function_name(&p, FALSE, TRUE);
14091 if (p != NULL)
14092 {
14093 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14094 n = (find_func(p) != NULL);
14095 else if (ASCII_ISLOWER(*p))
14096 n = (find_internal_func(p) >= 0);
14097 vim_free(p);
14098 }
14099 return n;
14100}
14101
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
14103
14104/*
14105 * Function given to ExpandGeneric() to obtain the list of user defined
14106 * function names.
14107 */
14108 char_u *
14109get_user_func_name(xp, idx)
14110 expand_T *xp;
14111 int idx;
14112{
14113 static ufunc_T *fp = NULL;
14114
14115 if (idx == 0)
14116 fp = firstfunc;
14117 if (fp != NULL)
14118 {
14119 if (STRLEN(fp->name) + 4 >= IOSIZE)
14120 return fp->name; /* prevents overflow */
14121
14122 cat_func_name(IObuff, fp);
14123 if (xp->xp_context != EXPAND_USER_FUNC)
14124 {
14125 STRCAT(IObuff, "(");
14126 if (!fp->varargs && fp->args.ga_len == 0)
14127 STRCAT(IObuff, ")");
14128 }
14129
14130 fp = fp->next;
14131 return IObuff;
14132 }
14133 return NULL;
14134}
14135
14136#endif /* FEAT_CMDL_COMPL */
14137
14138/*
14139 * Copy the function name of "fp" to buffer "buf".
14140 * "buf" must be able to hold the function name plus three bytes.
14141 * Takes care of script-local function names.
14142 */
14143 static void
14144cat_func_name(buf, fp)
14145 char_u *buf;
14146 ufunc_T *fp;
14147{
14148 if (fp->name[0] == K_SPECIAL)
14149 {
14150 STRCPY(buf, "<SNR>");
14151 STRCAT(buf, fp->name + 3);
14152 }
14153 else
14154 STRCPY(buf, fp->name);
14155}
14156
14157/*
14158 * ":delfunction {name}"
14159 */
14160 void
14161ex_delfunction(eap)
14162 exarg_T *eap;
14163{
14164 ufunc_T *fp = NULL, *pfp;
14165 char_u *p;
14166 char_u *name;
14167
14168 p = eap->arg;
14169 name = trans_function_name(&p, eap->skip, FALSE);
14170 if (name == NULL)
14171 return;
14172 if (!ends_excmd(*skipwhite(p)))
14173 {
14174 vim_free(name);
14175 EMSG(_(e_trailing));
14176 return;
14177 }
14178 eap->nextcmd = check_nextcmd(p);
14179 if (eap->nextcmd != NULL)
14180 *p = NUL;
14181
14182 if (!eap->skip)
14183 fp = find_func(name);
14184 vim_free(name);
14185
14186 if (!eap->skip)
14187 {
14188 if (fp == NULL)
14189 {
14190 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14191 return;
14192 }
14193 if (fp->calls)
14194 {
14195 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14196 return;
14197 }
14198
14199 /* clear this function */
14200 vim_free(fp->name);
14201 ga_clear_strings(&(fp->args));
14202 ga_clear_strings(&(fp->lines));
14203
14204 /* remove the function from the function list */
14205 if (firstfunc == fp)
14206 firstfunc = fp->next;
14207 else
14208 {
14209 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
14210 if (pfp->next == fp)
14211 {
14212 pfp->next = fp->next;
14213 break;
14214 }
14215 }
14216 vim_free(fp);
14217 }
14218}
14219
14220/*
14221 * Call a user function.
14222 */
14223 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000014224call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 ufunc_T *fp; /* pointer to function */
14226 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014227 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 linenr_T firstline; /* first line of range */
14230 linenr_T lastline; /* last line of range */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014231 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232{
14233 char_u *save_sourcing_name;
14234 linenr_T save_sourcing_lnum;
14235 scid_T save_current_SID;
14236 struct funccall fc;
14237 struct funccall *save_fcp = current_funccal;
14238 int save_did_emsg;
14239 static int depth = 0;
14240
14241 /* If depth of calling is getting too high, don't execute the function */
14242 if (depth >= p_mfd)
14243 {
14244 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245 rettv->v_type = VAR_NUMBER;
14246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 return;
14248 }
14249 ++depth;
14250
14251 line_breakcheck(); /* check for CTRL-C hit */
14252
14253 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014254 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 fc.func = fp;
14256 fc.argcount = argcount;
14257 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 fc.rettv = rettv;
14259 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 fc.linenr = 0;
14261 fc.returned = FALSE;
14262 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014263 fc.a0_var.tv.v_type = VAR_NUMBER;
14264 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
14265 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014267 fc.firstline.tv.v_type = VAR_NUMBER;
14268 fc.firstline.tv.vval.v_number = firstline;
14269 fc.firstline.v_name = NULL;
14270 fc.lastline.tv.v_type = VAR_NUMBER;
14271 fc.lastline.tv.vval.v_number = lastline;
14272 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 /* Check if this function has a breakpoint. */
14274 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
14275 fc.dbg_tick = debug_tick;
14276
Bram Moolenaare9a41262005-01-15 22:18:47 +000014277 if (selfdict != NULL && ga_grow(&fc.l_vars, 1) != FAIL)
14278 {
14279 VAR v = &VAR_GAP_ENTRY(0, &fc.l_vars);
14280
14281 /* Set the "self" local variable. */
14282 if ((v->v_name = vim_strsave((char_u *)"self")) != NULL)
14283 {
14284 ++fc.l_vars.ga_len;
14285 v->tv.v_type = VAR_DICT;
14286 v->tv.vval.v_dict = selfdict;
14287 ++selfdict->dv_refcount;
14288 }
14289 }
14290
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 /* Don't redraw while executing the function. */
14292 ++RedrawingDisabled;
14293 save_sourcing_name = sourcing_name;
14294 save_sourcing_lnum = sourcing_lnum;
14295 sourcing_lnum = 1;
14296 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
14297 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
14298 if (sourcing_name != NULL)
14299 {
14300 if (save_sourcing_name != NULL
14301 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
14302 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
14303 else
14304 STRCPY(sourcing_name, "function ");
14305 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
14306
14307 if (p_verbose >= 12)
14308 {
14309 ++no_wait_return;
14310 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14311 msg_str((char_u *)_("calling %s"), sourcing_name);
14312 if (p_verbose >= 14)
14313 {
14314 int i;
14315 char_u buf[MSG_BUF_LEN];
14316
14317 msg_puts((char_u *)"(");
14318 for (i = 0; i < argcount; ++i)
14319 {
14320 if (i > 0)
14321 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014322 if (argvars[i].v_type == VAR_NUMBER)
14323 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 else
14325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014326 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 buf, MSG_BUF_LEN);
14328 msg_puts((char_u *)"\"");
14329 msg_puts(buf);
14330 msg_puts((char_u *)"\"");
14331 }
14332 }
14333 msg_puts((char_u *)")");
14334 }
14335 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14336 cmdline_row = msg_row;
14337 --no_wait_return;
14338 }
14339 }
14340 save_current_SID = current_SID;
14341 current_SID = fp->script_ID;
14342 save_did_emsg = did_emsg;
14343 did_emsg = FALSE;
14344
14345 /* call do_cmdline() to execute the lines */
14346 do_cmdline(NULL, get_func_line, (void *)&fc,
14347 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
14348
14349 --RedrawingDisabled;
14350
14351 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 clear_tv(rettv);
14355 rettv->v_type = VAR_NUMBER;
14356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 }
14358
14359 /* when being verbose, mention the return value */
14360 if (p_verbose >= 12)
14361 {
14362 char_u *sn, *val;
14363
14364 ++no_wait_return;
14365 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14366
14367 /* Make sure the output fits in IObuff. */
14368 sn = sourcing_name;
14369 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
14370 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
14371
14372 if (aborting())
14373 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 (long)fc.rettv->vval.v_number);
14377 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014379 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 if (STRLEN(val) > IOSIZE / 2 - 50)
14381 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
14382 smsg((char_u *)_("%s returning \"%s\""), sn, val);
14383 }
14384 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14385 cmdline_row = msg_row;
14386 --no_wait_return;
14387 }
14388
14389 vim_free(sourcing_name);
14390 sourcing_name = save_sourcing_name;
14391 sourcing_lnum = save_sourcing_lnum;
14392 current_SID = save_current_SID;
14393
14394 if (p_verbose >= 12 && sourcing_name != NULL)
14395 {
14396 ++no_wait_return;
14397 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14398 msg_str((char_u *)_("continuing in %s"), sourcing_name);
14399 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14400 cmdline_row = msg_row;
14401 --no_wait_return;
14402 }
14403
14404 did_emsg |= save_did_emsg;
14405 current_funccal = save_fcp;
14406
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014407 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 --depth;
14409}
14410
14411/*
14412 * ":return [expr]"
14413 */
14414 void
14415ex_return(eap)
14416 exarg_T *eap;
14417{
14418 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 int returning = FALSE;
14421
14422 if (current_funccal == NULL)
14423 {
14424 EMSG(_("E133: :return not inside a function"));
14425 return;
14426 }
14427
14428 if (eap->skip)
14429 ++emsg_skip;
14430
14431 eap->nextcmd = NULL;
14432 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 {
14435 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 }
14440 /* It's safer to return also on error. */
14441 else if (!eap->skip)
14442 {
14443 /*
14444 * Return unless the expression evaluation has been cancelled due to an
14445 * aborting error, an interrupt, or an exception.
14446 */
14447 if (!aborting())
14448 returning = do_return(eap, FALSE, TRUE, NULL);
14449 }
14450
14451 /* When skipping or the return gets pending, advance to the next command
14452 * in this line (!returning). Otherwise, ignore the rest of the line.
14453 * Following lines will be ignored by get_func_line(). */
14454 if (returning)
14455 eap->nextcmd = NULL;
14456 else if (eap->nextcmd == NULL) /* no argument */
14457 eap->nextcmd = check_nextcmd(arg);
14458
14459 if (eap->skip)
14460 --emsg_skip;
14461}
14462
14463/*
14464 * Return from a function. Possibly makes the return pending. Also called
14465 * for a pending return at the ":endtry" or after returning from an extra
14466 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 * when called due to a ":return" command. "rettv" may point to a typeval
14468 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 * FALSE when the return gets pending.
14470 */
14471 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 exarg_T *eap;
14474 int reanimate;
14475 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477{
14478 int idx;
14479 struct condstack *cstack = eap->cstack;
14480
14481 if (reanimate)
14482 /* Undo the return. */
14483 current_funccal->returned = FALSE;
14484
14485 /*
14486 * Cleanup (and inactivate) conditionals, but stop when a try conditional
14487 * not in its finally clause (which then is to be executed next) is found.
14488 * In this case, make the ":return" pending for execution at the ":endtry".
14489 * Otherwise, return normally.
14490 */
14491 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
14492 if (idx >= 0)
14493 {
14494 cstack->cs_pending[idx] = CSTP_RETURN;
14495
14496 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497 /* A pending return again gets pending. "rettv" points to an
14498 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 else
14502 {
14503 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 {
14510 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
14512 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 else
14514 EMSG(_(e_outofmem));
14515 }
14516 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518
14519 if (reanimate)
14520 {
14521 /* The pending return value could be overwritten by a ":return"
14522 * without argument in a finally clause; reset the default
14523 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 current_funccal->rettv->v_type = VAR_NUMBER;
14525 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 }
14527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 }
14530 else
14531 {
14532 current_funccal->returned = TRUE;
14533
14534 /* If the return is carried out now, store the return value. For
14535 * a return immediately after reanimation, the value is already
14536 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 clear_tv(current_funccal->rettv);
14540 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 }
14544 }
14545
14546 return idx < 0;
14547}
14548
14549/*
14550 * Free the variable with a pending return value.
14551 */
14552 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553discard_pending_return(rettv)
14554 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557}
14558
14559/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 * is an allocated string. Used by report_pending() for verbose messages.
14562 */
14563 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564get_return_cmd(rettv)
14565 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014567 char_u *s;
14568 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014571 if (rettv == NULL)
14572 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014574 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575
14576 STRCPY(IObuff, ":return ");
14577 STRNCPY(IObuff + 8, s, IOSIZE - 8);
14578 if (STRLEN(s) + 8 >= IOSIZE)
14579 STRCPY(IObuff + IOSIZE - 4, "...");
14580 vim_free(tofree);
14581 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582}
14583
14584/*
14585 * Get next function line.
14586 * Called by do_cmdline() to get the next line.
14587 * Returns allocated string, or NULL for end of function.
14588 */
14589/* ARGSUSED */
14590 char_u *
14591get_func_line(c, cookie, indent)
14592 int c; /* not used */
14593 void *cookie;
14594 int indent; /* not used */
14595{
14596 struct funccall *fcp = (struct funccall *)cookie;
14597 char_u *retval;
14598 garray_T *gap; /* growarray with function lines */
14599
14600 /* If breakpoints have been added/deleted need to check for it. */
14601 if (fcp->dbg_tick != debug_tick)
14602 {
14603 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
14604 sourcing_lnum);
14605 fcp->dbg_tick = debug_tick;
14606 }
14607
14608 gap = &fcp->func->lines;
14609 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
14610 retval = NULL;
14611 else if (fcp->returned || fcp->linenr >= gap->ga_len)
14612 retval = NULL;
14613 else
14614 {
14615 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
14616 sourcing_lnum = fcp->linenr;
14617 }
14618
14619 /* Did we encounter a breakpoint? */
14620 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
14621 {
14622 dbg_breakpoint(fcp->func->name, sourcing_lnum);
14623 /* Find next breakpoint. */
14624 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
14625 sourcing_lnum);
14626 fcp->dbg_tick = debug_tick;
14627 }
14628
14629 return retval;
14630}
14631
14632/*
14633 * Return TRUE if the currently active function should be ended, because a
14634 * return was encountered or an error occured. Used inside a ":while".
14635 */
14636 int
14637func_has_ended(cookie)
14638 void *cookie;
14639{
14640 struct funccall *fcp = (struct funccall *)cookie;
14641
14642 /* Ignore the "abort" flag if the abortion behavior has been changed due to
14643 * an error inside a try conditional. */
14644 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
14645 || fcp->returned);
14646}
14647
14648/*
14649 * return TRUE if cookie indicates a function which "abort"s on errors.
14650 */
14651 int
14652func_has_abort(cookie)
14653 void *cookie;
14654{
14655 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
14656}
14657
14658#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
14659typedef enum
14660{
14661 VAR_FLAVOUR_DEFAULT,
14662 VAR_FLAVOUR_SESSION,
14663 VAR_FLAVOUR_VIMINFO
14664} var_flavour_T;
14665
14666static var_flavour_T var_flavour __ARGS((char_u *varname));
14667
14668 static var_flavour_T
14669var_flavour(varname)
14670 char_u *varname;
14671{
14672 char_u *p = varname;
14673
14674 if (ASCII_ISUPPER(*p))
14675 {
14676 while (*(++p))
14677 if (ASCII_ISLOWER(*p))
14678 return VAR_FLAVOUR_SESSION;
14679 return VAR_FLAVOUR_VIMINFO;
14680 }
14681 else
14682 return VAR_FLAVOUR_DEFAULT;
14683}
14684#endif
14685
14686#if defined(FEAT_VIMINFO) || defined(PROTO)
14687/*
14688 * Restore global vars that start with a capital from the viminfo file
14689 */
14690 int
14691read_viminfo_varlist(virp, writing)
14692 vir_T *virp;
14693 int writing;
14694{
14695 char_u *tab;
14696 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014697 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698
14699 if (!writing && (find_viminfo_parameter('!') != NULL))
14700 {
14701 tab = vim_strchr(virp->vir_line + 1, '\t');
14702 if (tab != NULL)
14703 {
14704 *tab++ = '\0'; /* isolate the variable name */
14705 if (*tab == 'S') /* string var */
14706 is_string = TRUE;
14707
14708 tab = vim_strchr(tab, '\t');
14709 if (tab != NULL)
14710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 if (is_string)
14712 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014713 tv.v_type = VAR_STRING;
14714 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 }
14717 else
14718 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014719 tv.v_type = VAR_NUMBER;
14720 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014722 set_var(virp->vir_line + 1, &tv, FALSE);
14723 if (is_string)
14724 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 }
14726 }
14727 }
14728
14729 return viminfo_readline(virp);
14730}
14731
14732/*
14733 * Write global vars that start with a capital to the viminfo file
14734 */
14735 void
14736write_viminfo_varlist(fp)
14737 FILE *fp;
14738{
14739 garray_T *gap = &variables; /* global variable */
14740 VAR this_var;
14741 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 char *s;
14743 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014744 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745
14746 if (find_viminfo_parameter('!') == NULL)
14747 return;
14748
14749 fprintf(fp, _("\n# global variables:\n"));
14750 for (i = gap->ga_len; --i >= 0; )
14751 {
14752 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014753 if (this_var->v_name != NULL
14754 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 switch (this_var->tv.v_type)
14757 {
14758 case VAR_STRING: s = "STR"; break;
14759 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014760 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761 }
14762 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014763 viminfo_writestring(fp, echo_string(&this_var->tv,
14764 &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014765 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 }
14767 }
14768}
14769#endif
14770
14771#if defined(FEAT_SESSION) || defined(PROTO)
14772 int
14773store_session_globals(fd)
14774 FILE *fd;
14775{
14776 garray_T *gap = &variables; /* global variable */
14777 VAR this_var;
14778 int i;
14779 char_u *p, *t;
14780
14781 for (i = gap->ga_len; --i >= 0; )
14782 {
14783 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014784 if (this_var->v_name != NULL
14785 && (this_var->tv.v_type == VAR_NUMBER
14786 || this_var->tv.v_type == VAR_STRING)
14787 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014789 /* Escape special characters with a backslash. Turn a LF and
14790 * CR into \n and \r. */
14791 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014793 if (p == NULL) /* out of memory */
14794 continue;
14795 for (t = p; *t != NUL; ++t)
14796 if (*t == '\n')
14797 *t = 'n';
14798 else if (*t == '\r')
14799 *t = 'r';
14800 if ((fprintf(fd, "let %s = %c%s%c",
14801 this_var->v_name,
14802 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
14803 p,
14804 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
14805 || put_eol(fd) == FAIL)
14806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014808 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014810 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 }
14812 }
14813 return OK;
14814}
14815#endif
14816
14817#endif /* FEAT_EVAL */
14818
14819#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
14820
14821
14822#ifdef WIN3264
14823/*
14824 * Functions for ":8" filename modifier: get 8.3 version of a filename.
14825 */
14826static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
14827static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
14828static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
14829
14830/*
14831 * Get the short pathname of a file.
14832 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
14833 */
14834 static int
14835get_short_pathname(fnamep, bufp, fnamelen)
14836 char_u **fnamep;
14837 char_u **bufp;
14838 int *fnamelen;
14839{
14840 int l,len;
14841 char_u *newbuf;
14842
14843 len = *fnamelen;
14844
14845 l = GetShortPathName(*fnamep, *fnamep, len);
14846 if (l > len - 1)
14847 {
14848 /* If that doesn't work (not enough space), then save the string
14849 * and try again with a new buffer big enough
14850 */
14851 newbuf = vim_strnsave(*fnamep, l);
14852 if (newbuf == NULL)
14853 return 0;
14854
14855 vim_free(*bufp);
14856 *fnamep = *bufp = newbuf;
14857
14858 l = GetShortPathName(*fnamep,*fnamep,l+1);
14859
14860 /* Really should always succeed, as the buffer is big enough */
14861 }
14862
14863 *fnamelen = l;
14864 return 1;
14865}
14866
14867/*
14868 * Create a short path name. Returns the length of the buffer it needs.
14869 * Doesn't copy over the end of the buffer passed in.
14870 */
14871 static int
14872shortpath_for_invalid_fname(fname, bufp, fnamelen)
14873 char_u **fname;
14874 char_u **bufp;
14875 int *fnamelen;
14876{
14877 char_u *s, *p, *pbuf2, *pbuf3;
14878 char_u ch;
14879 int l,len,len2,plen,slen;
14880
14881 /* Make a copy */
14882 len2 = *fnamelen;
14883 pbuf2 = vim_strnsave(*fname, len2);
14884 pbuf3 = NULL;
14885
14886 s = pbuf2 + len2 - 1; /* Find the end */
14887 slen = 1;
14888 plen = len2;
14889
14890 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014891 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 {
14893 --s;
14894 ++slen;
14895 --plen;
14896 }
14897
14898 do
14899 {
14900 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014901 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 {
14903 --s;
14904 ++slen;
14905 --plen;
14906 }
14907 if (s <= pbuf2)
14908 break;
14909
14910 /* Remeber the character that is about to be blatted */
14911 ch = *s;
14912 *s = 0; /* get_short_pathname requires a null-terminated string */
14913
14914 /* Try it in situ */
14915 p = pbuf2;
14916 if (!get_short_pathname(&p, &pbuf3, &plen))
14917 {
14918 vim_free(pbuf2);
14919 return -1;
14920 }
14921 *s = ch; /* Preserve the string */
14922 } while (plen == 0);
14923
14924 if (plen > 0)
14925 {
14926 /* Remeber the length of the new string. */
14927 *fnamelen = len = plen + slen;
14928 vim_free(*bufp);
14929 if (len > len2)
14930 {
14931 /* If there's not enough space in the currently allocated string,
14932 * then copy it to a buffer big enough.
14933 */
14934 *fname= *bufp = vim_strnsave(p, len);
14935 if (*fname == NULL)
14936 return -1;
14937 }
14938 else
14939 {
14940 /* Transfer pbuf2 to being the main buffer (it's big enough) */
14941 *fname = *bufp = pbuf2;
14942 if (p != pbuf2)
14943 strncpy(*fname, p, plen);
14944 pbuf2 = NULL;
14945 }
14946 /* Concat the next bit */
14947 strncpy(*fname + plen, s, slen);
14948 (*fname)[len] = '\0';
14949 }
14950 vim_free(pbuf3);
14951 vim_free(pbuf2);
14952 return 0;
14953}
14954
14955/*
14956 * Get a pathname for a partial path.
14957 */
14958 static int
14959shortpath_for_partial(fnamep, bufp, fnamelen)
14960 char_u **fnamep;
14961 char_u **bufp;
14962 int *fnamelen;
14963{
14964 int sepcount, len, tflen;
14965 char_u *p;
14966 char_u *pbuf, *tfname;
14967 int hasTilde;
14968
14969 /* Count up the path seperators from the RHS.. so we know which part
14970 * of the path to return.
14971 */
14972 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014973 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 if (vim_ispathsep(*p))
14975 ++sepcount;
14976
14977 /* Need full path first (use expand_env() to remove a "~/") */
14978 hasTilde = (**fnamep == '~');
14979 if (hasTilde)
14980 pbuf = tfname = expand_env_save(*fnamep);
14981 else
14982 pbuf = tfname = FullName_save(*fnamep, FALSE);
14983
14984 len = tflen = STRLEN(tfname);
14985
14986 if (!get_short_pathname(&tfname, &pbuf, &len))
14987 return -1;
14988
14989 if (len == 0)
14990 {
14991 /* Don't have a valid filename, so shorten the rest of the
14992 * path if we can. This CAN give us invalid 8.3 filenames, but
14993 * there's not a lot of point in guessing what it might be.
14994 */
14995 len = tflen;
14996 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
14997 return -1;
14998 }
14999
15000 /* Count the paths backward to find the beginning of the desired string. */
15001 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015002 {
15003#ifdef FEAT_MBYTE
15004 if (has_mbyte)
15005 p -= mb_head_off(tfname, p);
15006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 if (vim_ispathsep(*p))
15008 {
15009 if (sepcount == 0 || (hasTilde && sepcount == 1))
15010 break;
15011 else
15012 sepcount --;
15013 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 if (hasTilde)
15016 {
15017 --p;
15018 if (p >= tfname)
15019 *p = '~';
15020 else
15021 return -1;
15022 }
15023 else
15024 ++p;
15025
15026 /* Copy in the string - p indexes into tfname - allocated at pbuf */
15027 vim_free(*bufp);
15028 *fnamelen = (int)STRLEN(p);
15029 *bufp = pbuf;
15030 *fnamep = p;
15031
15032 return 0;
15033}
15034#endif /* WIN3264 */
15035
15036/*
15037 * Adjust a filename, according to a string of modifiers.
15038 * *fnamep must be NUL terminated when called. When returning, the length is
15039 * determined by *fnamelen.
15040 * Returns valid flags.
15041 * When there is an error, *fnamep is set to NULL.
15042 */
15043 int
15044modify_fname(src, usedlen, fnamep, bufp, fnamelen)
15045 char_u *src; /* string with modifiers */
15046 int *usedlen; /* characters after src that are used */
15047 char_u **fnamep; /* file name so far */
15048 char_u **bufp; /* buffer for allocated file name or NULL */
15049 int *fnamelen; /* length of fnamep */
15050{
15051 int valid = 0;
15052 char_u *tail;
15053 char_u *s, *p, *pbuf;
15054 char_u dirname[MAXPATHL];
15055 int c;
15056 int has_fullname = 0;
15057#ifdef WIN3264
15058 int has_shortname = 0;
15059#endif
15060
15061repeat:
15062 /* ":p" - full path/file_name */
15063 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
15064 {
15065 has_fullname = 1;
15066
15067 valid |= VALID_PATH;
15068 *usedlen += 2;
15069
15070 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
15071 if ((*fnamep)[0] == '~'
15072#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
15073 && ((*fnamep)[1] == '/'
15074# ifdef BACKSLASH_IN_FILENAME
15075 || (*fnamep)[1] == '\\'
15076# endif
15077 || (*fnamep)[1] == NUL)
15078
15079#endif
15080 )
15081 {
15082 *fnamep = expand_env_save(*fnamep);
15083 vim_free(*bufp); /* free any allocated file name */
15084 *bufp = *fnamep;
15085 if (*fnamep == NULL)
15086 return -1;
15087 }
15088
15089 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015090 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 {
15092 if (vim_ispathsep(*p)
15093 && p[1] == '.'
15094 && (p[2] == NUL
15095 || vim_ispathsep(p[2])
15096 || (p[2] == '.'
15097 && (p[3] == NUL || vim_ispathsep(p[3])))))
15098 break;
15099 }
15100
15101 /* FullName_save() is slow, don't use it when not needed. */
15102 if (*p != NUL || !vim_isAbsName(*fnamep))
15103 {
15104 *fnamep = FullName_save(*fnamep, *p != NUL);
15105 vim_free(*bufp); /* free any allocated file name */
15106 *bufp = *fnamep;
15107 if (*fnamep == NULL)
15108 return -1;
15109 }
15110
15111 /* Append a path separator to a directory. */
15112 if (mch_isdir(*fnamep))
15113 {
15114 /* Make room for one or two extra characters. */
15115 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
15116 vim_free(*bufp); /* free any allocated file name */
15117 *bufp = *fnamep;
15118 if (*fnamep == NULL)
15119 return -1;
15120 add_pathsep(*fnamep);
15121 }
15122 }
15123
15124 /* ":." - path relative to the current directory */
15125 /* ":~" - path relative to the home directory */
15126 /* ":8" - shortname path - postponed till after */
15127 while (src[*usedlen] == ':'
15128 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
15129 {
15130 *usedlen += 2;
15131 if (c == '8')
15132 {
15133#ifdef WIN3264
15134 has_shortname = 1; /* Postpone this. */
15135#endif
15136 continue;
15137 }
15138 pbuf = NULL;
15139 /* Need full path first (use expand_env() to remove a "~/") */
15140 if (!has_fullname)
15141 {
15142 if (c == '.' && **fnamep == '~')
15143 p = pbuf = expand_env_save(*fnamep);
15144 else
15145 p = pbuf = FullName_save(*fnamep, FALSE);
15146 }
15147 else
15148 p = *fnamep;
15149
15150 has_fullname = 0;
15151
15152 if (p != NULL)
15153 {
15154 if (c == '.')
15155 {
15156 mch_dirname(dirname, MAXPATHL);
15157 s = shorten_fname(p, dirname);
15158 if (s != NULL)
15159 {
15160 *fnamep = s;
15161 if (pbuf != NULL)
15162 {
15163 vim_free(*bufp); /* free any allocated file name */
15164 *bufp = pbuf;
15165 pbuf = NULL;
15166 }
15167 }
15168 }
15169 else
15170 {
15171 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
15172 /* Only replace it when it starts with '~' */
15173 if (*dirname == '~')
15174 {
15175 s = vim_strsave(dirname);
15176 if (s != NULL)
15177 {
15178 *fnamep = s;
15179 vim_free(*bufp);
15180 *bufp = s;
15181 }
15182 }
15183 }
15184 vim_free(pbuf);
15185 }
15186 }
15187
15188 tail = gettail(*fnamep);
15189 *fnamelen = (int)STRLEN(*fnamep);
15190
15191 /* ":h" - head, remove "/file_name", can be repeated */
15192 /* Don't remove the first "/" or "c:\" */
15193 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
15194 {
15195 valid |= VALID_HEAD;
15196 *usedlen += 2;
15197 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015198 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 --tail;
15200 *fnamelen = (int)(tail - *fnamep);
15201#ifdef VMS
15202 if (*fnamelen > 0)
15203 *fnamelen += 1; /* the path separator is part of the path */
15204#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015205 while (tail > s && !after_pathsep(s, tail))
15206 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 }
15208
15209 /* ":8" - shortname */
15210 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
15211 {
15212 *usedlen += 2;
15213#ifdef WIN3264
15214 has_shortname = 1;
15215#endif
15216 }
15217
15218#ifdef WIN3264
15219 /* Check shortname after we have done 'heads' and before we do 'tails'
15220 */
15221 if (has_shortname)
15222 {
15223 pbuf = NULL;
15224 /* Copy the string if it is shortened by :h */
15225 if (*fnamelen < (int)STRLEN(*fnamep))
15226 {
15227 p = vim_strnsave(*fnamep, *fnamelen);
15228 if (p == 0)
15229 return -1;
15230 vim_free(*bufp);
15231 *bufp = *fnamep = p;
15232 }
15233
15234 /* Split into two implementations - makes it easier. First is where
15235 * there isn't a full name already, second is where there is.
15236 */
15237 if (!has_fullname && !vim_isAbsName(*fnamep))
15238 {
15239 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
15240 return -1;
15241 }
15242 else
15243 {
15244 int l;
15245
15246 /* Simple case, already have the full-name
15247 * Nearly always shorter, so try first time. */
15248 l = *fnamelen;
15249 if (!get_short_pathname(fnamep, bufp, &l))
15250 return -1;
15251
15252 if (l == 0)
15253 {
15254 /* Couldn't find the filename.. search the paths.
15255 */
15256 l = *fnamelen;
15257 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
15258 return -1;
15259 }
15260 *fnamelen = l;
15261 }
15262 }
15263#endif /* WIN3264 */
15264
15265 /* ":t" - tail, just the basename */
15266 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
15267 {
15268 *usedlen += 2;
15269 *fnamelen -= (int)(tail - *fnamep);
15270 *fnamep = tail;
15271 }
15272
15273 /* ":e" - extension, can be repeated */
15274 /* ":r" - root, without extension, can be repeated */
15275 while (src[*usedlen] == ':'
15276 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
15277 {
15278 /* find a '.' in the tail:
15279 * - for second :e: before the current fname
15280 * - otherwise: The last '.'
15281 */
15282 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
15283 s = *fnamep - 2;
15284 else
15285 s = *fnamep + *fnamelen - 1;
15286 for ( ; s > tail; --s)
15287 if (s[0] == '.')
15288 break;
15289 if (src[*usedlen + 1] == 'e') /* :e */
15290 {
15291 if (s > tail)
15292 {
15293 *fnamelen += (int)(*fnamep - (s + 1));
15294 *fnamep = s + 1;
15295#ifdef VMS
15296 /* cut version from the extension */
15297 s = *fnamep + *fnamelen - 1;
15298 for ( ; s > *fnamep; --s)
15299 if (s[0] == ';')
15300 break;
15301 if (s > *fnamep)
15302 *fnamelen = s - *fnamep;
15303#endif
15304 }
15305 else if (*fnamep <= tail)
15306 *fnamelen = 0;
15307 }
15308 else /* :r */
15309 {
15310 if (s > tail) /* remove one extension */
15311 *fnamelen = (int)(s - *fnamep);
15312 }
15313 *usedlen += 2;
15314 }
15315
15316 /* ":s?pat?foo?" - substitute */
15317 /* ":gs?pat?foo?" - global substitute */
15318 if (src[*usedlen] == ':'
15319 && (src[*usedlen + 1] == 's'
15320 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
15321 {
15322 char_u *str;
15323 char_u *pat;
15324 char_u *sub;
15325 int sep;
15326 char_u *flags;
15327 int didit = FALSE;
15328
15329 flags = (char_u *)"";
15330 s = src + *usedlen + 2;
15331 if (src[*usedlen + 1] == 'g')
15332 {
15333 flags = (char_u *)"g";
15334 ++s;
15335 }
15336
15337 sep = *s++;
15338 if (sep)
15339 {
15340 /* find end of pattern */
15341 p = vim_strchr(s, sep);
15342 if (p != NULL)
15343 {
15344 pat = vim_strnsave(s, (int)(p - s));
15345 if (pat != NULL)
15346 {
15347 s = p + 1;
15348 /* find end of substitution */
15349 p = vim_strchr(s, sep);
15350 if (p != NULL)
15351 {
15352 sub = vim_strnsave(s, (int)(p - s));
15353 str = vim_strnsave(*fnamep, *fnamelen);
15354 if (sub != NULL && str != NULL)
15355 {
15356 *usedlen = (int)(p + 1 - src);
15357 s = do_string_sub(str, pat, sub, flags);
15358 if (s != NULL)
15359 {
15360 *fnamep = s;
15361 *fnamelen = (int)STRLEN(s);
15362 vim_free(*bufp);
15363 *bufp = s;
15364 didit = TRUE;
15365 }
15366 }
15367 vim_free(sub);
15368 vim_free(str);
15369 }
15370 vim_free(pat);
15371 }
15372 }
15373 /* after using ":s", repeat all the modifiers */
15374 if (didit)
15375 goto repeat;
15376 }
15377 }
15378
15379 return valid;
15380}
15381
15382/*
15383 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
15384 * "flags" can be "g" to do a global substitute.
15385 * Returns an allocated string, NULL for error.
15386 */
15387 char_u *
15388do_string_sub(str, pat, sub, flags)
15389 char_u *str;
15390 char_u *pat;
15391 char_u *sub;
15392 char_u *flags;
15393{
15394 int sublen;
15395 regmatch_T regmatch;
15396 int i;
15397 int do_all;
15398 char_u *tail;
15399 garray_T ga;
15400 char_u *ret;
15401 char_u *save_cpo;
15402
15403 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
15404 save_cpo = p_cpo;
15405 p_cpo = (char_u *)"";
15406
15407 ga_init2(&ga, 1, 200);
15408
15409 do_all = (flags[0] == 'g');
15410
15411 regmatch.rm_ic = p_ic;
15412 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15413 if (regmatch.regprog != NULL)
15414 {
15415 tail = str;
15416 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
15417 {
15418 /*
15419 * Get some space for a temporary buffer to do the substitution
15420 * into. It will contain:
15421 * - The text up to where the match is.
15422 * - The substituted text.
15423 * - The text after the match.
15424 */
15425 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
15426 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
15427 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
15428 {
15429 ga_clear(&ga);
15430 break;
15431 }
15432
15433 /* copy the text up to where the match is */
15434 i = (int)(regmatch.startp[0] - tail);
15435 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
15436 /* add the substituted text */
15437 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
15438 + ga.ga_len + i, TRUE, TRUE, FALSE);
15439 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 /* avoid getting stuck on a match with an empty string */
15441 if (tail == regmatch.endp[0])
15442 {
15443 if (*tail == NUL)
15444 break;
15445 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
15446 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 }
15448 else
15449 {
15450 tail = regmatch.endp[0];
15451 if (*tail == NUL)
15452 break;
15453 }
15454 if (!do_all)
15455 break;
15456 }
15457
15458 if (ga.ga_data != NULL)
15459 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
15460
15461 vim_free(regmatch.regprog);
15462 }
15463
15464 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
15465 ga_clear(&ga);
15466 p_cpo = save_cpo;
15467
15468 return ret;
15469}
15470
15471#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */