blob: 1b21a110dc89733a23b06d0b42edcf35b0e27d17 [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
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000133/*
134 * Structure returned by get_lval() and used by set_var_lval().
135 * For a plain name:
136 * "name" points to the variable name.
137 * "exp_name" is NULL.
138 * "tv" is NULL
139 * For a magic braces name:
140 * "name" points to the expanded variable name.
141 * "exp_name" is non-NULL, to be freed later.
142 * "tv" is NULL
143 * For an index in a list:
144 * "name" points to the (expanded) variable name.
145 * "exp_name" NULL or non-NULL, to be freed later.
146 * "tv" points to the (first) list item value
147 * "li" points to the (first) list item
148 * "range", "n1", "n2" and "empty2" indicate what items are used.
149 * For an existing Dict item:
150 * "name" points to the (expanded) variable name.
151 * "exp_name" NULL or non-NULL, to be freed later.
152 * "tv" points to the dict item value
153 * "newkey" is NULL
154 * For a non-existing Dict item:
155 * "name" points to the (expanded) variable name.
156 * "exp_name" NULL or non-NULL, to be freed later.
157 * "tv" points to the Dictionary typeval
158 * "newkey" is the key for the new item.
159 */
160typedef struct lval_S
161{
162 char_u *ll_name; /* start of variable name (can be NULL) */
163 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
164 typeval *ll_tv; /* Typeval of item being used. If "newkey"
165 isn't NULL it's the Dict to which to add
166 the item. */
167 listitem *ll_li; /* The list item or NULL. */
168 listvar *ll_list; /* The list or NULL. */
169 int ll_range; /* TRUE when a [i:j] range was used */
170 long ll_n1; /* First index for list */
171 long ll_n2; /* Second index for list range */
172 int ll_empty2; /* Second index is empty: [i:] */
173 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
174 dictitem *ll_di; /* The dictitem or NULL */
175 dictitem **ll_pdi; /* field that points to found dictitem */
176} lval;
177
Bram Moolenaar8c711452005-01-14 21:53:12 +0000178
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000179static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000180static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000181static char *e_undefvar = N_("E121: Undefined variable: %s");
182static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000183static char *e_intern2 = N_("E685: Internal error: %s");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000184static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +0000185static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000186static char *e_emptykey = N_("E999: Empty key in Dictionary");
187static char *e_listreq = N_("E999: List required");
188static char *e_dictreq = N_("E999: Dictionary required");
189static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
190static char *e_dictkey = N_("E999: key not found in Dictionary: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000191
192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 * All user-defined global variables are stored in "variables".
194 */
195garray_T variables = {0, 0, sizeof(var), 4, NULL};
196
197/*
198 * Array to hold an array with variables local to each sourced script.
199 */
200static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
201#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
202
203
204#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define VAR_GAP_ENTRY(idx, gap) (((VAR)((gap)->ga_data))[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
207#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
208
209static int echo_attr = 0; /* attributes used for ":echo" */
210
211/*
212 * Structure to hold info for a user function.
213 */
214typedef struct ufunc ufunc_T;
215
216struct ufunc
217{
218 ufunc_T *next; /* next function in list */
219 char_u *name; /* name of function; can start with <SNR>123_
220 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
221 int varargs; /* variable nr of arguments */
222 int flags;
223 int calls; /* nr of active calls */
224 garray_T args; /* arguments */
225 garray_T lines; /* function lines */
226 scid_T script_ID; /* ID of script where function was defined,
227 used for s: variables */
228};
229
230/* function flags */
231#define FC_ABORT 1 /* abort function on error */
232#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000233#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
235/*
236 * All user-defined functions are found in the forward-linked function list.
237 * The first function is pointed at by firstfunc.
238 */
239ufunc_T *firstfunc = NULL;
240
241#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
242#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
243
244/* structure to hold info for a function that is currently being executed. */
245struct funccall
246{
247 ufunc_T *func; /* function being called */
248 int linenr; /* next line to be executed */
249 int returned; /* ":return" used */
250 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000251 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 var a0_var; /* "a:0" variable */
253 var firstline; /* "a:firstline" variable */
254 var lastline; /* "a:lastline" variable */
255 garray_T l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000256 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 linenr_T breakpoint; /* next line with breakpoint or zero */
258 int dbg_tick; /* debug_tick when breakpoint was set */
259 int level; /* top nesting level of executed function */
260};
261
262/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263 * Info used by a ":for" loop.
264 */
265typedef struct forinfo_S
266{
267 int fi_semicolon; /* TRUE if ending in '; var]' */
268 int fi_varcount; /* nr of variables in the list */
269 listwatch fi_lw; /* keep an eye on the item used. */
270 listvar *fi_list; /* list being used */
271} forinfo;
272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 * Return the name of the executed function.
275 */
276 char_u *
277func_name(cookie)
278 void *cookie;
279{
280 return ((struct funccall *)cookie)->func->name;
281}
282
283/*
284 * Return the address holding the next breakpoint line for a funccall cookie.
285 */
286 linenr_T *
287func_breakpoint(cookie)
288 void *cookie;
289{
290 return &((struct funccall *)cookie)->breakpoint;
291}
292
293/*
294 * Return the address holding the debug tick for a funccall cookie.
295 */
296 int *
297func_dbg_tick(cookie)
298 void *cookie;
299{
300 return &((struct funccall *)cookie)->dbg_tick;
301}
302
303/*
304 * Return the nesting level for a funccall cookie.
305 */
306 int
307func_level(cookie)
308 void *cookie;
309{
310 return ((struct funccall *)cookie)->level;
311}
312
313/* pointer to funccal for currently active function */
314struct funccall *current_funccal = NULL;
315
316/*
317 * Return TRUE when a function was ended by a ":return" command.
318 */
319 int
320current_func_returned()
321{
322 return current_funccal->returned;
323}
324
325
326/*
327 * Array to hold the value of v: variables.
328 */
329#include "version.h"
330
331/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000332#define VV_COMPAT 1 /* compatible, also used without "v:" */
333#define VV_RO 2 /* read-only */
334#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaare9a41262005-01-15 22:18:47 +0000336#define VV_NAME(s) s, sizeof(s) - 1
337
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338struct vimvar
339{
340 char *name; /* name of variable, without v: */
341 int len; /* length of name */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000342 typeval tv; /* type and value */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000343 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344} vimvars[VV_LEN] =
Bram Moolenaare9a41262005-01-15 22:18:47 +0000345{
346 /*
347 * The order here must match the VV_ defines in vim.h!
348 */
349 {VV_NAME("count"), {VAR_NUMBER, {NULL}}, VV_COMPAT+VV_RO},
350 {VV_NAME("count1"), {VAR_NUMBER, {NULL}}, VV_RO},
351 {VV_NAME("prevcount"), {VAR_NUMBER, {NULL}}, VV_RO},
352 {VV_NAME("errmsg"), {VAR_STRING, {NULL}}, VV_COMPAT},
353 {VV_NAME("warningmsg"), {VAR_STRING, {NULL}}, 0},
354 {VV_NAME("statusmsg"), {VAR_STRING, {NULL}}, 0},
355 {VV_NAME("shell_error"), {VAR_NUMBER, {NULL}}, VV_COMPAT+VV_RO},
356 {VV_NAME("this_session"), {VAR_STRING, {NULL}}, VV_COMPAT},
357 {VV_NAME("version"), {VAR_NUMBER, {VIM_VERSION_100}}, VV_COMPAT+VV_RO},
358 {VV_NAME("lnum"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
359 {VV_NAME("termresponse"), {VAR_STRING, {NULL}}, VV_RO},
360 {VV_NAME("fname"), {VAR_STRING, {NULL}}, VV_RO},
361 {VV_NAME("lang"), {VAR_STRING, {NULL}}, VV_RO},
362 {VV_NAME("lc_time"), {VAR_STRING, {NULL}}, VV_RO},
363 {VV_NAME("ctype"), {VAR_STRING, {NULL}}, VV_RO},
364 {VV_NAME("charconvert_from"), {VAR_STRING, {NULL}}, VV_RO},
365 {VV_NAME("charconvert_to"), {VAR_STRING, {NULL}}, VV_RO},
366 {VV_NAME("fname_in"), {VAR_STRING, {NULL}}, VV_RO},
367 {VV_NAME("fname_out"), {VAR_STRING, {NULL}}, VV_RO},
368 {VV_NAME("fname_new"), {VAR_STRING, {NULL}}, VV_RO},
369 {VV_NAME("fname_diff"), {VAR_STRING, {NULL}}, VV_RO},
370 {VV_NAME("cmdarg"), {VAR_STRING, {NULL}}, VV_RO},
371 {VV_NAME("foldstart"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
372 {VV_NAME("foldend"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
373 {VV_NAME("folddashes"), {VAR_STRING, {NULL}}, VV_RO_SBX},
374 {VV_NAME("foldlevel"), {VAR_NUMBER, {NULL}}, VV_RO_SBX},
375 {VV_NAME("progname"), {VAR_STRING, {NULL}}, VV_RO},
376 {VV_NAME("servername"), {VAR_STRING, {NULL}}, VV_RO},
377 {VV_NAME("dying"), {VAR_NUMBER, {NULL}}, VV_RO},
378 {VV_NAME("exception"), {VAR_STRING, {NULL}}, VV_RO},
379 {VV_NAME("throwpoint"), {VAR_STRING, {NULL}}, VV_RO},
380 {VV_NAME("register"), {VAR_STRING, {NULL}}, VV_RO},
381 {VV_NAME("cmdbang"), {VAR_NUMBER, {NULL}}, VV_RO},
382 {VV_NAME("insertmode"), {VAR_STRING, {NULL}}, VV_RO},
383 {VV_NAME("val"), {VAR_UNKNOWN, {NULL}}, VV_RO},
384 {VV_NAME("key"), {VAR_UNKNOWN, {NULL}}, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385};
386
Bram Moolenaare9a41262005-01-15 22:18:47 +0000387/* shorthand */
388#define vv_nr tv.vval.v_number
389#define vv_str tv.vval.v_string
390
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000391static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
392static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
393static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
394static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
395static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
396static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
397static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
398static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
399static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
400static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
401static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
402static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
403static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000404static listvar *list_alloc __ARGS((void));
405static void list_unref __ARGS((listvar *l));
406static void list_free __ARGS((listvar *l));
407static listitem *listitem_alloc __ARGS((void));
408static void listitem_free __ARGS((listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000409static void listitem_remove __ARGS((listvar *l, listitem *item));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000410static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000411static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
412static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000413static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000414static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000415static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000416static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000417static void list_append __ARGS((listvar *l, listitem *item));
418static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000419static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
420static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
421static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000423static void list_remove __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000424static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000425static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
426
Bram Moolenaar8c711452005-01-14 21:53:12 +0000427static dictvar *dict_alloc __ARGS((void));
428static void dict_unref __ARGS((dictvar *d));
429static void dict_free __ARGS((dictvar *d));
430static dictitem *dictitem_alloc __ARGS((void));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000431static dictitem *dictitem_copy __ARGS((dictitem *org));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000432static void dictitem_free __ARGS((dictitem *item));
433static void dict_add __ARGS((dictvar *d, dictitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000434static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len, dictitem ***pdi));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000435static char_u *dict2string __ARGS((typeval *tv));
436static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
437
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000438static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000439static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000441static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000444static 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));
445static 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 +0000446
447static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000448static void f_append __ARGS((typeval *argvars, typeval *rettv));
449static void f_argc __ARGS((typeval *argvars, typeval *rettv));
450static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
451static void f_argv __ARGS((typeval *argvars, typeval *rettv));
452static void f_browse __ARGS((typeval *argvars, typeval *rettv));
453static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000454static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
455static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
456static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000457static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
458static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
459static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
460static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
461static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000462static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000463static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
464static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
465static void f_col __ARGS((typeval *argvars, typeval *rettv));
466static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
467static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000468static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000469static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
470static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
471static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
472static void f_delete __ARGS((typeval *argvars, typeval *rettv));
473static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
474static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
475static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000476static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000477static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000478static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000479static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
480static void f_executable __ARGS((typeval *argvars, typeval *rettv));
481static void f_exists __ARGS((typeval *argvars, typeval *rettv));
482static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000483static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000484static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
485static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000486static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000487static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
488static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000489static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
490static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
491static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000492static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
493static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
494static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
495static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
496static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000497static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000498static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
499static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
500static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
501static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
502static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
503static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
504static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
505static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
506static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
507static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
508static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
509static void f_getline __ARGS((typeval *argvars, typeval *rettv));
510static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
511static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
512static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
513static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
514static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
515static void f_glob __ARGS((typeval *argvars, typeval *rettv));
516static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
517static void f_has __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000518static void f_has_key __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000519static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
520static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
521static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
522static void f_histget __ARGS((typeval *argvars, typeval *rettv));
523static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000524static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000525static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000526static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
527static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
528static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000529static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000530static void f_input __ARGS((typeval *argvars, typeval *rettv));
531static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
532static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
533static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
534static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000535static void f_insert __ARGS((typeval *argvars, typeval *rettv));
536static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000537static void f_items __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000538static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000539static void f_keys __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000540static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
541static void f_len __ARGS((typeval *argvars, typeval *rettv));
542static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
543static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544static void f_line __ARGS((typeval *argvars, typeval *rettv));
545static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
546static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
547static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000548static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000549static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
550static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000551static void f_match __ARGS((typeval *argvars, typeval *rettv));
552static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
553static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000554static void f_max __ARGS((typeval *argvars, typeval *rettv));
555static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000556static void f_mode __ARGS((typeval *argvars, typeval *rettv));
557static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
558static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
559static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000560static void f_range __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000561static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
562static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
563static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
564static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
565static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000566static void f_remove __ARGS((typeval *argvars, typeval *rettv));
567static void f_rename __ARGS((typeval *argvars, typeval *rettv));
568static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
569static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
570static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
571static void f_search __ARGS((typeval *argvars, typeval *rettv));
572static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000573static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
574static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000575static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
576static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000577static void f_setline __ARGS((typeval *argvars, typeval *rettv));
578static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000579static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000580static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000581static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000582static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000583#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000584static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000585#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
587static void f_string __ARGS((typeval *argvars, typeval *rettv));
588static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
589static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
590static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
591static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000592static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
593static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594static void f_synID __ARGS((typeval *argvars, typeval *rettv));
595static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
596static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
597static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
599static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
600static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
601static void f_tr __ARGS((typeval *argvars, typeval *rettv));
602static void f_type __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000603static void f_values __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000604static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
605static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
606static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
607static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
608static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
609static void f_winline __ARGS((typeval *argvars, typeval *rettv));
610static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
611static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
612static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000613
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000614static win_T *find_win_by_nr __ARGS((typeval *vp));
615static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616static int get_env_len __ARGS((char_u **arg));
617static int get_id_len __ARGS((char_u **arg));
618static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000619static 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 +0000620static int eval_isnamec __ARGS((int c));
621static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000622static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
623static typeval *alloc_tv __ARGS((void));
624static typeval *alloc_string_tv __ARGS((char_u *string));
625static void free_tv __ARGS((typeval *varp));
626static void clear_tv __ARGS((typeval *varp));
627static void init_tv __ARGS((typeval *varp));
628static long get_tv_number __ARGS((typeval *varp));
629static linenr_T get_tv_lnum __ARGS((typeval *argvars));
630static char_u *get_tv_string __ARGS((typeval *varp));
631static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632static VAR find_var __ARGS((char_u *name, int writing));
633static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
634static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000635static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636static void list_one_var __ARGS((VAR v, char_u *prefix));
637static void list_vim_var __ARGS((int i));
638static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000639static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000640static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000641static void item_copy __ARGS((typeval *from, typeval *to, int deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000643static char_u *trans_function_name __ARGS((char_u **pp, int skip, int exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644static int eval_fname_script __ARGS((char_u *p));
645static int eval_fname_sid __ARGS((char_u *p));
646static void list_func_head __ARGS((ufunc_T *fp, int indent));
647static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
648static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000649static int function_exists __ARGS((char_u *name));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000650static 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 +0000651
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000652#define get_var_string(p) get_tv_string(&(p)->tv)
653#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
654#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656static 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 +0000657
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000658static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
659static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
660static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000661static void list_all_vars __ARGS((void));
662static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
663static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000664static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000665static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet));
666static void clear_lval __ARGS((lval *lp));
667static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000668static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000669static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
670static void list_fix_watch __ARGS((listvar *l, listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000671static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
673/*
674 * Set an internal variable to a string value. Creates the variable if it does
675 * not already exist.
676 */
677 void
678set_internal_string_var(name, value)
679 char_u *name;
680 char_u *value;
681{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000682 char_u *val;
683 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685 val = vim_strsave(value);
686 if (val != NULL)
687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000688 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000689 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000691 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000692 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694 }
695}
696
697# if defined(FEAT_MBYTE) || defined(PROTO)
698 int
699eval_charconvert(enc_from, enc_to, fname_from, fname_to)
700 char_u *enc_from;
701 char_u *enc_to;
702 char_u *fname_from;
703 char_u *fname_to;
704{
705 int err = FALSE;
706
707 set_vim_var_string(VV_CC_FROM, enc_from, -1);
708 set_vim_var_string(VV_CC_TO, enc_to, -1);
709 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
710 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
711 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
712 err = TRUE;
713 set_vim_var_string(VV_CC_FROM, NULL, -1);
714 set_vim_var_string(VV_CC_TO, NULL, -1);
715 set_vim_var_string(VV_FNAME_IN, NULL, -1);
716 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
717
718 if (err)
719 return FAIL;
720 return OK;
721}
722# endif
723
724# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
725 int
726eval_printexpr(fname, args)
727 char_u *fname;
728 char_u *args;
729{
730 int err = FALSE;
731
732 set_vim_var_string(VV_FNAME_IN, fname, -1);
733 set_vim_var_string(VV_CMDARG, args, -1);
734 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
735 err = TRUE;
736 set_vim_var_string(VV_FNAME_IN, NULL, -1);
737 set_vim_var_string(VV_CMDARG, NULL, -1);
738
739 if (err)
740 {
741 mch_remove(fname);
742 return FAIL;
743 }
744 return OK;
745}
746# endif
747
748# if defined(FEAT_DIFF) || defined(PROTO)
749 void
750eval_diff(origfile, newfile, outfile)
751 char_u *origfile;
752 char_u *newfile;
753 char_u *outfile;
754{
755 int err = FALSE;
756
757 set_vim_var_string(VV_FNAME_IN, origfile, -1);
758 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
759 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
760 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
761 set_vim_var_string(VV_FNAME_IN, NULL, -1);
762 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
763 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
764}
765
766 void
767eval_patch(origfile, difffile, outfile)
768 char_u *origfile;
769 char_u *difffile;
770 char_u *outfile;
771{
772 int err;
773
774 set_vim_var_string(VV_FNAME_IN, origfile, -1);
775 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
776 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
777 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
778 set_vim_var_string(VV_FNAME_IN, NULL, -1);
779 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
780 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
781}
782# endif
783
784/*
785 * Top level evaluation function, returning a boolean.
786 * Sets "error" to TRUE if there was an error.
787 * Return TRUE or FALSE.
788 */
789 int
790eval_to_bool(arg, error, nextcmd, skip)
791 char_u *arg;
792 int *error;
793 char_u **nextcmd;
794 int skip; /* only parse, don't execute */
795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000796 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 int retval = FALSE;
798
799 if (skip)
800 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 else
804 {
805 *error = FALSE;
806 if (!skip)
807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 retval = (get_tv_number(&tv) != 0);
809 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811 }
812 if (skip)
813 --emsg_skip;
814
815 return retval;
816}
817
818/*
819 * Top level evaluation function, returning a string. If "skip" is TRUE,
820 * only parsing to "nextcmd" is done, without reporting errors. Return
821 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
822 */
823 char_u *
824eval_to_string_skip(arg, nextcmd, skip)
825 char_u *arg;
826 char_u **nextcmd;
827 int skip; /* only parse, don't execute */
828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 char_u *retval;
831
832 if (skip)
833 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 retval = NULL;
836 else
837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000838 retval = vim_strsave(get_tv_string(&tv));
839 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841 if (skip)
842 --emsg_skip;
843
844 return retval;
845}
846
847/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000848 * Skip over an expression at "*pp".
849 * Return FAIL for an error, OK otherwise.
850 */
851 int
852skip_expr(pp)
853 char_u **pp;
854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000856
857 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000859}
860
861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * Top level evaluation function, returning a string.
863 * Return pointer to allocated memory, or NULL for failure.
864 */
865 char_u *
866eval_to_string(arg, nextcmd)
867 char_u *arg;
868 char_u **nextcmd;
869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 char_u *retval;
872
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 retval = NULL;
875 else
876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000877 retval = vim_strsave(get_tv_string(&tv));
878 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880
881 return retval;
882}
883
884/*
885 * Call eval_to_string() with "sandbox" set and not using local variables.
886 */
887 char_u *
888eval_to_string_safe(arg, nextcmd)
889 char_u *arg;
890 char_u **nextcmd;
891{
892 char_u *retval;
893 void *save_funccalp;
894
895 save_funccalp = save_funccal();
896 ++sandbox;
897 retval = eval_to_string(arg, nextcmd);
898 --sandbox;
899 restore_funccal(save_funccalp);
900 return retval;
901}
902
903#if 0 /* not used */
904/*
905 * Top level evaluation function, returning a string.
906 * Advances "arg" to the first non-blank after the evaluated expression.
907 * Return pointer to allocated memory, or NULL for failure.
908 * Doesn't give error messages.
909 */
910 char_u *
911eval_arg_to_string(arg)
912 char_u **arg;
913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000914 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 char_u *retval;
916 int ret;
917
918 ++emsg_off;
919
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000920 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (ret == FAIL)
922 retval = NULL;
923 else
924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 retval = vim_strsave(get_tv_string(&rettv));
926 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928
929 --emsg_off;
930
931 return retval;
932}
933#endif
934
935/*
936 * Top level evaluation function, returning a number.
937 * Evaluates "expr" silently.
938 * Returns -1 for an error.
939 */
940 int
941eval_to_number(expr)
942 char_u *expr;
943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000946 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
948 ++emsg_off;
949
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000950 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 retval = -1;
952 else
953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 retval = get_tv_number(&rettv);
955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957 --emsg_off;
958
959 return retval;
960}
961
962#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
963/*
964 * Call some vimL function and return the result as a string
965 * Uses argv[argc] for the function arguments.
966 */
967 char_u *
968call_vim_function(func, argc, argv, safe)
969 char_u *func;
970 int argc;
971 char_u **argv;
972 int safe; /* use the sandbox */
973{
974 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000976 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 long n;
978 int len;
979 int i;
980 int doesrange;
981 void *save_funccalp = NULL;
982
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000983 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (argvars == NULL)
985 return NULL;
986
987 for (i = 0; i < argc; i++)
988 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000989 /* Pass a NULL or empty argument as an empty string */
990 if (argv[i] == NULL || *argv[i] == NUL)
991 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000992 argvars[i].v_type = VAR_STRING;
993 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000994 continue;
995 }
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /* Recognize a number argument, the others must be strings. */
998 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
999 if (len != 0 && len == (int)STRLEN(argv[i]))
1000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 argvars[i].v_type = VAR_NUMBER;
1002 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 else
1005 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 argvars[i].v_type = VAR_STRING;
1007 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009 }
1010
1011 if (safe)
1012 {
1013 save_funccalp = save_funccal();
1014 ++sandbox;
1015 }
1016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1018 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001020 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001023 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 vim_free(argvars);
1025
1026 if (safe)
1027 {
1028 --sandbox;
1029 restore_funccal(save_funccalp);
1030 }
1031 return retval;
1032}
1033#endif
1034
1035/*
1036 * Save the current function call pointer, and set it to NULL.
1037 * Used when executing autocommands and for ":source".
1038 */
1039 void *
1040save_funccal()
1041{
1042 struct funccall *fc;
1043
1044 fc = current_funccal;
1045 current_funccal = NULL;
1046 return (void *)fc;
1047}
1048
1049 void
1050restore_funccal(fc)
1051 void *fc;
1052{
1053 current_funccal = (struct funccall *)fc;
1054}
1055
1056#ifdef FEAT_FOLDING
1057/*
1058 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1059 * it in "*cp". Doesn't give error messages.
1060 */
1061 int
1062eval_foldexpr(arg, cp)
1063 char_u *arg;
1064 int *cp;
1065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 int retval;
1068 char_u *s;
1069
1070 ++emsg_off;
1071 ++sandbox;
1072 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001073 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 retval = 0;
1075 else
1076 {
1077 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001078 if (tv.v_type == VAR_NUMBER)
1079 retval = tv.vval.v_number;
1080 else if (tv.v_type == VAR_UNKNOWN
1081 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 retval = 0;
1083 else
1084 {
1085 /* If the result is a string, check if there is a non-digit before
1086 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (!VIM_ISDIGIT(*s) && *s != '-')
1089 *cp = *s++;
1090 retval = atol((char *)s);
1091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 }
1094 --emsg_off;
1095 --sandbox;
1096
1097 return retval;
1098}
1099#endif
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101/*
1102 * Expands out the 'magic' {}'s in a variable/function name.
1103 * Note that this can call itself recursively, to deal with
1104 * constructs like foo{bar}{baz}{bam}
1105 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1106 * "in_start" ^
1107 * "expr_start" ^
1108 * "expr_end" ^
1109 * "in_end" ^
1110 *
1111 * Returns a new allocated string, which the caller must free.
1112 * Returns NULL for failure.
1113 */
1114 static char_u *
1115make_expanded_name(in_start, expr_start, expr_end, in_end)
1116 char_u *in_start;
1117 char_u *expr_start;
1118 char_u *expr_end;
1119 char_u *in_end;
1120{
1121 char_u c1;
1122 char_u *retval = NULL;
1123 char_u *temp_result;
1124 char_u *nextcmd = NULL;
1125
1126 if (expr_end == NULL || in_end == NULL)
1127 return NULL;
1128 *expr_start = NUL;
1129 *expr_end = NUL;
1130 c1 = *in_end;
1131 *in_end = NUL;
1132
1133 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1134 if (temp_result != NULL && nextcmd == NULL)
1135 {
1136 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1137 + (in_end - expr_end) + 1));
1138
1139 if (retval != NULL)
1140 {
1141 STRCPY(retval, in_start);
1142 STRCAT(retval, temp_result);
1143 STRCAT(retval, expr_end + 1);
1144 }
1145 }
1146 vim_free(temp_result);
1147
1148 *in_end = c1; /* put char back for error messages */
1149 *expr_start = '{';
1150 *expr_end = '}';
1151
1152 if (retval != NULL)
1153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (expr_start != NULL)
1156 {
1157 /* Further expansion! */
1158 temp_result = make_expanded_name(retval, expr_start,
1159 expr_end, temp_result);
1160 vim_free(retval);
1161 retval = temp_result;
1162 }
1163 }
1164
1165 return retval;
1166
1167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
1169/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 * ":let" list all variable values
1171 * ":let var1 var2" list variable values
1172 * ":let var = expr" assignment command.
1173 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 */
1175 void
1176ex_let(eap)
1177 exarg_T *eap;
1178{
1179 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001180 char_u *expr = NULL;
1181 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 int var_count = 0;
1184 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001186 expr = skip_var_list(arg, &var_count, &semicolon);
1187 if (expr == NULL)
1188 return;
1189 expr = vim_strchr(expr, '=');
1190 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001192 if (*arg == '[')
1193 EMSG(_(e_invarg));
1194 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 /* ":let var1 var2" */
1196 arg = list_arg_vars(eap, arg);
1197 else if (!eap->skip)
1198 /* ":let" */
1199 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 eap->nextcmd = check_nextcmd(arg);
1201 }
1202 else
1203 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001204 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (eap->skip)
1207 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001208 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (eap->skip)
1210 {
1211 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 --emsg_skip;
1214 }
1215 else if (i != FAIL)
1216 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001217 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1218 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 }
1222}
1223
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001224/*
1225 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1226 * Handles both "var" with any type and "[var, var; var]" with a list type.
1227 * Returns OK or FAIL;
1228 */
1229 static int
1230ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1231 char_u *arg_start;
1232 typeval *tv;
1233 int copy; /* copy values from "tv", don't move */
1234 int semicolon; /* from skip_var_list() */
1235 int var_count; /* from skip_var_list() */
1236 char_u *nextchars; /* characters that must follow or NULL */
1237{
1238 char_u *arg = arg_start;
1239 listvar *l;
1240 int i;
1241 listitem *item;
1242 typeval ltv;
1243
1244 if (*arg != '[')
1245 {
1246 /*
1247 * ":let var = expr" or ":for var in list"
1248 */
1249 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1250 return FAIL;
1251 return OK;
1252 }
1253
1254 /*
1255 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1256 */
1257 l = tv->vval.v_list;
1258 if (tv->v_type != VAR_LIST || l == NULL)
1259 {
1260 EMSG(_(e_listreq));
1261 return FAIL;
1262 }
1263
1264 i = list_len(l);
1265 if (semicolon == 0 && var_count < i)
1266 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001267 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001268 return FAIL;
1269 }
1270 if (var_count - semicolon > i)
1271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001272 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 }
1275
1276 item = l->lv_first;
1277 while (*arg != ']')
1278 {
1279 arg = skipwhite(arg + 1);
1280 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1281 item = item->li_next;
1282 if (arg == NULL)
1283 return FAIL;
1284
1285 arg = skipwhite(arg);
1286 if (*arg == ';')
1287 {
1288 /* Put the rest of the list (may be empty) in the var after ';'.
1289 * Create a new list for this. */
1290 l = list_alloc();
1291 if (l == NULL)
1292 return FAIL;
1293 while (item != NULL)
1294 {
1295 list_append_tv(l, &item->li_tv);
1296 item = item->li_next;
1297 }
1298
1299 ltv.v_type = VAR_LIST;
1300 ltv.vval.v_list = l;
1301 l->lv_refcount = 1;
1302
1303 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1304 clear_tv(&ltv);
1305 if (arg == NULL)
1306 return FAIL;
1307 break;
1308 }
1309 else if (*arg != ',' && *arg != ']')
1310 {
1311 EMSG2(_(e_intern2), "ex_let_vars()");
1312 return FAIL;
1313 }
1314 }
1315
1316 return OK;
1317}
1318
1319/*
1320 * Skip over assignable variable "var" or list of variables "[var, var]".
1321 * Used for ":let varvar = expr" and ":for varvar in expr".
1322 * For "[var, var]" increment "*var_count" for each variable.
1323 * for "[var, var; var]" set "semicolon".
1324 * Return NULL for an error.
1325 */
1326 static char_u *
1327skip_var_list(arg, var_count, semicolon)
1328 char_u *arg;
1329 int *var_count;
1330 int *semicolon;
1331{
1332 char_u *p, *s;
1333
1334 if (*arg == '[')
1335 {
1336 /* "[var, var]": find the matching ']'. */
1337 p = arg;
1338 while (1)
1339 {
1340 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1341 s = skip_var_one(p);
1342 if (s == p)
1343 {
1344 EMSG2(_(e_invarg2), p);
1345 return NULL;
1346 }
1347 ++*var_count;
1348
1349 p = skipwhite(s);
1350 if (*p == ']')
1351 break;
1352 else if (*p == ';')
1353 {
1354 if (*semicolon == 1)
1355 {
1356 EMSG(_("Double ; in list of variables"));
1357 return NULL;
1358 }
1359 *semicolon = 1;
1360 }
1361 else if (*p != ',')
1362 {
1363 EMSG2(_(e_invarg2), p);
1364 return NULL;
1365 }
1366 }
1367 return p + 1;
1368 }
1369 else
1370 return skip_var_one(arg);
1371}
1372
1373 static char_u *
1374skip_var_one(arg)
1375 char_u *arg;
1376{
1377 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1378 ++arg;
1379 return find_name_end(arg, NULL, NULL, TRUE);
1380}
1381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 static void
1383list_all_vars()
1384{
1385 int i;
1386
1387 /*
1388 * List all variables.
1389 */
1390 for (i = 0; i < variables.ga_len && !got_int; ++i)
1391 if (VAR_ENTRY(i).v_name != NULL)
1392 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1393 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1394 if (BVAR_ENTRY(i).v_name != NULL)
1395 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1396 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1397 if (WVAR_ENTRY(i).v_name != NULL)
1398 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1399 for (i = 0; i < VV_LEN && !got_int; ++i)
Bram Moolenaare9a41262005-01-15 22:18:47 +00001400 if (vimvars[i].tv.v_type == VAR_NUMBER || vimvars[i].vv_str != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 list_vim_var(i);
1402}
1403
1404/*
1405 * List variables in "arg".
1406 */
1407 static char_u *
1408list_arg_vars(eap, arg)
1409 exarg_T *eap;
1410 char_u *arg;
1411{
1412 int error = FALSE;
1413 char_u *temp_string = NULL;
1414 int arg_len;
1415 char_u *expr_start;
1416 char_u *expr_end;
1417 char_u *name_end;
1418 int c1 = 0, c2;
1419 int i;
1420 VAR varp;
1421 char_u *name;
1422
1423 while (!ends_excmd(*arg) && !got_int)
1424 {
1425 /* Find the end of the name. */
1426 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1427
1428 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1429 {
1430 emsg_severe = TRUE;
1431 EMSG(_(e_trailing));
1432 break;
1433 }
1434 if (!error && !eap->skip)
1435 {
1436 if (expr_start != NULL)
1437 {
1438 temp_string = make_expanded_name(arg, expr_start,
1439 expr_end, name_end);
1440 if (temp_string == NULL)
1441 {
1442 /*
1443 * Report an invalid expression in braces, unless
1444 * the expression evaluation has been cancelled due
1445 * to an aborting error, an interrupt, or an
1446 * exception.
1447 */
1448 if (!aborting())
1449 {
1450 emsg_severe = TRUE;
1451 EMSG2(_(e_invarg2), arg);
1452 break;
1453 }
1454 error = TRUE;
1455 arg = skipwhite(name_end);
1456 continue;
1457 }
1458 arg = temp_string;
1459 arg_len = STRLEN(temp_string);
1460 }
1461 else
1462 {
1463 c1 = *name_end;
1464 *name_end = NUL;
1465 arg_len = (int)(name_end - arg);
1466 }
1467 i = find_vim_var(arg, arg_len);
1468 if (i >= 0)
1469 list_vim_var(i);
1470 else if (STRCMP("b:changedtick", arg) == 0)
1471 {
1472 char_u numbuf[NUMBUFLEN];
1473
1474 sprintf((char *)numbuf, "%ld",
1475 (long)curbuf->b_changedtick);
1476 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1477 VAR_NUMBER, numbuf);
1478 }
1479 else
1480 {
1481 varp = find_var(arg, FALSE);
1482 if (varp == NULL)
1483 {
1484 /* Skip further arguments but do continue to
1485 * search for a trailing command. */
1486 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1487 error = TRUE;
1488 }
1489 else
1490 {
1491 name = vim_strchr(arg, ':');
1492 if (name != NULL)
1493 {
1494 /* "a:" vars have no name stored, use whole arg */
1495 if (arg[0] == 'a' && arg[1] == ':')
1496 c2 = NUL;
1497 else
1498 {
1499 c2 = *++name;
1500 *name = NUL;
1501 }
1502 list_one_var(varp, arg);
1503 if (c2 != NUL)
1504 *name = c2;
1505 }
1506 else
1507 list_one_var(varp, (char_u *)"");
1508 }
1509 }
1510 if (expr_start != NULL)
1511 vim_free(temp_string);
1512 else
1513 *name_end = c1;
1514 }
1515 arg = skipwhite(name_end);
1516 }
1517
1518 return arg;
1519}
1520
1521/*
1522 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1523 * Returns a pointer to the char just after the var name.
1524 * Returns NULL if there is an error.
1525 */
1526 static char_u *
1527ex_let_one(arg, tv, copy, endchars)
1528 char_u *arg; /* points to variable name */
1529 typeval *tv; /* value to assign to variable */
1530 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532{
1533 int c1;
1534 char_u *name;
1535 char_u *p;
1536 char_u *arg_end = NULL;
1537 int len;
1538 int opt_flags;
1539
1540 /*
1541 * ":let $VAR = expr": Set environment variable.
1542 */
1543 if (*arg == '$')
1544 {
1545 /* Find the end of the name. */
1546 ++arg;
1547 name = arg;
1548 len = get_env_len(&arg);
1549 if (len == 0)
1550 EMSG2(_(e_invarg2), name - 1);
1551 else
1552 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001553 if (endchars != NULL
1554 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 EMSG(_(e_letunexp));
1556 else
1557 {
1558 c1 = name[len];
1559 name[len] = NUL;
1560 p = get_tv_string(tv);
1561 vim_setenv(name, p);
1562 if (STRICMP(name, "HOME") == 0)
1563 init_homedir();
1564 else if (didset_vim && STRICMP(name, "VIM") == 0)
1565 didset_vim = FALSE;
1566 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1567 didset_vimruntime = FALSE;
1568 name[len] = c1;
1569 arg_end = arg;
1570 }
1571 }
1572 }
1573
1574 /*
1575 * ":let &option = expr": Set option value.
1576 * ":let &l:option = expr": Set local option value.
1577 * ":let &g:option = expr": Set global option value.
1578 */
1579 else if (*arg == '&')
1580 {
1581 /* Find the end of the name. */
1582 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001583 if (p == NULL || (endchars != NULL
1584 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 EMSG(_(e_letunexp));
1586 else
1587 {
1588 c1 = *p;
1589 *p = NUL;
1590 set_option_value(arg, get_tv_number(tv),
1591 get_tv_string(tv), opt_flags);
1592 *p = c1;
1593 arg_end = p;
1594 }
1595 }
1596
1597 /*
1598 * ":let @r = expr": Set register contents.
1599 */
1600 else if (*arg == '@')
1601 {
1602 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001603 if (endchars != NULL
1604 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 EMSG(_(e_letunexp));
1606 else
1607 {
1608 write_reg_contents(*arg == '@' ? '"' : *arg,
1609 get_tv_string(tv), -1, FALSE);
1610 arg_end = arg + 1;
1611 }
1612 }
1613
1614 /*
1615 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001618 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 lval lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1623 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001625 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1626 EMSG(_(e_letunexp));
1627 else
1628 {
1629 set_var_lval(&lv, p, tv, copy);
1630 arg_end = p;
1631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001633 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 }
1635
1636 else
1637 EMSG2(_(e_invarg2), arg);
1638
1639 return arg_end;
1640}
1641
1642/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001643 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1644 */
1645 static int
1646check_changedtick(arg)
1647 char_u *arg;
1648{
1649 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1650 {
1651 EMSG2(_(e_readonlyvar), arg);
1652 return TRUE;
1653 }
1654 return FALSE;
1655}
1656
1657/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001658 * Get an lval: variable, Dict item or List item that can be assigned a value
1659 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1660 * "name.key", "name.key[expr]" etc.
1661 * Indexing only works if "name" is an existing List or Dictionary.
1662 * "name" points to the start of the name.
1663 * If "rettv" is not NULL it points to the value to be assigned.
1664 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1665 * wrong; must end in space or cmd separator.
1666 *
1667 * Returns a pointer to just after the name, including indexes.
1668 * When an evaluation error occurs "lp->name" is NULL;
1669 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 */
1671 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001672get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 typeval *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 lval *lp;
1676 int unlet;
1677 int skip;
1678 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001681 char_u *expr_start, *expr_end;
1682 int cc;
1683 VAR v;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001685 typeval var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001686 int empty1 = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001687 listitem *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001688 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001689 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 /* Clear everything in "lp". */
1692 vim_memset(lp, 0, sizeof(lval));
1693
1694 if (skip)
1695 {
1696 /* When skipping just find the end of the name. */
1697 lp->ll_name = name;
1698 return find_name_end(name, NULL, NULL, TRUE);
1699 }
1700
1701 /* Find the end of the name. */
1702 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1703 if (expr_start != NULL)
1704 {
1705 /* Don't expand the name when we already know there is an error. */
1706 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1707 && *p != '[' && *p != '.')
1708 {
1709 EMSG(_(e_trailing));
1710 return NULL;
1711 }
1712
1713 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1714 if (lp->ll_exp_name == NULL)
1715 {
1716 /* Report an invalid expression in braces, unless the
1717 * expression evaluation has been cancelled due to an
1718 * aborting error, an interrupt, or an exception. */
1719 if (!aborting() && !quiet)
1720 {
1721 if (unlet)
1722 emsg_severe = TRUE;
1723 EMSG2(_(e_invarg2), name);
1724 return NULL;
1725 }
1726 }
1727 lp->ll_name = lp->ll_exp_name;
1728 }
1729 else
1730 lp->ll_name = name;
1731
1732 /* Without [idx] or .key we are done. */
1733 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1734 return p;
1735
1736 cc = *p;
1737 *p = NUL;
1738 v = find_var(lp->ll_name, TRUE);
1739 if (v == NULL && !quiet)
1740 EMSG2(_(e_undefvar), lp->ll_name);
1741 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 if (v == NULL)
1743 return NULL;
1744
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001745 /*
1746 * Loop until no more [idx] or .key is following.
1747 */
1748 lp->ll_tv = &v->tv;
1749 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001751 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1752 && !(lp->ll_tv->v_type == VAR_DICT
1753 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001755 if (!quiet)
1756 EMSG(_("E689: Can only index a List or Dictionary"));
1757 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001759 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001761 if (!quiet)
1762 EMSG(_("E708: [:] must come last"));
1763 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001765
Bram Moolenaar8c711452005-01-14 21:53:12 +00001766 len = -1;
1767 if (*p == '.')
1768 {
1769 key = p + 1;
1770 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1771 ;
1772 if (len == 0)
1773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001774 if (!quiet)
1775 EMSG(_(e_emptykey));
1776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001777 }
1778 p = key + len;
1779 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001780 else
1781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001782 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001783 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001784 if (*p == ':')
1785 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001786 else
1787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001788 empty1 = FALSE;
1789 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001791 }
1792
1793 /* Optionally get the second index [ :expr]. */
1794 if (*p == ':')
1795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001797 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001798 if (!quiet)
1799 EMSG(_("E999: Cannot use [:] with a Dictionary"));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001800 if (!empty1)
1801 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001802 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001803 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001804 if (rettv != NULL && (rettv->v_type != VAR_LIST
1805 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001807 if (!quiet)
1808 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001809 if (!empty1)
1810 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001811 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001812 }
1813 p = skipwhite(p + 1);
1814 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001815 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001816 else
1817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001818 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001819 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001821 if (!empty1)
1822 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001823 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001824 }
1825 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001826 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001827 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001829 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001830
Bram Moolenaar8c711452005-01-14 21:53:12 +00001831 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 if (!quiet)
1834 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001835 if (!empty1)
1836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001838 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001839 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001840 }
1841
1842 /* Skip to past ']'. */
1843 ++p;
1844 }
1845
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001846 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001847 {
1848 if (len == -1)
1849 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001850 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001851 key = get_tv_string(&var1);
1852 if (*key == NUL)
1853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 if (!quiet)
1855 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001858 }
1859 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 lp->ll_di = dict_find(lp->ll_tv->vval.v_dict, key, len,
1861 &lp->ll_pdi);
1862 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001863 {
1864 /* Key does not exist in dict: may need toadd it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001865 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 if (!quiet)
1868 EMSG2(_("E999: Key does not exist in Dictionary: %s"),
1869 key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001870 if (len == -1)
1871 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001873 }
1874 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001876 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001877 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001878 if (len == -1)
1879 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001880 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001881 p = NULL;
1882 break;
1883 }
1884 if (len == -1)
1885 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001887 }
1888 else
1889 {
1890 /*
1891 * Get the number and item for the only or first index of the List.
1892 */
1893 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001895 else
1896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001898 clear_tv(&var1);
1899 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 lp->ll_list = lp->ll_tv->vval.v_list;
1901 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1902 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (!quiet)
1905 EMSGN(_(e_listidx), lp->ll_n1);
1906 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001907 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001909 }
1910
1911 /*
1912 * May need to find the item or absolute index for the second
1913 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 * When no index given: "lp->ll_empty2" is TRUE.
1915 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001916 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001920 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001924 if (ni == NULL)
1925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001926 if (!quiet)
1927 EMSGN(_(e_listidx), lp->ll_n2);
1928 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 }
1932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
1934 if (lp->ll_n1 < 0)
1935 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1936 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 if (!quiet)
1939 EMSGN(_(e_listidx), lp->ll_n2);
1940 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001941 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001942 }
1943
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 }
1947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 return p;
1949}
1950
1951/*
1952 * Clear an "lval" that was filled by get_lval().
1953 */
1954 static void
1955clear_lval(lp)
1956 lval *lp;
1957{
1958 vim_free(lp->ll_exp_name);
1959 vim_free(lp->ll_newkey);
1960}
1961
1962/*
1963 * Set a variable that was parsed by get_lval().
1964 * "endp" points to just after the parsed name.
1965 */
1966 static void
1967set_var_lval(lp, endp, rettv, copy)
1968 lval *lp;
1969 char_u *endp;
1970 typeval *rettv;
1971 int copy;
1972{
1973 int cc;
1974 listitem *ni;
1975 listitem *ri;
1976 dictitem *di;
1977
1978 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 cc = *endp;
1983 *endp = NUL;
1984 set_var(lp->ll_name, rettv, copy);
1985 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 }
1988 else if (lp->ll_range)
1989 {
1990 /*
1991 * Assign the List values to the list items.
1992 */
1993 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 clear_tv(&lp->ll_li->li_tv);
1996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1997 ri = ri->li_next;
1998 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1999 break;
2000 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 /* Need to add an empty item. */
2003 ni = listitem_alloc();
2004 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 ri = NULL;
2007 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002008 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 ni->li_tv.v_type = VAR_NUMBER;
2010 ni->li_tv.vval.v_number = 0;
2011 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 lp->ll_li = lp->ll_li->li_next;
2014 ++lp->ll_n1;
2015 }
2016 if (ri != NULL)
2017 EMSG(_("E710: List value has more items than target"));
2018 else if (lp->ll_empty2 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
2019 : lp->ll_n1 != lp->ll_n2)
2020 EMSG(_("E711: List value has not enough items"));
2021 }
2022 else
2023 {
2024 /*
2025 * Assign to a List or Dictionary item.
2026 */
2027 if (lp->ll_newkey != NULL)
2028 {
2029 /* Need to add an item to the Dictionary. */
2030 di = dictitem_alloc();
2031 if (di == NULL)
2032 return;
2033 di->di_key = lp->ll_newkey;
2034 lp->ll_newkey = NULL;
2035 dict_add(lp->ll_tv->vval.v_dict, di);
2036 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002039 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 /*
2042 * Assign the value to the variable or list item.
2043 */
2044 if (copy)
2045 copy_tv(rettv, lp->ll_tv);
2046 else
2047 {
2048 *lp->ll_tv = *rettv;
2049 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 }
2051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052}
2053
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054/*
2055 * Add a watcher to a list.
2056 */
2057 static void
2058list_add_watch(l, lw)
2059 listvar *l;
2060 listwatch *lw;
2061{
2062 lw->lw_next = l->lv_watch;
2063 l->lv_watch = lw;
2064}
2065
2066/*
2067 * Remove a watches from a list.
2068 * No warning when it isn't found...
2069 */
2070 static void
2071list_rem_watch(l, lwrem)
2072 listvar *l;
2073 listwatch *lwrem;
2074{
2075 listwatch *lw, **lwp;
2076
2077 lwp = &l->lv_watch;
2078 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2079 {
2080 if (lw == lwrem)
2081 {
2082 *lwp = lw->lw_next;
2083 break;
2084 }
2085 lwp = &lw->lw_next;
2086 }
2087}
2088
2089/*
2090 * Just before removing an item from a list: advance watchers to the next
2091 * item.
2092 */
2093 static void
2094list_fix_watch(l, item)
2095 listvar *l;
2096 listitem *item;
2097{
2098 listwatch *lw;
2099
2100 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2101 if (lw->lw_item == item)
2102 lw->lw_item = item->li_next;
2103}
2104
2105/*
2106 * Evaluate the expression used in a ":for var in expr" command.
2107 * "arg" points to "var".
2108 * Set "*errp" to TRUE for an error, FALSE otherwise;
2109 * Return a pointer that holds the info. Null when there is an error.
2110 */
2111 void *
2112eval_for_line(arg, errp, nextcmdp, skip)
2113 char_u *arg;
2114 int *errp;
2115 char_u **nextcmdp;
2116 int skip;
2117{
2118 forinfo *fi;
2119 char_u *expr;
2120 typeval tv;
2121 listvar *l;
2122
2123 *errp = TRUE; /* default: there is an error */
2124
2125 fi = (forinfo *)alloc_clear(sizeof(forinfo));
2126 if (fi == NULL)
2127 return NULL;
2128
2129 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2130 if (expr == NULL)
2131 return fi;
2132
2133 expr = skipwhite(expr);
2134 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2135 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002136 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137 return fi;
2138 }
2139
2140 if (skip)
2141 ++emsg_skip;
2142 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2143 {
2144 *errp = FALSE;
2145 if (!skip)
2146 {
2147 l = tv.vval.v_list;
2148 if (tv.v_type != VAR_LIST || l == NULL)
2149 EMSG(_(e_listreq));
2150 else
2151 {
2152 fi->fi_list = l;
2153 list_add_watch(l, &fi->fi_lw);
2154 fi->fi_lw.lw_item = l->lv_first;
2155 }
2156 }
2157 }
2158 if (skip)
2159 --emsg_skip;
2160
2161 return fi;
2162}
2163
2164/*
2165 * Use the first item in a ":for" list. Advance to the next.
2166 * Assign the values to the variable (list). "arg" points to the first one.
2167 * Return TRUE when a valid item was found, FALSE when at end of list or
2168 * something wrong.
2169 */
2170 int
2171next_for_item(fi_void, arg)
2172 void *fi_void;
2173 char_u *arg;
2174{
2175 forinfo *fi = (forinfo *)fi_void;
2176 int result;
2177 listitem *item;
2178
2179 item = fi->fi_lw.lw_item;
2180 if (item == NULL)
2181 result = FALSE;
2182 else
2183 {
2184 fi->fi_lw.lw_item = item->li_next;
2185 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2186 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2187 }
2188 return result;
2189}
2190
2191/*
2192 * Free the structure used to store info used by ":for".
2193 */
2194 void
2195free_for_info(fi_void)
2196 void *fi_void;
2197{
2198 forinfo *fi = (forinfo *)fi_void;
2199
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002200 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002201 list_rem_watch(fi->fi_list, &fi->fi_lw);
2202 vim_free(fi);
2203}
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2206
2207 void
2208set_context_for_expression(xp, arg, cmdidx)
2209 expand_T *xp;
2210 char_u *arg;
2211 cmdidx_T cmdidx;
2212{
2213 int got_eq = FALSE;
2214 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002215 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002217 if (cmdidx == CMD_let)
2218 {
2219 xp->xp_context = EXPAND_USER_VARS;
2220 if (vim_strchr(arg, '=') == NULL)
2221 {
2222 /* ":let var1 var2 ...": find last space. */
2223 for (p = arg + STRLEN(arg); p > arg; )
2224 {
2225 xp->xp_pattern = p;
2226 p = mb_ptr_back(arg, p);
2227 if (vim_iswhite(*p))
2228 break;
2229 }
2230 return;
2231 }
2232 }
2233 else
2234 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2235 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 while ((xp->xp_pattern = vim_strpbrk(arg,
2237 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2238 {
2239 c = *xp->xp_pattern;
2240 if (c == '&')
2241 {
2242 c = xp->xp_pattern[1];
2243 if (c == '&')
2244 {
2245 ++xp->xp_pattern;
2246 xp->xp_context = cmdidx != CMD_let || got_eq
2247 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2248 }
2249 else if (c != ' ')
2250 xp->xp_context = EXPAND_SETTINGS;
2251 }
2252 else if (c == '$')
2253 {
2254 /* environment variable */
2255 xp->xp_context = EXPAND_ENV_VARS;
2256 }
2257 else if (c == '=')
2258 {
2259 got_eq = TRUE;
2260 xp->xp_context = EXPAND_EXPRESSION;
2261 }
2262 else if (c == '<'
2263 && xp->xp_context == EXPAND_FUNCTIONS
2264 && vim_strchr(xp->xp_pattern, '(') == NULL)
2265 {
2266 /* Function name can start with "<SNR>" */
2267 break;
2268 }
2269 else if (cmdidx != CMD_let || got_eq)
2270 {
2271 if (c == '"') /* string */
2272 {
2273 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2274 if (c == '\\' && xp->xp_pattern[1] != NUL)
2275 ++xp->xp_pattern;
2276 xp->xp_context = EXPAND_NOTHING;
2277 }
2278 else if (c == '\'') /* literal string */
2279 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2282 /* skip */ ;
2283 xp->xp_context = EXPAND_NOTHING;
2284 }
2285 else if (c == '|')
2286 {
2287 if (xp->xp_pattern[1] == '|')
2288 {
2289 ++xp->xp_pattern;
2290 xp->xp_context = EXPAND_EXPRESSION;
2291 }
2292 else
2293 xp->xp_context = EXPAND_COMMANDS;
2294 }
2295 else
2296 xp->xp_context = EXPAND_EXPRESSION;
2297 }
2298 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002299 /* Doesn't look like something valid, expand as an expression
2300 * anyway. */
2301 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 arg = xp->xp_pattern;
2303 if (*arg != NUL)
2304 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2305 /* skip */ ;
2306 }
2307 xp->xp_pattern = arg;
2308}
2309
2310#endif /* FEAT_CMDL_COMPL */
2311
2312/*
2313 * ":1,25call func(arg1, arg2)" function call.
2314 */
2315 void
2316ex_call(eap)
2317 exarg_T *eap;
2318{
2319 char_u *arg = eap->arg;
2320 char_u *startarg;
2321 char_u *alias;
2322 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int len;
2325 linenr_T lnum;
2326 int doesrange;
2327 int failed = FALSE;
2328
2329 name = arg;
2330 len = get_func_len(&arg, &alias, !eap->skip);
2331 if (len == 0)
2332 goto end;
2333 if (alias != NULL)
2334 name = alias;
2335
2336 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 if (*startarg != '(')
2340 {
2341 EMSG2(_("E107: Missing braces: %s"), name);
2342 goto end;
2343 }
2344
2345 /*
2346 * When skipping, evaluate the function once, to find the end of the
2347 * arguments.
2348 * When the function takes a range, this is discovered after the first
2349 * call, and the loop is broken.
2350 */
2351 if (eap->skip)
2352 {
2353 ++emsg_skip;
2354 lnum = eap->line2; /* do it once, also with an invalid range */
2355 }
2356 else
2357 lnum = eap->line1;
2358 for ( ; lnum <= eap->line2; ++lnum)
2359 {
2360 if (!eap->skip && eap->addr_count > 0)
2361 {
2362 curwin->w_cursor.lnum = lnum;
2363 curwin->w_cursor.col = 0;
2364 }
2365 arg = startarg;
Bram Moolenaare9a41262005-01-15 22:18:47 +00002366 if (get_func_tv(name, len, &rettv, &arg, eap->line1, eap->line2,
2367 &doesrange, !eap->skip, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
2369 failed = TRUE;
2370 break;
2371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (doesrange || eap->skip)
2374 break;
2375 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002376 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002378 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (aborting())
2380 break;
2381 }
2382 if (eap->skip)
2383 --emsg_skip;
2384
2385 if (!failed)
2386 {
2387 /* Check for trailing illegal characters and a following command. */
2388 if (!ends_excmd(*arg))
2389 {
2390 emsg_severe = TRUE;
2391 EMSG(_(e_trailing));
2392 }
2393 else
2394 eap->nextcmd = check_nextcmd(arg);
2395 }
2396
2397end:
2398 if (alias != NULL)
2399 vim_free(alias);
2400}
2401
2402/*
2403 * ":unlet[!] var1 ... " command.
2404 */
2405 void
2406ex_unlet(eap)
2407 exarg_T *eap;
2408{
2409 char_u *arg = eap->arg;
2410 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 int error = FALSE;
2412
2413 do
2414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 lval lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 /* Parse the name and find the end. */
2418 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2419 if (lv.ll_name == NULL)
2420 error = TRUE; /* error but continue parsing */
2421 if (name_end == NULL || (!vim_iswhite(*name_end)
2422 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (name_end != NULL)
2425 {
2426 emsg_severe = TRUE;
2427 EMSG(_(e_trailing));
2428 }
2429 if (!(eap->skip || error))
2430 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 break;
2432 }
2433
2434 if (!error && !eap->skip)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2436 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (!eap->skip)
2439 clear_lval(&lv);
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 arg = skipwhite(name_end);
2442 } while (!ends_excmd(*arg));
2443
2444 eap->nextcmd = check_nextcmd(arg);
2445}
2446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449do_unlet_var(lp, name_end, forceit)
2450 lval *lp;
2451 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 int forceit;
2453{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 int ret = OK;
2455 int cc;
2456
2457 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 cc = *name_end;
2460 *name_end = NUL;
2461
2462 /* Normal name or expanded name. */
2463 if (check_changedtick(lp->ll_name))
2464 ret = FAIL;
2465 else if (do_unlet(lp->ll_name) == FAIL && !forceit)
2466 {
2467 EMSG2(_("E108: No such variable: \"%s\""), lp->ll_name);
2468 ret = FAIL;
2469 }
2470 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 else if (lp->ll_range)
2473 {
2474 listitem *li;
2475
2476 /* Delete a range of List items. */
2477 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2478 {
2479 li = lp->ll_li->li_next;
2480 listitem_remove(lp->ll_list, lp->ll_li);
2481 lp->ll_li = li;
2482 ++lp->ll_n1;
2483 }
2484 }
2485 else
2486 {
2487 clear_tv(lp->ll_tv);
2488 if (lp->ll_list != NULL)
2489 {
2490 /* unlet a List item. */
2491 listitem_remove(lp->ll_list, lp->ll_li);
2492 }
2493 else
2494 {
2495 /* unlet a Dictionary item. */
2496 *lp->ll_pdi = lp->ll_di->di_next;
2497 dictitem_free(lp->ll_di);
2498 }
2499 }
2500
2501 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502}
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504/*
2505 * "unlet" a variable. Return OK if it existed, FAIL if not.
2506 */
2507 int
2508do_unlet(name)
2509 char_u *name;
2510{
2511 VAR v;
2512
2513 v = find_var(name, TRUE);
2514 if (v != NULL)
2515 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002516 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 return OK;
2518 }
2519 return FAIL;
2520}
2521
2522#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2523/*
2524 * Delete all "menutrans_" variables.
2525 */
2526 void
2527del_menutrans_vars()
2528{
2529 int i;
2530
2531 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002532 if (VAR_ENTRY(i).v_name != NULL
2533 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2534 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535}
2536#endif
2537
2538#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2539
2540/*
2541 * Local string buffer for the next two functions to store a variable name
2542 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2543 * get_user_var_name().
2544 */
2545
2546static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2547
2548static char_u *varnamebuf = NULL;
2549static int varnamebuflen = 0;
2550
2551/*
2552 * Function to concatenate a prefix and a variable name.
2553 */
2554 static char_u *
2555cat_prefix_varname(prefix, name)
2556 int prefix;
2557 char_u *name;
2558{
2559 int len;
2560
2561 len = (int)STRLEN(name) + 3;
2562 if (len > varnamebuflen)
2563 {
2564 vim_free(varnamebuf);
2565 len += 10; /* some additional space */
2566 varnamebuf = alloc(len);
2567 if (varnamebuf == NULL)
2568 {
2569 varnamebuflen = 0;
2570 return NULL;
2571 }
2572 varnamebuflen = len;
2573 }
2574 *varnamebuf = prefix;
2575 varnamebuf[1] = ':';
2576 STRCPY(varnamebuf + 2, name);
2577 return varnamebuf;
2578}
2579
2580/*
2581 * Function given to ExpandGeneric() to obtain the list of user defined
2582 * (global/buffer/window/built-in) variable names.
2583 */
2584/*ARGSUSED*/
2585 char_u *
2586get_user_var_name(xp, idx)
2587 expand_T *xp;
2588 int idx;
2589{
2590 static int gidx;
2591 static int bidx;
2592 static int widx;
2593 static int vidx;
2594 char_u *name;
2595
2596 if (idx == 0)
2597 gidx = bidx = widx = vidx = 0;
2598 if (gidx < variables.ga_len) /* Global variables */
2599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002600 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 && gidx < variables.ga_len)
2602 /* skip */;
2603 if (name != NULL)
2604 {
2605 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2606 return cat_prefix_varname('g', name);
2607 else
2608 return name;
2609 }
2610 }
2611 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002613 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 && bidx < curbuf->b_vars.ga_len)
2615 /* skip */;
2616 if (name != NULL)
2617 return cat_prefix_varname('b', name);
2618 }
2619 if (bidx == curbuf->b_vars.ga_len)
2620 {
2621 ++bidx;
2622 return (char_u *)"b:changedtick";
2623 }
2624 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002626 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 && widx < curwin->w_vars.ga_len)
2628 /* skip */;
2629 if (name != NULL)
2630 return cat_prefix_varname('w', name);
2631 }
2632 if (vidx < VV_LEN) /* Built-in variables */
2633 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2634
2635 vim_free(varnamebuf);
2636 varnamebuf = NULL;
2637 varnamebuflen = 0;
2638 return NULL;
2639}
2640
2641#endif /* FEAT_CMDL_COMPL */
2642
2643/*
2644 * types for expressions.
2645 */
2646typedef enum
2647{
2648 TYPE_UNKNOWN = 0
2649 , TYPE_EQUAL /* == */
2650 , TYPE_NEQUAL /* != */
2651 , TYPE_GREATER /* > */
2652 , TYPE_GEQUAL /* >= */
2653 , TYPE_SMALLER /* < */
2654 , TYPE_SEQUAL /* <= */
2655 , TYPE_MATCH /* =~ */
2656 , TYPE_NOMATCH /* !~ */
2657} exptype_T;
2658
2659/*
2660 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2663 */
2664
2665/*
2666 * Handle zero level expression.
2667 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 * Return OK or FAIL.
2670 */
2671 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 char_u **nextcmd;
2676 int evaluate;
2677{
2678 int ret;
2679 char_u *p;
2680
2681 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (ret == FAIL || !ends_excmd(*p))
2684 {
2685 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /*
2688 * Report the invalid expression unless the expression evaluation has
2689 * been cancelled due to an aborting error, an interrupt, or an
2690 * exception.
2691 */
2692 if (!aborting())
2693 EMSG2(_(e_invexpr2), arg);
2694 ret = FAIL;
2695 }
2696 if (nextcmd != NULL)
2697 *nextcmd = check_nextcmd(p);
2698
2699 return ret;
2700}
2701
2702/*
2703 * Handle top level expression:
2704 * expr1 ? expr0 : expr0
2705 *
2706 * "arg" must point to the first non-white of the expression.
2707 * "arg" is advanced to the next non-white after the recognized expression.
2708 *
2709 * Return OK or FAIL.
2710 */
2711 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002712eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int evaluate;
2716{
2717 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002718 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * Get the first variable.
2722 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002723 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return FAIL;
2725
2726 if ((*arg)[0] == '?')
2727 {
2728 result = FALSE;
2729 if (evaluate)
2730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002731 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002733 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735
2736 /*
2737 * Get the second variable.
2738 */
2739 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002740 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 return FAIL;
2742
2743 /*
2744 * Check for the ":".
2745 */
2746 if ((*arg)[0] != ':')
2747 {
2748 EMSG(_("E109: Missing ':' after '?'"));
2749 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 return FAIL;
2752 }
2753
2754 /*
2755 * Get the third variable.
2756 */
2757 *arg = skipwhite(*arg + 1);
2758 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2759 {
2760 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002761 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 return FAIL;
2763 }
2764 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002765 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767
2768 return OK;
2769}
2770
2771/*
2772 * Handle first level expression:
2773 * expr2 || expr2 || expr2 logical OR
2774 *
2775 * "arg" must point to the first non-white of the expression.
2776 * "arg" is advanced to the next non-white after the recognized expression.
2777 *
2778 * Return OK or FAIL.
2779 */
2780 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002783 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 int evaluate;
2785{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002786 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 long result;
2788 int first;
2789
2790 /*
2791 * Get the first variable.
2792 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002793 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return FAIL;
2795
2796 /*
2797 * Repeat until there is no following "||".
2798 */
2799 first = TRUE;
2800 result = FALSE;
2801 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2802 {
2803 if (evaluate && first)
2804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002805 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 first = FALSE;
2809 }
2810
2811 /*
2812 * Get the second variable.
2813 */
2814 *arg = skipwhite(*arg + 2);
2815 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2816 return FAIL;
2817
2818 /*
2819 * Compute the result.
2820 */
2821 if (evaluate && !result)
2822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002823 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002825 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827 if (evaluate)
2828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002829 rettv->v_type = VAR_NUMBER;
2830 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832 }
2833
2834 return OK;
2835}
2836
2837/*
2838 * Handle second level expression:
2839 * expr3 && expr3 && expr3 logical AND
2840 *
2841 * "arg" must point to the first non-white of the expression.
2842 * "arg" is advanced to the next non-white after the recognized expression.
2843 *
2844 * Return OK or FAIL.
2845 */
2846 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002847eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002849 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 int evaluate;
2851{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002852 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 long result;
2854 int first;
2855
2856 /*
2857 * Get the first variable.
2858 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 return FAIL;
2861
2862 /*
2863 * Repeat until there is no following "&&".
2864 */
2865 first = TRUE;
2866 result = TRUE;
2867 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2868 {
2869 if (evaluate && first)
2870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002873 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 first = FALSE;
2875 }
2876
2877 /*
2878 * Get the second variable.
2879 */
2880 *arg = skipwhite(*arg + 2);
2881 if (eval4(arg, &var2, evaluate && result) == FAIL)
2882 return FAIL;
2883
2884 /*
2885 * Compute the result.
2886 */
2887 if (evaluate && result)
2888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893 if (evaluate)
2894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002895 rettv->v_type = VAR_NUMBER;
2896 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
2899
2900 return OK;
2901}
2902
2903/*
2904 * Handle third level expression:
2905 * var1 == var2
2906 * var1 =~ var2
2907 * var1 != var2
2908 * var1 !~ var2
2909 * var1 > var2
2910 * var1 >= var2
2911 * var1 < var2
2912 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002913 * var1 is var2
2914 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 *
2916 * "arg" must point to the first non-white of the expression.
2917 * "arg" is advanced to the next non-white after the recognized expression.
2918 *
2919 * Return OK or FAIL.
2920 */
2921 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 int evaluate;
2926{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002927 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 char_u *p;
2929 int i;
2930 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002931 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 int len = 2;
2933 long n1, n2;
2934 char_u *s1, *s2;
2935 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2936 regmatch_T regmatch;
2937 int ic;
2938 char_u *save_cpo;
2939
2940 /*
2941 * Get the first variable.
2942 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 return FAIL;
2945
2946 p = *arg;
2947 switch (p[0])
2948 {
2949 case '=': if (p[1] == '=')
2950 type = TYPE_EQUAL;
2951 else if (p[1] == '~')
2952 type = TYPE_MATCH;
2953 break;
2954 case '!': if (p[1] == '=')
2955 type = TYPE_NEQUAL;
2956 else if (p[1] == '~')
2957 type = TYPE_NOMATCH;
2958 break;
2959 case '>': if (p[1] != '=')
2960 {
2961 type = TYPE_GREATER;
2962 len = 1;
2963 }
2964 else
2965 type = TYPE_GEQUAL;
2966 break;
2967 case '<': if (p[1] != '=')
2968 {
2969 type = TYPE_SMALLER;
2970 len = 1;
2971 }
2972 else
2973 type = TYPE_SEQUAL;
2974 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002975 case 'i': if (p[1] == 's')
2976 {
2977 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2978 len = 5;
2979 if (!vim_isIDc(p[len]))
2980 {
2981 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2982 type_is = TRUE;
2983 }
2984 }
2985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
2987
2988 /*
2989 * If there is a comparitive operator, use it.
2990 */
2991 if (type != TYPE_UNKNOWN)
2992 {
2993 /* extra question mark appended: ignore case */
2994 if (p[len] == '?')
2995 {
2996 ic = TRUE;
2997 ++len;
2998 }
2999 /* extra '#' appended: match case */
3000 else if (p[len] == '#')
3001 {
3002 ic = FALSE;
3003 ++len;
3004 }
3005 /* nothing appened: use 'ignorecase' */
3006 else
3007 ic = p_ic;
3008
3009 /*
3010 * Get the second variable.
3011 */
3012 *arg = skipwhite(p + len);
3013 if (eval5(arg, &var2, evaluate) == FAIL)
3014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003015 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 return FAIL;
3017 }
3018
3019 if (evaluate)
3020 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003021 if (type_is && rettv->v_type != var2.v_type)
3022 {
3023 /* For "is" a different type always means FALSE, for "notis"
3024 * it means TRUE. */
3025 n1 = (type == TYPE_NEQUAL);
3026 }
3027 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3028 {
3029 if (type_is)
3030 {
3031 n1 = (rettv->v_type == var2.v_type
3032 && rettv->vval.v_list == var2.vval.v_list);
3033 if (type == TYPE_NEQUAL)
3034 n1 = !n1;
3035 }
3036 else if (rettv->v_type != var2.v_type
3037 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3038 {
3039 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003040 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003041 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003042 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003043 clear_tv(rettv);
3044 clear_tv(&var2);
3045 return FAIL;
3046 }
3047 else
3048 {
3049 /* Compare two Lists for being equal or unequal. */
3050 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3051 if (type == TYPE_NEQUAL)
3052 n1 = !n1;
3053 }
3054 }
3055
3056 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3057 {
3058 if (rettv->v_type != var2.v_type
3059 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3060 {
3061 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003062 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003063 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003064 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003065 clear_tv(rettv);
3066 clear_tv(&var2);
3067 return FAIL;
3068 }
3069 else
3070 {
3071 /* Compare two Funcrefs for being equal or unequal. */
3072 if (rettv->vval.v_string == NULL
3073 || var2.vval.v_string == NULL)
3074 n1 = FALSE;
3075 else
3076 n1 = STRCMP(rettv->vval.v_string,
3077 var2.vval.v_string) == 0;
3078 if (type == TYPE_NEQUAL)
3079 n1 = !n1;
3080 }
3081 }
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 /*
3084 * If one of the two variables is a number, compare as a number.
3085 * When using "=~" or "!~", always compare as string.
3086 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003087 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003090 n1 = get_tv_number(rettv);
3091 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 switch (type)
3093 {
3094 case TYPE_EQUAL: n1 = (n1 == n2); break;
3095 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3096 case TYPE_GREATER: n1 = (n1 > n2); break;
3097 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3098 case TYPE_SMALLER: n1 = (n1 < n2); break;
3099 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3100 case TYPE_UNKNOWN:
3101 case TYPE_MATCH:
3102 case TYPE_NOMATCH: break; /* avoid gcc warning */
3103 }
3104 }
3105 else
3106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 s1 = get_tv_string_buf(rettv, buf1);
3108 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3110 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3111 else
3112 i = 0;
3113 n1 = FALSE;
3114 switch (type)
3115 {
3116 case TYPE_EQUAL: n1 = (i == 0); break;
3117 case TYPE_NEQUAL: n1 = (i != 0); break;
3118 case TYPE_GREATER: n1 = (i > 0); break;
3119 case TYPE_GEQUAL: n1 = (i >= 0); break;
3120 case TYPE_SMALLER: n1 = (i < 0); break;
3121 case TYPE_SEQUAL: n1 = (i <= 0); break;
3122
3123 case TYPE_MATCH:
3124 case TYPE_NOMATCH:
3125 /* avoid 'l' flag in 'cpoptions' */
3126 save_cpo = p_cpo;
3127 p_cpo = (char_u *)"";
3128 regmatch.regprog = vim_regcomp(s2,
3129 RE_MAGIC + RE_STRING);
3130 regmatch.rm_ic = ic;
3131 if (regmatch.regprog != NULL)
3132 {
3133 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3134 vim_free(regmatch.regprog);
3135 if (type == TYPE_NOMATCH)
3136 n1 = !n1;
3137 }
3138 p_cpo = save_cpo;
3139 break;
3140
3141 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3142 }
3143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003144 clear_tv(rettv);
3145 clear_tv(&var2);
3146 rettv->v_type = VAR_NUMBER;
3147 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149 }
3150
3151 return OK;
3152}
3153
3154/*
3155 * Handle fourth level expression:
3156 * + number addition
3157 * - number subtraction
3158 * . string concatenation
3159 *
3160 * "arg" must point to the first non-white of the expression.
3161 * "arg" is advanced to the next non-white after the recognized expression.
3162 *
3163 * Return OK or FAIL.
3164 */
3165 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003166eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003168 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 int evaluate;
3170{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003171 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003172 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int op;
3174 long n1, n2;
3175 char_u *s1, *s2;
3176 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3177 char_u *p;
3178
3179 /*
3180 * Get the first variable.
3181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003182 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 return FAIL;
3184
3185 /*
3186 * Repeat computing, until no '+', '-' or '.' is following.
3187 */
3188 for (;;)
3189 {
3190 op = **arg;
3191 if (op != '+' && op != '-' && op != '.')
3192 break;
3193
3194 /*
3195 * Get the second variable.
3196 */
3197 *arg = skipwhite(*arg + 1);
3198 if (eval6(arg, &var2, evaluate) == FAIL)
3199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 return FAIL;
3202 }
3203
3204 if (evaluate)
3205 {
3206 /*
3207 * Compute the result.
3208 */
3209 if (op == '.')
3210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003211 s1 = get_tv_string_buf(rettv, buf1);
3212 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 op = (int)STRLEN(s1);
3214 p = alloc((unsigned)(op + STRLEN(s2) + 1));
3215 if (p != NULL)
3216 {
3217 STRCPY(p, s1);
3218 STRCPY(p + op, s2);
3219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003220 clear_tv(rettv);
3221 rettv->v_type = VAR_STRING;
3222 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003224 else if (op == '+' && rettv->v_type == VAR_LIST
3225 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003226 {
3227 /* concatenate Lists */
3228 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3229 &var3) == FAIL)
3230 {
3231 clear_tv(rettv);
3232 clear_tv(&var2);
3233 return FAIL;
3234 }
3235 clear_tv(rettv);
3236 *rettv = var3;
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else
3239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 n1 = get_tv_number(rettv);
3241 n2 = get_tv_number(&var2);
3242 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (op == '+')
3244 n1 = n1 + n2;
3245 else
3246 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003247 rettv->v_type = VAR_NUMBER;
3248 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003250 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
3252 }
3253 return OK;
3254}
3255
3256/*
3257 * Handle fifth level expression:
3258 * * number multiplication
3259 * / number division
3260 * % number modulo
3261 *
3262 * "arg" must point to the first non-white of the expression.
3263 * "arg" is advanced to the next non-white after the recognized expression.
3264 *
3265 * Return OK or FAIL.
3266 */
3267 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003268eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003270 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 int evaluate;
3272{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003273 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 int op;
3275 long n1, n2;
3276
3277 /*
3278 * Get the first variable.
3279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 return FAIL;
3282
3283 /*
3284 * Repeat computing, until no '*', '/' or '%' is following.
3285 */
3286 for (;;)
3287 {
3288 op = **arg;
3289 if (op != '*' && op != '/' && op != '%')
3290 break;
3291
3292 if (evaluate)
3293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294 n1 = get_tv_number(rettv);
3295 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 else
3298 n1 = 0;
3299
3300 /*
3301 * Get the second variable.
3302 */
3303 *arg = skipwhite(*arg + 1);
3304 if (eval7(arg, &var2, evaluate) == FAIL)
3305 return FAIL;
3306
3307 if (evaluate)
3308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003309 n2 = get_tv_number(&var2);
3310 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
3312 /*
3313 * Compute the result.
3314 */
3315 if (op == '*')
3316 n1 = n1 * n2;
3317 else if (op == '/')
3318 {
3319 if (n2 == 0) /* give an error message? */
3320 n1 = 0x7fffffffL;
3321 else
3322 n1 = n1 / n2;
3323 }
3324 else
3325 {
3326 if (n2 == 0) /* give an error message? */
3327 n1 = 0;
3328 else
3329 n1 = n1 % n2;
3330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 rettv->v_type = VAR_NUMBER;
3332 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 }
3335
3336 return OK;
3337}
3338
3339/*
3340 * Handle sixth level expression:
3341 * number number constant
3342 * "string" string contstant
3343 * 'string' literal string contstant
3344 * &option-name option value
3345 * @r register contents
3346 * identifier variable value
3347 * function() function call
3348 * $VAR environment variable
3349 * (expression) nested expression
3350 *
3351 * Also handle:
3352 * ! in front logical NOT
3353 * - in front unary minus
3354 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 * trailing [] subscript in String or List
3356 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 *
3358 * "arg" must point to the first non-white of the expression.
3359 * "arg" is advanced to the next non-white after the recognized expression.
3360 *
3361 * Return OK or FAIL.
3362 */
3363 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003364eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 int evaluate;
3368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 long n;
3370 int len;
3371 char_u *s;
3372 int val;
3373 char_u *start_leader, *end_leader;
3374 int ret = OK;
3375 char_u *alias;
Bram Moolenaare9a41262005-01-15 22:18:47 +00003376 dictvar *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003379 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003380 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 /*
3385 * Skip '!' and '-' characters. They are handled later.
3386 */
3387 start_leader = *arg;
3388 while (**arg == '!' || **arg == '-' || **arg == '+')
3389 *arg = skipwhite(*arg + 1);
3390 end_leader = *arg;
3391
3392 switch (**arg)
3393 {
3394 /*
3395 * Number constant.
3396 */
3397 case '0':
3398 case '1':
3399 case '2':
3400 case '3':
3401 case '4':
3402 case '5':
3403 case '6':
3404 case '7':
3405 case '8':
3406 case '9':
3407 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3408 *arg += len;
3409 if (evaluate)
3410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003411 rettv->v_type = VAR_NUMBER;
3412 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 break;
3415
3416 /*
3417 * String constant: "string".
3418 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 break;
3421
3422 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003426 break;
3427
3428 /*
3429 * List: [expr, expr]
3430 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 break;
3433
3434 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003435 * Dictionary: {key: val, key: val}
3436 */
3437 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3438 break;
3439
3440 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003441 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003443 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445
3446 /*
3447 * Environment variable: $VAR.
3448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 break;
3451
3452 /*
3453 * Register contents: @r.
3454 */
3455 case '@': ++*arg;
3456 if (evaluate)
3457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003458 rettv->v_type = VAR_STRING;
3459 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461 if (**arg != NUL)
3462 ++*arg;
3463 break;
3464
3465 /*
3466 * nested expression: (expression).
3467 */
3468 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003469 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (**arg == ')')
3471 ++*arg;
3472 else if (ret == OK)
3473 {
3474 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 ret = FAIL;
3477 }
3478 break;
3479
Bram Moolenaar8c711452005-01-14 21:53:12 +00003480 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 break;
3482 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003483
3484 if (ret == NOTDONE)
3485 {
3486 /*
3487 * Must be a variable or function name.
3488 * Can also be a curly-braces kind of name: {expr}.
3489 */
3490 s = *arg;
3491 len = get_func_len(arg, &alias, evaluate);
3492 if (alias != NULL)
3493 s = alias;
3494
3495 if (len == 0)
3496 ret = FAIL;
3497 else
3498 {
3499 if (**arg == '(') /* recursive! */
3500 {
3501 /* If "s" is the name of a variable of type VAR_FUNC
3502 * use its contents. */
3503 s = deref_func_name(s, &len);
3504
3505 /* Invoke the function. */
3506 ret = get_func_tv(s, len, rettv, arg,
3507 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003508 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003509 /* Stop the expression evaluation when immediately
3510 * aborting on error, or when an interrupt occurred or
3511 * an exception was thrown but not caught. */
3512 if (aborting())
3513 {
3514 if (ret == OK)
3515 clear_tv(rettv);
3516 ret = FAIL;
3517 }
3518 }
3519 else if (evaluate)
3520 ret = get_var_tv(s, len, rettv);
3521 }
3522
3523 if (alias != NULL)
3524 vim_free(alias);
3525 }
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 *arg = skipwhite(*arg);
3528
3529 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003530 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00003531 * Also handle function call with Funcref variable: func(expr)
3532 * Can all be combined: dict.func(expr)[idx].func(expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003534 selfdict = NULL;
3535 while (ret == OK
3536 && (**arg == '['
3537 || (**arg == '.' && rettv->v_type == VAR_DICT)
3538 || (**arg == '(' && rettv->v_type == VAR_FUNC))
3539 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003541 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003543 s = rettv->vval.v_string;
3544
3545 /* Invoke the function. Recursive! */
3546 ret = get_func_tv(s, STRLEN(s), rettv, arg,
3547 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3548 &len, evaluate, selfdict);
3549
3550 /* Stop the expression evaluation when immediately
3551 * aborting on error, or when an interrupt occurred or
3552 * an exception was thrown but not caught. */
3553 if (aborting())
3554 {
3555 if (ret == OK)
3556 clear_tv(rettv);
3557 ret = FAIL;
3558 }
3559 selfdict = NULL;
3560 }
3561 else
3562 {
3563 if (rettv->v_type == VAR_DICT)
3564 selfdict = rettv->vval.v_dict;
3565 else
3566 selfdict = NULL;
3567 if (eval_index(arg, rettv, evaluate) == FAIL)
3568 {
3569 clear_tv(rettv);
3570 ret = FAIL;
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574
3575 /*
3576 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3577 */
3578 if (ret == OK && evaluate && end_leader > start_leader)
3579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 while (end_leader > start_leader)
3582 {
3583 --end_leader;
3584 if (*end_leader == '!')
3585 val = !val;
3586 else if (*end_leader == '-')
3587 val = -val;
3588 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 clear_tv(rettv);
3590 rettv->v_type = VAR_NUMBER;
3591 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593
3594 return ret;
3595}
3596
3597/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * Evaluate an "[expr]" or "[expr:expr]" index.
3599 * "*arg" points to the '['.
3600 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3601 */
3602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003603eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003604 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003605 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003606 int evaluate;
3607{
3608 int empty1 = FALSE, empty2 = FALSE;
3609 typeval var1, var2;
3610 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 long len = -1;
3612 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003613 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003618 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003619 return FAIL;
3620 }
3621
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624 /*
3625 * dict.name
3626 */
3627 key = *arg + 1;
3628 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3629 ;
3630 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003631 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003633 }
3634 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 /*
3637 * something[idx]
3638 *
3639 * Get the (first) variable from inside the [].
3640 */
3641 *arg = skipwhite(*arg + 1);
3642 if (**arg == ':')
3643 empty1 = TRUE;
3644 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3645 return FAIL;
3646
3647 /*
3648 * Get the second variable from inside the [:].
3649 */
3650 if (**arg == ':')
3651 {
3652 range = TRUE;
3653 *arg = skipwhite(*arg + 1);
3654 if (**arg == ']')
3655 empty2 = TRUE;
3656 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3657 {
3658 clear_tv(&var1);
3659 return FAIL;
3660 }
3661 }
3662
3663 /* Check for the ']'. */
3664 if (**arg != ']')
3665 {
3666 EMSG(_(e_missbrac));
3667 clear_tv(&var1);
3668 if (range)
3669 clear_tv(&var2);
3670 return FAIL;
3671 }
3672 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003673 }
3674
3675 if (evaluate)
3676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 n1 = 0;
3678 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003680 n1 = get_tv_number(&var1);
3681 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003682 }
3683 if (range)
3684 {
3685 if (empty2)
3686 n2 = -1;
3687 else
3688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003689 n2 = get_tv_number(&var2);
3690 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003691 }
3692 }
3693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003695 {
3696 case VAR_NUMBER:
3697 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003698 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003699 len = (long)STRLEN(s);
3700 if (range)
3701 {
3702 /* The resulting variable is a substring. If the indexes
3703 * are out of range the result is empty. */
3704 if (n1 < 0)
3705 {
3706 n1 = len + n1;
3707 if (n1 < 0)
3708 n1 = 0;
3709 }
3710 if (n2 < 0)
3711 n2 = len + n2;
3712 else if (n2 >= len)
3713 n2 = len;
3714 if (n1 >= len || n2 < 0 || n1 > n2)
3715 s = NULL;
3716 else
3717 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3718 }
3719 else
3720 {
3721 /* The resulting variable is a string of a single
3722 * character. If the index is too big or negative the
3723 * result is empty. */
3724 if (n1 >= len || n1 < 0)
3725 s = NULL;
3726 else
3727 s = vim_strnsave(s + n1, 1);
3728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 clear_tv(rettv);
3730 rettv->v_type = VAR_STRING;
3731 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003732 break;
3733
3734 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003735 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003736 if (n1 < 0)
3737 n1 = len + n1;
3738 if (!empty1 && (n1 < 0 || n1 >= len))
3739 {
3740 EMSGN(_(e_listidx), n1);
3741 return FAIL;
3742 }
3743 if (range)
3744 {
3745 listvar *l;
3746 listitem *item;
3747
3748 if (n2 < 0)
3749 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003750 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003751 {
3752 EMSGN(_(e_listidx), n2);
3753 return FAIL;
3754 }
3755 l = list_alloc();
3756 if (l == NULL)
3757 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003758 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003759 n1 <= n2; ++n1)
3760 {
3761 if (list_append_tv(l, &item->li_tv) == FAIL)
3762 {
3763 list_free(l);
3764 return FAIL;
3765 }
3766 item = item->li_next;
3767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 clear_tv(rettv);
3769 rettv->v_type = VAR_LIST;
3770 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003771 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003772 }
3773 else
3774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003776 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003777 clear_tv(rettv);
3778 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003779 }
3780 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003781
3782 case VAR_DICT:
3783 if (range)
3784 {
3785 EMSG(_("E999: Using range with Dictionary"));
3786 if (len == -1)
3787 clear_tv(&var1);
3788 return FAIL;
3789 }
3790 {
3791 dictitem *item;
3792
3793 if (len == -1)
3794 {
3795 key = get_tv_string(&var1);
3796 if (*key == NUL)
3797 {
3798 EMSG(_("E999: Empty key for Dictionary"));
3799 clear_tv(&var1);
3800 return FAIL;
3801 }
3802 }
3803
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003804 item = dict_find(rettv->vval.v_dict, key, (int)len, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003805
3806 if (item == NULL)
3807 EMSG2(_("E999: Key not found in Dictionary: %s"), key);
3808 if (len == -1)
3809 clear_tv(&var1);
3810 if (item == NULL)
3811 return FAIL;
3812
3813 copy_tv(&item->di_tv, &var1);
3814 clear_tv(rettv);
3815 *rettv = var1;
3816 }
3817 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003818 }
3819 }
3820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003821 return OK;
3822}
3823
3824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 * Get an option value.
3826 * "arg" points to the '&' or '+' before the option name.
3827 * "arg" is advanced to character after the option name.
3828 * Return OK or FAIL.
3829 */
3830 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 int evaluate;
3835{
3836 char_u *option_end;
3837 long numval;
3838 char_u *stringval;
3839 int opt_type;
3840 int c;
3841 int working = (**arg == '+'); /* has("+option") */
3842 int ret = OK;
3843 int opt_flags;
3844
3845 /*
3846 * Isolate the option name and find its value.
3847 */
3848 option_end = find_option_end(arg, &opt_flags);
3849 if (option_end == NULL)
3850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 EMSG2(_("E112: Option name missing: %s"), *arg);
3853 return FAIL;
3854 }
3855
3856 if (!evaluate)
3857 {
3858 *arg = option_end;
3859 return OK;
3860 }
3861
3862 c = *option_end;
3863 *option_end = NUL;
3864 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867 if (opt_type == -3) /* invalid name */
3868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003869 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 EMSG2(_("E113: Unknown option: %s"), *arg);
3871 ret = FAIL;
3872 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003873 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 if (opt_type == -2) /* hidden string option */
3876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 rettv->v_type = VAR_STRING;
3878 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 else if (opt_type == -1) /* hidden number option */
3881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 rettv->v_type = VAR_NUMBER;
3883 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 else if (opt_type == 1) /* number option */
3886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 rettv->v_type = VAR_NUMBER;
3888 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 else /* string option */
3891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003892 rettv->v_type = VAR_STRING;
3893 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 }
3896 else if (working && (opt_type == -2 || opt_type == -1))
3897 ret = FAIL;
3898
3899 *option_end = c; /* put back for error messages */
3900 *arg = option_end;
3901
3902 return ret;
3903}
3904
3905/*
3906 * Allocate a variable for a string constant.
3907 * Return OK or FAIL.
3908 */
3909 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 int evaluate;
3914{
3915 char_u *p;
3916 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 int extra = 0;
3918
3919 /*
3920 * Find the end of the string, skipping backslashed characters.
3921 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003922 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
3924 if (*p == '\\' && p[1] != NUL)
3925 {
3926 ++p;
3927 /* A "\<x>" form occupies at least 4 characters, and produces up
3928 * to 6 characters: reserve space for 2 extra */
3929 if (*p == '<')
3930 extra += 2;
3931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933
3934 if (*p != '"')
3935 {
3936 EMSG2(_("E114: Missing quote: %s"), *arg);
3937 return FAIL;
3938 }
3939
3940 /* If only parsing, set *arg and return here */
3941 if (!evaluate)
3942 {
3943 *arg = p + 1;
3944 return OK;
3945 }
3946
3947 /*
3948 * Copy the string into allocated memory, handling backslashed
3949 * characters.
3950 */
3951 name = alloc((unsigned)(p - *arg + extra));
3952 if (name == NULL)
3953 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003954 rettv->v_type = VAR_STRING;
3955 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
Bram Moolenaar8c711452005-01-14 21:53:12 +00003957 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 if (*p == '\\')
3960 {
3961 switch (*++p)
3962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 case 'b': *name++ = BS; ++p; break;
3964 case 'e': *name++ = ESC; ++p; break;
3965 case 'f': *name++ = FF; ++p; break;
3966 case 'n': *name++ = NL; ++p; break;
3967 case 'r': *name++ = CAR; ++p; break;
3968 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 case 'X': /* hex: "\x1", "\x12" */
3971 case 'x':
3972 case 'u': /* Unicode: "\u0023" */
3973 case 'U':
3974 if (vim_isxdigit(p[1]))
3975 {
3976 int n, nr;
3977 int c = toupper(*p);
3978
3979 if (c == 'X')
3980 n = 2;
3981 else
3982 n = 4;
3983 nr = 0;
3984 while (--n >= 0 && vim_isxdigit(p[1]))
3985 {
3986 ++p;
3987 nr = (nr << 4) + hex2nr(*p);
3988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003989 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#ifdef FEAT_MBYTE
3991 /* For "\u" store the number according to
3992 * 'encoding'. */
3993 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003994 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
3996#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 break;
4000
4001 /* octal: "\1", "\12", "\123" */
4002 case '0':
4003 case '1':
4004 case '2':
4005 case '3':
4006 case '4':
4007 case '5':
4008 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009 case '7': *name = *p++ - '0';
4010 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004012 *name = (*name << 3) + *p++ - '0';
4013 if (*p >= '0' && *p <= '7')
4014 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004016 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 break;
4018
4019 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (extra != 0)
4022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 break;
4025 }
4026 /* FALLTHROUGH */
4027
Bram Moolenaar8c711452005-01-14 21:53:12 +00004028 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 break;
4030 }
4031 }
4032 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004033 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 *arg = p + 1;
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 return OK;
4040}
4041
4042/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 * Return OK or FAIL.
4045 */
4046 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int evaluate;
4051{
4052 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004053 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004054 int reduce = 0;
4055
4056 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004058 */
4059 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4060 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004061 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004062 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004063 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004064 break;
4065 ++reduce;
4066 ++p;
4067 }
4068 }
4069
Bram Moolenaar8c711452005-01-14 21:53:12 +00004070 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004072 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004073 return FAIL;
4074 }
4075
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004077 if (!evaluate)
4078 {
4079 *arg = p + 1;
4080 return OK;
4081 }
4082
4083 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004084 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004085 */
4086 str = alloc((unsigned)((p - *arg) - reduce));
4087 if (str == NULL)
4088 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004089 rettv->v_type = VAR_STRING;
4090 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004091
Bram Moolenaar8c711452005-01-14 21:53:12 +00004092 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004093 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004094 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004096 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004097 break;
4098 ++p;
4099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004100 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004101 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004102 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004103 *arg = p + 1;
4104
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004105 return OK;
4106}
4107
4108/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 * Allocate a variable for a List and fill it from "*arg".
4110 * Return OK or FAIL.
4111 */
4112 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116 int evaluate;
4117{
4118 listvar *l = NULL;
4119 typeval tv;
4120 listitem *item;
4121
4122 if (evaluate)
4123 {
4124 l = list_alloc();
4125 if (l == NULL)
4126 return FAIL;
4127 }
4128
4129 *arg = skipwhite(*arg + 1);
4130 while (**arg != ']' && **arg != NUL)
4131 {
4132 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4133 goto failret;
4134 if (evaluate)
4135 {
4136 item = listitem_alloc();
4137 if (item != NULL)
4138 {
4139 item->li_tv = tv;
4140 list_append(l, item);
4141 }
4142 }
4143
4144 if (**arg == ']')
4145 break;
4146 if (**arg != ',')
4147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004149 goto failret;
4150 }
4151 *arg = skipwhite(*arg + 1);
4152 }
4153
4154 if (**arg != ']')
4155 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004157failret:
4158 if (evaluate)
4159 list_free(l);
4160 return FAIL;
4161 }
4162
4163 *arg = skipwhite(*arg + 1);
4164 if (evaluate)
4165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 rettv->v_type = VAR_LIST;
4167 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004168 ++l->lv_refcount;
4169 }
4170
4171 return OK;
4172}
4173
4174/*
4175 * Allocate an empty header for a list.
4176 */
4177 static listvar *
4178list_alloc()
4179{
4180 return (listvar *)alloc_clear(sizeof(listvar));
4181}
4182
4183/*
4184 * Unreference a list: decrement the reference count and free it when it
4185 * becomes zero.
4186 */
4187 static void
4188list_unref(l)
4189 listvar *l;
4190{
4191 if (l != NULL && --l->lv_refcount <= 0)
4192 list_free(l);
4193}
4194
4195/*
4196 * Free a list, including all items it points to.
4197 * Ignores the reference count.
4198 */
4199 static void
4200list_free(l)
4201 listvar *l;
4202{
4203 listitem *item;
4204 listitem *next;
4205
4206 for (item = l->lv_first; item != NULL; item = next)
4207 {
4208 next = item->li_next;
4209 listitem_free(item);
4210 }
4211 vim_free(l);
4212}
4213
4214/*
4215 * Allocate a list item.
4216 */
4217 static listitem *
4218listitem_alloc()
4219{
4220 return (listitem *)alloc(sizeof(listitem));
4221}
4222
4223/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004224 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004225 */
4226 static void
4227listitem_free(item)
4228 listitem *item;
4229{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231 vim_free(item);
4232}
4233
4234/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004235 * Remove a list item from a List and free it. Also clears the value.
4236 */
4237 static void
4238listitem_remove(l, item)
4239 listvar *l;
4240 listitem *item;
4241{
4242 list_remove(l, item, item);
4243 listitem_free(item);
4244}
4245
4246/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004247 * Get the number of items in a list.
4248 */
4249 static long
4250list_len(l)
4251 listvar *l;
4252{
4253 listitem *item;
4254 long len = 0;
4255
4256 if (l == NULL)
4257 return 0L;
4258 for (item = l->lv_first; item != NULL; item = item->li_next)
4259 ++len;
4260 return len;
4261}
4262
4263/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004264 * Return TRUE when two lists have exactly the same values.
4265 */
4266 static int
4267list_equal(l1, l2, ic)
4268 listvar *l1;
4269 listvar *l2;
4270 int ic; /* ignore case for strings */
4271{
4272 listitem *item1, *item2;
4273
4274 for (item1 = l1->lv_first, item2 = l2->lv_first;
4275 item1 != NULL && item2 != NULL;
4276 item1 = item1->li_next, item2 = item2->li_next)
4277 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4278 return FALSE;
4279 return item1 == NULL && item2 == NULL;
4280}
4281
4282/*
4283 * Return TRUE if "tv1" and "tv2" have the same value.
4284 * Compares the items just like "==" would compare them.
4285 */
4286 static int
4287tv_equal(tv1, tv2, ic)
4288 typeval *tv1;
4289 typeval *tv2;
4290 int ic; /* ignore case */
4291{
4292 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4293
4294 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4295 {
4296 /* recursive! */
4297 if (tv1->v_type != tv2->v_type
4298 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4299 return FALSE;
4300 }
4301 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4302 {
4303 if (tv1->v_type != tv2->v_type
4304 || tv1->vval.v_string == NULL
4305 || tv2->vval.v_string == NULL
4306 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4307 return FALSE;
4308 }
4309 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4310 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004311 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4312 * Don't consider "4x" to be equal to 4. */
4313 if ((tv1->v_type == VAR_STRING
4314 && !string_isa_number(tv1->vval.v_string))
4315 || (tv2->v_type == VAR_STRING
4316 && !string_isa_number(tv2->vval.v_string)))
4317 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004318 if (get_tv_number(tv1) != get_tv_number(tv2))
4319 return FALSE;
4320 }
4321 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4322 get_tv_string_buf(tv2, buf2)) != 0)
4323 return FALSE;
4324 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4325 get_tv_string_buf(tv2, buf2)) != 0)
4326 return FALSE;
4327 return TRUE;
4328}
4329
4330/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004331 * Return TRUE if "tv" is a number without other non-white characters.
4332 */
4333 static int
4334string_isa_number(s)
4335 char_u *s;
4336{
4337 int len;
4338
4339 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4340 return len > 0 && *skipwhite(s + len) == NUL;
4341}
4342
4343/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004344 * Locate item with index "n" in list "l" and return it.
4345 * A negative index is counted from the end; -1 is the last item.
4346 * Returns NULL when "n" is out of range.
4347 */
4348 static listitem *
4349list_find(l, n)
4350 listvar *l;
4351 long n;
4352{
4353 listitem *item;
4354 long idx;
4355
4356 if (l == NULL)
4357 return NULL;
4358 if (n < 0)
4359 {
4360 idx = -1; /* search from the end */
4361 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4362 --idx;
4363 }
4364 else
4365 {
4366 idx = 0; /* search from the start */
4367 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4368 ++idx;
4369 }
4370 if (idx != n)
4371 return NULL;
4372 return item;
4373}
4374
4375/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004376 * Locate "item" list "l" and return its index.
4377 * Returns -1 when "item" is not in the list.
4378 */
4379 static long
4380list_idx_of_item(l, item)
4381 listvar *l;
4382 listitem *item;
4383{
4384 long idx = 0;
4385 listitem *li;
4386
4387 if (l == NULL)
4388 return -1;
4389 idx = 0;
4390 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4391 ++idx;
4392 if (li == NULL)
4393 return -1;
4394 return idx;;
4395}
4396
4397/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 * Like list_find(), but also find an item just past the end.
4399 * "*ip" is the item to find.
4400 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4401 * Returns NULL when item not found or item is just past the end.
4402 */
4403 static listitem *
4404list_find_ext(l, ip)
4405 listvar *l;
4406 long *ip;
4407{
4408 long n;
4409 listitem *item;
4410
4411 if (*ip < 0)
4412 {
4413 /* Count from the end: -1 is before last item. */
4414 item = l->lv_last;
4415 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4416 item = item->li_prev;
4417 if (item == NULL)
4418 n = 1; /* error! */
4419 }
4420 else
4421 {
4422 item = l->lv_first;
4423 for (n = *ip; n > 0 && item != NULL; --n)
4424 item = item->li_next;
4425 }
4426 *ip = n;
4427 return item;
4428}
4429
4430/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004431 * Append item "item" to the end of list "l".
4432 */
4433 static void
4434list_append(l, item)
4435 listvar *l;
4436 listitem *item;
4437{
4438 if (l->lv_last == NULL)
4439 {
4440 /* empty list */
4441 l->lv_first = item;
4442 l->lv_last = item;
4443 item->li_prev = NULL;
4444 }
4445 else
4446 {
4447 l->lv_last->li_next = item;
4448 item->li_prev = l->lv_last;
4449 l->lv_last = item;
4450 }
4451 item->li_next = NULL;
4452}
4453
4454/*
4455 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004456 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 */
4458 static int
4459list_append_tv(l, tv)
4460 listvar *l;
4461 typeval *tv;
4462{
4463 listitem *ni = listitem_alloc();
4464
4465 if (ni == NULL)
4466 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 list_append(l, ni);
4469 return OK;
4470}
4471
4472/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004473 * Insert typeval "tv" in list "l" before "item".
4474 * If "item" is NULL append at the end.
4475 * Return FAIL when out of memory.
4476 */
4477 static int
4478list_insert_tv(l, tv, item)
4479 listvar *l;
4480 typeval *tv;
4481 listitem *item;
4482{
4483 listitem *ni = listitem_alloc();
4484
4485 if (ni == NULL)
4486 return FAIL;
4487 copy_tv(tv, &ni->li_tv);
4488 if (item == NULL)
4489 /* Append new item at end of list. */
4490 list_append(l, ni);
4491 else
4492 {
4493 /* Insert new item before existing item. */
4494 ni->li_prev = item->li_prev;
4495 ni->li_next = item;
4496 if (item->li_prev == NULL)
4497 l->lv_first = ni;
4498 else
4499 item->li_prev->li_next = ni;
4500 item->li_prev = ni;
4501 }
4502 return OK;
4503}
4504
4505/*
4506 * Extend "l1" with "l2".
4507 * If "bef" is NULL append at the end, otherwise insert before this item.
4508 * Returns FAIL when out of memory.
4509 */
4510 static int
4511list_extend(l1, l2, bef)
4512 listvar *l1;
4513 listvar *l2;
4514 listitem *bef;
4515{
4516 listitem *item;
4517
4518 for (item = l2->lv_first; item != NULL; item = item->li_next)
4519 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4520 return FAIL;
4521 return OK;
4522}
4523
4524/*
4525 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4526 * Return FAIL when out of memory.
4527 */
4528 static int
4529list_concat(l1, l2, tv)
4530 listvar *l1;
4531 listvar *l2;
4532 typeval *tv;
4533{
4534 listvar *l;
4535
4536 /* make a copy of the first list. */
4537 l = list_copy(l1, FALSE);
4538 if (l == NULL)
4539 return FAIL;
4540 tv->v_type = VAR_LIST;
4541 tv->vval.v_list = l;
4542
4543 /* append all items from the second list */
4544 return list_extend(l, l2, NULL);
4545}
4546
4547/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004548 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 * The refcount of the new list is set to 1.
4550 * Returns NULL when out of memory.
4551 */
4552 static listvar *
4553list_copy(orig, deep)
4554 listvar *orig;
4555 int deep;
4556{
4557 listvar *copy;
4558 listitem *item;
4559 listitem *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560
4561 if (orig == NULL)
4562 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563
4564 copy = list_alloc();
4565 if (copy != NULL)
4566 {
4567 for (item = orig->lv_first; item != NULL; item = item->li_next)
4568 {
4569 ni = listitem_alloc();
4570 if (ni == NULL)
4571 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004572 if (deep)
4573 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 list_append(copy, ni);
4577 }
4578 ++copy->lv_refcount;
4579 }
4580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 return copy;
4582}
4583
4584/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004585 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004586 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004588 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004589list_remove(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004592 listitem *item2;
4593{
4594 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004596 /* notify watchers */
4597 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 list_fix_watch(l, ip);
4600 if (ip == item2)
4601 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004603
4604 if (item2->li_next == NULL)
4605 l->lv_last = item->li_prev;
4606 else
4607 item2->li_next->li_prev = item->li_prev;
4608 if (item->li_prev == NULL)
4609 l->lv_first = item2->li_next;
4610 else
4611 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612}
4613
4614/*
4615 * Return an allocated string with the string representation of a list.
4616 * May return NULL.
4617 */
4618 static char_u *
4619list2string(tv)
4620 typeval *tv;
4621{
4622 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623
4624 if (tv->vval.v_list == NULL)
4625 return NULL;
4626 ga_init2(&ga, (int)sizeof(char), 80);
4627 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004628 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 ga_append(&ga, ']');
4630 ga_append(&ga, NUL);
4631 return (char_u *)ga.ga_data;
4632}
4633
4634/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004635 * Join list "l" into a string in "*gap", using separator "sep".
4636 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4637 */
4638 static void
4639list_join(gap, l, sep, echo)
4640 garray_T *gap;
4641 listvar *l;
4642 char_u *sep;
4643 int echo;
4644{
4645 int first = TRUE;
4646 char_u *tofree;
4647 char_u numbuf[NUMBUFLEN];
4648 listitem *item;
4649 char_u *s;
4650
4651 for (item = l->lv_first; item != NULL; item = item->li_next)
4652 {
4653 if (first)
4654 first = FALSE;
4655 else
4656 ga_concat(gap, sep);
4657
4658 if (echo)
4659 s = echo_string(&item->li_tv, &tofree, numbuf);
4660 else
4661 s = tv2string(&item->li_tv, &tofree, numbuf);
4662 if (s != NULL)
4663 ga_concat(gap, s);
4664 vim_free(tofree);
4665 }
4666}
4667
4668/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 * Allocate an empty header for a dictionary.
4670 */
4671 static dictvar *
4672dict_alloc()
4673{
4674 return (dictvar *)alloc_clear(sizeof(dictvar));
4675}
4676
4677/*
4678 * Unreference a Dictionary: decrement the reference count and free it when it
4679 * becomes zero.
4680 */
4681 static void
4682dict_unref(d)
4683 dictvar *d;
4684{
4685 if (d != NULL && --d->dv_refcount <= 0)
4686 dict_free(d);
4687}
4688
4689/*
4690 * Free a Dictionary, including all items it contains.
4691 * Ignores the reference count.
4692 */
4693 static void
4694dict_free(d)
4695 dictvar *d;
4696{
4697 dictitem *item;
4698 dictitem *next;
4699
4700 for (item = d->dv_first; item != NULL; item = next)
4701 {
4702 next = item->di_next;
4703 dictitem_free(item);
4704 }
4705 vim_free(d);
4706}
4707
4708/*
4709 * Allocate a Dictionary item.
4710 */
4711 static dictitem *
4712dictitem_alloc()
4713{
4714 return (dictitem *)alloc(sizeof(dictitem));
4715}
4716
4717/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004718 * Make a copy of a Dictionary item.
4719 */
4720 static dictitem *
4721dictitem_copy(org)
4722 dictitem *org;
4723{
4724 dictitem *di;
4725
4726 di = (dictitem *)alloc(sizeof(dictitem));
4727 if (di != NULL)
4728 {
4729 di->di_key = vim_strsave(org->di_key);
4730 if (di->di_key == NULL)
4731 {
4732 vim_free(di);
4733 return NULL;
4734 }
4735 copy_tv(&org->di_tv, &di->di_tv);
4736 }
4737 return di;
4738}
4739
4740/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 * Free a dict item. Also clears the value.
4742 */
4743 static void
4744dictitem_free(item)
4745 dictitem *item;
4746{
4747 vim_free(item->di_key);
4748 clear_tv(&item->di_tv);
4749 vim_free(item);
4750}
4751
4752/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004753 * Make a copy of dict "d". Shallow if "deep" is FALSE.
4754 * The refcount of the new dict is set to 1.
4755 * Returns NULL when out of memory.
4756 */
4757 static dictvar *
4758dict_copy(orig, deep)
4759 dictvar *orig;
4760 int deep;
4761{
4762 dictvar *copy;
4763 dictitem *item;
4764 dictitem *di;
4765
4766 if (orig == NULL)
4767 return NULL;
4768
4769 copy = dict_alloc();
4770 if (copy != NULL)
4771 {
4772 for (item = orig->dv_first; item != NULL; item = item->di_next)
4773 {
4774 di = dictitem_alloc();
4775 if (di == NULL)
4776 break;
4777 di->di_key = vim_strsave(item->di_key);
4778 if (di->di_key == NULL)
4779 {
4780 vim_free(di);
4781 break;
4782 }
4783 if (deep)
4784 item_copy(&item->di_tv, &di->di_tv, deep);
4785 else
4786 copy_tv(&item->di_tv, &di->di_tv);
4787 dict_add(copy, di);
4788 }
4789 ++copy->dv_refcount;
4790 }
4791
4792 return copy;
4793}
4794
4795/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 * Add item "item" to Dictionary "d".
4797 */
4798 static void
4799dict_add(d, item)
4800 dictvar *d;
4801 dictitem *item;
4802{
4803 item->di_next = d->dv_first;
4804 d->dv_first = item;
4805}
4806
4807#if 0 /* not currently used */
4808static void dict_set_item __ARGS((dictvar *d, int type, char *key, void *val));
4809
4810/*
4811 * Add an item to Dictionary "d" with type "type", key "key" and value "val".
4812 * If it already exists it is overwritten.
4813 * The key and value are copied to allocated memory.
4814 */
4815 static void
4816dict_set_item(d, type, key, val)
4817 dictvar *d;
4818 int type;
4819 char *key;
4820 void *val;
4821{
4822 dictitem *di;
4823 char_u *dkey;
4824
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004825 di = dict_find(d, (char_u *)key, -1, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 if (di == NULL)
4827 {
4828 dkey = vim_strsave((char_u *)key);
4829 if (dkey != NULL)
4830 {
4831 di = dictitem_alloc();
4832 if (di == NULL)
4833 vim_free(dkey);
4834 else
4835 di->di_key = dkey;
4836 }
4837 }
4838 else
4839 clear_tv(&di->di_tv);
4840
4841 if (di != NULL)
4842 {
4843 di->di_tv.v_type = type;
4844 switch (type)
4845 {
4846 case VAR_NUMBER:
4847 di->di_tv.vval.v_number = (varnumber_T)val;
4848 break;
4849 case VAR_FUNC:
4850 case VAR_STRING:
4851 di->di_tv.vval.v_string = vim_strsave((char_u *)val);
4852 break;
4853 default:
4854 EMSG2(_(e_intern2), "dict_set_item()");
4855 }
4856 dict_add(d, di);
4857 }
4858}
4859#endif
4860
4861/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004862 * Get the number of items in a Dictionary.
4863 */
4864 static long
4865dict_len(d)
4866 dictvar *d;
4867{
4868 dictitem *item;
4869 long len = 0;
4870
4871 if (d == NULL)
4872 return 0L;
4873 for (item = d->dv_first; item != NULL; item = item->di_next)
4874 ++len;
4875 return len;
4876}
4877
4878/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004879 * Find item "key[len]" in Dictionary "d".
4880 * If "len" is negative use strlen(key).
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004881 * Sets "*pdi" to pointer to found item, unless "pdi" is NULL.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004882 * Returns NULL when not found.
4883 */
4884 static dictitem *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004885dict_find(d, key, len, pdi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 dictvar *d;
4887 char_u *key;
4888 int len;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004889 dictitem ***pdi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004890{
4891 static dictitem *di;
4892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004893 if (pdi != NULL)
4894 *pdi = &d->dv_first;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004895 for (di = d->dv_first; di != NULL; di = di->di_next)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 if (len < 0
4898 ? STRCMP(di->di_key, key) == 0
4899 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL)
4900 return di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004901 if (pdi != NULL)
4902 *pdi = &di->di_next;
4903 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 return NULL;
4905}
4906
4907/*
4908 * Return an allocated string with the string representation of a Dictionary.
4909 * May return NULL.
4910 */
4911 static char_u *
4912dict2string(tv)
4913 typeval *tv;
4914{
4915 garray_T ga;
4916 int first = TRUE;
4917 char_u *tofree;
4918 char_u numbuf[NUMBUFLEN];
4919 dictitem *item;
4920 char_u *s;
4921
4922 if (tv->vval.v_dict == NULL)
4923 return NULL;
4924 ga_init2(&ga, (int)sizeof(char), 80);
4925 ga_append(&ga, '{');
4926
4927 for (item = tv->vval.v_dict->dv_first; item != NULL; item = item->di_next)
4928 {
4929 if (first)
4930 first = FALSE;
4931 else
4932 ga_concat(&ga, (char_u *)", ");
4933
4934 tofree = string_quote(item->di_key, FALSE);
4935 if (tofree != NULL)
4936 {
4937 ga_concat(&ga, tofree);
4938 vim_free(tofree);
4939 }
4940 ga_concat(&ga, (char_u *)": ");
4941 s = tv2string(&item->di_tv, &tofree, numbuf);
4942 if (s != NULL)
4943 ga_concat(&ga, s);
4944 vim_free(tofree);
4945 }
4946
4947 ga_append(&ga, '}');
4948 ga_append(&ga, NUL);
4949 return (char_u *)ga.ga_data;
4950}
4951
4952/*
4953 * Allocate a variable for a Dictionary and fill it from "*arg".
4954 * Return OK or FAIL. Returns NOTDONE for {expr}.
4955 */
4956 static int
4957get_dict_tv(arg, rettv, evaluate)
4958 char_u **arg;
4959 typeval *rettv;
4960 int evaluate;
4961{
4962 dictvar *d = NULL;
4963 typeval tv;
4964 char_u *key;
4965 dictitem *item;
4966 char_u *start = skipwhite(*arg + 1);
4967
4968 /*
4969 * First check if it's not a curly-braces thing: {expr}.
4970 * Must do this without evaluating, otherwise a function may be called
4971 * twice. Unfortunately this means we need to call eval1() twice for the
4972 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004973 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004975 if (*start != '}')
4976 {
4977 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
4978 return FAIL;
4979 if (*start == '}')
4980 return NOTDONE;
4981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982
4983 if (evaluate)
4984 {
4985 d = dict_alloc();
4986 if (d == NULL)
4987 return FAIL;
4988 }
4989
4990 *arg = skipwhite(*arg + 1);
4991 while (**arg != '}' && **arg != NUL)
4992 {
4993 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4994 goto failret;
4995 if (**arg != ':')
4996 {
4997 EMSG2(_("E999: Missing colon in Dictionary: %s"), *arg);
4998 clear_tv(&tv);
4999 goto failret;
5000 }
5001 key = get_tv_string(&tv);
5002 if (*key == NUL)
5003 {
5004 EMSG(_(e_emptykey));
5005 clear_tv(&tv);
5006 goto failret;
5007 }
5008 key = vim_strsave(key);
5009 clear_tv(&tv);
5010 if (key == NULL)
5011 goto failret;
5012
5013 *arg = skipwhite(*arg + 1);
5014 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5015 {
5016 vim_free(key);
5017 goto failret;
5018 }
5019 if (evaluate)
5020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005021 item = dict_find(d, key, -1, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 if (item != NULL)
5023 {
5024 EMSG(_("E999: Duplicate key in Dictionary"));
5025 vim_free(key);
5026 clear_tv(&tv);
5027 goto failret;
5028 }
5029 item = dictitem_alloc();
5030 if (item == NULL)
5031 vim_free(key);
5032 else
5033 {
5034 item->di_key = key;
5035 item->di_tv = tv;
5036 dict_add(d, item);
5037 }
5038 }
5039
5040 if (**arg == '}')
5041 break;
5042 if (**arg != ',')
5043 {
5044 EMSG2(_("E999: Missing comma in Dictionary: %s"), *arg);
5045 goto failret;
5046 }
5047 *arg = skipwhite(*arg + 1);
5048 }
5049
5050 if (**arg != '}')
5051 {
5052 EMSG2(_("E999: Missing end of Dictionary '}': %s"), *arg);
5053failret:
5054 if (evaluate)
5055 dict_free(d);
5056 return FAIL;
5057 }
5058
5059 *arg = skipwhite(*arg + 1);
5060 if (evaluate)
5061 {
5062 rettv->v_type = VAR_DICT;
5063 rettv->vval.v_dict = d;
5064 ++d->dv_refcount;
5065 }
5066
5067 return OK;
5068}
5069
5070
5071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005072 * Return a string with the string representation of a variable.
5073 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005074 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 * May return NULL;
5077 */
5078 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 typeval *tv;
5081 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005082 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005083{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005084 static int recurse = 0;
5085 char_u *r = NULL;
5086
5087 if (recurse >= VAR_MAXNEST)
5088 {
5089 EMSG(_("E999: variable nested too deep for displaying"));
5090 *tofree = NULL;
5091 return NULL;
5092 }
5093 ++recurse;
5094
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095 switch (tv->v_type)
5096 {
5097 case VAR_FUNC:
5098 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005099 r = tv->vval.v_string;
5100 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005101 case VAR_LIST:
5102 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103 r = *tofree;
5104 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 case VAR_DICT:
5106 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005107 r = *tofree;
5108 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 case VAR_STRING:
5110 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 *tofree = NULL;
5112 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005116 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118
5119 --recurse;
5120 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121}
5122
5123/*
5124 * Return a string with the string representation of a variable.
5125 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5126 * "numbuf" is used for a number.
5127 * Puts quotes around strings, so that they can be parsed back by eval().
5128 * May return NULL;
5129 */
5130 static char_u *
5131tv2string(tv, tofree, numbuf)
5132 typeval *tv;
5133 char_u **tofree;
5134 char_u *numbuf;
5135{
5136 switch (tv->v_type)
5137 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138 case VAR_FUNC:
5139 *tofree = string_quote(tv->vval.v_string, TRUE);
5140 return *tofree;
5141 case VAR_STRING:
5142 *tofree = string_quote(tv->vval.v_string, FALSE);
5143 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005147 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005148 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005151 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152}
5153
5154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Return a string in ' quotes, doubling ' characters.
5156 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005157 */
5158 static char_u *
5159string_quote(str, function)
5160 char_u *str;
5161 int function;
5162{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005163 unsigned len = STRLEN(str) + (function ? 13 : 3);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005164 char_u *p, *r, *s;
5165
5166 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005168 ++len;
5169 s = r = alloc(len);
5170 if (r != NULL)
5171 {
5172 if (function)
5173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 r += 10;
5176 }
5177 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 *r++ = '\'';
5179 for (p = str; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005180 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 if (*p == '\'')
5182 *r++ = '\'';
5183 MB_COPY_CHAR(p, r);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005184 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005186 if (function)
5187 *r++ = ')';
5188 *r++ = NUL;
5189 }
5190 return s;
5191}
5192
5193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 * Get the value of an environment variable.
5195 * "arg" is pointing to the '$'. It is advanced to after the name.
5196 * If the environment variable was not set, silently assume it is empty.
5197 * Always return OK.
5198 */
5199 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 int evaluate;
5204{
5205 char_u *string = NULL;
5206 int len;
5207 int cc;
5208 char_u *name;
5209
5210 ++*arg;
5211 name = *arg;
5212 len = get_env_len(arg);
5213 if (evaluate)
5214 {
5215 if (len != 0)
5216 {
5217 cc = name[len];
5218 name[len] = NUL;
5219 /* first try mch_getenv(), fast for normal environment vars */
5220 string = mch_getenv(name);
5221 if (string != NULL && *string != NUL)
5222 string = vim_strsave(string);
5223 else
5224 {
5225 /* next try expanding things like $VIM and ${HOME} */
5226 string = expand_env_save(name - 1);
5227 if (string != NULL && *string == '$')
5228 {
5229 vim_free(string);
5230 string = NULL;
5231 }
5232 }
5233 name[len] = cc;
5234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235 rettv->v_type = VAR_STRING;
5236 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238
5239 return OK;
5240}
5241
5242/*
5243 * Array with names and number of arguments of all internal functions
5244 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5245 */
5246static struct fst
5247{
5248 char *f_name; /* function name */
5249 char f_min_argc; /* minimal number of arguments */
5250 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 void (*f_func) __ARGS((typeval *args, typeval *rvar));
5252 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253} functions[] =
5254{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005255 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {"append", 2, 2, f_append},
5257 {"argc", 0, 0, f_argc},
5258 {"argidx", 0, 0, f_argidx},
5259 {"argv", 1, 1, f_argv},
5260 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005261 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {"bufexists", 1, 1, f_bufexists},
5263 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5264 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5265 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5266 {"buflisted", 1, 1, f_buflisted},
5267 {"bufloaded", 1, 1, f_bufloaded},
5268 {"bufname", 1, 1, f_bufname},
5269 {"bufnr", 1, 1, f_bufnr},
5270 {"bufwinnr", 1, 1, f_bufwinnr},
5271 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005272 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005273 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {"char2nr", 1, 1, f_char2nr},
5275 {"cindent", 1, 1, f_cindent},
5276 {"col", 1, 1, f_col},
5277 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005279 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {"cscope_connection",0,3, f_cscope_connection},
5281 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {"delete", 1, 1, f_delete},
5284 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005285 {"diff_filler", 1, 1, f_diff_filler},
5286 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005287 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005289 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {"eventhandler", 0, 0, f_eventhandler},
5291 {"executable", 1, 1, f_executable},
5292 {"exists", 1, 1, f_exists},
5293 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005294 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5296 {"filereadable", 1, 1, f_filereadable},
5297 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005298 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005299 {"finddir", 1, 3, f_finddir},
5300 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {"fnamemodify", 2, 2, f_fnamemodify},
5302 {"foldclosed", 1, 1, f_foldclosed},
5303 {"foldclosedend", 1, 1, f_foldclosedend},
5304 {"foldlevel", 1, 1, f_foldlevel},
5305 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005306 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005309 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {"getbufvar", 2, 2, f_getbufvar},
5311 {"getchar", 0, 1, f_getchar},
5312 {"getcharmod", 0, 0, f_getcharmod},
5313 {"getcmdline", 0, 0, f_getcmdline},
5314 {"getcmdpos", 0, 0, f_getcmdpos},
5315 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005316 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005317 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {"getfsize", 1, 1, f_getfsize},
5319 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005320 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005321 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {"getreg", 0, 1, f_getreg},
5323 {"getregtype", 0, 1, f_getregtype},
5324 {"getwinposx", 0, 0, f_getwinposx},
5325 {"getwinposy", 0, 0, f_getwinposy},
5326 {"getwinvar", 2, 2, f_getwinvar},
5327 {"glob", 1, 1, f_glob},
5328 {"globpath", 2, 2, f_globpath},
5329 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005330 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {"hasmapto", 1, 2, f_hasmapto},
5332 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5333 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5334 {"histadd", 2, 2, f_histadd},
5335 {"histdel", 1, 2, f_histdel},
5336 {"histget", 1, 2, f_histget},
5337 {"histnr", 1, 1, f_histnr},
5338 {"hlID", 1, 1, f_hlID},
5339 {"hlexists", 1, 1, f_hlexists},
5340 {"hostname", 0, 0, f_hostname},
5341 {"iconv", 3, 3, f_iconv},
5342 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005343 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {"input", 1, 2, f_input},
5345 {"inputdialog", 1, 3, f_inputdialog},
5346 {"inputrestore", 0, 0, f_inputrestore},
5347 {"inputsave", 0, 0, f_inputsave},
5348 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005352 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {"libcall", 3, 3, f_libcall},
5357 {"libcallnr", 3, 3, f_libcallnr},
5358 {"line", 1, 1, f_line},
5359 {"line2byte", 1, 1, f_line2byte},
5360 {"lispindent", 1, 1, f_lispindent},
5361 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005362 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {"maparg", 1, 2, f_maparg},
5364 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005365 {"match", 2, 4, f_match},
5366 {"matchend", 2, 4, f_matchend},
5367 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005368 {"max", 1, 1, f_max},
5369 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {"mode", 0, 0, f_mode},
5371 {"nextnonblank", 1, 1, f_nextnonblank},
5372 {"nr2char", 1, 1, f_nr2char},
5373 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {"remote_expr", 2, 3, f_remote_expr},
5376 {"remote_foreground", 1, 1, f_remote_foreground},
5377 {"remote_peek", 1, 2, f_remote_peek},
5378 {"remote_read", 1, 1, f_remote_read},
5379 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005380 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005382 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005384 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {"search", 1, 2, f_search},
5386 {"searchpair", 3, 5, f_searchpair},
5387 {"server2client", 2, 2, f_server2client},
5388 {"serverlist", 0, 0, f_serverlist},
5389 {"setbufvar", 3, 3, f_setbufvar},
5390 {"setcmdpos", 1, 1, f_setcmdpos},
5391 {"setline", 2, 2, f_setline},
5392 {"setreg", 2, 3, f_setreg},
5393 {"setwinvar", 3, 3, f_setwinvar},
5394 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005395 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005396 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#ifdef HAVE_STRFTIME
5398 {"strftime", 1, 2, f_strftime},
5399#endif
5400 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {"strlen", 1, 1, f_strlen},
5403 {"strpart", 2, 3, f_strpart},
5404 {"strridx", 2, 2, f_strridx},
5405 {"strtrans", 1, 1, f_strtrans},
5406 {"submatch", 1, 1, f_submatch},
5407 {"substitute", 4, 4, f_substitute},
5408 {"synID", 3, 3, f_synID},
5409 {"synIDattr", 2, 3, f_synIDattr},
5410 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005411 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {"tempname", 0, 0, f_tempname},
5413 {"tolower", 1, 1, f_tolower},
5414 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005415 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {"virtcol", 1, 1, f_virtcol},
5419 {"visualmode", 0, 1, f_visualmode},
5420 {"winbufnr", 1, 1, f_winbufnr},
5421 {"wincol", 0, 0, f_wincol},
5422 {"winheight", 1, 1, f_winheight},
5423 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005424 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {"winrestcmd", 0, 0, f_winrestcmd},
5426 {"winwidth", 1, 1, f_winwidth},
5427};
5428
5429#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5430
5431/*
5432 * Function given to ExpandGeneric() to obtain the list of internal
5433 * or user defined function names.
5434 */
5435 char_u *
5436get_function_name(xp, idx)
5437 expand_T *xp;
5438 int idx;
5439{
5440 static int intidx = -1;
5441 char_u *name;
5442
5443 if (idx == 0)
5444 intidx = -1;
5445 if (intidx < 0)
5446 {
5447 name = get_user_func_name(xp, idx);
5448 if (name != NULL)
5449 return name;
5450 }
5451 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5452 {
5453 STRCPY(IObuff, functions[intidx].f_name);
5454 STRCAT(IObuff, "(");
5455 if (functions[intidx].f_max_argc == 0)
5456 STRCAT(IObuff, ")");
5457 return IObuff;
5458 }
5459
5460 return NULL;
5461}
5462
5463/*
5464 * Function given to ExpandGeneric() to obtain the list of internal or
5465 * user defined variable or function names.
5466 */
5467/*ARGSUSED*/
5468 char_u *
5469get_expr_name(xp, idx)
5470 expand_T *xp;
5471 int idx;
5472{
5473 static int intidx = -1;
5474 char_u *name;
5475
5476 if (idx == 0)
5477 intidx = -1;
5478 if (intidx < 0)
5479 {
5480 name = get_function_name(xp, idx);
5481 if (name != NULL)
5482 return name;
5483 }
5484 return get_user_var_name(xp, ++intidx);
5485}
5486
5487#endif /* FEAT_CMDL_COMPL */
5488
5489/*
5490 * Find internal function in table above.
5491 * Return index, or -1 if not found
5492 */
5493 static int
5494find_internal_func(name)
5495 char_u *name; /* name of the function */
5496{
5497 int first = 0;
5498 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5499 int cmp;
5500 int x;
5501
5502 /*
5503 * Find the function name in the table. Binary search.
5504 */
5505 while (first <= last)
5506 {
5507 x = first + ((unsigned)(last - first) >> 1);
5508 cmp = STRCMP(name, functions[x].f_name);
5509 if (cmp < 0)
5510 last = x - 1;
5511 else if (cmp > 0)
5512 first = x + 1;
5513 else
5514 return x;
5515 }
5516 return -1;
5517}
5518
5519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5521 * name it contains, otherwise return "name".
5522 */
5523 static char_u *
5524deref_func_name(name, lenp)
5525 char_u *name;
5526 int *lenp;
5527{
5528 VAR v;
5529 int cc;
5530
5531 cc = name[*lenp];
5532 name[*lenp] = NUL;
5533 v = find_var(name, FALSE);
5534 name[*lenp] = cc;
5535 if (v != NULL && v->tv.v_type == VAR_FUNC)
5536 {
5537 if (v->tv.vval.v_string == NULL)
5538 {
5539 *lenp = 0;
5540 return (char_u *)""; /* just in case */
5541 }
5542 *lenp = STRLEN(v->tv.vval.v_string);
5543 return v->tv.vval.v_string;
5544 }
5545
5546 return name;
5547}
5548
5549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 * Allocate a variable for the result of a function.
5551 * Return OK or FAIL.
5552 */
5553 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005554get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5555 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 char_u *name; /* name of the function */
5557 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 char_u **arg; /* argument, pointing to the '(' */
5560 linenr_T firstline; /* first line of range */
5561 linenr_T lastline; /* last line of range */
5562 int *doesrange; /* return: function handled range */
5563 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005564 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 char_u *argp;
5567 int ret = OK;
5568#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 int argcount = 0; /* number of arguments found */
5571
5572 /*
5573 * Get the arguments.
5574 */
5575 argp = *arg;
5576 while (argcount < MAX_FUNC_ARGS)
5577 {
5578 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5579 if (*argp == ')' || *argp == ',' || *argp == NUL)
5580 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5582 {
5583 ret = FAIL;
5584 break;
5585 }
5586 ++argcount;
5587 if (*argp != ',')
5588 break;
5589 }
5590 if (*argp == ')')
5591 ++argp;
5592 else
5593 ret = FAIL;
5594
5595 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005597 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 else if (!aborting())
5599 EMSG2(_("E116: Invalid arguments for function %s"), name);
5600
5601 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005602 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604 *arg = skipwhite(argp);
5605 return ret;
5606}
5607
5608
5609/*
5610 * Call a function with its resolved parameters
5611 * Return OK or FAIL.
5612 */
5613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005615 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 char_u *name; /* name of the function */
5617 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 linenr_T firstline; /* first line of range */
5622 linenr_T lastline; /* last line of range */
5623 int *doesrange; /* return: function handled range */
5624 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005625 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#define ERROR_UNKNOWN 0
5629#define ERROR_TOOMANY 1
5630#define ERROR_TOOFEW 2
5631#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00005632#define ERROR_DICT 4
5633#define ERROR_NONE 5
5634#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 int error = ERROR_NONE;
5636 int i;
5637 int llen;
5638 ufunc_T *fp;
5639 int cc;
5640#define FLEN_FIXED 40
5641 char_u fname_buf[FLEN_FIXED + 1];
5642 char_u *fname;
5643
5644 /*
5645 * In a script change <SID>name() and s:name() to K_SNR 123_name().
5646 * Change <SNR>123_name() to K_SNR 123_name().
5647 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
5648 */
5649 cc = name[len];
5650 name[len] = NUL;
5651 llen = eval_fname_script(name);
5652 if (llen > 0)
5653 {
5654 fname_buf[0] = K_SPECIAL;
5655 fname_buf[1] = KS_EXTRA;
5656 fname_buf[2] = (int)KE_SNR;
5657 i = 3;
5658 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
5659 {
5660 if (current_SID <= 0)
5661 error = ERROR_SCRIPT;
5662 else
5663 {
5664 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
5665 i = (int)STRLEN(fname_buf);
5666 }
5667 }
5668 if (i + STRLEN(name + llen) < FLEN_FIXED)
5669 {
5670 STRCPY(fname_buf + i, name + llen);
5671 fname = fname_buf;
5672 }
5673 else
5674 {
5675 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
5676 if (fname == NULL)
5677 error = ERROR_OTHER;
5678 else
5679 {
5680 mch_memmove(fname, fname_buf, (size_t)i);
5681 STRCPY(fname + i, name + llen);
5682 }
5683 }
5684 }
5685 else
5686 fname = name;
5687
5688 *doesrange = FALSE;
5689
5690
5691 /* execute the function if no errors detected and executing */
5692 if (evaluate && error == ERROR_NONE)
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 error = ERROR_UNKNOWN;
5696
5697 if (!ASCII_ISLOWER(fname[0]))
5698 {
5699 /*
5700 * User defined function.
5701 */
5702 fp = find_func(fname);
5703#ifdef FEAT_AUTOCMD
5704 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
5705 fname, fname, TRUE, NULL)
5706#ifdef FEAT_EVAL
5707 && !aborting()
5708#endif
5709 )
5710 {
5711 /* executed an autocommand, search for function again */
5712 fp = find_func(fname);
5713 }
5714#endif
5715 if (fp != NULL)
5716 {
5717 if (fp->flags & FC_RANGE)
5718 *doesrange = TRUE;
5719 if (argcount < fp->args.ga_len)
5720 error = ERROR_TOOFEW;
5721 else if (!fp->varargs && argcount > fp->args.ga_len)
5722 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005723 else if ((fp->flags & FC_DICT) && selfdict == NULL)
5724 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 else
5726 {
5727 /*
5728 * Call the user function.
5729 * Save and restore search patterns, script variables and
5730 * redo buffer.
5731 */
5732 save_search_patterns();
5733 saveRedobuff();
5734 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005736 firstline, lastline,
5737 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 --fp->calls;
5739 restoreRedobuff();
5740 restore_search_patterns();
5741 error = ERROR_NONE;
5742 }
5743 }
5744 }
5745 else
5746 {
5747 /*
5748 * Find the function name in the table, call its implementation.
5749 */
5750 i = find_internal_func(fname);
5751 if (i >= 0)
5752 {
5753 if (argcount < functions[i].f_min_argc)
5754 error = ERROR_TOOFEW;
5755 else if (argcount > functions[i].f_max_argc)
5756 error = ERROR_TOOMANY;
5757 else
5758 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005760 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 error = ERROR_NONE;
5762 }
5763 }
5764 }
5765 /*
5766 * The function call (or "FuncUndefined" autocommand sequence) might
5767 * have been aborted by an error, an interrupt, or an explicitly thrown
5768 * exception that has not been caught so far. This situation can be
5769 * tested for by calling aborting(). For an error in an internal
5770 * function or for the "E132" error in call_user_func(), however, the
5771 * throw point at which the "force_abort" flag (temporarily reset by
5772 * emsg()) is normally updated has not been reached yet. We need to
5773 * update that flag first to make aborting() reliable.
5774 */
5775 update_force_abort();
5776 }
5777 if (error == ERROR_NONE)
5778 ret = OK;
5779
5780 /*
5781 * Report an error unless the argument evaluation or function call has been
5782 * cancelled due to an aborting error, an interrupt, or an exception.
5783 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 if (!aborting())
5785 {
5786 switch (error)
5787 {
5788 case ERROR_UNKNOWN:
5789 EMSG2(_("E117: Unknown function: %s"), name);
5790 break;
5791 case ERROR_TOOMANY:
5792 EMSG2(_(e_toomanyarg), name);
5793 break;
5794 case ERROR_TOOFEW:
5795 EMSG2(_("E119: Not enough arguments for function: %s"),
5796 name);
5797 break;
5798 case ERROR_SCRIPT:
5799 EMSG2(_("E120: Using <SID> not in a script context: %s"),
5800 name);
5801 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005802 case ERROR_DICT:
5803 EMSG2(_("E999: Calling dict function without Dictionary: %s"),
5804 name);
5805 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 }
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
5809 name[len] = cc;
5810 if (fname != name && fname != fname_buf)
5811 vim_free(fname);
5812
5813 return ret;
5814}
5815
5816/*********************************************
5817 * Implementation of the built-in functions
5818 */
5819
5820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005821 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 */
5823 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005824f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005826 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005828 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005830 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833 l = argvars[0].vval.v_list;
5834 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005835 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 }
5837 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00005838 EMSG(_(e_listreq));
5839}
5840
5841/*
5842 * "append(lnum, string/list)" function
5843 */
5844 static void
5845f_append(argvars, rettv)
5846 typeval *argvars;
5847 typeval *rettv;
5848{
5849 long lnum;
5850 listvar *l = NULL;
5851 listitem *li = NULL;
5852 typeval *tv;
5853 long added = 0;
5854
5855 rettv->vval.v_number = 1; /* Default: Failed */
5856 lnum = get_tv_lnum(argvars);
5857 if (lnum >= 0
5858 && lnum <= curbuf->b_ml.ml_line_count
5859 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005861 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005863 l = argvars[1].vval.v_list;
5864 if (l == NULL)
5865 return;
5866 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00005868 for (;;)
5869 {
5870 if (l == NULL)
5871 tv = &argvars[1]; /* append a string */
5872 else if (li == NULL)
5873 break; /* end of list */
5874 else
5875 tv = &li->li_tv; /* append item from list */
5876 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
5877 ++added;
5878 if (l == NULL)
5879 break;
5880 li = li->li_next;
5881 }
5882
5883 appended_lines_mark(lnum, added);
5884 if (curwin->w_cursor.lnum > lnum)
5885 curwin->w_cursor.lnum += added;
5886 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
5888}
5889
5890/*
5891 * "argc()" function
5892 */
5893/* ARGSUSED */
5894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005895f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005897 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005899 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900}
5901
5902/*
5903 * "argidx()" function
5904 */
5905/* ARGSUSED */
5906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005909 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005911 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912}
5913
5914/*
5915 * "argv(nr)" function
5916 */
5917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005918f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005920 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 int idx;
5923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005924 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005926 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005928 rettv->vval.v_string = NULL;
5929 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930}
5931
5932/*
5933 * "browse(save, title, initdir, default)" function
5934 */
5935/* ARGSUSED */
5936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005937f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005939 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941#ifdef FEAT_BROWSE
5942 int save;
5943 char_u *title;
5944 char_u *initdir;
5945 char_u *defname;
5946 char_u buf[NUMBUFLEN];
5947 char_u buf2[NUMBUFLEN];
5948
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005949 save = get_tv_number(&argvars[0]);
5950 title = get_tv_string(&argvars[1]);
5951 initdir = get_tv_string_buf(&argvars[2], buf);
5952 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005954 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005955 do_browse(save ? BROWSE_SAVE : 0,
5956 title, defname, NULL, initdir, NULL, curbuf);
5957#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005959#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005961}
5962
5963/*
5964 * "browsedir(title, initdir)" function
5965 */
5966/* ARGSUSED */
5967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005968f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005970 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005971{
5972#ifdef FEAT_BROWSE
5973 char_u *title;
5974 char_u *initdir;
5975 char_u buf[NUMBUFLEN];
5976
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005977 title = get_tv_string(&argvars[0]);
5978 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005980 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005981 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005983 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005985 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986}
5987
Bram Moolenaar0d660222005-01-07 21:51:51 +00005988static buf_T *find_buffer __ARGS((typeval *avar));
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990/*
5991 * Find a buffer by number or exact name.
5992 */
5993 static buf_T *
5994find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 if (avar->v_type == VAR_NUMBER)
6000 buf = buflist_findnr((int)avar->vval.v_number);
6001 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006004 if (buf == NULL)
6005 {
6006 /* No full path name match, try a match with a URL or a "nofile"
6007 * buffer, these don't use the full path. */
6008 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6009 if (buf->b_fname != NULL
6010 && (path_with_url(buf->b_fname)
6011#ifdef FEAT_QUICKFIX
6012 || bt_nofile(buf)
6013#endif
6014 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006016 break;
6017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
6019 return buf;
6020}
6021
6022/*
6023 * "bufexists(expr)" function
6024 */
6025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006026f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006028 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006030 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031}
6032
6033/*
6034 * "buflisted(expr)" function
6035 */
6036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006037f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006039 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 buf_T *buf;
6042
6043 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006044 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045}
6046
6047/*
6048 * "bufloaded(expr)" function
6049 */
6050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006051f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006053 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 buf_T *buf;
6056
6057 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006058 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059}
6060
Bram Moolenaar0d660222005-01-07 21:51:51 +00006061static buf_T *get_buf_tv __ARGS((typeval *tv));
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063/*
6064 * Get buffer by number or pattern.
6065 */
6066 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006067get_buf_tv(tv)
6068 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006070 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int save_magic;
6072 char_u *save_cpo;
6073 buf_T *buf;
6074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006075 if (tv->v_type == VAR_NUMBER)
6076 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (name == NULL || *name == NUL)
6078 return curbuf;
6079 if (name[0] == '$' && name[1] == NUL)
6080 return lastbuf;
6081
6082 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6083 save_magic = p_magic;
6084 p_magic = TRUE;
6085 save_cpo = p_cpo;
6086 p_cpo = (char_u *)"";
6087
6088 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6089 TRUE, FALSE));
6090
6091 p_magic = save_magic;
6092 p_cpo = save_cpo;
6093
6094 /* If not found, try expanding the name, like done for bufexists(). */
6095 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006096 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098 return buf;
6099}
6100
6101/*
6102 * "bufname(expr)" function
6103 */
6104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006105f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006107 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 buf_T *buf;
6110
6111 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006112 buf = get_buf_tv(&argvars[0]);
6113 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006115 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006117 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 --emsg_off;
6119}
6120
6121/*
6122 * "bufnr(expr)" function
6123 */
6124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006125f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006127 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 buf_T *buf;
6130
6131 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006132 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006134 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 --emsg_off;
6138}
6139
6140/*
6141 * "bufwinnr(nr)" function
6142 */
6143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006144f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006146 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148#ifdef FEAT_WINDOWS
6149 win_T *wp;
6150 int winnr = 0;
6151#endif
6152 buf_T *buf;
6153
6154 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156#ifdef FEAT_WINDOWS
6157 for (wp = firstwin; wp; wp = wp->w_next)
6158 {
6159 ++winnr;
6160 if (wp->w_buffer == buf)
6161 break;
6162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006165 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166#endif
6167 --emsg_off;
6168}
6169
6170/*
6171 * "byte2line(byte)" function
6172 */
6173/*ARGSUSED*/
6174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006175f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181#else
6182 long boff = 0;
6183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006186 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006188 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 (linenr_T)0, &boff);
6190#endif
6191}
6192
6193/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006194 * "byteidx()" function
6195 */
6196/*ARGSUSED*/
6197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006198f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006200 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006201{
6202#ifdef FEAT_MBYTE
6203 char_u *t;
6204#endif
6205 char_u *str;
6206 long idx;
6207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006208 str = get_tv_string(&argvars[0]);
6209 idx = get_tv_number(&argvars[1]);
6210 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006211 if (idx < 0)
6212 return;
6213
6214#ifdef FEAT_MBYTE
6215 t = str;
6216 for ( ; idx > 0; idx--)
6217 {
6218 if (*t == NUL) /* EOL reached */
6219 return;
6220 t += mb_ptr2len_check(t);
6221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006222 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006223#else
6224 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006225 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006226#endif
6227}
6228
6229/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 * "call(func, arglist)" function
6231 */
6232 static void
6233f_call(argvars, rettv)
6234 typeval *argvars;
6235 typeval *rettv;
6236{
6237 char_u *func;
6238 typeval argv[MAX_FUNC_ARGS];
6239 int argc = 0;
6240 listitem *item;
6241 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006242 dictvar *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243
6244 rettv->vval.v_number = 0;
6245 if (argvars[1].v_type != VAR_LIST)
6246 {
6247 EMSG(_(e_listreq));
6248 return;
6249 }
6250 if (argvars[1].vval.v_list == NULL)
6251 return;
6252
6253 if (argvars[0].v_type == VAR_FUNC)
6254 func = argvars[0].vval.v_string;
6255 else
6256 func = get_tv_string(&argvars[0]);
6257
Bram Moolenaare9a41262005-01-15 22:18:47 +00006258 if (argvars[2].v_type != VAR_UNKNOWN)
6259 {
6260 if (argvars[2].v_type != VAR_DICT)
6261 {
6262 EMSG(_(e_dictreq));
6263 return;
6264 }
6265 selfdict = argvars[2].vval.v_dict;
6266 }
6267
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006268 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6269 item = item->li_next)
6270 {
6271 if (argc == MAX_FUNC_ARGS)
6272 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006273 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274 break;
6275 }
6276 /* Make a copy of each argument (is this really needed?) */
6277 copy_tv(&item->li_tv, &argv[argc++]);
6278 }
6279
6280 if (item == NULL)
6281 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6283 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284
6285 /* Free the arguments. */
6286 while (argc > 0)
6287 clear_tv(&argv[--argc]);
6288}
6289
6290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 * "char2nr(string)" function
6292 */
6293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006296 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298#ifdef FEAT_MBYTE
6299 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006300 rettv->vval.v_number =
6301 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 else
6303#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006304 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305}
6306
6307/*
6308 * "cindent(lnum)" function
6309 */
6310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006311f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006313 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
6315#ifdef FEAT_CINDENT
6316 pos_T pos;
6317 linenr_T lnum;
6318
6319 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6322 {
6323 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006324 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 curwin->w_cursor = pos;
6326 }
6327 else
6328#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330}
6331
6332/*
6333 * "col(string)" function
6334 */
6335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006336f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006338 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 colnr_T col = 0;
6341 pos_T *fp;
6342
6343 fp = var2fpos(&argvars[0], FALSE);
6344 if (fp != NULL)
6345 {
6346 if (fp->col == MAXCOL)
6347 {
6348 /* '> can be MAXCOL, get the length of the line then */
6349 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6350 col = STRLEN(ml_get(fp->lnum)) + 1;
6351 else
6352 col = MAXCOL;
6353 }
6354 else
6355 {
6356 col = fp->col + 1;
6357#ifdef FEAT_VIRTUALEDIT
6358 /* col(".") when the cursor is on the NUL at the end of the line
6359 * because of "coladd" can be seen as an extra column. */
6360 if (virtual_active() && fp == &curwin->w_cursor)
6361 {
6362 char_u *p = ml_get_cursor();
6363
6364 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6365 curwin->w_virtcol - curwin->w_cursor.coladd))
6366 {
6367# ifdef FEAT_MBYTE
6368 int l;
6369
6370 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6371 col += l;
6372# else
6373 if (*p != NUL && p[1] == NUL)
6374 ++col;
6375# endif
6376 }
6377 }
6378#endif
6379 }
6380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006381 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382}
6383
6384/*
6385 * "confirm(message, buttons[, default [, type]])" function
6386 */
6387/*ARGSUSED*/
6388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006389f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6394 char_u *message;
6395 char_u *buttons = NULL;
6396 char_u buf[NUMBUFLEN];
6397 char_u buf2[NUMBUFLEN];
6398 int def = 1;
6399 int type = VIM_GENERIC;
6400 int c;
6401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006402 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006405 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 {
6411 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006412 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 switch (TOUPPER_ASC(c))
6414 {
6415 case 'E': type = VIM_ERROR; break;
6416 case 'Q': type = VIM_QUESTION; break;
6417 case 'I': type = VIM_INFO; break;
6418 case 'W': type = VIM_WARNING; break;
6419 case 'G': type = VIM_GENERIC; break;
6420 }
6421 }
6422 }
6423 }
6424
6425 if (buttons == NULL || *buttons == NUL)
6426 buttons = (char_u *)_("&Ok");
6427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006428 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 def, NULL);
6430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432#endif
6433}
6434
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435/*
6436 * "copy()" function
6437 */
6438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006439f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006441 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 * "count()" function
6448 */
6449 static void
6450f_count(argvars, rettv)
6451 typeval *argvars;
6452 typeval *rettv;
6453{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 long n = 0;
6455 int ic = FALSE;
6456
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 listitem *li;
6460 listvar *l;
6461 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006462
Bram Moolenaare9a41262005-01-15 22:18:47 +00006463 if ((l = argvars[0].vval.v_list) != NULL)
6464 {
6465 li = l->lv_first;
6466 if (argvars[2].v_type != VAR_UNKNOWN)
6467 {
6468 ic = get_tv_number(&argvars[2]);
6469 if (argvars[3].v_type != VAR_UNKNOWN)
6470 {
6471 idx = get_tv_number(&argvars[3]);
6472 li = list_find(l, idx);
6473 if (li == NULL)
6474 EMSGN(_(e_listidx), idx);
6475 }
6476 }
6477
6478 for ( ; li != NULL; li = li->li_next)
6479 if (tv_equal(&li->li_tv, &argvars[1], ic))
6480 ++n;
6481 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 else if (argvars[0].v_type == VAR_DICT)
6484 {
6485 if (argvars[0].vval.v_dict != NULL)
6486 {
6487 dictitem *di;
6488
6489 di = argvars[0].vval.v_dict->dv_first;
6490 if (argvars[2].v_type != VAR_UNKNOWN)
6491 {
6492 ic = get_tv_number(&argvars[2]);
6493 if (argvars[3].v_type != VAR_UNKNOWN)
6494 EMSG(_(e_invarg));
6495 }
6496
6497 for ( ; di != NULL; di = di->di_next)
6498 if (tv_equal(&di->di_tv, &argvars[1], ic))
6499 ++n;
6500 }
6501 }
6502 else
6503 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 rettv->vval.v_number = n;
6505}
6506
6507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6509 *
6510 * Checks the existence of a cscope connection.
6511 */
6512/*ARGSUSED*/
6513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006514f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006516 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518#ifdef FEAT_CSCOPE
6519 int num = 0;
6520 char_u *dbpath = NULL;
6521 char_u *prepend = NULL;
6522 char_u buf[NUMBUFLEN];
6523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 if (argvars[0].v_type != VAR_UNKNOWN
6525 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006527 num = (int)get_tv_number(&argvars[0]);
6528 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006530 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536#endif
6537}
6538
6539/*
6540 * "cursor(lnum, col)" function
6541 *
6542 * Moves the cursor to the specified line and column
6543 */
6544/*ARGSUSED*/
6545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 long line, col;
6551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006552 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if (line > 0)
6554 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006555 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (col > 0)
6557 curwin->w_cursor.col = col - 1;
6558#ifdef FEAT_VIRTUALEDIT
6559 curwin->w_cursor.coladd = 0;
6560#endif
6561
6562 /* Make sure the cursor is in a valid position. */
6563 check_cursor();
6564#ifdef FEAT_MBYTE
6565 /* Correct cursor for multi-byte character. */
6566 if (has_mbyte)
6567 mb_adjust_cursor();
6568#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006569
6570 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571}
6572
6573/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 */
6576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006579 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006581 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582}
6583
6584/*
6585 * "delete()" function
6586 */
6587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006588f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006593 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006595 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596}
6597
6598/*
6599 * "did_filetype()" function
6600 */
6601/*ARGSUSED*/
6602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006603f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006605 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006608 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611#endif
6612}
6613
6614/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00006615 * "diff_filler()" function
6616 */
6617/*ARGSUSED*/
6618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006621 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006622{
6623#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00006625#endif
6626}
6627
6628/*
6629 * "diff_hlID()" function
6630 */
6631/*ARGSUSED*/
6632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006633f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006635 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006636{
6637#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00006639 static linenr_T prev_lnum = 0;
6640 static int changedtick = 0;
6641 static int fnum = 0;
6642 static int change_start = 0;
6643 static int change_end = 0;
6644 static enum hlf_value hlID = 0;
6645 int filler_lines;
6646 int col;
6647
6648 if (lnum != prev_lnum
6649 || changedtick != curbuf->b_changedtick
6650 || fnum != curbuf->b_fnum)
6651 {
6652 /* New line, buffer, change: need to get the values. */
6653 filler_lines = diff_check(curwin, lnum);
6654 if (filler_lines < 0)
6655 {
6656 if (filler_lines == -1)
6657 {
6658 change_start = MAXCOL;
6659 change_end = -1;
6660 if (diff_find_change(curwin, lnum, &change_start, &change_end))
6661 hlID = HLF_ADD; /* added line */
6662 else
6663 hlID = HLF_CHD; /* changed line */
6664 }
6665 else
6666 hlID = HLF_ADD; /* added line */
6667 }
6668 else
6669 hlID = (enum hlf_value)0;
6670 prev_lnum = lnum;
6671 changedtick = curbuf->b_changedtick;
6672 fnum = curbuf->b_fnum;
6673 }
6674
6675 if (hlID == HLF_CHD || hlID == HLF_TXD)
6676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006677 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006678 if (col >= change_start && col <= change_end)
6679 hlID = HLF_TXD; /* changed text */
6680 else
6681 hlID = HLF_CHD; /* changed line */
6682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006683 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006684#endif
6685}
6686
6687/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006688 * "empty({expr})" function
6689 */
6690 static void
6691f_empty(argvars, rettv)
6692 typeval *argvars;
6693 typeval *rettv;
6694{
6695 int n;
6696
6697 switch (argvars[0].v_type)
6698 {
6699 case VAR_STRING:
6700 case VAR_FUNC:
6701 n = argvars[0].vval.v_string == NULL
6702 || *argvars[0].vval.v_string == NUL;
6703 break;
6704 case VAR_NUMBER:
6705 n = argvars[0].vval.v_number == 0;
6706 break;
6707 case VAR_LIST:
6708 n = argvars[0].vval.v_list == NULL
6709 || argvars[0].vval.v_list->lv_first == NULL;
6710 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 case VAR_DICT:
6712 n = argvars[0].vval.v_dict == NULL
6713 || argvars[0].vval.v_dict->dv_first == NULL;
6714 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006715 default:
6716 EMSG2(_(e_intern2), "f_empty()");
6717 n = 0;
6718 }
6719
6720 rettv->vval.v_number = n;
6721}
6722
6723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 * "escape({string}, {chars})" function
6725 */
6726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006727f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006729 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 char_u buf[NUMBUFLEN];
6732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006733 rettv->vval.v_string =
6734 vim_strsave_escaped(get_tv_string(&argvars[0]),
6735 get_tv_string_buf(&argvars[1], buf));
6736 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737}
6738
6739/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740 * "eval()" function
6741 */
6742/*ARGSUSED*/
6743 static void
6744f_eval(argvars, rettv)
6745 typeval *argvars;
6746 typeval *rettv;
6747{
6748 char_u *s;
6749
6750 s = get_tv_string(&argvars[0]);
6751 s = skipwhite(s);
6752
6753 if (eval1(&s, rettv, TRUE) == FAIL)
6754 rettv->vval.v_number = 0;
6755 else if (*s != NUL)
6756 EMSG(_(e_trailing));
6757}
6758
6759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 * "eventhandler()" function
6761 */
6762/*ARGSUSED*/
6763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006764f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006766 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006768 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769}
6770
6771/*
6772 * "executable()" function
6773 */
6774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006777 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006779 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780}
6781
6782/*
6783 * "exists()" function
6784 */
6785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006786f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006787 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006788 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 char_u *p;
6791 char_u *name;
6792 int n = FALSE;
6793 int len = 0;
6794
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if (*p == '$') /* environment variable */
6797 {
6798 /* first try "normal" environment variables (fast) */
6799 if (mch_getenv(p + 1) != NULL)
6800 n = TRUE;
6801 else
6802 {
6803 /* try expanding things like $VIM and ${HOME} */
6804 p = expand_env_save(p);
6805 if (p != NULL && *p != '$')
6806 n = TRUE;
6807 vim_free(p);
6808 }
6809 }
6810 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006811 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 else if (*p == '*') /* internal or user defined function */
6813 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816 else if (*p == ':')
6817 {
6818 n = cmd_exists(p + 1);
6819 }
6820 else if (*p == '#')
6821 {
6822#ifdef FEAT_AUTOCMD
6823 name = p + 1;
6824 p = vim_strchr(name, '#');
6825 if (p != NULL)
6826 n = au_exists(name, p, p + 1);
6827 else
6828 n = au_exists(name, name + STRLEN(name), NULL);
6829#endif
6830 }
6831 else /* internal variable */
6832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 char_u *expr_start;
6834 char_u *expr_end;
6835 char_u *temp_string = NULL;
6836 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 name = p;
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006840 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (expr_start != NULL)
6842 {
6843 temp_string = make_expanded_name(name, expr_start, expr_end, s);
6844 if (temp_string != NULL)
6845 {
6846 len = STRLEN(temp_string);
6847 name = temp_string;
6848 }
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 if (len == 0)
6851 len = get_id_len(&p);
6852 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006853 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 }
6857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859}
6860
6861/*
6862 * "expand()" function
6863 */
6864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006865f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869 char_u *s;
6870 int len;
6871 char_u *errormsg;
6872 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
6873 expand_T xpc;
6874
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006875 rettv->v_type = VAR_STRING;
6876 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 if (*s == '%' || *s == '#' || *s == '<')
6878 {
6879 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006880 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 --emsg_off;
6882 }
6883 else
6884 {
6885 /* When the optional second argument is non-zero, don't remove matches
6886 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 flags |= WILD_KEEP_ALL;
6889 ExpandInit(&xpc);
6890 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006891 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 ExpandCleanup(&xpc);
6893 }
6894}
6895
6896/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006897 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006899 */
6900 static void
6901f_extend(argvars, rettv)
6902 typeval *argvars;
6903 typeval *rettv;
6904{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006905 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006906 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006907 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006908 listvar *l1, *l2;
6909 listitem *item;
6910 long before;
6911 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006912
Bram Moolenaare9a41262005-01-15 22:18:47 +00006913 l1 = argvars[0].vval.v_list;
6914 l2 = argvars[1].vval.v_list;
6915 if (l1 != NULL && l2 != NULL)
6916 {
6917 if (argvars[2].v_type != VAR_UNKNOWN)
6918 {
6919 n = before = get_tv_number(&argvars[2]);
6920 item = list_find_ext(l1, &n);
6921 if (n != 0)
6922 {
6923 EMSGN(_(e_listidx), before);
6924 return;
6925 }
6926 }
6927 else
6928 item = NULL;
6929 list_extend(l1, l2, item);
6930
6931 ++l1->lv_refcount;
6932 copy_tv(&argvars[0], rettv);
6933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006935 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
6936 {
6937 dictvar *d1, *d2;
6938 dictitem *d1i, *d2i;
6939 char_u *action;
6940 int i;
6941
6942 d1 = argvars[0].vval.v_dict;
6943 d2 = argvars[1].vval.v_dict;
6944 if (d1 != NULL && d2 != NULL)
6945 {
6946 /* Check the third argument. */
6947 if (argvars[2].v_type != VAR_UNKNOWN)
6948 {
6949 static char *(av[]) = {"keep", "force", "error"};
6950
6951 action = get_tv_string(&argvars[2]);
6952 for (i = 0; i < 3; ++i)
6953 if (STRCMP(action, av[i]) == 0)
6954 break;
6955 if (i == 3)
6956 {
6957 EMSGN(_(e_invarg2), action);
6958 return;
6959 }
6960 }
6961 else
6962 action = (char_u *)"force";
6963
6964 /* Go over all entries in the second dict and add them to the
6965 * first dict. */
6966 for (d2i = d2->dv_first; d2i != NULL; d2i = d2i->di_next)
6967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006968 d1i = dict_find(d1, d2i->di_key, -1, NULL);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 if (d1i == NULL)
6970 {
6971 d1i = dictitem_copy(d2i);
6972 if (d1i != NULL)
6973 dict_add(d1, d1i);
6974 }
6975 else if (*action == 'e')
6976 {
6977 EMSG2(_("Key already exists: %s"), d2i->di_key);
6978 break;
6979 }
6980 else if (*action == 'f')
6981 {
6982 clear_tv(&d1i->di_tv);
6983 copy_tv(&d2i->di_tv, &d1i->di_tv);
6984 }
6985 }
6986
6987 ++d1->dv_refcount;
6988 copy_tv(&argvars[0], rettv);
6989 }
6990 }
6991 else
6992 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006993}
6994
6995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 * "filereadable()" function
6997 */
6998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006999f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007000 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007001 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002{
7003 FILE *fd;
7004 char_u *p;
7005 int n;
7006
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007007 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7009 {
7010 n = TRUE;
7011 fclose(fd);
7012 }
7013 else
7014 n = FALSE;
7015
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007016 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017}
7018
7019/*
7020 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7021 * rights to write into.
7022 */
7023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007024f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007025 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007026 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 char_u *p;
7029 int retval = 0;
7030#if defined(UNIX) || defined(VMS)
7031 int perm = 0;
7032#endif
7033
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007034 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035#if defined(UNIX) || defined(VMS)
7036 perm = mch_getperm(p);
7037#endif
7038#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7039 if (
7040# ifdef WIN3264
7041 mch_writable(p) &&
7042# else
7043# if defined(UNIX) || defined(VMS)
7044 (perm & 0222) &&
7045# endif
7046# endif
7047 mch_access((char *)p, W_OK) == 0
7048 )
7049#endif
7050 {
7051 ++retval;
7052 if (mch_isdir(p))
7053 ++retval;
7054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007055 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056}
7057
Bram Moolenaar0d660222005-01-07 21:51:51 +00007058static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007059
7060 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007061findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007062 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007063 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007064 int dir;
7065{
7066#ifdef FEAT_SEARCHPATH
7067 char_u *fname;
7068 char_u *fresult = NULL;
7069 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7070 char_u *p;
7071 char_u pathbuf[NUMBUFLEN];
7072 int count = 1;
7073 int first = TRUE;
7074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007076
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007077 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007079 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007080 if (*p != NUL)
7081 path = p;
7082
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007085 }
7086
7087 do
7088 {
7089 vim_free(fresult);
7090 fresult = find_file_in_path_option(first ? fname : NULL,
7091 first ? (int)STRLEN(fname) : 0,
7092 0, first, path, dir, NULL);
7093 first = FALSE;
7094 } while (--count > 0 && fresult != NULL);
7095
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007096 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007097#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007098 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007099#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007101}
7102
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007103static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104static int filter_map_one __ARGS((typeval *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007105
7106/*
7107 * Implementation of map() and filter().
7108 */
7109 static void
7110filter_map(argvars, rettv, map)
7111 typeval *argvars;
7112 typeval *rettv;
7113 int map;
7114{
7115 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007116 char_u *expr;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007117 listitem *li, *nli;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 listvar *l = NULL;
7119 dictitem *di, **pdi;
7120 dictvar *d = NULL;
7121 typeval save_val;
7122 typeval save_key;
7123 int rem;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007124
7125 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007127 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128 if ((l = argvars[0].vval.v_list) == NULL)
7129 return;
7130 }
7131 else if (argvars[0].v_type == VAR_DICT)
7132 {
7133 if ((d = argvars[0].vval.v_dict) == NULL)
7134 return;
7135 }
7136 else
7137 {
7138 EMSG2(_(e_listdictarg), map ? "map()" : "filter()");
7139 return;
7140 }
7141
7142 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
7143 save_val = vimvars[VV_VAL].tv;
7144
7145 if (argvars[0].v_type == VAR_DICT)
7146 {
7147 save_key = vimvars[VV_KEY].tv;
7148 vimvars[VV_KEY].tv.v_type = VAR_STRING;
7149 pdi = &d->dv_first;
7150 for (di = d->dv_first; di != NULL; di = *pdi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007151 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 vimvars[VV_KEY].tv.vval.v_string = vim_strsave(di->di_key);
7153 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007154 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 if (!map && rem)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007156 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 *pdi = di->di_next;
7158 dictitem_free(di);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007159 }
7160 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161 pdi = &di->di_next;
7162 clear_tv(&vimvars[VV_KEY].tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 clear_tv(&vimvars[VV_KEY].tv);
7165 vimvars[VV_KEY].tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 else
7168 {
7169 for (li = l->lv_first; li != NULL; li = nli)
7170 {
7171 nli = li->li_next;
7172 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7173 break;
7174 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007175 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 }
7177 }
7178
7179 clear_tv(&vimvars[VV_VAL].tv);
7180 vimvars[VV_VAL].tv = save_val;
7181
7182 copy_tv(&argvars[0], rettv);
7183}
7184
7185 static int
7186filter_map_one(tv, expr, map, remp)
7187 typeval *tv;
7188 char_u *expr;
7189 int map;
7190 int *remp;
7191{
7192 typeval rettv;
7193 char_u *s;
7194
7195 copy_tv(tv, &vimvars[VV_VAL].tv);
7196 s = expr;
7197 if (eval1(&s, &rettv, TRUE) == FAIL)
7198 return FAIL;
7199 if (*s != NUL) /* check for trailing chars after expr */
7200 {
7201 EMSG2(_(e_invexpr2), s);
7202 return FAIL;
7203 }
7204 if (map)
7205 {
7206 /* map(): replace the list item value */
7207 clear_tv(tv);
7208 *tv = rettv;
7209 }
7210 else
7211 {
7212 /* filter(): when expr is zero remove the item */
7213 *remp = (get_tv_number(&rettv) == 0);
7214 clear_tv(&rettv);
7215 }
7216 clear_tv(&vimvars[VV_VAL].tv);
7217 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007218}
7219
7220/*
7221 * "filter()" function
7222 */
7223 static void
7224f_filter(argvars, rettv)
7225 typeval *argvars;
7226 typeval *rettv;
7227{
7228 filter_map(argvars, rettv, FALSE);
7229}
7230
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007232 * "finddir({fname}[, {path}[, {count}]])" function
7233 */
7234 static void
7235f_finddir(argvars, rettv)
7236 typeval *argvars;
7237 typeval *rettv;
7238{
7239 findfilendir(argvars, rettv, TRUE);
7240}
7241
7242/*
7243 * "findfile({fname}[, {path}[, {count}]])" function
7244 */
7245 static void
7246f_findfile(argvars, rettv)
7247 typeval *argvars;
7248 typeval *rettv;
7249{
7250 findfilendir(argvars, rettv, FALSE);
7251}
7252
7253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 * "fnamemodify({fname}, {mods})" function
7255 */
7256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007257f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007258 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007259 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 char_u *fname;
7262 char_u *mods;
7263 int usedlen = 0;
7264 int len;
7265 char_u *fbuf = NULL;
7266 char_u buf[NUMBUFLEN];
7267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007268 fname = get_tv_string(&argvars[0]);
7269 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 len = (int)STRLEN(fname);
7271
7272 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007274 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 vim_free(fbuf);
7280}
7281
Bram Moolenaar0d660222005-01-07 21:51:51 +00007282static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284/*
7285 * "foldclosed()" function
7286 */
7287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007288foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007289 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007290 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 int end;
7292{
7293#ifdef FEAT_FOLDING
7294 linenr_T lnum;
7295 linenr_T first, last;
7296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007297 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7299 {
7300 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7301 {
7302 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007303 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 return;
7307 }
7308 }
7309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311}
7312
7313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007314 * "foldclosed()" function
7315 */
7316 static void
7317f_foldclosed(argvars, rettv)
7318 typeval *argvars;
7319 typeval *rettv;
7320{
7321 foldclosed_both(argvars, rettv, FALSE);
7322}
7323
7324/*
7325 * "foldclosedend()" function
7326 */
7327 static void
7328f_foldclosedend(argvars, rettv)
7329 typeval *argvars;
7330 typeval *rettv;
7331{
7332 foldclosed_both(argvars, rettv, TRUE);
7333}
7334
7335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 * "foldlevel()" function
7337 */
7338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007339f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007341 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343#ifdef FEAT_FOLDING
7344 linenr_T lnum;
7345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007348 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 else
7350#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007351 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352}
7353
7354/*
7355 * "foldtext()" function
7356 */
7357/*ARGSUSED*/
7358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363#ifdef FEAT_FOLDING
7364 linenr_T lnum;
7365 char_u *s;
7366 char_u *r;
7367 int len;
7368 char *txt;
7369#endif
7370
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007371 rettv->v_type = VAR_STRING;
7372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7375 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7376 <= curbuf->b_ml.ml_line_count
7377 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
7379 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7381 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
7383 if (!linewhite(lnum))
7384 break;
7385 ++lnum;
7386 }
7387
7388 /* Find interesting text in this line. */
7389 s = skipwhite(ml_get(lnum));
7390 /* skip C comment-start */
7391 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007394 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007396 {
7397 s = skipwhite(ml_get(lnum + 1));
7398 if (*s == '*')
7399 s = skipwhite(s + 1);
7400 }
7401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 txt = _("+-%s%3ld lines: ");
7403 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 + 20 /* for %3ld */
7406 + STRLEN(s))); /* concatenated */
7407 if (r != NULL)
7408 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7410 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7411 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 len = (int)STRLEN(r);
7413 STRCAT(r, s);
7414 /* remove 'foldmarker' and 'commentstring' */
7415 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007416 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
7418 }
7419#endif
7420}
7421
7422/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007423 * "foldtextresult(lnum)" function
7424 */
7425/*ARGSUSED*/
7426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007427f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007428 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007430{
7431#ifdef FEAT_FOLDING
7432 linenr_T lnum;
7433 char_u *text;
7434 char_u buf[51];
7435 foldinfo_T foldinfo;
7436 int fold_count;
7437#endif
7438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439 rettv->v_type = VAR_STRING;
7440 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007441#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007443 fold_count = foldedCount(curwin, lnum, &foldinfo);
7444 if (fold_count > 0)
7445 {
7446 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7447 &foldinfo, buf);
7448 if (text == buf)
7449 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007451 }
7452#endif
7453}
7454
7455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 * "foreground()" function
7457 */
7458/*ARGSUSED*/
7459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465#ifdef FEAT_GUI
7466 if (gui.in_use)
7467 gui_mch_set_foreground();
7468#else
7469# ifdef WIN32
7470 win32_set_foreground();
7471# endif
7472#endif
7473}
7474
7475/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007476 * "function()" function
7477 */
7478/*ARGSUSED*/
7479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483{
7484 char_u *s;
7485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007487 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007488 EMSG2(_(e_invarg2), s);
7489 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007490 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007491 else
7492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007493 rettv->vval.v_string = vim_strsave(s);
7494 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007495 }
7496}
7497
7498/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007499 * "get()" function
7500 */
7501 static void
7502f_get(argvars, rettv)
7503 typeval *argvars;
7504 typeval *rettv;
7505{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 listitem *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007507 listvar *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 dictitem *di;
7509 dictvar *d;
7510 typeval *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007511
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007513 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007514 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007515 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516 li = list_find(l, get_tv_number(&argvars[1]));
7517 if (li != NULL)
7518 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00007520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007521 else if (argvars[0].v_type == VAR_DICT)
7522 {
7523 if ((d = argvars[0].vval.v_dict) != NULL)
7524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007525 di = dict_find(d, get_tv_string(&argvars[1]), -1, NULL);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007526 if (di != NULL)
7527 tv = &di->di_tv;
7528 }
7529 }
7530 else
7531 EMSG2(_(e_listdictarg), "get()");
7532
7533 if (tv == NULL)
7534 {
7535 if (argvars[2].v_type == VAR_UNKNOWN)
7536 rettv->vval.v_number = 0;
7537 else
7538 copy_tv(&argvars[2], rettv);
7539 }
7540 else
7541 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007542}
7543
7544/*
7545 * "getbufvar()" function
7546 */
7547 static void
7548f_getbufvar(argvars, rettv)
7549 typeval *argvars;
7550 typeval *rettv;
7551{
7552 buf_T *buf;
7553 buf_T *save_curbuf;
7554 char_u *varname;
7555 VAR v;
7556
7557 ++emsg_off;
7558 buf = get_buf_tv(&argvars[0]);
7559 varname = get_tv_string(&argvars[1]);
7560
7561 rettv->v_type = VAR_STRING;
7562 rettv->vval.v_string = NULL;
7563
7564 if (buf != NULL && varname != NULL)
7565 {
7566 if (*varname == '&') /* buffer-local-option */
7567 {
7568 /* set curbuf to be our buf, temporarily */
7569 save_curbuf = curbuf;
7570 curbuf = buf;
7571
7572 get_option_tv(&varname, rettv, TRUE);
7573
7574 /* restore previous notion of curbuf */
7575 curbuf = save_curbuf;
7576 }
7577 else
7578 {
7579 /* look up the variable */
7580 v = find_var_in_ga(&buf->b_vars, varname);
7581 if (v != NULL)
7582 copy_tv(&v->tv, rettv);
7583 }
7584 }
7585
7586 --emsg_off;
7587}
7588
7589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 * "getchar()" function
7591 */
7592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007594 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 varnumber_T n;
7598
7599 ++no_mapping;
7600 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 /* getchar(): blocking wait. */
7603 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 /* getchar(1): only check if char avail */
7606 n = vpeekc();
7607 else if (vpeekc() == NUL)
7608 /* getchar(0) and no char avail: return zero */
7609 n = 0;
7610 else
7611 /* getchar(0) and char avail: return char */
7612 n = safe_vgetc();
7613 --no_mapping;
7614 --allow_keys;
7615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (IS_SPECIAL(n) || mod_mask != 0)
7618 {
7619 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
7620 int i = 0;
7621
7622 /* Turn a special key into three bytes, plus modifier. */
7623 if (mod_mask != 0)
7624 {
7625 temp[i++] = K_SPECIAL;
7626 temp[i++] = KS_MODIFIER;
7627 temp[i++] = mod_mask;
7628 }
7629 if (IS_SPECIAL(n))
7630 {
7631 temp[i++] = K_SPECIAL;
7632 temp[i++] = K_SECOND(n);
7633 temp[i++] = K_THIRD(n);
7634 }
7635#ifdef FEAT_MBYTE
7636 else if (has_mbyte)
7637 i += (*mb_char2bytes)(n, temp + i);
7638#endif
7639 else
7640 temp[i++] = n;
7641 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 rettv->v_type = VAR_STRING;
7643 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645}
7646
7647/*
7648 * "getcharmod()" function
7649 */
7650/*ARGSUSED*/
7651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007653 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657}
7658
7659/*
7660 * "getcmdline()" function
7661 */
7662/*ARGSUSED*/
7663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 rettv->v_type = VAR_STRING;
7669 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670}
7671
7672/*
7673 * "getcmdpos()" function
7674 */
7675/*ARGSUSED*/
7676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007677f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007678 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682}
7683
7684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 * "getcwd()" function
7686 */
7687/*ARGSUSED*/
7688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 char_u cwd[MAXPATHL];
7694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 else
7699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007700 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703#endif
7704 }
7705}
7706
7707/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007708 * "getfontname()" function
7709 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007710/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007715{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->v_type = VAR_STRING;
7717 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007718#ifdef FEAT_GUI
7719 if (gui.in_use)
7720 {
7721 GuiFont font;
7722 char_u *name = NULL;
7723
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007725 {
7726 /* Get the "Normal" font. Either the name saved by
7727 * hl_set_font_name() or from the font ID. */
7728 font = gui.norm_font;
7729 name = hl_get_font_name();
7730 }
7731 else
7732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007734 if (STRCMP(name, "*") == 0) /* don't use font dialog */
7735 return;
7736 font = gui_mch_get_font(name, FALSE);
7737 if (font == NOFONT)
7738 return; /* Invalid font name, return empty string. */
7739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007741 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007742 gui_mch_free_font(font);
7743 }
7744#endif
7745}
7746
7747/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007748 * "getfperm({fname})" function
7749 */
7750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007754{
7755 char_u *fname;
7756 struct stat st;
7757 char_u *perm = NULL;
7758 char_u flags[] = "rwx";
7759 int i;
7760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007764 if (mch_stat((char *)fname, &st) >= 0)
7765 {
7766 perm = vim_strsave((char_u *)"---------");
7767 if (perm != NULL)
7768 {
7769 for (i = 0; i < 9; i++)
7770 {
7771 if (st.st_mode & (1 << (8 - i)))
7772 perm[i] = flags[i % 3];
7773 }
7774 }
7775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007777}
7778
7779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 * "getfsize({fname})" function
7781 */
7782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007784 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 char_u *fname;
7788 struct stat st;
7789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007790 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 if (mch_stat((char *)fname, &st) >= 0)
7795 {
7796 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 }
7801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804
7805/*
7806 * "getftime({fname})" function
7807 */
7808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 char_u *fname;
7814 struct stat st;
7815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
7818 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007819 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
7824/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007825 * "getftype({fname})" function
7826 */
7827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007831{
7832 char_u *fname;
7833 struct stat st;
7834 char_u *type = NULL;
7835 char *t;
7836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007840 if (mch_lstat((char *)fname, &st) >= 0)
7841 {
7842#ifdef S_ISREG
7843 if (S_ISREG(st.st_mode))
7844 t = "file";
7845 else if (S_ISDIR(st.st_mode))
7846 t = "dir";
7847# ifdef S_ISLNK
7848 else if (S_ISLNK(st.st_mode))
7849 t = "link";
7850# endif
7851# ifdef S_ISBLK
7852 else if (S_ISBLK(st.st_mode))
7853 t = "bdev";
7854# endif
7855# ifdef S_ISCHR
7856 else if (S_ISCHR(st.st_mode))
7857 t = "cdev";
7858# endif
7859# ifdef S_ISFIFO
7860 else if (S_ISFIFO(st.st_mode))
7861 t = "fifo";
7862# endif
7863# ifdef S_ISSOCK
7864 else if (S_ISSOCK(st.st_mode))
7865 t = "fifo";
7866# endif
7867 else
7868 t = "other";
7869#else
7870# ifdef S_IFMT
7871 switch (st.st_mode & S_IFMT)
7872 {
7873 case S_IFREG: t = "file"; break;
7874 case S_IFDIR: t = "dir"; break;
7875# ifdef S_IFLNK
7876 case S_IFLNK: t = "link"; break;
7877# endif
7878# ifdef S_IFBLK
7879 case S_IFBLK: t = "bdev"; break;
7880# endif
7881# ifdef S_IFCHR
7882 case S_IFCHR: t = "cdev"; break;
7883# endif
7884# ifdef S_IFIFO
7885 case S_IFIFO: t = "fifo"; break;
7886# endif
7887# ifdef S_IFSOCK
7888 case S_IFSOCK: t = "socket"; break;
7889# endif
7890 default: t = "other";
7891 }
7892# else
7893 if (mch_isdir(fname))
7894 t = "dir";
7895 else
7896 t = "file";
7897# endif
7898#endif
7899 type = vim_strsave((char_u *)t);
7900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007902}
7903
7904/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007905 * "getline(lnum)" function
7906 */
7907 static void
7908f_getline(argvars, rettv)
7909 typeval *argvars;
7910 typeval *rettv;
7911{
7912 linenr_T lnum;
7913 linenr_T end;
7914 char_u *p;
7915 listvar *l;
7916 listitem *li;
7917
7918 lnum = get_tv_lnum(argvars);
7919
7920 if (argvars[1].v_type == VAR_UNKNOWN)
7921 {
7922 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7923 p = ml_get(lnum);
7924 else
7925 p = (char_u *)"";
7926
7927 rettv->v_type = VAR_STRING;
7928 rettv->vval.v_string = vim_strsave(p);
7929 }
7930 else
7931 {
7932 end = get_tv_lnum(&argvars[1]);
7933 if (end < lnum)
7934 {
7935 EMSG(_(e_invrange));
7936 rettv->vval.v_number = 0;
7937 }
7938 else
7939 {
7940 l = list_alloc();
7941 if (l != NULL)
7942 {
7943 if (lnum < 1)
7944 lnum = 1;
7945 if (end > curbuf->b_ml.ml_line_count)
7946 end = curbuf->b_ml.ml_line_count;
7947 while (lnum <= end)
7948 {
7949 li = listitem_alloc();
7950 if (li == NULL)
7951 break;
7952 list_append(l, li);
7953 li->li_tv.v_type = VAR_STRING;
7954 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
7955 }
7956 rettv->vval.v_list = l;
7957 rettv->v_type = VAR_LIST;
7958 ++l->lv_refcount;
7959 }
7960 }
7961 }
7962}
7963
7964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 * "getreg()" function
7966 */
7967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007969 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971{
7972 char_u *strregname;
7973 int regname;
7974
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 regname = (strregname == NULL ? '"' : *strregname);
7980 if (regname == 0)
7981 regname = '"';
7982
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983 rettv->v_type = VAR_STRING;
7984 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985}
7986
7987/*
7988 * "getregtype()" function
7989 */
7990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007991f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007992 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 char_u *strregname;
7996 int regname;
7997 char_u buf[NUMBUFLEN + 2];
7998 long reglen = 0;
7999
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008000 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 else
8003 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
8006 regname = (strregname == NULL ? '"' : *strregname);
8007 if (regname == 0)
8008 regname = '"';
8009
8010 buf[0] = NUL;
8011 buf[1] = NUL;
8012 switch (get_reg_type(regname, &reglen))
8013 {
8014 case MLINE: buf[0] = 'V'; break;
8015 case MCHAR: buf[0] = 'v'; break;
8016#ifdef FEAT_VISUAL
8017 case MBLOCK:
8018 buf[0] = Ctrl_V;
8019 sprintf((char *)buf + 1, "%ld", reglen + 1);
8020 break;
8021#endif
8022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 rettv->v_type = VAR_STRING;
8024 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025}
8026
8027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 * "getwinposx()" function
8029 */
8030/*ARGSUSED*/
8031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008033 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#ifdef FEAT_GUI
8038 if (gui.in_use)
8039 {
8040 int x, y;
8041
8042 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045#endif
8046}
8047
8048/*
8049 * "getwinposy()" function
8050 */
8051/*ARGSUSED*/
8052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008053f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008057 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058#ifdef FEAT_GUI
8059 if (gui.in_use)
8060 {
8061 int x, y;
8062
8063 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066#endif
8067}
8068
8069/*
8070 * "getwinvar()" function
8071 */
8072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008075 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 win_T *win, *oldcurwin;
8078 char_u *varname;
8079 VAR v;
8080
8081 ++emsg_off;
8082 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008085 rettv->v_type = VAR_STRING;
8086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
8088 if (win != NULL && varname != NULL)
8089 {
8090 if (*varname == '&') /* window-local-option */
8091 {
8092 /* set curwin to be our win, temporarily */
8093 oldcurwin = curwin;
8094 curwin = win;
8095
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008096 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
8098 /* restore previous notion of curwin */
8099 curwin = oldcurwin;
8100 }
8101 else
8102 {
8103 /* look up the variable */
8104 v = find_var_in_ga(&win->w_vars, varname);
8105 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008106 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 }
8109
8110 --emsg_off;
8111}
8112
8113/*
8114 * "glob()" function
8115 */
8116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008117f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008118 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008119 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 expand_T xpc;
8122
8123 ExpandInit(&xpc);
8124 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008125 rettv->v_type = VAR_STRING;
8126 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8128 ExpandCleanup(&xpc);
8129}
8130
8131/*
8132 * "globpath()" function
8133 */
8134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
8139 char_u buf1[NUMBUFLEN];
8140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 rettv->v_type = VAR_STRING;
8142 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8143 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144}
8145
8146/*
8147 * "has()" function
8148 */
8149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008150f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
8154 int i;
8155 char_u *name;
8156 int n = FALSE;
8157 static char *(has_list[]) =
8158 {
8159#ifdef AMIGA
8160 "amiga",
8161# ifdef FEAT_ARP
8162 "arp",
8163# endif
8164#endif
8165#ifdef __BEOS__
8166 "beos",
8167#endif
8168#ifdef MSDOS
8169# ifdef DJGPP
8170 "dos32",
8171# else
8172 "dos16",
8173# endif
8174#endif
8175#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8176 "mac",
8177#endif
8178#if defined(MACOS_X_UNIX)
8179 "macunix",
8180#endif
8181#ifdef OS2
8182 "os2",
8183#endif
8184#ifdef __QNX__
8185 "qnx",
8186#endif
8187#ifdef RISCOS
8188 "riscos",
8189#endif
8190#ifdef UNIX
8191 "unix",
8192#endif
8193#ifdef VMS
8194 "vms",
8195#endif
8196#ifdef WIN16
8197 "win16",
8198#endif
8199#ifdef WIN32
8200 "win32",
8201#endif
8202#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8203 "win32unix",
8204#endif
8205#ifdef WIN64
8206 "win64",
8207#endif
8208#ifdef EBCDIC
8209 "ebcdic",
8210#endif
8211#ifndef CASE_INSENSITIVE_FILENAME
8212 "fname_case",
8213#endif
8214#ifdef FEAT_ARABIC
8215 "arabic",
8216#endif
8217#ifdef FEAT_AUTOCMD
8218 "autocmd",
8219#endif
8220#ifdef FEAT_BEVAL
8221 "balloon_eval",
8222#endif
8223#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8224 "builtin_terms",
8225# ifdef ALL_BUILTIN_TCAPS
8226 "all_builtin_terms",
8227# endif
8228#endif
8229#ifdef FEAT_BYTEOFF
8230 "byte_offset",
8231#endif
8232#ifdef FEAT_CINDENT
8233 "cindent",
8234#endif
8235#ifdef FEAT_CLIENTSERVER
8236 "clientserver",
8237#endif
8238#ifdef FEAT_CLIPBOARD
8239 "clipboard",
8240#endif
8241#ifdef FEAT_CMDL_COMPL
8242 "cmdline_compl",
8243#endif
8244#ifdef FEAT_CMDHIST
8245 "cmdline_hist",
8246#endif
8247#ifdef FEAT_COMMENTS
8248 "comments",
8249#endif
8250#ifdef FEAT_CRYPT
8251 "cryptv",
8252#endif
8253#ifdef FEAT_CSCOPE
8254 "cscope",
8255#endif
8256#ifdef DEBUG
8257 "debug",
8258#endif
8259#ifdef FEAT_CON_DIALOG
8260 "dialog_con",
8261#endif
8262#ifdef FEAT_GUI_DIALOG
8263 "dialog_gui",
8264#endif
8265#ifdef FEAT_DIFF
8266 "diff",
8267#endif
8268#ifdef FEAT_DIGRAPHS
8269 "digraphs",
8270#endif
8271#ifdef FEAT_DND
8272 "dnd",
8273#endif
8274#ifdef FEAT_EMACS_TAGS
8275 "emacs_tags",
8276#endif
8277 "eval", /* always present, of course! */
8278#ifdef FEAT_EX_EXTRA
8279 "ex_extra",
8280#endif
8281#ifdef FEAT_SEARCH_EXTRA
8282 "extra_search",
8283#endif
8284#ifdef FEAT_FKMAP
8285 "farsi",
8286#endif
8287#ifdef FEAT_SEARCHPATH
8288 "file_in_path",
8289#endif
8290#ifdef FEAT_FIND_ID
8291 "find_in_path",
8292#endif
8293#ifdef FEAT_FOLDING
8294 "folding",
8295#endif
8296#ifdef FEAT_FOOTER
8297 "footer",
8298#endif
8299#if !defined(USE_SYSTEM) && defined(UNIX)
8300 "fork",
8301#endif
8302#ifdef FEAT_GETTEXT
8303 "gettext",
8304#endif
8305#ifdef FEAT_GUI
8306 "gui",
8307#endif
8308#ifdef FEAT_GUI_ATHENA
8309# ifdef FEAT_GUI_NEXTAW
8310 "gui_neXtaw",
8311# else
8312 "gui_athena",
8313# endif
8314#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008315#ifdef FEAT_GUI_KDE
8316 "gui_kde",
8317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318#ifdef FEAT_GUI_GTK
8319 "gui_gtk",
8320# ifdef HAVE_GTK2
8321 "gui_gtk2",
8322# endif
8323#endif
8324#ifdef FEAT_GUI_MAC
8325 "gui_mac",
8326#endif
8327#ifdef FEAT_GUI_MOTIF
8328 "gui_motif",
8329#endif
8330#ifdef FEAT_GUI_PHOTON
8331 "gui_photon",
8332#endif
8333#ifdef FEAT_GUI_W16
8334 "gui_win16",
8335#endif
8336#ifdef FEAT_GUI_W32
8337 "gui_win32",
8338#endif
8339#ifdef FEAT_HANGULIN
8340 "hangul_input",
8341#endif
8342#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8343 "iconv",
8344#endif
8345#ifdef FEAT_INS_EXPAND
8346 "insert_expand",
8347#endif
8348#ifdef FEAT_JUMPLIST
8349 "jumplist",
8350#endif
8351#ifdef FEAT_KEYMAP
8352 "keymap",
8353#endif
8354#ifdef FEAT_LANGMAP
8355 "langmap",
8356#endif
8357#ifdef FEAT_LIBCALL
8358 "libcall",
8359#endif
8360#ifdef FEAT_LINEBREAK
8361 "linebreak",
8362#endif
8363#ifdef FEAT_LISP
8364 "lispindent",
8365#endif
8366#ifdef FEAT_LISTCMDS
8367 "listcmds",
8368#endif
8369#ifdef FEAT_LOCALMAP
8370 "localmap",
8371#endif
8372#ifdef FEAT_MENU
8373 "menu",
8374#endif
8375#ifdef FEAT_SESSION
8376 "mksession",
8377#endif
8378#ifdef FEAT_MODIFY_FNAME
8379 "modify_fname",
8380#endif
8381#ifdef FEAT_MOUSE
8382 "mouse",
8383#endif
8384#ifdef FEAT_MOUSESHAPE
8385 "mouseshape",
8386#endif
8387#if defined(UNIX) || defined(VMS)
8388# ifdef FEAT_MOUSE_DEC
8389 "mouse_dec",
8390# endif
8391# ifdef FEAT_MOUSE_GPM
8392 "mouse_gpm",
8393# endif
8394# ifdef FEAT_MOUSE_JSB
8395 "mouse_jsbterm",
8396# endif
8397# ifdef FEAT_MOUSE_NET
8398 "mouse_netterm",
8399# endif
8400# ifdef FEAT_MOUSE_PTERM
8401 "mouse_pterm",
8402# endif
8403# ifdef FEAT_MOUSE_XTERM
8404 "mouse_xterm",
8405# endif
8406#endif
8407#ifdef FEAT_MBYTE
8408 "multi_byte",
8409#endif
8410#ifdef FEAT_MBYTE_IME
8411 "multi_byte_ime",
8412#endif
8413#ifdef FEAT_MULTI_LANG
8414 "multi_lang",
8415#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008416#ifdef FEAT_MZSCHEME
8417 "mzscheme",
8418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419#ifdef FEAT_OLE
8420 "ole",
8421#endif
8422#ifdef FEAT_OSFILETYPE
8423 "osfiletype",
8424#endif
8425#ifdef FEAT_PATH_EXTRA
8426 "path_extra",
8427#endif
8428#ifdef FEAT_PERL
8429#ifndef DYNAMIC_PERL
8430 "perl",
8431#endif
8432#endif
8433#ifdef FEAT_PYTHON
8434#ifndef DYNAMIC_PYTHON
8435 "python",
8436#endif
8437#endif
8438#ifdef FEAT_POSTSCRIPT
8439 "postscript",
8440#endif
8441#ifdef FEAT_PRINTER
8442 "printer",
8443#endif
8444#ifdef FEAT_QUICKFIX
8445 "quickfix",
8446#endif
8447#ifdef FEAT_RIGHTLEFT
8448 "rightleft",
8449#endif
8450#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8451 "ruby",
8452#endif
8453#ifdef FEAT_SCROLLBIND
8454 "scrollbind",
8455#endif
8456#ifdef FEAT_CMDL_INFO
8457 "showcmd",
8458 "cmdline_info",
8459#endif
8460#ifdef FEAT_SIGNS
8461 "signs",
8462#endif
8463#ifdef FEAT_SMARTINDENT
8464 "smartindent",
8465#endif
8466#ifdef FEAT_SNIFF
8467 "sniff",
8468#endif
8469#ifdef FEAT_STL_OPT
8470 "statusline",
8471#endif
8472#ifdef FEAT_SUN_WORKSHOP
8473 "sun_workshop",
8474#endif
8475#ifdef FEAT_NETBEANS_INTG
8476 "netbeans_intg",
8477#endif
8478#ifdef FEAT_SYN_HL
8479 "syntax",
8480#endif
8481#if defined(USE_SYSTEM) || !defined(UNIX)
8482 "system",
8483#endif
8484#ifdef FEAT_TAG_BINS
8485 "tag_binary",
8486#endif
8487#ifdef FEAT_TAG_OLDSTATIC
8488 "tag_old_static",
8489#endif
8490#ifdef FEAT_TAG_ANYWHITE
8491 "tag_any_white",
8492#endif
8493#ifdef FEAT_TCL
8494# ifndef DYNAMIC_TCL
8495 "tcl",
8496# endif
8497#endif
8498#ifdef TERMINFO
8499 "terminfo",
8500#endif
8501#ifdef FEAT_TERMRESPONSE
8502 "termresponse",
8503#endif
8504#ifdef FEAT_TEXTOBJ
8505 "textobjects",
8506#endif
8507#ifdef HAVE_TGETENT
8508 "tgetent",
8509#endif
8510#ifdef FEAT_TITLE
8511 "title",
8512#endif
8513#ifdef FEAT_TOOLBAR
8514 "toolbar",
8515#endif
8516#ifdef FEAT_USR_CMDS
8517 "user-commands", /* was accidentally included in 5.4 */
8518 "user_commands",
8519#endif
8520#ifdef FEAT_VIMINFO
8521 "viminfo",
8522#endif
8523#ifdef FEAT_VERTSPLIT
8524 "vertsplit",
8525#endif
8526#ifdef FEAT_VIRTUALEDIT
8527 "virtualedit",
8528#endif
8529#ifdef FEAT_VISUAL
8530 "visual",
8531#endif
8532#ifdef FEAT_VISUALEXTRA
8533 "visualextra",
8534#endif
8535#ifdef FEAT_VREPLACE
8536 "vreplace",
8537#endif
8538#ifdef FEAT_WILDIGN
8539 "wildignore",
8540#endif
8541#ifdef FEAT_WILDMENU
8542 "wildmenu",
8543#endif
8544#ifdef FEAT_WINDOWS
8545 "windows",
8546#endif
8547#ifdef FEAT_WAK
8548 "winaltkeys",
8549#endif
8550#ifdef FEAT_WRITEBACKUP
8551 "writebackup",
8552#endif
8553#ifdef FEAT_XIM
8554 "xim",
8555#endif
8556#ifdef FEAT_XFONTSET
8557 "xfontset",
8558#endif
8559#ifdef USE_XSMP
8560 "xsmp",
8561#endif
8562#ifdef USE_XSMP_INTERACT
8563 "xsmp_interact",
8564#endif
8565#ifdef FEAT_XCLIPBOARD
8566 "xterm_clipboard",
8567#endif
8568#ifdef FEAT_XTERM_SAVE
8569 "xterm_save",
8570#endif
8571#if defined(UNIX) && defined(FEAT_X11)
8572 "X11",
8573#endif
8574 NULL
8575 };
8576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 for (i = 0; has_list[i] != NULL; ++i)
8579 if (STRICMP(name, has_list[i]) == 0)
8580 {
8581 n = TRUE;
8582 break;
8583 }
8584
8585 if (n == FALSE)
8586 {
8587 if (STRNICMP(name, "patch", 5) == 0)
8588 n = has_patch(atoi((char *)name + 5));
8589 else if (STRICMP(name, "vim_starting") == 0)
8590 n = (starting != 0);
8591#ifdef DYNAMIC_TCL
8592 else if (STRICMP(name, "tcl") == 0)
8593 n = tcl_enabled(FALSE);
8594#endif
8595#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
8596 else if (STRICMP(name, "iconv") == 0)
8597 n = iconv_enabled(FALSE);
8598#endif
8599#ifdef DYNAMIC_RUBY
8600 else if (STRICMP(name, "ruby") == 0)
8601 n = ruby_enabled(FALSE);
8602#endif
8603#ifdef DYNAMIC_PYTHON
8604 else if (STRICMP(name, "python") == 0)
8605 n = python_enabled(FALSE);
8606#endif
8607#ifdef DYNAMIC_PERL
8608 else if (STRICMP(name, "perl") == 0)
8609 n = perl_enabled(FALSE);
8610#endif
8611#ifdef FEAT_GUI
8612 else if (STRICMP(name, "gui_running") == 0)
8613 n = (gui.in_use || gui.starting);
8614# ifdef FEAT_GUI_W32
8615 else if (STRICMP(name, "gui_win32s") == 0)
8616 n = gui_is_win32s();
8617# endif
8618# ifdef FEAT_BROWSE
8619 else if (STRICMP(name, "browse") == 0)
8620 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
8621# endif
8622#endif
8623#ifdef FEAT_SYN_HL
8624 else if (STRICMP(name, "syntax_items") == 0)
8625 n = syntax_present(curbuf);
8626#endif
8627#if defined(WIN3264)
8628 else if (STRICMP(name, "win95") == 0)
8629 n = mch_windows95();
8630#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00008631#ifdef FEAT_NETBEANS_INTG
8632 else if (STRICMP(name, "netbeans_enabled") == 0)
8633 n = usingNetbeans;
8634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638}
8639
8640/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008641 * "has_key()" function
8642 */
8643 static void
8644f_has_key(argvars, rettv)
8645 typeval *argvars;
8646 typeval *rettv;
8647{
8648 rettv->vval.v_number = 0;
8649 if (argvars[0].v_type != VAR_DICT)
8650 {
8651 EMSG(_(e_dictreq));
8652 return;
8653 }
8654 if (argvars[0].vval.v_dict == NULL)
8655 return;
8656
8657 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008658 get_tv_string(&argvars[1]), -1, NULL) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008659}
8660
8661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 * "hasmapto()" function
8663 */
8664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008667 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 char_u *name;
8670 char_u *mode;
8671 char_u buf[NUMBUFLEN];
8672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 mode = (char_u *)"nvo";
8676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683}
8684
8685/*
8686 * "histadd()" function
8687 */
8688/*ARGSUSED*/
8689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008691 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
8694#ifdef FEAT_CMDHIST
8695 int histype;
8696 char_u *str;
8697 char_u buf[NUMBUFLEN];
8698#endif
8699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 if (check_restricted() || check_secure())
8702 return;
8703#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 if (histype >= 0)
8706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 if (*str != NUL)
8709 {
8710 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 return;
8713 }
8714 }
8715#endif
8716}
8717
8718/*
8719 * "histdel()" function
8720 */
8721/*ARGSUSED*/
8722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008724 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727#ifdef FEAT_CMDHIST
8728 int n;
8729 char_u buf[NUMBUFLEN];
8730
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008731 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008734 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
8737 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 else
8739 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
8741 get_tv_string_buf(&argvars[1], buf));
8742 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#endif
8746}
8747
8748/*
8749 * "histget()" function
8750 */
8751/*ARGSUSED*/
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008754 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757#ifdef FEAT_CMDHIST
8758 int type;
8759 int idx;
8760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008762 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 idx = get_history_idx(type);
8764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 idx = (int)get_tv_number(&argvars[1]);
8766 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771}
8772
8773/*
8774 * "histnr()" function
8775 */
8776/*ARGSUSED*/
8777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008779 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 int i;
8783
8784#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 if (i >= HIST_CMD && i < HIST_COUNT)
8787 i = get_history_idx(i);
8788 else
8789#endif
8790 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792}
8793
8794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 * "highlightID(name)" function
8796 */
8797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008799 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804
8805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008806 * "highlight_exists()" function
8807 */
8808 static void
8809f_hlexists(argvars, rettv)
8810 typeval *argvars;
8811 typeval *rettv;
8812{
8813 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
8814}
8815
8816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 * "hostname()" function
8818 */
8819/*ARGSUSED*/
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008822 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 char_u hostname[256];
8826
8827 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->v_type = VAR_STRING;
8829 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
8831
8832/*
8833 * iconv() function
8834 */
8835/*ARGSUSED*/
8836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008838 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841#ifdef FEAT_MBYTE
8842 char_u buf1[NUMBUFLEN];
8843 char_u buf2[NUMBUFLEN];
8844 char_u *from, *to, *str;
8845 vimconv_T vimconv;
8846#endif
8847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->v_type = VAR_STRING;
8849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
8851#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 str = get_tv_string(&argvars[0]);
8853 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
8854 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 vimconv.vc_type = CONV_NONE;
8856 convert_setup(&vimconv, from, to);
8857
8858 /* If the encodings are equal, no conversion needed. */
8859 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863
8864 convert_setup(&vimconv, NULL, NULL);
8865 vim_free(from);
8866 vim_free(to);
8867#endif
8868}
8869
8870/*
8871 * "indent()" function
8872 */
8873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008875 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878 linenr_T lnum;
8879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885}
8886
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008887/*
8888 * "index()" function
8889 */
8890 static void
8891f_index(argvars, rettv)
8892 typeval *argvars;
8893 typeval *rettv;
8894{
8895 listvar *l;
8896 listitem *item;
8897 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008898 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008899 int ic = FALSE;
8900
8901 rettv->vval.v_number = -1;
8902 if (argvars[0].v_type != VAR_LIST)
8903 {
8904 EMSG(_(e_listreq));
8905 return;
8906 }
8907 l = argvars[0].vval.v_list;
8908 if (l != NULL)
8909 {
8910 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008911 {
8912 min_idx = get_tv_number(&argvars[2]);
8913 if (argvars[3].v_type != VAR_UNKNOWN)
8914 ic = get_tv_number(&argvars[3]);
8915 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008916
8917 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008918 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008919 {
8920 rettv->vval.v_number = idx;
8921 break;
8922 }
8923 }
8924}
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926static int inputsecret_flag = 0;
8927
8928/*
8929 * "input()" function
8930 * Also handles inputsecret() when inputsecret is set.
8931 */
8932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008934 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 char_u *p = NULL;
8939 int c;
8940 char_u buf[NUMBUFLEN];
8941 int cmd_silent_save = cmd_silent;
8942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945#ifdef NO_CONSOLE_INPUT
8946 /* While starting up, there is no place to enter text. */
8947 if (no_console_input())
8948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 return;
8951 }
8952#endif
8953
8954 cmd_silent = FALSE; /* Want to see the prompt. */
8955 if (prompt != NULL)
8956 {
8957 /* Only the part of the message after the last NL is considered as
8958 * prompt for the command line */
8959 p = vim_strrchr(prompt, '\n');
8960 if (p == NULL)
8961 p = prompt;
8962 else
8963 {
8964 ++p;
8965 c = *p;
8966 *p = NUL;
8967 msg_start();
8968 msg_clr_eos();
8969 msg_puts_attr(prompt, echo_attr);
8970 msg_didout = FALSE;
8971 msg_starthere();
8972 *p = c;
8973 }
8974 cmdline_row = msg_row;
8975 }
8976
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008977 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
8982
8983 /* since the user typed this, no need to wait for return */
8984 need_wait_return = FALSE;
8985 msg_didout = FALSE;
8986 cmd_silent = cmd_silent_save;
8987}
8988
8989/*
8990 * "inputdialog()" function
8991 */
8992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997#if defined(FEAT_GUI_TEXTDIALOG)
8998 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
8999 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9000 {
9001 char_u *message;
9002 char_u buf[NUMBUFLEN];
9003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 IObuff[IOSIZE - 1] = NUL;
9009 }
9010 else
9011 IObuff[0] = NUL;
9012 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9013 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 else
9016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009017 if (argvars[1].v_type != VAR_UNKNOWN
9018 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 rettv->vval.v_string = vim_strsave(
9020 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 }
9026 else
9027#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029}
9030
9031static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9032
9033/*
9034 * "inputrestore()" function
9035 */
9036/*ARGSUSED*/
9037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009039 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 if (ga_userinput.ga_len > 0)
9043 {
9044 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9046 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049 else if (p_verbose > 1)
9050 {
9051 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 }
9054}
9055
9056/*
9057 * "inputsave()" function
9058 */
9059/*ARGSUSED*/
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009062 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 /* Add an entry to the stack of typehead storage. */
9066 if (ga_grow(&ga_userinput, 1) == OK)
9067 {
9068 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9069 + ga_userinput.ga_len);
9070 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 }
9073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075}
9076
9077/*
9078 * "inputsecret()" function
9079 */
9080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009082 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085 ++cmdline_star;
9086 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 --cmdline_star;
9089 --inputsecret_flag;
9090}
9091
9092/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093 * "insert()" function
9094 */
9095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009097 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009099{
9100 long before = 0;
9101 long n;
9102 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009103 listvar *l;
9104
9105 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009106 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009107 else if ((l = argvars[0].vval.v_list) != NULL)
9108 {
9109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009111
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009112 n = before;
9113 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009114 if (n > 0)
9115 EMSGN(_(e_listidx), before);
9116 else
9117 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009118 list_insert_tv(l, &argvars[1], item);
9119 ++l->lv_refcount;
9120 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009121 }
9122 }
9123}
9124
9125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 * "isdirectory()" function
9127 */
9128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009130 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134}
9135
Bram Moolenaar8c711452005-01-14 21:53:12 +00009136static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
9137
9138/*
9139 * Turn a dict into a list:
9140 * "what" == 0: list of keys
9141 * "what" == 1: list of values
9142 * "what" == 2: list of items
9143 */
9144 static void
9145dict_list(argvars, rettv, what)
9146 typeval *argvars;
9147 typeval *rettv;
9148 int what;
9149{
9150 listvar *l;
9151 listvar *l2;
9152 dictitem *di;
9153 listitem *li;
9154 listitem *li2;
9155
9156 rettv->vval.v_number = 0;
9157 if (argvars[0].v_type != VAR_DICT)
9158 {
9159 EMSG(_(e_dictreq));
9160 return;
9161 }
9162 if (argvars[0].vval.v_dict == NULL)
9163 return;
9164
9165 l = list_alloc();
9166 if (l == NULL)
9167 return;
9168 rettv->v_type = VAR_LIST;
9169 rettv->vval.v_list = l;
9170 ++l->lv_refcount;
9171
9172 for (di = argvars[0].vval.v_dict->dv_first; di != NULL; di = di->di_next)
9173 {
9174 li = listitem_alloc();
9175 if (li == NULL)
9176 break;
9177 list_append(l, li);
9178
9179 if (what == 0)
9180 {
9181 /* keys() */
9182 li->li_tv.v_type = VAR_STRING;
9183 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9184 }
9185 else if (what == 1)
9186 {
9187 /* values() */
9188 copy_tv(&di->di_tv, &li->li_tv);
9189 }
9190 else
9191 {
9192 /* items() */
9193 l2 = list_alloc();
9194 li->li_tv.v_type = VAR_LIST;
9195 li->li_tv.vval.v_list = l2;
9196 if (l2 == NULL)
9197 break;
9198 ++l2->lv_refcount;
9199
9200 li2 = listitem_alloc();
9201 if (li2 == NULL)
9202 break;
9203 list_append(l2, li2);
9204 li2->li_tv.v_type = VAR_STRING;
9205 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9206
9207 li2 = listitem_alloc();
9208 if (li2 == NULL)
9209 break;
9210 list_append(l2, li2);
9211 copy_tv(&di->di_tv, &li2->li_tv);
9212 }
9213 }
9214}
9215
9216/*
9217 * "items(dict)" function
9218 */
9219 static void
9220f_items(argvars, rettv)
9221 typeval *argvars;
9222 typeval *rettv;
9223{
9224 dict_list(argvars, rettv, 2);
9225}
9226
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009228 * "join()" function
9229 */
9230 static void
9231f_join(argvars, rettv)
9232 typeval *argvars;
9233 typeval *rettv;
9234{
9235 garray_T ga;
9236 char_u *sep;
9237
9238 rettv->vval.v_number = 0;
9239 if (argvars[0].v_type != VAR_LIST)
9240 {
9241 EMSG(_(e_listreq));
9242 return;
9243 }
9244 if (argvars[0].vval.v_list == NULL)
9245 return;
9246 if (argvars[1].v_type == VAR_UNKNOWN)
9247 sep = (char_u *)" ";
9248 else
9249 sep = get_tv_string(&argvars[1]);
9250
9251 ga_init2(&ga, (int)sizeof(char), 80);
9252 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9253 ga_append(&ga, NUL);
9254
9255 rettv->v_type = VAR_STRING;
9256 rettv->vval.v_string = (char_u *)ga.ga_data;
9257}
9258
9259/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009260 * "keys()" function
9261 */
9262 static void
9263f_keys(argvars, rettv)
9264 typeval *argvars;
9265 typeval *rettv;
9266{
9267 dict_list(argvars, rettv, 0);
9268}
9269
9270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 * "last_buffer_nr()" function.
9272 */
9273/*ARGSUSED*/
9274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009276 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 int n = 0;
9280 buf_T *buf;
9281
9282 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9283 if (n < buf->b_fnum)
9284 n = buf->b_fnum;
9285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009287}
9288
9289/*
9290 * "len()" function
9291 */
9292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009294 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009296{
9297 switch (argvars[0].v_type)
9298 {
9299 case VAR_STRING:
9300 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 rettv->vval.v_number = (varnumber_T)STRLEN(
9302 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009303 break;
9304 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009306 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 case VAR_DICT:
9308 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9309 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009310 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009311 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009312 break;
9313 }
9314}
9315
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009317
9318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009320 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009322 int type;
9323{
9324#ifdef FEAT_LIBCALL
9325 char_u *string_in;
9326 char_u **string_result;
9327 int nr_result;
9328#endif
9329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009331 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009333 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009335
9336 if (check_restricted() || check_secure())
9337 return;
9338
9339#ifdef FEAT_LIBCALL
9340 /* The first two args must be strings, otherwise its meaningless */
9341 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9342 {
9343 if (argvars[2].v_type == VAR_NUMBER)
9344 string_in = NULL;
9345 else
9346 string_in = argvars[2].vval.v_string;
9347 if (type == VAR_NUMBER)
9348 string_result = NULL;
9349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009351 if (mch_libcall(argvars[0].vval.v_string,
9352 argvars[1].vval.v_string,
9353 string_in,
9354 argvars[2].vval.v_number,
9355 string_result,
9356 &nr_result) == OK
9357 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 }
9360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361}
9362
9363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009364 * "libcall()" function
9365 */
9366 static void
9367f_libcall(argvars, rettv)
9368 typeval *argvars;
9369 typeval *rettv;
9370{
9371 libcall_common(argvars, rettv, VAR_STRING);
9372}
9373
9374/*
9375 * "libcallnr()" function
9376 */
9377 static void
9378f_libcallnr(argvars, rettv)
9379 typeval *argvars;
9380 typeval *rettv;
9381{
9382 libcall_common(argvars, rettv, VAR_NUMBER);
9383}
9384
9385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 * "line(string)" function
9387 */
9388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009390 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
9393 linenr_T lnum = 0;
9394 pos_T *fp;
9395
9396 fp = var2fpos(&argvars[0], TRUE);
9397 if (fp != NULL)
9398 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
9403 * "line2byte(lnum)" function
9404 */
9405/*ARGSUSED*/
9406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009408 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413#else
9414 linenr_T lnum;
9415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
9421 if (rettv->vval.v_number >= 0)
9422 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#endif
9424}
9425
9426/*
9427 * "lispindent(lnum)" function
9428 */
9429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009431 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434#ifdef FEAT_LISP
9435 pos_T pos;
9436 linenr_T lnum;
9437
9438 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9441 {
9442 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 curwin->w_cursor = pos;
9445 }
9446 else
9447#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449}
9450
9451/*
9452 * "localtime()" function
9453 */
9454/*ARGSUSED*/
9455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009457 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
9465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009467 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 int exact;
9470{
9471 char_u *keys;
9472 char_u *which;
9473 char_u buf[NUMBUFLEN];
9474 char_u *keys_buf = NULL;
9475 char_u *rhs;
9476 int mode;
9477 garray_T ga;
9478
9479 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 rettv->v_type = VAR_STRING;
9481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (*keys == NUL)
9485 return;
9486
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009487 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 else
9490 which = (char_u *)"";
9491 mode = get_map_mode(&which, 0);
9492
9493 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9494 rhs = check_map(keys, mode, exact);
9495 vim_free(keys_buf);
9496 if (rhs != NULL)
9497 {
9498 ga_init(&ga);
9499 ga.ga_itemsize = 1;
9500 ga.ga_growsize = 40;
9501
9502 while (*rhs != NUL)
9503 ga_concat(&ga, str2special(&rhs, FALSE));
9504
9505 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 }
9508}
9509
9510/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009511 * "map()" function
9512 */
9513 static void
9514f_map(argvars, rettv)
9515 typeval *argvars;
9516 typeval *rettv;
9517{
9518 filter_map(argvars, rettv, TRUE);
9519}
9520
9521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009522 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 */
9524 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009525f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009526 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531
9532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009533 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 */
9535 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009537 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009540 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541}
9542
Bram Moolenaar0d660222005-01-07 21:51:51 +00009543static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
9545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009547 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 int type;
9550{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009551 char_u *str = NULL;
9552 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 char_u *pat;
9554 regmatch_T regmatch;
9555 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009556 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 char_u *save_cpo;
9558 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009559 long nth = 1;
9560 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009561 listvar *l = NULL;
9562 listitem *li = NULL;
9563 long idx = 0;
9564 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
9566 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9567 save_cpo = p_cpo;
9568 p_cpo = (char_u *)"";
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 if (type == 2)
9571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->v_type = VAR_STRING;
9573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 }
9575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009578 if (argvars[0].v_type == VAR_LIST)
9579 {
9580 if ((l = argvars[0].vval.v_list) == NULL)
9581 goto theend;
9582 li = l->lv_first;
9583 }
9584 else
9585 expr = str = get_tv_string(&argvars[0]);
9586
9587 pat = get_tv_string_buf(&argvars[1], patbuf);
9588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009592 if (l != NULL)
9593 {
9594 li = list_find(l, start);
9595 if (li == NULL)
9596 goto theend;
9597 if (start < 0)
9598 {
9599 listitem *ni;
9600
9601 /* Need to compute the index. */
9602 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
9603 ++idx;
9604 }
9605 else
9606 idx = start;
9607 }
9608 else
9609 {
9610 if (start < 0)
9611 start = 0;
9612 if (start > (long)STRLEN(str))
9613 goto theend;
9614 str += start;
9615 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009617 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 }
9620
9621 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9622 if (regmatch.regprog != NULL)
9623 {
9624 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009625
9626 while (1)
9627 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009628 if (l != NULL)
9629 {
9630 if (li == NULL)
9631 {
9632 match = FALSE;
9633 break;
9634 }
9635 str = echo_string(&li->li_tv, &tofree, strbuf);
9636 }
9637
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009638 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009639
9640 if (l != NULL)
9641 vim_free(tofree);
9642 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009643 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009644 if (l == NULL && !match)
9645 break;
9646
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009647 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009648 if (l != NULL)
9649 {
9650 li = li->li_next;
9651 ++idx;
9652 }
9653 else
9654 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009655#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009656 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009657#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009658 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009659#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009660 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009661 }
9662
9663 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 {
9665 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009666 {
9667 if (l != NULL)
9668 copy_tv(&li->li_tv, rettv);
9669 else
9670 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009672 }
9673 else if (l != NULL)
9674 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 else
9676 {
9677 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 (varnumber_T)(regmatch.startp[0] - str);
9680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 }
9685 }
9686 vim_free(regmatch.regprog);
9687 }
9688
9689theend:
9690 p_cpo = save_cpo;
9691}
9692
9693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009694 * "match()" function
9695 */
9696 static void
9697f_match(argvars, rettv)
9698 typeval *argvars;
9699 typeval *rettv;
9700{
9701 find_some_match(argvars, rettv, 1);
9702}
9703
9704/*
9705 * "matchend()" function
9706 */
9707 static void
9708f_matchend(argvars, rettv)
9709 typeval *argvars;
9710 typeval *rettv;
9711{
9712 find_some_match(argvars, rettv, 0);
9713}
9714
9715/*
9716 * "matchstr()" function
9717 */
9718 static void
9719f_matchstr(argvars, rettv)
9720 typeval *argvars;
9721 typeval *rettv;
9722{
9723 find_some_match(argvars, rettv, 2);
9724}
9725
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009726static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
9727
9728 static void
9729max_min(argvars, rettv, domax)
9730 typeval *argvars;
9731 typeval *rettv;
9732 int domax;
9733{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009734 long n = 0;
9735 long i;
9736
9737 if (argvars[0].v_type == VAR_LIST)
9738 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009739 listvar *l;
9740 listitem *li;
9741
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009742 l = argvars[0].vval.v_list;
9743 if (l != NULL)
9744 {
9745 li = l->lv_first;
9746 if (li != NULL)
9747 {
9748 n = get_tv_number(&li->li_tv);
9749 while (1)
9750 {
9751 li = li->li_next;
9752 if (li == NULL)
9753 break;
9754 i = get_tv_number(&li->li_tv);
9755 if (domax ? i > n : i < n)
9756 n = i;
9757 }
9758 }
9759 }
9760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009761 else if (argvars[0].v_type == VAR_DICT)
9762 {
9763 dictvar *d;
9764 dictitem *di;
9765
9766 d = argvars[0].vval.v_dict;
9767 if (d != NULL)
9768 {
9769 di = d->dv_first;
9770 if (di != NULL)
9771 {
9772 n = get_tv_number(&di->di_tv);
9773 while (1)
9774 {
9775 di = di->di_next;
9776 if (di == NULL)
9777 break;
9778 i = get_tv_number(&di->di_tv);
9779 if (domax ? i > n : i < n)
9780 n = i;
9781 }
9782 }
9783 }
9784 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009785 else
9786 EMSG(_(e_listreq));
9787 rettv->vval.v_number = n;
9788}
9789
9790/*
9791 * "max()" function
9792 */
9793 static void
9794f_max(argvars, rettv)
9795 typeval *argvars;
9796 typeval *rettv;
9797{
9798 max_min(argvars, rettv, TRUE);
9799}
9800
9801/*
9802 * "min()" function
9803 */
9804 static void
9805f_min(argvars, rettv)
9806 typeval *argvars;
9807 typeval *rettv;
9808{
9809 max_min(argvars, rettv, FALSE);
9810}
9811
Bram Moolenaar0d660222005-01-07 21:51:51 +00009812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 * "mode()" function
9814 */
9815/*ARGSUSED*/
9816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009818 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821 char_u buf[2];
9822
9823#ifdef FEAT_VISUAL
9824 if (VIsual_active)
9825 {
9826 if (VIsual_select)
9827 buf[0] = VIsual_mode + 's' - 'v';
9828 else
9829 buf[0] = VIsual_mode;
9830 }
9831 else
9832#endif
9833 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
9834 buf[0] = 'r';
9835 else if (State & INSERT)
9836 {
9837 if (State & REPLACE_FLAG)
9838 buf[0] = 'R';
9839 else
9840 buf[0] = 'i';
9841 }
9842 else if (State & CMDLINE)
9843 buf[0] = 'c';
9844 else
9845 buf[0] = 'n';
9846
9847 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 rettv->vval.v_string = vim_strsave(buf);
9849 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850}
9851
9852/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009853 * "nextnonblank()" function
9854 */
9855 static void
9856f_nextnonblank(argvars, rettv)
9857 typeval *argvars;
9858 typeval *rettv;
9859{
9860 linenr_T lnum;
9861
9862 for (lnum = get_tv_lnum(argvars); ; ++lnum)
9863 {
9864 if (lnum > curbuf->b_ml.ml_line_count)
9865 {
9866 lnum = 0;
9867 break;
9868 }
9869 if (*skipwhite(ml_get(lnum)) != NUL)
9870 break;
9871 }
9872 rettv->vval.v_number = lnum;
9873}
9874
9875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 * "nr2char()" function
9877 */
9878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009880 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
9883 char_u buf[NUMBUFLEN];
9884
9885#ifdef FEAT_MBYTE
9886 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 else
9889#endif
9890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 buf[1] = NUL;
9893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 rettv->v_type = VAR_STRING;
9895 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009896}
9897
9898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009899 * "prevnonblank()" function
9900 */
9901 static void
9902f_prevnonblank(argvars, rettv)
9903 typeval *argvars;
9904 typeval *rettv;
9905{
9906 linenr_T lnum;
9907
9908 lnum = get_tv_lnum(argvars);
9909 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9910 lnum = 0;
9911 else
9912 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9913 --lnum;
9914 rettv->vval.v_number = lnum;
9915}
9916
Bram Moolenaar8c711452005-01-14 21:53:12 +00009917/*
9918 * "range()" function
9919 */
9920 static void
9921f_range(argvars, rettv)
9922 typeval *argvars;
9923 typeval *rettv;
9924{
9925 long start;
9926 long end;
9927 long stride = 1;
9928 long i;
9929 listvar *l;
9930 listitem *li;
9931
9932 start = get_tv_number(&argvars[0]);
9933 if (argvars[1].v_type == VAR_UNKNOWN)
9934 {
9935 end = start - 1;
9936 start = 0;
9937 }
9938 else
9939 {
9940 end = get_tv_number(&argvars[1]);
9941 if (argvars[2].v_type != VAR_UNKNOWN)
9942 stride = get_tv_number(&argvars[2]);
9943 }
9944
9945 rettv->vval.v_number = 0;
9946 if (stride == 0)
9947 EMSG(_("E999: Stride is zero"));
9948 else if (stride > 0 ? end < start : end > start)
9949 EMSG(_("E999: Start past end"));
9950 else
9951 {
9952 l = list_alloc();
9953 if (l != NULL)
9954 {
9955 rettv->v_type = VAR_LIST;
9956 rettv->vval.v_list = l;
9957 ++l->lv_refcount;
9958
9959 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9960 {
9961 li = listitem_alloc();
9962 if (li == NULL)
9963 break;
9964 li->li_tv.v_type = VAR_NUMBER;
9965 li->li_tv.vval.v_number = i;
9966 list_append(l, li);
9967 }
9968 }
9969 }
9970}
9971
Bram Moolenaar0d660222005-01-07 21:51:51 +00009972#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9973static void make_connection __ARGS((void));
9974static int check_connection __ARGS((void));
9975
9976 static void
9977make_connection()
9978{
9979 if (X_DISPLAY == NULL
9980# ifdef FEAT_GUI
9981 && !gui.in_use
9982# endif
9983 )
9984 {
9985 x_force_connect = TRUE;
9986 setup_term_clip();
9987 x_force_connect = FALSE;
9988 }
9989}
9990
9991 static int
9992check_connection()
9993{
9994 make_connection();
9995 if (X_DISPLAY == NULL)
9996 {
9997 EMSG(_("E240: No connection to Vim server"));
9998 return FAIL;
9999 }
10000 return OK;
10001}
10002#endif
10003
10004#ifdef FEAT_CLIENTSERVER
10005static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
10006
10007 static void
10008remote_common(argvars, rettv, expr)
10009 typeval *argvars;
10010 typeval *rettv;
10011 int expr;
10012{
10013 char_u *server_name;
10014 char_u *keys;
10015 char_u *r = NULL;
10016 char_u buf[NUMBUFLEN];
10017# ifdef WIN32
10018 HWND w;
10019# else
10020 Window w;
10021# endif
10022
10023 if (check_restricted() || check_secure())
10024 return;
10025
10026# ifdef FEAT_X11
10027 if (check_connection() == FAIL)
10028 return;
10029# endif
10030
10031 server_name = get_tv_string(&argvars[0]);
10032 keys = get_tv_string_buf(&argvars[1], buf);
10033# ifdef WIN32
10034 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10035# else
10036 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10037 < 0)
10038# endif
10039 {
10040 if (r != NULL)
10041 EMSG(r); /* sending worked but evaluation failed */
10042 else
10043 EMSG2(_("E241: Unable to send to %s"), server_name);
10044 return;
10045 }
10046
10047 rettv->vval.v_string = r;
10048
10049 if (argvars[2].v_type != VAR_UNKNOWN)
10050 {
10051 var v;
10052 char_u str[30];
10053
10054 sprintf((char *)str, "0x%x", (unsigned int)w);
10055 v.tv.v_type = VAR_STRING;
10056 v.tv.vval.v_string = vim_strsave(str);
10057 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
10058 vim_free(v.tv.vval.v_string);
10059 }
10060}
10061#endif
10062
10063/*
10064 * "remote_expr()" function
10065 */
10066/*ARGSUSED*/
10067 static void
10068f_remote_expr(argvars, rettv)
10069 typeval *argvars;
10070 typeval *rettv;
10071{
10072 rettv->v_type = VAR_STRING;
10073 rettv->vval.v_string = NULL;
10074#ifdef FEAT_CLIENTSERVER
10075 remote_common(argvars, rettv, TRUE);
10076#endif
10077}
10078
10079/*
10080 * "remote_foreground()" function
10081 */
10082/*ARGSUSED*/
10083 static void
10084f_remote_foreground(argvars, rettv)
10085 typeval *argvars;
10086 typeval *rettv;
10087{
10088 rettv->vval.v_number = 0;
10089#ifdef FEAT_CLIENTSERVER
10090# ifdef WIN32
10091 /* On Win32 it's done in this application. */
10092 serverForeground(get_tv_string(&argvars[0]));
10093# else
10094 /* Send a foreground() expression to the server. */
10095 argvars[1].v_type = VAR_STRING;
10096 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10097 argvars[2].v_type = VAR_UNKNOWN;
10098 remote_common(argvars, rettv, TRUE);
10099 vim_free(argvars[1].vval.v_string);
10100# endif
10101#endif
10102}
10103
10104/*ARGSUSED*/
10105 static void
10106f_remote_peek(argvars, rettv)
10107 typeval *argvars;
10108 typeval *rettv;
10109{
10110#ifdef FEAT_CLIENTSERVER
10111 var v;
10112 char_u *s = NULL;
10113# ifdef WIN32
10114 int n = 0;
10115# endif
10116
10117 if (check_restricted() || check_secure())
10118 {
10119 rettv->vval.v_number = -1;
10120 return;
10121 }
10122# ifdef WIN32
10123 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10124 if (n == 0)
10125 rettv->vval.v_number = -1;
10126 else
10127 {
10128 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10129 rettv->vval.v_number = (s != NULL);
10130 }
10131# else
10132 rettv->vval.v_number = 0;
10133 if (check_connection() == FAIL)
10134 return;
10135
10136 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10137 serverStrToWin(get_tv_string(&argvars[0])), &s);
10138# endif
10139
10140 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10141 {
10142 v.tv.v_type = VAR_STRING;
10143 v.tv.vval.v_string = vim_strsave(s);
10144 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
10145 vim_free(v.tv.vval.v_string);
10146 }
10147#else
10148 rettv->vval.v_number = -1;
10149#endif
10150}
10151
10152/*ARGSUSED*/
10153 static void
10154f_remote_read(argvars, rettv)
10155 typeval *argvars;
10156 typeval *rettv;
10157{
10158 char_u *r = NULL;
10159
10160#ifdef FEAT_CLIENTSERVER
10161 if (!check_restricted() && !check_secure())
10162 {
10163# ifdef WIN32
10164 /* The server's HWND is encoded in the 'id' parameter */
10165 int n = 0;
10166
10167 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10168 if (n != 0)
10169 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10170 if (r == NULL)
10171# else
10172 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10173 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10174# endif
10175 EMSG(_("E277: Unable to read a server reply"));
10176 }
10177#endif
10178 rettv->v_type = VAR_STRING;
10179 rettv->vval.v_string = r;
10180}
10181
10182/*
10183 * "remote_send()" function
10184 */
10185/*ARGSUSED*/
10186 static void
10187f_remote_send(argvars, rettv)
10188 typeval *argvars;
10189 typeval *rettv;
10190{
10191 rettv->v_type = VAR_STRING;
10192 rettv->vval.v_string = NULL;
10193#ifdef FEAT_CLIENTSERVER
10194 remote_common(argvars, rettv, FALSE);
10195#endif
10196}
10197
10198/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010199 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010200 */
10201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010202f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010203 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010204 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010205{
10206 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010207 listitem *item, *item2;
10208 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010209 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010210 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010211 char_u *key;
10212 dictvar *d;
10213 dictitem *di, **pdi;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010214
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010215 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010216 if (argvars[0].v_type == VAR_DICT)
10217 {
10218 if (argvars[2].v_type != VAR_UNKNOWN)
10219 EMSG2(_(e_toomanyarg), "remove()");
10220 else if ((d = argvars[0].vval.v_dict) != NULL)
10221 {
10222 key = get_tv_string(&argvars[1]);
10223 pdi = &d->dv_first;
10224 for (di = d->dv_first; di != NULL; pdi = &di->di_next, di = *pdi)
10225 if (STRCMP(di->di_key, key) == 0)
10226 {
10227 *pdi = di->di_next;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 *rettv = di->di_tv;
10229 init_tv(&di->di_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010230 dictitem_free(di);
10231 break;
10232 }
10233 if (di == NULL)
10234 EMSG2(_(e_dictkey), key);
10235 }
10236 }
10237 else if (argvars[0].v_type != VAR_LIST)
10238 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010239 else if ((l = argvars[0].vval.v_list) != NULL)
10240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010242 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010243 if (item == NULL)
10244 EMSGN(_(e_listidx), idx);
10245 else
10246 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010247 if (argvars[2].v_type == VAR_UNKNOWN)
10248 {
10249 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010250 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010251 *rettv = item->li_tv;
10252 vim_free(item);
10253 }
10254 else
10255 {
10256 /* Remove range of items, return list with values. */
10257 end = get_tv_number(&argvars[2]);
10258 item2 = list_find(l, end);
10259 if (item2 == NULL)
10260 EMSGN(_(e_listidx), end);
10261 else
10262 {
10263 for (li = item; li != item2 && li != NULL; li = li->li_next)
10264 ;
10265 if (li == NULL) /* didn't find "item2" after "item" */
10266 EMSG(_(e_invrange));
10267 else
10268 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010269 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010270 l = list_alloc();
10271 if (l != NULL)
10272 {
10273 rettv->v_type = VAR_LIST;
10274 rettv->vval.v_list = l;
10275 l->lv_first = item;
10276 l->lv_last = item2;
10277 l->lv_refcount = 1;
10278 item->li_prev = NULL;
10279 item2->li_next = NULL;
10280 }
10281 }
10282 }
10283 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010284 }
10285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287
10288/*
10289 * "rename({from}, {to})" function
10290 */
10291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010293 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
10296 char_u buf[NUMBUFLEN];
10297
10298 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10302 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303}
10304
10305/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306 * "repeat()" function
10307 */
10308/*ARGSUSED*/
10309 static void
10310f_repeat(argvars, rettv)
10311 typeval *argvars;
10312 typeval *rettv;
10313{
10314 char_u *p;
10315 int n;
10316 int slen;
10317 int len;
10318 char_u *r;
10319 int i;
10320 listvar *l;
10321
10322 n = get_tv_number(&argvars[1]);
10323 if (argvars[0].v_type == VAR_LIST)
10324 {
10325 l = list_alloc();
10326 if (l != NULL && argvars[0].vval.v_list != NULL)
10327 {
10328 l->lv_refcount = 1;
10329 while (n-- > 0)
10330 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
10331 break;
10332 }
10333 rettv->v_type = VAR_LIST;
10334 rettv->vval.v_list = l;
10335 }
10336 else
10337 {
10338 p = get_tv_string(&argvars[0]);
10339 rettv->v_type = VAR_STRING;
10340 rettv->vval.v_string = NULL;
10341
10342 slen = (int)STRLEN(p);
10343 len = slen * n;
10344 if (len <= 0)
10345 return;
10346
10347 r = alloc(len + 1);
10348 if (r != NULL)
10349 {
10350 for (i = 0; i < n; i++)
10351 mch_memmove(r + i * slen, p, (size_t)slen);
10352 r[len] = NUL;
10353 }
10354
10355 rettv->vval.v_string = r;
10356 }
10357}
10358
10359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 * "resolve()" function
10361 */
10362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010363f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010364 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366{
10367 char_u *p;
10368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010369 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370#ifdef FEAT_SHORTCUT
10371 {
10372 char_u *v = NULL;
10373
10374 v = mch_resolve_shortcut(p);
10375 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 }
10380#else
10381# ifdef HAVE_READLINK
10382 {
10383 char_u buf[MAXPATHL + 1];
10384 char_u *cpy;
10385 int len;
10386 char_u *remain = NULL;
10387 char_u *q;
10388 int is_relative_to_current = FALSE;
10389 int has_trailing_pathsep = FALSE;
10390 int limit = 100;
10391
10392 p = vim_strsave(p);
10393
10394 if (p[0] == '.' && (vim_ispathsep(p[1])
10395 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10396 is_relative_to_current = TRUE;
10397
10398 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010399 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 has_trailing_pathsep = TRUE;
10401
10402 q = getnextcomp(p);
10403 if (*q != NUL)
10404 {
10405 /* Separate the first path component in "p", and keep the
10406 * remainder (beginning with the path separator). */
10407 remain = vim_strsave(q - 1);
10408 q[-1] = NUL;
10409 }
10410
10411 for (;;)
10412 {
10413 for (;;)
10414 {
10415 len = readlink((char *)p, (char *)buf, MAXPATHL);
10416 if (len <= 0)
10417 break;
10418 buf[len] = NUL;
10419
10420 if (limit-- == 0)
10421 {
10422 vim_free(p);
10423 vim_free(remain);
10424 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 goto fail;
10427 }
10428
10429 /* Ensure that the result will have a trailing path separator
10430 * if the argument has one. */
10431 if (remain == NULL && has_trailing_pathsep)
10432 add_pathsep(buf);
10433
10434 /* Separate the first path component in the link value and
10435 * concatenate the remainders. */
10436 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10437 if (*q != NUL)
10438 {
10439 if (remain == NULL)
10440 remain = vim_strsave(q - 1);
10441 else
10442 {
10443 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
10444 if (cpy != NULL)
10445 {
10446 STRCAT(cpy, remain);
10447 vim_free(remain);
10448 remain = cpy;
10449 }
10450 }
10451 q[-1] = NUL;
10452 }
10453
10454 q = gettail(p);
10455 if (q > p && *q == NUL)
10456 {
10457 /* Ignore trailing path separator. */
10458 q[-1] = NUL;
10459 q = gettail(p);
10460 }
10461 if (q > p && !mch_isFullName(buf))
10462 {
10463 /* symlink is relative to directory of argument */
10464 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10465 if (cpy != NULL)
10466 {
10467 STRCPY(cpy, p);
10468 STRCPY(gettail(cpy), buf);
10469 vim_free(p);
10470 p = cpy;
10471 }
10472 }
10473 else
10474 {
10475 vim_free(p);
10476 p = vim_strsave(buf);
10477 }
10478 }
10479
10480 if (remain == NULL)
10481 break;
10482
10483 /* Append the first path component of "remain" to "p". */
10484 q = getnextcomp(remain + 1);
10485 len = q - remain - (*q != NUL);
10486 cpy = vim_strnsave(p, STRLEN(p) + len);
10487 if (cpy != NULL)
10488 {
10489 STRNCAT(cpy, remain, len);
10490 vim_free(p);
10491 p = cpy;
10492 }
10493 /* Shorten "remain". */
10494 if (*q != NUL)
10495 STRCPY(remain, q - 1);
10496 else
10497 {
10498 vim_free(remain);
10499 remain = NULL;
10500 }
10501 }
10502
10503 /* If the result is a relative path name, make it explicitly relative to
10504 * the current directory if and only if the argument had this form. */
10505 if (!vim_ispathsep(*p))
10506 {
10507 if (is_relative_to_current
10508 && *p != NUL
10509 && !(p[0] == '.'
10510 && (p[1] == NUL
10511 || vim_ispathsep(p[1])
10512 || (p[1] == '.'
10513 && (p[2] == NUL
10514 || vim_ispathsep(p[2]))))))
10515 {
10516 /* Prepend "./". */
10517 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
10518 if (cpy != NULL)
10519 {
10520 STRCAT(cpy, p);
10521 vim_free(p);
10522 p = cpy;
10523 }
10524 }
10525 else if (!is_relative_to_current)
10526 {
10527 /* Strip leading "./". */
10528 q = p;
10529 while (q[0] == '.' && vim_ispathsep(q[1]))
10530 q += 2;
10531 if (q > p)
10532 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10533 }
10534 }
10535
10536 /* Ensure that the result will have no trailing path separator
10537 * if the argument had none. But keep "/" or "//". */
10538 if (!has_trailing_pathsep)
10539 {
10540 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010541 if (after_pathsep(p, q))
10542 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 }
10544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 }
10547# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549# endif
10550#endif
10551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553
10554#ifdef HAVE_READLINK
10555fail:
10556#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 */
10563 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010564f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010565 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010568 listvar *l;
10569 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570
Bram Moolenaar0d660222005-01-07 21:51:51 +000010571 rettv->vval.v_number = 0;
10572 if (argvars[0].v_type != VAR_LIST)
10573 EMSG2(_(e_listarg), "reverse()");
10574 else if ((l = argvars[0].vval.v_list) != NULL)
10575 {
10576 li = l->lv_last;
10577 l->lv_first = l->lv_last = li;
10578 while (li != NULL)
10579 {
10580 ni = li->li_prev;
10581 list_append(l, li);
10582 li = ni;
10583 }
10584 rettv->vval.v_list = l;
10585 rettv->v_type = VAR_LIST;
10586 ++l->lv_refcount;
10587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588}
10589
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010590#define SP_NOMOVE 1 /* don't move cursor */
10591#define SP_REPEAT 2 /* repeat to find outer pair */
10592#define SP_RETCOUNT 4 /* return matchcount */
10593
Bram Moolenaar0d660222005-01-07 21:51:51 +000010594static int get_search_arg __ARGS((typeval *varp, int *flagsp));
10595
10596/*
10597 * Get flags for a search function.
10598 * Possibly sets "p_ws".
10599 * Returns BACKWARD, FORWARD or zero (for an error).
10600 */
10601 static int
10602get_search_arg(varp, flagsp)
10603 typeval *varp;
10604 int *flagsp;
10605{
10606 int dir = FORWARD;
10607 char_u *flags;
10608 char_u nbuf[NUMBUFLEN];
10609 int mask;
10610
10611 if (varp->v_type != VAR_UNKNOWN)
10612 {
10613 flags = get_tv_string_buf(varp, nbuf);
10614 while (*flags != NUL)
10615 {
10616 switch (*flags)
10617 {
10618 case 'b': dir = BACKWARD; break;
10619 case 'w': p_ws = TRUE; break;
10620 case 'W': p_ws = FALSE; break;
10621 default: mask = 0;
10622 if (flagsp != NULL)
10623 switch (*flags)
10624 {
10625 case 'n': mask = SP_NOMOVE; break;
10626 case 'r': mask = SP_REPEAT; break;
10627 case 'm': mask = SP_RETCOUNT; break;
10628 }
10629 if (mask == 0)
10630 {
10631 EMSG2(_(e_invarg2), flags);
10632 dir = 0;
10633 }
10634 else
10635 *flagsp |= mask;
10636 }
10637 if (dir == 0)
10638 break;
10639 ++flags;
10640 }
10641 }
10642 return dir;
10643}
10644
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645/*
10646 * "search()" function
10647 */
10648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010650 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652{
10653 char_u *pat;
10654 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010655 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 int save_p_ws = p_ws;
10657 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010658 int flags = 0;
10659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010663 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
10664 if (dir == 0)
10665 goto theend;
10666 if ((flags & ~SP_NOMOVE) != 0)
10667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010669 goto theend;
10670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010672 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
10674 SEARCH_KEEP, RE_SEARCH) != FAIL)
10675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 curwin->w_cursor = pos;
10678 /* "/$" will put the cursor after the end of the line, may need to
10679 * correct that here */
10680 check_cursor();
10681 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010682
10683 /* If 'n' flag is used: restore cursor position. */
10684 if (flags & SP_NOMOVE)
10685 curwin->w_cursor = save_cursor;
10686theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 p_ws = save_p_ws;
10688}
10689
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690/*
10691 * "searchpair()" function
10692 */
10693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010695 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
10698 char_u *spat, *mpat, *epat;
10699 char_u *skip;
10700 char_u *pat, *pat2, *pat3;
10701 pos_T pos;
10702 pos_T firstpos;
10703 pos_T save_cursor;
10704 pos_T save_pos;
10705 int save_p_ws = p_ws;
10706 char_u *save_cpo;
10707 int dir;
10708 int flags = 0;
10709 char_u nbuf1[NUMBUFLEN];
10710 char_u nbuf2[NUMBUFLEN];
10711 char_u nbuf3[NUMBUFLEN];
10712 int n;
10713 int r;
10714 int nest = 1;
10715 int err;
10716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718
10719 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10720 save_cpo = p_cpo;
10721 p_cpo = (char_u *)"";
10722
10723 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 spat = get_tv_string(&argvars[0]);
10725 mpat = get_tv_string_buf(&argvars[1], nbuf1);
10726 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727
10728 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10729 * start/middle/end (pat3, for the top pair). */
10730 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
10731 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
10732 if (pat2 == NULL || pat3 == NULL)
10733 goto theend;
10734 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
10735 if (*mpat == NUL)
10736 STRCPY(pat3, pat2);
10737 else
10738 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
10739 spat, epat, mpat);
10740
10741 /* Handle the optional fourth argument: flags */
10742 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010743 if (dir == 0)
10744 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
10746 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010747 if (argvars[3].v_type == VAR_UNKNOWN
10748 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 skip = (char_u *)"";
10750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752
10753 save_cursor = curwin->w_cursor;
10754 pos = curwin->w_cursor;
10755 firstpos.lnum = 0;
10756 pat = pat3;
10757 for (;;)
10758 {
10759 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
10760 SEARCH_KEEP, RE_SEARCH);
10761 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
10762 /* didn't find it or found the first match again: FAIL */
10763 break;
10764
10765 if (firstpos.lnum == 0)
10766 firstpos = pos;
10767
10768 /* If the skip pattern matches, ignore this match. */
10769 if (*skip != NUL)
10770 {
10771 save_pos = curwin->w_cursor;
10772 curwin->w_cursor = pos;
10773 r = eval_to_bool(skip, &err, NULL, FALSE);
10774 curwin->w_cursor = save_pos;
10775 if (err)
10776 {
10777 /* Evaluating {skip} caused an error, break here. */
10778 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 break;
10781 }
10782 if (r)
10783 continue;
10784 }
10785
10786 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10787 {
10788 /* Found end when searching backwards or start when searching
10789 * forward: nested pair. */
10790 ++nest;
10791 pat = pat2; /* nested, don't search for middle */
10792 }
10793 else
10794 {
10795 /* Found end when searching forward or start when searching
10796 * backward: end of (nested) pair; or found middle in outer pair. */
10797 if (--nest == 1)
10798 pat = pat3; /* outer level, search for middle */
10799 }
10800
10801 if (nest == 0)
10802 {
10803 /* Found the match: return matchcount or line number. */
10804 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 curwin->w_cursor = pos;
10809 if (!(flags & SP_REPEAT))
10810 break;
10811 nest = 1; /* search for next unmatched */
10812 }
10813 }
10814
10815 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 curwin->w_cursor = save_cursor;
10818
10819theend:
10820 vim_free(pat2);
10821 vim_free(pat3);
10822 p_ws = save_p_ws;
10823 p_cpo = save_cpo;
10824}
10825
Bram Moolenaar0d660222005-01-07 21:51:51 +000010826/*ARGSUSED*/
10827 static void
10828f_server2client(argvars, rettv)
10829 typeval *argvars;
10830 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832#ifdef FEAT_CLIENTSERVER
10833 char_u buf[NUMBUFLEN];
10834 char_u *server = get_tv_string(&argvars[0]);
10835 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836
Bram Moolenaar0d660222005-01-07 21:51:51 +000010837 rettv->vval.v_number = -1;
10838 if (check_restricted() || check_secure())
10839 return;
10840# ifdef FEAT_X11
10841 if (check_connection() == FAIL)
10842 return;
10843# endif
10844
10845 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010847 EMSG(_("E258: Unable to send to client"));
10848 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010850 rettv->vval.v_number = 0;
10851#else
10852 rettv->vval.v_number = -1;
10853#endif
10854}
10855
10856/*ARGSUSED*/
10857 static void
10858f_serverlist(argvars, rettv)
10859 typeval *argvars;
10860 typeval *rettv;
10861{
10862 char_u *r = NULL;
10863
10864#ifdef FEAT_CLIENTSERVER
10865# ifdef WIN32
10866 r = serverGetVimNames();
10867# else
10868 make_connection();
10869 if (X_DISPLAY != NULL)
10870 r = serverGetVimNames(X_DISPLAY);
10871# endif
10872#endif
10873 rettv->v_type = VAR_STRING;
10874 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877/*
10878 * "setbufvar()" function
10879 */
10880/*ARGSUSED*/
10881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010883 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886 buf_T *buf;
10887#ifdef FEAT_AUTOCMD
10888 aco_save_T aco;
10889#else
10890 buf_T *save_curbuf;
10891#endif
10892 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010893 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 char_u nbuf[NUMBUFLEN];
10895
10896 if (check_restricted() || check_secure())
10897 return;
10898 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899 buf = get_buf_tv(&argvars[0]);
10900 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 varp = &argvars[2];
10902
10903 if (buf != NULL && varname != NULL && varp != NULL)
10904 {
10905 /* set curbuf to be our buf, temporarily */
10906#ifdef FEAT_AUTOCMD
10907 aucmd_prepbuf(&aco, buf);
10908#else
10909 save_curbuf = curbuf;
10910 curbuf = buf;
10911#endif
10912
10913 if (*varname == '&')
10914 {
10915 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 set_option_value(varname, get_tv_number(varp),
10917 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 }
10919 else
10920 {
10921 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10922 if (bufvarname != NULL)
10923 {
10924 STRCPY(bufvarname, "b:");
10925 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010926 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 vim_free(bufvarname);
10928 }
10929 }
10930
10931 /* reset notion of buffer */
10932#ifdef FEAT_AUTOCMD
10933 aucmd_restbuf(&aco);
10934#else
10935 curbuf = save_curbuf;
10936#endif
10937 }
10938 --emsg_off;
10939}
10940
10941/*
10942 * "setcmdpos()" function
10943 */
10944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010946 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_number = set_cmdline_pos(
10950 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951}
10952
10953/*
10954 * "setline()" function
10955 */
10956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010958 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
10961 linenr_T lnum;
10962 char_u *line;
10963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 lnum = get_tv_lnum(argvars);
10965 line = get_tv_string(&argvars[1]);
10966 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967
10968 if (lnum >= 1
10969 && lnum <= curbuf->b_ml.ml_line_count
10970 && u_savesub(lnum) == OK
10971 && ml_replace(lnum, line, TRUE) == OK)
10972 {
10973 changed_bytes(lnum, 0);
10974 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
10977}
10978
10979/*
10980 * "setreg()" function
10981 */
10982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010984 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986{
10987 int regname;
10988 char_u *strregname;
10989 char_u *stropt;
10990 int append;
10991 char_u yank_type;
10992 long block_len;
10993
10994 block_len = -1;
10995 yank_type = MAUTO;
10996 append = FALSE;
10997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998 strregname = get_tv_string(argvars);
10999 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000
11001 regname = (strregname == NULL ? '"' : *strregname);
11002 if (regname == 0 || regname == '@')
11003 regname = '"';
11004 else if (regname == '=')
11005 return;
11006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011007 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 switch (*stropt)
11011 {
11012 case 'a': case 'A': /* append */
11013 append = TRUE;
11014 break;
11015 case 'v': case 'c': /* character-wise selection */
11016 yank_type = MCHAR;
11017 break;
11018 case 'V': case 'l': /* line-wise selection */
11019 yank_type = MLINE;
11020 break;
11021#ifdef FEAT_VISUAL
11022 case 'b': case Ctrl_V: /* block-wise selection */
11023 yank_type = MBLOCK;
11024 if (VIM_ISDIGIT(stropt[1]))
11025 {
11026 ++stropt;
11027 block_len = getdigits(&stropt) - 1;
11028 --stropt;
11029 }
11030 break;
11031#endif
11032 }
11033 }
11034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
11040
11041/*
11042 * "setwinvar(expr)" function
11043 */
11044/*ARGSUSED*/
11045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011047 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 win_T *win;
11051#ifdef FEAT_WINDOWS
11052 win_T *save_curwin;
11053#endif
11054 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 char_u nbuf[NUMBUFLEN];
11057
11058 if (check_restricted() || check_secure())
11059 return;
11060 ++emsg_off;
11061 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 varp = &argvars[2];
11064
11065 if (win != NULL && varname != NULL && varp != NULL)
11066 {
11067#ifdef FEAT_WINDOWS
11068 /* set curwin to be our win, temporarily */
11069 save_curwin = curwin;
11070 curwin = win;
11071 curbuf = curwin->w_buffer;
11072#endif
11073
11074 if (*varname == '&')
11075 {
11076 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 set_option_value(varname, get_tv_number(varp),
11078 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 }
11080 else
11081 {
11082 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11083 if (winvarname != NULL)
11084 {
11085 STRCPY(winvarname, "w:");
11086 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011087 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 vim_free(winvarname);
11089 }
11090 }
11091
11092#ifdef FEAT_WINDOWS
11093 /* Restore current window, if it's still valid (autocomands can make
11094 * it invalid). */
11095 if (win_valid(save_curwin))
11096 {
11097 curwin = save_curwin;
11098 curbuf = curwin->w_buffer;
11099 }
11100#endif
11101 }
11102 --emsg_off;
11103}
11104
11105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 */
11108 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011110 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115 p = get_tv_string(&argvars[0]);
11116 rettv->vval.v_string = vim_strsave(p);
11117 simplify_filename(rettv->vval.v_string); /* simplify in place */
11118 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119}
11120
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121static int
11122#ifdef __BORLANDC__
11123 _RTLENTRYF
11124#endif
11125 item_compare __ARGS((const void *s1, const void *s2));
11126static int
11127#ifdef __BORLANDC__
11128 _RTLENTRYF
11129#endif
11130 item_compare2 __ARGS((const void *s1, const void *s2));
11131
11132static int item_compare_ic;
11133static char_u *item_compare_func;
11134#define ITEM_COMPARE_FAIL 999
11135
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011137 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139 static int
11140#ifdef __BORLANDC__
11141_RTLENTRYF
11142#endif
11143item_compare(s1, s2)
11144 const void *s1;
11145 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147 char_u *p1, *p2;
11148 char_u *tofree1, *tofree2;
11149 int res;
11150 char_u numbuf1[NUMBUFLEN];
11151 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
11154 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
11155 if (item_compare_ic)
11156 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158 res = STRCMP(p1, p2);
11159 vim_free(tofree1);
11160 vim_free(tofree2);
11161 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162}
11163
11164 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165#ifdef __BORLANDC__
11166_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168item_compare2(s1, s2)
11169 const void *s1;
11170 const void *s2;
11171{
11172 int res;
11173 typeval rettv;
11174 typeval argv[2];
11175 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176
Bram Moolenaar0d660222005-01-07 21:51:51 +000011177 /* copy the values (is this really needed?) */
11178 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
11179 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
11180
11181 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11182 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011183 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011184 clear_tv(&argv[0]);
11185 clear_tv(&argv[1]);
11186
11187 if (res == FAIL)
11188 res = ITEM_COMPARE_FAIL;
11189 else
11190 res = get_tv_number(&rettv);
11191 clear_tv(&rettv);
11192 return res;
11193}
11194
11195/*
11196 * "sort({list})" function
11197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011199f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011200 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011203 listvar *l;
11204 listitem *li;
11205 listitem **ptrs;
11206 long len;
11207 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209 rettv->vval.v_number = 0;
11210 if (argvars[0].v_type != VAR_LIST)
11211 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 else
11213 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214 l = argvars[0].vval.v_list;
11215 if (l == NULL)
11216 return;
11217 rettv->vval.v_list = l;
11218 rettv->v_type = VAR_LIST;
11219 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221 len = list_len(l);
11222 if (len <= 1)
11223 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
Bram Moolenaar0d660222005-01-07 21:51:51 +000011225 item_compare_ic = FALSE;
11226 item_compare_func = NULL;
11227 if (argvars[1].v_type != VAR_UNKNOWN)
11228 {
11229 if (argvars[1].v_type == VAR_FUNC)
11230 item_compare_func = argvars[0].vval.v_string;
11231 else
11232 {
11233 i = get_tv_number(&argvars[1]);
11234 if (i == 1)
11235 item_compare_ic = TRUE;
11236 else
11237 item_compare_func = get_tv_string(&argvars[1]);
11238 }
11239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240
Bram Moolenaar0d660222005-01-07 21:51:51 +000011241 /* Make an array with each entry pointing to an item in the List. */
11242 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
11243 if (ptrs == NULL)
11244 return;
11245 i = 0;
11246 for (li = l->lv_first; li != NULL; li = li->li_next)
11247 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 /* test the compare function */
11250 if (item_compare_func != NULL
11251 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11252 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011253 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 {
11256 /* Sort the array with item pointers. */
11257 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
11258 item_compare_func == NULL ? item_compare : item_compare2);
11259
11260 /* Clear the List and append the items in the sorted order. */
11261 l->lv_first = l->lv_last = NULL;
11262 for (i = 0; i < len; ++i)
11263 list_append(l, ptrs[i]);
11264 }
11265
11266 vim_free(ptrs);
11267 }
11268}
11269
11270 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011271f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011272 typeval *argvars;
11273 typeval *rettv;
11274{
11275 char_u *str;
11276 char_u *end;
11277 char_u *pat;
11278 regmatch_T regmatch;
11279 char_u patbuf[NUMBUFLEN];
11280 char_u *save_cpo;
11281 int match;
11282 listitem *ni;
11283 listvar *l;
11284 colnr_T col = 0;
11285
11286 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11287 save_cpo = p_cpo;
11288 p_cpo = (char_u *)"";
11289
11290 str = get_tv_string(&argvars[0]);
11291 if (argvars[1].v_type == VAR_UNKNOWN)
11292 pat = (char_u *)"[\\x01- ]\\+";
11293 else
11294 pat = get_tv_string_buf(&argvars[1], patbuf);
11295
11296 l = list_alloc();
11297 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011299 rettv->v_type = VAR_LIST;
11300 rettv->vval.v_list = l;
11301 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302
Bram Moolenaar0d660222005-01-07 21:51:51 +000011303 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11304 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306 regmatch.rm_ic = FALSE;
11307 while (*str != NUL)
11308 {
11309 match = vim_regexec_nl(&regmatch, str, col);
11310 if (match)
11311 end = regmatch.startp[0];
11312 else
11313 end = str + STRLEN(str);
11314 if (end > str)
11315 {
11316 ni = listitem_alloc();
11317 if (ni == NULL)
11318 break;
11319 ni->li_tv.v_type = VAR_STRING;
11320 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
11321 list_append(l, ni);
11322 }
11323 if (!match)
11324 break;
11325 /* Advance to just after the match. */
11326 if (regmatch.endp[0] > str)
11327 col = 0;
11328 else
11329 {
11330 /* Don't get stuck at the same match. */
11331#ifdef FEAT_MBYTE
11332 col = mb_ptr2len_check(regmatch.endp[0]);
11333#else
11334 col = 1;
11335#endif
11336 }
11337 str = regmatch.endp[0];
11338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339
Bram Moolenaar0d660222005-01-07 21:51:51 +000011340 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342
Bram Moolenaar0d660222005-01-07 21:51:51 +000011343 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344}
11345
11346#ifdef HAVE_STRFTIME
11347/*
11348 * "strftime({format}[, {time}])" function
11349 */
11350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011352 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
11355 char_u result_buf[256];
11356 struct tm *curtime;
11357 time_t seconds;
11358 char_u *p;
11359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 seconds = time(NULL);
11365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 curtime = localtime(&seconds);
11368 /* MSVC returns NULL for an invalid value of seconds. */
11369 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 else
11372 {
11373# ifdef FEAT_MBYTE
11374 vimconv_T conv;
11375 char_u *enc;
11376
11377 conv.vc_type = CONV_NONE;
11378 enc = enc_locale();
11379 convert_setup(&conv, p_enc, enc);
11380 if (conv.vc_type != CONV_NONE)
11381 p = string_convert(&conv, p, NULL);
11382# endif
11383 if (p != NULL)
11384 (void)strftime((char *)result_buf, sizeof(result_buf),
11385 (char *)p, curtime);
11386 else
11387 result_buf[0] = NUL;
11388
11389# ifdef FEAT_MBYTE
11390 if (conv.vc_type != CONV_NONE)
11391 vim_free(p);
11392 convert_setup(&conv, enc, p_enc);
11393 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 else
11396# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398
11399# ifdef FEAT_MBYTE
11400 /* Release conversion descriptors */
11401 convert_setup(&conv, NULL, NULL);
11402 vim_free(enc);
11403# endif
11404 }
11405}
11406#endif
11407
11408/*
11409 * "stridx()" function
11410 */
11411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011413 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
11416 char_u buf[NUMBUFLEN];
11417 char_u *needle;
11418 char_u *haystack;
11419 char_u *pos;
11420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 needle = get_tv_string(&argvars[1]);
11422 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11424
11425 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429}
11430
11431/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011432 * "string()" function
11433 */
11434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011436 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011438{
11439 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011440 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011443 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011444 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
11449 * "strlen()" function
11450 */
11451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->vval.v_number = (varnumber_T)(STRLEN(
11457 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458}
11459
11460/*
11461 * "strpart()" function
11462 */
11463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011465 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
11468 char_u *p;
11469 int n;
11470 int len;
11471 int slen;
11472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 slen = (int)STRLEN(p);
11475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 else
11480 len = slen - n; /* default len: all bytes that are available. */
11481
11482 /*
11483 * Only return the overlap between the specified part and the actual
11484 * string.
11485 */
11486 if (n < 0)
11487 {
11488 len += n;
11489 n = 0;
11490 }
11491 else if (n > slen)
11492 n = slen;
11493 if (len < 0)
11494 len = 0;
11495 else if (n + len > slen)
11496 len = slen - n;
11497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->v_type = VAR_STRING;
11499 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500}
11501
11502/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 * "strridx()" function
11504 */
11505 static void
11506f_strridx(argvars, rettv)
11507 typeval *argvars;
11508 typeval *rettv;
11509{
11510 char_u buf[NUMBUFLEN];
11511 char_u *needle;
11512 char_u *haystack;
11513 char_u *rest;
11514 char_u *lastmatch = NULL;
11515
11516 needle = get_tv_string(&argvars[1]);
11517 haystack = get_tv_string_buf(&argvars[0], buf);
11518 if (*needle == NUL)
11519 /* Empty string matches past the end. */
11520 lastmatch = haystack + STRLEN(haystack);
11521 else
11522 for (rest = haystack; *rest != '\0'; ++rest)
11523 {
11524 rest = (char_u *)strstr((char *)rest, (char *)needle);
11525 if (rest == NULL)
11526 break;
11527 lastmatch = rest;
11528 }
11529
11530 if (lastmatch == NULL)
11531 rettv->vval.v_number = -1;
11532 else
11533 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11534}
11535
11536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 * "strtrans()" function
11538 */
11539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011541 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->v_type = VAR_STRING;
11545 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546}
11547
11548/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549 * "submatch()" function
11550 */
11551 static void
11552f_submatch(argvars, rettv)
11553 typeval *argvars;
11554 typeval *rettv;
11555{
11556 rettv->v_type = VAR_STRING;
11557 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11558}
11559
11560/*
11561 * "substitute()" function
11562 */
11563 static void
11564f_substitute(argvars, rettv)
11565 typeval *argvars;
11566 typeval *rettv;
11567{
11568 char_u patbuf[NUMBUFLEN];
11569 char_u subbuf[NUMBUFLEN];
11570 char_u flagsbuf[NUMBUFLEN];
11571
11572 rettv->v_type = VAR_STRING;
11573 rettv->vval.v_string = do_string_sub(
11574 get_tv_string(&argvars[0]),
11575 get_tv_string_buf(&argvars[1], patbuf),
11576 get_tv_string_buf(&argvars[2], subbuf),
11577 get_tv_string_buf(&argvars[3], flagsbuf));
11578}
11579
11580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 * "synID(line, col, trans)" function
11582 */
11583/*ARGSUSED*/
11584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011586 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
11589 int id = 0;
11590#ifdef FEAT_SYN_HL
11591 long line;
11592 long col;
11593 int trans;
11594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 line = get_tv_lnum(argvars);
11596 col = get_tv_number(&argvars[1]) - 1;
11597 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
11599 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
11600 && col >= 0 && col < (long)STRLEN(ml_get(line)))
11601 id = syn_get_id(line, col, trans);
11602#endif
11603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605}
11606
11607/*
11608 * "synIDattr(id, what [, mode])" function
11609 */
11610/*ARGSUSED*/
11611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011613 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615{
11616 char_u *p = NULL;
11617#ifdef FEAT_SYN_HL
11618 int id;
11619 char_u *what;
11620 char_u *mode;
11621 char_u modebuf[NUMBUFLEN];
11622 int modec;
11623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 id = get_tv_number(&argvars[0]);
11625 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 modec = TOLOWER_ASC(mode[0]);
11630 if (modec != 't' && modec != 'c'
11631#ifdef FEAT_GUI
11632 && modec != 'g'
11633#endif
11634 )
11635 modec = 0; /* replace invalid with current */
11636 }
11637 else
11638 {
11639#ifdef FEAT_GUI
11640 if (gui.in_use)
11641 modec = 'g';
11642 else
11643#endif
11644 if (t_colors > 1)
11645 modec = 'c';
11646 else
11647 modec = 't';
11648 }
11649
11650
11651 switch (TOLOWER_ASC(what[0]))
11652 {
11653 case 'b':
11654 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11655 p = highlight_color(id, what, modec);
11656 else /* bold */
11657 p = highlight_has_attr(id, HL_BOLD, modec);
11658 break;
11659
11660 case 'f': /* fg[#] */
11661 p = highlight_color(id, what, modec);
11662 break;
11663
11664 case 'i':
11665 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11666 p = highlight_has_attr(id, HL_INVERSE, modec);
11667 else /* italic */
11668 p = highlight_has_attr(id, HL_ITALIC, modec);
11669 break;
11670
11671 case 'n': /* name */
11672 p = get_highlight_name(NULL, id - 1);
11673 break;
11674
11675 case 'r': /* reverse */
11676 p = highlight_has_attr(id, HL_INVERSE, modec);
11677 break;
11678
11679 case 's': /* standout */
11680 p = highlight_has_attr(id, HL_STANDOUT, modec);
11681 break;
11682
11683 case 'u': /* underline */
11684 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11685 break;
11686 }
11687
11688 if (p != NULL)
11689 p = vim_strsave(p);
11690#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->v_type = VAR_STRING;
11692 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693}
11694
11695/*
11696 * "synIDtrans(id)" function
11697 */
11698/*ARGSUSED*/
11699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011700f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011701 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703{
11704 int id;
11705
11706#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708
11709 if (id > 0)
11710 id = syn_get_final_id(id);
11711 else
11712#endif
11713 id = 0;
11714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716}
11717
11718/*
11719 * "system()" function
11720 */
11721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011723 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011726 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011728 char_u *infile = NULL;
11729 char_u buf[NUMBUFLEN];
11730 int err = FALSE;
11731 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011733 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011734 {
11735 /*
11736 * Write the string to a temp file, to be used for input of the shell
11737 * command.
11738 */
11739 if ((infile = vim_tempname('i')) == NULL)
11740 {
11741 EMSG(_(e_notmp));
11742 return;
11743 }
11744
11745 fd = mch_fopen((char *)infile, WRITEBIN);
11746 if (fd == NULL)
11747 {
11748 EMSG2(_(e_notopen), infile);
11749 goto done;
11750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011752 if (fwrite(p, STRLEN(p), 1, fd) != 1)
11753 err = TRUE;
11754 if (fclose(fd) != 0)
11755 err = TRUE;
11756 if (err)
11757 {
11758 EMSG(_("E677: Error writing temp file"));
11759 goto done;
11760 }
11761 }
11762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011764
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765#ifdef USE_CR
11766 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011767 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 {
11769 char_u *s;
11770
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011771 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 {
11773 if (*s == CAR)
11774 *s = NL;
11775 }
11776 }
11777#else
11778# ifdef USE_CRNL
11779 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011780 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 {
11782 char_u *s, *d;
11783
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011784 d = res;
11785 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 {
11787 if (s[0] == CAR && s[1] == NL)
11788 ++s;
11789 *d++ = *s;
11790 }
11791 *d = NUL;
11792 }
11793# endif
11794#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011795
11796done:
11797 if (infile != NULL)
11798 {
11799 mch_remove(infile);
11800 vim_free(infile);
11801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->v_type = VAR_STRING;
11803 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 * "tempname()" function
11808 */
11809/*ARGSUSED*/
11810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814{
11815 static int x = 'A';
11816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->v_type = VAR_STRING;
11818 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819
11820 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
11821 * names. Skip 'I' and 'O', they are used for shell redirection. */
11822 do
11823 {
11824 if (x == 'Z')
11825 x = '0';
11826 else if (x == '9')
11827 x = 'A';
11828 else
11829 {
11830#ifdef EBCDIC
11831 if (x == 'I')
11832 x = 'J';
11833 else if (x == 'R')
11834 x = 'S';
11835 else
11836#endif
11837 ++x;
11838 }
11839 } while (x == 'I' || x == 'O');
11840}
11841
11842/*
11843 * "tolower(string)" function
11844 */
11845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
11850 char_u *p;
11851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 p = vim_strsave(get_tv_string(&argvars[0]));
11853 rettv->v_type = VAR_STRING;
11854 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855
11856 if (p != NULL)
11857 while (*p != NUL)
11858 {
11859#ifdef FEAT_MBYTE
11860 int l;
11861
11862 if (enc_utf8)
11863 {
11864 int c, lc;
11865
11866 c = utf_ptr2char(p);
11867 lc = utf_tolower(c);
11868 l = utf_ptr2len_check(p);
11869 /* TODO: reallocate string when byte count changes. */
11870 if (utf_char2len(lc) == l)
11871 utf_char2bytes(lc, p);
11872 p += l;
11873 }
11874 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
11875 p += l; /* skip multi-byte character */
11876 else
11877#endif
11878 {
11879 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
11880 ++p;
11881 }
11882 }
11883}
11884
11885/*
11886 * "toupper(string)" function
11887 */
11888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011890 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011891 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892{
11893 char_u *p;
11894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895 p = vim_strsave(get_tv_string(&argvars[0]));
11896 rettv->v_type = VAR_STRING;
11897 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898
11899 if (p != NULL)
11900 while (*p != NUL)
11901 {
11902#ifdef FEAT_MBYTE
11903 int l;
11904
11905 if (enc_utf8)
11906 {
11907 int c, uc;
11908
11909 c = utf_ptr2char(p);
11910 uc = utf_toupper(c);
11911 l = utf_ptr2len_check(p);
11912 /* TODO: reallocate string when byte count changes. */
11913 if (utf_char2len(uc) == l)
11914 utf_char2bytes(uc, p);
11915 p += l;
11916 }
11917 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
11918 p += l; /* skip multi-byte character */
11919 else
11920#endif
11921 {
11922 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
11923 p++;
11924 }
11925 }
11926}
11927
11928/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000011929 * "tr(string, fromstr, tostr)" function
11930 */
11931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011933 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011935{
11936 char_u *instr;
11937 char_u *fromstr;
11938 char_u *tostr;
11939 char_u *p;
11940#ifdef FEAT_MBYTE
11941 int inlen;
11942 int fromlen;
11943 int tolen;
11944 int idx;
11945 char_u *cpstr;
11946 int cplen;
11947 int first = TRUE;
11948#endif
11949 char_u buf[NUMBUFLEN];
11950 char_u buf2[NUMBUFLEN];
11951 garray_T ga;
11952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 instr = get_tv_string(&argvars[0]);
11954 fromstr = get_tv_string_buf(&argvars[1], buf);
11955 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000011956
11957 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 rettv->v_type = VAR_STRING;
11959 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011960 ga_init2(&ga, (int)sizeof(char), 80);
11961
11962#ifdef FEAT_MBYTE
11963 if (!has_mbyte)
11964#endif
11965 /* not multi-byte: fromstr and tostr must be the same length */
11966 if (STRLEN(fromstr) != STRLEN(tostr))
11967 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011968#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000011969error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011970#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000011971 EMSG2(_(e_invarg2), fromstr);
11972 ga_clear(&ga);
11973 return;
11974 }
11975
11976 /* fromstr and tostr have to contain the same number of chars */
11977 while (*instr != NUL)
11978 {
11979#ifdef FEAT_MBYTE
11980 if (has_mbyte)
11981 {
11982 inlen = mb_ptr2len_check(instr);
11983 cpstr = instr;
11984 cplen = inlen;
11985 idx = 0;
11986 for (p = fromstr; *p != NUL; p += fromlen)
11987 {
11988 fromlen = mb_ptr2len_check(p);
11989 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
11990 {
11991 for (p = tostr; *p != NUL; p += tolen)
11992 {
11993 tolen = mb_ptr2len_check(p);
11994 if (idx-- == 0)
11995 {
11996 cplen = tolen;
11997 cpstr = p;
11998 break;
11999 }
12000 }
12001 if (*p == NUL) /* tostr is shorter than fromstr */
12002 goto error;
12003 break;
12004 }
12005 ++idx;
12006 }
12007
12008 if (first && cpstr == instr)
12009 {
12010 /* Check that fromstr and tostr have the same number of
12011 * (multi-byte) characters. Done only once when a character
12012 * of instr doesn't appear in fromstr. */
12013 first = FALSE;
12014 for (p = tostr; *p != NUL; p += tolen)
12015 {
12016 tolen = mb_ptr2len_check(p);
12017 --idx;
12018 }
12019 if (idx != 0)
12020 goto error;
12021 }
12022
12023 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012024 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012025 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012026
12027 instr += inlen;
12028 }
12029 else
12030#endif
12031 {
12032 /* When not using multi-byte chars we can do it faster. */
12033 p = vim_strchr(fromstr, *instr);
12034 if (p != NULL)
12035 ga_append(&ga, tostr[p - fromstr]);
12036 else
12037 ga_append(&ga, *instr);
12038 ++instr;
12039 }
12040 }
12041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012043}
12044
12045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 * "type(expr)" function
12047 */
12048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012050 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012053 int n;
12054
12055 switch (argvars[0].v_type)
12056 {
12057 case VAR_NUMBER: n = 0; break;
12058 case VAR_STRING: n = 1; break;
12059 case VAR_FUNC: n = 2; break;
12060 case VAR_LIST: n = 3; break;
12061 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12062 }
12063 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012067 * "values(dict)" function
12068 */
12069 static void
12070f_values(argvars, rettv)
12071 typeval *argvars;
12072 typeval *rettv;
12073{
12074 dict_list(argvars, rettv, 1);
12075}
12076
12077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 * "virtcol(string)" function
12079 */
12080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012082 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084{
12085 colnr_T vcol = 0;
12086 pos_T *fp;
12087
12088 fp = var2fpos(&argvars[0], FALSE);
12089 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12090 {
12091 getvvcol(curwin, fp, NULL, NULL, &vcol);
12092 ++vcol;
12093 }
12094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096}
12097
12098/*
12099 * "visualmode()" function
12100 */
12101/*ARGSUSED*/
12102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012104 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106{
12107#ifdef FEAT_VISUAL
12108 char_u str[2];
12109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012110 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 str[0] = curbuf->b_visual_mode_eval;
12112 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114
12115 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012116 if ((argvars[0].v_type == VAR_NUMBER
12117 && argvars[0].vval.v_number != 0)
12118 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 curbuf->b_visual_mode_eval = NUL;
12121#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123#endif
12124}
12125
12126/*
12127 * "winbufnr(nr)" function
12128 */
12129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012131 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133{
12134 win_T *wp;
12135
12136 wp = find_win_by_nr(&argvars[0]);
12137 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141}
12142
12143/*
12144 * "wincol()" function
12145 */
12146/*ARGSUSED*/
12147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012149 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151{
12152 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154}
12155
12156/*
12157 * "winheight(nr)" function
12158 */
12159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012161 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163{
12164 win_T *wp;
12165
12166 wp = find_win_by_nr(&argvars[0]);
12167 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171}
12172
12173/*
12174 * "winline()" function
12175 */
12176/*ARGSUSED*/
12177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012179 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185
12186/*
12187 * "winnr()" function
12188 */
12189/* ARGSUSED */
12190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012192 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195 int nr = 1;
12196#ifdef FEAT_WINDOWS
12197 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012198 win_T *twin = curwin;
12199 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012201 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012204 if (STRCMP(arg, "$") == 0)
12205 twin = lastwin;
12206 else if (STRCMP(arg, "#") == 0)
12207 {
12208 twin = prevwin;
12209 if (prevwin == NULL)
12210 nr = 0;
12211 }
12212 else
12213 {
12214 EMSG2(_(e_invexpr2), arg);
12215 nr = 0;
12216 }
12217 }
12218
12219 if (nr > 0)
12220 for (wp = firstwin; wp != twin; wp = wp->w_next)
12221 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224}
12225
12226/*
12227 * "winrestcmd()" function
12228 */
12229/* ARGSUSED */
12230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012232 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234{
12235#ifdef FEAT_WINDOWS
12236 win_T *wp;
12237 int winnr = 1;
12238 garray_T ga;
12239 char_u buf[50];
12240
12241 ga_init2(&ga, (int)sizeof(char), 70);
12242 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12243 {
12244 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12245 ga_concat(&ga, buf);
12246# ifdef FEAT_VERTSPLIT
12247 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12248 ga_concat(&ga, buf);
12249# endif
12250 ++winnr;
12251 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012252 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259}
12260
12261/*
12262 * "winwidth(nr)" function
12263 */
12264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012266 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268{
12269 win_T *wp;
12270
12271 wp = find_win_by_nr(&argvars[0]);
12272 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 else
12275#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279#endif
12280}
12281
12282 static win_T *
12283find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012284 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285{
12286#ifdef FEAT_WINDOWS
12287 win_T *wp;
12288#endif
12289 int nr;
12290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292
12293#ifdef FEAT_WINDOWS
12294 if (nr == 0)
12295 return curwin;
12296
12297 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12298 if (--nr <= 0)
12299 break;
12300 return wp;
12301#else
12302 if (nr == 0 || nr == 1)
12303 return curwin;
12304 return NULL;
12305#endif
12306}
12307
12308/*
12309 * Translate a String variable into a position.
12310 */
12311 static pos_T *
12312var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012313 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 int lnum; /* TRUE when $ is last line */
12315{
12316 char_u *name;
12317 static pos_T pos;
12318 pos_T *pp;
12319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 if (name[0] == '.') /* cursor */
12322 return &curwin->w_cursor;
12323 if (name[0] == '\'') /* mark */
12324 {
12325 pp = getmark(name[1], FALSE);
12326 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
12327 return NULL;
12328 return pp;
12329 }
12330 if (name[0] == '$') /* last column or line */
12331 {
12332 if (lnum)
12333 {
12334 pos.lnum = curbuf->b_ml.ml_line_count;
12335 pos.col = 0;
12336 }
12337 else
12338 {
12339 pos.lnum = curwin->w_cursor.lnum;
12340 pos.col = (colnr_T)STRLEN(ml_get_curline());
12341 }
12342 return &pos;
12343 }
12344 return NULL;
12345}
12346
12347/*
12348 * Get the length of an environment variable name.
12349 * Advance "arg" to the first character after the name.
12350 * Return 0 for error.
12351 */
12352 static int
12353get_env_len(arg)
12354 char_u **arg;
12355{
12356 char_u *p;
12357 int len;
12358
12359 for (p = *arg; vim_isIDc(*p); ++p)
12360 ;
12361 if (p == *arg) /* no name found */
12362 return 0;
12363
12364 len = (int)(p - *arg);
12365 *arg = p;
12366 return len;
12367}
12368
12369/*
12370 * Get the length of the name of a function or internal variable.
12371 * "arg" is advanced to the first non-white character after the name.
12372 * Return 0 if something is wrong.
12373 */
12374 static int
12375get_id_len(arg)
12376 char_u **arg;
12377{
12378 char_u *p;
12379 int len;
12380
12381 /* Find the end of the name. */
12382 for (p = *arg; eval_isnamec(*p); ++p)
12383 ;
12384 if (p == *arg) /* no name found */
12385 return 0;
12386
12387 len = (int)(p - *arg);
12388 *arg = skipwhite(p);
12389
12390 return len;
12391}
12392
12393/*
12394 * Get the length of the name of a function.
12395 * "arg" is advanced to the first non-white character after the name.
12396 * Return 0 if something is wrong.
12397 * If the name contains 'magic' {}'s, expand them and return the
12398 * expanded name in an allocated string via 'alias' - caller must free.
12399 */
12400 static int
12401get_func_len(arg, alias, evaluate)
12402 char_u **arg;
12403 char_u **alias;
12404 int evaluate;
12405{
12406 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 char_u *p;
12408 char_u *expr_start;
12409 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410
12411 *alias = NULL; /* default to no alias */
12412
12413 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
12414 && (*arg)[2] == (int)KE_SNR)
12415 {
12416 /* hard coded <SNR>, already translated */
12417 *arg += 3;
12418 return get_id_len(arg) + 3;
12419 }
12420 len = eval_fname_script(*arg);
12421 if (len > 0)
12422 {
12423 /* literal "<SID>", "s:" or "<SNR>" */
12424 *arg += len;
12425 }
12426
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 if (expr_start != NULL)
12432 {
12433 char_u *temp_string;
12434
12435 if (!evaluate)
12436 {
12437 len += (int)(p - *arg);
12438 *arg = skipwhite(p);
12439 return len;
12440 }
12441
12442 /*
12443 * Include any <SID> etc in the expanded string:
12444 * Thus the -len here.
12445 */
12446 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
12447 if (temp_string == NULL)
12448 return 0;
12449 *alias = temp_string;
12450 *arg = skipwhite(p);
12451 return (int)STRLEN(temp_string);
12452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453
12454 len += get_id_len(arg);
12455 if (len == 0)
12456 EMSG2(_(e_invexpr2), *arg);
12457
12458 return len;
12459}
12460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461/*
12462 * Find the end of a variable or function name, taking care of magic braces.
12463 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
12464 * start and end of the first magic braces item.
12465 * Return a pointer to just after the name. Equal to "arg" if there is no
12466 * valid name.
12467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 char_u *arg;
12471 char_u **expr_start;
12472 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012473 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 int mb_nest = 0;
12476 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 char_u *p;
12478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 *expr_start = NULL;
12482 *expr_end = NULL;
12483 }
12484
12485 for (p = arg; *p != NUL
12486 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012487 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000012488 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 || mb_nest != 0
12490 || br_nest != 0); ++p)
12491 {
12492 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 if (*p == '[')
12495 ++br_nest;
12496 else if (*p == ']')
12497 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 if (*p == '{')
12502 {
12503 mb_nest++;
12504 if (expr_start != NULL && *expr_start == NULL)
12505 *expr_start = p;
12506 }
12507 else if (*p == '}')
12508 {
12509 mb_nest--;
12510 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12511 *expr_end = p;
12512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 }
12515
12516 return p;
12517}
12518
12519/*
12520 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000012521 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 */
12523 static int
12524eval_isnamec(c)
12525 int c;
12526{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012527 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528}
12529
12530/*
12531 * Find a v: variable.
12532 * Return it's index, or -1 if not found.
12533 */
12534 static int
12535find_vim_var(name, len)
12536 char_u *name;
12537 int len; /* length of "name" */
12538{
12539 char_u *vname;
12540 int vlen;
12541 int i;
12542
12543 /*
12544 * Ignore "v:" for old built-in variables, require it for new ones.
12545 */
12546 if (name[0] == 'v' && name[1] == ':')
12547 {
12548 vname = name + 2;
12549 vlen = len - 2;
12550 }
12551 else
12552 {
12553 vname = name;
12554 vlen = len;
12555 }
12556 for (i = 0; i < VV_LEN; ++i)
12557 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
12558 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
12559 return i;
12560 return -1;
12561}
12562
12563/*
12564 * Set number v: variable to "val".
12565 */
12566 void
12567set_vim_var_nr(idx, val)
12568 int idx;
12569 long val;
12570{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012571 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573
12574/*
12575 * Get number v: variable value;
12576 */
12577 long
12578get_vim_var_nr(idx)
12579 int idx;
12580{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012581 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582}
12583
12584/*
12585 * Set v:count, v:count1 and v:prevcount.
12586 */
12587 void
12588set_vcount(count, count1)
12589 long count;
12590 long count1;
12591{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012592 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
12593 vimvars[VV_COUNT].vv_nr = count;
12594 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595}
12596
12597/*
12598 * Set string v: variable to a copy of "val".
12599 */
12600 void
12601set_vim_var_string(idx, val, len)
12602 int idx;
12603 char_u *val;
12604 int len; /* length of "val" to use or -1 (whole string) */
12605{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012606 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012608 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012610 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012612 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613}
12614
12615/*
12616 * Set v:register if needed.
12617 */
12618 void
12619set_reg_var(c)
12620 int c;
12621{
12622 char_u regname;
12623
12624 if (c == 0 || c == ' ')
12625 regname = '"';
12626 else
12627 regname = c;
12628 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012629 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 set_vim_var_string(VV_REG, &regname, 1);
12631}
12632
12633/*
12634 * Get or set v:exception. If "oldval" == NULL, return the current value.
12635 * Otherwise, restore the value to "oldval" and return NULL.
12636 * Must always be called in pairs to save and restore v:exception! Does not
12637 * take care of memory allocations.
12638 */
12639 char_u *
12640v_exception(oldval)
12641 char_u *oldval;
12642{
12643 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012644 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
Bram Moolenaare9a41262005-01-15 22:18:47 +000012646 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 return NULL;
12648}
12649
12650/*
12651 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
12652 * Otherwise, restore the value to "oldval" and return NULL.
12653 * Must always be called in pairs to save and restore v:throwpoint! Does not
12654 * take care of memory allocations.
12655 */
12656 char_u *
12657v_throwpoint(oldval)
12658 char_u *oldval;
12659{
12660 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012661 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662
Bram Moolenaare9a41262005-01-15 22:18:47 +000012663 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 return NULL;
12665}
12666
12667#if defined(FEAT_AUTOCMD) || defined(PROTO)
12668/*
12669 * Set v:cmdarg.
12670 * If "eap" != NULL, use "eap" to generate the value and return the old value.
12671 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
12672 * Must always be called in pairs!
12673 */
12674 char_u *
12675set_cmdarg(eap, oldarg)
12676 exarg_T *eap;
12677 char_u *oldarg;
12678{
12679 char_u *oldval;
12680 char_u *newval;
12681 unsigned len;
12682
Bram Moolenaare9a41262005-01-15 22:18:47 +000012683 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012684 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012686 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012687 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012688 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 }
12690
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012691 if (eap->force_bin == FORCE_BIN)
12692 len = 6;
12693 else if (eap->force_bin == FORCE_NOBIN)
12694 len = 8;
12695 else
12696 len = 0;
12697 if (eap->force_ff != 0)
12698 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
12699# ifdef FEAT_MBYTE
12700 if (eap->force_enc != 0)
12701 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
12702# endif
12703
12704 newval = alloc(len + 1);
12705 if (newval == NULL)
12706 return NULL;
12707
12708 if (eap->force_bin == FORCE_BIN)
12709 sprintf((char *)newval, " ++bin");
12710 else if (eap->force_bin == FORCE_NOBIN)
12711 sprintf((char *)newval, " ++nobin");
12712 else
12713 *newval = NUL;
12714 if (eap->force_ff != 0)
12715 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
12716 eap->cmd + eap->force_ff);
12717# ifdef FEAT_MBYTE
12718 if (eap->force_enc != 0)
12719 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
12720 eap->cmd + eap->force_enc);
12721# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000012722 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012723 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724}
12725#endif
12726
12727/*
12728 * Get the value of internal variable "name".
12729 * Return OK or FAIL.
12730 */
12731 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 char_u *name;
12734 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736{
12737 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012738 typeval *tv = NULL;
12739 typeval atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 VAR v;
12741 int cc;
12742 int i;
12743
12744 /* truncate the name, so that we can use strcmp() */
12745 cc = name[len];
12746 name[len] = NUL;
12747
12748 /*
12749 * Check for "b:changedtick".
12750 */
12751 if (STRCMP(name, "b:changedtick") == 0)
12752 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012753 atv.v_type = VAR_NUMBER;
12754 atv.vval.v_number = curbuf->b_changedtick;
12755 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 }
12757
12758 /*
12759 * Check for built-in v: variables.
12760 */
12761 else if ((i = find_vim_var(name, len)) >= 0)
12762 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012763 tv = &vimvars[i].tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 }
12765
12766 /*
12767 * Check for user-defined variables.
12768 */
12769 else
12770 {
12771 v = find_var(name, FALSE);
12772 if (v != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012773 tv = &v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 }
12775
Bram Moolenaare9a41262005-01-15 22:18:47 +000012776 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 if (rettv != NULL)
12779 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 ret = FAIL;
12781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012783 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784
12785 name[len] = cc;
12786
12787 return ret;
12788}
12789
12790/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012791 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
12792 * value).
12793 */
12794 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012796{
12797 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
12798}
12799
12800/*
12801 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 * The string "s" must have been allocated, it is consumed.
12803 * Return NULL for out of memory, the variable otherwise.
12804 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012805 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 char_u *s;
12808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv = alloc_tv();
12812 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->v_type = VAR_STRING;
12815 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 }
12817 else
12818 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820}
12821
12822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012823 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 */
12825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012827 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828{
12829 if (varp != NULL)
12830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012831 switch (varp->v_type)
12832 {
12833 case VAR_STRING:
12834 case VAR_FUNC:
12835 vim_free(varp->vval.v_string);
12836 break;
12837 case VAR_LIST:
12838 list_unref(varp->vval.v_list);
12839 break;
12840 default:
12841 break;
12842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 vim_free(varp);
12844 }
12845}
12846
12847/*
12848 * Free the memory for a variable value and set the value to NULL or 0.
12849 */
12850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012852 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853{
12854 if (varp != NULL)
12855 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012856 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012858 case VAR_STRING:
12859 case VAR_FUNC:
12860 vim_free(varp->vval.v_string);
12861 varp->vval.v_string = NULL;
12862 break;
12863 case VAR_LIST:
12864 list_unref(varp->vval.v_list);
12865 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012866 case VAR_DICT:
12867 dict_unref(varp->vval.v_dict);
12868 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012870 varp->vval.v_number = 0;
12871 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 case VAR_UNKNOWN:
12873 break;
12874 default:
12875 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 }
12878}
12879
12880/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 * Set the value of a variable to NULL without freeing items.
12882 */
12883 static void
12884init_tv(varp)
12885 typeval *varp;
12886{
12887 if (varp != NULL)
12888 vim_memset(varp, 0, sizeof(typeval));
12889}
12890
12891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 * Get the number value of a variable.
12893 * If it is a String variable, uses vim_str2nr().
12894 */
12895 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012897 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012901 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012903 case VAR_NUMBER:
12904 n = (long)(varp->vval.v_number);
12905 break;
12906 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012907 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012908 break;
12909 case VAR_STRING:
12910 if (varp->vval.v_string != NULL)
12911 vim_str2nr(varp->vval.v_string, NULL, NULL,
12912 TRUE, TRUE, &n, NULL);
12913 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012914 case VAR_LIST:
12915 EMSG(_("E703: Using a List as a number"));
12916 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012917 default:
12918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012920 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921}
12922
12923/*
12924 * Get the lnum from the first argument. Also accepts ".", "$", etc.
12925 */
12926 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012928 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 linenr_T lnum;
12932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 if (lnum == 0) /* no valid number, try using line() */
12935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 rettv.v_type = VAR_NUMBER;
12937 f_line(argvars, &rettv);
12938 lnum = rettv.vval.v_number;
12939 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 }
12941 return lnum;
12942}
12943
12944/*
12945 * Get the string value of a variable.
12946 * If it is a Number variable, the number is converted into a string.
12947 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
12948 * get_var_string_buf() uses a given buffer.
12949 * If the String variable has never been set, return an empty string.
12950 * Never returns NULL;
12951 */
12952 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012954 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955{
12956 static char_u mybuf[NUMBUFLEN];
12957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959}
12960
12961 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012963 typeval *varp;
12964 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012966 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012968 case VAR_NUMBER:
12969 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
12970 return buf;
12971 case VAR_FUNC:
Bram Moolenaar8c711452005-01-14 21:53:12 +000012972 EMSG(_("E999: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012973 break;
12974 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +000012975 EMSG(_("E999: using List as a String"));
12976 break;
12977 case VAR_DICT:
12978 EMSG(_("E999: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012979 break;
12980 case VAR_STRING:
12981 if (varp->vval.v_string != NULL)
12982 return varp->vval.v_string;
12983 break;
12984 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012988 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989}
12990
12991/*
12992 * Find variable "name" in the list of variables.
12993 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012994 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 */
12996 static VAR
12997find_var(name, writing)
12998 char_u *name;
12999 int writing;
13000{
13001 int i;
13002 char_u *varname;
13003 garray_T *gap;
13004
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 if (name[0] == 'a' && name[1] == ':')
13006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013007 /* Function arguments "a:".
13008 * NOTE: We use a typecast, because function arguments don't have a
13009 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 if (writing)
13011 {
13012 EMSG2(_(e_readonlyvar), name);
13013 return NULL;
13014 }
13015 name += 2;
13016 if (current_funccal == NULL)
13017 return NULL;
13018 if (VIM_ISDIGIT(*name))
13019 {
13020 i = atol((char *)name);
13021 if (i == 0) /* a:0 */
13022 return &current_funccal->a0_var;
13023 i += current_funccal->func->args.ga_len;
13024 if (i > current_funccal->argcount) /* a:999 */
13025 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013026 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 }
13028 if (STRCMP(name, "firstline") == 0)
13029 return &(current_funccal->firstline);
13030 if (STRCMP(name, "lastline") == 0)
13031 return &(current_funccal->lastline);
13032 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
13033 if (STRCMP(name, ((char_u **)
13034 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013035 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 return NULL;
13037 }
13038
13039 gap = find_var_ga(name, &varname);
13040 if (gap == NULL)
13041 return NULL;
13042 return find_var_in_ga(gap, varname);
13043}
13044
13045 static VAR
13046find_var_in_ga(gap, varname)
13047 garray_T *gap;
13048 char_u *varname;
13049{
13050 int i;
13051
13052 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013053 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
13054 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 break;
13056 if (i < 0)
13057 return NULL;
13058 return &VAR_GAP_ENTRY(i, gap);
13059}
13060
13061/*
13062 * Find the growarray and start of name without ':' for a variable name.
13063 */
13064 static garray_T *
13065find_var_ga(name, varname)
13066 char_u *name;
13067 char_u **varname;
13068{
13069 if (name[1] != ':')
13070 {
13071 /* If not "x:name" there must not be any ":" in the name. */
13072 if (vim_strchr(name, ':') != NULL)
13073 return NULL;
13074 *varname = name;
13075 if (current_funccal == NULL)
13076 return &variables; /* global variable */
13077 return &current_funccal->l_vars; /* local function variable */
13078 }
13079 *varname = name + 2;
13080 if (*name == 'b') /* buffer variable */
13081 return &curbuf->b_vars;
13082 if (*name == 'w') /* window variable */
13083 return &curwin->w_vars;
13084 if (*name == 'g') /* global variable */
13085 return &variables;
13086 if (*name == 'l' && current_funccal != NULL)/* local function variable */
13087 return &current_funccal->l_vars;
13088 if (*name == 's' /* script variable */
13089 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
13090 return &SCRIPT_VARS(current_SID);
13091 return NULL;
13092}
13093
13094/*
13095 * Get the string value of a (global/local) variable.
13096 * Returns NULL when it doesn't exist.
13097 */
13098 char_u *
13099get_var_value(name)
13100 char_u *name;
13101{
13102 VAR v;
13103
13104 v = find_var(name, FALSE);
13105 if (v == NULL)
13106 return NULL;
13107 return get_var_string(v);
13108}
13109
13110/*
13111 * Allocate a new growarry for a sourced script. It will be used while
13112 * sourcing this script and when executing functions defined in the script.
13113 */
13114 void
13115new_script_vars(id)
13116 scid_T id;
13117{
13118 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
13119 {
13120 while (ga_scripts.ga_len < id)
13121 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013122 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 }
13125 }
13126}
13127
13128/*
13129 * Initialize internal variables for use.
13130 */
13131 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013132vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 garray_T *gap;
13134{
13135 ga_init2(gap, (int)sizeof(var), 4);
13136}
13137
13138/*
13139 * Clean up a list of internal variables.
13140 */
13141 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013142vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 garray_T *gap;
13144{
13145 int i;
13146
13147 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013148 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 ga_clear(gap);
13150}
13151
13152 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013153clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 VAR v;
13155{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013156 vim_free(v->v_name);
13157 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
13162 * List the value of one internal variable.
13163 */
13164 static void
13165list_one_var(v, prefix)
13166 VAR v;
13167 char_u *prefix;
13168{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 char_u *tofree;
13170 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013171 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013172
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013173 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013174 list_one_var_a(prefix, v->v_name, v->tv.v_type,
13175 s == NULL ? (char_u *)"" : s);
13176 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177}
13178
13179/*
13180 * List the value of one "v:" variable.
13181 */
13182 static void
13183list_vim_var(i)
13184 int i; /* index in vimvars[] */
13185{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013186 char_u *tofree;
13187 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 char_u numbuf[NUMBUFLEN];
13189
Bram Moolenaare9a41262005-01-15 22:18:47 +000013190 s = echo_string(&vimvars[i].tv, &tofree, numbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
Bram Moolenaare9a41262005-01-15 22:18:47 +000013192 vimvars[i].tv.v_type, s == NULL ? (char_u *)"" : s);
13193 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194}
13195
13196 static void
13197list_one_var_a(prefix, name, type, string)
13198 char_u *prefix;
13199 char_u *name;
13200 int type;
13201 char_u *string;
13202{
13203 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13204 if (name != NULL) /* "a:" vars don't have a name stored */
13205 msg_puts(name);
13206 msg_putchar(' ');
13207 msg_advance(22);
13208 if (type == VAR_NUMBER)
13209 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 else if (type == VAR_FUNC)
13211 msg_putchar('*');
13212 else if (type == VAR_LIST)
13213 {
13214 msg_putchar('[');
13215 if (*string == '[')
13216 ++string;
13217 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013218 else if (type == VAR_DICT)
13219 {
13220 msg_putchar('{');
13221 if (*string == '{')
13222 ++string;
13223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 else
13225 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013226
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228
13229 if (type == VAR_FUNC)
13230 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231}
13232
13233/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 * If the variable already exists, the value is updated.
13236 * Otherwise the variable is created.
13237 */
13238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 typeval *tv;
13242 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243{
13244 int i;
13245 VAR v;
13246 char_u *varname;
13247 garray_T *gap;
13248
13249 /*
13250 * Handle setting internal v: variables.
13251 */
13252 i = find_vim_var(name, (int)STRLEN(name));
13253 if (i >= 0)
13254 {
13255 if (vimvars[i].flags & VV_RO)
13256 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000013257 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
13258 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 else
13260 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013261 if (vimvars[i].tv.v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013263 vim_free(vimvars[i].vv_str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013265 vimvars[i].vv_str = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013266 else
13267 {
13268 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013269 vimvars[i].vv_str = tv->vval.v_string;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 }
13273 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013274 vimvars[i].vv_nr = get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 }
13276 return;
13277 }
13278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280 {
13281 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13282 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13283 ? name[2] : name[0]))
13284 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013285 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013286 return;
13287 }
13288 if (function_exists(name))
13289 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013290 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013291 return;
13292 }
13293 }
13294
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 v = find_var(name, TRUE);
13296 if (v != NULL) /* existing variable, only need to free string */
13297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013299 && !((v->tv.v_type == VAR_STRING
13300 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 && (tv->v_type == VAR_STRING
13302 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013303 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013304 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013305 return;
13306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 }
13309 else /* add a new variable */
13310 {
13311 gap = find_var_ga(name, &varname);
13312 if (gap == NULL) /* illegal name */
13313 {
13314 EMSG2(_("E461: Illegal variable name: %s"), name);
13315 return;
13316 }
13317
13318 /* Try to use an empty entry */
13319 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013320 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 break;
13322 if (i < 0) /* need to allocate more room */
13323 {
13324 if (ga_grow(gap, 1) == FAIL)
13325 return;
13326 i = gap->ga_len;
13327 }
13328 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013329 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 return;
13331 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 if (copy || tv->v_type == VAR_NUMBER)
13335 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013336 else
13337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 v->tv = *tv;
13339 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341}
13342
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013343/*
13344 * Copy the values from typeval "from" to typeval "to".
13345 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013346 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013349copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013350 typeval *from;
13351 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013353 to->v_type = from->v_type;
13354 switch (from->v_type)
13355 {
13356 case VAR_NUMBER:
13357 to->vval.v_number = from->vval.v_number;
13358 break;
13359 case VAR_STRING:
13360 case VAR_FUNC:
13361 if (from->vval.v_string == NULL)
13362 to->vval.v_string = NULL;
13363 else
13364 to->vval.v_string = vim_strsave(from->vval.v_string);
13365 break;
13366 case VAR_LIST:
13367 if (from->vval.v_list == NULL)
13368 to->vval.v_list = NULL;
13369 else
13370 {
13371 to->vval.v_list = from->vval.v_list;
13372 ++to->vval.v_list->lv_refcount;
13373 }
13374 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013375 case VAR_DICT:
13376 if (from->vval.v_dict == NULL)
13377 to->vval.v_dict = NULL;
13378 else
13379 {
13380 to->vval.v_dict = from->vval.v_dict;
13381 ++to->vval.v_dict->dv_refcount;
13382 }
13383 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013384 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013386 break;
13387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388}
13389
13390/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013391 * Make a copy of an item.
13392 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
13393 */
13394 static void
13395item_copy(from, to, deep)
13396 typeval *from;
13397 typeval *to;
13398 int deep;
13399{
13400 static int recurse = 0;
13401
13402 if (recurse >= VAR_MAXNEST)
13403 {
13404 EMSG(_("E698: variable nested too deep for making a copy"));
13405 return;
13406 }
13407 ++recurse;
13408
13409 switch (from->v_type)
13410 {
13411 case VAR_NUMBER:
13412 case VAR_STRING:
13413 case VAR_FUNC:
13414 copy_tv(from, to);
13415 break;
13416 case VAR_LIST:
13417 to->v_type = VAR_LIST;
13418 to->vval.v_list = list_copy(from->vval.v_list, deep);
13419 break;
13420 case VAR_DICT:
13421 to->v_type = VAR_DICT;
13422 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
13423 break;
13424 default:
13425 EMSG2(_(e_intern2), "item_copy()");
13426 }
13427 --recurse;
13428}
13429
13430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 * ":echo expr1 ..." print each argument separated with a space, add a
13432 * newline at the end.
13433 * ":echon expr1 ..." print each argument plain.
13434 */
13435 void
13436ex_echo(eap)
13437 exarg_T *eap;
13438{
13439 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013440 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013441 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 char_u *p;
13443 int needclr = TRUE;
13444 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013445 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446
13447 if (eap->skip)
13448 ++emsg_skip;
13449 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
13450 {
13451 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 {
13454 /*
13455 * Report the invalid expression unless the expression evaluation
13456 * has been cancelled due to an aborting error, an interrupt, or an
13457 * exception.
13458 */
13459 if (!aborting())
13460 EMSG2(_(e_invexpr2), p);
13461 break;
13462 }
13463 if (!eap->skip)
13464 {
13465 if (atstart)
13466 {
13467 atstart = FALSE;
13468 /* Call msg_start() after eval1(), evaluating the expression
13469 * may cause a message to appear. */
13470 if (eap->cmdidx == CMD_echo)
13471 msg_start();
13472 }
13473 else if (eap->cmdidx == CMD_echo)
13474 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013475 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013476 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 if (*p == '\n' || *p == '\r' || *p == TAB)
13478 {
13479 if (*p != TAB && needclr)
13480 {
13481 /* remove any text still there from the command */
13482 msg_clr_eos();
13483 needclr = FALSE;
13484 }
13485 msg_putchar_attr(*p, echo_attr);
13486 }
13487 else
13488 {
13489#ifdef FEAT_MBYTE
13490 if (has_mbyte)
13491 {
13492 int i = (*mb_ptr2len_check)(p);
13493
13494 (void)msg_outtrans_len_attr(p, i, echo_attr);
13495 p += i - 1;
13496 }
13497 else
13498#endif
13499 (void)msg_outtrans_len_attr(p, 1, echo_attr);
13500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013501 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 arg = skipwhite(arg);
13505 }
13506 eap->nextcmd = check_nextcmd(arg);
13507
13508 if (eap->skip)
13509 --emsg_skip;
13510 else
13511 {
13512 /* remove text that may still be there from the command */
13513 if (needclr)
13514 msg_clr_eos();
13515 if (eap->cmdidx == CMD_echo)
13516 msg_end();
13517 }
13518}
13519
13520/*
13521 * ":echohl {name}".
13522 */
13523 void
13524ex_echohl(eap)
13525 exarg_T *eap;
13526{
13527 int id;
13528
13529 id = syn_name2id(eap->arg);
13530 if (id == 0)
13531 echo_attr = 0;
13532 else
13533 echo_attr = syn_id2attr(id);
13534}
13535
13536/*
13537 * ":execute expr1 ..." execute the result of an expression.
13538 * ":echomsg expr1 ..." Print a message
13539 * ":echoerr expr1 ..." Print an error
13540 * Each gets spaces around each argument and a newline at the end for
13541 * echo commands
13542 */
13543 void
13544ex_execute(eap)
13545 exarg_T *eap;
13546{
13547 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 int ret = OK;
13550 char_u *p;
13551 garray_T ga;
13552 int len;
13553 int save_did_emsg;
13554
13555 ga_init2(&ga, 1, 80);
13556
13557 if (eap->skip)
13558 ++emsg_skip;
13559 while (*arg != NUL && *arg != '|' && *arg != '\n')
13560 {
13561 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 {
13564 /*
13565 * Report the invalid expression unless the expression evaluation
13566 * has been cancelled due to an aborting error, an interrupt, or an
13567 * exception.
13568 */
13569 if (!aborting())
13570 EMSG2(_(e_invexpr2), p);
13571 ret = FAIL;
13572 break;
13573 }
13574
13575 if (!eap->skip)
13576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 len = (int)STRLEN(p);
13579 if (ga_grow(&ga, len + 2) == FAIL)
13580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 ret = FAIL;
13583 break;
13584 }
13585 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 ga.ga_len += len;
13589 }
13590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 arg = skipwhite(arg);
13593 }
13594
13595 if (ret != FAIL && ga.ga_data != NULL)
13596 {
13597 if (eap->cmdidx == CMD_echomsg)
13598 MSG_ATTR(ga.ga_data, echo_attr);
13599 else if (eap->cmdidx == CMD_echoerr)
13600 {
13601 /* We don't want to abort following commands, restore did_emsg. */
13602 save_did_emsg = did_emsg;
13603 EMSG((char_u *)ga.ga_data);
13604 if (!force_abort)
13605 did_emsg = save_did_emsg;
13606 }
13607 else if (eap->cmdidx == CMD_execute)
13608 do_cmdline((char_u *)ga.ga_data,
13609 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
13610 }
13611
13612 ga_clear(&ga);
13613
13614 if (eap->skip)
13615 --emsg_skip;
13616
13617 eap->nextcmd = check_nextcmd(arg);
13618}
13619
13620/*
13621 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
13622 * "arg" points to the "&" or '+' when called, to "option" when returning.
13623 * Returns NULL when no option name found. Otherwise pointer to the char
13624 * after the option name.
13625 */
13626 static char_u *
13627find_option_end(arg, opt_flags)
13628 char_u **arg;
13629 int *opt_flags;
13630{
13631 char_u *p = *arg;
13632
13633 ++p;
13634 if (*p == 'g' && p[1] == ':')
13635 {
13636 *opt_flags = OPT_GLOBAL;
13637 p += 2;
13638 }
13639 else if (*p == 'l' && p[1] == ':')
13640 {
13641 *opt_flags = OPT_LOCAL;
13642 p += 2;
13643 }
13644 else
13645 *opt_flags = 0;
13646
13647 if (!ASCII_ISALPHA(*p))
13648 return NULL;
13649 *arg = p;
13650
13651 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
13652 p += 4; /* termcap option */
13653 else
13654 while (ASCII_ISALPHA(*p))
13655 ++p;
13656 return p;
13657}
13658
13659/*
13660 * ":function"
13661 */
13662 void
13663ex_function(eap)
13664 exarg_T *eap;
13665{
13666 char_u *theline;
13667 int j;
13668 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 char_u *name = NULL;
13671 char_u *p;
13672 char_u *arg;
13673 garray_T newargs;
13674 garray_T newlines;
13675 int varargs = FALSE;
13676 int mustend = FALSE;
13677 int flags = 0;
13678 ufunc_T *fp;
13679 int indent;
13680 int nesting;
13681 char_u *skip_until = NULL;
13682 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013683 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
13685 /*
13686 * ":function" without argument: list functions.
13687 */
13688 if (ends_excmd(*eap->arg))
13689 {
13690 if (!eap->skip)
13691 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
13692 list_func_head(fp, FALSE);
13693 eap->nextcmd = check_nextcmd(eap->arg);
13694 return;
13695 }
13696
13697 p = eap->arg;
13698 name = trans_function_name(&p, eap->skip, FALSE);
13699 if (name == NULL && !eap->skip)
13700 {
13701 /*
13702 * Return on an invalid expression in braces, unless the expression
13703 * evaluation has been cancelled due to an aborting error, an
13704 * interrupt, or an exception.
13705 */
13706 if (!aborting())
13707 return;
13708 else
13709 eap->skip = TRUE;
13710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 /* An error in a function call during evaluation of an expression in magic
13712 * braces should not cause the function not to be defined. */
13713 saved_did_emsg = did_emsg;
13714 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715
13716 /*
13717 * ":function func" with only function name: list function.
13718 */
13719 if (vim_strchr(p, '(') == NULL)
13720 {
13721 if (!ends_excmd(*skipwhite(p)))
13722 {
13723 EMSG(_(e_trailing));
13724 goto erret_name;
13725 }
13726 eap->nextcmd = check_nextcmd(p);
13727 if (eap->nextcmd != NULL)
13728 *p = NUL;
13729 if (!eap->skip && !got_int)
13730 {
13731 fp = find_func(name);
13732 if (fp != NULL)
13733 {
13734 list_func_head(fp, TRUE);
13735 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
13736 {
13737 msg_putchar('\n');
13738 msg_outnum((long)(j + 1));
13739 if (j < 9)
13740 msg_putchar(' ');
13741 if (j < 99)
13742 msg_putchar(' ');
13743 msg_prt_line(FUNCLINE(fp, j));
13744 out_flush(); /* show a line at a time */
13745 ui_breakcheck();
13746 }
13747 if (!got_int)
13748 {
13749 msg_putchar('\n');
13750 msg_puts((char_u *)" endfunction");
13751 }
13752 }
13753 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013754 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 }
13756 goto erret_name;
13757 }
13758
13759 /*
13760 * ":function name(arg1, arg2)" Define function.
13761 */
13762 p = skipwhite(p);
13763 if (*p != '(')
13764 {
13765 if (!eap->skip)
13766 {
13767 EMSG2(_("E124: Missing '(': %s"), eap->arg);
13768 goto erret_name;
13769 }
13770 /* attempt to continue by skipping some text */
13771 if (vim_strchr(p, '(') != NULL)
13772 p = vim_strchr(p, '(');
13773 }
13774 p = skipwhite(p + 1);
13775
13776 ga_init2(&newargs, (int)sizeof(char_u *), 3);
13777 ga_init2(&newlines, (int)sizeof(char_u *), 3);
13778
13779 /*
13780 * Isolate the arguments: "arg1, arg2, ...)"
13781 */
13782 while (*p != ')')
13783 {
13784 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
13785 {
13786 varargs = TRUE;
13787 p += 3;
13788 mustend = TRUE;
13789 }
13790 else
13791 {
13792 arg = p;
13793 while (ASCII_ISALNUM(*p) || *p == '_')
13794 ++p;
13795 if (arg == p || isdigit(*arg)
13796 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
13797 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
13798 {
13799 if (!eap->skip)
13800 EMSG2(_("E125: Illegal argument: %s"), arg);
13801 break;
13802 }
13803 if (ga_grow(&newargs, 1) == FAIL)
13804 goto erret;
13805 c = *p;
13806 *p = NUL;
13807 arg = vim_strsave(arg);
13808 if (arg == NULL)
13809 goto erret;
13810 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
13811 *p = c;
13812 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 if (*p == ',')
13814 ++p;
13815 else
13816 mustend = TRUE;
13817 }
13818 p = skipwhite(p);
13819 if (mustend && *p != ')')
13820 {
13821 if (!eap->skip)
13822 EMSG2(_(e_invarg2), eap->arg);
13823 break;
13824 }
13825 }
13826 ++p; /* skip the ')' */
13827
Bram Moolenaare9a41262005-01-15 22:18:47 +000013828 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 for (;;)
13830 {
13831 p = skipwhite(p);
13832 if (STRNCMP(p, "range", 5) == 0)
13833 {
13834 flags |= FC_RANGE;
13835 p += 5;
13836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013837 else if (STRNCMP(p, "dict", 4) == 0)
13838 {
13839 flags |= FC_DICT;
13840 p += 4;
13841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 else if (STRNCMP(p, "abort", 5) == 0)
13843 {
13844 flags |= FC_ABORT;
13845 p += 5;
13846 }
13847 else
13848 break;
13849 }
13850
13851 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
13852 EMSG(_(e_trailing));
13853
13854 /*
13855 * Read the body of the function, until ":endfunction" is found.
13856 */
13857 if (KeyTyped)
13858 {
13859 /* Check if the function already exists, don't let the user type the
13860 * whole function before telling him it doesn't work! For a script we
13861 * need to skip the body to be able to find what follows. */
13862 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
13863 EMSG2(_(e_funcexts), name);
13864
13865 msg_putchar('\n'); /* don't overwrite the function name */
13866 cmdline_row = msg_row;
13867 }
13868
13869 indent = 2;
13870 nesting = 0;
13871 for (;;)
13872 {
13873 msg_scroll = TRUE;
13874 need_wait_return = FALSE;
13875 if (eap->getline == NULL)
13876 theline = getcmdline(':', 0L, indent);
13877 else
13878 theline = eap->getline(':', eap->cookie, indent);
13879 if (KeyTyped)
13880 lines_left = Rows - 1;
13881 if (theline == NULL)
13882 {
13883 EMSG(_("E126: Missing :endfunction"));
13884 goto erret;
13885 }
13886
13887 if (skip_until != NULL)
13888 {
13889 /* between ":append" and "." and between ":python <<EOF" and "EOF"
13890 * don't check for ":endfunc". */
13891 if (STRCMP(theline, skip_until) == 0)
13892 {
13893 vim_free(skip_until);
13894 skip_until = NULL;
13895 }
13896 }
13897 else
13898 {
13899 /* skip ':' and blanks*/
13900 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
13901 ;
13902
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013903 /* Check for "endfunction". */
13904 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 {
13906 vim_free(theline);
13907 break;
13908 }
13909
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013910 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 * at "end". */
13912 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
13913 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013914 else if (STRNCMP(p, "if", 2) == 0
13915 || STRNCMP(p, "wh", 2) == 0
13916 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 || STRNCMP(p, "try", 3) == 0)
13918 indent += 2;
13919
13920 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013921 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 if (*p == '!')
13924 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 p += eval_fname_script(p);
13926 if (ASCII_ISALPHA(*p))
13927 {
13928 vim_free(trans_function_name(&p, TRUE, FALSE));
13929 if (*skipwhite(p) == '(')
13930 {
13931 ++nesting;
13932 indent += 2;
13933 }
13934 }
13935 }
13936
13937 /* Check for ":append" or ":insert". */
13938 p = skip_range(p, NULL);
13939 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
13940 || (p[0] == 'i'
13941 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
13942 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
13943 skip_until = vim_strsave((char_u *)".");
13944
13945 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
13946 arg = skipwhite(skiptowhite(p));
13947 if (arg[0] == '<' && arg[1] =='<'
13948 && ((p[0] == 'p' && p[1] == 'y'
13949 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
13950 || (p[0] == 'p' && p[1] == 'e'
13951 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
13952 || (p[0] == 't' && p[1] == 'c'
13953 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
13954 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
13955 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013956 || (p[0] == 'm' && p[1] == 'z'
13957 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 ))
13959 {
13960 /* ":python <<" continues until a dot, like ":append" */
13961 p = skipwhite(arg + 2);
13962 if (*p == NUL)
13963 skip_until = vim_strsave((char_u *)".");
13964 else
13965 skip_until = vim_strsave(p);
13966 }
13967 }
13968
13969 /* Add the line to the function. */
13970 if (ga_grow(&newlines, 1) == FAIL)
13971 goto erret;
13972 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
13973 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 }
13975
13976 /* Don't define the function when skipping commands or when an error was
13977 * detected. */
13978 if (eap->skip || did_emsg)
13979 goto erret;
13980
13981 /*
13982 * If there are no errors, add the function
13983 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984 v = find_var(name, FALSE);
13985 if (v != NULL && v->tv.v_type == VAR_FUNC)
13986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013987 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013988 goto erret;
13989 }
13990
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 fp = find_func(name);
13992 if (fp != NULL)
13993 {
13994 if (!eap->forceit)
13995 {
13996 EMSG2(_(e_funcexts), name);
13997 goto erret;
13998 }
13999 if (fp->calls)
14000 {
14001 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
14002 goto erret;
14003 }
14004 /* redefine existing function */
14005 ga_clear_strings(&(fp->args));
14006 ga_clear_strings(&(fp->lines));
14007 vim_free(name);
14008 }
14009 else
14010 {
14011 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14012 if (fp == NULL)
14013 goto erret;
14014 /* insert the new function in the function list */
14015 fp->next = firstfunc;
14016 firstfunc = fp;
14017 fp->name = name;
14018 }
14019 fp->args = newargs;
14020 fp->lines = newlines;
14021 fp->varargs = varargs;
14022 fp->flags = flags;
14023 fp->calls = 0;
14024 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 vim_free(skip_until);
14027 return;
14028
14029erret:
14030 vim_free(skip_until);
14031 ga_clear_strings(&newargs);
14032 ga_clear_strings(&newlines);
14033erret_name:
14034 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036}
14037
14038/*
14039 * Get a function name, translating "<SID>" and "<SNR>".
14040 * Returns the function name in allocated memory, or NULL for failure.
14041 * Advances "pp" to just after the function name (if no error).
14042 */
14043 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014044trans_function_name(pp, skip, exists)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 char_u **pp;
14046 int skip; /* only find the end, don't evaluate */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014047 int exists; /* TRUE for exists(): internal function name
14048 OK and be quiet. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014050 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 char_u *start;
14052 char_u *end;
14053 int lead;
14054 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 int len;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014056#if 0
14057 char_u *expr_start, *expr_end;
14058 char_u *temp_string = NULL;
14059#else
14060 lval lv;
14061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062
14063 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
14064 start = *pp;
14065 lead = eval_fname_script(start);
14066 if (lead > 0)
14067 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014068
14069#if 1
14070 end = get_lval(start, NULL, &lv, FALSE, skip, exists);
14071 if (end == start)
14072 {
14073 if (!skip)
14074 EMSG(_("E129: Function name required"));
14075 goto theend;
14076 }
14077 if (end == NULL || (lv.ll_tv != NULL && (lead > 0 || lv.ll_range)))
14078 {
14079 /*
14080 * Report an invalid expression in braces, unless the expression
14081 * evaluation has been cancelled due to an aborting error, an
14082 * interrupt, or an exception.
14083 */
14084 if (!aborting())
14085 {
14086 if (end != NULL)
14087 EMSG2(_(e_invarg2), start);
14088 }
14089 else
14090 *pp = find_name_end(start, NULL, NULL, TRUE);
14091 goto theend;
14092 }
14093
14094 if (lv.ll_tv != NULL)
14095 {
14096 /* TODO: When defining a function accept a Dict here. */
14097 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14098 {
14099 name = vim_strsave(lv.ll_tv->vval.v_string);
14100 *pp = end;
14101 }
14102 else
14103 {
14104 if (!skip && !exists)
14105 EMSG(_("E999: Funcref required"));
14106 name = NULL;
14107 }
14108 goto theend;
14109 }
14110
14111 if (lv.ll_name == NULL)
14112 {
14113 /* Error found, but continue after the function name. */
14114 *pp = end;
14115 goto theend;
14116 }
14117
14118 if (lv.ll_exp_name != NULL)
14119 len = STRLEN(lv.ll_exp_name);
14120 else
14121 len = (int)(end - start);
14122
14123 /*
14124 * Copy the function name to allocated memory.
14125 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14126 * Accept <SNR>123_name() outside a script.
14127 */
14128 if (skip)
14129 lead = 0; /* do nothing */
14130 else if (lead > 0)
14131 {
14132 lead = 3;
14133 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14134 {
14135 if (current_SID <= 0)
14136 {
14137 EMSG(_(e_usingsid));
14138 goto theend;
14139 }
14140 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14141 lead += (int)STRLEN(sid_buf);
14142 }
14143 }
14144 else if (!exists && !ASCII_ISUPPER(*lv.ll_name))
14145 {
14146 EMSG2(_("E128: Function name must start with a capital: %s"),
14147 lv.ll_name);
14148 goto theend;
14149 }
14150 name = alloc((unsigned)(len + lead + 1));
14151 if (name != NULL)
14152 {
14153 if (lead > 0)
14154 {
14155 name[0] = K_SPECIAL;
14156 name[1] = KS_EXTRA;
14157 name[2] = (int)KE_SNR;
14158 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14159 STRCPY(name + 3, sid_buf);
14160 }
14161 mch_memmove(name + lead, lv.ll_name, (size_t)len);
14162 name[len + lead] = NUL;
14163 }
14164 *pp = end;
14165
14166theend:
14167 clear_lval(&lv);
14168 return name;
14169#endif
14170
14171#if 0
14172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014173 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 if (end == start)
14175 {
14176 if (!skip)
14177 EMSG(_("E129: Function name required"));
14178 return NULL;
14179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 if (expr_start != NULL && !skip)
14181 {
14182 /* expand magic curlies */
14183 temp_string = make_expanded_name(start, expr_start, expr_end, end);
14184 if (temp_string == NULL)
14185 {
14186 /*
14187 * Report an invalid expression in braces, unless the expression
14188 * evaluation has been cancelled due to an aborting error, an
14189 * interrupt, or an exception.
14190 */
14191 if (!aborting())
14192 EMSG2(_(e_invarg2), start);
14193 else
14194 *pp = end;
14195 return NULL;
14196 }
14197 start = temp_string;
14198 len = (int)STRLEN(temp_string);
14199 }
14200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 len = (int)(end - start);
14202
14203 /*
14204 * Copy the function name to allocated memory.
14205 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14206 * Accept <SNR>123_name() outside a script.
14207 */
14208 if (skip)
14209 lead = 0; /* do nothing */
14210 else if (lead > 0)
14211 {
14212 lead = 3;
14213 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14214 {
14215 if (current_SID <= 0)
14216 {
14217 EMSG(_(e_usingsid));
14218 return NULL;
14219 }
14220 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14221 lead += (int)STRLEN(sid_buf);
14222 }
14223 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014224 else if (!exists && !ASCII_ISUPPER(*start))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 {
14226 EMSG2(_("E128: Function name must start with a capital: %s"), start);
14227 return NULL;
14228 }
14229 name = alloc((unsigned)(len + lead + 1));
14230 if (name != NULL)
14231 {
14232 if (lead > 0)
14233 {
14234 name[0] = K_SPECIAL;
14235 name[1] = KS_EXTRA;
14236 name[2] = (int)KE_SNR;
14237 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14238 STRCPY(name + 3, sid_buf);
14239 }
14240 mch_memmove(name + lead, start, (size_t)len);
14241 name[len + lead] = NUL;
14242 }
14243 *pp = end;
14244
14245 vim_free(temp_string);
14246 return name;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248}
14249
14250/*
14251 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14252 * Return 2 if "p" starts with "s:".
14253 * Return 0 otherwise.
14254 */
14255 static int
14256eval_fname_script(p)
14257 char_u *p;
14258{
14259 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
14260 || STRNICMP(p + 1, "SNR>", 4) == 0))
14261 return 5;
14262 if (p[0] == 's' && p[1] == ':')
14263 return 2;
14264 return 0;
14265}
14266
14267/*
14268 * Return TRUE if "p" starts with "<SID>" or "s:".
14269 * Only works if eval_fname_script() returned non-zero for "p"!
14270 */
14271 static int
14272eval_fname_sid(p)
14273 char_u *p;
14274{
14275 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
14276}
14277
14278/*
14279 * List the head of the function: "name(arg1, arg2)".
14280 */
14281 static void
14282list_func_head(fp, indent)
14283 ufunc_T *fp;
14284 int indent;
14285{
14286 int j;
14287
14288 msg_start();
14289 if (indent)
14290 MSG_PUTS(" ");
14291 MSG_PUTS("function ");
14292 if (fp->name[0] == K_SPECIAL)
14293 {
14294 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
14295 msg_puts(fp->name + 3);
14296 }
14297 else
14298 msg_puts(fp->name);
14299 msg_putchar('(');
14300 for (j = 0; j < fp->args.ga_len; ++j)
14301 {
14302 if (j)
14303 MSG_PUTS(", ");
14304 msg_puts(FUNCARG(fp, j));
14305 }
14306 if (fp->varargs)
14307 {
14308 if (j)
14309 MSG_PUTS(", ");
14310 MSG_PUTS("...");
14311 }
14312 msg_putchar(')');
14313}
14314
14315/*
14316 * Find a function by name, return pointer to it in ufuncs.
14317 * Return NULL for unknown function.
14318 */
14319 static ufunc_T *
14320find_func(name)
14321 char_u *name;
14322{
14323 ufunc_T *fp;
14324
14325 for (fp = firstfunc; fp != NULL; fp = fp->next)
14326 if (STRCMP(name, fp->name) == 0)
14327 break;
14328 return fp;
14329}
14330
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014331/*
14332 * Return TRUE if a function "name" exists.
14333 */
14334 static int
14335function_exists(name)
14336 char_u *name;
14337{
14338 char_u *p = name;
14339 int n = FALSE;
14340
14341 p = trans_function_name(&p, FALSE, TRUE);
14342 if (p != NULL)
14343 {
14344 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14345 n = (find_func(p) != NULL);
14346 else if (ASCII_ISLOWER(*p))
14347 n = (find_internal_func(p) >= 0);
14348 vim_free(p);
14349 }
14350 return n;
14351}
14352
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
14354
14355/*
14356 * Function given to ExpandGeneric() to obtain the list of user defined
14357 * function names.
14358 */
14359 char_u *
14360get_user_func_name(xp, idx)
14361 expand_T *xp;
14362 int idx;
14363{
14364 static ufunc_T *fp = NULL;
14365
14366 if (idx == 0)
14367 fp = firstfunc;
14368 if (fp != NULL)
14369 {
14370 if (STRLEN(fp->name) + 4 >= IOSIZE)
14371 return fp->name; /* prevents overflow */
14372
14373 cat_func_name(IObuff, fp);
14374 if (xp->xp_context != EXPAND_USER_FUNC)
14375 {
14376 STRCAT(IObuff, "(");
14377 if (!fp->varargs && fp->args.ga_len == 0)
14378 STRCAT(IObuff, ")");
14379 }
14380
14381 fp = fp->next;
14382 return IObuff;
14383 }
14384 return NULL;
14385}
14386
14387#endif /* FEAT_CMDL_COMPL */
14388
14389/*
14390 * Copy the function name of "fp" to buffer "buf".
14391 * "buf" must be able to hold the function name plus three bytes.
14392 * Takes care of script-local function names.
14393 */
14394 static void
14395cat_func_name(buf, fp)
14396 char_u *buf;
14397 ufunc_T *fp;
14398{
14399 if (fp->name[0] == K_SPECIAL)
14400 {
14401 STRCPY(buf, "<SNR>");
14402 STRCAT(buf, fp->name + 3);
14403 }
14404 else
14405 STRCPY(buf, fp->name);
14406}
14407
14408/*
14409 * ":delfunction {name}"
14410 */
14411 void
14412ex_delfunction(eap)
14413 exarg_T *eap;
14414{
14415 ufunc_T *fp = NULL, *pfp;
14416 char_u *p;
14417 char_u *name;
14418
14419 p = eap->arg;
14420 name = trans_function_name(&p, eap->skip, FALSE);
14421 if (name == NULL)
14422 return;
14423 if (!ends_excmd(*skipwhite(p)))
14424 {
14425 vim_free(name);
14426 EMSG(_(e_trailing));
14427 return;
14428 }
14429 eap->nextcmd = check_nextcmd(p);
14430 if (eap->nextcmd != NULL)
14431 *p = NUL;
14432
14433 if (!eap->skip)
14434 fp = find_func(name);
14435 vim_free(name);
14436
14437 if (!eap->skip)
14438 {
14439 if (fp == NULL)
14440 {
14441 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14442 return;
14443 }
14444 if (fp->calls)
14445 {
14446 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14447 return;
14448 }
14449
14450 /* clear this function */
14451 vim_free(fp->name);
14452 ga_clear_strings(&(fp->args));
14453 ga_clear_strings(&(fp->lines));
14454
14455 /* remove the function from the function list */
14456 if (firstfunc == fp)
14457 firstfunc = fp->next;
14458 else
14459 {
14460 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
14461 if (pfp->next == fp)
14462 {
14463 pfp->next = fp->next;
14464 break;
14465 }
14466 }
14467 vim_free(fp);
14468 }
14469}
14470
14471/*
14472 * Call a user function.
14473 */
14474 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000014475call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 ufunc_T *fp; /* pointer to function */
14477 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014478 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 linenr_T firstline; /* first line of range */
14481 linenr_T lastline; /* last line of range */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014482 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
14484 char_u *save_sourcing_name;
14485 linenr_T save_sourcing_lnum;
14486 scid_T save_current_SID;
14487 struct funccall fc;
14488 struct funccall *save_fcp = current_funccal;
14489 int save_did_emsg;
14490 static int depth = 0;
14491
14492 /* If depth of calling is getting too high, don't execute the function */
14493 if (depth >= p_mfd)
14494 {
14495 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->v_type = VAR_NUMBER;
14497 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 return;
14499 }
14500 ++depth;
14501
14502 line_breakcheck(); /* check for CTRL-C hit */
14503
14504 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014505 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 fc.func = fp;
14507 fc.argcount = argcount;
14508 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 fc.rettv = rettv;
14510 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 fc.linenr = 0;
14512 fc.returned = FALSE;
14513 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014514 fc.a0_var.tv.v_type = VAR_NUMBER;
14515 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
14516 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014518 fc.firstline.tv.v_type = VAR_NUMBER;
14519 fc.firstline.tv.vval.v_number = firstline;
14520 fc.firstline.v_name = NULL;
14521 fc.lastline.tv.v_type = VAR_NUMBER;
14522 fc.lastline.tv.vval.v_number = lastline;
14523 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 /* Check if this function has a breakpoint. */
14525 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
14526 fc.dbg_tick = debug_tick;
14527
Bram Moolenaare9a41262005-01-15 22:18:47 +000014528 if (selfdict != NULL && ga_grow(&fc.l_vars, 1) != FAIL)
14529 {
14530 VAR v = &VAR_GAP_ENTRY(0, &fc.l_vars);
14531
14532 /* Set the "self" local variable. */
14533 if ((v->v_name = vim_strsave((char_u *)"self")) != NULL)
14534 {
14535 ++fc.l_vars.ga_len;
14536 v->tv.v_type = VAR_DICT;
14537 v->tv.vval.v_dict = selfdict;
14538 ++selfdict->dv_refcount;
14539 }
14540 }
14541
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 /* Don't redraw while executing the function. */
14543 ++RedrawingDisabled;
14544 save_sourcing_name = sourcing_name;
14545 save_sourcing_lnum = sourcing_lnum;
14546 sourcing_lnum = 1;
14547 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
14548 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
14549 if (sourcing_name != NULL)
14550 {
14551 if (save_sourcing_name != NULL
14552 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
14553 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
14554 else
14555 STRCPY(sourcing_name, "function ");
14556 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
14557
14558 if (p_verbose >= 12)
14559 {
14560 ++no_wait_return;
14561 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14562 msg_str((char_u *)_("calling %s"), sourcing_name);
14563 if (p_verbose >= 14)
14564 {
14565 int i;
14566 char_u buf[MSG_BUF_LEN];
14567
14568 msg_puts((char_u *)"(");
14569 for (i = 0; i < argcount; ++i)
14570 {
14571 if (i > 0)
14572 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014573 if (argvars[i].v_type == VAR_NUMBER)
14574 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 else
14576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 buf, MSG_BUF_LEN);
14579 msg_puts((char_u *)"\"");
14580 msg_puts(buf);
14581 msg_puts((char_u *)"\"");
14582 }
14583 }
14584 msg_puts((char_u *)")");
14585 }
14586 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14587 cmdline_row = msg_row;
14588 --no_wait_return;
14589 }
14590 }
14591 save_current_SID = current_SID;
14592 current_SID = fp->script_ID;
14593 save_did_emsg = did_emsg;
14594 did_emsg = FALSE;
14595
14596 /* call do_cmdline() to execute the lines */
14597 do_cmdline(NULL, get_func_line, (void *)&fc,
14598 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
14599
14600 --RedrawingDisabled;
14601
14602 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014603 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 clear_tv(rettv);
14606 rettv->v_type = VAR_NUMBER;
14607 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 }
14609
14610 /* when being verbose, mention the return value */
14611 if (p_verbose >= 12)
14612 {
14613 char_u *sn, *val;
14614
14615 ++no_wait_return;
14616 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14617
14618 /* Make sure the output fits in IObuff. */
14619 sn = sourcing_name;
14620 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
14621 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
14622
14623 if (aborting())
14624 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014625 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 (long)fc.rettv->vval.v_number);
14628 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 if (STRLEN(val) > IOSIZE / 2 - 50)
14632 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
14633 smsg((char_u *)_("%s returning \"%s\""), sn, val);
14634 }
14635 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14636 cmdline_row = msg_row;
14637 --no_wait_return;
14638 }
14639
14640 vim_free(sourcing_name);
14641 sourcing_name = save_sourcing_name;
14642 sourcing_lnum = save_sourcing_lnum;
14643 current_SID = save_current_SID;
14644
14645 if (p_verbose >= 12 && sourcing_name != NULL)
14646 {
14647 ++no_wait_return;
14648 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14649 msg_str((char_u *)_("continuing in %s"), sourcing_name);
14650 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14651 cmdline_row = msg_row;
14652 --no_wait_return;
14653 }
14654
14655 did_emsg |= save_did_emsg;
14656 current_funccal = save_fcp;
14657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014658 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 --depth;
14660}
14661
14662/*
14663 * ":return [expr]"
14664 */
14665 void
14666ex_return(eap)
14667 exarg_T *eap;
14668{
14669 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 int returning = FALSE;
14672
14673 if (current_funccal == NULL)
14674 {
14675 EMSG(_("E133: :return not inside a function"));
14676 return;
14677 }
14678
14679 if (eap->skip)
14680 ++emsg_skip;
14681
14682 eap->nextcmd = NULL;
14683 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685 {
14686 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 }
14691 /* It's safer to return also on error. */
14692 else if (!eap->skip)
14693 {
14694 /*
14695 * Return unless the expression evaluation has been cancelled due to an
14696 * aborting error, an interrupt, or an exception.
14697 */
14698 if (!aborting())
14699 returning = do_return(eap, FALSE, TRUE, NULL);
14700 }
14701
14702 /* When skipping or the return gets pending, advance to the next command
14703 * in this line (!returning). Otherwise, ignore the rest of the line.
14704 * Following lines will be ignored by get_func_line(). */
14705 if (returning)
14706 eap->nextcmd = NULL;
14707 else if (eap->nextcmd == NULL) /* no argument */
14708 eap->nextcmd = check_nextcmd(arg);
14709
14710 if (eap->skip)
14711 --emsg_skip;
14712}
14713
14714/*
14715 * Return from a function. Possibly makes the return pending. Also called
14716 * for a pending return at the ":endtry" or after returning from an extra
14717 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 * when called due to a ":return" command. "rettv" may point to a typeval
14719 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 * FALSE when the return gets pending.
14721 */
14722 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014723do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 exarg_T *eap;
14725 int reanimate;
14726 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728{
14729 int idx;
14730 struct condstack *cstack = eap->cstack;
14731
14732 if (reanimate)
14733 /* Undo the return. */
14734 current_funccal->returned = FALSE;
14735
14736 /*
14737 * Cleanup (and inactivate) conditionals, but stop when a try conditional
14738 * not in its finally clause (which then is to be executed next) is found.
14739 * In this case, make the ":return" pending for execution at the ":endtry".
14740 * Otherwise, return normally.
14741 */
14742 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
14743 if (idx >= 0)
14744 {
14745 cstack->cs_pending[idx] = CSTP_RETURN;
14746
14747 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 /* A pending return again gets pending. "rettv" points to an
14749 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 else
14753 {
14754 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014759 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 {
14761 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
14763 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 else
14765 EMSG(_(e_outofmem));
14766 }
14767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769
14770 if (reanimate)
14771 {
14772 /* The pending return value could be overwritten by a ":return"
14773 * without argument in a finally clause; reset the default
14774 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 current_funccal->rettv->v_type = VAR_NUMBER;
14776 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 }
14778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 }
14781 else
14782 {
14783 current_funccal->returned = TRUE;
14784
14785 /* If the return is carried out now, store the return value. For
14786 * a return immediately after reanimation, the value is already
14787 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014790 clear_tv(current_funccal->rettv);
14791 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 }
14795 }
14796
14797 return idx < 0;
14798}
14799
14800/*
14801 * Free the variable with a pending return value.
14802 */
14803 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804discard_pending_return(rettv)
14805 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808}
14809
14810/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014811 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812 * is an allocated string. Used by report_pending() for verbose messages.
14813 */
14814 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014815get_return_cmd(rettv)
14816 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014818 char_u *s;
14819 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014820 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 if (rettv == NULL)
14823 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014825 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826
14827 STRCPY(IObuff, ":return ");
14828 STRNCPY(IObuff + 8, s, IOSIZE - 8);
14829 if (STRLEN(s) + 8 >= IOSIZE)
14830 STRCPY(IObuff + IOSIZE - 4, "...");
14831 vim_free(tofree);
14832 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833}
14834
14835/*
14836 * Get next function line.
14837 * Called by do_cmdline() to get the next line.
14838 * Returns allocated string, or NULL for end of function.
14839 */
14840/* ARGSUSED */
14841 char_u *
14842get_func_line(c, cookie, indent)
14843 int c; /* not used */
14844 void *cookie;
14845 int indent; /* not used */
14846{
14847 struct funccall *fcp = (struct funccall *)cookie;
14848 char_u *retval;
14849 garray_T *gap; /* growarray with function lines */
14850
14851 /* If breakpoints have been added/deleted need to check for it. */
14852 if (fcp->dbg_tick != debug_tick)
14853 {
14854 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
14855 sourcing_lnum);
14856 fcp->dbg_tick = debug_tick;
14857 }
14858
14859 gap = &fcp->func->lines;
14860 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
14861 retval = NULL;
14862 else if (fcp->returned || fcp->linenr >= gap->ga_len)
14863 retval = NULL;
14864 else
14865 {
14866 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
14867 sourcing_lnum = fcp->linenr;
14868 }
14869
14870 /* Did we encounter a breakpoint? */
14871 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
14872 {
14873 dbg_breakpoint(fcp->func->name, sourcing_lnum);
14874 /* Find next breakpoint. */
14875 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
14876 sourcing_lnum);
14877 fcp->dbg_tick = debug_tick;
14878 }
14879
14880 return retval;
14881}
14882
14883/*
14884 * Return TRUE if the currently active function should be ended, because a
14885 * return was encountered or an error occured. Used inside a ":while".
14886 */
14887 int
14888func_has_ended(cookie)
14889 void *cookie;
14890{
14891 struct funccall *fcp = (struct funccall *)cookie;
14892
14893 /* Ignore the "abort" flag if the abortion behavior has been changed due to
14894 * an error inside a try conditional. */
14895 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
14896 || fcp->returned);
14897}
14898
14899/*
14900 * return TRUE if cookie indicates a function which "abort"s on errors.
14901 */
14902 int
14903func_has_abort(cookie)
14904 void *cookie;
14905{
14906 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
14907}
14908
14909#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
14910typedef enum
14911{
14912 VAR_FLAVOUR_DEFAULT,
14913 VAR_FLAVOUR_SESSION,
14914 VAR_FLAVOUR_VIMINFO
14915} var_flavour_T;
14916
14917static var_flavour_T var_flavour __ARGS((char_u *varname));
14918
14919 static var_flavour_T
14920var_flavour(varname)
14921 char_u *varname;
14922{
14923 char_u *p = varname;
14924
14925 if (ASCII_ISUPPER(*p))
14926 {
14927 while (*(++p))
14928 if (ASCII_ISLOWER(*p))
14929 return VAR_FLAVOUR_SESSION;
14930 return VAR_FLAVOUR_VIMINFO;
14931 }
14932 else
14933 return VAR_FLAVOUR_DEFAULT;
14934}
14935#endif
14936
14937#if defined(FEAT_VIMINFO) || defined(PROTO)
14938/*
14939 * Restore global vars that start with a capital from the viminfo file
14940 */
14941 int
14942read_viminfo_varlist(virp, writing)
14943 vir_T *virp;
14944 int writing;
14945{
14946 char_u *tab;
14947 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014948 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949
14950 if (!writing && (find_viminfo_parameter('!') != NULL))
14951 {
14952 tab = vim_strchr(virp->vir_line + 1, '\t');
14953 if (tab != NULL)
14954 {
14955 *tab++ = '\0'; /* isolate the variable name */
14956 if (*tab == 'S') /* string var */
14957 is_string = TRUE;
14958
14959 tab = vim_strchr(tab, '\t');
14960 if (tab != NULL)
14961 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962 if (is_string)
14963 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014964 tv.v_type = VAR_STRING;
14965 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 }
14968 else
14969 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014970 tv.v_type = VAR_NUMBER;
14971 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014973 set_var(virp->vir_line + 1, &tv, FALSE);
14974 if (is_string)
14975 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 }
14977 }
14978 }
14979
14980 return viminfo_readline(virp);
14981}
14982
14983/*
14984 * Write global vars that start with a capital to the viminfo file
14985 */
14986 void
14987write_viminfo_varlist(fp)
14988 FILE *fp;
14989{
14990 garray_T *gap = &variables; /* global variable */
14991 VAR this_var;
14992 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 char *s;
14994 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014995 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
14997 if (find_viminfo_parameter('!') == NULL)
14998 return;
14999
15000 fprintf(fp, _("\n# global variables:\n"));
15001 for (i = gap->ga_len; --i >= 0; )
15002 {
15003 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015004 if (this_var->v_name != NULL
15005 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015007 switch (this_var->tv.v_type)
15008 {
15009 case VAR_STRING: s = "STR"; break;
15010 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015011 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 }
15013 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015014 viminfo_writestring(fp, echo_string(&this_var->tv,
15015 &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015016 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 }
15018 }
15019}
15020#endif
15021
15022#if defined(FEAT_SESSION) || defined(PROTO)
15023 int
15024store_session_globals(fd)
15025 FILE *fd;
15026{
15027 garray_T *gap = &variables; /* global variable */
15028 VAR this_var;
15029 int i;
15030 char_u *p, *t;
15031
15032 for (i = gap->ga_len; --i >= 0; )
15033 {
15034 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015035 if (this_var->v_name != NULL
15036 && (this_var->tv.v_type == VAR_NUMBER
15037 || this_var->tv.v_type == VAR_STRING)
15038 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015040 /* Escape special characters with a backslash. Turn a LF and
15041 * CR into \n and \r. */
15042 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015044 if (p == NULL) /* out of memory */
15045 continue;
15046 for (t = p; *t != NUL; ++t)
15047 if (*t == '\n')
15048 *t = 'n';
15049 else if (*t == '\r')
15050 *t = 'r';
15051 if ((fprintf(fd, "let %s = %c%s%c",
15052 this_var->v_name,
15053 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
15054 p,
15055 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
15056 || put_eol(fd) == FAIL)
15057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015059 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015061 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 }
15063 }
15064 return OK;
15065}
15066#endif
15067
15068#endif /* FEAT_EVAL */
15069
15070#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
15071
15072
15073#ifdef WIN3264
15074/*
15075 * Functions for ":8" filename modifier: get 8.3 version of a filename.
15076 */
15077static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15078static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
15079static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15080
15081/*
15082 * Get the short pathname of a file.
15083 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
15084 */
15085 static int
15086get_short_pathname(fnamep, bufp, fnamelen)
15087 char_u **fnamep;
15088 char_u **bufp;
15089 int *fnamelen;
15090{
15091 int l,len;
15092 char_u *newbuf;
15093
15094 len = *fnamelen;
15095
15096 l = GetShortPathName(*fnamep, *fnamep, len);
15097 if (l > len - 1)
15098 {
15099 /* If that doesn't work (not enough space), then save the string
15100 * and try again with a new buffer big enough
15101 */
15102 newbuf = vim_strnsave(*fnamep, l);
15103 if (newbuf == NULL)
15104 return 0;
15105
15106 vim_free(*bufp);
15107 *fnamep = *bufp = newbuf;
15108
15109 l = GetShortPathName(*fnamep,*fnamep,l+1);
15110
15111 /* Really should always succeed, as the buffer is big enough */
15112 }
15113
15114 *fnamelen = l;
15115 return 1;
15116}
15117
15118/*
15119 * Create a short path name. Returns the length of the buffer it needs.
15120 * Doesn't copy over the end of the buffer passed in.
15121 */
15122 static int
15123shortpath_for_invalid_fname(fname, bufp, fnamelen)
15124 char_u **fname;
15125 char_u **bufp;
15126 int *fnamelen;
15127{
15128 char_u *s, *p, *pbuf2, *pbuf3;
15129 char_u ch;
15130 int l,len,len2,plen,slen;
15131
15132 /* Make a copy */
15133 len2 = *fnamelen;
15134 pbuf2 = vim_strnsave(*fname, len2);
15135 pbuf3 = NULL;
15136
15137 s = pbuf2 + len2 - 1; /* Find the end */
15138 slen = 1;
15139 plen = len2;
15140
15141 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015142 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 {
15144 --s;
15145 ++slen;
15146 --plen;
15147 }
15148
15149 do
15150 {
15151 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015152 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 {
15154 --s;
15155 ++slen;
15156 --plen;
15157 }
15158 if (s <= pbuf2)
15159 break;
15160
15161 /* Remeber the character that is about to be blatted */
15162 ch = *s;
15163 *s = 0; /* get_short_pathname requires a null-terminated string */
15164
15165 /* Try it in situ */
15166 p = pbuf2;
15167 if (!get_short_pathname(&p, &pbuf3, &plen))
15168 {
15169 vim_free(pbuf2);
15170 return -1;
15171 }
15172 *s = ch; /* Preserve the string */
15173 } while (plen == 0);
15174
15175 if (plen > 0)
15176 {
15177 /* Remeber the length of the new string. */
15178 *fnamelen = len = plen + slen;
15179 vim_free(*bufp);
15180 if (len > len2)
15181 {
15182 /* If there's not enough space in the currently allocated string,
15183 * then copy it to a buffer big enough.
15184 */
15185 *fname= *bufp = vim_strnsave(p, len);
15186 if (*fname == NULL)
15187 return -1;
15188 }
15189 else
15190 {
15191 /* Transfer pbuf2 to being the main buffer (it's big enough) */
15192 *fname = *bufp = pbuf2;
15193 if (p != pbuf2)
15194 strncpy(*fname, p, plen);
15195 pbuf2 = NULL;
15196 }
15197 /* Concat the next bit */
15198 strncpy(*fname + plen, s, slen);
15199 (*fname)[len] = '\0';
15200 }
15201 vim_free(pbuf3);
15202 vim_free(pbuf2);
15203 return 0;
15204}
15205
15206/*
15207 * Get a pathname for a partial path.
15208 */
15209 static int
15210shortpath_for_partial(fnamep, bufp, fnamelen)
15211 char_u **fnamep;
15212 char_u **bufp;
15213 int *fnamelen;
15214{
15215 int sepcount, len, tflen;
15216 char_u *p;
15217 char_u *pbuf, *tfname;
15218 int hasTilde;
15219
15220 /* Count up the path seperators from the RHS.. so we know which part
15221 * of the path to return.
15222 */
15223 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015224 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 if (vim_ispathsep(*p))
15226 ++sepcount;
15227
15228 /* Need full path first (use expand_env() to remove a "~/") */
15229 hasTilde = (**fnamep == '~');
15230 if (hasTilde)
15231 pbuf = tfname = expand_env_save(*fnamep);
15232 else
15233 pbuf = tfname = FullName_save(*fnamep, FALSE);
15234
15235 len = tflen = STRLEN(tfname);
15236
15237 if (!get_short_pathname(&tfname, &pbuf, &len))
15238 return -1;
15239
15240 if (len == 0)
15241 {
15242 /* Don't have a valid filename, so shorten the rest of the
15243 * path if we can. This CAN give us invalid 8.3 filenames, but
15244 * there's not a lot of point in guessing what it might be.
15245 */
15246 len = tflen;
15247 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
15248 return -1;
15249 }
15250
15251 /* Count the paths backward to find the beginning of the desired string. */
15252 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015253 {
15254#ifdef FEAT_MBYTE
15255 if (has_mbyte)
15256 p -= mb_head_off(tfname, p);
15257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 if (vim_ispathsep(*p))
15259 {
15260 if (sepcount == 0 || (hasTilde && sepcount == 1))
15261 break;
15262 else
15263 sepcount --;
15264 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 if (hasTilde)
15267 {
15268 --p;
15269 if (p >= tfname)
15270 *p = '~';
15271 else
15272 return -1;
15273 }
15274 else
15275 ++p;
15276
15277 /* Copy in the string - p indexes into tfname - allocated at pbuf */
15278 vim_free(*bufp);
15279 *fnamelen = (int)STRLEN(p);
15280 *bufp = pbuf;
15281 *fnamep = p;
15282
15283 return 0;
15284}
15285#endif /* WIN3264 */
15286
15287/*
15288 * Adjust a filename, according to a string of modifiers.
15289 * *fnamep must be NUL terminated when called. When returning, the length is
15290 * determined by *fnamelen.
15291 * Returns valid flags.
15292 * When there is an error, *fnamep is set to NULL.
15293 */
15294 int
15295modify_fname(src, usedlen, fnamep, bufp, fnamelen)
15296 char_u *src; /* string with modifiers */
15297 int *usedlen; /* characters after src that are used */
15298 char_u **fnamep; /* file name so far */
15299 char_u **bufp; /* buffer for allocated file name or NULL */
15300 int *fnamelen; /* length of fnamep */
15301{
15302 int valid = 0;
15303 char_u *tail;
15304 char_u *s, *p, *pbuf;
15305 char_u dirname[MAXPATHL];
15306 int c;
15307 int has_fullname = 0;
15308#ifdef WIN3264
15309 int has_shortname = 0;
15310#endif
15311
15312repeat:
15313 /* ":p" - full path/file_name */
15314 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
15315 {
15316 has_fullname = 1;
15317
15318 valid |= VALID_PATH;
15319 *usedlen += 2;
15320
15321 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
15322 if ((*fnamep)[0] == '~'
15323#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
15324 && ((*fnamep)[1] == '/'
15325# ifdef BACKSLASH_IN_FILENAME
15326 || (*fnamep)[1] == '\\'
15327# endif
15328 || (*fnamep)[1] == NUL)
15329
15330#endif
15331 )
15332 {
15333 *fnamep = expand_env_save(*fnamep);
15334 vim_free(*bufp); /* free any allocated file name */
15335 *bufp = *fnamep;
15336 if (*fnamep == NULL)
15337 return -1;
15338 }
15339
15340 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015341 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 {
15343 if (vim_ispathsep(*p)
15344 && p[1] == '.'
15345 && (p[2] == NUL
15346 || vim_ispathsep(p[2])
15347 || (p[2] == '.'
15348 && (p[3] == NUL || vim_ispathsep(p[3])))))
15349 break;
15350 }
15351
15352 /* FullName_save() is slow, don't use it when not needed. */
15353 if (*p != NUL || !vim_isAbsName(*fnamep))
15354 {
15355 *fnamep = FullName_save(*fnamep, *p != NUL);
15356 vim_free(*bufp); /* free any allocated file name */
15357 *bufp = *fnamep;
15358 if (*fnamep == NULL)
15359 return -1;
15360 }
15361
15362 /* Append a path separator to a directory. */
15363 if (mch_isdir(*fnamep))
15364 {
15365 /* Make room for one or two extra characters. */
15366 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
15367 vim_free(*bufp); /* free any allocated file name */
15368 *bufp = *fnamep;
15369 if (*fnamep == NULL)
15370 return -1;
15371 add_pathsep(*fnamep);
15372 }
15373 }
15374
15375 /* ":." - path relative to the current directory */
15376 /* ":~" - path relative to the home directory */
15377 /* ":8" - shortname path - postponed till after */
15378 while (src[*usedlen] == ':'
15379 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
15380 {
15381 *usedlen += 2;
15382 if (c == '8')
15383 {
15384#ifdef WIN3264
15385 has_shortname = 1; /* Postpone this. */
15386#endif
15387 continue;
15388 }
15389 pbuf = NULL;
15390 /* Need full path first (use expand_env() to remove a "~/") */
15391 if (!has_fullname)
15392 {
15393 if (c == '.' && **fnamep == '~')
15394 p = pbuf = expand_env_save(*fnamep);
15395 else
15396 p = pbuf = FullName_save(*fnamep, FALSE);
15397 }
15398 else
15399 p = *fnamep;
15400
15401 has_fullname = 0;
15402
15403 if (p != NULL)
15404 {
15405 if (c == '.')
15406 {
15407 mch_dirname(dirname, MAXPATHL);
15408 s = shorten_fname(p, dirname);
15409 if (s != NULL)
15410 {
15411 *fnamep = s;
15412 if (pbuf != NULL)
15413 {
15414 vim_free(*bufp); /* free any allocated file name */
15415 *bufp = pbuf;
15416 pbuf = NULL;
15417 }
15418 }
15419 }
15420 else
15421 {
15422 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
15423 /* Only replace it when it starts with '~' */
15424 if (*dirname == '~')
15425 {
15426 s = vim_strsave(dirname);
15427 if (s != NULL)
15428 {
15429 *fnamep = s;
15430 vim_free(*bufp);
15431 *bufp = s;
15432 }
15433 }
15434 }
15435 vim_free(pbuf);
15436 }
15437 }
15438
15439 tail = gettail(*fnamep);
15440 *fnamelen = (int)STRLEN(*fnamep);
15441
15442 /* ":h" - head, remove "/file_name", can be repeated */
15443 /* Don't remove the first "/" or "c:\" */
15444 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
15445 {
15446 valid |= VALID_HEAD;
15447 *usedlen += 2;
15448 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015449 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 --tail;
15451 *fnamelen = (int)(tail - *fnamep);
15452#ifdef VMS
15453 if (*fnamelen > 0)
15454 *fnamelen += 1; /* the path separator is part of the path */
15455#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015456 while (tail > s && !after_pathsep(s, tail))
15457 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 }
15459
15460 /* ":8" - shortname */
15461 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
15462 {
15463 *usedlen += 2;
15464#ifdef WIN3264
15465 has_shortname = 1;
15466#endif
15467 }
15468
15469#ifdef WIN3264
15470 /* Check shortname after we have done 'heads' and before we do 'tails'
15471 */
15472 if (has_shortname)
15473 {
15474 pbuf = NULL;
15475 /* Copy the string if it is shortened by :h */
15476 if (*fnamelen < (int)STRLEN(*fnamep))
15477 {
15478 p = vim_strnsave(*fnamep, *fnamelen);
15479 if (p == 0)
15480 return -1;
15481 vim_free(*bufp);
15482 *bufp = *fnamep = p;
15483 }
15484
15485 /* Split into two implementations - makes it easier. First is where
15486 * there isn't a full name already, second is where there is.
15487 */
15488 if (!has_fullname && !vim_isAbsName(*fnamep))
15489 {
15490 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
15491 return -1;
15492 }
15493 else
15494 {
15495 int l;
15496
15497 /* Simple case, already have the full-name
15498 * Nearly always shorter, so try first time. */
15499 l = *fnamelen;
15500 if (!get_short_pathname(fnamep, bufp, &l))
15501 return -1;
15502
15503 if (l == 0)
15504 {
15505 /* Couldn't find the filename.. search the paths.
15506 */
15507 l = *fnamelen;
15508 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
15509 return -1;
15510 }
15511 *fnamelen = l;
15512 }
15513 }
15514#endif /* WIN3264 */
15515
15516 /* ":t" - tail, just the basename */
15517 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
15518 {
15519 *usedlen += 2;
15520 *fnamelen -= (int)(tail - *fnamep);
15521 *fnamep = tail;
15522 }
15523
15524 /* ":e" - extension, can be repeated */
15525 /* ":r" - root, without extension, can be repeated */
15526 while (src[*usedlen] == ':'
15527 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
15528 {
15529 /* find a '.' in the tail:
15530 * - for second :e: before the current fname
15531 * - otherwise: The last '.'
15532 */
15533 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
15534 s = *fnamep - 2;
15535 else
15536 s = *fnamep + *fnamelen - 1;
15537 for ( ; s > tail; --s)
15538 if (s[0] == '.')
15539 break;
15540 if (src[*usedlen + 1] == 'e') /* :e */
15541 {
15542 if (s > tail)
15543 {
15544 *fnamelen += (int)(*fnamep - (s + 1));
15545 *fnamep = s + 1;
15546#ifdef VMS
15547 /* cut version from the extension */
15548 s = *fnamep + *fnamelen - 1;
15549 for ( ; s > *fnamep; --s)
15550 if (s[0] == ';')
15551 break;
15552 if (s > *fnamep)
15553 *fnamelen = s - *fnamep;
15554#endif
15555 }
15556 else if (*fnamep <= tail)
15557 *fnamelen = 0;
15558 }
15559 else /* :r */
15560 {
15561 if (s > tail) /* remove one extension */
15562 *fnamelen = (int)(s - *fnamep);
15563 }
15564 *usedlen += 2;
15565 }
15566
15567 /* ":s?pat?foo?" - substitute */
15568 /* ":gs?pat?foo?" - global substitute */
15569 if (src[*usedlen] == ':'
15570 && (src[*usedlen + 1] == 's'
15571 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
15572 {
15573 char_u *str;
15574 char_u *pat;
15575 char_u *sub;
15576 int sep;
15577 char_u *flags;
15578 int didit = FALSE;
15579
15580 flags = (char_u *)"";
15581 s = src + *usedlen + 2;
15582 if (src[*usedlen + 1] == 'g')
15583 {
15584 flags = (char_u *)"g";
15585 ++s;
15586 }
15587
15588 sep = *s++;
15589 if (sep)
15590 {
15591 /* find end of pattern */
15592 p = vim_strchr(s, sep);
15593 if (p != NULL)
15594 {
15595 pat = vim_strnsave(s, (int)(p - s));
15596 if (pat != NULL)
15597 {
15598 s = p + 1;
15599 /* find end of substitution */
15600 p = vim_strchr(s, sep);
15601 if (p != NULL)
15602 {
15603 sub = vim_strnsave(s, (int)(p - s));
15604 str = vim_strnsave(*fnamep, *fnamelen);
15605 if (sub != NULL && str != NULL)
15606 {
15607 *usedlen = (int)(p + 1 - src);
15608 s = do_string_sub(str, pat, sub, flags);
15609 if (s != NULL)
15610 {
15611 *fnamep = s;
15612 *fnamelen = (int)STRLEN(s);
15613 vim_free(*bufp);
15614 *bufp = s;
15615 didit = TRUE;
15616 }
15617 }
15618 vim_free(sub);
15619 vim_free(str);
15620 }
15621 vim_free(pat);
15622 }
15623 }
15624 /* after using ":s", repeat all the modifiers */
15625 if (didit)
15626 goto repeat;
15627 }
15628 }
15629
15630 return valid;
15631}
15632
15633/*
15634 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
15635 * "flags" can be "g" to do a global substitute.
15636 * Returns an allocated string, NULL for error.
15637 */
15638 char_u *
15639do_string_sub(str, pat, sub, flags)
15640 char_u *str;
15641 char_u *pat;
15642 char_u *sub;
15643 char_u *flags;
15644{
15645 int sublen;
15646 regmatch_T regmatch;
15647 int i;
15648 int do_all;
15649 char_u *tail;
15650 garray_T ga;
15651 char_u *ret;
15652 char_u *save_cpo;
15653
15654 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
15655 save_cpo = p_cpo;
15656 p_cpo = (char_u *)"";
15657
15658 ga_init2(&ga, 1, 200);
15659
15660 do_all = (flags[0] == 'g');
15661
15662 regmatch.rm_ic = p_ic;
15663 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15664 if (regmatch.regprog != NULL)
15665 {
15666 tail = str;
15667 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
15668 {
15669 /*
15670 * Get some space for a temporary buffer to do the substitution
15671 * into. It will contain:
15672 * - The text up to where the match is.
15673 * - The substituted text.
15674 * - The text after the match.
15675 */
15676 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
15677 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
15678 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
15679 {
15680 ga_clear(&ga);
15681 break;
15682 }
15683
15684 /* copy the text up to where the match is */
15685 i = (int)(regmatch.startp[0] - tail);
15686 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
15687 /* add the substituted text */
15688 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
15689 + ga.ga_len + i, TRUE, TRUE, FALSE);
15690 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 /* avoid getting stuck on a match with an empty string */
15692 if (tail == regmatch.endp[0])
15693 {
15694 if (*tail == NUL)
15695 break;
15696 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
15697 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 }
15699 else
15700 {
15701 tail = regmatch.endp[0];
15702 if (*tail == NUL)
15703 break;
15704 }
15705 if (!do_all)
15706 break;
15707 }
15708
15709 if (ga.ga_data != NULL)
15710 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
15711
15712 vim_free(regmatch.regprog);
15713 }
15714
15715 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
15716 ga_clear(&ga);
15717 p_cpo = save_cpo;
15718
15719 return ret;
15720}
15721
15722#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */