blob: ed1551e1dd731fe88dd2f9dea0eb056d903d18c9 [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.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000112 * The key is copied into "di_key" to avoid an extra alloc/free for it.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000113 */
114struct dictitem_S
115{
Bram Moolenaar8c711452005-01-14 21:53:12 +0000116 typeval di_tv; /* type and value of the variable */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000117 char_u di_key[1]; /* key (actually longer!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000118};
119
120typedef struct dictitem_S dictitem;
121
122/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000123 * In a hashtable item "hi_key" points to "di_key" in a dictitem.
124 * This avoids adding a pointer to the hashtable item.
125 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
126 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
127 * HI2DI() converts a hashitem pointer to a dictitem pointer.
128 */
129static dictitem dumdi;
130#define DI2HIKEY(p) ((p)->di_key)
131#define HIKEY2DI(p) ((dictitem *)(p - (dumdi.di_key - (char_u *)&dumdi.di_tv)))
132#define HI2DI(p) HIKEY2DI((p)->hi_key)
133
134/*
Bram Moolenaar8c711452005-01-14 21:53:12 +0000135 * Structure to hold info about a Dictionary.
136 */
137struct dictvar_S
138{
139 int dv_refcount; /* reference count */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000140 hashtable dv_hashtable; /* hashtable that refers to the items */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000141};
142
143typedef struct dictvar_S dictvar;
144
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000145/*
146 * Structure returned by get_lval() and used by set_var_lval().
147 * For a plain name:
148 * "name" points to the variable name.
149 * "exp_name" is NULL.
150 * "tv" is NULL
151 * For a magic braces name:
152 * "name" points to the expanded variable name.
153 * "exp_name" is non-NULL, to be freed later.
154 * "tv" is NULL
155 * For an index in a list:
156 * "name" points to the (expanded) variable name.
157 * "exp_name" NULL or non-NULL, to be freed later.
158 * "tv" points to the (first) list item value
159 * "li" points to the (first) list item
160 * "range", "n1", "n2" and "empty2" indicate what items are used.
161 * For an existing Dict item:
162 * "name" points to the (expanded) variable name.
163 * "exp_name" NULL or non-NULL, to be freed later.
164 * "tv" points to the dict item value
165 * "newkey" is NULL
166 * For a non-existing Dict item:
167 * "name" points to the (expanded) variable name.
168 * "exp_name" NULL or non-NULL, to be freed later.
169 * "tv" points to the Dictionary typeval
170 * "newkey" is the key for the new item.
171 */
172typedef struct lval_S
173{
174 char_u *ll_name; /* start of variable name (can be NULL) */
175 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
176 typeval *ll_tv; /* Typeval of item being used. If "newkey"
177 isn't NULL it's the Dict to which to add
178 the item. */
179 listitem *ll_li; /* The list item or NULL. */
180 listvar *ll_list; /* The list or NULL. */
181 int ll_range; /* TRUE when a [i:j] range was used */
182 long ll_n1; /* First index for list */
183 long ll_n2; /* Second index for list range */
184 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000185 dictvar *ll_dict; /* The Dictionary or NULL */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000186 dictitem *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000187 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000188} lval;
189
Bram Moolenaar8c711452005-01-14 21:53:12 +0000190
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000191static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000192static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000193static char *e_undefvar = N_("E121: Undefined variable: %s");
194static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000195static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +0000196static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000197static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000198static char *e_listreq = N_("E714: List required");
199static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000200static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000201static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
202static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
203static char *e_funcdict = N_("E717: Dictionary entry already exists");
204static char *e_funcref = N_("E718: Funcref required");
205static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
206static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000207
208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 * All user-defined global variables are stored in "variables".
210 */
211garray_T variables = {0, 0, sizeof(var), 4, NULL};
212
213/*
214 * Array to hold an array with variables local to each sourced script.
215 */
216static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
217#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
218
219
220#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
Bram Moolenaare9a41262005-01-15 22:18:47 +0000221#define VAR_GAP_ENTRY(idx, gap) (((VAR)((gap)->ga_data))[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
223#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
224
225static int echo_attr = 0; /* attributes used for ":echo" */
226
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000227/* Values for trans_function_name() argument: */
228#define TFN_INT 1 /* internal function name OK */
229#define TFN_QUIET 2 /* no error messages */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/*
232 * Structure to hold info for a user function.
233 */
234typedef struct ufunc ufunc_T;
235
236struct ufunc
237{
238 ufunc_T *next; /* next function in list */
239 char_u *name; /* name of function; can start with <SNR>123_
240 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
241 int varargs; /* variable nr of arguments */
242 int flags;
243 int calls; /* nr of active calls */
244 garray_T args; /* arguments */
245 garray_T lines; /* function lines */
246 scid_T script_ID; /* ID of script where function was defined,
247 used for s: variables */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000248 int refcount; /* for numbered function: reference count */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249};
250
251/* function flags */
252#define FC_ABORT 1 /* abort function on error */
253#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000254#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256/*
257 * All user-defined functions are found in the forward-linked function list.
258 * The first function is pointed at by firstfunc.
259 */
260ufunc_T *firstfunc = NULL;
261
262#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
263#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
264
265/* structure to hold info for a function that is currently being executed. */
266struct funccall
267{
268 ufunc_T *func; /* function being called */
269 int linenr; /* next line to be executed */
270 int returned; /* ":return" used */
271 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000272 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 var a0_var; /* "a:0" variable */
274 var firstline; /* "a:firstline" variable */
275 var lastline; /* "a:lastline" variable */
276 garray_T l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000277 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 linenr_T breakpoint; /* next line with breakpoint or zero */
279 int dbg_tick; /* debug_tick when breakpoint was set */
280 int level; /* top nesting level of executed function */
281};
282
283/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000284 * Info used by a ":for" loop.
285 */
286typedef struct forinfo_S
287{
288 int fi_semicolon; /* TRUE if ending in '; var]' */
289 int fi_varcount; /* nr of variables in the list */
290 listwatch fi_lw; /* keep an eye on the item used. */
291 listvar *fi_list; /* list being used */
292} forinfo;
293
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000294/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000295 * Struct used by trans_function_name()
296 */
297typedef struct
298{
299 dictvar *fd_dict; /* Dictionary used */
300 char_u *fd_newkey; /* new key in "dict" */
301 dictitem *fd_di; /* Dictionary item used */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000302} funcdict;
303
304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 * Return the name of the executed function.
306 */
307 char_u *
308func_name(cookie)
309 void *cookie;
310{
311 return ((struct funccall *)cookie)->func->name;
312}
313
314/*
315 * Return the address holding the next breakpoint line for a funccall cookie.
316 */
317 linenr_T *
318func_breakpoint(cookie)
319 void *cookie;
320{
321 return &((struct funccall *)cookie)->breakpoint;
322}
323
324/*
325 * Return the address holding the debug tick for a funccall cookie.
326 */
327 int *
328func_dbg_tick(cookie)
329 void *cookie;
330{
331 return &((struct funccall *)cookie)->dbg_tick;
332}
333
334/*
335 * Return the nesting level for a funccall cookie.
336 */
337 int
338func_level(cookie)
339 void *cookie;
340{
341 return ((struct funccall *)cookie)->level;
342}
343
344/* pointer to funccal for currently active function */
345struct funccall *current_funccal = NULL;
346
347/*
348 * Return TRUE when a function was ended by a ":return" command.
349 */
350 int
351current_func_returned()
352{
353 return current_funccal->returned;
354}
355
356
357/*
358 * Array to hold the value of v: variables.
359 */
360#include "version.h"
361
362/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000363#define VV_COMPAT 1 /* compatible, also used without "v:" */
364#define VV_RO 2 /* read-only */
365#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
Bram Moolenaare9a41262005-01-15 22:18:47 +0000367#define VV_NAME(s) s, sizeof(s) - 1
368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369struct vimvar
370{
371 char *name; /* name of variable, without v: */
372 int len; /* length of name */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000373 typeval tv; /* type and value */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000374 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375} vimvars[VV_LEN] =
Bram Moolenaare9a41262005-01-15 22:18:47 +0000376{
377 /*
378 * The order here must match the VV_ defines in vim.h!
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000379 * Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare9a41262005-01-15 22:18:47 +0000380 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000381 {VV_NAME("count"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
382 {VV_NAME("count1"), {VAR_NUMBER}, VV_RO},
383 {VV_NAME("prevcount"), {VAR_NUMBER}, VV_RO},
384 {VV_NAME("errmsg"), {VAR_STRING}, VV_COMPAT},
385 {VV_NAME("warningmsg"), {VAR_STRING}, 0},
386 {VV_NAME("statusmsg"), {VAR_STRING}, 0},
387 {VV_NAME("shell_error"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
388 {VV_NAME("this_session"), {VAR_STRING}, VV_COMPAT},
389 {VV_NAME("version"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
390 {VV_NAME("lnum"), {VAR_NUMBER}, VV_RO_SBX},
391 {VV_NAME("termresponse"), {VAR_STRING}, VV_RO},
392 {VV_NAME("fname"), {VAR_STRING}, VV_RO},
393 {VV_NAME("lang"), {VAR_STRING}, VV_RO},
394 {VV_NAME("lc_time"), {VAR_STRING}, VV_RO},
395 {VV_NAME("ctype"), {VAR_STRING}, VV_RO},
396 {VV_NAME("charconvert_from"), {VAR_STRING}, VV_RO},
397 {VV_NAME("charconvert_to"), {VAR_STRING}, VV_RO},
398 {VV_NAME("fname_in"), {VAR_STRING}, VV_RO},
399 {VV_NAME("fname_out"), {VAR_STRING}, VV_RO},
400 {VV_NAME("fname_new"), {VAR_STRING}, VV_RO},
401 {VV_NAME("fname_diff"), {VAR_STRING}, VV_RO},
402 {VV_NAME("cmdarg"), {VAR_STRING}, VV_RO},
403 {VV_NAME("foldstart"), {VAR_NUMBER}, VV_RO_SBX},
404 {VV_NAME("foldend"), {VAR_NUMBER}, VV_RO_SBX},
405 {VV_NAME("folddashes"), {VAR_STRING}, VV_RO_SBX},
406 {VV_NAME("foldlevel"), {VAR_NUMBER}, VV_RO_SBX},
407 {VV_NAME("progname"), {VAR_STRING}, VV_RO},
408 {VV_NAME("servername"), {VAR_STRING}, VV_RO},
409 {VV_NAME("dying"), {VAR_NUMBER}, VV_RO},
410 {VV_NAME("exception"), {VAR_STRING}, VV_RO},
411 {VV_NAME("throwpoint"), {VAR_STRING}, VV_RO},
412 {VV_NAME("register"), {VAR_STRING}, VV_RO},
413 {VV_NAME("cmdbang"), {VAR_NUMBER}, VV_RO},
414 {VV_NAME("insertmode"), {VAR_STRING}, VV_RO},
415 {VV_NAME("val"), {VAR_UNKNOWN}, VV_RO},
416 {VV_NAME("key"), {VAR_UNKNOWN}, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417};
418
Bram Moolenaare9a41262005-01-15 22:18:47 +0000419/* shorthand */
420#define vv_nr tv.vval.v_number
421#define vv_str tv.vval.v_string
422
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000423static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
424static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
425static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
426static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
427static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
428static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
429static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
430static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
431static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
432static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
433static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
434static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
435static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000436static listvar *list_alloc __ARGS((void));
437static void list_unref __ARGS((listvar *l));
438static void list_free __ARGS((listvar *l));
439static listitem *listitem_alloc __ARGS((void));
440static void listitem_free __ARGS((listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000441static void listitem_remove __ARGS((listvar *l, listitem *item));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000442static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000443static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000444static int dict_equal __ARGS((dictvar *d1, dictvar *d2, int ic));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000445static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000446static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000447static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000448static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000449static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000450static void list_append __ARGS((listvar *l, listitem *item));
451static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000452static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
453static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
454static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000455static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000456static void list_remove __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000457static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000458static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
459
Bram Moolenaar8c711452005-01-14 21:53:12 +0000460static dictvar *dict_alloc __ARGS((void));
461static void dict_unref __ARGS((dictvar *d));
462static void dict_free __ARGS((dictvar *d));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000463static dictitem *dictitem_alloc __ARGS((char_u *key));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000464static dictitem *dictitem_copy __ARGS((dictitem *org));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000465static void dictitem_remove __ARGS((dictvar *dict, dictitem *item));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000466static void dictitem_free __ARGS((dictitem *item));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000467static int dict_add __ARGS((dictvar *d, dictitem *item));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000468static long dict_len __ARGS((dictvar *d));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000469static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000470static char_u *dict2string __ARGS((typeval *tv));
471static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
472
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000473static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000474static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000475static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000476static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000478static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000479static 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));
480static 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 +0000481
482static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000483static void f_append __ARGS((typeval *argvars, typeval *rettv));
484static void f_argc __ARGS((typeval *argvars, typeval *rettv));
485static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
486static void f_argv __ARGS((typeval *argvars, typeval *rettv));
487static void f_browse __ARGS((typeval *argvars, typeval *rettv));
488static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000489static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
490static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
491static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000492static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
493static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
494static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
495static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
496static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000497static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000498static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
499static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
500static void f_col __ARGS((typeval *argvars, typeval *rettv));
501static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
502static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000503static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000504static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
505static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
506static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
507static void f_delete __ARGS((typeval *argvars, typeval *rettv));
508static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
509static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
510static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000511static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000512static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000513static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000514static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
515static void f_executable __ARGS((typeval *argvars, typeval *rettv));
516static void f_exists __ARGS((typeval *argvars, typeval *rettv));
517static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000518static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000519static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
520static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000521static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000522static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
523static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000524static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
525static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
526static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000527static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
528static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
529static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
530static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
531static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000532static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000533static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
534static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
535static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
536static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
537static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
538static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
539static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
540static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
541static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
542static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
543static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
544static void f_getline __ARGS((typeval *argvars, typeval *rettv));
545static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
546static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
547static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
548static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
549static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
550static void f_glob __ARGS((typeval *argvars, typeval *rettv));
551static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
552static void f_has __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000553static void f_has_key __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000554static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
555static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
556static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
557static void f_histget __ARGS((typeval *argvars, typeval *rettv));
558static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000559static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000560static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000561static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
562static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
563static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000564static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000565static void f_input __ARGS((typeval *argvars, typeval *rettv));
566static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
567static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
568static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
569static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000570static void f_insert __ARGS((typeval *argvars, typeval *rettv));
571static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000572static void f_items __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000573static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000574static void f_keys __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000575static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
576static void f_len __ARGS((typeval *argvars, typeval *rettv));
577static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
578static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000579static void f_line __ARGS((typeval *argvars, typeval *rettv));
580static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
581static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
582static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000583static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000584static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
585static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586static void f_match __ARGS((typeval *argvars, typeval *rettv));
587static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
588static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000589static void f_max __ARGS((typeval *argvars, typeval *rettv));
590static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000591static void f_mode __ARGS((typeval *argvars, typeval *rettv));
592static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
593static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
594static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000595static void f_range __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
597static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
598static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
599static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
600static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000601static void f_remove __ARGS((typeval *argvars, typeval *rettv));
602static void f_rename __ARGS((typeval *argvars, typeval *rettv));
603static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
604static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
605static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
606static void f_search __ARGS((typeval *argvars, typeval *rettv));
607static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000608static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
609static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000610static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
611static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000612static void f_setline __ARGS((typeval *argvars, typeval *rettv));
613static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000614static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000615static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000616static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000617static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000618#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000619static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000621static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
622static void f_string __ARGS((typeval *argvars, typeval *rettv));
623static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
624static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
625static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
626static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000627static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
628static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000629static void f_synID __ARGS((typeval *argvars, typeval *rettv));
630static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
631static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
632static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000633static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
634static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
635static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
636static void f_tr __ARGS((typeval *argvars, typeval *rettv));
637static void f_type __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000638static void f_values __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000639static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
640static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
641static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
642static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
643static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
644static void f_winline __ARGS((typeval *argvars, typeval *rettv));
645static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
646static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
647static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000648
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000649static win_T *find_win_by_nr __ARGS((typeval *vp));
650static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651static int get_env_len __ARGS((char_u **arg));
652static int get_id_len __ARGS((char_u **arg));
653static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000654static 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 +0000655static int eval_isnamec __ARGS((int c));
656static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000657static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
658static typeval *alloc_tv __ARGS((void));
659static typeval *alloc_string_tv __ARGS((char_u *string));
660static void free_tv __ARGS((typeval *varp));
661static void clear_tv __ARGS((typeval *varp));
662static void init_tv __ARGS((typeval *varp));
663static long get_tv_number __ARGS((typeval *varp));
664static linenr_T get_tv_lnum __ARGS((typeval *argvars));
665static char_u *get_tv_string __ARGS((typeval *varp));
666static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667static VAR find_var __ARGS((char_u *name, int writing));
668static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
669static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000670static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671static void list_one_var __ARGS((VAR v, char_u *prefix));
672static void list_vim_var __ARGS((int i));
673static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000674static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000675static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000676static void item_copy __ARGS((typeval *from, typeval *to, int deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000678static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict *fd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679static int eval_fname_script __ARGS((char_u *p));
680static int eval_fname_sid __ARGS((char_u *p));
681static void list_func_head __ARGS((ufunc_T *fp, int indent));
682static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
683static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000684static int function_exists __ARGS((char_u *name));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000685static void func_free __ARGS((ufunc_T *fp));
686static void func_unref __ARGS((char_u *name));
687static void func_ref __ARGS((char_u *name));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000688static 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 +0000689
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690#define get_var_string(p) get_tv_string(&(p)->tv)
691#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
692#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694static 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 +0000695
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000696static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
697static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
698static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000699static void list_all_vars __ARGS((void));
700static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000701static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars, char_u *op));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000702static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000703static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet));
704static void clear_lval __ARGS((lval *lp));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000705static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy, char_u *op));
706static int tv_op __ARGS((typeval *tv1, typeval *tv2, char_u *op));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000707static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000708static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
709static void list_fix_watch __ARGS((listvar *l, listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000710static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712/*
713 * Set an internal variable to a string value. Creates the variable if it does
714 * not already exist.
715 */
716 void
717set_internal_string_var(name, value)
718 char_u *name;
719 char_u *value;
720{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000721 char_u *val;
722 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724 val = vim_strsave(value);
725 if (val != NULL)
726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000728 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000730 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 }
734}
735
736# if defined(FEAT_MBYTE) || defined(PROTO)
737 int
738eval_charconvert(enc_from, enc_to, fname_from, fname_to)
739 char_u *enc_from;
740 char_u *enc_to;
741 char_u *fname_from;
742 char_u *fname_to;
743{
744 int err = FALSE;
745
746 set_vim_var_string(VV_CC_FROM, enc_from, -1);
747 set_vim_var_string(VV_CC_TO, enc_to, -1);
748 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
749 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
750 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
751 err = TRUE;
752 set_vim_var_string(VV_CC_FROM, NULL, -1);
753 set_vim_var_string(VV_CC_TO, NULL, -1);
754 set_vim_var_string(VV_FNAME_IN, NULL, -1);
755 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
756
757 if (err)
758 return FAIL;
759 return OK;
760}
761# endif
762
763# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
764 int
765eval_printexpr(fname, args)
766 char_u *fname;
767 char_u *args;
768{
769 int err = FALSE;
770
771 set_vim_var_string(VV_FNAME_IN, fname, -1);
772 set_vim_var_string(VV_CMDARG, args, -1);
773 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
774 err = TRUE;
775 set_vim_var_string(VV_FNAME_IN, NULL, -1);
776 set_vim_var_string(VV_CMDARG, NULL, -1);
777
778 if (err)
779 {
780 mch_remove(fname);
781 return FAIL;
782 }
783 return OK;
784}
785# endif
786
787# if defined(FEAT_DIFF) || defined(PROTO)
788 void
789eval_diff(origfile, newfile, outfile)
790 char_u *origfile;
791 char_u *newfile;
792 char_u *outfile;
793{
794 int err = FALSE;
795
796 set_vim_var_string(VV_FNAME_IN, origfile, -1);
797 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
798 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
799 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
800 set_vim_var_string(VV_FNAME_IN, NULL, -1);
801 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
802 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
803}
804
805 void
806eval_patch(origfile, difffile, outfile)
807 char_u *origfile;
808 char_u *difffile;
809 char_u *outfile;
810{
811 int err;
812
813 set_vim_var_string(VV_FNAME_IN, origfile, -1);
814 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
815 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
816 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
817 set_vim_var_string(VV_FNAME_IN, NULL, -1);
818 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
819 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
820}
821# endif
822
823/*
824 * Top level evaluation function, returning a boolean.
825 * Sets "error" to TRUE if there was an error.
826 * Return TRUE or FALSE.
827 */
828 int
829eval_to_bool(arg, error, nextcmd, skip)
830 char_u *arg;
831 int *error;
832 char_u **nextcmd;
833 int skip; /* only parse, don't execute */
834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 int retval = FALSE;
837
838 if (skip)
839 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
843 {
844 *error = FALSE;
845 if (!skip)
846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 retval = (get_tv_number(&tv) != 0);
848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850 }
851 if (skip)
852 --emsg_skip;
853
854 return retval;
855}
856
857/*
858 * Top level evaluation function, returning a string. If "skip" is TRUE,
859 * only parsing to "nextcmd" is done, without reporting errors. Return
860 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
861 */
862 char_u *
863eval_to_string_skip(arg, nextcmd, skip)
864 char_u *arg;
865 char_u **nextcmd;
866 int skip; /* only parse, don't execute */
867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000868 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 char_u *retval;
870
871 if (skip)
872 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
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 if (skip)
881 --emsg_skip;
882
883 return retval;
884}
885
886/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000887 * Skip over an expression at "*pp".
888 * Return FAIL for an error, OK otherwise.
889 */
890 int
891skip_expr(pp)
892 char_u **pp;
893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000895
896 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000898}
899
900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * Top level evaluation function, returning a string.
902 * Return pointer to allocated memory, or NULL for failure.
903 */
904 char_u *
905eval_to_string(arg, nextcmd)
906 char_u *arg;
907 char_u **nextcmd;
908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000909 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 char_u *retval;
911
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 retval = NULL;
914 else
915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 retval = vim_strsave(get_tv_string(&tv));
917 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 return retval;
921}
922
923/*
924 * Call eval_to_string() with "sandbox" set and not using local variables.
925 */
926 char_u *
927eval_to_string_safe(arg, nextcmd)
928 char_u *arg;
929 char_u **nextcmd;
930{
931 char_u *retval;
932 void *save_funccalp;
933
934 save_funccalp = save_funccal();
935 ++sandbox;
936 retval = eval_to_string(arg, nextcmd);
937 --sandbox;
938 restore_funccal(save_funccalp);
939 return retval;
940}
941
942#if 0 /* not used */
943/*
944 * Top level evaluation function, returning a string.
945 * Advances "arg" to the first non-blank after the evaluated expression.
946 * Return pointer to allocated memory, or NULL for failure.
947 * Doesn't give error messages.
948 */
949 char_u *
950eval_arg_to_string(arg)
951 char_u **arg;
952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 char_u *retval;
955 int ret;
956
957 ++emsg_off;
958
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (ret == FAIL)
961 retval = NULL;
962 else
963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 retval = vim_strsave(get_tv_string(&rettv));
965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967
968 --emsg_off;
969
970 return retval;
971}
972#endif
973
974/*
975 * Top level evaluation function, returning a number.
976 * Evaluates "expr" silently.
977 * Returns -1 for an error.
978 */
979 int
980eval_to_number(expr)
981 char_u *expr;
982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000983 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000985 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 ++emsg_off;
988
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000989 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 retval = -1;
991 else
992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000993 retval = get_tv_number(&rettv);
994 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 --emsg_off;
997
998 return retval;
999}
1000
1001#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1002/*
1003 * Call some vimL function and return the result as a string
1004 * Uses argv[argc] for the function arguments.
1005 */
1006 char_u *
1007call_vim_function(func, argc, argv, safe)
1008 char_u *func;
1009 int argc;
1010 char_u **argv;
1011 int safe; /* use the sandbox */
1012{
1013 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 long n;
1017 int len;
1018 int i;
1019 int doesrange;
1020 void *save_funccalp = NULL;
1021
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001022 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (argvars == NULL)
1024 return NULL;
1025
1026 for (i = 0; i < argc; i++)
1027 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001028 /* Pass a NULL or empty argument as an empty string */
1029 if (argv[i] == NULL || *argv[i] == NUL)
1030 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001031 argvars[i].v_type = VAR_STRING;
1032 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001033 continue;
1034 }
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 /* Recognize a number argument, the others must be strings. */
1037 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1038 if (len != 0 && len == (int)STRLEN(argv[i]))
1039 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001040 argvars[i].v_type = VAR_NUMBER;
1041 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043 else
1044 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001045 argvars[i].v_type = VAR_STRING;
1046 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048 }
1049
1050 if (safe)
1051 {
1052 save_funccalp = save_funccal();
1053 ++sandbox;
1054 }
1055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1057 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001059 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001060 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 vim_free(argvars);
1064
1065 if (safe)
1066 {
1067 --sandbox;
1068 restore_funccal(save_funccalp);
1069 }
1070 return retval;
1071}
1072#endif
1073
1074/*
1075 * Save the current function call pointer, and set it to NULL.
1076 * Used when executing autocommands and for ":source".
1077 */
1078 void *
1079save_funccal()
1080{
1081 struct funccall *fc;
1082
1083 fc = current_funccal;
1084 current_funccal = NULL;
1085 return (void *)fc;
1086}
1087
1088 void
1089restore_funccal(fc)
1090 void *fc;
1091{
1092 current_funccal = (struct funccall *)fc;
1093}
1094
1095#ifdef FEAT_FOLDING
1096/*
1097 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1098 * it in "*cp". Doesn't give error messages.
1099 */
1100 int
1101eval_foldexpr(arg, cp)
1102 char_u *arg;
1103 int *cp;
1104{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 int retval;
1107 char_u *s;
1108
1109 ++emsg_off;
1110 ++sandbox;
1111 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001112 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 retval = 0;
1114 else
1115 {
1116 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 if (tv.v_type == VAR_NUMBER)
1118 retval = tv.vval.v_number;
1119 else if (tv.v_type == VAR_UNKNOWN
1120 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 retval = 0;
1122 else
1123 {
1124 /* If the result is a string, check if there is a non-digit before
1125 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (!VIM_ISDIGIT(*s) && *s != '-')
1128 *cp = *s++;
1129 retval = atol((char *)s);
1130 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 --emsg_off;
1134 --sandbox;
1135
1136 return retval;
1137}
1138#endif
1139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140/*
1141 * Expands out the 'magic' {}'s in a variable/function name.
1142 * Note that this can call itself recursively, to deal with
1143 * constructs like foo{bar}{baz}{bam}
1144 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1145 * "in_start" ^
1146 * "expr_start" ^
1147 * "expr_end" ^
1148 * "in_end" ^
1149 *
1150 * Returns a new allocated string, which the caller must free.
1151 * Returns NULL for failure.
1152 */
1153 static char_u *
1154make_expanded_name(in_start, expr_start, expr_end, in_end)
1155 char_u *in_start;
1156 char_u *expr_start;
1157 char_u *expr_end;
1158 char_u *in_end;
1159{
1160 char_u c1;
1161 char_u *retval = NULL;
1162 char_u *temp_result;
1163 char_u *nextcmd = NULL;
1164
1165 if (expr_end == NULL || in_end == NULL)
1166 return NULL;
1167 *expr_start = NUL;
1168 *expr_end = NUL;
1169 c1 = *in_end;
1170 *in_end = NUL;
1171
1172 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1173 if (temp_result != NULL && nextcmd == NULL)
1174 {
1175 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1176 + (in_end - expr_end) + 1));
1177
1178 if (retval != NULL)
1179 {
1180 STRCPY(retval, in_start);
1181 STRCAT(retval, temp_result);
1182 STRCAT(retval, expr_end + 1);
1183 }
1184 }
1185 vim_free(temp_result);
1186
1187 *in_end = c1; /* put char back for error messages */
1188 *expr_start = '{';
1189 *expr_end = '}';
1190
1191 if (retval != NULL)
1192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001193 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (expr_start != NULL)
1195 {
1196 /* Further expansion! */
1197 temp_result = make_expanded_name(retval, expr_start,
1198 expr_end, temp_result);
1199 vim_free(retval);
1200 retval = temp_result;
1201 }
1202 }
1203
1204 return retval;
1205
1206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
1208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 * ":let" list all variable values
1210 * ":let var1 var2" list variable values
1211 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001212 * ":let var += expr" assignment command.
1213 * ":let var -= expr" assignment command.
1214 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
1217 void
1218ex_let(eap)
1219 exarg_T *eap;
1220{
1221 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 char_u *expr = NULL;
1223 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 int var_count = 0;
1226 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001229 expr = skip_var_list(arg, &var_count, &semicolon);
1230 if (expr == NULL)
1231 return;
1232 expr = vim_strchr(expr, '=');
1233 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001235 if (*arg == '[')
1236 EMSG(_(e_invarg));
1237 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 /* ":let var1 var2" */
1239 arg = list_arg_vars(eap, arg);
1240 else if (!eap->skip)
1241 /* ":let" */
1242 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 eap->nextcmd = check_nextcmd(arg);
1244 }
1245 else
1246 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 op[0] = '=';
1248 op[1] = NUL;
1249 if (expr > arg)
1250 {
1251 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1252 op[0] = expr[-1]; /* +=, -= or .= */
1253 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001254 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (eap->skip)
1257 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001258 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (eap->skip)
1260 {
1261 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 --emsg_skip;
1264 }
1265 else if (i != FAIL)
1266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001267 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 }
1272}
1273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001274/*
1275 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1276 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277 * When "nextchars" is not NULL it points to a string with characters that
1278 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1279 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001280 * Returns OK or FAIL;
1281 */
1282 static int
1283ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1284 char_u *arg_start;
1285 typeval *tv;
1286 int copy; /* copy values from "tv", don't move */
1287 int semicolon; /* from skip_var_list() */
1288 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001289 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290{
1291 char_u *arg = arg_start;
1292 listvar *l;
1293 int i;
1294 listitem *item;
1295 typeval ltv;
1296
1297 if (*arg != '[')
1298 {
1299 /*
1300 * ":let var = expr" or ":for var in list"
1301 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001303 return FAIL;
1304 return OK;
1305 }
1306
1307 /*
1308 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1309 */
1310 l = tv->vval.v_list;
1311 if (tv->v_type != VAR_LIST || l == NULL)
1312 {
1313 EMSG(_(e_listreq));
1314 return FAIL;
1315 }
1316
1317 i = list_len(l);
1318 if (semicolon == 0 && var_count < i)
1319 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001320 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001321 return FAIL;
1322 }
1323 if (var_count - semicolon > i)
1324 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001325 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001326 return FAIL;
1327 }
1328
1329 item = l->lv_first;
1330 while (*arg != ']')
1331 {
1332 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001333 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001334 item = item->li_next;
1335 if (arg == NULL)
1336 return FAIL;
1337
1338 arg = skipwhite(arg);
1339 if (*arg == ';')
1340 {
1341 /* Put the rest of the list (may be empty) in the var after ';'.
1342 * Create a new list for this. */
1343 l = list_alloc();
1344 if (l == NULL)
1345 return FAIL;
1346 while (item != NULL)
1347 {
1348 list_append_tv(l, &item->li_tv);
1349 item = item->li_next;
1350 }
1351
1352 ltv.v_type = VAR_LIST;
1353 ltv.vval.v_list = l;
1354 l->lv_refcount = 1;
1355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1357 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001358 clear_tv(&ltv);
1359 if (arg == NULL)
1360 return FAIL;
1361 break;
1362 }
1363 else if (*arg != ',' && *arg != ']')
1364 {
1365 EMSG2(_(e_intern2), "ex_let_vars()");
1366 return FAIL;
1367 }
1368 }
1369
1370 return OK;
1371}
1372
1373/*
1374 * Skip over assignable variable "var" or list of variables "[var, var]".
1375 * Used for ":let varvar = expr" and ":for varvar in expr".
1376 * For "[var, var]" increment "*var_count" for each variable.
1377 * for "[var, var; var]" set "semicolon".
1378 * Return NULL for an error.
1379 */
1380 static char_u *
1381skip_var_list(arg, var_count, semicolon)
1382 char_u *arg;
1383 int *var_count;
1384 int *semicolon;
1385{
1386 char_u *p, *s;
1387
1388 if (*arg == '[')
1389 {
1390 /* "[var, var]": find the matching ']'. */
1391 p = arg;
1392 while (1)
1393 {
1394 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1395 s = skip_var_one(p);
1396 if (s == p)
1397 {
1398 EMSG2(_(e_invarg2), p);
1399 return NULL;
1400 }
1401 ++*var_count;
1402
1403 p = skipwhite(s);
1404 if (*p == ']')
1405 break;
1406 else if (*p == ';')
1407 {
1408 if (*semicolon == 1)
1409 {
1410 EMSG(_("Double ; in list of variables"));
1411 return NULL;
1412 }
1413 *semicolon = 1;
1414 }
1415 else if (*p != ',')
1416 {
1417 EMSG2(_(e_invarg2), p);
1418 return NULL;
1419 }
1420 }
1421 return p + 1;
1422 }
1423 else
1424 return skip_var_one(arg);
1425}
1426
1427 static char_u *
1428skip_var_one(arg)
1429 char_u *arg;
1430{
1431 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1432 ++arg;
1433 return find_name_end(arg, NULL, NULL, TRUE);
1434}
1435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 static void
1437list_all_vars()
1438{
1439 int i;
1440
1441 /*
1442 * List all variables.
1443 */
1444 for (i = 0; i < variables.ga_len && !got_int; ++i)
1445 if (VAR_ENTRY(i).v_name != NULL)
1446 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1447 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1448 if (BVAR_ENTRY(i).v_name != NULL)
1449 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1450 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1451 if (WVAR_ENTRY(i).v_name != NULL)
1452 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1453 for (i = 0; i < VV_LEN && !got_int; ++i)
Bram Moolenaare9a41262005-01-15 22:18:47 +00001454 if (vimvars[i].tv.v_type == VAR_NUMBER || vimvars[i].vv_str != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 list_vim_var(i);
1456}
1457
1458/*
1459 * List variables in "arg".
1460 */
1461 static char_u *
1462list_arg_vars(eap, arg)
1463 exarg_T *eap;
1464 char_u *arg;
1465{
1466 int error = FALSE;
1467 char_u *temp_string = NULL;
1468 int arg_len;
1469 char_u *expr_start;
1470 char_u *expr_end;
1471 char_u *name_end;
1472 int c1 = 0, c2;
1473 int i;
1474 VAR varp;
1475 char_u *name;
1476
1477 while (!ends_excmd(*arg) && !got_int)
1478 {
1479 /* Find the end of the name. */
1480 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1481
1482 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1483 {
1484 emsg_severe = TRUE;
1485 EMSG(_(e_trailing));
1486 break;
1487 }
1488 if (!error && !eap->skip)
1489 {
1490 if (expr_start != NULL)
1491 {
1492 temp_string = make_expanded_name(arg, expr_start,
1493 expr_end, name_end);
1494 if (temp_string == NULL)
1495 {
1496 /*
1497 * Report an invalid expression in braces, unless
1498 * the expression evaluation has been cancelled due
1499 * to an aborting error, an interrupt, or an
1500 * exception.
1501 */
1502 if (!aborting())
1503 {
1504 emsg_severe = TRUE;
1505 EMSG2(_(e_invarg2), arg);
1506 break;
1507 }
1508 error = TRUE;
1509 arg = skipwhite(name_end);
1510 continue;
1511 }
1512 arg = temp_string;
1513 arg_len = STRLEN(temp_string);
1514 }
1515 else
1516 {
1517 c1 = *name_end;
1518 *name_end = NUL;
1519 arg_len = (int)(name_end - arg);
1520 }
1521 i = find_vim_var(arg, arg_len);
1522 if (i >= 0)
1523 list_vim_var(i);
1524 else if (STRCMP("b:changedtick", arg) == 0)
1525 {
1526 char_u numbuf[NUMBUFLEN];
1527
1528 sprintf((char *)numbuf, "%ld",
1529 (long)curbuf->b_changedtick);
1530 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1531 VAR_NUMBER, numbuf);
1532 }
1533 else
1534 {
1535 varp = find_var(arg, FALSE);
1536 if (varp == NULL)
1537 {
1538 /* Skip further arguments but do continue to
1539 * search for a trailing command. */
1540 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1541 error = TRUE;
1542 }
1543 else
1544 {
1545 name = vim_strchr(arg, ':');
1546 if (name != NULL)
1547 {
1548 /* "a:" vars have no name stored, use whole arg */
1549 if (arg[0] == 'a' && arg[1] == ':')
1550 c2 = NUL;
1551 else
1552 {
1553 c2 = *++name;
1554 *name = NUL;
1555 }
1556 list_one_var(varp, arg);
1557 if (c2 != NUL)
1558 *name = c2;
1559 }
1560 else
1561 list_one_var(varp, (char_u *)"");
1562 }
1563 }
1564 if (expr_start != NULL)
1565 vim_free(temp_string);
1566 else
1567 *name_end = c1;
1568 }
1569 arg = skipwhite(name_end);
1570 }
1571
1572 return arg;
1573}
1574
1575/*
1576 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1577 * Returns a pointer to the char just after the var name.
1578 * Returns NULL if there is an error.
1579 */
1580 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001581ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 char_u *arg; /* points to variable name */
1583 typeval *tv; /* value to assign to variable */
1584 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587{
1588 int c1;
1589 char_u *name;
1590 char_u *p;
1591 char_u *arg_end = NULL;
1592 int len;
1593 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001594 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595
1596 /*
1597 * ":let $VAR = expr": Set environment variable.
1598 */
1599 if (*arg == '$')
1600 {
1601 /* Find the end of the name. */
1602 ++arg;
1603 name = arg;
1604 len = get_env_len(&arg);
1605 if (len == 0)
1606 EMSG2(_(e_invarg2), name - 1);
1607 else
1608 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 if (op != NULL && (*op == '+' || *op == '-'))
1610 EMSG2(_(e_letwrong), op);
1611 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 EMSG(_(e_letunexp));
1614 else
1615 {
1616 c1 = name[len];
1617 name[len] = NUL;
1618 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 if (op != NULL && *op == '.')
1620 {
1621 int mustfree = FALSE;
1622 char_u *s = vim_getenv(name, &mustfree);
1623
1624 if (s != NULL)
1625 {
1626 p = tofree = concat_str(s, p);
1627 if (mustfree)
1628 vim_free(s);
1629 }
1630 }
1631 if (p != NULL)
1632 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 if (STRICMP(name, "HOME") == 0)
1634 init_homedir();
1635 else if (didset_vim && STRICMP(name, "VIM") == 0)
1636 didset_vim = FALSE;
1637 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1638 didset_vimruntime = FALSE;
1639 name[len] = c1;
1640 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 }
1643 }
1644 }
1645
1646 /*
1647 * ":let &option = expr": Set option value.
1648 * ":let &l:option = expr": Set local option value.
1649 * ":let &g:option = expr": Set global option value.
1650 */
1651 else if (*arg == '&')
1652 {
1653 /* Find the end of the name. */
1654 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 if (p == NULL || (endchars != NULL
1656 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 EMSG(_(e_letunexp));
1658 else
1659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 long n;
1661 int opt_type;
1662 long numval;
1663 char_u *stringval = NULL;
1664 char_u *s;
1665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666 c1 = *p;
1667 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668
1669 n = get_tv_number(tv);
1670 s = get_tv_string(tv);
1671 if (op != NULL && *op != '=')
1672 {
1673 opt_type = get_option_value(arg, &numval,
1674 &stringval, opt_flags);
1675 if ((opt_type == 1 && *op == '.')
1676 || (opt_type == 0 && *op != '.'))
1677 EMSG2(_(e_letwrong), op);
1678 else
1679 {
1680 if (opt_type == 1) /* number */
1681 {
1682 if (*op == '+')
1683 n = numval + n;
1684 else
1685 n = numval - n;
1686 }
1687 else if (opt_type == 0 && stringval != NULL) /* string */
1688 {
1689 s = concat_str(stringval, s);
1690 vim_free(stringval);
1691 stringval = s;
1692 }
1693 }
1694 }
1695 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 *p = c1;
1697 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 }
1700 }
1701
1702 /*
1703 * ":let @r = expr": Set register contents.
1704 */
1705 else if (*arg == '@')
1706 {
1707 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 if (op != NULL && (*op == '+' || *op == '-'))
1709 EMSG2(_(e_letwrong), op);
1710 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 EMSG(_(e_letunexp));
1713 else
1714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715 char_u *tofree = NULL;
1716 char_u *s;
1717
1718 p = get_tv_string(tv);
1719 if (op != NULL && *op == '.')
1720 {
1721 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1722 if (s != NULL)
1723 {
1724 p = tofree = concat_str(s, p);
1725 vim_free(s);
1726 }
1727 }
1728 if (p != NULL)
1729 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 }
1733 }
1734
1735 /*
1736 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001737 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001739 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001741 lval lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001743 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1744 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001746 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1747 EMSG(_(e_letunexp));
1748 else
1749 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001750 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001751 arg_end = p;
1752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001754 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 }
1756
1757 else
1758 EMSG2(_(e_invarg2), arg);
1759
1760 return arg_end;
1761}
1762
1763/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001764 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1765 */
1766 static int
1767check_changedtick(arg)
1768 char_u *arg;
1769{
1770 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1771 {
1772 EMSG2(_(e_readonlyvar), arg);
1773 return TRUE;
1774 }
1775 return FALSE;
1776}
1777
1778/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001779 * Get an lval: variable, Dict item or List item that can be assigned a value
1780 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1781 * "name.key", "name.key[expr]" etc.
1782 * Indexing only works if "name" is an existing List or Dictionary.
1783 * "name" points to the start of the name.
1784 * If "rettv" is not NULL it points to the value to be assigned.
1785 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1786 * wrong; must end in space or cmd separator.
1787 *
1788 * Returns a pointer to just after the name, including indexes.
1789 * When an evaluation error occurs "lp->name" is NULL;
1790 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 */
1792 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 typeval *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 lval *lp;
1797 int unlet;
1798 int skip;
1799 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001802 char_u *expr_start, *expr_end;
1803 int cc;
1804 VAR v;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001806 typeval var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001807 int empty1 = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001808 listitem *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001809 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001810 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001812 /* Clear everything in "lp". */
1813 vim_memset(lp, 0, sizeof(lval));
1814
1815 if (skip)
1816 {
1817 /* When skipping just find the end of the name. */
1818 lp->ll_name = name;
1819 return find_name_end(name, NULL, NULL, TRUE);
1820 }
1821
1822 /* Find the end of the name. */
1823 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1824 if (expr_start != NULL)
1825 {
1826 /* Don't expand the name when we already know there is an error. */
1827 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1828 && *p != '[' && *p != '.')
1829 {
1830 EMSG(_(e_trailing));
1831 return NULL;
1832 }
1833
1834 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1835 if (lp->ll_exp_name == NULL)
1836 {
1837 /* Report an invalid expression in braces, unless the
1838 * expression evaluation has been cancelled due to an
1839 * aborting error, an interrupt, or an exception. */
1840 if (!aborting() && !quiet)
1841 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001842 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 EMSG2(_(e_invarg2), name);
1844 return NULL;
1845 }
1846 }
1847 lp->ll_name = lp->ll_exp_name;
1848 }
1849 else
1850 lp->ll_name = name;
1851
1852 /* Without [idx] or .key we are done. */
1853 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1854 return p;
1855
1856 cc = *p;
1857 *p = NUL;
1858 v = find_var(lp->ll_name, TRUE);
1859 if (v == NULL && !quiet)
1860 EMSG2(_(e_undefvar), lp->ll_name);
1861 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 if (v == NULL)
1863 return NULL;
1864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001865 /*
1866 * Loop until no more [idx] or .key is following.
1867 */
1868 lp->ll_tv = &v->tv;
1869 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001871 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1872 && !(lp->ll_tv->v_type == VAR_DICT
1873 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 if (!quiet)
1876 EMSG(_("E689: Can only index a List or Dictionary"));
1877 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001879 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 if (!quiet)
1882 EMSG(_("E708: [:] must come last"));
1883 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001885
Bram Moolenaar8c711452005-01-14 21:53:12 +00001886 len = -1;
1887 if (*p == '.')
1888 {
1889 key = p + 1;
1890 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1891 ;
1892 if (len == 0)
1893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 if (!quiet)
1895 EMSG(_(e_emptykey));
1896 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001897 }
1898 p = key + len;
1899 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001900 else
1901 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001902 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001903 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001904 if (*p == ':')
1905 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001906 else
1907 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001908 empty1 = FALSE;
1909 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001911 }
1912
1913 /* Optionally get the second index [ :expr]. */
1914 if (*p == ':')
1915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001920 if (!empty1)
1921 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 if (rettv != NULL && (rettv->v_type != VAR_LIST
1925 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 if (!quiet)
1928 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001929 if (!empty1)
1930 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001931 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001932 }
1933 p = skipwhite(p + 1);
1934 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001936 else
1937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001939 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 if (!empty1)
1942 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 }
1945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001948 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001950
Bram Moolenaar8c711452005-01-14 21:53:12 +00001951 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 if (!quiet)
1954 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001955 if (!empty1)
1956 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001958 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001960 }
1961
1962 /* Skip to past ']'. */
1963 ++p;
1964 }
1965
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 {
1968 if (len == -1)
1969 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 key = get_tv_string(&var1);
1972 if (*key == NUL)
1973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!quiet)
1975 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 }
1979 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 lp->ll_list = NULL;
1981 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001982 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001985 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001990 if (len == -1)
1991 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 }
1994 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 if (len == -1)
1999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 p = NULL;
2002 break;
2003 }
2004 if (len == -1)
2005 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002007 }
2008 else
2009 {
2010 /*
2011 * Get the number and item for the only or first index of the List.
2012 */
2013 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 else
2016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 clear_tv(&var1);
2019 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 lp->ll_list = lp->ll_tv->vval.v_list;
2022 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2023 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 if (!quiet)
2026 EMSGN(_(e_listidx), lp->ll_n1);
2027 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002028 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 }
2031
2032 /*
2033 * May need to find the item or absolute index for the second
2034 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 * When no index given: "lp->ll_empty2" is TRUE.
2036 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002037 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002043 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002044 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002045 if (ni == NULL)
2046 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 if (!quiet)
2048 EMSGN(_(e_listidx), lp->ll_n2);
2049 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002052 }
2053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2055 if (lp->ll_n1 < 0)
2056 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2057 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002058 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 if (!quiet)
2060 EMSGN(_(e_listidx), lp->ll_n2);
2061 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002062 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002063 }
2064
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 }
2068
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 return p;
2070}
2071
2072/*
2073 * Clear an "lval" that was filled by get_lval().
2074 */
2075 static void
2076clear_lval(lp)
2077 lval *lp;
2078{
2079 vim_free(lp->ll_exp_name);
2080 vim_free(lp->ll_newkey);
2081}
2082
2083/*
2084 * Set a variable that was parsed by get_lval().
2085 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 */
2088 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 lval *lp;
2091 char_u *endp;
2092 typeval *rettv;
2093 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095{
2096 int cc;
2097 listitem *ni;
2098 listitem *ri;
2099 dictitem *di;
2100
2101 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002103 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 cc = *endp;
2106 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107 if (op != NULL && *op != '=')
2108 {
2109 typeval tv;
2110
2111 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK)
2112 {
2113 if (tv_op(&tv, rettv, op) == OK)
2114 set_var(lp->ll_name, &tv, FALSE);
2115 clear_tv(&tv);
2116 }
2117 }
2118 else
2119 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 }
2123 else if (lp->ll_range)
2124 {
2125 /*
2126 * Assign the List values to the list items.
2127 */
2128 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002129 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 if (op != NULL && *op != '=')
2131 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2132 else
2133 {
2134 clear_tv(&lp->ll_li->li_tv);
2135 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 ri = ri->li_next;
2138 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2139 break;
2140 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002141 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 /* Need to add an empty item. */
2143 ni = listitem_alloc();
2144 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002145 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 ri = NULL;
2147 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002148 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 ni->li_tv.v_type = VAR_NUMBER;
2150 ni->li_tv.vval.v_number = 0;
2151 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002152 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_li = lp->ll_li->li_next;
2154 ++lp->ll_n1;
2155 }
2156 if (ri != NULL)
2157 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002158 else if (lp->ll_empty2
2159 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 : lp->ll_n1 != lp->ll_n2)
2161 EMSG(_("E711: List value has not enough items"));
2162 }
2163 else
2164 {
2165 /*
2166 * Assign to a List or Dictionary item.
2167 */
2168 if (lp->ll_newkey != NULL)
2169 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002170 if (op != NULL && *op != '=')
2171 {
2172 EMSG2(_(e_letwrong), op);
2173 return;
2174 }
2175
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002177 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 if (di == NULL)
2179 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002180 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2181 {
2182 vim_free(di);
2183 return;
2184 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002186 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 else if (op != NULL && *op != '=')
2188 {
2189 tv_op(lp->ll_tv, rettv, op);
2190 return;
2191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 /*
2196 * Assign the value to the variable or list item.
2197 */
2198 if (copy)
2199 copy_tv(rettv, lp->ll_tv);
2200 else
2201 {
2202 *lp->ll_tv = *rettv;
2203 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 }
2205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206}
2207
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002208/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002209 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2210 * Returns OK or FAIL.
2211 */
2212 static int
2213tv_op(tv1, tv2, op)
2214 typeval *tv1;
2215 typeval *tv2;
2216 char_u *op;
2217{
2218 long n;
2219 char_u numbuf[NUMBUFLEN];
2220 char_u *s;
2221
2222 /* Can't do anything with a Funcref or a Dict on the right. */
2223 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2224 {
2225 switch (tv1->v_type)
2226 {
2227 case VAR_DICT:
2228 case VAR_FUNC:
2229 break;
2230
2231 case VAR_LIST:
2232 if (*op != '+' || tv2->v_type != VAR_LIST)
2233 break;
2234 /* List += List */
2235 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2236 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2237 return OK;
2238
2239 case VAR_NUMBER:
2240 case VAR_STRING:
2241 if (tv2->v_type == VAR_LIST)
2242 break;
2243 if (*op == '+' || *op == '-')
2244 {
2245 /* nr += nr or nr -= nr*/
2246 n = get_tv_number(tv1);
2247 if (*op == '+')
2248 n += get_tv_number(tv2);
2249 else
2250 n -= get_tv_number(tv2);
2251 clear_tv(tv1);
2252 tv1->v_type = VAR_NUMBER;
2253 tv1->vval.v_number = n;
2254 }
2255 else
2256 {
2257 /* str .= str */
2258 s = get_tv_string(tv1);
2259 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2260 clear_tv(tv1);
2261 tv1->v_type = VAR_STRING;
2262 tv1->vval.v_string = s;
2263 }
2264 return OK;
2265 }
2266 }
2267
2268 EMSG2(_(e_letwrong), op);
2269 return FAIL;
2270}
2271
2272/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002273 * Add a watcher to a list.
2274 */
2275 static void
2276list_add_watch(l, lw)
2277 listvar *l;
2278 listwatch *lw;
2279{
2280 lw->lw_next = l->lv_watch;
2281 l->lv_watch = lw;
2282}
2283
2284/*
2285 * Remove a watches from a list.
2286 * No warning when it isn't found...
2287 */
2288 static void
2289list_rem_watch(l, lwrem)
2290 listvar *l;
2291 listwatch *lwrem;
2292{
2293 listwatch *lw, **lwp;
2294
2295 lwp = &l->lv_watch;
2296 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2297 {
2298 if (lw == lwrem)
2299 {
2300 *lwp = lw->lw_next;
2301 break;
2302 }
2303 lwp = &lw->lw_next;
2304 }
2305}
2306
2307/*
2308 * Just before removing an item from a list: advance watchers to the next
2309 * item.
2310 */
2311 static void
2312list_fix_watch(l, item)
2313 listvar *l;
2314 listitem *item;
2315{
2316 listwatch *lw;
2317
2318 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2319 if (lw->lw_item == item)
2320 lw->lw_item = item->li_next;
2321}
2322
2323/*
2324 * Evaluate the expression used in a ":for var in expr" command.
2325 * "arg" points to "var".
2326 * Set "*errp" to TRUE for an error, FALSE otherwise;
2327 * Return a pointer that holds the info. Null when there is an error.
2328 */
2329 void *
2330eval_for_line(arg, errp, nextcmdp, skip)
2331 char_u *arg;
2332 int *errp;
2333 char_u **nextcmdp;
2334 int skip;
2335{
2336 forinfo *fi;
2337 char_u *expr;
2338 typeval tv;
2339 listvar *l;
2340
2341 *errp = TRUE; /* default: there is an error */
2342
2343 fi = (forinfo *)alloc_clear(sizeof(forinfo));
2344 if (fi == NULL)
2345 return NULL;
2346
2347 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2348 if (expr == NULL)
2349 return fi;
2350
2351 expr = skipwhite(expr);
2352 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2353 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002354 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002355 return fi;
2356 }
2357
2358 if (skip)
2359 ++emsg_skip;
2360 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2361 {
2362 *errp = FALSE;
2363 if (!skip)
2364 {
2365 l = tv.vval.v_list;
2366 if (tv.v_type != VAR_LIST || l == NULL)
2367 EMSG(_(e_listreq));
2368 else
2369 {
2370 fi->fi_list = l;
2371 list_add_watch(l, &fi->fi_lw);
2372 fi->fi_lw.lw_item = l->lv_first;
2373 }
2374 }
2375 }
2376 if (skip)
2377 --emsg_skip;
2378
2379 return fi;
2380}
2381
2382/*
2383 * Use the first item in a ":for" list. Advance to the next.
2384 * Assign the values to the variable (list). "arg" points to the first one.
2385 * Return TRUE when a valid item was found, FALSE when at end of list or
2386 * something wrong.
2387 */
2388 int
2389next_for_item(fi_void, arg)
2390 void *fi_void;
2391 char_u *arg;
2392{
2393 forinfo *fi = (forinfo *)fi_void;
2394 int result;
2395 listitem *item;
2396
2397 item = fi->fi_lw.lw_item;
2398 if (item == NULL)
2399 result = FALSE;
2400 else
2401 {
2402 fi->fi_lw.lw_item = item->li_next;
2403 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2404 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2405 }
2406 return result;
2407}
2408
2409/*
2410 * Free the structure used to store info used by ":for".
2411 */
2412 void
2413free_for_info(fi_void)
2414 void *fi_void;
2415{
2416 forinfo *fi = (forinfo *)fi_void;
2417
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002418 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 list_rem_watch(fi->fi_list, &fi->fi_lw);
2420 vim_free(fi);
2421}
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2424
2425 void
2426set_context_for_expression(xp, arg, cmdidx)
2427 expand_T *xp;
2428 char_u *arg;
2429 cmdidx_T cmdidx;
2430{
2431 int got_eq = FALSE;
2432 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 if (cmdidx == CMD_let)
2436 {
2437 xp->xp_context = EXPAND_USER_VARS;
2438 if (vim_strchr(arg, '=') == NULL)
2439 {
2440 /* ":let var1 var2 ...": find last space. */
2441 for (p = arg + STRLEN(arg); p > arg; )
2442 {
2443 xp->xp_pattern = p;
2444 p = mb_ptr_back(arg, p);
2445 if (vim_iswhite(*p))
2446 break;
2447 }
2448 return;
2449 }
2450 }
2451 else
2452 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2453 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 while ((xp->xp_pattern = vim_strpbrk(arg,
2455 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2456 {
2457 c = *xp->xp_pattern;
2458 if (c == '&')
2459 {
2460 c = xp->xp_pattern[1];
2461 if (c == '&')
2462 {
2463 ++xp->xp_pattern;
2464 xp->xp_context = cmdidx != CMD_let || got_eq
2465 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2466 }
2467 else if (c != ' ')
2468 xp->xp_context = EXPAND_SETTINGS;
2469 }
2470 else if (c == '$')
2471 {
2472 /* environment variable */
2473 xp->xp_context = EXPAND_ENV_VARS;
2474 }
2475 else if (c == '=')
2476 {
2477 got_eq = TRUE;
2478 xp->xp_context = EXPAND_EXPRESSION;
2479 }
2480 else if (c == '<'
2481 && xp->xp_context == EXPAND_FUNCTIONS
2482 && vim_strchr(xp->xp_pattern, '(') == NULL)
2483 {
2484 /* Function name can start with "<SNR>" */
2485 break;
2486 }
2487 else if (cmdidx != CMD_let || got_eq)
2488 {
2489 if (c == '"') /* string */
2490 {
2491 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2492 if (c == '\\' && xp->xp_pattern[1] != NUL)
2493 ++xp->xp_pattern;
2494 xp->xp_context = EXPAND_NOTHING;
2495 }
2496 else if (c == '\'') /* literal string */
2497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2500 /* skip */ ;
2501 xp->xp_context = EXPAND_NOTHING;
2502 }
2503 else if (c == '|')
2504 {
2505 if (xp->xp_pattern[1] == '|')
2506 {
2507 ++xp->xp_pattern;
2508 xp->xp_context = EXPAND_EXPRESSION;
2509 }
2510 else
2511 xp->xp_context = EXPAND_COMMANDS;
2512 }
2513 else
2514 xp->xp_context = EXPAND_EXPRESSION;
2515 }
2516 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002517 /* Doesn't look like something valid, expand as an expression
2518 * anyway. */
2519 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 arg = xp->xp_pattern;
2521 if (*arg != NUL)
2522 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2523 /* skip */ ;
2524 }
2525 xp->xp_pattern = arg;
2526}
2527
2528#endif /* FEAT_CMDL_COMPL */
2529
2530/*
2531 * ":1,25call func(arg1, arg2)" function call.
2532 */
2533 void
2534ex_call(eap)
2535 exarg_T *eap;
2536{
2537 char_u *arg = eap->arg;
2538 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002540 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 int len;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 linenr_T lnum;
2544 int doesrange;
2545 int failed = FALSE;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002546 funcdict fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002548 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2549 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 if (tofree == NULL)
2551 return;
2552
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002553 /* Increase refcount on dictionary, it could get deleted when evaluating
2554 * the arguments. */
2555 if (fudi.fd_dict != NULL)
2556 ++fudi.fd_dict->dv_refcount;
2557
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2559 len = STRLEN(tofree);
2560 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 if (*startarg != '(')
2566 {
2567 EMSG2(_("E107: Missing braces: %s"), name);
2568 goto end;
2569 }
2570
2571 /*
2572 * When skipping, evaluate the function once, to find the end of the
2573 * arguments.
2574 * When the function takes a range, this is discovered after the first
2575 * call, and the loop is broken.
2576 */
2577 if (eap->skip)
2578 {
2579 ++emsg_skip;
2580 lnum = eap->line2; /* do it once, also with an invalid range */
2581 }
2582 else
2583 lnum = eap->line1;
2584 for ( ; lnum <= eap->line2; ++lnum)
2585 {
2586 if (!eap->skip && eap->addr_count > 0)
2587 {
2588 curwin->w_cursor.lnum = lnum;
2589 curwin->w_cursor.col = 0;
2590 }
2591 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002593 eap->line1, eap->line2, &doesrange,
2594 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 failed = TRUE;
2597 break;
2598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (doesrange || eap->skip)
2601 break;
2602 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002603 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002605 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (aborting())
2607 break;
2608 }
2609 if (eap->skip)
2610 --emsg_skip;
2611
2612 if (!failed)
2613 {
2614 /* Check for trailing illegal characters and a following command. */
2615 if (!ends_excmd(*arg))
2616 {
2617 emsg_severe = TRUE;
2618 EMSG(_(e_trailing));
2619 }
2620 else
2621 eap->nextcmd = check_nextcmd(arg);
2622 }
2623
2624end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002625 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002626 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627}
2628
2629/*
2630 * ":unlet[!] var1 ... " command.
2631 */
2632 void
2633ex_unlet(eap)
2634 exarg_T *eap;
2635{
2636 char_u *arg = eap->arg;
2637 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 int error = FALSE;
2639
2640 do
2641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 lval lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 /* Parse the name and find the end. */
2645 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2646 if (lv.ll_name == NULL)
2647 error = TRUE; /* error but continue parsing */
2648 if (name_end == NULL || (!vim_iswhite(*name_end)
2649 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (name_end != NULL)
2652 {
2653 emsg_severe = TRUE;
2654 EMSG(_(e_trailing));
2655 }
2656 if (!(eap->skip || error))
2657 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 break;
2659 }
2660
2661 if (!error && !eap->skip)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2663 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!eap->skip)
2666 clear_lval(&lv);
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 arg = skipwhite(name_end);
2669 } while (!ends_excmd(*arg));
2670
2671 eap->nextcmd = check_nextcmd(arg);
2672}
2673
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676do_unlet_var(lp, name_end, forceit)
2677 lval *lp;
2678 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 int forceit;
2680{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 int ret = OK;
2682 int cc;
2683
2684 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 cc = *name_end;
2687 *name_end = NUL;
2688
2689 /* Normal name or expanded name. */
2690 if (check_changedtick(lp->ll_name))
2691 ret = FAIL;
2692 else if (do_unlet(lp->ll_name) == FAIL && !forceit)
2693 {
2694 EMSG2(_("E108: No such variable: \"%s\""), lp->ll_name);
2695 ret = FAIL;
2696 }
2697 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 else if (lp->ll_range)
2700 {
2701 listitem *li;
2702
2703 /* Delete a range of List items. */
2704 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2705 {
2706 li = lp->ll_li->li_next;
2707 listitem_remove(lp->ll_list, lp->ll_li);
2708 lp->ll_li = li;
2709 ++lp->ll_n1;
2710 }
2711 }
2712 else
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* unlet a List item. */
2716 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002719 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 }
2721
2722 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723}
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725/*
2726 * "unlet" a variable. Return OK if it existed, FAIL if not.
2727 */
2728 int
2729do_unlet(name)
2730 char_u *name;
2731{
2732 VAR v;
2733
2734 v = find_var(name, TRUE);
2735 if (v != NULL)
2736 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002737 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return OK;
2739 }
2740 return FAIL;
2741}
2742
2743#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2744/*
2745 * Delete all "menutrans_" variables.
2746 */
2747 void
2748del_menutrans_vars()
2749{
2750 int i;
2751
2752 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002753 if (VAR_ENTRY(i).v_name != NULL
2754 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2755 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756}
2757#endif
2758
2759#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2760
2761/*
2762 * Local string buffer for the next two functions to store a variable name
2763 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2764 * get_user_var_name().
2765 */
2766
2767static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2768
2769static char_u *varnamebuf = NULL;
2770static int varnamebuflen = 0;
2771
2772/*
2773 * Function to concatenate a prefix and a variable name.
2774 */
2775 static char_u *
2776cat_prefix_varname(prefix, name)
2777 int prefix;
2778 char_u *name;
2779{
2780 int len;
2781
2782 len = (int)STRLEN(name) + 3;
2783 if (len > varnamebuflen)
2784 {
2785 vim_free(varnamebuf);
2786 len += 10; /* some additional space */
2787 varnamebuf = alloc(len);
2788 if (varnamebuf == NULL)
2789 {
2790 varnamebuflen = 0;
2791 return NULL;
2792 }
2793 varnamebuflen = len;
2794 }
2795 *varnamebuf = prefix;
2796 varnamebuf[1] = ':';
2797 STRCPY(varnamebuf + 2, name);
2798 return varnamebuf;
2799}
2800
2801/*
2802 * Function given to ExpandGeneric() to obtain the list of user defined
2803 * (global/buffer/window/built-in) variable names.
2804 */
2805/*ARGSUSED*/
2806 char_u *
2807get_user_var_name(xp, idx)
2808 expand_T *xp;
2809 int idx;
2810{
2811 static int gidx;
2812 static int bidx;
2813 static int widx;
2814 static int vidx;
2815 char_u *name;
2816
2817 if (idx == 0)
2818 gidx = bidx = widx = vidx = 0;
2819 if (gidx < variables.ga_len) /* Global variables */
2820 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002821 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 && gidx < variables.ga_len)
2823 /* skip */;
2824 if (name != NULL)
2825 {
2826 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2827 return cat_prefix_varname('g', name);
2828 else
2829 return name;
2830 }
2831 }
2832 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2833 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002834 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 && bidx < curbuf->b_vars.ga_len)
2836 /* skip */;
2837 if (name != NULL)
2838 return cat_prefix_varname('b', name);
2839 }
2840 if (bidx == curbuf->b_vars.ga_len)
2841 {
2842 ++bidx;
2843 return (char_u *)"b:changedtick";
2844 }
2845 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2846 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002847 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 && widx < curwin->w_vars.ga_len)
2849 /* skip */;
2850 if (name != NULL)
2851 return cat_prefix_varname('w', name);
2852 }
2853 if (vidx < VV_LEN) /* Built-in variables */
2854 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2855
2856 vim_free(varnamebuf);
2857 varnamebuf = NULL;
2858 varnamebuflen = 0;
2859 return NULL;
2860}
2861
2862#endif /* FEAT_CMDL_COMPL */
2863
2864/*
2865 * types for expressions.
2866 */
2867typedef enum
2868{
2869 TYPE_UNKNOWN = 0
2870 , TYPE_EQUAL /* == */
2871 , TYPE_NEQUAL /* != */
2872 , TYPE_GREATER /* > */
2873 , TYPE_GEQUAL /* >= */
2874 , TYPE_SMALLER /* < */
2875 , TYPE_SEQUAL /* <= */
2876 , TYPE_MATCH /* =~ */
2877 , TYPE_NOMATCH /* !~ */
2878} exptype_T;
2879
2880/*
2881 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002882 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2884 */
2885
2886/*
2887 * Handle zero level expression.
2888 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * Return OK or FAIL.
2891 */
2892 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002895 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 char_u **nextcmd;
2897 int evaluate;
2898{
2899 int ret;
2900 char_u *p;
2901
2902 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (ret == FAIL || !ends_excmd(*p))
2905 {
2906 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002907 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /*
2909 * Report the invalid expression unless the expression evaluation has
2910 * been cancelled due to an aborting error, an interrupt, or an
2911 * exception.
2912 */
2913 if (!aborting())
2914 EMSG2(_(e_invexpr2), arg);
2915 ret = FAIL;
2916 }
2917 if (nextcmd != NULL)
2918 *nextcmd = check_nextcmd(p);
2919
2920 return ret;
2921}
2922
2923/*
2924 * Handle top level expression:
2925 * expr1 ? expr0 : expr0
2926 *
2927 * "arg" must point to the first non-white of the expression.
2928 * "arg" is advanced to the next non-white after the recognized expression.
2929 *
2930 * Return OK or FAIL.
2931 */
2932 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 int evaluate;
2937{
2938 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002939 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 /*
2942 * Get the first variable.
2943 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return FAIL;
2946
2947 if ((*arg)[0] == '?')
2948 {
2949 result = FALSE;
2950 if (evaluate)
2951 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002954 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956
2957 /*
2958 * Get the second variable.
2959 */
2960 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 return FAIL;
2963
2964 /*
2965 * Check for the ":".
2966 */
2967 if ((*arg)[0] != ':')
2968 {
2969 EMSG(_("E109: Missing ':' after '?'"));
2970 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002971 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 return FAIL;
2973 }
2974
2975 /*
2976 * Get the third variable.
2977 */
2978 *arg = skipwhite(*arg + 1);
2979 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2980 {
2981 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 return FAIL;
2984 }
2985 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002986 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
2988
2989 return OK;
2990}
2991
2992/*
2993 * Handle first level expression:
2994 * expr2 || expr2 || expr2 logical OR
2995 *
2996 * "arg" must point to the first non-white of the expression.
2997 * "arg" is advanced to the next non-white after the recognized expression.
2998 *
2999 * Return OK or FAIL.
3000 */
3001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 int evaluate;
3006{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003007 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 long result;
3009 int first;
3010
3011 /*
3012 * Get the first variable.
3013 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003014 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 return FAIL;
3016
3017 /*
3018 * Repeat until there is no following "||".
3019 */
3020 first = TRUE;
3021 result = FALSE;
3022 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3023 {
3024 if (evaluate && first)
3025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003026 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 first = FALSE;
3030 }
3031
3032 /*
3033 * Get the second variable.
3034 */
3035 *arg = skipwhite(*arg + 2);
3036 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3037 return FAIL;
3038
3039 /*
3040 * Compute the result.
3041 */
3042 if (evaluate && !result)
3043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
3048 if (evaluate)
3049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003050 rettv->v_type = VAR_NUMBER;
3051 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 }
3054
3055 return OK;
3056}
3057
3058/*
3059 * Handle second level expression:
3060 * expr3 && expr3 && expr3 logical AND
3061 *
3062 * "arg" must point to the first non-white of the expression.
3063 * "arg" is advanced to the next non-white after the recognized expression.
3064 *
3065 * Return OK or FAIL.
3066 */
3067 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 int evaluate;
3072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003073 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 long result;
3075 int first;
3076
3077 /*
3078 * Get the first variable.
3079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return FAIL;
3082
3083 /*
3084 * Repeat until there is no following "&&".
3085 */
3086 first = TRUE;
3087 result = TRUE;
3088 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3089 {
3090 if (evaluate && first)
3091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 first = FALSE;
3096 }
3097
3098 /*
3099 * Get the second variable.
3100 */
3101 *arg = skipwhite(*arg + 2);
3102 if (eval4(arg, &var2, evaluate && result) == FAIL)
3103 return FAIL;
3104
3105 /*
3106 * Compute the result.
3107 */
3108 if (evaluate && result)
3109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003112 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
3114 if (evaluate)
3115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003116 rettv->v_type = VAR_NUMBER;
3117 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 }
3120
3121 return OK;
3122}
3123
3124/*
3125 * Handle third level expression:
3126 * var1 == var2
3127 * var1 =~ var2
3128 * var1 != var2
3129 * var1 !~ var2
3130 * var1 > var2
3131 * var1 >= var2
3132 * var1 < var2
3133 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003134 * var1 is var2
3135 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 *
3137 * "arg" must point to the first non-white of the expression.
3138 * "arg" is advanced to the next non-white after the recognized expression.
3139 *
3140 * Return OK or FAIL.
3141 */
3142 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003143eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003145 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int evaluate;
3147{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003148 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 char_u *p;
3150 int i;
3151 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003152 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int len = 2;
3154 long n1, n2;
3155 char_u *s1, *s2;
3156 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3157 regmatch_T regmatch;
3158 int ic;
3159 char_u *save_cpo;
3160
3161 /*
3162 * Get the first variable.
3163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003164 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 return FAIL;
3166
3167 p = *arg;
3168 switch (p[0])
3169 {
3170 case '=': if (p[1] == '=')
3171 type = TYPE_EQUAL;
3172 else if (p[1] == '~')
3173 type = TYPE_MATCH;
3174 break;
3175 case '!': if (p[1] == '=')
3176 type = TYPE_NEQUAL;
3177 else if (p[1] == '~')
3178 type = TYPE_NOMATCH;
3179 break;
3180 case '>': if (p[1] != '=')
3181 {
3182 type = TYPE_GREATER;
3183 len = 1;
3184 }
3185 else
3186 type = TYPE_GEQUAL;
3187 break;
3188 case '<': if (p[1] != '=')
3189 {
3190 type = TYPE_SMALLER;
3191 len = 1;
3192 }
3193 else
3194 type = TYPE_SEQUAL;
3195 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003196 case 'i': if (p[1] == 's')
3197 {
3198 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3199 len = 5;
3200 if (!vim_isIDc(p[len]))
3201 {
3202 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3203 type_is = TRUE;
3204 }
3205 }
3206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208
3209 /*
3210 * If there is a comparitive operator, use it.
3211 */
3212 if (type != TYPE_UNKNOWN)
3213 {
3214 /* extra question mark appended: ignore case */
3215 if (p[len] == '?')
3216 {
3217 ic = TRUE;
3218 ++len;
3219 }
3220 /* extra '#' appended: match case */
3221 else if (p[len] == '#')
3222 {
3223 ic = FALSE;
3224 ++len;
3225 }
3226 /* nothing appened: use 'ignorecase' */
3227 else
3228 ic = p_ic;
3229
3230 /*
3231 * Get the second variable.
3232 */
3233 *arg = skipwhite(p + len);
3234 if (eval5(arg, &var2, evaluate) == FAIL)
3235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 return FAIL;
3238 }
3239
3240 if (evaluate)
3241 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003242 if (type_is && rettv->v_type != var2.v_type)
3243 {
3244 /* For "is" a different type always means FALSE, for "notis"
3245 * it means TRUE. */
3246 n1 = (type == TYPE_NEQUAL);
3247 }
3248 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3249 {
3250 if (type_is)
3251 {
3252 n1 = (rettv->v_type == var2.v_type
3253 && rettv->vval.v_list == var2.vval.v_list);
3254 if (type == TYPE_NEQUAL)
3255 n1 = !n1;
3256 }
3257 else if (rettv->v_type != var2.v_type
3258 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3259 {
3260 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003261 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003262 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003263 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003264 clear_tv(rettv);
3265 clear_tv(&var2);
3266 return FAIL;
3267 }
3268 else
3269 {
3270 /* Compare two Lists for being equal or unequal. */
3271 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3272 if (type == TYPE_NEQUAL)
3273 n1 = !n1;
3274 }
3275 }
3276
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003277 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3278 {
3279 if (type_is)
3280 {
3281 n1 = (rettv->v_type == var2.v_type
3282 && rettv->vval.v_dict == var2.vval.v_dict);
3283 if (type == TYPE_NEQUAL)
3284 n1 = !n1;
3285 }
3286 else if (rettv->v_type != var2.v_type
3287 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3288 {
3289 if (rettv->v_type != var2.v_type)
3290 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3291 else
3292 EMSG(_("E736: Invalid operation for Dictionary"));
3293 clear_tv(rettv);
3294 clear_tv(&var2);
3295 return FAIL;
3296 }
3297 else
3298 {
3299 /* Compare two Dictionaries for being equal or unequal. */
3300 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3301 if (type == TYPE_NEQUAL)
3302 n1 = !n1;
3303 }
3304 }
3305
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003306 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3307 {
3308 if (rettv->v_type != var2.v_type
3309 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3310 {
3311 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003312 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003313 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003314 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003315 clear_tv(rettv);
3316 clear_tv(&var2);
3317 return FAIL;
3318 }
3319 else
3320 {
3321 /* Compare two Funcrefs for being equal or unequal. */
3322 if (rettv->vval.v_string == NULL
3323 || var2.vval.v_string == NULL)
3324 n1 = FALSE;
3325 else
3326 n1 = STRCMP(rettv->vval.v_string,
3327 var2.vval.v_string) == 0;
3328 if (type == TYPE_NEQUAL)
3329 n1 = !n1;
3330 }
3331 }
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 /*
3334 * If one of the two variables is a number, compare as a number.
3335 * When using "=~" or "!~", always compare as string.
3336 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003337 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003340 n1 = get_tv_number(rettv);
3341 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 switch (type)
3343 {
3344 case TYPE_EQUAL: n1 = (n1 == n2); break;
3345 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3346 case TYPE_GREATER: n1 = (n1 > n2); break;
3347 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3348 case TYPE_SMALLER: n1 = (n1 < n2); break;
3349 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3350 case TYPE_UNKNOWN:
3351 case TYPE_MATCH:
3352 case TYPE_NOMATCH: break; /* avoid gcc warning */
3353 }
3354 }
3355 else
3356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 s1 = get_tv_string_buf(rettv, buf1);
3358 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3360 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3361 else
3362 i = 0;
3363 n1 = FALSE;
3364 switch (type)
3365 {
3366 case TYPE_EQUAL: n1 = (i == 0); break;
3367 case TYPE_NEQUAL: n1 = (i != 0); break;
3368 case TYPE_GREATER: n1 = (i > 0); break;
3369 case TYPE_GEQUAL: n1 = (i >= 0); break;
3370 case TYPE_SMALLER: n1 = (i < 0); break;
3371 case TYPE_SEQUAL: n1 = (i <= 0); break;
3372
3373 case TYPE_MATCH:
3374 case TYPE_NOMATCH:
3375 /* avoid 'l' flag in 'cpoptions' */
3376 save_cpo = p_cpo;
3377 p_cpo = (char_u *)"";
3378 regmatch.regprog = vim_regcomp(s2,
3379 RE_MAGIC + RE_STRING);
3380 regmatch.rm_ic = ic;
3381 if (regmatch.regprog != NULL)
3382 {
3383 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3384 vim_free(regmatch.regprog);
3385 if (type == TYPE_NOMATCH)
3386 n1 = !n1;
3387 }
3388 p_cpo = save_cpo;
3389 break;
3390
3391 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3392 }
3393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 clear_tv(rettv);
3395 clear_tv(&var2);
3396 rettv->v_type = VAR_NUMBER;
3397 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399 }
3400
3401 return OK;
3402}
3403
3404/*
3405 * Handle fourth level expression:
3406 * + number addition
3407 * - number subtraction
3408 * . string concatenation
3409 *
3410 * "arg" must point to the first non-white of the expression.
3411 * "arg" is advanced to the next non-white after the recognized expression.
3412 *
3413 * Return OK or FAIL.
3414 */
3415 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003416eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 int evaluate;
3420{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003421 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003422 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 int op;
3424 long n1, n2;
3425 char_u *s1, *s2;
3426 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3427 char_u *p;
3428
3429 /*
3430 * Get the first variable.
3431 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003432 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return FAIL;
3434
3435 /*
3436 * Repeat computing, until no '+', '-' or '.' is following.
3437 */
3438 for (;;)
3439 {
3440 op = **arg;
3441 if (op != '+' && op != '-' && op != '.')
3442 break;
3443
3444 /*
3445 * Get the second variable.
3446 */
3447 *arg = skipwhite(*arg + 1);
3448 if (eval6(arg, &var2, evaluate) == FAIL)
3449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return FAIL;
3452 }
3453
3454 if (evaluate)
3455 {
3456 /*
3457 * Compute the result.
3458 */
3459 if (op == '.')
3460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003461 s1 = get_tv_string_buf(rettv, buf1);
3462 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003463 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003464 clear_tv(rettv);
3465 rettv->v_type = VAR_STRING;
3466 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003468 else if (op == '+' && rettv->v_type == VAR_LIST
3469 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003470 {
3471 /* concatenate Lists */
3472 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3473 &var3) == FAIL)
3474 {
3475 clear_tv(rettv);
3476 clear_tv(&var2);
3477 return FAIL;
3478 }
3479 clear_tv(rettv);
3480 *rettv = var3;
3481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else
3483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 n1 = get_tv_number(rettv);
3485 n2 = get_tv_number(&var2);
3486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (op == '+')
3488 n1 = n1 + n2;
3489 else
3490 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 rettv->v_type = VAR_NUMBER;
3492 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 }
3497 return OK;
3498}
3499
3500/*
3501 * Handle fifth level expression:
3502 * * number multiplication
3503 * / number division
3504 * % number modulo
3505 *
3506 * "arg" must point to the first non-white of the expression.
3507 * "arg" is advanced to the next non-white after the recognized expression.
3508 *
3509 * Return OK or FAIL.
3510 */
3511 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003512eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003514 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 int evaluate;
3516{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int op;
3519 long n1, n2;
3520
3521 /*
3522 * Get the first variable.
3523 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return FAIL;
3526
3527 /*
3528 * Repeat computing, until no '*', '/' or '%' is following.
3529 */
3530 for (;;)
3531 {
3532 op = **arg;
3533 if (op != '*' && op != '/' && op != '%')
3534 break;
3535
3536 if (evaluate)
3537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003538 n1 = get_tv_number(rettv);
3539 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 else
3542 n1 = 0;
3543
3544 /*
3545 * Get the second variable.
3546 */
3547 *arg = skipwhite(*arg + 1);
3548 if (eval7(arg, &var2, evaluate) == FAIL)
3549 return FAIL;
3550
3551 if (evaluate)
3552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 n2 = get_tv_number(&var2);
3554 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 /*
3557 * Compute the result.
3558 */
3559 if (op == '*')
3560 n1 = n1 * n2;
3561 else if (op == '/')
3562 {
3563 if (n2 == 0) /* give an error message? */
3564 n1 = 0x7fffffffL;
3565 else
3566 n1 = n1 / n2;
3567 }
3568 else
3569 {
3570 if (n2 == 0) /* give an error message? */
3571 n1 = 0;
3572 else
3573 n1 = n1 % n2;
3574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003575 rettv->v_type = VAR_NUMBER;
3576 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 }
3579
3580 return OK;
3581}
3582
3583/*
3584 * Handle sixth level expression:
3585 * number number constant
3586 * "string" string contstant
3587 * 'string' literal string contstant
3588 * &option-name option value
3589 * @r register contents
3590 * identifier variable value
3591 * function() function call
3592 * $VAR environment variable
3593 * (expression) nested expression
3594 *
3595 * Also handle:
3596 * ! in front logical NOT
3597 * - in front unary minus
3598 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 * trailing [] subscript in String or List
3600 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 *
3602 * "arg" must point to the first non-white of the expression.
3603 * "arg" is advanced to the next non-white after the recognized expression.
3604 *
3605 * Return OK or FAIL.
3606 */
3607 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int evaluate;
3612{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 long n;
3614 int len;
3615 char_u *s;
3616 int val;
3617 char_u *start_leader, *end_leader;
3618 int ret = OK;
3619 char_u *alias;
Bram Moolenaare9a41262005-01-15 22:18:47 +00003620 dictvar *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003624 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 /*
3629 * Skip '!' and '-' characters. They are handled later.
3630 */
3631 start_leader = *arg;
3632 while (**arg == '!' || **arg == '-' || **arg == '+')
3633 *arg = skipwhite(*arg + 1);
3634 end_leader = *arg;
3635
3636 switch (**arg)
3637 {
3638 /*
3639 * Number constant.
3640 */
3641 case '0':
3642 case '1':
3643 case '2':
3644 case '3':
3645 case '4':
3646 case '5':
3647 case '6':
3648 case '7':
3649 case '8':
3650 case '9':
3651 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3652 *arg += len;
3653 if (evaluate)
3654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 rettv->v_type = VAR_NUMBER;
3656 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 break;
3659
3660 /*
3661 * String constant: "string".
3662 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003663 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 break;
3665
3666 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003670 break;
3671
3672 /*
3673 * List: [expr, expr]
3674 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 break;
3677
3678 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 * Dictionary: {key: val, key: val}
3680 */
3681 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3682 break;
3683
3684 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003685 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003687 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 break;
3689
3690 /*
3691 * Environment variable: $VAR.
3692 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 break;
3695
3696 /*
3697 * Register contents: @r.
3698 */
3699 case '@': ++*arg;
3700 if (evaluate)
3701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003702 rettv->v_type = VAR_STRING;
3703 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 if (**arg != NUL)
3706 ++*arg;
3707 break;
3708
3709 /*
3710 * nested expression: (expression).
3711 */
3712 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (**arg == ')')
3715 ++*arg;
3716 else if (ret == OK)
3717 {
3718 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 ret = FAIL;
3721 }
3722 break;
3723
Bram Moolenaar8c711452005-01-14 21:53:12 +00003724 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 break;
3726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727
3728 if (ret == NOTDONE)
3729 {
3730 /*
3731 * Must be a variable or function name.
3732 * Can also be a curly-braces kind of name: {expr}.
3733 */
3734 s = *arg;
3735 len = get_func_len(arg, &alias, evaluate);
3736 if (alias != NULL)
3737 s = alias;
3738
3739 if (len == 0)
3740 ret = FAIL;
3741 else
3742 {
3743 if (**arg == '(') /* recursive! */
3744 {
3745 /* If "s" is the name of a variable of type VAR_FUNC
3746 * use its contents. */
3747 s = deref_func_name(s, &len);
3748
3749 /* Invoke the function. */
3750 ret = get_func_tv(s, len, rettv, arg,
3751 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003752 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003753 /* Stop the expression evaluation when immediately
3754 * aborting on error, or when an interrupt occurred or
3755 * an exception was thrown but not caught. */
3756 if (aborting())
3757 {
3758 if (ret == OK)
3759 clear_tv(rettv);
3760 ret = FAIL;
3761 }
3762 }
3763 else if (evaluate)
3764 ret = get_var_tv(s, len, rettv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003765 else
3766 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003767 }
3768
3769 if (alias != NULL)
3770 vim_free(alias);
3771 }
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 *arg = skipwhite(*arg);
3774
3775 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003776 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00003777 * Also handle function call with Funcref variable: func(expr)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003778 * Can all be combined: dict.func(expr)[idx]['func'](expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003780 selfdict = NULL;
3781 while (ret == OK
3782 && (**arg == '['
3783 || (**arg == '.' && rettv->v_type == VAR_DICT)
3784 || (**arg == '(' && rettv->v_type == VAR_FUNC))
3785 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003787 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003789 s = rettv->vval.v_string;
3790
3791 /* Invoke the function. Recursive! */
3792 ret = get_func_tv(s, STRLEN(s), rettv, arg,
3793 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3794 &len, evaluate, selfdict);
3795
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003796 /* Stop the expression evaluation when immediately aborting on
3797 * error, or when an interrupt occurred or an exception was thrown
3798 * but not caught. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003799 if (aborting())
3800 {
3801 if (ret == OK)
3802 clear_tv(rettv);
3803 ret = FAIL;
3804 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003805 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003806 selfdict = NULL;
3807 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003808 else /* **arg == '[' || **arg == '.' */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003809 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003810 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003811 if (rettv->v_type == VAR_DICT)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003812 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003813 selfdict = rettv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003814 if (selfdict != NULL)
3815 ++selfdict->dv_refcount;
3816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003817 else
3818 selfdict = NULL;
3819 if (eval_index(arg, rettv, evaluate) == FAIL)
3820 {
3821 clear_tv(rettv);
3822 ret = FAIL;
3823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003826 dict_unref(selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 /*
3829 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3830 */
3831 if (ret == OK && evaluate && end_leader > start_leader)
3832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 while (end_leader > start_leader)
3835 {
3836 --end_leader;
3837 if (*end_leader == '!')
3838 val = !val;
3839 else if (*end_leader == '-')
3840 val = -val;
3841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 clear_tv(rettv);
3843 rettv->v_type = VAR_NUMBER;
3844 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846
3847 return ret;
3848}
3849
3850/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003851 * Evaluate an "[expr]" or "[expr:expr]" index.
3852 * "*arg" points to the '['.
3853 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3854 */
3855 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003857 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003859 int evaluate;
3860{
3861 int empty1 = FALSE, empty2 = FALSE;
3862 typeval var1, var2;
3863 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003864 long len = -1;
3865 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003866 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003867 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003868
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003869 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003870 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003871 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003872 return FAIL;
3873 }
3874
Bram Moolenaar8c711452005-01-14 21:53:12 +00003875 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003876 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003877 /*
3878 * dict.name
3879 */
3880 key = *arg + 1;
3881 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3882 ;
3883 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003884 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003886 }
3887 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003889 /*
3890 * something[idx]
3891 *
3892 * Get the (first) variable from inside the [].
3893 */
3894 *arg = skipwhite(*arg + 1);
3895 if (**arg == ':')
3896 empty1 = TRUE;
3897 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3898 return FAIL;
3899
3900 /*
3901 * Get the second variable from inside the [:].
3902 */
3903 if (**arg == ':')
3904 {
3905 range = TRUE;
3906 *arg = skipwhite(*arg + 1);
3907 if (**arg == ']')
3908 empty2 = TRUE;
3909 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3910 {
3911 clear_tv(&var1);
3912 return FAIL;
3913 }
3914 }
3915
3916 /* Check for the ']'. */
3917 if (**arg != ']')
3918 {
3919 EMSG(_(e_missbrac));
3920 clear_tv(&var1);
3921 if (range)
3922 clear_tv(&var2);
3923 return FAIL;
3924 }
3925 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003926 }
3927
3928 if (evaluate)
3929 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003930 n1 = 0;
3931 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 n1 = get_tv_number(&var1);
3934 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003935 }
3936 if (range)
3937 {
3938 if (empty2)
3939 n2 = -1;
3940 else
3941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 n2 = get_tv_number(&var2);
3943 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003944 }
3945 }
3946
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003948 {
3949 case VAR_NUMBER:
3950 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003952 len = (long)STRLEN(s);
3953 if (range)
3954 {
3955 /* The resulting variable is a substring. If the indexes
3956 * are out of range the result is empty. */
3957 if (n1 < 0)
3958 {
3959 n1 = len + n1;
3960 if (n1 < 0)
3961 n1 = 0;
3962 }
3963 if (n2 < 0)
3964 n2 = len + n2;
3965 else if (n2 >= len)
3966 n2 = len;
3967 if (n1 >= len || n2 < 0 || n1 > n2)
3968 s = NULL;
3969 else
3970 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3971 }
3972 else
3973 {
3974 /* The resulting variable is a string of a single
3975 * character. If the index is too big or negative the
3976 * result is empty. */
3977 if (n1 >= len || n1 < 0)
3978 s = NULL;
3979 else
3980 s = vim_strnsave(s + n1, 1);
3981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 clear_tv(rettv);
3983 rettv->v_type = VAR_STRING;
3984 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003985 break;
3986
3987 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003989 if (n1 < 0)
3990 n1 = len + n1;
3991 if (!empty1 && (n1 < 0 || n1 >= len))
3992 {
3993 EMSGN(_(e_listidx), n1);
3994 return FAIL;
3995 }
3996 if (range)
3997 {
3998 listvar *l;
3999 listitem *item;
4000
4001 if (n2 < 0)
4002 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004003 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004004 {
4005 EMSGN(_(e_listidx), n2);
4006 return FAIL;
4007 }
4008 l = list_alloc();
4009 if (l == NULL)
4010 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004012 n1 <= n2; ++n1)
4013 {
4014 if (list_append_tv(l, &item->li_tv) == FAIL)
4015 {
4016 list_free(l);
4017 return FAIL;
4018 }
4019 item = item->li_next;
4020 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004021 clear_tv(rettv);
4022 rettv->v_type = VAR_LIST;
4023 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004024 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004025 }
4026 else
4027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004029 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
4031 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004032 }
4033 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004034
4035 case VAR_DICT:
4036 if (range)
4037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004038 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004039 if (len == -1)
4040 clear_tv(&var1);
4041 return FAIL;
4042 }
4043 {
4044 dictitem *item;
4045
4046 if (len == -1)
4047 {
4048 key = get_tv_string(&var1);
4049 if (*key == NUL)
4050 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004051 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 clear_tv(&var1);
4053 return FAIL;
4054 }
4055 }
4056
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004057 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004058
4059 if (item == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004060 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004061 if (len == -1)
4062 clear_tv(&var1);
4063 if (item == NULL)
4064 return FAIL;
4065
4066 copy_tv(&item->di_tv, &var1);
4067 clear_tv(rettv);
4068 *rettv = var1;
4069 }
4070 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004071 }
4072 }
4073
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 return OK;
4075}
4076
4077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * Get an option value.
4079 * "arg" points to the '&' or '+' before the option name.
4080 * "arg" is advanced to character after the option name.
4081 * Return OK or FAIL.
4082 */
4083 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 int evaluate;
4088{
4089 char_u *option_end;
4090 long numval;
4091 char_u *stringval;
4092 int opt_type;
4093 int c;
4094 int working = (**arg == '+'); /* has("+option") */
4095 int ret = OK;
4096 int opt_flags;
4097
4098 /*
4099 * Isolate the option name and find its value.
4100 */
4101 option_end = find_option_end(arg, &opt_flags);
4102 if (option_end == NULL)
4103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 EMSG2(_("E112: Option name missing: %s"), *arg);
4106 return FAIL;
4107 }
4108
4109 if (!evaluate)
4110 {
4111 *arg = option_end;
4112 return OK;
4113 }
4114
4115 c = *option_end;
4116 *option_end = NUL;
4117 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 if (opt_type == -3) /* invalid name */
4121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 EMSG2(_("E113: Unknown option: %s"), *arg);
4124 ret = FAIL;
4125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
4128 if (opt_type == -2) /* hidden string option */
4129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 rettv->v_type = VAR_STRING;
4131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 else if (opt_type == -1) /* hidden number option */
4134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 rettv->v_type = VAR_NUMBER;
4136 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 else if (opt_type == 1) /* number option */
4139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 rettv->v_type = VAR_NUMBER;
4141 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else /* string option */
4144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 rettv->v_type = VAR_STRING;
4146 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148 }
4149 else if (working && (opt_type == -2 || opt_type == -1))
4150 ret = FAIL;
4151
4152 *option_end = c; /* put back for error messages */
4153 *arg = option_end;
4154
4155 return ret;
4156}
4157
4158/*
4159 * Allocate a variable for a string constant.
4160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
4168 char_u *p;
4169 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 int extra = 0;
4171
4172 /*
4173 * Find the end of the string, skipping backslashed characters.
4174 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004175 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 if (*p == '\\' && p[1] != NUL)
4178 {
4179 ++p;
4180 /* A "\<x>" form occupies at least 4 characters, and produces up
4181 * to 6 characters: reserve space for 2 extra */
4182 if (*p == '<')
4183 extra += 2;
4184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186
4187 if (*p != '"')
4188 {
4189 EMSG2(_("E114: Missing quote: %s"), *arg);
4190 return FAIL;
4191 }
4192
4193 /* If only parsing, set *arg and return here */
4194 if (!evaluate)
4195 {
4196 *arg = p + 1;
4197 return OK;
4198 }
4199
4200 /*
4201 * Copy the string into allocated memory, handling backslashed
4202 * characters.
4203 */
4204 name = alloc((unsigned)(p - *arg + extra));
4205 if (name == NULL)
4206 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207 rettv->v_type = VAR_STRING;
4208 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 if (*p == '\\')
4213 {
4214 switch (*++p)
4215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004216 case 'b': *name++ = BS; ++p; break;
4217 case 'e': *name++ = ESC; ++p; break;
4218 case 'f': *name++ = FF; ++p; break;
4219 case 'n': *name++ = NL; ++p; break;
4220 case 'r': *name++ = CAR; ++p; break;
4221 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 case 'X': /* hex: "\x1", "\x12" */
4224 case 'x':
4225 case 'u': /* Unicode: "\u0023" */
4226 case 'U':
4227 if (vim_isxdigit(p[1]))
4228 {
4229 int n, nr;
4230 int c = toupper(*p);
4231
4232 if (c == 'X')
4233 n = 2;
4234 else
4235 n = 4;
4236 nr = 0;
4237 while (--n >= 0 && vim_isxdigit(p[1]))
4238 {
4239 ++p;
4240 nr = (nr << 4) + hex2nr(*p);
4241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004242 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#ifdef FEAT_MBYTE
4244 /* For "\u" store the number according to
4245 * 'encoding'. */
4246 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004247 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 else
4249#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004250 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 break;
4253
4254 /* octal: "\1", "\12", "\123" */
4255 case '0':
4256 case '1':
4257 case '2':
4258 case '3':
4259 case '4':
4260 case '5':
4261 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 case '7': *name = *p++ - '0';
4263 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004265 *name = (*name << 3) + *p++ - '0';
4266 if (*p >= '0' && *p <= '7')
4267 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004269 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 break;
4271
4272 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004273 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (extra != 0)
4275 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004276 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 break;
4278 }
4279 /* FALLTHROUGH */
4280
Bram Moolenaar8c711452005-01-14 21:53:12 +00004281 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 break;
4283 }
4284 }
4285 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004286 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004289 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 *arg = p + 1;
4291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return OK;
4293}
4294
4295/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004296 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 * Return OK or FAIL.
4298 */
4299 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int evaluate;
4304{
4305 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004306 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004307 int reduce = 0;
4308
4309 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004311 */
4312 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4313 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004315 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004316 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004317 break;
4318 ++reduce;
4319 ++p;
4320 }
4321 }
4322
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004326 return FAIL;
4327 }
4328
Bram Moolenaar8c711452005-01-14 21:53:12 +00004329 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004330 if (!evaluate)
4331 {
4332 *arg = p + 1;
4333 return OK;
4334 }
4335
4336 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004337 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004338 */
4339 str = alloc((unsigned)((p - *arg) - reduce));
4340 if (str == NULL)
4341 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004342 rettv->v_type = VAR_STRING;
4343 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004344
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004350 break;
4351 ++p;
4352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004355 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004356 *arg = p + 1;
4357
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004358 return OK;
4359}
4360
4361/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004362 * Allocate a variable for a List and fill it from "*arg".
4363 * Return OK or FAIL.
4364 */
4365 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004367 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004369 int evaluate;
4370{
4371 listvar *l = NULL;
4372 typeval tv;
4373 listitem *item;
4374
4375 if (evaluate)
4376 {
4377 l = list_alloc();
4378 if (l == NULL)
4379 return FAIL;
4380 }
4381
4382 *arg = skipwhite(*arg + 1);
4383 while (**arg != ']' && **arg != NUL)
4384 {
4385 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4386 goto failret;
4387 if (evaluate)
4388 {
4389 item = listitem_alloc();
4390 if (item != NULL)
4391 {
4392 item->li_tv = tv;
4393 list_append(l, item);
4394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004395 else
4396 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004397 }
4398
4399 if (**arg == ']')
4400 break;
4401 if (**arg != ',')
4402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004403 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 goto failret;
4405 }
4406 *arg = skipwhite(*arg + 1);
4407 }
4408
4409 if (**arg != ']')
4410 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004411 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004412failret:
4413 if (evaluate)
4414 list_free(l);
4415 return FAIL;
4416 }
4417
4418 *arg = skipwhite(*arg + 1);
4419 if (evaluate)
4420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004421 rettv->v_type = VAR_LIST;
4422 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 ++l->lv_refcount;
4424 }
4425
4426 return OK;
4427}
4428
4429/*
4430 * Allocate an empty header for a list.
4431 */
4432 static listvar *
4433list_alloc()
4434{
4435 return (listvar *)alloc_clear(sizeof(listvar));
4436}
4437
4438/*
4439 * Unreference a list: decrement the reference count and free it when it
4440 * becomes zero.
4441 */
4442 static void
4443list_unref(l)
4444 listvar *l;
4445{
4446 if (l != NULL && --l->lv_refcount <= 0)
4447 list_free(l);
4448}
4449
4450/*
4451 * Free a list, including all items it points to.
4452 * Ignores the reference count.
4453 */
4454 static void
4455list_free(l)
4456 listvar *l;
4457{
4458 listitem *item;
4459 listitem *next;
4460
4461 for (item = l->lv_first; item != NULL; item = next)
4462 {
4463 next = item->li_next;
4464 listitem_free(item);
4465 }
4466 vim_free(l);
4467}
4468
4469/*
4470 * Allocate a list item.
4471 */
4472 static listitem *
4473listitem_alloc()
4474{
4475 return (listitem *)alloc(sizeof(listitem));
4476}
4477
4478/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004479 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480 */
4481 static void
4482listitem_free(item)
4483 listitem *item;
4484{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004486 vim_free(item);
4487}
4488
4489/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004490 * Remove a list item from a List and free it. Also clears the value.
4491 */
4492 static void
4493listitem_remove(l, item)
4494 listvar *l;
4495 listitem *item;
4496{
4497 list_remove(l, item, item);
4498 listitem_free(item);
4499}
4500
4501/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 * Get the number of items in a list.
4503 */
4504 static long
4505list_len(l)
4506 listvar *l;
4507{
4508 listitem *item;
4509 long len = 0;
4510
4511 if (l == NULL)
4512 return 0L;
4513 for (item = l->lv_first; item != NULL; item = item->li_next)
4514 ++len;
4515 return len;
4516}
4517
4518/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004519 * Return TRUE when two lists have exactly the same values.
4520 */
4521 static int
4522list_equal(l1, l2, ic)
4523 listvar *l1;
4524 listvar *l2;
4525 int ic; /* ignore case for strings */
4526{
4527 listitem *item1, *item2;
4528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 if (list_len(l1) != list_len(l2))
4530 return FALSE;
4531
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004532 for (item1 = l1->lv_first, item2 = l2->lv_first;
4533 item1 != NULL && item2 != NULL;
4534 item1 = item1->li_next, item2 = item2->li_next)
4535 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4536 return FALSE;
4537 return item1 == NULL && item2 == NULL;
4538}
4539
4540/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004541 * Return TRUE when two dictionaries have exactly the same key/values.
4542 */
4543 static int
4544dict_equal(d1, d2, ic)
4545 dictvar *d1;
4546 dictvar *d2;
4547 int ic; /* ignore case for strings */
4548{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004549 hashitem *hi;
4550 dictitem *item2;
4551 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004552
4553 if (dict_len(d1) != dict_len(d2))
4554 return FALSE;
4555
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004556 todo = d1->dv_hashtable.ht_used;
4557 for (hi = d1->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004558 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004559 if (!HASHITEM_EMPTY(hi))
4560 {
4561 item2 = dict_find(d2, hi->hi_key, -1);
4562 if (item2 == NULL)
4563 return FALSE;
4564 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4565 return FALSE;
4566 --todo;
4567 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004568 }
4569 return TRUE;
4570}
4571
4572/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 * Return TRUE if "tv1" and "tv2" have the same value.
4574 * Compares the items just like "==" would compare them.
4575 */
4576 static int
4577tv_equal(tv1, tv2, ic)
4578 typeval *tv1;
4579 typeval *tv2;
4580 int ic; /* ignore case */
4581{
4582 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4583
4584 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4585 {
4586 /* recursive! */
4587 if (tv1->v_type != tv2->v_type
4588 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4589 return FALSE;
4590 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004591 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4592 {
4593 /* recursive! */
4594 if (tv1->v_type != tv2->v_type
4595 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4596 return FALSE;
4597 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004598 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4599 {
4600 if (tv1->v_type != tv2->v_type
4601 || tv1->vval.v_string == NULL
4602 || tv2->vval.v_string == NULL
4603 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4604 return FALSE;
4605 }
4606 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4607 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004608 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4609 * Don't consider "4x" to be equal to 4. */
4610 if ((tv1->v_type == VAR_STRING
4611 && !string_isa_number(tv1->vval.v_string))
4612 || (tv2->v_type == VAR_STRING
4613 && !string_isa_number(tv2->vval.v_string)))
4614 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004615 if (get_tv_number(tv1) != get_tv_number(tv2))
4616 return FALSE;
4617 }
4618 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4619 get_tv_string_buf(tv2, buf2)) != 0)
4620 return FALSE;
4621 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4622 get_tv_string_buf(tv2, buf2)) != 0)
4623 return FALSE;
4624 return TRUE;
4625}
4626
4627/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004628 * Return TRUE if "tv" is a number without other non-white characters.
4629 */
4630 static int
4631string_isa_number(s)
4632 char_u *s;
4633{
4634 int len;
4635
4636 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4637 return len > 0 && *skipwhite(s + len) == NUL;
4638}
4639
4640/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 * Locate item with index "n" in list "l" and return it.
4642 * A negative index is counted from the end; -1 is the last item.
4643 * Returns NULL when "n" is out of range.
4644 */
4645 static listitem *
4646list_find(l, n)
4647 listvar *l;
4648 long n;
4649{
4650 listitem *item;
4651 long idx;
4652
4653 if (l == NULL)
4654 return NULL;
4655 if (n < 0)
4656 {
4657 idx = -1; /* search from the end */
4658 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4659 --idx;
4660 }
4661 else
4662 {
4663 idx = 0; /* search from the start */
4664 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4665 ++idx;
4666 }
4667 if (idx != n)
4668 return NULL;
4669 return item;
4670}
4671
4672/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004673 * Locate "item" list "l" and return its index.
4674 * Returns -1 when "item" is not in the list.
4675 */
4676 static long
4677list_idx_of_item(l, item)
4678 listvar *l;
4679 listitem *item;
4680{
4681 long idx = 0;
4682 listitem *li;
4683
4684 if (l == NULL)
4685 return -1;
4686 idx = 0;
4687 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4688 ++idx;
4689 if (li == NULL)
4690 return -1;
4691 return idx;;
4692}
4693
4694/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004695 * Like list_find(), but also find an item just past the end.
4696 * "*ip" is the item to find.
4697 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4698 * Returns NULL when item not found or item is just past the end.
4699 */
4700 static listitem *
4701list_find_ext(l, ip)
4702 listvar *l;
4703 long *ip;
4704{
4705 long n;
4706 listitem *item;
4707
4708 if (*ip < 0)
4709 {
4710 /* Count from the end: -1 is before last item. */
4711 item = l->lv_last;
4712 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4713 item = item->li_prev;
4714 if (item == NULL)
4715 n = 1; /* error! */
4716 }
4717 else
4718 {
4719 item = l->lv_first;
4720 for (n = *ip; n > 0 && item != NULL; --n)
4721 item = item->li_next;
4722 }
4723 *ip = n;
4724 return item;
4725}
4726
4727/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 * Append item "item" to the end of list "l".
4729 */
4730 static void
4731list_append(l, item)
4732 listvar *l;
4733 listitem *item;
4734{
4735 if (l->lv_last == NULL)
4736 {
4737 /* empty list */
4738 l->lv_first = item;
4739 l->lv_last = item;
4740 item->li_prev = NULL;
4741 }
4742 else
4743 {
4744 l->lv_last->li_next = item;
4745 item->li_prev = l->lv_last;
4746 l->lv_last = item;
4747 }
4748 item->li_next = NULL;
4749}
4750
4751/*
4752 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004753 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 */
4755 static int
4756list_append_tv(l, tv)
4757 listvar *l;
4758 typeval *tv;
4759{
4760 listitem *ni = listitem_alloc();
4761
4762 if (ni == NULL)
4763 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 list_append(l, ni);
4766 return OK;
4767}
4768
4769/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 * Insert typeval "tv" in list "l" before "item".
4771 * If "item" is NULL append at the end.
4772 * Return FAIL when out of memory.
4773 */
4774 static int
4775list_insert_tv(l, tv, item)
4776 listvar *l;
4777 typeval *tv;
4778 listitem *item;
4779{
4780 listitem *ni = listitem_alloc();
4781
4782 if (ni == NULL)
4783 return FAIL;
4784 copy_tv(tv, &ni->li_tv);
4785 if (item == NULL)
4786 /* Append new item at end of list. */
4787 list_append(l, ni);
4788 else
4789 {
4790 /* Insert new item before existing item. */
4791 ni->li_prev = item->li_prev;
4792 ni->li_next = item;
4793 if (item->li_prev == NULL)
4794 l->lv_first = ni;
4795 else
4796 item->li_prev->li_next = ni;
4797 item->li_prev = ni;
4798 }
4799 return OK;
4800}
4801
4802/*
4803 * Extend "l1" with "l2".
4804 * If "bef" is NULL append at the end, otherwise insert before this item.
4805 * Returns FAIL when out of memory.
4806 */
4807 static int
4808list_extend(l1, l2, bef)
4809 listvar *l1;
4810 listvar *l2;
4811 listitem *bef;
4812{
4813 listitem *item;
4814
4815 for (item = l2->lv_first; item != NULL; item = item->li_next)
4816 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4817 return FAIL;
4818 return OK;
4819}
4820
4821/*
4822 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4823 * Return FAIL when out of memory.
4824 */
4825 static int
4826list_concat(l1, l2, tv)
4827 listvar *l1;
4828 listvar *l2;
4829 typeval *tv;
4830{
4831 listvar *l;
4832
4833 /* make a copy of the first list. */
4834 l = list_copy(l1, FALSE);
4835 if (l == NULL)
4836 return FAIL;
4837 tv->v_type = VAR_LIST;
4838 tv->vval.v_list = l;
4839
4840 /* append all items from the second list */
4841 return list_extend(l, l2, NULL);
4842}
4843
4844/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004845 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 * The refcount of the new list is set to 1.
4847 * Returns NULL when out of memory.
4848 */
4849 static listvar *
4850list_copy(orig, deep)
4851 listvar *orig;
4852 int deep;
4853{
4854 listvar *copy;
4855 listitem *item;
4856 listitem *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004857
4858 if (orig == NULL)
4859 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860
4861 copy = list_alloc();
4862 if (copy != NULL)
4863 {
4864 for (item = orig->lv_first; item != NULL; item = item->li_next)
4865 {
4866 ni = listitem_alloc();
4867 if (ni == NULL)
4868 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004869 if (deep)
4870 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 list_append(copy, ni);
4874 }
4875 ++copy->lv_refcount;
4876 }
4877
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 return copy;
4879}
4880
4881/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004882 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004883 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004885 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004886list_remove(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004889 listitem *item2;
4890{
4891 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004893 /* notify watchers */
4894 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004895 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004896 list_fix_watch(l, ip);
4897 if (ip == item2)
4898 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004900
4901 if (item2->li_next == NULL)
4902 l->lv_last = item->li_prev;
4903 else
4904 item2->li_next->li_prev = item->li_prev;
4905 if (item->li_prev == NULL)
4906 l->lv_first = item2->li_next;
4907 else
4908 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909}
4910
4911/*
4912 * Return an allocated string with the string representation of a list.
4913 * May return NULL.
4914 */
4915 static char_u *
4916list2string(tv)
4917 typeval *tv;
4918{
4919 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004920
4921 if (tv->vval.v_list == NULL)
4922 return NULL;
4923 ga_init2(&ga, (int)sizeof(char), 80);
4924 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004925 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004926 ga_append(&ga, ']');
4927 ga_append(&ga, NUL);
4928 return (char_u *)ga.ga_data;
4929}
4930
4931/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004932 * Join list "l" into a string in "*gap", using separator "sep".
4933 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4934 */
4935 static void
4936list_join(gap, l, sep, echo)
4937 garray_T *gap;
4938 listvar *l;
4939 char_u *sep;
4940 int echo;
4941{
4942 int first = TRUE;
4943 char_u *tofree;
4944 char_u numbuf[NUMBUFLEN];
4945 listitem *item;
4946 char_u *s;
4947
4948 for (item = l->lv_first; item != NULL; item = item->li_next)
4949 {
4950 if (first)
4951 first = FALSE;
4952 else
4953 ga_concat(gap, sep);
4954
4955 if (echo)
4956 s = echo_string(&item->li_tv, &tofree, numbuf);
4957 else
4958 s = tv2string(&item->li_tv, &tofree, numbuf);
4959 if (s != NULL)
4960 ga_concat(gap, s);
4961 vim_free(tofree);
4962 }
4963}
4964
4965/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 * Allocate an empty header for a dictionary.
4967 */
4968 static dictvar *
4969dict_alloc()
4970{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004971 dictvar *d;
4972
4973 d = (dictvar *)alloc(sizeof(dictvar));
4974 if (d != NULL)
4975 hash_init(&d->dv_hashtable);
4976 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977}
4978
4979/*
4980 * Unreference a Dictionary: decrement the reference count and free it when it
4981 * becomes zero.
4982 */
4983 static void
4984dict_unref(d)
4985 dictvar *d;
4986{
4987 if (d != NULL && --d->dv_refcount <= 0)
4988 dict_free(d);
4989}
4990
4991/*
4992 * Free a Dictionary, including all items it contains.
4993 * Ignores the reference count.
4994 */
4995 static void
4996dict_free(d)
4997 dictvar *d;
4998{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004999 int todo;
5000 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005002 /* Careful: we free the dictitems while they still appear in the
5003 * hashtable. Must not try to resize the hashtable! */
5004 todo = d->dv_hashtable.ht_used;
5005 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005007 if (!HASHITEM_EMPTY(hi))
5008 {
5009 dictitem_free(HI2DI(hi));
5010 --todo;
5011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012 }
5013 vim_free(d);
5014}
5015
5016/*
5017 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005018 * The "key" is copied to the new item.
5019 * Note that the value of the item "di_tv" still needs to be initialized!
5020 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 */
5022 static dictitem *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005023dictitem_alloc(key)
5024 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005026 dictitem *di;
5027
5028 di = (dictitem *)alloc(sizeof(dictitem) + STRLEN(key));
5029 if (di != NULL)
5030 STRCPY(di->di_key, key);
5031 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032}
5033
5034/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005035 * Make a copy of a Dictionary item.
5036 */
5037 static dictitem *
5038dictitem_copy(org)
5039 dictitem *org;
5040{
5041 dictitem *di;
5042
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005043 di = (dictitem *)alloc(sizeof(dictitem) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005044 if (di != NULL)
5045 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005046 STRCPY(di->di_key, org->di_key);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005047 copy_tv(&org->di_tv, &di->di_tv);
5048 }
5049 return di;
5050}
5051
5052/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005053 * Remove item "item" from Dictionary "dict" and free it.
5054 */
5055 static void
5056dictitem_remove(dict, item)
5057 dictvar *dict;
5058 dictitem *item;
5059{
5060 hashitem *hi;
5061
5062 hi = hash_find(&dict->dv_hashtable, item->di_key);
5063 if (HASHITEM_EMPTY(hi))
5064 EMSG2(_(e_intern2), "dictitem_remove()");
5065 else
5066 hash_remove(&dict->dv_hashtable, hi);
5067 dictitem_free(item);
5068}
5069
5070/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * Free a dict item. Also clears the value.
5072 */
5073 static void
5074dictitem_free(item)
5075 dictitem *item;
5076{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 clear_tv(&item->di_tv);
5078 vim_free(item);
5079}
5080
5081/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005082 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5083 * The refcount of the new dict is set to 1.
5084 * Returns NULL when out of memory.
5085 */
5086 static dictvar *
5087dict_copy(orig, deep)
5088 dictvar *orig;
5089 int deep;
5090{
5091 dictvar *copy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005092 dictitem *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005093 int todo;
5094 hashitem *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095
5096 if (orig == NULL)
5097 return NULL;
5098
5099 copy = dict_alloc();
5100 if (copy != NULL)
5101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005102 todo = orig->dv_hashtable.ht_used;
5103 for (hi = orig->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005105 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005107 --todo;
5108
5109 di = dictitem_alloc(hi->hi_key);
5110 if (di == NULL)
5111 break;
5112 if (deep)
5113 item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep);
5114 else
5115 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5116 if (dict_add(copy, di) == FAIL)
5117 {
5118 dictitem_free(di);
5119 break;
5120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005122 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005123
Bram Moolenaare9a41262005-01-15 22:18:47 +00005124 ++copy->dv_refcount;
5125 }
5126
5127 return copy;
5128}
5129
5130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005132 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005134 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135dict_add(d, item)
5136 dictvar *d;
5137 dictitem *item;
5138{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005139 return hash_add(&d->dv_hashtable, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140}
5141
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005143 * Get the number of items in a Dictionary.
5144 */
5145 static long
5146dict_len(d)
5147 dictvar *d;
5148{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005149 if (d == NULL)
5150 return 0L;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005151 return d->dv_hashtable.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005152}
5153
5154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Find item "key[len]" in Dictionary "d".
5156 * If "len" is negative use strlen(key).
5157 * Returns NULL when not found.
5158 */
5159 static dictitem *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005160dict_find(d, key, len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 dictvar *d;
5162 char_u *key;
5163 int len;
5164{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005165#define AKEYLEN 200
5166 char_u buf[AKEYLEN];
5167 char_u *akey;
5168 char_u *tofree = NULL;
5169 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005171 if (len < 0)
5172 akey = key;
5173 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005174 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005175 tofree = akey = vim_strnsave(key, len);
5176 if (akey == NULL)
5177 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005178 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005179 else
5180 {
5181 /* Avoid a malloc/free by using buf[]. */
5182 STRNCPY(buf, key, len);
5183 buf[len] = NUL;
5184 akey = buf;
5185 }
5186
5187 hi = hash_find(&d->dv_hashtable, akey);
5188 vim_free(tofree);
5189 if (HASHITEM_EMPTY(hi))
5190 return NULL;
5191 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192}
5193
5194/*
5195 * Return an allocated string with the string representation of a Dictionary.
5196 * May return NULL.
5197 */
5198 static char_u *
5199dict2string(tv)
5200 typeval *tv;
5201{
5202 garray_T ga;
5203 int first = TRUE;
5204 char_u *tofree;
5205 char_u numbuf[NUMBUFLEN];
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005206 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 char_u *s;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005208 dictvar *d;
5209 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005211 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 return NULL;
5213 ga_init2(&ga, (int)sizeof(char), 80);
5214 ga_append(&ga, '{');
5215
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005216 todo = d->dv_hashtable.ht_used;
5217 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005219 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005221 --todo;
5222
5223 if (first)
5224 first = FALSE;
5225 else
5226 ga_concat(&ga, (char_u *)", ");
5227
5228 tofree = string_quote(hi->hi_key, FALSE);
5229 if (tofree != NULL)
5230 {
5231 ga_concat(&ga, tofree);
5232 vim_free(tofree);
5233 }
5234 ga_concat(&ga, (char_u *)": ");
5235 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5236 if (s != NULL)
5237 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 vim_free(tofree);
5239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 }
5241
5242 ga_append(&ga, '}');
5243 ga_append(&ga, NUL);
5244 return (char_u *)ga.ga_data;
5245}
5246
5247/*
5248 * Allocate a variable for a Dictionary and fill it from "*arg".
5249 * Return OK or FAIL. Returns NOTDONE for {expr}.
5250 */
5251 static int
5252get_dict_tv(arg, rettv, evaluate)
5253 char_u **arg;
5254 typeval *rettv;
5255 int evaluate;
5256{
5257 dictvar *d = NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005258 typeval tvkey;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 typeval tv;
5260 char_u *key;
5261 dictitem *item;
5262 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005263 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264
5265 /*
5266 * First check if it's not a curly-braces thing: {expr}.
5267 * Must do this without evaluating, otherwise a function may be called
5268 * twice. Unfortunately this means we need to call eval1() twice for the
5269 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005270 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 if (*start != '}')
5273 {
5274 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5275 return FAIL;
5276 if (*start == '}')
5277 return NOTDONE;
5278 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279
5280 if (evaluate)
5281 {
5282 d = dict_alloc();
5283 if (d == NULL)
5284 return FAIL;
5285 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005286 tvkey.v_type = VAR_UNKNOWN;
5287 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288
5289 *arg = skipwhite(*arg + 1);
5290 while (**arg != '}' && **arg != NUL)
5291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005292 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 goto failret;
5294 if (**arg != ':')
5295 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005296 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005297 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 goto failret;
5299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005300 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (*key == NUL)
5302 {
5303 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005304 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 goto failret;
5306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307
5308 *arg = skipwhite(*arg + 1);
5309 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005311 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 goto failret;
5313 }
5314 if (evaluate)
5315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005316 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 if (item != NULL)
5318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005319 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005320 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 clear_tv(&tv);
5322 goto failret;
5323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005324 item = dictitem_alloc(key);
5325 clear_tv(&tvkey);
5326 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 item->di_tv = tv;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005329 if (dict_add(d, item) == FAIL)
5330 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 }
5332 }
5333
5334 if (**arg == '}')
5335 break;
5336 if (**arg != ',')
5337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005338 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 goto failret;
5340 }
5341 *arg = skipwhite(*arg + 1);
5342 }
5343
5344 if (**arg != '}')
5345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005346 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347failret:
5348 if (evaluate)
5349 dict_free(d);
5350 return FAIL;
5351 }
5352
5353 *arg = skipwhite(*arg + 1);
5354 if (evaluate)
5355 {
5356 rettv->v_type = VAR_DICT;
5357 rettv->vval.v_dict = d;
5358 ++d->dv_refcount;
5359 }
5360
5361 return OK;
5362}
5363
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 * Return a string with the string representation of a variable.
5366 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005367 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005368 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 * May return NULL;
5370 */
5371 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005372echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 typeval *tv;
5374 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005375 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005377 static int recurse = 0;
5378 char_u *r = NULL;
5379
5380 if (recurse >= VAR_MAXNEST)
5381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005382 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005383 *tofree = NULL;
5384 return NULL;
5385 }
5386 ++recurse;
5387
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 switch (tv->v_type)
5389 {
5390 case VAR_FUNC:
5391 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005392 r = tv->vval.v_string;
5393 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 case VAR_LIST:
5395 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005396 r = *tofree;
5397 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 case VAR_DICT:
5399 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005400 r = *tofree;
5401 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005402 case VAR_STRING:
5403 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005404 *tofree = NULL;
5405 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005408 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005409 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005411
5412 --recurse;
5413 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005414}
5415
5416/*
5417 * Return a string with the string representation of a variable.
5418 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5419 * "numbuf" is used for a number.
5420 * Puts quotes around strings, so that they can be parsed back by eval().
5421 * May return NULL;
5422 */
5423 static char_u *
5424tv2string(tv, tofree, numbuf)
5425 typeval *tv;
5426 char_u **tofree;
5427 char_u *numbuf;
5428{
5429 switch (tv->v_type)
5430 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005431 case VAR_FUNC:
5432 *tofree = string_quote(tv->vval.v_string, TRUE);
5433 return *tofree;
5434 case VAR_STRING:
5435 *tofree = string_quote(tv->vval.v_string, FALSE);
5436 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005437 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005438 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005440 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005441 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005442 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005444 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445}
5446
5447/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 * Return a string in ' quotes, doubling ' characters.
5449 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005450 */
5451 static char_u *
5452string_quote(str, function)
5453 char_u *str;
5454 int function;
5455{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005456 unsigned len = STRLEN(str) + (function ? 13 : 3);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005457 char_u *p, *r, *s;
5458
5459 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005461 ++len;
5462 s = r = alloc(len);
5463 if (r != NULL)
5464 {
5465 if (function)
5466 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005468 r += 10;
5469 }
5470 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005471 *r++ = '\'';
5472 for (p = str; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005473 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474 if (*p == '\'')
5475 *r++ = '\'';
5476 MB_COPY_CHAR(p, r);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005479 if (function)
5480 *r++ = ')';
5481 *r++ = NUL;
5482 }
5483 return s;
5484}
5485
5486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 * Get the value of an environment variable.
5488 * "arg" is pointing to the '$'. It is advanced to after the name.
5489 * If the environment variable was not set, silently assume it is empty.
5490 * Always return OK.
5491 */
5492 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int evaluate;
5497{
5498 char_u *string = NULL;
5499 int len;
5500 int cc;
5501 char_u *name;
5502
5503 ++*arg;
5504 name = *arg;
5505 len = get_env_len(arg);
5506 if (evaluate)
5507 {
5508 if (len != 0)
5509 {
5510 cc = name[len];
5511 name[len] = NUL;
5512 /* first try mch_getenv(), fast for normal environment vars */
5513 string = mch_getenv(name);
5514 if (string != NULL && *string != NUL)
5515 string = vim_strsave(string);
5516 else
5517 {
5518 /* next try expanding things like $VIM and ${HOME} */
5519 string = expand_env_save(name - 1);
5520 if (string != NULL && *string == '$')
5521 {
5522 vim_free(string);
5523 string = NULL;
5524 }
5525 }
5526 name[len] = cc;
5527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 rettv->v_type = VAR_STRING;
5529 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531
5532 return OK;
5533}
5534
5535/*
5536 * Array with names and number of arguments of all internal functions
5537 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5538 */
5539static struct fst
5540{
5541 char *f_name; /* function name */
5542 char f_min_argc; /* minimal number of arguments */
5543 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 void (*f_func) __ARGS((typeval *args, typeval *rvar));
5545 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546} functions[] =
5547{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005548 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {"append", 2, 2, f_append},
5550 {"argc", 0, 0, f_argc},
5551 {"argidx", 0, 0, f_argidx},
5552 {"argv", 1, 1, f_argv},
5553 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005554 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {"bufexists", 1, 1, f_bufexists},
5556 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5557 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5558 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5559 {"buflisted", 1, 1, f_buflisted},
5560 {"bufloaded", 1, 1, f_bufloaded},
5561 {"bufname", 1, 1, f_bufname},
5562 {"bufnr", 1, 1, f_bufnr},
5563 {"bufwinnr", 1, 1, f_bufwinnr},
5564 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005565 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005566 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {"char2nr", 1, 1, f_char2nr},
5568 {"cindent", 1, 1, f_cindent},
5569 {"col", 1, 1, f_col},
5570 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005572 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {"cscope_connection",0,3, f_cscope_connection},
5574 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {"delete", 1, 1, f_delete},
5577 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005578 {"diff_filler", 1, 1, f_diff_filler},
5579 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005580 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005582 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {"eventhandler", 0, 0, f_eventhandler},
5584 {"executable", 1, 1, f_executable},
5585 {"exists", 1, 1, f_exists},
5586 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005587 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5589 {"filereadable", 1, 1, f_filereadable},
5590 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005591 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005592 {"finddir", 1, 3, f_finddir},
5593 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {"fnamemodify", 2, 2, f_fnamemodify},
5595 {"foldclosed", 1, 1, f_foldclosed},
5596 {"foldclosedend", 1, 1, f_foldclosedend},
5597 {"foldlevel", 1, 1, f_foldlevel},
5598 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005599 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005602 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {"getbufvar", 2, 2, f_getbufvar},
5604 {"getchar", 0, 1, f_getchar},
5605 {"getcharmod", 0, 0, f_getcharmod},
5606 {"getcmdline", 0, 0, f_getcmdline},
5607 {"getcmdpos", 0, 0, f_getcmdpos},
5608 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005609 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005610 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {"getfsize", 1, 1, f_getfsize},
5612 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005613 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005614 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {"getreg", 0, 1, f_getreg},
5616 {"getregtype", 0, 1, f_getregtype},
5617 {"getwinposx", 0, 0, f_getwinposx},
5618 {"getwinposy", 0, 0, f_getwinposy},
5619 {"getwinvar", 2, 2, f_getwinvar},
5620 {"glob", 1, 1, f_glob},
5621 {"globpath", 2, 2, f_globpath},
5622 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005623 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {"hasmapto", 1, 2, f_hasmapto},
5625 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5626 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5627 {"histadd", 2, 2, f_histadd},
5628 {"histdel", 1, 2, f_histdel},
5629 {"histget", 1, 2, f_histget},
5630 {"histnr", 1, 1, f_histnr},
5631 {"hlID", 1, 1, f_hlID},
5632 {"hlexists", 1, 1, f_hlexists},
5633 {"hostname", 0, 0, f_hostname},
5634 {"iconv", 3, 3, f_iconv},
5635 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {"input", 1, 2, f_input},
5638 {"inputdialog", 1, 3, f_inputdialog},
5639 {"inputrestore", 0, 0, f_inputrestore},
5640 {"inputsave", 0, 0, f_inputsave},
5641 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005645 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {"libcall", 3, 3, f_libcall},
5650 {"libcallnr", 3, 3, f_libcallnr},
5651 {"line", 1, 1, f_line},
5652 {"line2byte", 1, 1, f_line2byte},
5653 {"lispindent", 1, 1, f_lispindent},
5654 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {"maparg", 1, 2, f_maparg},
5657 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005658 {"match", 2, 4, f_match},
5659 {"matchend", 2, 4, f_matchend},
5660 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005661 {"max", 1, 1, f_max},
5662 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {"mode", 0, 0, f_mode},
5664 {"nextnonblank", 1, 1, f_nextnonblank},
5665 {"nr2char", 1, 1, f_nr2char},
5666 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {"remote_expr", 2, 3, f_remote_expr},
5669 {"remote_foreground", 1, 1, f_remote_foreground},
5670 {"remote_peek", 1, 2, f_remote_peek},
5671 {"remote_read", 1, 1, f_remote_read},
5672 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005675 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005677 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {"search", 1, 2, f_search},
5679 {"searchpair", 3, 5, f_searchpair},
5680 {"server2client", 2, 2, f_server2client},
5681 {"serverlist", 0, 0, f_serverlist},
5682 {"setbufvar", 3, 3, f_setbufvar},
5683 {"setcmdpos", 1, 1, f_setcmdpos},
5684 {"setline", 2, 2, f_setline},
5685 {"setreg", 2, 3, f_setreg},
5686 {"setwinvar", 3, 3, f_setwinvar},
5687 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005688 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#ifdef HAVE_STRFTIME
5691 {"strftime", 1, 2, f_strftime},
5692#endif
5693 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {"strlen", 1, 1, f_strlen},
5696 {"strpart", 2, 3, f_strpart},
5697 {"strridx", 2, 2, f_strridx},
5698 {"strtrans", 1, 1, f_strtrans},
5699 {"submatch", 1, 1, f_submatch},
5700 {"substitute", 4, 4, f_substitute},
5701 {"synID", 3, 3, f_synID},
5702 {"synIDattr", 2, 3, f_synIDattr},
5703 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005704 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {"tempname", 0, 0, f_tempname},
5706 {"tolower", 1, 1, f_tolower},
5707 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005708 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {"virtcol", 1, 1, f_virtcol},
5712 {"visualmode", 0, 1, f_visualmode},
5713 {"winbufnr", 1, 1, f_winbufnr},
5714 {"wincol", 0, 0, f_wincol},
5715 {"winheight", 1, 1, f_winheight},
5716 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005717 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {"winrestcmd", 0, 0, f_winrestcmd},
5719 {"winwidth", 1, 1, f_winwidth},
5720};
5721
5722#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5723
5724/*
5725 * Function given to ExpandGeneric() to obtain the list of internal
5726 * or user defined function names.
5727 */
5728 char_u *
5729get_function_name(xp, idx)
5730 expand_T *xp;
5731 int idx;
5732{
5733 static int intidx = -1;
5734 char_u *name;
5735
5736 if (idx == 0)
5737 intidx = -1;
5738 if (intidx < 0)
5739 {
5740 name = get_user_func_name(xp, idx);
5741 if (name != NULL)
5742 return name;
5743 }
5744 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5745 {
5746 STRCPY(IObuff, functions[intidx].f_name);
5747 STRCAT(IObuff, "(");
5748 if (functions[intidx].f_max_argc == 0)
5749 STRCAT(IObuff, ")");
5750 return IObuff;
5751 }
5752
5753 return NULL;
5754}
5755
5756/*
5757 * Function given to ExpandGeneric() to obtain the list of internal or
5758 * user defined variable or function names.
5759 */
5760/*ARGSUSED*/
5761 char_u *
5762get_expr_name(xp, idx)
5763 expand_T *xp;
5764 int idx;
5765{
5766 static int intidx = -1;
5767 char_u *name;
5768
5769 if (idx == 0)
5770 intidx = -1;
5771 if (intidx < 0)
5772 {
5773 name = get_function_name(xp, idx);
5774 if (name != NULL)
5775 return name;
5776 }
5777 return get_user_var_name(xp, ++intidx);
5778}
5779
5780#endif /* FEAT_CMDL_COMPL */
5781
5782/*
5783 * Find internal function in table above.
5784 * Return index, or -1 if not found
5785 */
5786 static int
5787find_internal_func(name)
5788 char_u *name; /* name of the function */
5789{
5790 int first = 0;
5791 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5792 int cmp;
5793 int x;
5794
5795 /*
5796 * Find the function name in the table. Binary search.
5797 */
5798 while (first <= last)
5799 {
5800 x = first + ((unsigned)(last - first) >> 1);
5801 cmp = STRCMP(name, functions[x].f_name);
5802 if (cmp < 0)
5803 last = x - 1;
5804 else if (cmp > 0)
5805 first = x + 1;
5806 else
5807 return x;
5808 }
5809 return -1;
5810}
5811
5812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5814 * name it contains, otherwise return "name".
5815 */
5816 static char_u *
5817deref_func_name(name, lenp)
5818 char_u *name;
5819 int *lenp;
5820{
5821 VAR v;
5822 int cc;
5823
5824 cc = name[*lenp];
5825 name[*lenp] = NUL;
5826 v = find_var(name, FALSE);
5827 name[*lenp] = cc;
5828 if (v != NULL && v->tv.v_type == VAR_FUNC)
5829 {
5830 if (v->tv.vval.v_string == NULL)
5831 {
5832 *lenp = 0;
5833 return (char_u *)""; /* just in case */
5834 }
5835 *lenp = STRLEN(v->tv.vval.v_string);
5836 return v->tv.vval.v_string;
5837 }
5838
5839 return name;
5840}
5841
5842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 * Allocate a variable for the result of a function.
5844 * Return OK or FAIL.
5845 */
5846 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005847get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5848 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 char_u *name; /* name of the function */
5850 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005851 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 char_u **arg; /* argument, pointing to the '(' */
5853 linenr_T firstline; /* first line of range */
5854 linenr_T lastline; /* last line of range */
5855 int *doesrange; /* return: function handled range */
5856 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 char_u *argp;
5860 int ret = OK;
5861#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 int argcount = 0; /* number of arguments found */
5864
5865 /*
5866 * Get the arguments.
5867 */
5868 argp = *arg;
5869 while (argcount < MAX_FUNC_ARGS)
5870 {
5871 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5872 if (*argp == ')' || *argp == ',' || *argp == NUL)
5873 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5875 {
5876 ret = FAIL;
5877 break;
5878 }
5879 ++argcount;
5880 if (*argp != ',')
5881 break;
5882 }
5883 if (*argp == ')')
5884 ++argp;
5885 else
5886 ret = FAIL;
5887
5888 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005889 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 else if (!aborting())
5892 EMSG2(_("E116: Invalid arguments for function %s"), name);
5893
5894 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005895 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 *arg = skipwhite(argp);
5898 return ret;
5899}
5900
5901
5902/*
5903 * Call a function with its resolved parameters
5904 * Return OK or FAIL.
5905 */
5906 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005908 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 char_u *name; /* name of the function */
5910 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005911 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 linenr_T firstline; /* first line of range */
5915 linenr_T lastline; /* last line of range */
5916 int *doesrange; /* return: function handled range */
5917 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005918 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#define ERROR_UNKNOWN 0
5922#define ERROR_TOOMANY 1
5923#define ERROR_TOOFEW 2
5924#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925#define ERROR_DICT 4
5926#define ERROR_NONE 5
5927#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 int error = ERROR_NONE;
5929 int i;
5930 int llen;
5931 ufunc_T *fp;
5932 int cc;
5933#define FLEN_FIXED 40
5934 char_u fname_buf[FLEN_FIXED + 1];
5935 char_u *fname;
5936
5937 /*
5938 * In a script change <SID>name() and s:name() to K_SNR 123_name().
5939 * Change <SNR>123_name() to K_SNR 123_name().
5940 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
5941 */
5942 cc = name[len];
5943 name[len] = NUL;
5944 llen = eval_fname_script(name);
5945 if (llen > 0)
5946 {
5947 fname_buf[0] = K_SPECIAL;
5948 fname_buf[1] = KS_EXTRA;
5949 fname_buf[2] = (int)KE_SNR;
5950 i = 3;
5951 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
5952 {
5953 if (current_SID <= 0)
5954 error = ERROR_SCRIPT;
5955 else
5956 {
5957 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
5958 i = (int)STRLEN(fname_buf);
5959 }
5960 }
5961 if (i + STRLEN(name + llen) < FLEN_FIXED)
5962 {
5963 STRCPY(fname_buf + i, name + llen);
5964 fname = fname_buf;
5965 }
5966 else
5967 {
5968 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
5969 if (fname == NULL)
5970 error = ERROR_OTHER;
5971 else
5972 {
5973 mch_memmove(fname, fname_buf, (size_t)i);
5974 STRCPY(fname + i, name + llen);
5975 }
5976 }
5977 }
5978 else
5979 fname = name;
5980
5981 *doesrange = FALSE;
5982
5983
5984 /* execute the function if no errors detected and executing */
5985 if (evaluate && error == ERROR_NONE)
5986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005987 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 error = ERROR_UNKNOWN;
5989
5990 if (!ASCII_ISLOWER(fname[0]))
5991 {
5992 /*
5993 * User defined function.
5994 */
5995 fp = find_func(fname);
5996#ifdef FEAT_AUTOCMD
5997 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
5998 fname, fname, TRUE, NULL)
5999#ifdef FEAT_EVAL
6000 && !aborting()
6001#endif
6002 )
6003 {
6004 /* executed an autocommand, search for function again */
6005 fp = find_func(fname);
6006 }
6007#endif
6008 if (fp != NULL)
6009 {
6010 if (fp->flags & FC_RANGE)
6011 *doesrange = TRUE;
6012 if (argcount < fp->args.ga_len)
6013 error = ERROR_TOOFEW;
6014 else if (!fp->varargs && argcount > fp->args.ga_len)
6015 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006016 else if ((fp->flags & FC_DICT) && selfdict == NULL)
6017 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 else
6019 {
6020 /*
6021 * Call the user function.
6022 * Save and restore search patterns, script variables and
6023 * redo buffer.
6024 */
6025 save_search_patterns();
6026 saveRedobuff();
6027 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006028 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 firstline, lastline,
6030 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006031 if (--fp->calls <= 0 && isdigit(*fp->name)
6032 && fp->refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 /* Function was unreferenced while being used, free it
6034 * now. */
6035 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 restoreRedobuff();
6037 restore_search_patterns();
6038 error = ERROR_NONE;
6039 }
6040 }
6041 }
6042 else
6043 {
6044 /*
6045 * Find the function name in the table, call its implementation.
6046 */
6047 i = find_internal_func(fname);
6048 if (i >= 0)
6049 {
6050 if (argcount < functions[i].f_min_argc)
6051 error = ERROR_TOOFEW;
6052 else if (argcount > functions[i].f_max_argc)
6053 error = ERROR_TOOMANY;
6054 else
6055 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006057 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 error = ERROR_NONE;
6059 }
6060 }
6061 }
6062 /*
6063 * The function call (or "FuncUndefined" autocommand sequence) might
6064 * have been aborted by an error, an interrupt, or an explicitly thrown
6065 * exception that has not been caught so far. This situation can be
6066 * tested for by calling aborting(). For an error in an internal
6067 * function or for the "E132" error in call_user_func(), however, the
6068 * throw point at which the "force_abort" flag (temporarily reset by
6069 * emsg()) is normally updated has not been reached yet. We need to
6070 * update that flag first to make aborting() reliable.
6071 */
6072 update_force_abort();
6073 }
6074 if (error == ERROR_NONE)
6075 ret = OK;
6076
6077 /*
6078 * Report an error unless the argument evaluation or function call has been
6079 * cancelled due to an aborting error, an interrupt, or an exception.
6080 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006081 if (!aborting())
6082 {
6083 switch (error)
6084 {
6085 case ERROR_UNKNOWN:
6086 EMSG2(_("E117: Unknown function: %s"), name);
6087 break;
6088 case ERROR_TOOMANY:
6089 EMSG2(_(e_toomanyarg), name);
6090 break;
6091 case ERROR_TOOFEW:
6092 EMSG2(_("E119: Not enough arguments for function: %s"),
6093 name);
6094 break;
6095 case ERROR_SCRIPT:
6096 EMSG2(_("E120: Using <SID> not in a script context: %s"),
6097 name);
6098 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 case ERROR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006100 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 name);
6102 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006103 }
6104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 name[len] = cc;
6107 if (fname != name && fname != fname_buf)
6108 vim_free(fname);
6109
6110 return ret;
6111}
6112
6113/*********************************************
6114 * Implementation of the built-in functions
6115 */
6116
6117/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006118 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006121f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006123 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006127 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130 l = argvars[0].vval.v_list;
6131 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006132 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 }
6134 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006135 EMSG(_(e_listreq));
6136}
6137
6138/*
6139 * "append(lnum, string/list)" function
6140 */
6141 static void
6142f_append(argvars, rettv)
6143 typeval *argvars;
6144 typeval *rettv;
6145{
6146 long lnum;
6147 listvar *l = NULL;
6148 listitem *li = NULL;
6149 typeval *tv;
6150 long added = 0;
6151
6152 rettv->vval.v_number = 1; /* Default: Failed */
6153 lnum = get_tv_lnum(argvars);
6154 if (lnum >= 0
6155 && lnum <= curbuf->b_ml.ml_line_count
6156 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006158 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006160 l = argvars[1].vval.v_list;
6161 if (l == NULL)
6162 return;
6163 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006165 for (;;)
6166 {
6167 if (l == NULL)
6168 tv = &argvars[1]; /* append a string */
6169 else if (li == NULL)
6170 break; /* end of list */
6171 else
6172 tv = &li->li_tv; /* append item from list */
6173 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6174 ++added;
6175 if (l == NULL)
6176 break;
6177 li = li->li_next;
6178 }
6179
6180 appended_lines_mark(lnum, added);
6181 if (curwin->w_cursor.lnum > lnum)
6182 curwin->w_cursor.lnum += added;
6183 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185}
6186
6187/*
6188 * "argc()" function
6189 */
6190/* ARGSUSED */
6191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006194 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006196 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197}
6198
6199/*
6200 * "argidx()" function
6201 */
6202/* ARGSUSED */
6203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006204f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006206 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006208 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209}
6210
6211/*
6212 * "argv(nr)" function
6213 */
6214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006215f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006217 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 int idx;
6220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006221 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006223 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006225 rettv->vval.v_string = NULL;
6226 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227}
6228
6229/*
6230 * "browse(save, title, initdir, default)" function
6231 */
6232/* ARGSUSED */
6233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006234f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006235 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006236 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238#ifdef FEAT_BROWSE
6239 int save;
6240 char_u *title;
6241 char_u *initdir;
6242 char_u *defname;
6243 char_u buf[NUMBUFLEN];
6244 char_u buf2[NUMBUFLEN];
6245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006246 save = get_tv_number(&argvars[0]);
6247 title = get_tv_string(&argvars[1]);
6248 initdir = get_tv_string_buf(&argvars[2], buf);
6249 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006251 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006252 do_browse(save ? BROWSE_SAVE : 0,
6253 title, defname, NULL, initdir, NULL, curbuf);
6254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006255 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006257 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006258}
6259
6260/*
6261 * "browsedir(title, initdir)" function
6262 */
6263/* ARGSUSED */
6264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006265f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006268{
6269#ifdef FEAT_BROWSE
6270 char_u *title;
6271 char_u *initdir;
6272 char_u buf[NUMBUFLEN];
6273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006274 title = get_tv_string(&argvars[0]);
6275 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006277 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006278 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006280 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006282 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283}
6284
Bram Moolenaar0d660222005-01-07 21:51:51 +00006285static buf_T *find_buffer __ARGS((typeval *avar));
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287/*
6288 * Find a buffer by number or exact name.
6289 */
6290 static buf_T *
6291find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 if (avar->v_type == VAR_NUMBER)
6297 buf = buflist_findnr((int)avar->vval.v_number);
6298 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006301 if (buf == NULL)
6302 {
6303 /* No full path name match, try a match with a URL or a "nofile"
6304 * buffer, these don't use the full path. */
6305 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6306 if (buf->b_fname != NULL
6307 && (path_with_url(buf->b_fname)
6308#ifdef FEAT_QUICKFIX
6309 || bt_nofile(buf)
6310#endif
6311 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006313 break;
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316 return buf;
6317}
6318
6319/*
6320 * "bufexists(expr)" function
6321 */
6322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006323f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006325 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006327 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329
6330/*
6331 * "buflisted(expr)" function
6332 */
6333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006334f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006336 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 buf_T *buf;
6339
6340 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006341 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
6343
6344/*
6345 * "bufloaded(expr)" function
6346 */
6347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006348f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 buf_T *buf;
6353
6354 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006355 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
Bram Moolenaar0d660222005-01-07 21:51:51 +00006358static buf_T *get_buf_tv __ARGS((typeval *tv));
6359
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360/*
6361 * Get buffer by number or pattern.
6362 */
6363 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364get_buf_tv(tv)
6365 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006367 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 int save_magic;
6369 char_u *save_cpo;
6370 buf_T *buf;
6371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372 if (tv->v_type == VAR_NUMBER)
6373 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (name == NULL || *name == NUL)
6375 return curbuf;
6376 if (name[0] == '$' && name[1] == NUL)
6377 return lastbuf;
6378
6379 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6380 save_magic = p_magic;
6381 p_magic = TRUE;
6382 save_cpo = p_cpo;
6383 p_cpo = (char_u *)"";
6384
6385 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6386 TRUE, FALSE));
6387
6388 p_magic = save_magic;
6389 p_cpo = save_cpo;
6390
6391 /* If not found, try expanding the name, like done for bufexists(). */
6392 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006393 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395 return buf;
6396}
6397
6398/*
6399 * "bufname(expr)" function
6400 */
6401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006402f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006404 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
6406 buf_T *buf;
6407
6408 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006409 buf = get_buf_tv(&argvars[0]);
6410 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006412 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 --emsg_off;
6416}
6417
6418/*
6419 * "bufnr(expr)" function
6420 */
6421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006422f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006424 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 buf_T *buf;
6427
6428 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006429 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006433 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 --emsg_off;
6435}
6436
6437/*
6438 * "bufwinnr(nr)" function
6439 */
6440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006441f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006443 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445#ifdef FEAT_WINDOWS
6446 win_T *wp;
6447 int winnr = 0;
6448#endif
6449 buf_T *buf;
6450
6451 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006452 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453#ifdef FEAT_WINDOWS
6454 for (wp = firstwin; wp; wp = wp->w_next)
6455 {
6456 ++winnr;
6457 if (wp->w_buffer == buf)
6458 break;
6459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463#endif
6464 --emsg_off;
6465}
6466
6467/*
6468 * "byte2line(byte)" function
6469 */
6470/*ARGSUSED*/
6471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006472f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478#else
6479 long boff = 0;
6480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006485 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 (linenr_T)0, &boff);
6487#endif
6488}
6489
6490/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006491 * "byteidx()" function
6492 */
6493/*ARGSUSED*/
6494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006497 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006498{
6499#ifdef FEAT_MBYTE
6500 char_u *t;
6501#endif
6502 char_u *str;
6503 long idx;
6504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006505 str = get_tv_string(&argvars[0]);
6506 idx = get_tv_number(&argvars[1]);
6507 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006508 if (idx < 0)
6509 return;
6510
6511#ifdef FEAT_MBYTE
6512 t = str;
6513 for ( ; idx > 0; idx--)
6514 {
6515 if (*t == NUL) /* EOL reached */
6516 return;
6517 t += mb_ptr2len_check(t);
6518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006520#else
6521 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006522 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006523#endif
6524}
6525
6526/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 * "call(func, arglist)" function
6528 */
6529 static void
6530f_call(argvars, rettv)
6531 typeval *argvars;
6532 typeval *rettv;
6533{
6534 char_u *func;
6535 typeval argv[MAX_FUNC_ARGS];
6536 int argc = 0;
6537 listitem *item;
6538 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 dictvar *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
6541 rettv->vval.v_number = 0;
6542 if (argvars[1].v_type != VAR_LIST)
6543 {
6544 EMSG(_(e_listreq));
6545 return;
6546 }
6547 if (argvars[1].vval.v_list == NULL)
6548 return;
6549
6550 if (argvars[0].v_type == VAR_FUNC)
6551 func = argvars[0].vval.v_string;
6552 else
6553 func = get_tv_string(&argvars[0]);
6554
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 if (argvars[2].v_type != VAR_UNKNOWN)
6556 {
6557 if (argvars[2].v_type != VAR_DICT)
6558 {
6559 EMSG(_(e_dictreq));
6560 return;
6561 }
6562 selfdict = argvars[2].vval.v_dict;
6563 }
6564
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6566 item = item->li_next)
6567 {
6568 if (argc == MAX_FUNC_ARGS)
6569 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006570 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571 break;
6572 }
6573 /* Make a copy of each argument (is this really needed?) */
6574 copy_tv(&item->li_tv, &argv[argc++]);
6575 }
6576
6577 if (item == NULL)
6578 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6580 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581
6582 /* Free the arguments. */
6583 while (argc > 0)
6584 clear_tv(&argv[--argc]);
6585}
6586
6587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 * "char2nr(string)" function
6589 */
6590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006591f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006593 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595#ifdef FEAT_MBYTE
6596 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006597 rettv->vval.v_number =
6598 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 else
6600#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006601 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602}
6603
6604/*
6605 * "cindent(lnum)" function
6606 */
6607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006608f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612#ifdef FEAT_CINDENT
6613 pos_T pos;
6614 linenr_T lnum;
6615
6616 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006617 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6619 {
6620 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006621 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 curwin->w_cursor = pos;
6623 }
6624 else
6625#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
6628
6629/*
6630 * "col(string)" function
6631 */
6632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006633f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006635 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
6637 colnr_T col = 0;
6638 pos_T *fp;
6639
6640 fp = var2fpos(&argvars[0], FALSE);
6641 if (fp != NULL)
6642 {
6643 if (fp->col == MAXCOL)
6644 {
6645 /* '> can be MAXCOL, get the length of the line then */
6646 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6647 col = STRLEN(ml_get(fp->lnum)) + 1;
6648 else
6649 col = MAXCOL;
6650 }
6651 else
6652 {
6653 col = fp->col + 1;
6654#ifdef FEAT_VIRTUALEDIT
6655 /* col(".") when the cursor is on the NUL at the end of the line
6656 * because of "coladd" can be seen as an extra column. */
6657 if (virtual_active() && fp == &curwin->w_cursor)
6658 {
6659 char_u *p = ml_get_cursor();
6660
6661 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6662 curwin->w_virtcol - curwin->w_cursor.coladd))
6663 {
6664# ifdef FEAT_MBYTE
6665 int l;
6666
6667 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6668 col += l;
6669# else
6670 if (*p != NUL && p[1] == NUL)
6671 ++col;
6672# endif
6673 }
6674 }
6675#endif
6676 }
6677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006678 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681/*
6682 * "confirm(message, buttons[, default [, type]])" function
6683 */
6684/*ARGSUSED*/
6685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006686f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006688 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6691 char_u *message;
6692 char_u *buttons = NULL;
6693 char_u buf[NUMBUFLEN];
6694 char_u buf2[NUMBUFLEN];
6695 int def = 1;
6696 int type = VIM_GENERIC;
6697 int c;
6698
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006699 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006702 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006705 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006709 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 switch (TOUPPER_ASC(c))
6711 {
6712 case 'E': type = VIM_ERROR; break;
6713 case 'Q': type = VIM_QUESTION; break;
6714 case 'I': type = VIM_INFO; break;
6715 case 'W': type = VIM_WARNING; break;
6716 case 'G': type = VIM_GENERIC; break;
6717 }
6718 }
6719 }
6720 }
6721
6722 if (buttons == NULL || *buttons == NUL)
6723 buttons = (char_u *)_("&Ok");
6724
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006725 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 def, NULL);
6727#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006728 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729#endif
6730}
6731
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732/*
6733 * "copy()" function
6734 */
6735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006737 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006739{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006740 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006744 * "count()" function
6745 */
6746 static void
6747f_count(argvars, rettv)
6748 typeval *argvars;
6749 typeval *rettv;
6750{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006751 long n = 0;
6752 int ic = FALSE;
6753
Bram Moolenaare9a41262005-01-15 22:18:47 +00006754 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006755 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006756 listitem *li;
6757 listvar *l;
6758 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 if ((l = argvars[0].vval.v_list) != NULL)
6761 {
6762 li = l->lv_first;
6763 if (argvars[2].v_type != VAR_UNKNOWN)
6764 {
6765 ic = get_tv_number(&argvars[2]);
6766 if (argvars[3].v_type != VAR_UNKNOWN)
6767 {
6768 idx = get_tv_number(&argvars[3]);
6769 li = list_find(l, idx);
6770 if (li == NULL)
6771 EMSGN(_(e_listidx), idx);
6772 }
6773 }
6774
6775 for ( ; li != NULL; li = li->li_next)
6776 if (tv_equal(&li->li_tv, &argvars[1], ic))
6777 ++n;
6778 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006779 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006780 else if (argvars[0].v_type == VAR_DICT)
6781 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006782 int todo;
6783 dictvar *d;
6784 hashitem *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006785
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006786 if ((d = argvars[0].vval.v_dict) != NULL)
6787 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006788 if (argvars[2].v_type != VAR_UNKNOWN)
6789 {
6790 ic = get_tv_number(&argvars[2]);
6791 if (argvars[3].v_type != VAR_UNKNOWN)
6792 EMSG(_(e_invarg));
6793 }
6794
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006795 todo = d->dv_hashtable.ht_used;
6796 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
6797 {
6798 if (!HASHITEM_EMPTY(hi))
6799 {
6800 --todo;
6801 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
6802 ++n;
6803 }
6804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 }
6806 }
6807 else
6808 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006809 rettv->vval.v_number = n;
6810}
6811
6812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6814 *
6815 * Checks the existence of a cscope connection.
6816 */
6817/*ARGSUSED*/
6818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006819f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006820 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006821 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823#ifdef FEAT_CSCOPE
6824 int num = 0;
6825 char_u *dbpath = NULL;
6826 char_u *prepend = NULL;
6827 char_u buf[NUMBUFLEN];
6828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 if (argvars[0].v_type != VAR_UNKNOWN
6830 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832 num = (int)get_tv_number(&argvars[0]);
6833 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006838 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006840 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841#endif
6842}
6843
6844/*
6845 * "cursor(lnum, col)" function
6846 *
6847 * Moves the cursor to the specified line and column
6848 */
6849/*ARGSUSED*/
6850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006851f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006852 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006853 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 long line, col;
6856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if (line > 0)
6859 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006860 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (col > 0)
6862 curwin->w_cursor.col = col - 1;
6863#ifdef FEAT_VIRTUALEDIT
6864 curwin->w_cursor.coladd = 0;
6865#endif
6866
6867 /* Make sure the cursor is in a valid position. */
6868 check_cursor();
6869#ifdef FEAT_MBYTE
6870 /* Correct cursor for multi-byte character. */
6871 if (has_mbyte)
6872 mb_adjust_cursor();
6873#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006874
6875 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876}
6877
6878/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006879 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 */
6881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006882f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006883 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006884 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006886 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887}
6888
6889/*
6890 * "delete()" function
6891 */
6892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006894 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
6897 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901}
6902
6903/*
6904 * "did_filetype()" function
6905 */
6906/*ARGSUSED*/
6907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006908f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006909 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006913 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006915 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916#endif
6917}
6918
6919/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00006920 * "diff_filler()" function
6921 */
6922/*ARGSUSED*/
6923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006924f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006925 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006926 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006927{
6928#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006929 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00006930#endif
6931}
6932
6933/*
6934 * "diff_hlID()" function
6935 */
6936/*ARGSUSED*/
6937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006938f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006939 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006940 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006941{
6942#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006943 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00006944 static linenr_T prev_lnum = 0;
6945 static int changedtick = 0;
6946 static int fnum = 0;
6947 static int change_start = 0;
6948 static int change_end = 0;
6949 static enum hlf_value hlID = 0;
6950 int filler_lines;
6951 int col;
6952
6953 if (lnum != prev_lnum
6954 || changedtick != curbuf->b_changedtick
6955 || fnum != curbuf->b_fnum)
6956 {
6957 /* New line, buffer, change: need to get the values. */
6958 filler_lines = diff_check(curwin, lnum);
6959 if (filler_lines < 0)
6960 {
6961 if (filler_lines == -1)
6962 {
6963 change_start = MAXCOL;
6964 change_end = -1;
6965 if (diff_find_change(curwin, lnum, &change_start, &change_end))
6966 hlID = HLF_ADD; /* added line */
6967 else
6968 hlID = HLF_CHD; /* changed line */
6969 }
6970 else
6971 hlID = HLF_ADD; /* added line */
6972 }
6973 else
6974 hlID = (enum hlf_value)0;
6975 prev_lnum = lnum;
6976 changedtick = curbuf->b_changedtick;
6977 fnum = curbuf->b_fnum;
6978 }
6979
6980 if (hlID == HLF_CHD || hlID == HLF_TXD)
6981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006982 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006983 if (col >= change_start && col <= change_end)
6984 hlID = HLF_TXD; /* changed text */
6985 else
6986 hlID = HLF_CHD; /* changed line */
6987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006988 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006989#endif
6990}
6991
6992/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006993 * "empty({expr})" function
6994 */
6995 static void
6996f_empty(argvars, rettv)
6997 typeval *argvars;
6998 typeval *rettv;
6999{
7000 int n;
7001
7002 switch (argvars[0].v_type)
7003 {
7004 case VAR_STRING:
7005 case VAR_FUNC:
7006 n = argvars[0].vval.v_string == NULL
7007 || *argvars[0].vval.v_string == NUL;
7008 break;
7009 case VAR_NUMBER:
7010 n = argvars[0].vval.v_number == 0;
7011 break;
7012 case VAR_LIST:
7013 n = argvars[0].vval.v_list == NULL
7014 || argvars[0].vval.v_list->lv_first == NULL;
7015 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 case VAR_DICT:
7017 n = argvars[0].vval.v_dict == NULL
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018 || argvars[0].vval.v_dict->dv_hashtable.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007019 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007020 default:
7021 EMSG2(_(e_intern2), "f_empty()");
7022 n = 0;
7023 }
7024
7025 rettv->vval.v_number = n;
7026}
7027
7028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 * "escape({string}, {chars})" function
7030 */
7031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007032f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007033 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007034 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 char_u buf[NUMBUFLEN];
7037
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 rettv->vval.v_string =
7039 vim_strsave_escaped(get_tv_string(&argvars[0]),
7040 get_tv_string_buf(&argvars[1], buf));
7041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007045 * "eval()" function
7046 */
7047/*ARGSUSED*/
7048 static void
7049f_eval(argvars, rettv)
7050 typeval *argvars;
7051 typeval *rettv;
7052{
7053 char_u *s;
7054
7055 s = get_tv_string(&argvars[0]);
7056 s = skipwhite(s);
7057
7058 if (eval1(&s, rettv, TRUE) == FAIL)
7059 rettv->vval.v_number = 0;
7060 else if (*s != NUL)
7061 EMSG(_(e_trailing));
7062}
7063
7064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 * "eventhandler()" function
7066 */
7067/*ARGSUSED*/
7068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007069f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007070 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007071 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074}
7075
7076/*
7077 * "executable()" function
7078 */
7079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007080f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007081 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007082 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
7086
7087/*
7088 * "exists()" function
7089 */
7090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007091f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007092 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007093 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
7095 char_u *p;
7096 char_u *name;
7097 int n = FALSE;
7098 int len = 0;
7099
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 if (*p == '$') /* environment variable */
7102 {
7103 /* first try "normal" environment variables (fast) */
7104 if (mch_getenv(p + 1) != NULL)
7105 n = TRUE;
7106 else
7107 {
7108 /* try expanding things like $VIM and ${HOME} */
7109 p = expand_env_save(p);
7110 if (p != NULL && *p != '$')
7111 n = TRUE;
7112 vim_free(p);
7113 }
7114 }
7115 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007116 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 else if (*p == '*') /* internal or user defined function */
7118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121 else if (*p == ':')
7122 {
7123 n = cmd_exists(p + 1);
7124 }
7125 else if (*p == '#')
7126 {
7127#ifdef FEAT_AUTOCMD
7128 name = p + 1;
7129 p = vim_strchr(name, '#');
7130 if (p != NULL)
7131 n = au_exists(name, p, p + 1);
7132 else
7133 n = au_exists(name, name + STRLEN(name), NULL);
7134#endif
7135 }
7136 else /* internal variable */
7137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 char_u *expr_start;
7139 char_u *expr_end;
7140 char_u *temp_string = NULL;
7141 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 name = p;
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007145 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (expr_start != NULL)
7147 {
7148 temp_string = make_expanded_name(name, expr_start, expr_end, s);
7149 if (temp_string != NULL)
7150 {
7151 len = STRLEN(temp_string);
7152 name = temp_string;
7153 }
7154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (len == 0)
7156 len = get_id_len(&p);
7157 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007158 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
7162
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007163 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164}
7165
7166/*
7167 * "expand()" function
7168 */
7169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007170f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007171 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007172 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 char_u *s;
7175 int len;
7176 char_u *errormsg;
7177 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7178 expand_T xpc;
7179
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007180 rettv->v_type = VAR_STRING;
7181 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (*s == '%' || *s == '#' || *s == '<')
7183 {
7184 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007185 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 --emsg_off;
7187 }
7188 else
7189 {
7190 /* When the optional second argument is non-zero, don't remove matches
7191 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007192 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 flags |= WILD_KEEP_ALL;
7194 ExpandInit(&xpc);
7195 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007196 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 ExpandCleanup(&xpc);
7198 }
7199}
7200
7201/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007202 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007204 */
7205 static void
7206f_extend(argvars, rettv)
7207 typeval *argvars;
7208 typeval *rettv;
7209{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007210 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007212 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 listvar *l1, *l2;
7214 listitem *item;
7215 long before;
7216 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007217
Bram Moolenaare9a41262005-01-15 22:18:47 +00007218 l1 = argvars[0].vval.v_list;
7219 l2 = argvars[1].vval.v_list;
7220 if (l1 != NULL && l2 != NULL)
7221 {
7222 if (argvars[2].v_type != VAR_UNKNOWN)
7223 {
7224 n = before = get_tv_number(&argvars[2]);
7225 item = list_find_ext(l1, &n);
7226 if (n != 0)
7227 {
7228 EMSGN(_(e_listidx), before);
7229 return;
7230 }
7231 }
7232 else
7233 item = NULL;
7234 list_extend(l1, l2, item);
7235
7236 ++l1->lv_refcount;
7237 copy_tv(&argvars[0], rettv);
7238 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7241 {
7242 dictvar *d1, *d2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 dictitem *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 char_u *action;
7245 int i;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 hashitem *hi2;
7247 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007248
7249 d1 = argvars[0].vval.v_dict;
7250 d2 = argvars[1].vval.v_dict;
7251 if (d1 != NULL && d2 != NULL)
7252 {
7253 /* Check the third argument. */
7254 if (argvars[2].v_type != VAR_UNKNOWN)
7255 {
7256 static char *(av[]) = {"keep", "force", "error"};
7257
7258 action = get_tv_string(&argvars[2]);
7259 for (i = 0; i < 3; ++i)
7260 if (STRCMP(action, av[i]) == 0)
7261 break;
7262 if (i == 3)
7263 {
7264 EMSGN(_(e_invarg2), action);
7265 return;
7266 }
7267 }
7268 else
7269 action = (char_u *)"force";
7270
7271 /* Go over all entries in the second dict and add them to the
7272 * first dict. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 todo = d2->dv_hashtable.ht_used;
7274 for (hi2 = d2->dv_hashtable.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 --todo;
7279 di1 = dict_find(d1, hi2->hi_key, -1);
7280 if (di1 == NULL)
7281 {
7282 di1 = dictitem_copy(HI2DI(hi2));
7283 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7284 dictitem_free(di1);
7285 }
7286 else if (*action == 'e')
7287 {
7288 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7289 break;
7290 }
7291 else if (*action == 'f')
7292 {
7293 clear_tv(&di1->di_tv);
7294 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7295 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 }
7297 }
7298
7299 ++d1->dv_refcount;
7300 copy_tv(&argvars[0], rettv);
7301 }
7302 }
7303 else
7304 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007305}
7306
7307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 * "filereadable()" function
7309 */
7310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007311f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007313 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 FILE *fd;
7316 char_u *p;
7317 int n;
7318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7321 {
7322 n = TRUE;
7323 fclose(fd);
7324 }
7325 else
7326 n = FALSE;
7327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007328 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
7331/*
7332 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7333 * rights to write into.
7334 */
7335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007336f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007338 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340 char_u *p;
7341 int retval = 0;
7342#if defined(UNIX) || defined(VMS)
7343 int perm = 0;
7344#endif
7345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347#if defined(UNIX) || defined(VMS)
7348 perm = mch_getperm(p);
7349#endif
7350#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7351 if (
7352# ifdef WIN3264
7353 mch_writable(p) &&
7354# else
7355# if defined(UNIX) || defined(VMS)
7356 (perm & 0222) &&
7357# endif
7358# endif
7359 mch_access((char *)p, W_OK) == 0
7360 )
7361#endif
7362 {
7363 ++retval;
7364 if (mch_isdir(p))
7365 ++retval;
7366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368}
7369
Bram Moolenaar0d660222005-01-07 21:51:51 +00007370static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007371
7372 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007373findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007374 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007376 int dir;
7377{
7378#ifdef FEAT_SEARCHPATH
7379 char_u *fname;
7380 char_u *fresult = NULL;
7381 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7382 char_u *p;
7383 char_u pathbuf[NUMBUFLEN];
7384 int count = 1;
7385 int first = TRUE;
7386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007387 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007388
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007391 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007392 if (*p != NUL)
7393 path = p;
7394
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007396 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007397 }
7398
7399 do
7400 {
7401 vim_free(fresult);
7402 fresult = find_file_in_path_option(first ? fname : NULL,
7403 first ? (int)STRLEN(fname) : 0,
7404 0, first, path, dir, NULL);
7405 first = FALSE;
7406 } while (--count > 0 && fresult != NULL);
7407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007408 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007409#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007410 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007411#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007413}
7414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007415static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416static int filter_map_one __ARGS((typeval *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007417
7418/*
7419 * Implementation of map() and filter().
7420 */
7421 static void
7422filter_map(argvars, rettv, map)
7423 typeval *argvars;
7424 typeval *rettv;
7425 int map;
7426{
7427 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 char_u *expr;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007429 listitem *li, *nli;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 listvar *l = NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 dictitem *di;
7432 hashitem *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 dictvar *d = NULL;
7434 typeval save_val;
7435 typeval save_key;
7436 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 int todo;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007438
7439 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007440 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007441 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007442 if ((l = argvars[0].vval.v_list) == NULL)
7443 return;
7444 }
7445 else if (argvars[0].v_type == VAR_DICT)
7446 {
7447 if ((d = argvars[0].vval.v_dict) == NULL)
7448 return;
7449 }
7450 else
7451 {
7452 EMSG2(_(e_listdictarg), map ? "map()" : "filter()");
7453 return;
7454 }
7455
7456 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
7457 save_val = vimvars[VV_VAL].tv;
7458
7459 if (argvars[0].v_type == VAR_DICT)
7460 {
7461 save_key = vimvars[VV_KEY].tv;
7462 vimvars[VV_KEY].tv.v_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007463
7464 todo = d->dv_hashtable.ht_used;
7465 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007466 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007468 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007469 --todo;
7470 di = HI2DI(hi);
7471 vimvars[VV_KEY].tv.vval.v_string = vim_strsave(di->di_key);
7472 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
7473 break;
7474 if (!map && rem)
7475 dictitem_remove(d, di);
7476 clear_tv(&vimvars[VV_KEY].tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007477 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007478 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 clear_tv(&vimvars[VV_KEY].tv);
7481 vimvars[VV_KEY].tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 else
7484 {
7485 for (li = l->lv_first; li != NULL; li = nli)
7486 {
7487 nli = li->li_next;
7488 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7489 break;
7490 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007491 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 }
7493 }
7494
7495 clear_tv(&vimvars[VV_VAL].tv);
7496 vimvars[VV_VAL].tv = save_val;
7497
7498 copy_tv(&argvars[0], rettv);
7499}
7500
7501 static int
7502filter_map_one(tv, expr, map, remp)
7503 typeval *tv;
7504 char_u *expr;
7505 int map;
7506 int *remp;
7507{
7508 typeval rettv;
7509 char_u *s;
7510
7511 copy_tv(tv, &vimvars[VV_VAL].tv);
7512 s = expr;
7513 if (eval1(&s, &rettv, TRUE) == FAIL)
7514 return FAIL;
7515 if (*s != NUL) /* check for trailing chars after expr */
7516 {
7517 EMSG2(_(e_invexpr2), s);
7518 return FAIL;
7519 }
7520 if (map)
7521 {
7522 /* map(): replace the list item value */
7523 clear_tv(tv);
7524 *tv = rettv;
7525 }
7526 else
7527 {
7528 /* filter(): when expr is zero remove the item */
7529 *remp = (get_tv_number(&rettv) == 0);
7530 clear_tv(&rettv);
7531 }
7532 clear_tv(&vimvars[VV_VAL].tv);
7533 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534}
7535
7536/*
7537 * "filter()" function
7538 */
7539 static void
7540f_filter(argvars, rettv)
7541 typeval *argvars;
7542 typeval *rettv;
7543{
7544 filter_map(argvars, rettv, FALSE);
7545}
7546
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007547/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007548 * "finddir({fname}[, {path}[, {count}]])" function
7549 */
7550 static void
7551f_finddir(argvars, rettv)
7552 typeval *argvars;
7553 typeval *rettv;
7554{
7555 findfilendir(argvars, rettv, TRUE);
7556}
7557
7558/*
7559 * "findfile({fname}[, {path}[, {count}]])" function
7560 */
7561 static void
7562f_findfile(argvars, rettv)
7563 typeval *argvars;
7564 typeval *rettv;
7565{
7566 findfilendir(argvars, rettv, FALSE);
7567}
7568
7569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 * "fnamemodify({fname}, {mods})" function
7571 */
7572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 char_u *fname;
7578 char_u *mods;
7579 int usedlen = 0;
7580 int len;
7581 char_u *fbuf = NULL;
7582 char_u buf[NUMBUFLEN];
7583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 fname = get_tv_string(&argvars[0]);
7585 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 len = (int)STRLEN(fname);
7587
7588 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 vim_free(fbuf);
7596}
7597
Bram Moolenaar0d660222005-01-07 21:51:51 +00007598static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
7600/*
7601 * "foldclosed()" function
7602 */
7603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 int end;
7608{
7609#ifdef FEAT_FOLDING
7610 linenr_T lnum;
7611 linenr_T first, last;
7612
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7615 {
7616 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7617 {
7618 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 return;
7623 }
7624 }
7625#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627}
7628
7629/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007630 * "foldclosed()" function
7631 */
7632 static void
7633f_foldclosed(argvars, rettv)
7634 typeval *argvars;
7635 typeval *rettv;
7636{
7637 foldclosed_both(argvars, rettv, FALSE);
7638}
7639
7640/*
7641 * "foldclosedend()" function
7642 */
7643 static void
7644f_foldclosedend(argvars, rettv)
7645 typeval *argvars;
7646 typeval *rettv;
7647{
7648 foldclosed_both(argvars, rettv, TRUE);
7649}
7650
7651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 * "foldlevel()" function
7653 */
7654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007656 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
7659#ifdef FEAT_FOLDING
7660 linenr_T lnum;
7661
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 else
7666#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668}
7669
7670/*
7671 * "foldtext()" function
7672 */
7673/*ARGSUSED*/
7674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007677 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679#ifdef FEAT_FOLDING
7680 linenr_T lnum;
7681 char_u *s;
7682 char_u *r;
7683 int len;
7684 char *txt;
7685#endif
7686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->v_type = VAR_STRING;
7688 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7691 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7692 <= curbuf->b_ml.ml_line_count
7693 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
7695 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007696 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7697 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
7699 if (!linewhite(lnum))
7700 break;
7701 ++lnum;
7702 }
7703
7704 /* Find interesting text in this line. */
7705 s = skipwhite(ml_get(lnum));
7706 /* skip C comment-start */
7707 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007710 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007711 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007712 {
7713 s = skipwhite(ml_get(lnum + 1));
7714 if (*s == '*')
7715 s = skipwhite(s + 1);
7716 }
7717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 txt = _("+-%s%3ld lines: ");
7719 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 + 20 /* for %3ld */
7722 + STRLEN(s))); /* concatenated */
7723 if (r != NULL)
7724 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7726 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7727 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 len = (int)STRLEN(r);
7729 STRCAT(r, s);
7730 /* remove 'foldmarker' and 'commentstring' */
7731 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734 }
7735#endif
7736}
7737
7738/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007739 * "foldtextresult(lnum)" function
7740 */
7741/*ARGSUSED*/
7742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007744 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007746{
7747#ifdef FEAT_FOLDING
7748 linenr_T lnum;
7749 char_u *text;
7750 char_u buf[51];
7751 foldinfo_T foldinfo;
7752 int fold_count;
7753#endif
7754
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->v_type = VAR_STRING;
7756 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007757#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007759 fold_count = foldedCount(curwin, lnum, &foldinfo);
7760 if (fold_count > 0)
7761 {
7762 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7763 &foldinfo, buf);
7764 if (text == buf)
7765 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007767 }
7768#endif
7769}
7770
7771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 * "foreground()" function
7773 */
7774/*ARGSUSED*/
7775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781#ifdef FEAT_GUI
7782 if (gui.in_use)
7783 gui_mch_set_foreground();
7784#else
7785# ifdef WIN32
7786 win32_set_foreground();
7787# endif
7788#endif
7789}
7790
7791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792 * "function()" function
7793 */
7794/*ARGSUSED*/
7795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007798 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799{
7800 char_u *s;
7801
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007803 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007804 EMSG2(_(e_invarg2), s);
7805 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007806 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 else
7808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809 rettv->vval.v_string = vim_strsave(s);
7810 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 }
7812}
7813
7814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007815 * "get()" function
7816 */
7817 static void
7818f_get(argvars, rettv)
7819 typeval *argvars;
7820 typeval *rettv;
7821{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 listitem *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007823 listvar *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 dictitem *di;
7825 dictvar *d;
7826 typeval *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007827
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007829 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007831 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 li = list_find(l, get_tv_number(&argvars[1]));
7833 if (li != NULL)
7834 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007835 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00007836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 else if (argvars[0].v_type == VAR_DICT)
7838 {
7839 if ((d = argvars[0].vval.v_dict) != NULL)
7840 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007841 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 if (di != NULL)
7843 tv = &di->di_tv;
7844 }
7845 }
7846 else
7847 EMSG2(_(e_listdictarg), "get()");
7848
7849 if (tv == NULL)
7850 {
7851 if (argvars[2].v_type == VAR_UNKNOWN)
7852 rettv->vval.v_number = 0;
7853 else
7854 copy_tv(&argvars[2], rettv);
7855 }
7856 else
7857 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007858}
7859
7860/*
7861 * "getbufvar()" function
7862 */
7863 static void
7864f_getbufvar(argvars, rettv)
7865 typeval *argvars;
7866 typeval *rettv;
7867{
7868 buf_T *buf;
7869 buf_T *save_curbuf;
7870 char_u *varname;
7871 VAR v;
7872
7873 ++emsg_off;
7874 buf = get_buf_tv(&argvars[0]);
7875 varname = get_tv_string(&argvars[1]);
7876
7877 rettv->v_type = VAR_STRING;
7878 rettv->vval.v_string = NULL;
7879
7880 if (buf != NULL && varname != NULL)
7881 {
7882 if (*varname == '&') /* buffer-local-option */
7883 {
7884 /* set curbuf to be our buf, temporarily */
7885 save_curbuf = curbuf;
7886 curbuf = buf;
7887
7888 get_option_tv(&varname, rettv, TRUE);
7889
7890 /* restore previous notion of curbuf */
7891 curbuf = save_curbuf;
7892 }
7893 else
7894 {
7895 /* look up the variable */
7896 v = find_var_in_ga(&buf->b_vars, varname);
7897 if (v != NULL)
7898 copy_tv(&v->tv, rettv);
7899 }
7900 }
7901
7902 --emsg_off;
7903}
7904
7905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 * "getchar()" function
7907 */
7908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 varnumber_T n;
7914
7915 ++no_mapping;
7916 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 /* getchar(): blocking wait. */
7919 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 /* getchar(1): only check if char avail */
7922 n = vpeekc();
7923 else if (vpeekc() == NUL)
7924 /* getchar(0) and no char avail: return zero */
7925 n = 0;
7926 else
7927 /* getchar(0) and char avail: return char */
7928 n = safe_vgetc();
7929 --no_mapping;
7930 --allow_keys;
7931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 if (IS_SPECIAL(n) || mod_mask != 0)
7934 {
7935 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
7936 int i = 0;
7937
7938 /* Turn a special key into three bytes, plus modifier. */
7939 if (mod_mask != 0)
7940 {
7941 temp[i++] = K_SPECIAL;
7942 temp[i++] = KS_MODIFIER;
7943 temp[i++] = mod_mask;
7944 }
7945 if (IS_SPECIAL(n))
7946 {
7947 temp[i++] = K_SPECIAL;
7948 temp[i++] = K_SECOND(n);
7949 temp[i++] = K_THIRD(n);
7950 }
7951#ifdef FEAT_MBYTE
7952 else if (has_mbyte)
7953 i += (*mb_char2bytes)(n, temp + i);
7954#endif
7955 else
7956 temp[i++] = n;
7957 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->v_type = VAR_STRING;
7959 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961}
7962
7963/*
7964 * "getcharmod()" function
7965 */
7966/*ARGSUSED*/
7967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968f_getcharmod(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973}
7974
7975/*
7976 * "getcmdline()" function
7977 */
7978/*ARGSUSED*/
7979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007980f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007981 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 rettv->v_type = VAR_STRING;
7985 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986}
7987
7988/*
7989 * "getcmdpos()" function
7990 */
7991/*ARGSUSED*/
7992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007994 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998}
7999
8000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 * "getcwd()" function
8002 */
8003/*ARGSUSED*/
8004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 char_u cwd[MAXPATHL];
8010
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 else
8015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019#endif
8020 }
8021}
8022
8023/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008024 * "getfontname()" function
8025 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008026/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->v_type = VAR_STRING;
8033 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008034#ifdef FEAT_GUI
8035 if (gui.in_use)
8036 {
8037 GuiFont font;
8038 char_u *name = NULL;
8039
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008040 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008041 {
8042 /* Get the "Normal" font. Either the name saved by
8043 * hl_set_font_name() or from the font ID. */
8044 font = gui.norm_font;
8045 name = hl_get_font_name();
8046 }
8047 else
8048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008050 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8051 return;
8052 font = gui_mch_get_font(name, FALSE);
8053 if (font == NOFONT)
8054 return; /* Invalid font name, return empty string. */
8055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008057 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008058 gui_mch_free_font(font);
8059 }
8060#endif
8061}
8062
8063/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008064 * "getfperm({fname})" function
8065 */
8066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008067f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008069 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008070{
8071 char_u *fname;
8072 struct stat st;
8073 char_u *perm = NULL;
8074 char_u flags[] = "rwx";
8075 int i;
8076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008077 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008079 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008080 if (mch_stat((char *)fname, &st) >= 0)
8081 {
8082 perm = vim_strsave((char_u *)"---------");
8083 if (perm != NULL)
8084 {
8085 for (i = 0; i < 9; i++)
8086 {
8087 if (st.st_mode & (1 << (8 - i)))
8088 perm[i] = flags[i % 3];
8089 }
8090 }
8091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008093}
8094
8095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 * "getfsize({fname})" function
8097 */
8098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008100 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 char_u *fname;
8104 struct stat st;
8105
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008106 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008108 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109
8110 if (mch_stat((char *)fname, &st) >= 0)
8111 {
8112 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119}
8120
8121/*
8122 * "getftime({fname})" function
8123 */
8124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008125f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 char_u *fname;
8130 struct stat st;
8131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133
8134 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138}
8139
8140/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008141 * "getftype({fname})" function
8142 */
8143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008147{
8148 char_u *fname;
8149 struct stat st;
8150 char_u *type = NULL;
8151 char *t;
8152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008153 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008156 if (mch_lstat((char *)fname, &st) >= 0)
8157 {
8158#ifdef S_ISREG
8159 if (S_ISREG(st.st_mode))
8160 t = "file";
8161 else if (S_ISDIR(st.st_mode))
8162 t = "dir";
8163# ifdef S_ISLNK
8164 else if (S_ISLNK(st.st_mode))
8165 t = "link";
8166# endif
8167# ifdef S_ISBLK
8168 else if (S_ISBLK(st.st_mode))
8169 t = "bdev";
8170# endif
8171# ifdef S_ISCHR
8172 else if (S_ISCHR(st.st_mode))
8173 t = "cdev";
8174# endif
8175# ifdef S_ISFIFO
8176 else if (S_ISFIFO(st.st_mode))
8177 t = "fifo";
8178# endif
8179# ifdef S_ISSOCK
8180 else if (S_ISSOCK(st.st_mode))
8181 t = "fifo";
8182# endif
8183 else
8184 t = "other";
8185#else
8186# ifdef S_IFMT
8187 switch (st.st_mode & S_IFMT)
8188 {
8189 case S_IFREG: t = "file"; break;
8190 case S_IFDIR: t = "dir"; break;
8191# ifdef S_IFLNK
8192 case S_IFLNK: t = "link"; break;
8193# endif
8194# ifdef S_IFBLK
8195 case S_IFBLK: t = "bdev"; break;
8196# endif
8197# ifdef S_IFCHR
8198 case S_IFCHR: t = "cdev"; break;
8199# endif
8200# ifdef S_IFIFO
8201 case S_IFIFO: t = "fifo"; break;
8202# endif
8203# ifdef S_IFSOCK
8204 case S_IFSOCK: t = "socket"; break;
8205# endif
8206 default: t = "other";
8207 }
8208# else
8209 if (mch_isdir(fname))
8210 t = "dir";
8211 else
8212 t = "file";
8213# endif
8214#endif
8215 type = vim_strsave((char_u *)t);
8216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008218}
8219
8220/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008221 * "getline(lnum)" function
8222 */
8223 static void
8224f_getline(argvars, rettv)
8225 typeval *argvars;
8226 typeval *rettv;
8227{
8228 linenr_T lnum;
8229 linenr_T end;
8230 char_u *p;
8231 listvar *l;
8232 listitem *li;
8233
8234 lnum = get_tv_lnum(argvars);
8235
8236 if (argvars[1].v_type == VAR_UNKNOWN)
8237 {
8238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8239 p = ml_get(lnum);
8240 else
8241 p = (char_u *)"";
8242
8243 rettv->v_type = VAR_STRING;
8244 rettv->vval.v_string = vim_strsave(p);
8245 }
8246 else
8247 {
8248 end = get_tv_lnum(&argvars[1]);
8249 if (end < lnum)
8250 {
8251 EMSG(_(e_invrange));
8252 rettv->vval.v_number = 0;
8253 }
8254 else
8255 {
8256 l = list_alloc();
8257 if (l != NULL)
8258 {
8259 if (lnum < 1)
8260 lnum = 1;
8261 if (end > curbuf->b_ml.ml_line_count)
8262 end = curbuf->b_ml.ml_line_count;
8263 while (lnum <= end)
8264 {
8265 li = listitem_alloc();
8266 if (li == NULL)
8267 break;
8268 list_append(l, li);
8269 li->li_tv.v_type = VAR_STRING;
8270 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8271 }
8272 rettv->vval.v_list = l;
8273 rettv->v_type = VAR_LIST;
8274 ++l->lv_refcount;
8275 }
8276 }
8277 }
8278}
8279
8280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 * "getreg()" function
8282 */
8283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
8288 char_u *strregname;
8289 int regname;
8290
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008294 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 regname = (strregname == NULL ? '"' : *strregname);
8296 if (regname == 0)
8297 regname = '"';
8298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299 rettv->v_type = VAR_STRING;
8300 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
8302
8303/*
8304 * "getregtype()" function
8305 */
8306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008307f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311 char_u *strregname;
8312 int regname;
8313 char_u buf[NUMBUFLEN + 2];
8314 long reglen = 0;
8315
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 else
8319 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008320 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322 regname = (strregname == NULL ? '"' : *strregname);
8323 if (regname == 0)
8324 regname = '"';
8325
8326 buf[0] = NUL;
8327 buf[1] = NUL;
8328 switch (get_reg_type(regname, &reglen))
8329 {
8330 case MLINE: buf[0] = 'V'; break;
8331 case MCHAR: buf[0] = 'v'; break;
8332#ifdef FEAT_VISUAL
8333 case MBLOCK:
8334 buf[0] = Ctrl_V;
8335 sprintf((char *)buf + 1, "%ld", reglen + 1);
8336 break;
8337#endif
8338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 rettv->v_type = VAR_STRING;
8340 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
8343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 * "getwinposx()" function
8345 */
8346/*ARGSUSED*/
8347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008349 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353#ifdef FEAT_GUI
8354 if (gui.in_use)
8355 {
8356 int x, y;
8357
8358 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
8361#endif
8362}
8363
8364/*
8365 * "getwinposy()" function
8366 */
8367/*ARGSUSED*/
8368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008369f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008370 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374#ifdef FEAT_GUI
8375 if (gui.in_use)
8376 {
8377 int x, y;
8378
8379 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382#endif
8383}
8384
8385/*
8386 * "getwinvar()" function
8387 */
8388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008390 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
8393 win_T *win, *oldcurwin;
8394 char_u *varname;
8395 VAR v;
8396
8397 ++emsg_off;
8398 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 rettv->v_type = VAR_STRING;
8402 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 if (win != NULL && varname != NULL)
8405 {
8406 if (*varname == '&') /* window-local-option */
8407 {
8408 /* set curwin to be our win, temporarily */
8409 oldcurwin = curwin;
8410 curwin = win;
8411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 /* restore previous notion of curwin */
8415 curwin = oldcurwin;
8416 }
8417 else
8418 {
8419 /* look up the variable */
8420 v = find_var_in_ga(&win->w_vars, varname);
8421 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 }
8424 }
8425
8426 --emsg_off;
8427}
8428
8429/*
8430 * "glob()" function
8431 */
8432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 expand_T xpc;
8438
8439 ExpandInit(&xpc);
8440 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441 rettv->v_type = VAR_STRING;
8442 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8444 ExpandCleanup(&xpc);
8445}
8446
8447/*
8448 * "globpath()" function
8449 */
8450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008452 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008453 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 char_u buf1[NUMBUFLEN];
8456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457 rettv->v_type = VAR_STRING;
8458 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8459 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460}
8461
8462/*
8463 * "has()" function
8464 */
8465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008467 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470 int i;
8471 char_u *name;
8472 int n = FALSE;
8473 static char *(has_list[]) =
8474 {
8475#ifdef AMIGA
8476 "amiga",
8477# ifdef FEAT_ARP
8478 "arp",
8479# endif
8480#endif
8481#ifdef __BEOS__
8482 "beos",
8483#endif
8484#ifdef MSDOS
8485# ifdef DJGPP
8486 "dos32",
8487# else
8488 "dos16",
8489# endif
8490#endif
8491#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8492 "mac",
8493#endif
8494#if defined(MACOS_X_UNIX)
8495 "macunix",
8496#endif
8497#ifdef OS2
8498 "os2",
8499#endif
8500#ifdef __QNX__
8501 "qnx",
8502#endif
8503#ifdef RISCOS
8504 "riscos",
8505#endif
8506#ifdef UNIX
8507 "unix",
8508#endif
8509#ifdef VMS
8510 "vms",
8511#endif
8512#ifdef WIN16
8513 "win16",
8514#endif
8515#ifdef WIN32
8516 "win32",
8517#endif
8518#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8519 "win32unix",
8520#endif
8521#ifdef WIN64
8522 "win64",
8523#endif
8524#ifdef EBCDIC
8525 "ebcdic",
8526#endif
8527#ifndef CASE_INSENSITIVE_FILENAME
8528 "fname_case",
8529#endif
8530#ifdef FEAT_ARABIC
8531 "arabic",
8532#endif
8533#ifdef FEAT_AUTOCMD
8534 "autocmd",
8535#endif
8536#ifdef FEAT_BEVAL
8537 "balloon_eval",
8538#endif
8539#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8540 "builtin_terms",
8541# ifdef ALL_BUILTIN_TCAPS
8542 "all_builtin_terms",
8543# endif
8544#endif
8545#ifdef FEAT_BYTEOFF
8546 "byte_offset",
8547#endif
8548#ifdef FEAT_CINDENT
8549 "cindent",
8550#endif
8551#ifdef FEAT_CLIENTSERVER
8552 "clientserver",
8553#endif
8554#ifdef FEAT_CLIPBOARD
8555 "clipboard",
8556#endif
8557#ifdef FEAT_CMDL_COMPL
8558 "cmdline_compl",
8559#endif
8560#ifdef FEAT_CMDHIST
8561 "cmdline_hist",
8562#endif
8563#ifdef FEAT_COMMENTS
8564 "comments",
8565#endif
8566#ifdef FEAT_CRYPT
8567 "cryptv",
8568#endif
8569#ifdef FEAT_CSCOPE
8570 "cscope",
8571#endif
8572#ifdef DEBUG
8573 "debug",
8574#endif
8575#ifdef FEAT_CON_DIALOG
8576 "dialog_con",
8577#endif
8578#ifdef FEAT_GUI_DIALOG
8579 "dialog_gui",
8580#endif
8581#ifdef FEAT_DIFF
8582 "diff",
8583#endif
8584#ifdef FEAT_DIGRAPHS
8585 "digraphs",
8586#endif
8587#ifdef FEAT_DND
8588 "dnd",
8589#endif
8590#ifdef FEAT_EMACS_TAGS
8591 "emacs_tags",
8592#endif
8593 "eval", /* always present, of course! */
8594#ifdef FEAT_EX_EXTRA
8595 "ex_extra",
8596#endif
8597#ifdef FEAT_SEARCH_EXTRA
8598 "extra_search",
8599#endif
8600#ifdef FEAT_FKMAP
8601 "farsi",
8602#endif
8603#ifdef FEAT_SEARCHPATH
8604 "file_in_path",
8605#endif
8606#ifdef FEAT_FIND_ID
8607 "find_in_path",
8608#endif
8609#ifdef FEAT_FOLDING
8610 "folding",
8611#endif
8612#ifdef FEAT_FOOTER
8613 "footer",
8614#endif
8615#if !defined(USE_SYSTEM) && defined(UNIX)
8616 "fork",
8617#endif
8618#ifdef FEAT_GETTEXT
8619 "gettext",
8620#endif
8621#ifdef FEAT_GUI
8622 "gui",
8623#endif
8624#ifdef FEAT_GUI_ATHENA
8625# ifdef FEAT_GUI_NEXTAW
8626 "gui_neXtaw",
8627# else
8628 "gui_athena",
8629# endif
8630#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008631#ifdef FEAT_GUI_KDE
8632 "gui_kde",
8633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634#ifdef FEAT_GUI_GTK
8635 "gui_gtk",
8636# ifdef HAVE_GTK2
8637 "gui_gtk2",
8638# endif
8639#endif
8640#ifdef FEAT_GUI_MAC
8641 "gui_mac",
8642#endif
8643#ifdef FEAT_GUI_MOTIF
8644 "gui_motif",
8645#endif
8646#ifdef FEAT_GUI_PHOTON
8647 "gui_photon",
8648#endif
8649#ifdef FEAT_GUI_W16
8650 "gui_win16",
8651#endif
8652#ifdef FEAT_GUI_W32
8653 "gui_win32",
8654#endif
8655#ifdef FEAT_HANGULIN
8656 "hangul_input",
8657#endif
8658#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8659 "iconv",
8660#endif
8661#ifdef FEAT_INS_EXPAND
8662 "insert_expand",
8663#endif
8664#ifdef FEAT_JUMPLIST
8665 "jumplist",
8666#endif
8667#ifdef FEAT_KEYMAP
8668 "keymap",
8669#endif
8670#ifdef FEAT_LANGMAP
8671 "langmap",
8672#endif
8673#ifdef FEAT_LIBCALL
8674 "libcall",
8675#endif
8676#ifdef FEAT_LINEBREAK
8677 "linebreak",
8678#endif
8679#ifdef FEAT_LISP
8680 "lispindent",
8681#endif
8682#ifdef FEAT_LISTCMDS
8683 "listcmds",
8684#endif
8685#ifdef FEAT_LOCALMAP
8686 "localmap",
8687#endif
8688#ifdef FEAT_MENU
8689 "menu",
8690#endif
8691#ifdef FEAT_SESSION
8692 "mksession",
8693#endif
8694#ifdef FEAT_MODIFY_FNAME
8695 "modify_fname",
8696#endif
8697#ifdef FEAT_MOUSE
8698 "mouse",
8699#endif
8700#ifdef FEAT_MOUSESHAPE
8701 "mouseshape",
8702#endif
8703#if defined(UNIX) || defined(VMS)
8704# ifdef FEAT_MOUSE_DEC
8705 "mouse_dec",
8706# endif
8707# ifdef FEAT_MOUSE_GPM
8708 "mouse_gpm",
8709# endif
8710# ifdef FEAT_MOUSE_JSB
8711 "mouse_jsbterm",
8712# endif
8713# ifdef FEAT_MOUSE_NET
8714 "mouse_netterm",
8715# endif
8716# ifdef FEAT_MOUSE_PTERM
8717 "mouse_pterm",
8718# endif
8719# ifdef FEAT_MOUSE_XTERM
8720 "mouse_xterm",
8721# endif
8722#endif
8723#ifdef FEAT_MBYTE
8724 "multi_byte",
8725#endif
8726#ifdef FEAT_MBYTE_IME
8727 "multi_byte_ime",
8728#endif
8729#ifdef FEAT_MULTI_LANG
8730 "multi_lang",
8731#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008732#ifdef FEAT_MZSCHEME
8733 "mzscheme",
8734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735#ifdef FEAT_OLE
8736 "ole",
8737#endif
8738#ifdef FEAT_OSFILETYPE
8739 "osfiletype",
8740#endif
8741#ifdef FEAT_PATH_EXTRA
8742 "path_extra",
8743#endif
8744#ifdef FEAT_PERL
8745#ifndef DYNAMIC_PERL
8746 "perl",
8747#endif
8748#endif
8749#ifdef FEAT_PYTHON
8750#ifndef DYNAMIC_PYTHON
8751 "python",
8752#endif
8753#endif
8754#ifdef FEAT_POSTSCRIPT
8755 "postscript",
8756#endif
8757#ifdef FEAT_PRINTER
8758 "printer",
8759#endif
8760#ifdef FEAT_QUICKFIX
8761 "quickfix",
8762#endif
8763#ifdef FEAT_RIGHTLEFT
8764 "rightleft",
8765#endif
8766#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8767 "ruby",
8768#endif
8769#ifdef FEAT_SCROLLBIND
8770 "scrollbind",
8771#endif
8772#ifdef FEAT_CMDL_INFO
8773 "showcmd",
8774 "cmdline_info",
8775#endif
8776#ifdef FEAT_SIGNS
8777 "signs",
8778#endif
8779#ifdef FEAT_SMARTINDENT
8780 "smartindent",
8781#endif
8782#ifdef FEAT_SNIFF
8783 "sniff",
8784#endif
8785#ifdef FEAT_STL_OPT
8786 "statusline",
8787#endif
8788#ifdef FEAT_SUN_WORKSHOP
8789 "sun_workshop",
8790#endif
8791#ifdef FEAT_NETBEANS_INTG
8792 "netbeans_intg",
8793#endif
8794#ifdef FEAT_SYN_HL
8795 "syntax",
8796#endif
8797#if defined(USE_SYSTEM) || !defined(UNIX)
8798 "system",
8799#endif
8800#ifdef FEAT_TAG_BINS
8801 "tag_binary",
8802#endif
8803#ifdef FEAT_TAG_OLDSTATIC
8804 "tag_old_static",
8805#endif
8806#ifdef FEAT_TAG_ANYWHITE
8807 "tag_any_white",
8808#endif
8809#ifdef FEAT_TCL
8810# ifndef DYNAMIC_TCL
8811 "tcl",
8812# endif
8813#endif
8814#ifdef TERMINFO
8815 "terminfo",
8816#endif
8817#ifdef FEAT_TERMRESPONSE
8818 "termresponse",
8819#endif
8820#ifdef FEAT_TEXTOBJ
8821 "textobjects",
8822#endif
8823#ifdef HAVE_TGETENT
8824 "tgetent",
8825#endif
8826#ifdef FEAT_TITLE
8827 "title",
8828#endif
8829#ifdef FEAT_TOOLBAR
8830 "toolbar",
8831#endif
8832#ifdef FEAT_USR_CMDS
8833 "user-commands", /* was accidentally included in 5.4 */
8834 "user_commands",
8835#endif
8836#ifdef FEAT_VIMINFO
8837 "viminfo",
8838#endif
8839#ifdef FEAT_VERTSPLIT
8840 "vertsplit",
8841#endif
8842#ifdef FEAT_VIRTUALEDIT
8843 "virtualedit",
8844#endif
8845#ifdef FEAT_VISUAL
8846 "visual",
8847#endif
8848#ifdef FEAT_VISUALEXTRA
8849 "visualextra",
8850#endif
8851#ifdef FEAT_VREPLACE
8852 "vreplace",
8853#endif
8854#ifdef FEAT_WILDIGN
8855 "wildignore",
8856#endif
8857#ifdef FEAT_WILDMENU
8858 "wildmenu",
8859#endif
8860#ifdef FEAT_WINDOWS
8861 "windows",
8862#endif
8863#ifdef FEAT_WAK
8864 "winaltkeys",
8865#endif
8866#ifdef FEAT_WRITEBACKUP
8867 "writebackup",
8868#endif
8869#ifdef FEAT_XIM
8870 "xim",
8871#endif
8872#ifdef FEAT_XFONTSET
8873 "xfontset",
8874#endif
8875#ifdef USE_XSMP
8876 "xsmp",
8877#endif
8878#ifdef USE_XSMP_INTERACT
8879 "xsmp_interact",
8880#endif
8881#ifdef FEAT_XCLIPBOARD
8882 "xterm_clipboard",
8883#endif
8884#ifdef FEAT_XTERM_SAVE
8885 "xterm_save",
8886#endif
8887#if defined(UNIX) && defined(FEAT_X11)
8888 "X11",
8889#endif
8890 NULL
8891 };
8892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 for (i = 0; has_list[i] != NULL; ++i)
8895 if (STRICMP(name, has_list[i]) == 0)
8896 {
8897 n = TRUE;
8898 break;
8899 }
8900
8901 if (n == FALSE)
8902 {
8903 if (STRNICMP(name, "patch", 5) == 0)
8904 n = has_patch(atoi((char *)name + 5));
8905 else if (STRICMP(name, "vim_starting") == 0)
8906 n = (starting != 0);
8907#ifdef DYNAMIC_TCL
8908 else if (STRICMP(name, "tcl") == 0)
8909 n = tcl_enabled(FALSE);
8910#endif
8911#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
8912 else if (STRICMP(name, "iconv") == 0)
8913 n = iconv_enabled(FALSE);
8914#endif
8915#ifdef DYNAMIC_RUBY
8916 else if (STRICMP(name, "ruby") == 0)
8917 n = ruby_enabled(FALSE);
8918#endif
8919#ifdef DYNAMIC_PYTHON
8920 else if (STRICMP(name, "python") == 0)
8921 n = python_enabled(FALSE);
8922#endif
8923#ifdef DYNAMIC_PERL
8924 else if (STRICMP(name, "perl") == 0)
8925 n = perl_enabled(FALSE);
8926#endif
8927#ifdef FEAT_GUI
8928 else if (STRICMP(name, "gui_running") == 0)
8929 n = (gui.in_use || gui.starting);
8930# ifdef FEAT_GUI_W32
8931 else if (STRICMP(name, "gui_win32s") == 0)
8932 n = gui_is_win32s();
8933# endif
8934# ifdef FEAT_BROWSE
8935 else if (STRICMP(name, "browse") == 0)
8936 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
8937# endif
8938#endif
8939#ifdef FEAT_SYN_HL
8940 else if (STRICMP(name, "syntax_items") == 0)
8941 n = syntax_present(curbuf);
8942#endif
8943#if defined(WIN3264)
8944 else if (STRICMP(name, "win95") == 0)
8945 n = mch_windows95();
8946#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00008947#ifdef FEAT_NETBEANS_INTG
8948 else if (STRICMP(name, "netbeans_enabled") == 0)
8949 n = usingNetbeans;
8950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
8956/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 * "has_key()" function
8958 */
8959 static void
8960f_has_key(argvars, rettv)
8961 typeval *argvars;
8962 typeval *rettv;
8963{
8964 rettv->vval.v_number = 0;
8965 if (argvars[0].v_type != VAR_DICT)
8966 {
8967 EMSG(_(e_dictreq));
8968 return;
8969 }
8970 if (argvars[0].vval.v_dict == NULL)
8971 return;
8972
8973 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008974 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975}
8976
8977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 * "hasmapto()" function
8979 */
8980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008982 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985 char_u *name;
8986 char_u *mode;
8987 char_u buf[NUMBUFLEN];
8988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 mode = (char_u *)"nvo";
8992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994
8995 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001/*
9002 * "histadd()" function
9003 */
9004/*ARGSUSED*/
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010#ifdef FEAT_CMDHIST
9011 int histype;
9012 char_u *str;
9013 char_u buf[NUMBUFLEN];
9014#endif
9015
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 if (check_restricted() || check_secure())
9018 return;
9019#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 if (histype >= 0)
9022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 if (*str != NUL)
9025 {
9026 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 return;
9029 }
9030 }
9031#endif
9032}
9033
9034/*
9035 * "histdel()" function
9036 */
9037/*ARGSUSED*/
9038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009040 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
9043#ifdef FEAT_CMDHIST
9044 int n;
9045 char_u buf[NUMBUFLEN];
9046
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009047 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009050 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9053 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 else
9055 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9057 get_tv_string_buf(&argvars[1], buf));
9058 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061#endif
9062}
9063
9064/*
9065 * "histget()" function
9066 */
9067/*ARGSUSED*/
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009070 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073#ifdef FEAT_CMDHIST
9074 int type;
9075 int idx;
9076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009078 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 idx = get_history_idx(type);
9080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 idx = (int)get_tv_number(&argvars[1]);
9082 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087}
9088
9089/*
9090 * "histnr()" function
9091 */
9092/*ARGSUSED*/
9093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009095 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 int i;
9099
9100#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 if (i >= HIST_CMD && i < HIST_COUNT)
9103 i = get_history_idx(i);
9104 else
9105#endif
9106 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108}
9109
9110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 * "highlightID(name)" function
9112 */
9113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009115 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119}
9120
9121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009122 * "highlight_exists()" function
9123 */
9124 static void
9125f_hlexists(argvars, rettv)
9126 typeval *argvars;
9127 typeval *rettv;
9128{
9129 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9130}
9131
9132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * "hostname()" function
9134 */
9135/*ARGSUSED*/
9136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009138 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141 char_u hostname[256];
9142
9143 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->v_type = VAR_STRING;
9145 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146}
9147
9148/*
9149 * iconv() function
9150 */
9151/*ARGSUSED*/
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009154 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifdef FEAT_MBYTE
9158 char_u buf1[NUMBUFLEN];
9159 char_u buf2[NUMBUFLEN];
9160 char_u *from, *to, *str;
9161 vimconv_T vimconv;
9162#endif
9163
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->v_type = VAR_STRING;
9165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166
9167#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 str = get_tv_string(&argvars[0]);
9169 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9170 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 vimconv.vc_type = CONV_NONE;
9172 convert_setup(&vimconv, from, to);
9173
9174 /* If the encodings are equal, no conversion needed. */
9175 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
9180 convert_setup(&vimconv, NULL, NULL);
9181 vim_free(from);
9182 vim_free(to);
9183#endif
9184}
9185
9186/*
9187 * "indent()" function
9188 */
9189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009191 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 linenr_T lnum;
9195
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009203/*
9204 * "index()" function
9205 */
9206 static void
9207f_index(argvars, rettv)
9208 typeval *argvars;
9209 typeval *rettv;
9210{
9211 listvar *l;
9212 listitem *item;
9213 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009214 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009215 int ic = FALSE;
9216
9217 rettv->vval.v_number = -1;
9218 if (argvars[0].v_type != VAR_LIST)
9219 {
9220 EMSG(_(e_listreq));
9221 return;
9222 }
9223 l = argvars[0].vval.v_list;
9224 if (l != NULL)
9225 {
9226 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009227 {
9228 min_idx = get_tv_number(&argvars[2]);
9229 if (argvars[3].v_type != VAR_UNKNOWN)
9230 ic = get_tv_number(&argvars[3]);
9231 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009232
9233 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009234 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009235 {
9236 rettv->vval.v_number = idx;
9237 break;
9238 }
9239 }
9240}
9241
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242static int inputsecret_flag = 0;
9243
9244/*
9245 * "input()" function
9246 * Also handles inputsecret() when inputsecret is set.
9247 */
9248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 char_u *p = NULL;
9255 int c;
9256 char_u buf[NUMBUFLEN];
9257 int cmd_silent_save = cmd_silent;
9258
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
9261#ifdef NO_CONSOLE_INPUT
9262 /* While starting up, there is no place to enter text. */
9263 if (no_console_input())
9264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 return;
9267 }
9268#endif
9269
9270 cmd_silent = FALSE; /* Want to see the prompt. */
9271 if (prompt != NULL)
9272 {
9273 /* Only the part of the message after the last NL is considered as
9274 * prompt for the command line */
9275 p = vim_strrchr(prompt, '\n');
9276 if (p == NULL)
9277 p = prompt;
9278 else
9279 {
9280 ++p;
9281 c = *p;
9282 *p = NUL;
9283 msg_start();
9284 msg_clr_eos();
9285 msg_puts_attr(prompt, echo_attr);
9286 msg_didout = FALSE;
9287 msg_starthere();
9288 *p = c;
9289 }
9290 cmdline_row = msg_row;
9291 }
9292
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009293 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9298
9299 /* since the user typed this, no need to wait for return */
9300 need_wait_return = FALSE;
9301 msg_didout = FALSE;
9302 cmd_silent = cmd_silent_save;
9303}
9304
9305/*
9306 * "inputdialog()" function
9307 */
9308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009310 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313#if defined(FEAT_GUI_TEXTDIALOG)
9314 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9315 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9316 {
9317 char_u *message;
9318 char_u buf[NUMBUFLEN];
9319
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009321 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 IObuff[IOSIZE - 1] = NUL;
9325 }
9326 else
9327 IObuff[0] = NUL;
9328 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9329 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 else
9332 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009333 if (argvars[1].v_type != VAR_UNKNOWN
9334 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->vval.v_string = vim_strsave(
9336 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 }
9342 else
9343#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9348
9349/*
9350 * "inputrestore()" function
9351 */
9352/*ARGSUSED*/
9353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009355 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357{
9358 if (ga_userinput.ga_len > 0)
9359 {
9360 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9362 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 }
9365 else if (p_verbose > 1)
9366 {
9367 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 }
9370}
9371
9372/*
9373 * "inputsave()" function
9374 */
9375/*ARGSUSED*/
9376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009378 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 /* Add an entry to the stack of typehead storage. */
9382 if (ga_grow(&ga_userinput, 1) == OK)
9383 {
9384 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9385 + ga_userinput.ga_len);
9386 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 }
9389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391}
9392
9393/*
9394 * "inputsecret()" function
9395 */
9396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009398 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 ++cmdline_star;
9402 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009403 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 --cmdline_star;
9405 --inputsecret_flag;
9406}
9407
9408/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009409 * "insert()" function
9410 */
9411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009413 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009415{
9416 long before = 0;
9417 long n;
9418 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009419 listvar *l;
9420
9421 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009423 else if ((l = argvars[0].vval.v_list) != NULL)
9424 {
9425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009428 n = before;
9429 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009430 if (n > 0)
9431 EMSGN(_(e_listidx), before);
9432 else
9433 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009434 list_insert_tv(l, &argvars[1], item);
9435 ++l->lv_refcount;
9436 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009437 }
9438 }
9439}
9440
9441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 * "isdirectory()" function
9443 */
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009446 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450}
9451
Bram Moolenaar8c711452005-01-14 21:53:12 +00009452static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
9453
9454/*
9455 * Turn a dict into a list:
9456 * "what" == 0: list of keys
9457 * "what" == 1: list of values
9458 * "what" == 2: list of items
9459 */
9460 static void
9461dict_list(argvars, rettv, what)
9462 typeval *argvars;
9463 typeval *rettv;
9464 int what;
9465{
9466 listvar *l;
9467 listvar *l2;
9468 dictitem *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009469 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009470 listitem *li;
9471 listitem *li2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009472 dictvar *d;
9473 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009474
9475 rettv->vval.v_number = 0;
9476 if (argvars[0].v_type != VAR_DICT)
9477 {
9478 EMSG(_(e_dictreq));
9479 return;
9480 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009481 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009482 return;
9483
9484 l = list_alloc();
9485 if (l == NULL)
9486 return;
9487 rettv->v_type = VAR_LIST;
9488 rettv->vval.v_list = l;
9489 ++l->lv_refcount;
9490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009491 todo = d->dv_hashtable.ht_used;
9492 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009494 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00009495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009496 --todo;
9497 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009498
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009499 li = listitem_alloc();
9500 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009501 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009502 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009503
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009504 if (what == 0)
9505 {
9506 /* keys() */
9507 li->li_tv.v_type = VAR_STRING;
9508 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9509 }
9510 else if (what == 1)
9511 {
9512 /* values() */
9513 copy_tv(&di->di_tv, &li->li_tv);
9514 }
9515 else
9516 {
9517 /* items() */
9518 l2 = list_alloc();
9519 li->li_tv.v_type = VAR_LIST;
9520 li->li_tv.vval.v_list = l2;
9521 if (l2 == NULL)
9522 break;
9523 ++l2->lv_refcount;
9524
9525 li2 = listitem_alloc();
9526 if (li2 == NULL)
9527 break;
9528 list_append(l2, li2);
9529 li2->li_tv.v_type = VAR_STRING;
9530 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9531
9532 li2 = listitem_alloc();
9533 if (li2 == NULL)
9534 break;
9535 list_append(l2, li2);
9536 copy_tv(&di->di_tv, &li2->li_tv);
9537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00009538 }
9539 }
9540}
9541
9542/*
9543 * "items(dict)" function
9544 */
9545 static void
9546f_items(argvars, rettv)
9547 typeval *argvars;
9548 typeval *rettv;
9549{
9550 dict_list(argvars, rettv, 2);
9551}
9552
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009554 * "join()" function
9555 */
9556 static void
9557f_join(argvars, rettv)
9558 typeval *argvars;
9559 typeval *rettv;
9560{
9561 garray_T ga;
9562 char_u *sep;
9563
9564 rettv->vval.v_number = 0;
9565 if (argvars[0].v_type != VAR_LIST)
9566 {
9567 EMSG(_(e_listreq));
9568 return;
9569 }
9570 if (argvars[0].vval.v_list == NULL)
9571 return;
9572 if (argvars[1].v_type == VAR_UNKNOWN)
9573 sep = (char_u *)" ";
9574 else
9575 sep = get_tv_string(&argvars[1]);
9576
9577 ga_init2(&ga, (int)sizeof(char), 80);
9578 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9579 ga_append(&ga, NUL);
9580
9581 rettv->v_type = VAR_STRING;
9582 rettv->vval.v_string = (char_u *)ga.ga_data;
9583}
9584
9585/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009586 * "keys()" function
9587 */
9588 static void
9589f_keys(argvars, rettv)
9590 typeval *argvars;
9591 typeval *rettv;
9592{
9593 dict_list(argvars, rettv, 0);
9594}
9595
9596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 * "last_buffer_nr()" function.
9598 */
9599/*ARGSUSED*/
9600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009602 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 int n = 0;
9606 buf_T *buf;
9607
9608 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9609 if (n < buf->b_fnum)
9610 n = buf->b_fnum;
9611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613}
9614
9615/*
9616 * "len()" function
9617 */
9618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009620 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009622{
9623 switch (argvars[0].v_type)
9624 {
9625 case VAR_STRING:
9626 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_number = (varnumber_T)STRLEN(
9628 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009629 break;
9630 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009632 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 case VAR_DICT:
9634 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9635 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009636 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009637 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009638 break;
9639 }
9640}
9641
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009643
9644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009646 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009648 int type;
9649{
9650#ifdef FEAT_LIBCALL
9651 char_u *string_in;
9652 char_u **string_result;
9653 int nr_result;
9654#endif
9655
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009657 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009661
9662 if (check_restricted() || check_secure())
9663 return;
9664
9665#ifdef FEAT_LIBCALL
9666 /* The first two args must be strings, otherwise its meaningless */
9667 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9668 {
9669 if (argvars[2].v_type == VAR_NUMBER)
9670 string_in = NULL;
9671 else
9672 string_in = argvars[2].vval.v_string;
9673 if (type == VAR_NUMBER)
9674 string_result = NULL;
9675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009677 if (mch_libcall(argvars[0].vval.v_string,
9678 argvars[1].vval.v_string,
9679 string_in,
9680 argvars[2].vval.v_number,
9681 string_result,
9682 &nr_result) == OK
9683 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009685 }
9686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687}
9688
9689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009690 * "libcall()" function
9691 */
9692 static void
9693f_libcall(argvars, rettv)
9694 typeval *argvars;
9695 typeval *rettv;
9696{
9697 libcall_common(argvars, rettv, VAR_STRING);
9698}
9699
9700/*
9701 * "libcallnr()" function
9702 */
9703 static void
9704f_libcallnr(argvars, rettv)
9705 typeval *argvars;
9706 typeval *rettv;
9707{
9708 libcall_common(argvars, rettv, VAR_NUMBER);
9709}
9710
9711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 * "line(string)" function
9713 */
9714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009716 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 linenr_T lnum = 0;
9720 pos_T *fp;
9721
9722 fp = var2fpos(&argvars[0], TRUE);
9723 if (fp != NULL)
9724 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
9728/*
9729 * "line2byte(lnum)" function
9730 */
9731/*ARGSUSED*/
9732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009734 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
9737#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#else
9740 linenr_T lnum;
9741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
9747 if (rettv->vval.v_number >= 0)
9748 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
9750}
9751
9752/*
9753 * "lispindent(lnum)" function
9754 */
9755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009757 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760#ifdef FEAT_LISP
9761 pos_T pos;
9762 linenr_T lnum;
9763
9764 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9767 {
9768 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 curwin->w_cursor = pos;
9771 }
9772 else
9773#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775}
9776
9777/*
9778 * "localtime()" function
9779 */
9780/*ARGSUSED*/
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009783 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
Bram Moolenaar0d660222005-01-07 21:51:51 +00009789static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009793 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 int exact;
9796{
9797 char_u *keys;
9798 char_u *which;
9799 char_u buf[NUMBUFLEN];
9800 char_u *keys_buf = NULL;
9801 char_u *rhs;
9802 int mode;
9803 garray_T ga;
9804
9805 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 rettv->v_type = VAR_STRING;
9807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 if (*keys == NUL)
9811 return;
9812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009813 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 else
9816 which = (char_u *)"";
9817 mode = get_map_mode(&which, 0);
9818
9819 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9820 rhs = check_map(keys, mode, exact);
9821 vim_free(keys_buf);
9822 if (rhs != NULL)
9823 {
9824 ga_init(&ga);
9825 ga.ga_itemsize = 1;
9826 ga.ga_growsize = 40;
9827
9828 while (*rhs != NUL)
9829 ga_concat(&ga, str2special(&rhs, FALSE));
9830
9831 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 }
9834}
9835
9836/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009837 * "map()" function
9838 */
9839 static void
9840f_map(argvars, rettv)
9841 typeval *argvars;
9842 typeval *rettv;
9843{
9844 filter_map(argvars, rettv, TRUE);
9845}
9846
9847/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009848 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 */
9850 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009851f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009852 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009855 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009859 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 */
9861 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009862f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009863 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009866 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
Bram Moolenaar0d660222005-01-07 21:51:51 +00009869static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
9871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009873 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 int type;
9876{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009877 char_u *str = NULL;
9878 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 char_u *pat;
9880 regmatch_T regmatch;
9881 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009882 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 char_u *save_cpo;
9884 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009885 long nth = 1;
9886 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009887 listvar *l = NULL;
9888 listitem *li = NULL;
9889 long idx = 0;
9890 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891
9892 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9893 save_cpo = p_cpo;
9894 p_cpo = (char_u *)"";
9895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 if (type == 2)
9897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 }
9901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009904 if (argvars[0].v_type == VAR_LIST)
9905 {
9906 if ((l = argvars[0].vval.v_list) == NULL)
9907 goto theend;
9908 li = l->lv_first;
9909 }
9910 else
9911 expr = str = get_tv_string(&argvars[0]);
9912
9913 pat = get_tv_string_buf(&argvars[1], patbuf);
9914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009918 if (l != NULL)
9919 {
9920 li = list_find(l, start);
9921 if (li == NULL)
9922 goto theend;
9923 if (start < 0)
9924 {
9925 listitem *ni;
9926
9927 /* Need to compute the index. */
9928 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
9929 ++idx;
9930 }
9931 else
9932 idx = start;
9933 }
9934 else
9935 {
9936 if (start < 0)
9937 start = 0;
9938 if (start > (long)STRLEN(str))
9939 goto theend;
9940 str += start;
9941 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009942
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009943 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 }
9946
9947 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9948 if (regmatch.regprog != NULL)
9949 {
9950 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009951
9952 while (1)
9953 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954 if (l != NULL)
9955 {
9956 if (li == NULL)
9957 {
9958 match = FALSE;
9959 break;
9960 }
9961 str = echo_string(&li->li_tv, &tofree, strbuf);
9962 }
9963
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009964 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009965
9966 if (l != NULL)
9967 vim_free(tofree);
9968 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009969 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009970 if (l == NULL && !match)
9971 break;
9972
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009973 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009974 if (l != NULL)
9975 {
9976 li = li->li_next;
9977 ++idx;
9978 }
9979 else
9980 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009981#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009982 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009983#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009984 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009985#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009986 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009987 }
9988
9989 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 {
9991 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009992 {
9993 if (l != NULL)
9994 copy_tv(&li->li_tv, rettv);
9995 else
9996 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009998 }
9999 else if (l != NULL)
10000 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 else
10002 {
10003 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 (varnumber_T)(regmatch.startp[0] - str);
10006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011 }
10012 vim_free(regmatch.regprog);
10013 }
10014
10015theend:
10016 p_cpo = save_cpo;
10017}
10018
10019/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010020 * "match()" function
10021 */
10022 static void
10023f_match(argvars, rettv)
10024 typeval *argvars;
10025 typeval *rettv;
10026{
10027 find_some_match(argvars, rettv, 1);
10028}
10029
10030/*
10031 * "matchend()" function
10032 */
10033 static void
10034f_matchend(argvars, rettv)
10035 typeval *argvars;
10036 typeval *rettv;
10037{
10038 find_some_match(argvars, rettv, 0);
10039}
10040
10041/*
10042 * "matchstr()" function
10043 */
10044 static void
10045f_matchstr(argvars, rettv)
10046 typeval *argvars;
10047 typeval *rettv;
10048{
10049 find_some_match(argvars, rettv, 2);
10050}
10051
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010052static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
10053
10054 static void
10055max_min(argvars, rettv, domax)
10056 typeval *argvars;
10057 typeval *rettv;
10058 int domax;
10059{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010060 long n = 0;
10061 long i;
10062
10063 if (argvars[0].v_type == VAR_LIST)
10064 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010065 listvar *l;
10066 listitem *li;
10067
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010068 l = argvars[0].vval.v_list;
10069 if (l != NULL)
10070 {
10071 li = l->lv_first;
10072 if (li != NULL)
10073 {
10074 n = get_tv_number(&li->li_tv);
10075 while (1)
10076 {
10077 li = li->li_next;
10078 if (li == NULL)
10079 break;
10080 i = get_tv_number(&li->li_tv);
10081 if (domax ? i > n : i < n)
10082 n = i;
10083 }
10084 }
10085 }
10086 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010087 else if (argvars[0].v_type == VAR_DICT)
10088 {
10089 dictvar *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010090 int first = TRUE;
10091 hashitem *hi;
10092 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010093
10094 d = argvars[0].vval.v_dict;
10095 if (d != NULL)
10096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010097 todo = d->dv_hashtable.ht_used;
10098 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010100 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010102 --todo;
10103 i = get_tv_number(&HI2DI(hi)->di_tv);
10104 if (first)
10105 {
10106 n = i;
10107 first = FALSE;
10108 }
10109 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010110 n = i;
10111 }
10112 }
10113 }
10114 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010115 else
10116 EMSG(_(e_listreq));
10117 rettv->vval.v_number = n;
10118}
10119
10120/*
10121 * "max()" function
10122 */
10123 static void
10124f_max(argvars, rettv)
10125 typeval *argvars;
10126 typeval *rettv;
10127{
10128 max_min(argvars, rettv, TRUE);
10129}
10130
10131/*
10132 * "min()" function
10133 */
10134 static void
10135f_min(argvars, rettv)
10136 typeval *argvars;
10137 typeval *rettv;
10138{
10139 max_min(argvars, rettv, FALSE);
10140}
10141
Bram Moolenaar0d660222005-01-07 21:51:51 +000010142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 * "mode()" function
10144 */
10145/*ARGSUSED*/
10146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010148 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 char_u buf[2];
10152
10153#ifdef FEAT_VISUAL
10154 if (VIsual_active)
10155 {
10156 if (VIsual_select)
10157 buf[0] = VIsual_mode + 's' - 'v';
10158 else
10159 buf[0] = VIsual_mode;
10160 }
10161 else
10162#endif
10163 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10164 buf[0] = 'r';
10165 else if (State & INSERT)
10166 {
10167 if (State & REPLACE_FLAG)
10168 buf[0] = 'R';
10169 else
10170 buf[0] = 'i';
10171 }
10172 else if (State & CMDLINE)
10173 buf[0] = 'c';
10174 else
10175 buf[0] = 'n';
10176
10177 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 rettv->vval.v_string = vim_strsave(buf);
10179 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180}
10181
10182/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010183 * "nextnonblank()" function
10184 */
10185 static void
10186f_nextnonblank(argvars, rettv)
10187 typeval *argvars;
10188 typeval *rettv;
10189{
10190 linenr_T lnum;
10191
10192 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10193 {
10194 if (lnum > curbuf->b_ml.ml_line_count)
10195 {
10196 lnum = 0;
10197 break;
10198 }
10199 if (*skipwhite(ml_get(lnum)) != NUL)
10200 break;
10201 }
10202 rettv->vval.v_number = lnum;
10203}
10204
10205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 * "nr2char()" function
10207 */
10208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010210 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212{
10213 char_u buf[NUMBUFLEN];
10214
10215#ifdef FEAT_MBYTE
10216 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 else
10219#endif
10220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 buf[1] = NUL;
10223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224 rettv->v_type = VAR_STRING;
10225 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010226}
10227
10228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010229 * "prevnonblank()" function
10230 */
10231 static void
10232f_prevnonblank(argvars, rettv)
10233 typeval *argvars;
10234 typeval *rettv;
10235{
10236 linenr_T lnum;
10237
10238 lnum = get_tv_lnum(argvars);
10239 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10240 lnum = 0;
10241 else
10242 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10243 --lnum;
10244 rettv->vval.v_number = lnum;
10245}
10246
Bram Moolenaar8c711452005-01-14 21:53:12 +000010247/*
10248 * "range()" function
10249 */
10250 static void
10251f_range(argvars, rettv)
10252 typeval *argvars;
10253 typeval *rettv;
10254{
10255 long start;
10256 long end;
10257 long stride = 1;
10258 long i;
10259 listvar *l;
10260 listitem *li;
10261
10262 start = get_tv_number(&argvars[0]);
10263 if (argvars[1].v_type == VAR_UNKNOWN)
10264 {
10265 end = start - 1;
10266 start = 0;
10267 }
10268 else
10269 {
10270 end = get_tv_number(&argvars[1]);
10271 if (argvars[2].v_type != VAR_UNKNOWN)
10272 stride = get_tv_number(&argvars[2]);
10273 }
10274
10275 rettv->vval.v_number = 0;
10276 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010277 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010278 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010279 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010280 else
10281 {
10282 l = list_alloc();
10283 if (l != NULL)
10284 {
10285 rettv->v_type = VAR_LIST;
10286 rettv->vval.v_list = l;
10287 ++l->lv_refcount;
10288
10289 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10290 {
10291 li = listitem_alloc();
10292 if (li == NULL)
10293 break;
10294 li->li_tv.v_type = VAR_NUMBER;
10295 li->li_tv.vval.v_number = i;
10296 list_append(l, li);
10297 }
10298 }
10299 }
10300}
10301
Bram Moolenaar0d660222005-01-07 21:51:51 +000010302#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10303static void make_connection __ARGS((void));
10304static int check_connection __ARGS((void));
10305
10306 static void
10307make_connection()
10308{
10309 if (X_DISPLAY == NULL
10310# ifdef FEAT_GUI
10311 && !gui.in_use
10312# endif
10313 )
10314 {
10315 x_force_connect = TRUE;
10316 setup_term_clip();
10317 x_force_connect = FALSE;
10318 }
10319}
10320
10321 static int
10322check_connection()
10323{
10324 make_connection();
10325 if (X_DISPLAY == NULL)
10326 {
10327 EMSG(_("E240: No connection to Vim server"));
10328 return FAIL;
10329 }
10330 return OK;
10331}
10332#endif
10333
10334#ifdef FEAT_CLIENTSERVER
10335static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
10336
10337 static void
10338remote_common(argvars, rettv, expr)
10339 typeval *argvars;
10340 typeval *rettv;
10341 int expr;
10342{
10343 char_u *server_name;
10344 char_u *keys;
10345 char_u *r = NULL;
10346 char_u buf[NUMBUFLEN];
10347# ifdef WIN32
10348 HWND w;
10349# else
10350 Window w;
10351# endif
10352
10353 if (check_restricted() || check_secure())
10354 return;
10355
10356# ifdef FEAT_X11
10357 if (check_connection() == FAIL)
10358 return;
10359# endif
10360
10361 server_name = get_tv_string(&argvars[0]);
10362 keys = get_tv_string_buf(&argvars[1], buf);
10363# ifdef WIN32
10364 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10365# else
10366 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10367 < 0)
10368# endif
10369 {
10370 if (r != NULL)
10371 EMSG(r); /* sending worked but evaluation failed */
10372 else
10373 EMSG2(_("E241: Unable to send to %s"), server_name);
10374 return;
10375 }
10376
10377 rettv->vval.v_string = r;
10378
10379 if (argvars[2].v_type != VAR_UNKNOWN)
10380 {
10381 var v;
10382 char_u str[30];
10383
10384 sprintf((char *)str, "0x%x", (unsigned int)w);
10385 v.tv.v_type = VAR_STRING;
10386 v.tv.vval.v_string = vim_strsave(str);
10387 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
10388 vim_free(v.tv.vval.v_string);
10389 }
10390}
10391#endif
10392
10393/*
10394 * "remote_expr()" function
10395 */
10396/*ARGSUSED*/
10397 static void
10398f_remote_expr(argvars, rettv)
10399 typeval *argvars;
10400 typeval *rettv;
10401{
10402 rettv->v_type = VAR_STRING;
10403 rettv->vval.v_string = NULL;
10404#ifdef FEAT_CLIENTSERVER
10405 remote_common(argvars, rettv, TRUE);
10406#endif
10407}
10408
10409/*
10410 * "remote_foreground()" function
10411 */
10412/*ARGSUSED*/
10413 static void
10414f_remote_foreground(argvars, rettv)
10415 typeval *argvars;
10416 typeval *rettv;
10417{
10418 rettv->vval.v_number = 0;
10419#ifdef FEAT_CLIENTSERVER
10420# ifdef WIN32
10421 /* On Win32 it's done in this application. */
10422 serverForeground(get_tv_string(&argvars[0]));
10423# else
10424 /* Send a foreground() expression to the server. */
10425 argvars[1].v_type = VAR_STRING;
10426 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10427 argvars[2].v_type = VAR_UNKNOWN;
10428 remote_common(argvars, rettv, TRUE);
10429 vim_free(argvars[1].vval.v_string);
10430# endif
10431#endif
10432}
10433
10434/*ARGSUSED*/
10435 static void
10436f_remote_peek(argvars, rettv)
10437 typeval *argvars;
10438 typeval *rettv;
10439{
10440#ifdef FEAT_CLIENTSERVER
10441 var v;
10442 char_u *s = NULL;
10443# ifdef WIN32
10444 int n = 0;
10445# endif
10446
10447 if (check_restricted() || check_secure())
10448 {
10449 rettv->vval.v_number = -1;
10450 return;
10451 }
10452# ifdef WIN32
10453 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10454 if (n == 0)
10455 rettv->vval.v_number = -1;
10456 else
10457 {
10458 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10459 rettv->vval.v_number = (s != NULL);
10460 }
10461# else
10462 rettv->vval.v_number = 0;
10463 if (check_connection() == FAIL)
10464 return;
10465
10466 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10467 serverStrToWin(get_tv_string(&argvars[0])), &s);
10468# endif
10469
10470 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10471 {
10472 v.tv.v_type = VAR_STRING;
10473 v.tv.vval.v_string = vim_strsave(s);
10474 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
10475 vim_free(v.tv.vval.v_string);
10476 }
10477#else
10478 rettv->vval.v_number = -1;
10479#endif
10480}
10481
10482/*ARGSUSED*/
10483 static void
10484f_remote_read(argvars, rettv)
10485 typeval *argvars;
10486 typeval *rettv;
10487{
10488 char_u *r = NULL;
10489
10490#ifdef FEAT_CLIENTSERVER
10491 if (!check_restricted() && !check_secure())
10492 {
10493# ifdef WIN32
10494 /* The server's HWND is encoded in the 'id' parameter */
10495 int n = 0;
10496
10497 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10498 if (n != 0)
10499 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10500 if (r == NULL)
10501# else
10502 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10503 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10504# endif
10505 EMSG(_("E277: Unable to read a server reply"));
10506 }
10507#endif
10508 rettv->v_type = VAR_STRING;
10509 rettv->vval.v_string = r;
10510}
10511
10512/*
10513 * "remote_send()" function
10514 */
10515/*ARGSUSED*/
10516 static void
10517f_remote_send(argvars, rettv)
10518 typeval *argvars;
10519 typeval *rettv;
10520{
10521 rettv->v_type = VAR_STRING;
10522 rettv->vval.v_string = NULL;
10523#ifdef FEAT_CLIENTSERVER
10524 remote_common(argvars, rettv, FALSE);
10525#endif
10526}
10527
10528/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010529 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010530 */
10531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010533 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010535{
10536 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010537 listitem *item, *item2;
10538 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010539 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010540 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010541 char_u *key;
10542 dictvar *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010543 dictitem *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010544
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010545 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010546 if (argvars[0].v_type == VAR_DICT)
10547 {
10548 if (argvars[2].v_type != VAR_UNKNOWN)
10549 EMSG2(_(e_toomanyarg), "remove()");
10550 else if ((d = argvars[0].vval.v_dict) != NULL)
10551 {
10552 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010553 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010554 if (di == NULL)
10555 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010556 else
10557 {
10558 *rettv = di->di_tv;
10559 init_tv(&di->di_tv);
10560 dictitem_remove(d, di);
10561 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010562 }
10563 }
10564 else if (argvars[0].v_type != VAR_LIST)
10565 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010566 else if ((l = argvars[0].vval.v_list) != NULL)
10567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010569 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010570 if (item == NULL)
10571 EMSGN(_(e_listidx), idx);
10572 else
10573 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010574 if (argvars[2].v_type == VAR_UNKNOWN)
10575 {
10576 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010577 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010578 *rettv = item->li_tv;
10579 vim_free(item);
10580 }
10581 else
10582 {
10583 /* Remove range of items, return list with values. */
10584 end = get_tv_number(&argvars[2]);
10585 item2 = list_find(l, end);
10586 if (item2 == NULL)
10587 EMSGN(_(e_listidx), end);
10588 else
10589 {
10590 for (li = item; li != item2 && li != NULL; li = li->li_next)
10591 ;
10592 if (li == NULL) /* didn't find "item2" after "item" */
10593 EMSG(_(e_invrange));
10594 else
10595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010596 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010597 l = list_alloc();
10598 if (l != NULL)
10599 {
10600 rettv->v_type = VAR_LIST;
10601 rettv->vval.v_list = l;
10602 l->lv_first = item;
10603 l->lv_last = item2;
10604 l->lv_refcount = 1;
10605 item->li_prev = NULL;
10606 item2->li_next = NULL;
10607 }
10608 }
10609 }
10610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010611 }
10612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613}
10614
10615/*
10616 * "rename({from}, {to})" function
10617 */
10618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010620 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 char_u buf[NUMBUFLEN];
10624
10625 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10629 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630}
10631
10632/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010633 * "repeat()" function
10634 */
10635/*ARGSUSED*/
10636 static void
10637f_repeat(argvars, rettv)
10638 typeval *argvars;
10639 typeval *rettv;
10640{
10641 char_u *p;
10642 int n;
10643 int slen;
10644 int len;
10645 char_u *r;
10646 int i;
10647 listvar *l;
10648
10649 n = get_tv_number(&argvars[1]);
10650 if (argvars[0].v_type == VAR_LIST)
10651 {
10652 l = list_alloc();
10653 if (l != NULL && argvars[0].vval.v_list != NULL)
10654 {
10655 l->lv_refcount = 1;
10656 while (n-- > 0)
10657 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
10658 break;
10659 }
10660 rettv->v_type = VAR_LIST;
10661 rettv->vval.v_list = l;
10662 }
10663 else
10664 {
10665 p = get_tv_string(&argvars[0]);
10666 rettv->v_type = VAR_STRING;
10667 rettv->vval.v_string = NULL;
10668
10669 slen = (int)STRLEN(p);
10670 len = slen * n;
10671 if (len <= 0)
10672 return;
10673
10674 r = alloc(len + 1);
10675 if (r != NULL)
10676 {
10677 for (i = 0; i < n; i++)
10678 mch_memmove(r + i * slen, p, (size_t)slen);
10679 r[len] = NUL;
10680 }
10681
10682 rettv->vval.v_string = r;
10683 }
10684}
10685
10686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 * "resolve()" function
10688 */
10689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010691 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
10694 char_u *p;
10695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697#ifdef FEAT_SHORTCUT
10698 {
10699 char_u *v = NULL;
10700
10701 v = mch_resolve_shortcut(p);
10702 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010705 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 }
10707#else
10708# ifdef HAVE_READLINK
10709 {
10710 char_u buf[MAXPATHL + 1];
10711 char_u *cpy;
10712 int len;
10713 char_u *remain = NULL;
10714 char_u *q;
10715 int is_relative_to_current = FALSE;
10716 int has_trailing_pathsep = FALSE;
10717 int limit = 100;
10718
10719 p = vim_strsave(p);
10720
10721 if (p[0] == '.' && (vim_ispathsep(p[1])
10722 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10723 is_relative_to_current = TRUE;
10724
10725 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010726 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 has_trailing_pathsep = TRUE;
10728
10729 q = getnextcomp(p);
10730 if (*q != NUL)
10731 {
10732 /* Separate the first path component in "p", and keep the
10733 * remainder (beginning with the path separator). */
10734 remain = vim_strsave(q - 1);
10735 q[-1] = NUL;
10736 }
10737
10738 for (;;)
10739 {
10740 for (;;)
10741 {
10742 len = readlink((char *)p, (char *)buf, MAXPATHL);
10743 if (len <= 0)
10744 break;
10745 buf[len] = NUL;
10746
10747 if (limit-- == 0)
10748 {
10749 vim_free(p);
10750 vim_free(remain);
10751 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 goto fail;
10754 }
10755
10756 /* Ensure that the result will have a trailing path separator
10757 * if the argument has one. */
10758 if (remain == NULL && has_trailing_pathsep)
10759 add_pathsep(buf);
10760
10761 /* Separate the first path component in the link value and
10762 * concatenate the remainders. */
10763 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10764 if (*q != NUL)
10765 {
10766 if (remain == NULL)
10767 remain = vim_strsave(q - 1);
10768 else
10769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010770 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 if (cpy != NULL)
10772 {
10773 STRCAT(cpy, remain);
10774 vim_free(remain);
10775 remain = cpy;
10776 }
10777 }
10778 q[-1] = NUL;
10779 }
10780
10781 q = gettail(p);
10782 if (q > p && *q == NUL)
10783 {
10784 /* Ignore trailing path separator. */
10785 q[-1] = NUL;
10786 q = gettail(p);
10787 }
10788 if (q > p && !mch_isFullName(buf))
10789 {
10790 /* symlink is relative to directory of argument */
10791 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10792 if (cpy != NULL)
10793 {
10794 STRCPY(cpy, p);
10795 STRCPY(gettail(cpy), buf);
10796 vim_free(p);
10797 p = cpy;
10798 }
10799 }
10800 else
10801 {
10802 vim_free(p);
10803 p = vim_strsave(buf);
10804 }
10805 }
10806
10807 if (remain == NULL)
10808 break;
10809
10810 /* Append the first path component of "remain" to "p". */
10811 q = getnextcomp(remain + 1);
10812 len = q - remain - (*q != NUL);
10813 cpy = vim_strnsave(p, STRLEN(p) + len);
10814 if (cpy != NULL)
10815 {
10816 STRNCAT(cpy, remain, len);
10817 vim_free(p);
10818 p = cpy;
10819 }
10820 /* Shorten "remain". */
10821 if (*q != NUL)
10822 STRCPY(remain, q - 1);
10823 else
10824 {
10825 vim_free(remain);
10826 remain = NULL;
10827 }
10828 }
10829
10830 /* If the result is a relative path name, make it explicitly relative to
10831 * the current directory if and only if the argument had this form. */
10832 if (!vim_ispathsep(*p))
10833 {
10834 if (is_relative_to_current
10835 && *p != NUL
10836 && !(p[0] == '.'
10837 && (p[1] == NUL
10838 || vim_ispathsep(p[1])
10839 || (p[1] == '.'
10840 && (p[2] == NUL
10841 || vim_ispathsep(p[2]))))))
10842 {
10843 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010844 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 if (cpy != NULL)
10846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 vim_free(p);
10848 p = cpy;
10849 }
10850 }
10851 else if (!is_relative_to_current)
10852 {
10853 /* Strip leading "./". */
10854 q = p;
10855 while (q[0] == '.' && vim_ispathsep(q[1]))
10856 q += 2;
10857 if (q > p)
10858 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10859 }
10860 }
10861
10862 /* Ensure that the result will have no trailing path separator
10863 * if the argument had none. But keep "/" or "//". */
10864 if (!has_trailing_pathsep)
10865 {
10866 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010867 if (after_pathsep(p, q))
10868 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 }
10870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 }
10873# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875# endif
10876#endif
10877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879
10880#ifdef HAVE_READLINK
10881fail:
10882#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884}
10885
10886/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010887 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 */
10889 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010890f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010891 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894 listvar *l;
10895 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897 rettv->vval.v_number = 0;
10898 if (argvars[0].v_type != VAR_LIST)
10899 EMSG2(_(e_listarg), "reverse()");
10900 else if ((l = argvars[0].vval.v_list) != NULL)
10901 {
10902 li = l->lv_last;
10903 l->lv_first = l->lv_last = li;
10904 while (li != NULL)
10905 {
10906 ni = li->li_prev;
10907 list_append(l, li);
10908 li = ni;
10909 }
10910 rettv->vval.v_list = l;
10911 rettv->v_type = VAR_LIST;
10912 ++l->lv_refcount;
10913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914}
10915
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010916#define SP_NOMOVE 1 /* don't move cursor */
10917#define SP_REPEAT 2 /* repeat to find outer pair */
10918#define SP_RETCOUNT 4 /* return matchcount */
10919
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920static int get_search_arg __ARGS((typeval *varp, int *flagsp));
10921
10922/*
10923 * Get flags for a search function.
10924 * Possibly sets "p_ws".
10925 * Returns BACKWARD, FORWARD or zero (for an error).
10926 */
10927 static int
10928get_search_arg(varp, flagsp)
10929 typeval *varp;
10930 int *flagsp;
10931{
10932 int dir = FORWARD;
10933 char_u *flags;
10934 char_u nbuf[NUMBUFLEN];
10935 int mask;
10936
10937 if (varp->v_type != VAR_UNKNOWN)
10938 {
10939 flags = get_tv_string_buf(varp, nbuf);
10940 while (*flags != NUL)
10941 {
10942 switch (*flags)
10943 {
10944 case 'b': dir = BACKWARD; break;
10945 case 'w': p_ws = TRUE; break;
10946 case 'W': p_ws = FALSE; break;
10947 default: mask = 0;
10948 if (flagsp != NULL)
10949 switch (*flags)
10950 {
10951 case 'n': mask = SP_NOMOVE; break;
10952 case 'r': mask = SP_REPEAT; break;
10953 case 'm': mask = SP_RETCOUNT; break;
10954 }
10955 if (mask == 0)
10956 {
10957 EMSG2(_(e_invarg2), flags);
10958 dir = 0;
10959 }
10960 else
10961 *flagsp |= mask;
10962 }
10963 if (dir == 0)
10964 break;
10965 ++flags;
10966 }
10967 }
10968 return dir;
10969}
10970
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971/*
10972 * "search()" function
10973 */
10974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010976 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978{
10979 char_u *pat;
10980 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010981 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 int save_p_ws = p_ws;
10983 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010984 int flags = 0;
10985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010989 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
10990 if (dir == 0)
10991 goto theend;
10992 if ((flags & ~SP_NOMOVE) != 0)
10993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010995 goto theend;
10996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010998 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11000 SEARCH_KEEP, RE_SEARCH) != FAIL)
11001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 curwin->w_cursor = pos;
11004 /* "/$" will put the cursor after the end of the line, may need to
11005 * correct that here */
11006 check_cursor();
11007 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011008
11009 /* If 'n' flag is used: restore cursor position. */
11010 if (flags & SP_NOMOVE)
11011 curwin->w_cursor = save_cursor;
11012theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 p_ws = save_p_ws;
11014}
11015
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016/*
11017 * "searchpair()" function
11018 */
11019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011021 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024 char_u *spat, *mpat, *epat;
11025 char_u *skip;
11026 char_u *pat, *pat2, *pat3;
11027 pos_T pos;
11028 pos_T firstpos;
11029 pos_T save_cursor;
11030 pos_T save_pos;
11031 int save_p_ws = p_ws;
11032 char_u *save_cpo;
11033 int dir;
11034 int flags = 0;
11035 char_u nbuf1[NUMBUFLEN];
11036 char_u nbuf2[NUMBUFLEN];
11037 char_u nbuf3[NUMBUFLEN];
11038 int n;
11039 int r;
11040 int nest = 1;
11041 int err;
11042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044
11045 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11046 save_cpo = p_cpo;
11047 p_cpo = (char_u *)"";
11048
11049 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 spat = get_tv_string(&argvars[0]);
11051 mpat = get_tv_string_buf(&argvars[1], nbuf1);
11052 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053
11054 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11055 * start/middle/end (pat3, for the top pair). */
11056 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
11057 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
11058 if (pat2 == NULL || pat3 == NULL)
11059 goto theend;
11060 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
11061 if (*mpat == NUL)
11062 STRCPY(pat3, pat2);
11063 else
11064 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11065 spat, epat, mpat);
11066
11067 /* Handle the optional fourth argument: flags */
11068 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011069 if (dir == 0)
11070 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071
11072 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011073 if (argvars[3].v_type == VAR_UNKNOWN
11074 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 skip = (char_u *)"";
11076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
11079 save_cursor = curwin->w_cursor;
11080 pos = curwin->w_cursor;
11081 firstpos.lnum = 0;
11082 pat = pat3;
11083 for (;;)
11084 {
11085 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11086 SEARCH_KEEP, RE_SEARCH);
11087 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11088 /* didn't find it or found the first match again: FAIL */
11089 break;
11090
11091 if (firstpos.lnum == 0)
11092 firstpos = pos;
11093
11094 /* If the skip pattern matches, ignore this match. */
11095 if (*skip != NUL)
11096 {
11097 save_pos = curwin->w_cursor;
11098 curwin->w_cursor = pos;
11099 r = eval_to_bool(skip, &err, NULL, FALSE);
11100 curwin->w_cursor = save_pos;
11101 if (err)
11102 {
11103 /* Evaluating {skip} caused an error, break here. */
11104 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 break;
11107 }
11108 if (r)
11109 continue;
11110 }
11111
11112 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11113 {
11114 /* Found end when searching backwards or start when searching
11115 * forward: nested pair. */
11116 ++nest;
11117 pat = pat2; /* nested, don't search for middle */
11118 }
11119 else
11120 {
11121 /* Found end when searching forward or start when searching
11122 * backward: end of (nested) pair; or found middle in outer pair. */
11123 if (--nest == 1)
11124 pat = pat3; /* outer level, search for middle */
11125 }
11126
11127 if (nest == 0)
11128 {
11129 /* Found the match: return matchcount or line number. */
11130 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 curwin->w_cursor = pos;
11135 if (!(flags & SP_REPEAT))
11136 break;
11137 nest = 1; /* search for next unmatched */
11138 }
11139 }
11140
11141 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 curwin->w_cursor = save_cursor;
11144
11145theend:
11146 vim_free(pat2);
11147 vim_free(pat3);
11148 p_ws = save_p_ws;
11149 p_cpo = save_cpo;
11150}
11151
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152/*ARGSUSED*/
11153 static void
11154f_server2client(argvars, rettv)
11155 typeval *argvars;
11156 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158#ifdef FEAT_CLIENTSERVER
11159 char_u buf[NUMBUFLEN];
11160 char_u *server = get_tv_string(&argvars[0]);
11161 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
Bram Moolenaar0d660222005-01-07 21:51:51 +000011163 rettv->vval.v_number = -1;
11164 if (check_restricted() || check_secure())
11165 return;
11166# ifdef FEAT_X11
11167 if (check_connection() == FAIL)
11168 return;
11169# endif
11170
11171 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173 EMSG(_("E258: Unable to send to client"));
11174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176 rettv->vval.v_number = 0;
11177#else
11178 rettv->vval.v_number = -1;
11179#endif
11180}
11181
11182/*ARGSUSED*/
11183 static void
11184f_serverlist(argvars, rettv)
11185 typeval *argvars;
11186 typeval *rettv;
11187{
11188 char_u *r = NULL;
11189
11190#ifdef FEAT_CLIENTSERVER
11191# ifdef WIN32
11192 r = serverGetVimNames();
11193# else
11194 make_connection();
11195 if (X_DISPLAY != NULL)
11196 r = serverGetVimNames(X_DISPLAY);
11197# endif
11198#endif
11199 rettv->v_type = VAR_STRING;
11200 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201}
11202
11203/*
11204 * "setbufvar()" function
11205 */
11206/*ARGSUSED*/
11207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011209 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211{
11212 buf_T *buf;
11213#ifdef FEAT_AUTOCMD
11214 aco_save_T aco;
11215#else
11216 buf_T *save_curbuf;
11217#endif
11218 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011219 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 char_u nbuf[NUMBUFLEN];
11221
11222 if (check_restricted() || check_secure())
11223 return;
11224 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 buf = get_buf_tv(&argvars[0]);
11226 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 varp = &argvars[2];
11228
11229 if (buf != NULL && varname != NULL && varp != NULL)
11230 {
11231 /* set curbuf to be our buf, temporarily */
11232#ifdef FEAT_AUTOCMD
11233 aucmd_prepbuf(&aco, buf);
11234#else
11235 save_curbuf = curbuf;
11236 curbuf = buf;
11237#endif
11238
11239 if (*varname == '&')
11240 {
11241 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 set_option_value(varname, get_tv_number(varp),
11243 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 }
11245 else
11246 {
11247 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11248 if (bufvarname != NULL)
11249 {
11250 STRCPY(bufvarname, "b:");
11251 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011252 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 vim_free(bufvarname);
11254 }
11255 }
11256
11257 /* reset notion of buffer */
11258#ifdef FEAT_AUTOCMD
11259 aucmd_restbuf(&aco);
11260#else
11261 curbuf = save_curbuf;
11262#endif
11263 }
11264 --emsg_off;
11265}
11266
11267/*
11268 * "setcmdpos()" function
11269 */
11270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011272 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = set_cmdline_pos(
11276 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277}
11278
11279/*
11280 * "setline()" function
11281 */
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011284 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 linenr_T lnum;
11288 char_u *line;
11289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 lnum = get_tv_lnum(argvars);
11291 line = get_tv_string(&argvars[1]);
11292 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
11294 if (lnum >= 1
11295 && lnum <= curbuf->b_ml.ml_line_count
11296 && u_savesub(lnum) == OK
11297 && ml_replace(lnum, line, TRUE) == OK)
11298 {
11299 changed_bytes(lnum, 0);
11300 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 }
11303}
11304
11305/*
11306 * "setreg()" function
11307 */
11308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011310 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
11313 int regname;
11314 char_u *strregname;
11315 char_u *stropt;
11316 int append;
11317 char_u yank_type;
11318 long block_len;
11319
11320 block_len = -1;
11321 yank_type = MAUTO;
11322 append = FALSE;
11323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 strregname = get_tv_string(argvars);
11325 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326
11327 regname = (strregname == NULL ? '"' : *strregname);
11328 if (regname == 0 || regname == '@')
11329 regname = '"';
11330 else if (regname == '=')
11331 return;
11332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011333 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 switch (*stropt)
11337 {
11338 case 'a': case 'A': /* append */
11339 append = TRUE;
11340 break;
11341 case 'v': case 'c': /* character-wise selection */
11342 yank_type = MCHAR;
11343 break;
11344 case 'V': case 'l': /* line-wise selection */
11345 yank_type = MLINE;
11346 break;
11347#ifdef FEAT_VISUAL
11348 case 'b': case Ctrl_V: /* block-wise selection */
11349 yank_type = MBLOCK;
11350 if (VIM_ISDIGIT(stropt[1]))
11351 {
11352 ++stropt;
11353 block_len = getdigits(&stropt) - 1;
11354 --stropt;
11355 }
11356 break;
11357#endif
11358 }
11359 }
11360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364}
11365
11366
11367/*
11368 * "setwinvar(expr)" function
11369 */
11370/*ARGSUSED*/
11371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
11376 win_T *win;
11377#ifdef FEAT_WINDOWS
11378 win_T *save_curwin;
11379#endif
11380 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011381 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 char_u nbuf[NUMBUFLEN];
11383
11384 if (check_restricted() || check_secure())
11385 return;
11386 ++emsg_off;
11387 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 varp = &argvars[2];
11390
11391 if (win != NULL && varname != NULL && varp != NULL)
11392 {
11393#ifdef FEAT_WINDOWS
11394 /* set curwin to be our win, temporarily */
11395 save_curwin = curwin;
11396 curwin = win;
11397 curbuf = curwin->w_buffer;
11398#endif
11399
11400 if (*varname == '&')
11401 {
11402 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 set_option_value(varname, get_tv_number(varp),
11404 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 }
11406 else
11407 {
11408 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11409 if (winvarname != NULL)
11410 {
11411 STRCPY(winvarname, "w:");
11412 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011413 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 vim_free(winvarname);
11415 }
11416 }
11417
11418#ifdef FEAT_WINDOWS
11419 /* Restore current window, if it's still valid (autocomands can make
11420 * it invalid). */
11421 if (win_valid(save_curwin))
11422 {
11423 curwin = save_curwin;
11424 curbuf = curwin->w_buffer;
11425 }
11426#endif
11427 }
11428 --emsg_off;
11429}
11430
11431/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011432 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 */
11434 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011435f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011436 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011439 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440
Bram Moolenaar0d660222005-01-07 21:51:51 +000011441 p = get_tv_string(&argvars[0]);
11442 rettv->vval.v_string = vim_strsave(p);
11443 simplify_filename(rettv->vval.v_string); /* simplify in place */
11444 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445}
11446
Bram Moolenaar0d660222005-01-07 21:51:51 +000011447static int
11448#ifdef __BORLANDC__
11449 _RTLENTRYF
11450#endif
11451 item_compare __ARGS((const void *s1, const void *s2));
11452static int
11453#ifdef __BORLANDC__
11454 _RTLENTRYF
11455#endif
11456 item_compare2 __ARGS((const void *s1, const void *s2));
11457
11458static int item_compare_ic;
11459static char_u *item_compare_func;
11460#define ITEM_COMPARE_FAIL 999
11461
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011463 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011465 static int
11466#ifdef __BORLANDC__
11467_RTLENTRYF
11468#endif
11469item_compare(s1, s2)
11470 const void *s1;
11471 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011473 char_u *p1, *p2;
11474 char_u *tofree1, *tofree2;
11475 int res;
11476 char_u numbuf1[NUMBUFLEN];
11477 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
Bram Moolenaar0d660222005-01-07 21:51:51 +000011479 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
11480 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
11481 if (item_compare_ic)
11482 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484 res = STRCMP(p1, p2);
11485 vim_free(tofree1);
11486 vim_free(tofree2);
11487 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488}
11489
11490 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011491#ifdef __BORLANDC__
11492_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494item_compare2(s1, s2)
11495 const void *s1;
11496 const void *s2;
11497{
11498 int res;
11499 typeval rettv;
11500 typeval argv[2];
11501 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 /* copy the values (is this really needed?) */
11504 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
11505 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
11506
11507 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11508 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 clear_tv(&argv[0]);
11511 clear_tv(&argv[1]);
11512
11513 if (res == FAIL)
11514 res = ITEM_COMPARE_FAIL;
11515 else
11516 res = get_tv_number(&rettv);
11517 clear_tv(&rettv);
11518 return res;
11519}
11520
11521/*
11522 * "sort({list})" function
11523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011526 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529 listvar *l;
11530 listitem *li;
11531 listitem **ptrs;
11532 long len;
11533 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 rettv->vval.v_number = 0;
11536 if (argvars[0].v_type != VAR_LIST)
11537 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 else
11539 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540 l = argvars[0].vval.v_list;
11541 if (l == NULL)
11542 return;
11543 rettv->vval.v_list = l;
11544 rettv->v_type = VAR_LIST;
11545 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 len = list_len(l);
11548 if (len <= 1)
11549 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 item_compare_ic = FALSE;
11552 item_compare_func = NULL;
11553 if (argvars[1].v_type != VAR_UNKNOWN)
11554 {
11555 if (argvars[1].v_type == VAR_FUNC)
11556 item_compare_func = argvars[0].vval.v_string;
11557 else
11558 {
11559 i = get_tv_number(&argvars[1]);
11560 if (i == 1)
11561 item_compare_ic = TRUE;
11562 else
11563 item_compare_func = get_tv_string(&argvars[1]);
11564 }
11565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567 /* Make an array with each entry pointing to an item in the List. */
11568 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
11569 if (ptrs == NULL)
11570 return;
11571 i = 0;
11572 for (li = l->lv_first; li != NULL; li = li->li_next)
11573 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574
Bram Moolenaar0d660222005-01-07 21:51:51 +000011575 /* test the compare function */
11576 if (item_compare_func != NULL
11577 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11578 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011579 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581 {
11582 /* Sort the array with item pointers. */
11583 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
11584 item_compare_func == NULL ? item_compare : item_compare2);
11585
11586 /* Clear the List and append the items in the sorted order. */
11587 l->lv_first = l->lv_last = NULL;
11588 for (i = 0; i < len; ++i)
11589 list_append(l, ptrs[i]);
11590 }
11591
11592 vim_free(ptrs);
11593 }
11594}
11595
11596 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011597f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598 typeval *argvars;
11599 typeval *rettv;
11600{
11601 char_u *str;
11602 char_u *end;
11603 char_u *pat;
11604 regmatch_T regmatch;
11605 char_u patbuf[NUMBUFLEN];
11606 char_u *save_cpo;
11607 int match;
11608 listitem *ni;
11609 listvar *l;
11610 colnr_T col = 0;
11611
11612 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11613 save_cpo = p_cpo;
11614 p_cpo = (char_u *)"";
11615
11616 str = get_tv_string(&argvars[0]);
11617 if (argvars[1].v_type == VAR_UNKNOWN)
11618 pat = (char_u *)"[\\x01- ]\\+";
11619 else
11620 pat = get_tv_string_buf(&argvars[1], patbuf);
11621
11622 l = list_alloc();
11623 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011625 rettv->v_type = VAR_LIST;
11626 rettv->vval.v_list = l;
11627 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11630 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011632 regmatch.rm_ic = FALSE;
11633 while (*str != NUL)
11634 {
11635 match = vim_regexec_nl(&regmatch, str, col);
11636 if (match)
11637 end = regmatch.startp[0];
11638 else
11639 end = str + STRLEN(str);
11640 if (end > str)
11641 {
11642 ni = listitem_alloc();
11643 if (ni == NULL)
11644 break;
11645 ni->li_tv.v_type = VAR_STRING;
11646 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
11647 list_append(l, ni);
11648 }
11649 if (!match)
11650 break;
11651 /* Advance to just after the match. */
11652 if (regmatch.endp[0] > str)
11653 col = 0;
11654 else
11655 {
11656 /* Don't get stuck at the same match. */
11657#ifdef FEAT_MBYTE
11658 col = mb_ptr2len_check(regmatch.endp[0]);
11659#else
11660 col = 1;
11661#endif
11662 }
11663 str = regmatch.endp[0];
11664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668
Bram Moolenaar0d660222005-01-07 21:51:51 +000011669 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670}
11671
11672#ifdef HAVE_STRFTIME
11673/*
11674 * "strftime({format}[, {time}])" function
11675 */
11676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011678 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
11681 char_u result_buf[256];
11682 struct tm *curtime;
11683 time_t seconds;
11684 char_u *p;
11685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011689 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 seconds = time(NULL);
11691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 curtime = localtime(&seconds);
11694 /* MSVC returns NULL for an invalid value of seconds. */
11695 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 else
11698 {
11699# ifdef FEAT_MBYTE
11700 vimconv_T conv;
11701 char_u *enc;
11702
11703 conv.vc_type = CONV_NONE;
11704 enc = enc_locale();
11705 convert_setup(&conv, p_enc, enc);
11706 if (conv.vc_type != CONV_NONE)
11707 p = string_convert(&conv, p, NULL);
11708# endif
11709 if (p != NULL)
11710 (void)strftime((char *)result_buf, sizeof(result_buf),
11711 (char *)p, curtime);
11712 else
11713 result_buf[0] = NUL;
11714
11715# ifdef FEAT_MBYTE
11716 if (conv.vc_type != CONV_NONE)
11717 vim_free(p);
11718 convert_setup(&conv, enc, p_enc);
11719 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 else
11722# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724
11725# ifdef FEAT_MBYTE
11726 /* Release conversion descriptors */
11727 convert_setup(&conv, NULL, NULL);
11728 vim_free(enc);
11729# endif
11730 }
11731}
11732#endif
11733
11734/*
11735 * "stridx()" function
11736 */
11737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011739 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741{
11742 char_u buf[NUMBUFLEN];
11743 char_u *needle;
11744 char_u *haystack;
11745 char_u *pos;
11746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 needle = get_tv_string(&argvars[1]);
11748 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11750
11751 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755}
11756
11757/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011758 * "string()" function
11759 */
11760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011762 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011764{
11765 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011766 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011768 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011769 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011770 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772}
11773
11774/*
11775 * "strlen()" function
11776 */
11777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011779 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->vval.v_number = (varnumber_T)(STRLEN(
11783 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784}
11785
11786/*
11787 * "strpart()" function
11788 */
11789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011791 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793{
11794 char_u *p;
11795 int n;
11796 int len;
11797 int slen;
11798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 slen = (int)STRLEN(p);
11801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 else
11806 len = slen - n; /* default len: all bytes that are available. */
11807
11808 /*
11809 * Only return the overlap between the specified part and the actual
11810 * string.
11811 */
11812 if (n < 0)
11813 {
11814 len += n;
11815 n = 0;
11816 }
11817 else if (n > slen)
11818 n = slen;
11819 if (len < 0)
11820 len = 0;
11821 else if (n + len > slen)
11822 len = slen - n;
11823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 rettv->v_type = VAR_STRING;
11825 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826}
11827
11828/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011829 * "strridx()" function
11830 */
11831 static void
11832f_strridx(argvars, rettv)
11833 typeval *argvars;
11834 typeval *rettv;
11835{
11836 char_u buf[NUMBUFLEN];
11837 char_u *needle;
11838 char_u *haystack;
11839 char_u *rest;
11840 char_u *lastmatch = NULL;
11841
11842 needle = get_tv_string(&argvars[1]);
11843 haystack = get_tv_string_buf(&argvars[0], buf);
11844 if (*needle == NUL)
11845 /* Empty string matches past the end. */
11846 lastmatch = haystack + STRLEN(haystack);
11847 else
11848 for (rest = haystack; *rest != '\0'; ++rest)
11849 {
11850 rest = (char_u *)strstr((char *)rest, (char *)needle);
11851 if (rest == NULL)
11852 break;
11853 lastmatch = rest;
11854 }
11855
11856 if (lastmatch == NULL)
11857 rettv->vval.v_number = -1;
11858 else
11859 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11860}
11861
11862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 * "strtrans()" function
11864 */
11865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 rettv->v_type = VAR_STRING;
11871 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872}
11873
11874/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011875 * "submatch()" function
11876 */
11877 static void
11878f_submatch(argvars, rettv)
11879 typeval *argvars;
11880 typeval *rettv;
11881{
11882 rettv->v_type = VAR_STRING;
11883 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11884}
11885
11886/*
11887 * "substitute()" function
11888 */
11889 static void
11890f_substitute(argvars, rettv)
11891 typeval *argvars;
11892 typeval *rettv;
11893{
11894 char_u patbuf[NUMBUFLEN];
11895 char_u subbuf[NUMBUFLEN];
11896 char_u flagsbuf[NUMBUFLEN];
11897
11898 rettv->v_type = VAR_STRING;
11899 rettv->vval.v_string = do_string_sub(
11900 get_tv_string(&argvars[0]),
11901 get_tv_string_buf(&argvars[1], patbuf),
11902 get_tv_string_buf(&argvars[2], subbuf),
11903 get_tv_string_buf(&argvars[3], flagsbuf));
11904}
11905
11906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 * "synID(line, col, trans)" function
11908 */
11909/*ARGSUSED*/
11910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011912 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914{
11915 int id = 0;
11916#ifdef FEAT_SYN_HL
11917 long line;
11918 long col;
11919 int trans;
11920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011921 line = get_tv_lnum(argvars);
11922 col = get_tv_number(&argvars[1]) - 1;
11923 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924
11925 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
11926 && col >= 0 && col < (long)STRLEN(ml_get(line)))
11927 id = syn_get_id(line, col, trans);
11928#endif
11929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931}
11932
11933/*
11934 * "synIDattr(id, what [, mode])" function
11935 */
11936/*ARGSUSED*/
11937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011938f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011939 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941{
11942 char_u *p = NULL;
11943#ifdef FEAT_SYN_HL
11944 int id;
11945 char_u *what;
11946 char_u *mode;
11947 char_u modebuf[NUMBUFLEN];
11948 int modec;
11949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 id = get_tv_number(&argvars[0]);
11951 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011952 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011954 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 modec = TOLOWER_ASC(mode[0]);
11956 if (modec != 't' && modec != 'c'
11957#ifdef FEAT_GUI
11958 && modec != 'g'
11959#endif
11960 )
11961 modec = 0; /* replace invalid with current */
11962 }
11963 else
11964 {
11965#ifdef FEAT_GUI
11966 if (gui.in_use)
11967 modec = 'g';
11968 else
11969#endif
11970 if (t_colors > 1)
11971 modec = 'c';
11972 else
11973 modec = 't';
11974 }
11975
11976
11977 switch (TOLOWER_ASC(what[0]))
11978 {
11979 case 'b':
11980 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11981 p = highlight_color(id, what, modec);
11982 else /* bold */
11983 p = highlight_has_attr(id, HL_BOLD, modec);
11984 break;
11985
11986 case 'f': /* fg[#] */
11987 p = highlight_color(id, what, modec);
11988 break;
11989
11990 case 'i':
11991 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11992 p = highlight_has_attr(id, HL_INVERSE, modec);
11993 else /* italic */
11994 p = highlight_has_attr(id, HL_ITALIC, modec);
11995 break;
11996
11997 case 'n': /* name */
11998 p = get_highlight_name(NULL, id - 1);
11999 break;
12000
12001 case 'r': /* reverse */
12002 p = highlight_has_attr(id, HL_INVERSE, modec);
12003 break;
12004
12005 case 's': /* standout */
12006 p = highlight_has_attr(id, HL_STANDOUT, modec);
12007 break;
12008
12009 case 'u': /* underline */
12010 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12011 break;
12012 }
12013
12014 if (p != NULL)
12015 p = vim_strsave(p);
12016#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017 rettv->v_type = VAR_STRING;
12018 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019}
12020
12021/*
12022 * "synIDtrans(id)" function
12023 */
12024/*ARGSUSED*/
12025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012027 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029{
12030 int id;
12031
12032#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034
12035 if (id > 0)
12036 id = syn_get_final_id(id);
12037 else
12038#endif
12039 id = 0;
12040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042}
12043
12044/*
12045 * "system()" function
12046 */
12047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012049 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012052 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012054 char_u *infile = NULL;
12055 char_u buf[NUMBUFLEN];
12056 int err = FALSE;
12057 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012059 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012060 {
12061 /*
12062 * Write the string to a temp file, to be used for input of the shell
12063 * command.
12064 */
12065 if ((infile = vim_tempname('i')) == NULL)
12066 {
12067 EMSG(_(e_notmp));
12068 return;
12069 }
12070
12071 fd = mch_fopen((char *)infile, WRITEBIN);
12072 if (fd == NULL)
12073 {
12074 EMSG2(_(e_notopen), infile);
12075 goto done;
12076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012078 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12079 err = TRUE;
12080 if (fclose(fd) != 0)
12081 err = TRUE;
12082 if (err)
12083 {
12084 EMSG(_("E677: Error writing temp file"));
12085 goto done;
12086 }
12087 }
12088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012090
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091#ifdef USE_CR
12092 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012093 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 {
12095 char_u *s;
12096
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012097 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 {
12099 if (*s == CAR)
12100 *s = NL;
12101 }
12102 }
12103#else
12104# ifdef USE_CRNL
12105 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012106 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 {
12108 char_u *s, *d;
12109
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012110 d = res;
12111 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 {
12113 if (s[0] == CAR && s[1] == NL)
12114 ++s;
12115 *d++ = *s;
12116 }
12117 *d = NUL;
12118 }
12119# endif
12120#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012121
12122done:
12123 if (infile != NULL)
12124 {
12125 mch_remove(infile);
12126 vim_free(infile);
12127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->v_type = VAR_STRING;
12129 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130}
12131
12132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 * "tempname()" function
12134 */
12135/*ARGSUSED*/
12136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012138 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
12141 static int x = 'A';
12142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->v_type = VAR_STRING;
12144 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145
12146 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12147 * names. Skip 'I' and 'O', they are used for shell redirection. */
12148 do
12149 {
12150 if (x == 'Z')
12151 x = '0';
12152 else if (x == '9')
12153 x = 'A';
12154 else
12155 {
12156#ifdef EBCDIC
12157 if (x == 'I')
12158 x = 'J';
12159 else if (x == 'R')
12160 x = 'S';
12161 else
12162#endif
12163 ++x;
12164 }
12165 } while (x == 'I' || x == 'O');
12166}
12167
12168/*
12169 * "tolower(string)" function
12170 */
12171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012173 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175{
12176 char_u *p;
12177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 p = vim_strsave(get_tv_string(&argvars[0]));
12179 rettv->v_type = VAR_STRING;
12180 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181
12182 if (p != NULL)
12183 while (*p != NUL)
12184 {
12185#ifdef FEAT_MBYTE
12186 int l;
12187
12188 if (enc_utf8)
12189 {
12190 int c, lc;
12191
12192 c = utf_ptr2char(p);
12193 lc = utf_tolower(c);
12194 l = utf_ptr2len_check(p);
12195 /* TODO: reallocate string when byte count changes. */
12196 if (utf_char2len(lc) == l)
12197 utf_char2bytes(lc, p);
12198 p += l;
12199 }
12200 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12201 p += l; /* skip multi-byte character */
12202 else
12203#endif
12204 {
12205 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12206 ++p;
12207 }
12208 }
12209}
12210
12211/*
12212 * "toupper(string)" function
12213 */
12214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218{
12219 char_u *p;
12220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 p = vim_strsave(get_tv_string(&argvars[0]));
12222 rettv->v_type = VAR_STRING;
12223 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224
12225 if (p != NULL)
12226 while (*p != NUL)
12227 {
12228#ifdef FEAT_MBYTE
12229 int l;
12230
12231 if (enc_utf8)
12232 {
12233 int c, uc;
12234
12235 c = utf_ptr2char(p);
12236 uc = utf_toupper(c);
12237 l = utf_ptr2len_check(p);
12238 /* TODO: reallocate string when byte count changes. */
12239 if (utf_char2len(uc) == l)
12240 utf_char2bytes(uc, p);
12241 p += l;
12242 }
12243 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12244 p += l; /* skip multi-byte character */
12245 else
12246#endif
12247 {
12248 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12249 p++;
12250 }
12251 }
12252}
12253
12254/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012255 * "tr(string, fromstr, tostr)" function
12256 */
12257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012259 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012261{
12262 char_u *instr;
12263 char_u *fromstr;
12264 char_u *tostr;
12265 char_u *p;
12266#ifdef FEAT_MBYTE
12267 int inlen;
12268 int fromlen;
12269 int tolen;
12270 int idx;
12271 char_u *cpstr;
12272 int cplen;
12273 int first = TRUE;
12274#endif
12275 char_u buf[NUMBUFLEN];
12276 char_u buf2[NUMBUFLEN];
12277 garray_T ga;
12278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 instr = get_tv_string(&argvars[0]);
12280 fromstr = get_tv_string_buf(&argvars[1], buf);
12281 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012282
12283 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 rettv->v_type = VAR_STRING;
12285 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012286 ga_init2(&ga, (int)sizeof(char), 80);
12287
12288#ifdef FEAT_MBYTE
12289 if (!has_mbyte)
12290#endif
12291 /* not multi-byte: fromstr and tostr must be the same length */
12292 if (STRLEN(fromstr) != STRLEN(tostr))
12293 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012294#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012295error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012296#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012297 EMSG2(_(e_invarg2), fromstr);
12298 ga_clear(&ga);
12299 return;
12300 }
12301
12302 /* fromstr and tostr have to contain the same number of chars */
12303 while (*instr != NUL)
12304 {
12305#ifdef FEAT_MBYTE
12306 if (has_mbyte)
12307 {
12308 inlen = mb_ptr2len_check(instr);
12309 cpstr = instr;
12310 cplen = inlen;
12311 idx = 0;
12312 for (p = fromstr; *p != NUL; p += fromlen)
12313 {
12314 fromlen = mb_ptr2len_check(p);
12315 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12316 {
12317 for (p = tostr; *p != NUL; p += tolen)
12318 {
12319 tolen = mb_ptr2len_check(p);
12320 if (idx-- == 0)
12321 {
12322 cplen = tolen;
12323 cpstr = p;
12324 break;
12325 }
12326 }
12327 if (*p == NUL) /* tostr is shorter than fromstr */
12328 goto error;
12329 break;
12330 }
12331 ++idx;
12332 }
12333
12334 if (first && cpstr == instr)
12335 {
12336 /* Check that fromstr and tostr have the same number of
12337 * (multi-byte) characters. Done only once when a character
12338 * of instr doesn't appear in fromstr. */
12339 first = FALSE;
12340 for (p = tostr; *p != NUL; p += tolen)
12341 {
12342 tolen = mb_ptr2len_check(p);
12343 --idx;
12344 }
12345 if (idx != 0)
12346 goto error;
12347 }
12348
12349 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012350 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012351 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012352
12353 instr += inlen;
12354 }
12355 else
12356#endif
12357 {
12358 /* When not using multi-byte chars we can do it faster. */
12359 p = vim_strchr(fromstr, *instr);
12360 if (p != NULL)
12361 ga_append(&ga, tostr[p - fromstr]);
12362 else
12363 ga_append(&ga, *instr);
12364 ++instr;
12365 }
12366 }
12367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012369}
12370
12371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 * "type(expr)" function
12373 */
12374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012375f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012376 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012379 int n;
12380
12381 switch (argvars[0].v_type)
12382 {
12383 case VAR_NUMBER: n = 0; break;
12384 case VAR_STRING: n = 1; break;
12385 case VAR_FUNC: n = 2; break;
12386 case VAR_LIST: n = 3; break;
12387 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12388 }
12389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390}
12391
12392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012393 * "values(dict)" function
12394 */
12395 static void
12396f_values(argvars, rettv)
12397 typeval *argvars;
12398 typeval *rettv;
12399{
12400 dict_list(argvars, rettv, 1);
12401}
12402
12403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 * "virtcol(string)" function
12405 */
12406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012408 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410{
12411 colnr_T vcol = 0;
12412 pos_T *fp;
12413
12414 fp = var2fpos(&argvars[0], FALSE);
12415 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12416 {
12417 getvvcol(curwin, fp, NULL, NULL, &vcol);
12418 ++vcol;
12419 }
12420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422}
12423
12424/*
12425 * "visualmode()" function
12426 */
12427/*ARGSUSED*/
12428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012430 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432{
12433#ifdef FEAT_VISUAL
12434 char_u str[2];
12435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 str[0] = curbuf->b_visual_mode_eval;
12438 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440
12441 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012442 if ((argvars[0].v_type == VAR_NUMBER
12443 && argvars[0].vval.v_number != 0)
12444 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 curbuf->b_visual_mode_eval = NUL;
12447#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449#endif
12450}
12451
12452/*
12453 * "winbufnr(nr)" function
12454 */
12455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012457 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459{
12460 win_T *wp;
12461
12462 wp = find_win_by_nr(&argvars[0]);
12463 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467}
12468
12469/*
12470 * "wincol()" function
12471 */
12472/*ARGSUSED*/
12473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012475 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477{
12478 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480}
12481
12482/*
12483 * "winheight(nr)" function
12484 */
12485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012487 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489{
12490 win_T *wp;
12491
12492 wp = find_win_by_nr(&argvars[0]);
12493 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497}
12498
12499/*
12500 * "winline()" function
12501 */
12502/*ARGSUSED*/
12503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012505 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507{
12508 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510}
12511
12512/*
12513 * "winnr()" function
12514 */
12515/* ARGSUSED */
12516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012518 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520{
12521 int nr = 1;
12522#ifdef FEAT_WINDOWS
12523 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012524 win_T *twin = curwin;
12525 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012527 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012530 if (STRCMP(arg, "$") == 0)
12531 twin = lastwin;
12532 else if (STRCMP(arg, "#") == 0)
12533 {
12534 twin = prevwin;
12535 if (prevwin == NULL)
12536 nr = 0;
12537 }
12538 else
12539 {
12540 EMSG2(_(e_invexpr2), arg);
12541 nr = 0;
12542 }
12543 }
12544
12545 if (nr > 0)
12546 for (wp = firstwin; wp != twin; wp = wp->w_next)
12547 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550}
12551
12552/*
12553 * "winrestcmd()" function
12554 */
12555/* ARGSUSED */
12556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012557f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012558 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560{
12561#ifdef FEAT_WINDOWS
12562 win_T *wp;
12563 int winnr = 1;
12564 garray_T ga;
12565 char_u buf[50];
12566
12567 ga_init2(&ga, (int)sizeof(char), 70);
12568 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12569 {
12570 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12571 ga_concat(&ga, buf);
12572# ifdef FEAT_VERTSPLIT
12573 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12574 ga_concat(&ga, buf);
12575# endif
12576 ++winnr;
12577 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012578 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585}
12586
12587/*
12588 * "winwidth(nr)" function
12589 */
12590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012592 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012593 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594{
12595 win_T *wp;
12596
12597 wp = find_win_by_nr(&argvars[0]);
12598 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 else
12601#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012602 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605#endif
12606}
12607
12608 static win_T *
12609find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012610 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611{
12612#ifdef FEAT_WINDOWS
12613 win_T *wp;
12614#endif
12615 int nr;
12616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618
12619#ifdef FEAT_WINDOWS
12620 if (nr == 0)
12621 return curwin;
12622
12623 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12624 if (--nr <= 0)
12625 break;
12626 return wp;
12627#else
12628 if (nr == 0 || nr == 1)
12629 return curwin;
12630 return NULL;
12631#endif
12632}
12633
12634/*
12635 * Translate a String variable into a position.
12636 */
12637 static pos_T *
12638var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012639 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 int lnum; /* TRUE when $ is last line */
12641{
12642 char_u *name;
12643 static pos_T pos;
12644 pos_T *pp;
12645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 if (name[0] == '.') /* cursor */
12648 return &curwin->w_cursor;
12649 if (name[0] == '\'') /* mark */
12650 {
12651 pp = getmark(name[1], FALSE);
12652 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
12653 return NULL;
12654 return pp;
12655 }
12656 if (name[0] == '$') /* last column or line */
12657 {
12658 if (lnum)
12659 {
12660 pos.lnum = curbuf->b_ml.ml_line_count;
12661 pos.col = 0;
12662 }
12663 else
12664 {
12665 pos.lnum = curwin->w_cursor.lnum;
12666 pos.col = (colnr_T)STRLEN(ml_get_curline());
12667 }
12668 return &pos;
12669 }
12670 return NULL;
12671}
12672
12673/*
12674 * Get the length of an environment variable name.
12675 * Advance "arg" to the first character after the name.
12676 * Return 0 for error.
12677 */
12678 static int
12679get_env_len(arg)
12680 char_u **arg;
12681{
12682 char_u *p;
12683 int len;
12684
12685 for (p = *arg; vim_isIDc(*p); ++p)
12686 ;
12687 if (p == *arg) /* no name found */
12688 return 0;
12689
12690 len = (int)(p - *arg);
12691 *arg = p;
12692 return len;
12693}
12694
12695/*
12696 * Get the length of the name of a function or internal variable.
12697 * "arg" is advanced to the first non-white character after the name.
12698 * Return 0 if something is wrong.
12699 */
12700 static int
12701get_id_len(arg)
12702 char_u **arg;
12703{
12704 char_u *p;
12705 int len;
12706
12707 /* Find the end of the name. */
12708 for (p = *arg; eval_isnamec(*p); ++p)
12709 ;
12710 if (p == *arg) /* no name found */
12711 return 0;
12712
12713 len = (int)(p - *arg);
12714 *arg = skipwhite(p);
12715
12716 return len;
12717}
12718
12719/*
12720 * Get the length of the name of a function.
12721 * "arg" is advanced to the first non-white character after the name.
12722 * Return 0 if something is wrong.
12723 * If the name contains 'magic' {}'s, expand them and return the
12724 * expanded name in an allocated string via 'alias' - caller must free.
12725 */
12726 static int
12727get_func_len(arg, alias, evaluate)
12728 char_u **arg;
12729 char_u **alias;
12730 int evaluate;
12731{
12732 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 char_u *p;
12734 char_u *expr_start;
12735 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
12737 *alias = NULL; /* default to no alias */
12738
12739 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
12740 && (*arg)[2] == (int)KE_SNR)
12741 {
12742 /* hard coded <SNR>, already translated */
12743 *arg += 3;
12744 return get_id_len(arg) + 3;
12745 }
12746 len = eval_fname_script(*arg);
12747 if (len > 0)
12748 {
12749 /* literal "<SID>", "s:" or "<SNR>" */
12750 *arg += len;
12751 }
12752
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 if (expr_start != NULL)
12758 {
12759 char_u *temp_string;
12760
12761 if (!evaluate)
12762 {
12763 len += (int)(p - *arg);
12764 *arg = skipwhite(p);
12765 return len;
12766 }
12767
12768 /*
12769 * Include any <SID> etc in the expanded string:
12770 * Thus the -len here.
12771 */
12772 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
12773 if (temp_string == NULL)
12774 return 0;
12775 *alias = temp_string;
12776 *arg = skipwhite(p);
12777 return (int)STRLEN(temp_string);
12778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779
12780 len += get_id_len(arg);
12781 if (len == 0)
12782 EMSG2(_(e_invexpr2), *arg);
12783
12784 return len;
12785}
12786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787/*
12788 * Find the end of a variable or function name, taking care of magic braces.
12789 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
12790 * start and end of the first magic braces item.
12791 * Return a pointer to just after the name. Equal to "arg" if there is no
12792 * valid name.
12793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 char_u *arg;
12797 char_u **expr_start;
12798 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012799 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 int mb_nest = 0;
12802 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 char_u *p;
12804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 *expr_start = NULL;
12808 *expr_end = NULL;
12809 }
12810
12811 for (p = arg; *p != NUL
12812 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012813 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000012814 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 || mb_nest != 0
12816 || br_nest != 0); ++p)
12817 {
12818 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 if (*p == '[')
12821 ++br_nest;
12822 else if (*p == ']')
12823 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 if (*p == '{')
12828 {
12829 mb_nest++;
12830 if (expr_start != NULL && *expr_start == NULL)
12831 *expr_start = p;
12832 }
12833 else if (*p == '}')
12834 {
12835 mb_nest--;
12836 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12837 *expr_end = p;
12838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 }
12841
12842 return p;
12843}
12844
12845/*
12846 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000012847 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 */
12849 static int
12850eval_isnamec(c)
12851 int c;
12852{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012853 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854}
12855
12856/*
12857 * Find a v: variable.
12858 * Return it's index, or -1 if not found.
12859 */
12860 static int
12861find_vim_var(name, len)
12862 char_u *name;
12863 int len; /* length of "name" */
12864{
12865 char_u *vname;
12866 int vlen;
12867 int i;
12868
12869 /*
12870 * Ignore "v:" for old built-in variables, require it for new ones.
12871 */
12872 if (name[0] == 'v' && name[1] == ':')
12873 {
12874 vname = name + 2;
12875 vlen = len - 2;
12876 }
12877 else
12878 {
12879 vname = name;
12880 vlen = len;
12881 }
12882 for (i = 0; i < VV_LEN; ++i)
12883 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
12884 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
12885 return i;
12886 return -1;
12887}
12888
12889/*
12890 * Set number v: variable to "val".
12891 */
12892 void
12893set_vim_var_nr(idx, val)
12894 int idx;
12895 long val;
12896{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012897 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
12901 * Get number v: variable value;
12902 */
12903 long
12904get_vim_var_nr(idx)
12905 int idx;
12906{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012907 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908}
12909
12910/*
12911 * Set v:count, v:count1 and v:prevcount.
12912 */
12913 void
12914set_vcount(count, count1)
12915 long count;
12916 long count1;
12917{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012918 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
12919 vimvars[VV_COUNT].vv_nr = count;
12920 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921}
12922
12923/*
12924 * Set string v: variable to a copy of "val".
12925 */
12926 void
12927set_vim_var_string(idx, val, len)
12928 int idx;
12929 char_u *val;
12930 int len; /* length of "val" to use or -1 (whole string) */
12931{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012932 /* Need to do this (at least) once, since we can't initialize a union.
12933 * Will always be invoked when "v:progname" is set. */
12934 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
12935
Bram Moolenaare9a41262005-01-15 22:18:47 +000012936 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012938 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012940 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012942 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943}
12944
12945/*
12946 * Set v:register if needed.
12947 */
12948 void
12949set_reg_var(c)
12950 int c;
12951{
12952 char_u regname;
12953
12954 if (c == 0 || c == ' ')
12955 regname = '"';
12956 else
12957 regname = c;
12958 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012959 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 set_vim_var_string(VV_REG, &regname, 1);
12961}
12962
12963/*
12964 * Get or set v:exception. If "oldval" == NULL, return the current value.
12965 * Otherwise, restore the value to "oldval" and return NULL.
12966 * Must always be called in pairs to save and restore v:exception! Does not
12967 * take care of memory allocations.
12968 */
12969 char_u *
12970v_exception(oldval)
12971 char_u *oldval;
12972{
12973 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012974 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975
Bram Moolenaare9a41262005-01-15 22:18:47 +000012976 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977 return NULL;
12978}
12979
12980/*
12981 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
12982 * Otherwise, restore the value to "oldval" and return NULL.
12983 * Must always be called in pairs to save and restore v:throwpoint! Does not
12984 * take care of memory allocations.
12985 */
12986 char_u *
12987v_throwpoint(oldval)
12988 char_u *oldval;
12989{
12990 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012991 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992
Bram Moolenaare9a41262005-01-15 22:18:47 +000012993 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 return NULL;
12995}
12996
12997#if defined(FEAT_AUTOCMD) || defined(PROTO)
12998/*
12999 * Set v:cmdarg.
13000 * If "eap" != NULL, use "eap" to generate the value and return the old value.
13001 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
13002 * Must always be called in pairs!
13003 */
13004 char_u *
13005set_cmdarg(eap, oldarg)
13006 exarg_T *eap;
13007 char_u *oldarg;
13008{
13009 char_u *oldval;
13010 char_u *newval;
13011 unsigned len;
13012
Bram Moolenaare9a41262005-01-15 22:18:47 +000013013 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013014 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013016 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000013017 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013018 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 }
13020
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013021 if (eap->force_bin == FORCE_BIN)
13022 len = 6;
13023 else if (eap->force_bin == FORCE_NOBIN)
13024 len = 8;
13025 else
13026 len = 0;
13027 if (eap->force_ff != 0)
13028 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
13029# ifdef FEAT_MBYTE
13030 if (eap->force_enc != 0)
13031 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
13032# endif
13033
13034 newval = alloc(len + 1);
13035 if (newval == NULL)
13036 return NULL;
13037
13038 if (eap->force_bin == FORCE_BIN)
13039 sprintf((char *)newval, " ++bin");
13040 else if (eap->force_bin == FORCE_NOBIN)
13041 sprintf((char *)newval, " ++nobin");
13042 else
13043 *newval = NUL;
13044 if (eap->force_ff != 0)
13045 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
13046 eap->cmd + eap->force_ff);
13047# ifdef FEAT_MBYTE
13048 if (eap->force_enc != 0)
13049 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
13050 eap->cmd + eap->force_enc);
13051# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000013052 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013053 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054}
13055#endif
13056
13057/*
13058 * Get the value of internal variable "name".
13059 * Return OK or FAIL.
13060 */
13061 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 char_u *name;
13064 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066{
13067 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013068 typeval *tv = NULL;
13069 typeval atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 VAR v;
13071 int cc;
13072 int i;
13073
13074 /* truncate the name, so that we can use strcmp() */
13075 cc = name[len];
13076 name[len] = NUL;
13077
13078 /*
13079 * Check for "b:changedtick".
13080 */
13081 if (STRCMP(name, "b:changedtick") == 0)
13082 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013083 atv.v_type = VAR_NUMBER;
13084 atv.vval.v_number = curbuf->b_changedtick;
13085 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 }
13087
13088 /*
13089 * Check for built-in v: variables.
13090 */
13091 else if ((i = find_vim_var(name, len)) >= 0)
13092 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013093 tv = &vimvars[i].tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 }
13095
13096 /*
13097 * Check for user-defined variables.
13098 */
13099 else
13100 {
13101 v = find_var(name, FALSE);
13102 if (v != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013103 tv = &v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 }
13105
Bram Moolenaare9a41262005-01-15 22:18:47 +000013106 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108 if (rettv != NULL)
13109 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 ret = FAIL;
13111 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013112 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013113 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114
13115 name[len] = cc;
13116
13117 return ret;
13118}
13119
13120/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013121 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13122 * value).
13123 */
13124 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126{
13127 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
13128}
13129
13130/*
13131 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 * The string "s" must have been allocated, it is consumed.
13133 * Return NULL for out of memory, the variable otherwise.
13134 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013135 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 char_u *s;
13138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv = alloc_tv();
13142 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144 rettv->v_type = VAR_STRING;
13145 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 }
13147 else
13148 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150}
13151
13152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013153 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 */
13155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158{
13159 if (varp != NULL)
13160 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013161 switch (varp->v_type)
13162 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013163 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013164 func_unref(varp->vval.v_string);
13165 /*FALLTHROUGH*/
13166 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 vim_free(varp->vval.v_string);
13168 break;
13169 case VAR_LIST:
13170 list_unref(varp->vval.v_list);
13171 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013172 case VAR_DICT:
13173 dict_unref(varp->vval.v_dict);
13174 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013175 default:
13176 break;
13177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 vim_free(varp);
13179 }
13180}
13181
13182/*
13183 * Free the memory for a variable value and set the value to NULL or 0.
13184 */
13185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188{
13189 if (varp != NULL)
13190 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013191 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013193 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013194 func_unref(varp->vval.v_string);
13195 /*FALLTHROUGH*/
13196 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197 vim_free(varp->vval.v_string);
13198 varp->vval.v_string = NULL;
13199 break;
13200 case VAR_LIST:
13201 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013202 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013204 case VAR_DICT:
13205 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013206 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013207 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 varp->vval.v_number = 0;
13210 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 case VAR_UNKNOWN:
13212 break;
13213 default:
13214 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 }
13217}
13218
13219/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 * Set the value of a variable to NULL without freeing items.
13221 */
13222 static void
13223init_tv(varp)
13224 typeval *varp;
13225{
13226 if (varp != NULL)
13227 vim_memset(varp, 0, sizeof(typeval));
13228}
13229
13230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 * Get the number value of a variable.
13232 * If it is a String variable, uses vim_str2nr().
13233 */
13234 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013236 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013238 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013240 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013242 case VAR_NUMBER:
13243 n = (long)(varp->vval.v_number);
13244 break;
13245 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013246 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013247 break;
13248 case VAR_STRING:
13249 if (varp->vval.v_string != NULL)
13250 vim_str2nr(varp->vval.v_string, NULL, NULL,
13251 TRUE, TRUE, &n, NULL);
13252 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013253 case VAR_LIST:
13254 EMSG(_("E703: Using a List as a number"));
13255 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013256 case VAR_DICT:
13257 EMSG(_("E728: Using a Dictionary as a number"));
13258 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013260 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013263 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264}
13265
13266/*
13267 * Get the lnum from the first argument. Also accepts ".", "$", etc.
13268 */
13269 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013271 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 linenr_T lnum;
13275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 if (lnum == 0) /* no valid number, try using line() */
13278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 rettv.v_type = VAR_NUMBER;
13280 f_line(argvars, &rettv);
13281 lnum = rettv.vval.v_number;
13282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 }
13284 return lnum;
13285}
13286
13287/*
13288 * Get the string value of a variable.
13289 * If it is a Number variable, the number is converted into a string.
13290 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
13291 * get_var_string_buf() uses a given buffer.
13292 * If the String variable has never been set, return an empty string.
13293 * Never returns NULL;
13294 */
13295 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013297 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298{
13299 static char_u mybuf[NUMBUFLEN];
13300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302}
13303
13304 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013306 typeval *varp;
13307 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013309 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 case VAR_NUMBER:
13312 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
13313 return buf;
13314 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013315 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013316 break;
13317 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013318 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319 break;
13320 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013321 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013322 break;
13323 case VAR_STRING:
13324 if (varp->vval.v_string != NULL)
13325 return varp->vval.v_string;
13326 break;
13327 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013329 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013331 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332}
13333
13334/*
13335 * Find variable "name" in the list of variables.
13336 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013337 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 */
13339 static VAR
13340find_var(name, writing)
13341 char_u *name;
13342 int writing;
13343{
13344 int i;
13345 char_u *varname;
13346 garray_T *gap;
13347
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 if (name[0] == 'a' && name[1] == ':')
13349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013350 /* Function arguments "a:".
13351 * NOTE: We use a typecast, because function arguments don't have a
13352 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 if (writing)
13354 {
13355 EMSG2(_(e_readonlyvar), name);
13356 return NULL;
13357 }
13358 name += 2;
13359 if (current_funccal == NULL)
13360 return NULL;
13361 if (VIM_ISDIGIT(*name))
13362 {
13363 i = atol((char *)name);
13364 if (i == 0) /* a:0 */
13365 return &current_funccal->a0_var;
13366 i += current_funccal->func->args.ga_len;
13367 if (i > current_funccal->argcount) /* a:999 */
13368 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013369 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 }
13371 if (STRCMP(name, "firstline") == 0)
13372 return &(current_funccal->firstline);
13373 if (STRCMP(name, "lastline") == 0)
13374 return &(current_funccal->lastline);
13375 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
13376 if (STRCMP(name, ((char_u **)
13377 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013378 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 return NULL;
13380 }
13381
13382 gap = find_var_ga(name, &varname);
13383 if (gap == NULL)
13384 return NULL;
13385 return find_var_in_ga(gap, varname);
13386}
13387
13388 static VAR
13389find_var_in_ga(gap, varname)
13390 garray_T *gap;
13391 char_u *varname;
13392{
13393 int i;
13394
13395 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013396 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
13397 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 break;
13399 if (i < 0)
13400 return NULL;
13401 return &VAR_GAP_ENTRY(i, gap);
13402}
13403
13404/*
13405 * Find the growarray and start of name without ':' for a variable name.
13406 */
13407 static garray_T *
13408find_var_ga(name, varname)
13409 char_u *name;
13410 char_u **varname;
13411{
13412 if (name[1] != ':')
13413 {
13414 /* If not "x:name" there must not be any ":" in the name. */
13415 if (vim_strchr(name, ':') != NULL)
13416 return NULL;
13417 *varname = name;
13418 if (current_funccal == NULL)
13419 return &variables; /* global variable */
13420 return &current_funccal->l_vars; /* local function variable */
13421 }
13422 *varname = name + 2;
13423 if (*name == 'b') /* buffer variable */
13424 return &curbuf->b_vars;
13425 if (*name == 'w') /* window variable */
13426 return &curwin->w_vars;
13427 if (*name == 'g') /* global variable */
13428 return &variables;
13429 if (*name == 'l' && current_funccal != NULL)/* local function variable */
13430 return &current_funccal->l_vars;
13431 if (*name == 's' /* script variable */
13432 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
13433 return &SCRIPT_VARS(current_SID);
13434 return NULL;
13435}
13436
13437/*
13438 * Get the string value of a (global/local) variable.
13439 * Returns NULL when it doesn't exist.
13440 */
13441 char_u *
13442get_var_value(name)
13443 char_u *name;
13444{
13445 VAR v;
13446
13447 v = find_var(name, FALSE);
13448 if (v == NULL)
13449 return NULL;
13450 return get_var_string(v);
13451}
13452
13453/*
13454 * Allocate a new growarry for a sourced script. It will be used while
13455 * sourcing this script and when executing functions defined in the script.
13456 */
13457 void
13458new_script_vars(id)
13459 scid_T id;
13460{
13461 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
13462 {
13463 while (ga_scripts.ga_len < id)
13464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013465 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 }
13468 }
13469}
13470
13471/*
13472 * Initialize internal variables for use.
13473 */
13474 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 garray_T *gap;
13477{
13478 ga_init2(gap, (int)sizeof(var), 4);
13479}
13480
13481/*
13482 * Clean up a list of internal variables.
13483 */
13484 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 garray_T *gap;
13487{
13488 int i;
13489
13490 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 ga_clear(gap);
13493}
13494
13495 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013496clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 VAR v;
13498{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013499 vim_free(v->v_name);
13500 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502}
13503
13504/*
13505 * List the value of one internal variable.
13506 */
13507 static void
13508list_one_var(v, prefix)
13509 VAR v;
13510 char_u *prefix;
13511{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013512 char_u *tofree;
13513 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013514 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013516 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013517 list_one_var_a(prefix, v->v_name, v->tv.v_type,
13518 s == NULL ? (char_u *)"" : s);
13519 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520}
13521
13522/*
13523 * List the value of one "v:" variable.
13524 */
13525 static void
13526list_vim_var(i)
13527 int i; /* index in vimvars[] */
13528{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013529 char_u *tofree;
13530 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 char_u numbuf[NUMBUFLEN];
13532
Bram Moolenaare9a41262005-01-15 22:18:47 +000013533 s = echo_string(&vimvars[i].tv, &tofree, numbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
Bram Moolenaare9a41262005-01-15 22:18:47 +000013535 vimvars[i].tv.v_type, s == NULL ? (char_u *)"" : s);
13536 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537}
13538
13539 static void
13540list_one_var_a(prefix, name, type, string)
13541 char_u *prefix;
13542 char_u *name;
13543 int type;
13544 char_u *string;
13545{
13546 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13547 if (name != NULL) /* "a:" vars don't have a name stored */
13548 msg_puts(name);
13549 msg_putchar(' ');
13550 msg_advance(22);
13551 if (type == VAR_NUMBER)
13552 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013553 else if (type == VAR_FUNC)
13554 msg_putchar('*');
13555 else if (type == VAR_LIST)
13556 {
13557 msg_putchar('[');
13558 if (*string == '[')
13559 ++string;
13560 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013561 else if (type == VAR_DICT)
13562 {
13563 msg_putchar('{');
13564 if (*string == '{')
13565 ++string;
13566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 else
13568 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013569
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013571
13572 if (type == VAR_FUNC)
13573 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574}
13575
13576/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 * If the variable already exists, the value is updated.
13579 * Otherwise the variable is created.
13580 */
13581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 typeval *tv;
13585 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586{
13587 int i;
13588 VAR v;
13589 char_u *varname;
13590 garray_T *gap;
13591
13592 /*
13593 * Handle setting internal v: variables.
13594 */
13595 i = find_vim_var(name, (int)STRLEN(name));
13596 if (i >= 0)
13597 {
13598 if (vimvars[i].flags & VV_RO)
13599 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000013600 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
13601 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 else
13603 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013604 if (vimvars[i].tv.v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013606 vim_free(vimvars[i].vv_str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013608 vimvars[i].vv_str = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013609 else
13610 {
13611 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013612 vimvars[i].vv_str = tv->vval.v_string;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 }
13616 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013617 vimvars[i].vv_nr = get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 }
13619 return;
13620 }
13621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 {
13624 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13625 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13626 ? name[2] : name[0]))
13627 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013628 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013629 return;
13630 }
13631 if (function_exists(name))
13632 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013633 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634 return;
13635 }
13636 }
13637
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 v = find_var(name, TRUE);
13639 if (v != NULL) /* existing variable, only need to free string */
13640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013642 && !((v->tv.v_type == VAR_STRING
13643 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 && (tv->v_type == VAR_STRING
13645 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013646 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013647 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013648 return;
13649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 }
13652 else /* add a new variable */
13653 {
13654 gap = find_var_ga(name, &varname);
13655 if (gap == NULL) /* illegal name */
13656 {
13657 EMSG2(_("E461: Illegal variable name: %s"), name);
13658 return;
13659 }
13660
13661 /* Try to use an empty entry */
13662 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013663 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 break;
13665 if (i < 0) /* need to allocate more room */
13666 {
13667 if (ga_grow(gap, 1) == FAIL)
13668 return;
13669 i = gap->ga_len;
13670 }
13671 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013672 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 return;
13674 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 if (copy || tv->v_type == VAR_NUMBER)
13678 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013679 else
13680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681 v->tv = *tv;
13682 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684}
13685
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013686/*
13687 * Copy the values from typeval "from" to typeval "to".
13688 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013689 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013693 typeval *from;
13694 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013696 to->v_type = from->v_type;
13697 switch (from->v_type)
13698 {
13699 case VAR_NUMBER:
13700 to->vval.v_number = from->vval.v_number;
13701 break;
13702 case VAR_STRING:
13703 case VAR_FUNC:
13704 if (from->vval.v_string == NULL)
13705 to->vval.v_string = NULL;
13706 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013708 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013709 if (from->v_type == VAR_FUNC)
13710 func_ref(to->vval.v_string);
13711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013712 break;
13713 case VAR_LIST:
13714 if (from->vval.v_list == NULL)
13715 to->vval.v_list = NULL;
13716 else
13717 {
13718 to->vval.v_list = from->vval.v_list;
13719 ++to->vval.v_list->lv_refcount;
13720 }
13721 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013722 case VAR_DICT:
13723 if (from->vval.v_dict == NULL)
13724 to->vval.v_dict = NULL;
13725 else
13726 {
13727 to->vval.v_dict = from->vval.v_dict;
13728 ++to->vval.v_dict->dv_refcount;
13729 }
13730 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013731 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013733 break;
13734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735}
13736
13737/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013738 * Make a copy of an item.
13739 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
13740 */
13741 static void
13742item_copy(from, to, deep)
13743 typeval *from;
13744 typeval *to;
13745 int deep;
13746{
13747 static int recurse = 0;
13748
13749 if (recurse >= VAR_MAXNEST)
13750 {
13751 EMSG(_("E698: variable nested too deep for making a copy"));
13752 return;
13753 }
13754 ++recurse;
13755
13756 switch (from->v_type)
13757 {
13758 case VAR_NUMBER:
13759 case VAR_STRING:
13760 case VAR_FUNC:
13761 copy_tv(from, to);
13762 break;
13763 case VAR_LIST:
13764 to->v_type = VAR_LIST;
13765 to->vval.v_list = list_copy(from->vval.v_list, deep);
13766 break;
13767 case VAR_DICT:
13768 to->v_type = VAR_DICT;
13769 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
13770 break;
13771 default:
13772 EMSG2(_(e_intern2), "item_copy()");
13773 }
13774 --recurse;
13775}
13776
13777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 * ":echo expr1 ..." print each argument separated with a space, add a
13779 * newline at the end.
13780 * ":echon expr1 ..." print each argument plain.
13781 */
13782 void
13783ex_echo(eap)
13784 exarg_T *eap;
13785{
13786 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013788 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 char_u *p;
13790 int needclr = TRUE;
13791 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013792 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793
13794 if (eap->skip)
13795 ++emsg_skip;
13796 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
13797 {
13798 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013799 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 {
13801 /*
13802 * Report the invalid expression unless the expression evaluation
13803 * has been cancelled due to an aborting error, an interrupt, or an
13804 * exception.
13805 */
13806 if (!aborting())
13807 EMSG2(_(e_invexpr2), p);
13808 break;
13809 }
13810 if (!eap->skip)
13811 {
13812 if (atstart)
13813 {
13814 atstart = FALSE;
13815 /* Call msg_start() after eval1(), evaluating the expression
13816 * may cause a message to appear. */
13817 if (eap->cmdidx == CMD_echo)
13818 msg_start();
13819 }
13820 else if (eap->cmdidx == CMD_echo)
13821 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013823 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 if (*p == '\n' || *p == '\r' || *p == TAB)
13825 {
13826 if (*p != TAB && needclr)
13827 {
13828 /* remove any text still there from the command */
13829 msg_clr_eos();
13830 needclr = FALSE;
13831 }
13832 msg_putchar_attr(*p, echo_attr);
13833 }
13834 else
13835 {
13836#ifdef FEAT_MBYTE
13837 if (has_mbyte)
13838 {
13839 int i = (*mb_ptr2len_check)(p);
13840
13841 (void)msg_outtrans_len_attr(p, i, echo_attr);
13842 p += i - 1;
13843 }
13844 else
13845#endif
13846 (void)msg_outtrans_len_attr(p, 1, echo_attr);
13847 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013848 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 arg = skipwhite(arg);
13852 }
13853 eap->nextcmd = check_nextcmd(arg);
13854
13855 if (eap->skip)
13856 --emsg_skip;
13857 else
13858 {
13859 /* remove text that may still be there from the command */
13860 if (needclr)
13861 msg_clr_eos();
13862 if (eap->cmdidx == CMD_echo)
13863 msg_end();
13864 }
13865}
13866
13867/*
13868 * ":echohl {name}".
13869 */
13870 void
13871ex_echohl(eap)
13872 exarg_T *eap;
13873{
13874 int id;
13875
13876 id = syn_name2id(eap->arg);
13877 if (id == 0)
13878 echo_attr = 0;
13879 else
13880 echo_attr = syn_id2attr(id);
13881}
13882
13883/*
13884 * ":execute expr1 ..." execute the result of an expression.
13885 * ":echomsg expr1 ..." Print a message
13886 * ":echoerr expr1 ..." Print an error
13887 * Each gets spaces around each argument and a newline at the end for
13888 * echo commands
13889 */
13890 void
13891ex_execute(eap)
13892 exarg_T *eap;
13893{
13894 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013895 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 int ret = OK;
13897 char_u *p;
13898 garray_T ga;
13899 int len;
13900 int save_did_emsg;
13901
13902 ga_init2(&ga, 1, 80);
13903
13904 if (eap->skip)
13905 ++emsg_skip;
13906 while (*arg != NUL && *arg != '|' && *arg != '\n')
13907 {
13908 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 {
13911 /*
13912 * Report the invalid expression unless the expression evaluation
13913 * has been cancelled due to an aborting error, an interrupt, or an
13914 * exception.
13915 */
13916 if (!aborting())
13917 EMSG2(_(e_invexpr2), p);
13918 ret = FAIL;
13919 break;
13920 }
13921
13922 if (!eap->skip)
13923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013924 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 len = (int)STRLEN(p);
13926 if (ga_grow(&ga, len + 2) == FAIL)
13927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 ret = FAIL;
13930 break;
13931 }
13932 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 ga.ga_len += len;
13936 }
13937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 arg = skipwhite(arg);
13940 }
13941
13942 if (ret != FAIL && ga.ga_data != NULL)
13943 {
13944 if (eap->cmdidx == CMD_echomsg)
13945 MSG_ATTR(ga.ga_data, echo_attr);
13946 else if (eap->cmdidx == CMD_echoerr)
13947 {
13948 /* We don't want to abort following commands, restore did_emsg. */
13949 save_did_emsg = did_emsg;
13950 EMSG((char_u *)ga.ga_data);
13951 if (!force_abort)
13952 did_emsg = save_did_emsg;
13953 }
13954 else if (eap->cmdidx == CMD_execute)
13955 do_cmdline((char_u *)ga.ga_data,
13956 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
13957 }
13958
13959 ga_clear(&ga);
13960
13961 if (eap->skip)
13962 --emsg_skip;
13963
13964 eap->nextcmd = check_nextcmd(arg);
13965}
13966
13967/*
13968 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
13969 * "arg" points to the "&" or '+' when called, to "option" when returning.
13970 * Returns NULL when no option name found. Otherwise pointer to the char
13971 * after the option name.
13972 */
13973 static char_u *
13974find_option_end(arg, opt_flags)
13975 char_u **arg;
13976 int *opt_flags;
13977{
13978 char_u *p = *arg;
13979
13980 ++p;
13981 if (*p == 'g' && p[1] == ':')
13982 {
13983 *opt_flags = OPT_GLOBAL;
13984 p += 2;
13985 }
13986 else if (*p == 'l' && p[1] == ':')
13987 {
13988 *opt_flags = OPT_LOCAL;
13989 p += 2;
13990 }
13991 else
13992 *opt_flags = 0;
13993
13994 if (!ASCII_ISALPHA(*p))
13995 return NULL;
13996 *arg = p;
13997
13998 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
13999 p += 4; /* termcap option */
14000 else
14001 while (ASCII_ISALPHA(*p))
14002 ++p;
14003 return p;
14004}
14005
14006/*
14007 * ":function"
14008 */
14009 void
14010ex_function(eap)
14011 exarg_T *eap;
14012{
14013 char_u *theline;
14014 int j;
14015 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 char_u *name = NULL;
14018 char_u *p;
14019 char_u *arg;
14020 garray_T newargs;
14021 garray_T newlines;
14022 int varargs = FALSE;
14023 int mustend = FALSE;
14024 int flags = 0;
14025 ufunc_T *fp;
14026 int indent;
14027 int nesting;
14028 char_u *skip_until = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014029 VAR v;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014030 funcdict fudi;
14031 static int func_nr = 0; /* number for nameless function */
14032 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033
14034 /*
14035 * ":function" without argument: list functions.
14036 */
14037 if (ends_excmd(*eap->arg))
14038 {
14039 if (!eap->skip)
14040 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014041 if (!isdigit(*fp->name))
14042 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 eap->nextcmd = check_nextcmd(eap->arg);
14044 return;
14045 }
14046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014047 /*
14048 * Get the function name. There are these situations:
14049 * func normal function name
14050 * "name" == func, "fudi.fd_dict" == NULL
14051 * dict.func new dictionary entry
14052 * "name" == NULL, "fudi.fd_dict" set,
14053 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
14054 * dict.func existing dict entry with a Funcref
14055 * "name" == fname, "fudi.fd_dict" set,
14056 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14057 * dict.func existing dict entry that's not a Funcref
14058 * "name" == NULL, "fudi.fd_dict" set,
14059 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014062 name = trans_function_name(&p, eap->skip, 0, &fudi);
14063 paren = (vim_strchr(p, '(') != NULL);
14064 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 {
14066 /*
14067 * Return on an invalid expression in braces, unless the expression
14068 * evaluation has been cancelled due to an aborting error, an
14069 * interrupt, or an exception.
14070 */
14071 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014072 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014073 if (!eap->skip && fudi.fd_newkey != NULL)
14074 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014075 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 else
14079 eap->skip = TRUE;
14080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 /* An error in a function call during evaluation of an expression in magic
14082 * braces should not cause the function not to be defined. */
14083 saved_did_emsg = did_emsg;
14084 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085
14086 /*
14087 * ":function func" with only function name: list function.
14088 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014089 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 {
14091 if (!ends_excmd(*skipwhite(p)))
14092 {
14093 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014094 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 }
14096 eap->nextcmd = check_nextcmd(p);
14097 if (eap->nextcmd != NULL)
14098 *p = NUL;
14099 if (!eap->skip && !got_int)
14100 {
14101 fp = find_func(name);
14102 if (fp != NULL)
14103 {
14104 list_func_head(fp, TRUE);
14105 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
14106 {
14107 msg_putchar('\n');
14108 msg_outnum((long)(j + 1));
14109 if (j < 9)
14110 msg_putchar(' ');
14111 if (j < 99)
14112 msg_putchar(' ');
14113 msg_prt_line(FUNCLINE(fp, j));
14114 out_flush(); /* show a line at a time */
14115 ui_breakcheck();
14116 }
14117 if (!got_int)
14118 {
14119 msg_putchar('\n');
14120 msg_puts((char_u *)" endfunction");
14121 }
14122 }
14123 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014124 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014126 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 }
14128
14129 /*
14130 * ":function name(arg1, arg2)" Define function.
14131 */
14132 p = skipwhite(p);
14133 if (*p != '(')
14134 {
14135 if (!eap->skip)
14136 {
14137 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014138 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 }
14140 /* attempt to continue by skipping some text */
14141 if (vim_strchr(p, '(') != NULL)
14142 p = vim_strchr(p, '(');
14143 }
14144 p = skipwhite(p + 1);
14145
14146 ga_init2(&newargs, (int)sizeof(char_u *), 3);
14147 ga_init2(&newlines, (int)sizeof(char_u *), 3);
14148
14149 /*
14150 * Isolate the arguments: "arg1, arg2, ...)"
14151 */
14152 while (*p != ')')
14153 {
14154 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
14155 {
14156 varargs = TRUE;
14157 p += 3;
14158 mustend = TRUE;
14159 }
14160 else
14161 {
14162 arg = p;
14163 while (ASCII_ISALNUM(*p) || *p == '_')
14164 ++p;
14165 if (arg == p || isdigit(*arg)
14166 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
14167 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
14168 {
14169 if (!eap->skip)
14170 EMSG2(_("E125: Illegal argument: %s"), arg);
14171 break;
14172 }
14173 if (ga_grow(&newargs, 1) == FAIL)
14174 goto erret;
14175 c = *p;
14176 *p = NUL;
14177 arg = vim_strsave(arg);
14178 if (arg == NULL)
14179 goto erret;
14180 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
14181 *p = c;
14182 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 if (*p == ',')
14184 ++p;
14185 else
14186 mustend = TRUE;
14187 }
14188 p = skipwhite(p);
14189 if (mustend && *p != ')')
14190 {
14191 if (!eap->skip)
14192 EMSG2(_(e_invarg2), eap->arg);
14193 break;
14194 }
14195 }
14196 ++p; /* skip the ')' */
14197
Bram Moolenaare9a41262005-01-15 22:18:47 +000014198 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 for (;;)
14200 {
14201 p = skipwhite(p);
14202 if (STRNCMP(p, "range", 5) == 0)
14203 {
14204 flags |= FC_RANGE;
14205 p += 5;
14206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014207 else if (STRNCMP(p, "dict", 4) == 0)
14208 {
14209 flags |= FC_DICT;
14210 p += 4;
14211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212 else if (STRNCMP(p, "abort", 5) == 0)
14213 {
14214 flags |= FC_ABORT;
14215 p += 5;
14216 }
14217 else
14218 break;
14219 }
14220
14221 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
14222 EMSG(_(e_trailing));
14223
14224 /*
14225 * Read the body of the function, until ":endfunction" is found.
14226 */
14227 if (KeyTyped)
14228 {
14229 /* Check if the function already exists, don't let the user type the
14230 * whole function before telling him it doesn't work! For a script we
14231 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014232 if (!eap->skip && !eap->forceit)
14233 {
14234 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
14235 EMSG(_(e_funcdict));
14236 else if (name != NULL && find_func(name) != NULL)
14237 EMSG2(_(e_funcexts), name);
14238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239
14240 msg_putchar('\n'); /* don't overwrite the function name */
14241 cmdline_row = msg_row;
14242 }
14243
14244 indent = 2;
14245 nesting = 0;
14246 for (;;)
14247 {
14248 msg_scroll = TRUE;
14249 need_wait_return = FALSE;
14250 if (eap->getline == NULL)
14251 theline = getcmdline(':', 0L, indent);
14252 else
14253 theline = eap->getline(':', eap->cookie, indent);
14254 if (KeyTyped)
14255 lines_left = Rows - 1;
14256 if (theline == NULL)
14257 {
14258 EMSG(_("E126: Missing :endfunction"));
14259 goto erret;
14260 }
14261
14262 if (skip_until != NULL)
14263 {
14264 /* between ":append" and "." and between ":python <<EOF" and "EOF"
14265 * don't check for ":endfunc". */
14266 if (STRCMP(theline, skip_until) == 0)
14267 {
14268 vim_free(skip_until);
14269 skip_until = NULL;
14270 }
14271 }
14272 else
14273 {
14274 /* skip ':' and blanks*/
14275 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
14276 ;
14277
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014278 /* Check for "endfunction". */
14279 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 {
14281 vim_free(theline);
14282 break;
14283 }
14284
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014285 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 * at "end". */
14287 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
14288 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014289 else if (STRNCMP(p, "if", 2) == 0
14290 || STRNCMP(p, "wh", 2) == 0
14291 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 || STRNCMP(p, "try", 3) == 0)
14293 indent += 2;
14294
14295 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014296 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014298 if (*p == '!')
14299 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 p += eval_fname_script(p);
14301 if (ASCII_ISALPHA(*p))
14302 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014303 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 if (*skipwhite(p) == '(')
14305 {
14306 ++nesting;
14307 indent += 2;
14308 }
14309 }
14310 }
14311
14312 /* Check for ":append" or ":insert". */
14313 p = skip_range(p, NULL);
14314 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
14315 || (p[0] == 'i'
14316 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
14317 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
14318 skip_until = vim_strsave((char_u *)".");
14319
14320 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
14321 arg = skipwhite(skiptowhite(p));
14322 if (arg[0] == '<' && arg[1] =='<'
14323 && ((p[0] == 'p' && p[1] == 'y'
14324 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
14325 || (p[0] == 'p' && p[1] == 'e'
14326 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
14327 || (p[0] == 't' && p[1] == 'c'
14328 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
14329 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
14330 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014331 || (p[0] == 'm' && p[1] == 'z'
14332 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 ))
14334 {
14335 /* ":python <<" continues until a dot, like ":append" */
14336 p = skipwhite(arg + 2);
14337 if (*p == NUL)
14338 skip_until = vim_strsave((char_u *)".");
14339 else
14340 skip_until = vim_strsave(p);
14341 }
14342 }
14343
14344 /* Add the line to the function. */
14345 if (ga_grow(&newlines, 1) == FAIL)
14346 goto erret;
14347 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
14348 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 }
14350
14351 /* Don't define the function when skipping commands or when an error was
14352 * detected. */
14353 if (eap->skip || did_emsg)
14354 goto erret;
14355
14356 /*
14357 * If there are no errors, add the function
14358 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014359 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014361 v = find_var(name, FALSE);
14362 if (v != NULL && v->tv.v_type == VAR_FUNC)
14363 {
14364 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
14365 goto erret;
14366 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014367
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014368 fp = find_func(name);
14369 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014371 if (!eap->forceit)
14372 {
14373 EMSG2(_(e_funcexts), name);
14374 goto erret;
14375 }
14376 if (fp->calls > 0)
14377 {
14378 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
14379 name);
14380 goto erret;
14381 }
14382 /* redefine existing function */
14383 ga_clear_strings(&(fp->args));
14384 ga_clear_strings(&(fp->lines));
14385 vim_free(name);
14386 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 }
14389 else
14390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014391 char numbuf[20];
14392
14393 fp = NULL;
14394 if (fudi.fd_newkey == NULL && !eap->forceit)
14395 {
14396 EMSG(_(e_funcdict));
14397 goto erret;
14398 }
14399
14400 /* Give the function a sequential number. Can only be used with a
14401 * Funcref! */
14402 vim_free(name);
14403 sprintf(numbuf, "%d", ++func_nr);
14404 name = vim_strsave((char_u *)numbuf);
14405 if (name == NULL)
14406 goto erret;
14407 }
14408
14409 if (fp == NULL)
14410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14412 if (fp == NULL)
14413 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014414
14415 if (fudi.fd_dict != NULL)
14416 {
14417 if (fudi.fd_di == NULL)
14418 {
14419 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014420 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014421 if (fudi.fd_di == NULL)
14422 {
14423 vim_free(fp);
14424 goto erret;
14425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014426 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
14427 {
14428 vim_free(fudi.fd_di);
14429 goto erret;
14430 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014431 }
14432 else
14433 /* overwrite existing dict entry */
14434 clear_tv(&fudi.fd_di->di_tv);
14435 fudi.fd_di->di_tv.v_type = VAR_FUNC;
14436 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
14437 fp->refcount = 1;
14438 }
14439
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014441 fp->name = name;
14442 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 fp->next = firstfunc;
14444 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 }
14446 fp->args = newargs;
14447 fp->lines = newlines;
14448 fp->varargs = varargs;
14449 fp->flags = flags;
14450 fp->calls = 0;
14451 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014452 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453
14454erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 ga_clear_strings(&newargs);
14456 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014457ret_free:
14458 vim_free(skip_until);
14459 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462}
14463
14464/*
14465 * Get a function name, translating "<SID>" and "<SNR>".
14466 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014467 * flags:
14468 * TFN_INT: internal function name OK
14469 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 * Advances "pp" to just after the function name (if no error).
14471 */
14472 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014473trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 char_u **pp;
14475 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014476 int flags;
14477 funcdict *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014479 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 char_u *start;
14481 char_u *end;
14482 int lead;
14483 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 int len;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014485 lval lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014486
14487 if (fdp != NULL)
14488 vim_memset(fdp, 0, sizeof(funcdict));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489
14490 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
14491 start = *pp;
14492 lead = eval_fname_script(start);
14493 if (lead > 0)
14494 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014495
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014496 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014497 if (end == start)
14498 {
14499 if (!skip)
14500 EMSG(_("E129: Function name required"));
14501 goto theend;
14502 }
14503 if (end == NULL || (lv.ll_tv != NULL && (lead > 0 || lv.ll_range)))
14504 {
14505 /*
14506 * Report an invalid expression in braces, unless the expression
14507 * evaluation has been cancelled due to an aborting error, an
14508 * interrupt, or an exception.
14509 */
14510 if (!aborting())
14511 {
14512 if (end != NULL)
14513 EMSG2(_(e_invarg2), start);
14514 }
14515 else
14516 *pp = find_name_end(start, NULL, NULL, TRUE);
14517 goto theend;
14518 }
14519
14520 if (lv.ll_tv != NULL)
14521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014522 if (fdp != NULL)
14523 {
14524 fdp->fd_dict = lv.ll_dict;
14525 fdp->fd_newkey = lv.ll_newkey;
14526 lv.ll_newkey = NULL;
14527 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014528 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014529 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14530 {
14531 name = vim_strsave(lv.ll_tv->vval.v_string);
14532 *pp = end;
14533 }
14534 else
14535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014536 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
14537 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014538 EMSG(_(e_funcref));
14539 else
14540 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014541 name = NULL;
14542 }
14543 goto theend;
14544 }
14545
14546 if (lv.ll_name == NULL)
14547 {
14548 /* Error found, but continue after the function name. */
14549 *pp = end;
14550 goto theend;
14551 }
14552
14553 if (lv.ll_exp_name != NULL)
14554 len = STRLEN(lv.ll_exp_name);
14555 else
14556 len = (int)(end - start);
14557
14558 /*
14559 * Copy the function name to allocated memory.
14560 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14561 * Accept <SNR>123_name() outside a script.
14562 */
14563 if (skip)
14564 lead = 0; /* do nothing */
14565 else if (lead > 0)
14566 {
14567 lead = 3;
14568 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14569 {
14570 if (current_SID <= 0)
14571 {
14572 EMSG(_(e_usingsid));
14573 goto theend;
14574 }
14575 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14576 lead += (int)STRLEN(sid_buf);
14577 }
14578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014579 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014580 {
14581 EMSG2(_("E128: Function name must start with a capital: %s"),
14582 lv.ll_name);
14583 goto theend;
14584 }
14585 name = alloc((unsigned)(len + lead + 1));
14586 if (name != NULL)
14587 {
14588 if (lead > 0)
14589 {
14590 name[0] = K_SPECIAL;
14591 name[1] = KS_EXTRA;
14592 name[2] = (int)KE_SNR;
14593 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14594 STRCPY(name + 3, sid_buf);
14595 }
14596 mch_memmove(name + lead, lv.ll_name, (size_t)len);
14597 name[len + lead] = NUL;
14598 }
14599 *pp = end;
14600
14601theend:
14602 clear_lval(&lv);
14603 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604}
14605
14606/*
14607 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14608 * Return 2 if "p" starts with "s:".
14609 * Return 0 otherwise.
14610 */
14611 static int
14612eval_fname_script(p)
14613 char_u *p;
14614{
14615 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
14616 || STRNICMP(p + 1, "SNR>", 4) == 0))
14617 return 5;
14618 if (p[0] == 's' && p[1] == ':')
14619 return 2;
14620 return 0;
14621}
14622
14623/*
14624 * Return TRUE if "p" starts with "<SID>" or "s:".
14625 * Only works if eval_fname_script() returned non-zero for "p"!
14626 */
14627 static int
14628eval_fname_sid(p)
14629 char_u *p;
14630{
14631 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
14632}
14633
14634/*
14635 * List the head of the function: "name(arg1, arg2)".
14636 */
14637 static void
14638list_func_head(fp, indent)
14639 ufunc_T *fp;
14640 int indent;
14641{
14642 int j;
14643
14644 msg_start();
14645 if (indent)
14646 MSG_PUTS(" ");
14647 MSG_PUTS("function ");
14648 if (fp->name[0] == K_SPECIAL)
14649 {
14650 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
14651 msg_puts(fp->name + 3);
14652 }
14653 else
14654 msg_puts(fp->name);
14655 msg_putchar('(');
14656 for (j = 0; j < fp->args.ga_len; ++j)
14657 {
14658 if (j)
14659 MSG_PUTS(", ");
14660 msg_puts(FUNCARG(fp, j));
14661 }
14662 if (fp->varargs)
14663 {
14664 if (j)
14665 MSG_PUTS(", ");
14666 MSG_PUTS("...");
14667 }
14668 msg_putchar(')');
14669}
14670
14671/*
14672 * Find a function by name, return pointer to it in ufuncs.
14673 * Return NULL for unknown function.
14674 */
14675 static ufunc_T *
14676find_func(name)
14677 char_u *name;
14678{
14679 ufunc_T *fp;
14680
14681 for (fp = firstfunc; fp != NULL; fp = fp->next)
14682 if (STRCMP(name, fp->name) == 0)
14683 break;
14684 return fp;
14685}
14686
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014687/*
14688 * Return TRUE if a function "name" exists.
14689 */
14690 static int
14691function_exists(name)
14692 char_u *name;
14693{
14694 char_u *p = name;
14695 int n = FALSE;
14696
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014697 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014698 if (p != NULL)
14699 {
14700 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14701 n = (find_func(p) != NULL);
14702 else if (ASCII_ISLOWER(*p))
14703 n = (find_internal_func(p) >= 0);
14704 vim_free(p);
14705 }
14706 return n;
14707}
14708
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
14710
14711/*
14712 * Function given to ExpandGeneric() to obtain the list of user defined
14713 * function names.
14714 */
14715 char_u *
14716get_user_func_name(xp, idx)
14717 expand_T *xp;
14718 int idx;
14719{
14720 static ufunc_T *fp = NULL;
14721
14722 if (idx == 0)
14723 fp = firstfunc;
14724 if (fp != NULL)
14725 {
14726 if (STRLEN(fp->name) + 4 >= IOSIZE)
14727 return fp->name; /* prevents overflow */
14728
14729 cat_func_name(IObuff, fp);
14730 if (xp->xp_context != EXPAND_USER_FUNC)
14731 {
14732 STRCAT(IObuff, "(");
14733 if (!fp->varargs && fp->args.ga_len == 0)
14734 STRCAT(IObuff, ")");
14735 }
14736
14737 fp = fp->next;
14738 return IObuff;
14739 }
14740 return NULL;
14741}
14742
14743#endif /* FEAT_CMDL_COMPL */
14744
14745/*
14746 * Copy the function name of "fp" to buffer "buf".
14747 * "buf" must be able to hold the function name plus three bytes.
14748 * Takes care of script-local function names.
14749 */
14750 static void
14751cat_func_name(buf, fp)
14752 char_u *buf;
14753 ufunc_T *fp;
14754{
14755 if (fp->name[0] == K_SPECIAL)
14756 {
14757 STRCPY(buf, "<SNR>");
14758 STRCAT(buf, fp->name + 3);
14759 }
14760 else
14761 STRCPY(buf, fp->name);
14762}
14763
14764/*
14765 * ":delfunction {name}"
14766 */
14767 void
14768ex_delfunction(eap)
14769 exarg_T *eap;
14770{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014771 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 char_u *p;
14773 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014774 funcdict fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775
14776 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014777 name = trans_function_name(&p, eap->skip, 0, &fudi);
14778 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014780 {
14781 if (fudi.fd_dict != NULL && !eap->skip)
14782 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 if (!ends_excmd(*skipwhite(p)))
14786 {
14787 vim_free(name);
14788 EMSG(_(e_trailing));
14789 return;
14790 }
14791 eap->nextcmd = check_nextcmd(p);
14792 if (eap->nextcmd != NULL)
14793 *p = NUL;
14794
14795 if (!eap->skip)
14796 fp = find_func(name);
14797 vim_free(name);
14798
14799 if (!eap->skip)
14800 {
14801 if (fp == NULL)
14802 {
14803 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14804 return;
14805 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014806 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807 {
14808 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14809 return;
14810 }
14811
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014812 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014814 /* Delete the dict item that refers to the function, it will
14815 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014816 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014818 else
14819 func_free(fp);
14820 }
14821}
14822
14823/*
14824 * Free a function and remove it from the list of functions.
14825 */
14826 static void
14827func_free(fp)
14828 ufunc_T *fp;
14829{
14830 ufunc_T *pfp;
14831
14832 /* clear this function */
14833 vim_free(fp->name);
14834 ga_clear_strings(&(fp->args));
14835 ga_clear_strings(&(fp->lines));
14836
14837 /* remove the function from the function list */
14838 if (firstfunc == fp)
14839 firstfunc = fp->next;
14840 else
14841 {
14842 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
14843 if (pfp->next == fp)
14844 {
14845 pfp->next = fp->next;
14846 break;
14847 }
14848 }
14849 vim_free(fp);
14850}
14851
14852/*
14853 * Unreference a Function: decrement the reference count and free it when it
14854 * becomes zero. Only for numbered functions.
14855 */
14856 static void
14857func_unref(name)
14858 char_u *name;
14859{
14860 ufunc_T *fp;
14861
14862 if (name != NULL && isdigit(*name))
14863 {
14864 fp = find_func(name);
14865 if (fp == NULL)
14866 EMSG2(_(e_intern2), "func_unref()");
14867 else if (--fp->refcount <= 0)
14868 {
14869 /* Only delete it when it's not being used. Otherwise it's done
14870 * when "calls" becomes zero. */
14871 if (fp->calls == 0)
14872 func_free(fp);
14873 }
14874 }
14875}
14876
14877/*
14878 * Count a reference to a Function.
14879 */
14880 static void
14881func_ref(name)
14882 char_u *name;
14883{
14884 ufunc_T *fp;
14885
14886 if (name != NULL && isdigit(*name))
14887 {
14888 fp = find_func(name);
14889 if (fp == NULL)
14890 EMSG2(_(e_intern2), "func_ref()");
14891 else
14892 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893 }
14894}
14895
14896/*
14897 * Call a user function.
14898 */
14899 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000014900call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 ufunc_T *fp; /* pointer to function */
14902 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014903 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 linenr_T firstline; /* first line of range */
14906 linenr_T lastline; /* last line of range */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014907 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908{
14909 char_u *save_sourcing_name;
14910 linenr_T save_sourcing_lnum;
14911 scid_T save_current_SID;
14912 struct funccall fc;
14913 struct funccall *save_fcp = current_funccal;
14914 int save_did_emsg;
14915 static int depth = 0;
14916
14917 /* If depth of calling is getting too high, don't execute the function */
14918 if (depth >= p_mfd)
14919 {
14920 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014921 rettv->v_type = VAR_NUMBER;
14922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 return;
14924 }
14925 ++depth;
14926
14927 line_breakcheck(); /* check for CTRL-C hit */
14928
14929 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014930 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 fc.func = fp;
14932 fc.argcount = argcount;
14933 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014934 fc.rettv = rettv;
14935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 fc.linenr = 0;
14937 fc.returned = FALSE;
14938 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014939 fc.a0_var.tv.v_type = VAR_NUMBER;
14940 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
14941 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014943 fc.firstline.tv.v_type = VAR_NUMBER;
14944 fc.firstline.tv.vval.v_number = firstline;
14945 fc.firstline.v_name = NULL;
14946 fc.lastline.tv.v_type = VAR_NUMBER;
14947 fc.lastline.tv.vval.v_number = lastline;
14948 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 /* Check if this function has a breakpoint. */
14950 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
14951 fc.dbg_tick = debug_tick;
14952
Bram Moolenaare9a41262005-01-15 22:18:47 +000014953 if (selfdict != NULL && ga_grow(&fc.l_vars, 1) != FAIL)
14954 {
14955 VAR v = &VAR_GAP_ENTRY(0, &fc.l_vars);
14956
14957 /* Set the "self" local variable. */
14958 if ((v->v_name = vim_strsave((char_u *)"self")) != NULL)
14959 {
14960 ++fc.l_vars.ga_len;
14961 v->tv.v_type = VAR_DICT;
14962 v->tv.vval.v_dict = selfdict;
14963 ++selfdict->dv_refcount;
14964 }
14965 }
14966
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 /* Don't redraw while executing the function. */
14968 ++RedrawingDisabled;
14969 save_sourcing_name = sourcing_name;
14970 save_sourcing_lnum = sourcing_lnum;
14971 sourcing_lnum = 1;
14972 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
14973 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
14974 if (sourcing_name != NULL)
14975 {
14976 if (save_sourcing_name != NULL
14977 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
14978 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
14979 else
14980 STRCPY(sourcing_name, "function ");
14981 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
14982
14983 if (p_verbose >= 12)
14984 {
14985 ++no_wait_return;
14986 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14987 msg_str((char_u *)_("calling %s"), sourcing_name);
14988 if (p_verbose >= 14)
14989 {
14990 int i;
14991 char_u buf[MSG_BUF_LEN];
14992
14993 msg_puts((char_u *)"(");
14994 for (i = 0; i < argcount; ++i)
14995 {
14996 if (i > 0)
14997 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014998 if (argvars[i].v_type == VAR_NUMBER)
14999 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 else
15001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003 buf, MSG_BUF_LEN);
15004 msg_puts((char_u *)"\"");
15005 msg_puts(buf);
15006 msg_puts((char_u *)"\"");
15007 }
15008 }
15009 msg_puts((char_u *)")");
15010 }
15011 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15012 cmdline_row = msg_row;
15013 --no_wait_return;
15014 }
15015 }
15016 save_current_SID = current_SID;
15017 current_SID = fp->script_ID;
15018 save_did_emsg = did_emsg;
15019 did_emsg = FALSE;
15020
15021 /* call do_cmdline() to execute the lines */
15022 do_cmdline(NULL, get_func_line, (void *)&fc,
15023 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
15024
15025 --RedrawingDisabled;
15026
15027 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 clear_tv(rettv);
15031 rettv->v_type = VAR_NUMBER;
15032 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 }
15034
15035 /* when being verbose, mention the return value */
15036 if (p_verbose >= 12)
15037 {
15038 char_u *sn, *val;
15039
15040 ++no_wait_return;
15041 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15042
15043 /* Make sure the output fits in IObuff. */
15044 sn = sourcing_name;
15045 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
15046 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
15047
15048 if (aborting())
15049 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052 (long)fc.rettv->vval.v_number);
15053 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 if (STRLEN(val) > IOSIZE / 2 - 50)
15057 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
15058 smsg((char_u *)_("%s returning \"%s\""), sn, val);
15059 }
15060 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15061 cmdline_row = msg_row;
15062 --no_wait_return;
15063 }
15064
15065 vim_free(sourcing_name);
15066 sourcing_name = save_sourcing_name;
15067 sourcing_lnum = save_sourcing_lnum;
15068 current_SID = save_current_SID;
15069
15070 if (p_verbose >= 12 && sourcing_name != NULL)
15071 {
15072 ++no_wait_return;
15073 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15074 msg_str((char_u *)_("continuing in %s"), sourcing_name);
15075 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15076 cmdline_row = msg_row;
15077 --no_wait_return;
15078 }
15079
15080 did_emsg |= save_did_emsg;
15081 current_funccal = save_fcp;
15082
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015083 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 --depth;
15085}
15086
15087/*
15088 * ":return [expr]"
15089 */
15090 void
15091ex_return(eap)
15092 exarg_T *eap;
15093{
15094 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015095 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096 int returning = FALSE;
15097
15098 if (current_funccal == NULL)
15099 {
15100 EMSG(_("E133: :return not inside a function"));
15101 return;
15102 }
15103
15104 if (eap->skip)
15105 ++emsg_skip;
15106
15107 eap->nextcmd = NULL;
15108 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 {
15111 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015112 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 }
15116 /* It's safer to return also on error. */
15117 else if (!eap->skip)
15118 {
15119 /*
15120 * Return unless the expression evaluation has been cancelled due to an
15121 * aborting error, an interrupt, or an exception.
15122 */
15123 if (!aborting())
15124 returning = do_return(eap, FALSE, TRUE, NULL);
15125 }
15126
15127 /* When skipping or the return gets pending, advance to the next command
15128 * in this line (!returning). Otherwise, ignore the rest of the line.
15129 * Following lines will be ignored by get_func_line(). */
15130 if (returning)
15131 eap->nextcmd = NULL;
15132 else if (eap->nextcmd == NULL) /* no argument */
15133 eap->nextcmd = check_nextcmd(arg);
15134
15135 if (eap->skip)
15136 --emsg_skip;
15137}
15138
15139/*
15140 * Return from a function. Possibly makes the return pending. Also called
15141 * for a pending return at the ":endtry" or after returning from an extra
15142 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 * when called due to a ":return" command. "rettv" may point to a typeval
15144 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 * FALSE when the return gets pending.
15146 */
15147 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 exarg_T *eap;
15150 int reanimate;
15151 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153{
15154 int idx;
15155 struct condstack *cstack = eap->cstack;
15156
15157 if (reanimate)
15158 /* Undo the return. */
15159 current_funccal->returned = FALSE;
15160
15161 /*
15162 * Cleanup (and inactivate) conditionals, but stop when a try conditional
15163 * not in its finally clause (which then is to be executed next) is found.
15164 * In this case, make the ":return" pending for execution at the ":endtry".
15165 * Otherwise, return normally.
15166 */
15167 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
15168 if (idx >= 0)
15169 {
15170 cstack->cs_pending[idx] = CSTP_RETURN;
15171
15172 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 /* A pending return again gets pending. "rettv" points to an
15174 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015176 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 else
15178 {
15179 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015182 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015184 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 {
15186 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
15188 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 else
15190 EMSG(_(e_outofmem));
15191 }
15192 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194
15195 if (reanimate)
15196 {
15197 /* The pending return value could be overwritten by a ":return"
15198 * without argument in a finally clause; reset the default
15199 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200 current_funccal->rettv->v_type = VAR_NUMBER;
15201 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 }
15203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 }
15206 else
15207 {
15208 current_funccal->returned = TRUE;
15209
15210 /* If the return is carried out now, store the return value. For
15211 * a return immediately after reanimation, the value is already
15212 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015213 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015215 clear_tv(current_funccal->rettv);
15216 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015218 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 }
15220 }
15221
15222 return idx < 0;
15223}
15224
15225/*
15226 * Free the variable with a pending return value.
15227 */
15228 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015229discard_pending_return(rettv)
15230 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015232 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233}
15234
15235/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015236 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 * is an allocated string. Used by report_pending() for verbose messages.
15238 */
15239 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015240get_return_cmd(rettv)
15241 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015243 char_u *s;
15244 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015245 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 if (rettv == NULL)
15248 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015250 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251
15252 STRCPY(IObuff, ":return ");
15253 STRNCPY(IObuff + 8, s, IOSIZE - 8);
15254 if (STRLEN(s) + 8 >= IOSIZE)
15255 STRCPY(IObuff + IOSIZE - 4, "...");
15256 vim_free(tofree);
15257 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258}
15259
15260/*
15261 * Get next function line.
15262 * Called by do_cmdline() to get the next line.
15263 * Returns allocated string, or NULL for end of function.
15264 */
15265/* ARGSUSED */
15266 char_u *
15267get_func_line(c, cookie, indent)
15268 int c; /* not used */
15269 void *cookie;
15270 int indent; /* not used */
15271{
15272 struct funccall *fcp = (struct funccall *)cookie;
15273 char_u *retval;
15274 garray_T *gap; /* growarray with function lines */
15275
15276 /* If breakpoints have been added/deleted need to check for it. */
15277 if (fcp->dbg_tick != debug_tick)
15278 {
15279 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15280 sourcing_lnum);
15281 fcp->dbg_tick = debug_tick;
15282 }
15283
15284 gap = &fcp->func->lines;
15285 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15286 retval = NULL;
15287 else if (fcp->returned || fcp->linenr >= gap->ga_len)
15288 retval = NULL;
15289 else
15290 {
15291 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
15292 sourcing_lnum = fcp->linenr;
15293 }
15294
15295 /* Did we encounter a breakpoint? */
15296 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
15297 {
15298 dbg_breakpoint(fcp->func->name, sourcing_lnum);
15299 /* Find next breakpoint. */
15300 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15301 sourcing_lnum);
15302 fcp->dbg_tick = debug_tick;
15303 }
15304
15305 return retval;
15306}
15307
15308/*
15309 * Return TRUE if the currently active function should be ended, because a
15310 * return was encountered or an error occured. Used inside a ":while".
15311 */
15312 int
15313func_has_ended(cookie)
15314 void *cookie;
15315{
15316 struct funccall *fcp = (struct funccall *)cookie;
15317
15318 /* Ignore the "abort" flag if the abortion behavior has been changed due to
15319 * an error inside a try conditional. */
15320 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15321 || fcp->returned);
15322}
15323
15324/*
15325 * return TRUE if cookie indicates a function which "abort"s on errors.
15326 */
15327 int
15328func_has_abort(cookie)
15329 void *cookie;
15330{
15331 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
15332}
15333
15334#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
15335typedef enum
15336{
15337 VAR_FLAVOUR_DEFAULT,
15338 VAR_FLAVOUR_SESSION,
15339 VAR_FLAVOUR_VIMINFO
15340} var_flavour_T;
15341
15342static var_flavour_T var_flavour __ARGS((char_u *varname));
15343
15344 static var_flavour_T
15345var_flavour(varname)
15346 char_u *varname;
15347{
15348 char_u *p = varname;
15349
15350 if (ASCII_ISUPPER(*p))
15351 {
15352 while (*(++p))
15353 if (ASCII_ISLOWER(*p))
15354 return VAR_FLAVOUR_SESSION;
15355 return VAR_FLAVOUR_VIMINFO;
15356 }
15357 else
15358 return VAR_FLAVOUR_DEFAULT;
15359}
15360#endif
15361
15362#if defined(FEAT_VIMINFO) || defined(PROTO)
15363/*
15364 * Restore global vars that start with a capital from the viminfo file
15365 */
15366 int
15367read_viminfo_varlist(virp, writing)
15368 vir_T *virp;
15369 int writing;
15370{
15371 char_u *tab;
15372 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015373 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374
15375 if (!writing && (find_viminfo_parameter('!') != NULL))
15376 {
15377 tab = vim_strchr(virp->vir_line + 1, '\t');
15378 if (tab != NULL)
15379 {
15380 *tab++ = '\0'; /* isolate the variable name */
15381 if (*tab == 'S') /* string var */
15382 is_string = TRUE;
15383
15384 tab = vim_strchr(tab, '\t');
15385 if (tab != NULL)
15386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 if (is_string)
15388 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015389 tv.v_type = VAR_STRING;
15390 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 }
15393 else
15394 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015395 tv.v_type = VAR_NUMBER;
15396 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015398 set_var(virp->vir_line + 1, &tv, FALSE);
15399 if (is_string)
15400 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 }
15402 }
15403 }
15404
15405 return viminfo_readline(virp);
15406}
15407
15408/*
15409 * Write global vars that start with a capital to the viminfo file
15410 */
15411 void
15412write_viminfo_varlist(fp)
15413 FILE *fp;
15414{
15415 garray_T *gap = &variables; /* global variable */
15416 VAR this_var;
15417 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015418 char *s;
15419 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015420 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421
15422 if (find_viminfo_parameter('!') == NULL)
15423 return;
15424
15425 fprintf(fp, _("\n# global variables:\n"));
15426 for (i = gap->ga_len; --i >= 0; )
15427 {
15428 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015429 if (this_var->v_name != NULL
15430 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 switch (this_var->tv.v_type)
15433 {
15434 case VAR_STRING: s = "STR"; break;
15435 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015436 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437 }
15438 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015439 viminfo_writestring(fp, echo_string(&this_var->tv,
15440 &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 }
15443 }
15444}
15445#endif
15446
15447#if defined(FEAT_SESSION) || defined(PROTO)
15448 int
15449store_session_globals(fd)
15450 FILE *fd;
15451{
15452 garray_T *gap = &variables; /* global variable */
15453 VAR this_var;
15454 int i;
15455 char_u *p, *t;
15456
15457 for (i = gap->ga_len; --i >= 0; )
15458 {
15459 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015460 if (this_var->v_name != NULL
15461 && (this_var->tv.v_type == VAR_NUMBER
15462 || this_var->tv.v_type == VAR_STRING)
15463 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015465 /* Escape special characters with a backslash. Turn a LF and
15466 * CR into \n and \r. */
15467 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015469 if (p == NULL) /* out of memory */
15470 continue;
15471 for (t = p; *t != NUL; ++t)
15472 if (*t == '\n')
15473 *t = 'n';
15474 else if (*t == '\r')
15475 *t = 'r';
15476 if ((fprintf(fd, "let %s = %c%s%c",
15477 this_var->v_name,
15478 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
15479 p,
15480 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
15481 || put_eol(fd) == FAIL)
15482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015484 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015486 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 }
15488 }
15489 return OK;
15490}
15491#endif
15492
15493#endif /* FEAT_EVAL */
15494
15495#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
15496
15497
15498#ifdef WIN3264
15499/*
15500 * Functions for ":8" filename modifier: get 8.3 version of a filename.
15501 */
15502static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15503static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
15504static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15505
15506/*
15507 * Get the short pathname of a file.
15508 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
15509 */
15510 static int
15511get_short_pathname(fnamep, bufp, fnamelen)
15512 char_u **fnamep;
15513 char_u **bufp;
15514 int *fnamelen;
15515{
15516 int l,len;
15517 char_u *newbuf;
15518
15519 len = *fnamelen;
15520
15521 l = GetShortPathName(*fnamep, *fnamep, len);
15522 if (l > len - 1)
15523 {
15524 /* If that doesn't work (not enough space), then save the string
15525 * and try again with a new buffer big enough
15526 */
15527 newbuf = vim_strnsave(*fnamep, l);
15528 if (newbuf == NULL)
15529 return 0;
15530
15531 vim_free(*bufp);
15532 *fnamep = *bufp = newbuf;
15533
15534 l = GetShortPathName(*fnamep,*fnamep,l+1);
15535
15536 /* Really should always succeed, as the buffer is big enough */
15537 }
15538
15539 *fnamelen = l;
15540 return 1;
15541}
15542
15543/*
15544 * Create a short path name. Returns the length of the buffer it needs.
15545 * Doesn't copy over the end of the buffer passed in.
15546 */
15547 static int
15548shortpath_for_invalid_fname(fname, bufp, fnamelen)
15549 char_u **fname;
15550 char_u **bufp;
15551 int *fnamelen;
15552{
15553 char_u *s, *p, *pbuf2, *pbuf3;
15554 char_u ch;
15555 int l,len,len2,plen,slen;
15556
15557 /* Make a copy */
15558 len2 = *fnamelen;
15559 pbuf2 = vim_strnsave(*fname, len2);
15560 pbuf3 = NULL;
15561
15562 s = pbuf2 + len2 - 1; /* Find the end */
15563 slen = 1;
15564 plen = len2;
15565
15566 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015567 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 {
15569 --s;
15570 ++slen;
15571 --plen;
15572 }
15573
15574 do
15575 {
15576 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015577 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 {
15579 --s;
15580 ++slen;
15581 --plen;
15582 }
15583 if (s <= pbuf2)
15584 break;
15585
15586 /* Remeber the character that is about to be blatted */
15587 ch = *s;
15588 *s = 0; /* get_short_pathname requires a null-terminated string */
15589
15590 /* Try it in situ */
15591 p = pbuf2;
15592 if (!get_short_pathname(&p, &pbuf3, &plen))
15593 {
15594 vim_free(pbuf2);
15595 return -1;
15596 }
15597 *s = ch; /* Preserve the string */
15598 } while (plen == 0);
15599
15600 if (plen > 0)
15601 {
15602 /* Remeber the length of the new string. */
15603 *fnamelen = len = plen + slen;
15604 vim_free(*bufp);
15605 if (len > len2)
15606 {
15607 /* If there's not enough space in the currently allocated string,
15608 * then copy it to a buffer big enough.
15609 */
15610 *fname= *bufp = vim_strnsave(p, len);
15611 if (*fname == NULL)
15612 return -1;
15613 }
15614 else
15615 {
15616 /* Transfer pbuf2 to being the main buffer (it's big enough) */
15617 *fname = *bufp = pbuf2;
15618 if (p != pbuf2)
15619 strncpy(*fname, p, plen);
15620 pbuf2 = NULL;
15621 }
15622 /* Concat the next bit */
15623 strncpy(*fname + plen, s, slen);
15624 (*fname)[len] = '\0';
15625 }
15626 vim_free(pbuf3);
15627 vim_free(pbuf2);
15628 return 0;
15629}
15630
15631/*
15632 * Get a pathname for a partial path.
15633 */
15634 static int
15635shortpath_for_partial(fnamep, bufp, fnamelen)
15636 char_u **fnamep;
15637 char_u **bufp;
15638 int *fnamelen;
15639{
15640 int sepcount, len, tflen;
15641 char_u *p;
15642 char_u *pbuf, *tfname;
15643 int hasTilde;
15644
15645 /* Count up the path seperators from the RHS.. so we know which part
15646 * of the path to return.
15647 */
15648 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015649 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 if (vim_ispathsep(*p))
15651 ++sepcount;
15652
15653 /* Need full path first (use expand_env() to remove a "~/") */
15654 hasTilde = (**fnamep == '~');
15655 if (hasTilde)
15656 pbuf = tfname = expand_env_save(*fnamep);
15657 else
15658 pbuf = tfname = FullName_save(*fnamep, FALSE);
15659
15660 len = tflen = STRLEN(tfname);
15661
15662 if (!get_short_pathname(&tfname, &pbuf, &len))
15663 return -1;
15664
15665 if (len == 0)
15666 {
15667 /* Don't have a valid filename, so shorten the rest of the
15668 * path if we can. This CAN give us invalid 8.3 filenames, but
15669 * there's not a lot of point in guessing what it might be.
15670 */
15671 len = tflen;
15672 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
15673 return -1;
15674 }
15675
15676 /* Count the paths backward to find the beginning of the desired string. */
15677 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015678 {
15679#ifdef FEAT_MBYTE
15680 if (has_mbyte)
15681 p -= mb_head_off(tfname, p);
15682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 if (vim_ispathsep(*p))
15684 {
15685 if (sepcount == 0 || (hasTilde && sepcount == 1))
15686 break;
15687 else
15688 sepcount --;
15689 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 if (hasTilde)
15692 {
15693 --p;
15694 if (p >= tfname)
15695 *p = '~';
15696 else
15697 return -1;
15698 }
15699 else
15700 ++p;
15701
15702 /* Copy in the string - p indexes into tfname - allocated at pbuf */
15703 vim_free(*bufp);
15704 *fnamelen = (int)STRLEN(p);
15705 *bufp = pbuf;
15706 *fnamep = p;
15707
15708 return 0;
15709}
15710#endif /* WIN3264 */
15711
15712/*
15713 * Adjust a filename, according to a string of modifiers.
15714 * *fnamep must be NUL terminated when called. When returning, the length is
15715 * determined by *fnamelen.
15716 * Returns valid flags.
15717 * When there is an error, *fnamep is set to NULL.
15718 */
15719 int
15720modify_fname(src, usedlen, fnamep, bufp, fnamelen)
15721 char_u *src; /* string with modifiers */
15722 int *usedlen; /* characters after src that are used */
15723 char_u **fnamep; /* file name so far */
15724 char_u **bufp; /* buffer for allocated file name or NULL */
15725 int *fnamelen; /* length of fnamep */
15726{
15727 int valid = 0;
15728 char_u *tail;
15729 char_u *s, *p, *pbuf;
15730 char_u dirname[MAXPATHL];
15731 int c;
15732 int has_fullname = 0;
15733#ifdef WIN3264
15734 int has_shortname = 0;
15735#endif
15736
15737repeat:
15738 /* ":p" - full path/file_name */
15739 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
15740 {
15741 has_fullname = 1;
15742
15743 valid |= VALID_PATH;
15744 *usedlen += 2;
15745
15746 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
15747 if ((*fnamep)[0] == '~'
15748#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
15749 && ((*fnamep)[1] == '/'
15750# ifdef BACKSLASH_IN_FILENAME
15751 || (*fnamep)[1] == '\\'
15752# endif
15753 || (*fnamep)[1] == NUL)
15754
15755#endif
15756 )
15757 {
15758 *fnamep = expand_env_save(*fnamep);
15759 vim_free(*bufp); /* free any allocated file name */
15760 *bufp = *fnamep;
15761 if (*fnamep == NULL)
15762 return -1;
15763 }
15764
15765 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015766 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 {
15768 if (vim_ispathsep(*p)
15769 && p[1] == '.'
15770 && (p[2] == NUL
15771 || vim_ispathsep(p[2])
15772 || (p[2] == '.'
15773 && (p[3] == NUL || vim_ispathsep(p[3])))))
15774 break;
15775 }
15776
15777 /* FullName_save() is slow, don't use it when not needed. */
15778 if (*p != NUL || !vim_isAbsName(*fnamep))
15779 {
15780 *fnamep = FullName_save(*fnamep, *p != NUL);
15781 vim_free(*bufp); /* free any allocated file name */
15782 *bufp = *fnamep;
15783 if (*fnamep == NULL)
15784 return -1;
15785 }
15786
15787 /* Append a path separator to a directory. */
15788 if (mch_isdir(*fnamep))
15789 {
15790 /* Make room for one or two extra characters. */
15791 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
15792 vim_free(*bufp); /* free any allocated file name */
15793 *bufp = *fnamep;
15794 if (*fnamep == NULL)
15795 return -1;
15796 add_pathsep(*fnamep);
15797 }
15798 }
15799
15800 /* ":." - path relative to the current directory */
15801 /* ":~" - path relative to the home directory */
15802 /* ":8" - shortname path - postponed till after */
15803 while (src[*usedlen] == ':'
15804 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
15805 {
15806 *usedlen += 2;
15807 if (c == '8')
15808 {
15809#ifdef WIN3264
15810 has_shortname = 1; /* Postpone this. */
15811#endif
15812 continue;
15813 }
15814 pbuf = NULL;
15815 /* Need full path first (use expand_env() to remove a "~/") */
15816 if (!has_fullname)
15817 {
15818 if (c == '.' && **fnamep == '~')
15819 p = pbuf = expand_env_save(*fnamep);
15820 else
15821 p = pbuf = FullName_save(*fnamep, FALSE);
15822 }
15823 else
15824 p = *fnamep;
15825
15826 has_fullname = 0;
15827
15828 if (p != NULL)
15829 {
15830 if (c == '.')
15831 {
15832 mch_dirname(dirname, MAXPATHL);
15833 s = shorten_fname(p, dirname);
15834 if (s != NULL)
15835 {
15836 *fnamep = s;
15837 if (pbuf != NULL)
15838 {
15839 vim_free(*bufp); /* free any allocated file name */
15840 *bufp = pbuf;
15841 pbuf = NULL;
15842 }
15843 }
15844 }
15845 else
15846 {
15847 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
15848 /* Only replace it when it starts with '~' */
15849 if (*dirname == '~')
15850 {
15851 s = vim_strsave(dirname);
15852 if (s != NULL)
15853 {
15854 *fnamep = s;
15855 vim_free(*bufp);
15856 *bufp = s;
15857 }
15858 }
15859 }
15860 vim_free(pbuf);
15861 }
15862 }
15863
15864 tail = gettail(*fnamep);
15865 *fnamelen = (int)STRLEN(*fnamep);
15866
15867 /* ":h" - head, remove "/file_name", can be repeated */
15868 /* Don't remove the first "/" or "c:\" */
15869 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
15870 {
15871 valid |= VALID_HEAD;
15872 *usedlen += 2;
15873 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015874 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 --tail;
15876 *fnamelen = (int)(tail - *fnamep);
15877#ifdef VMS
15878 if (*fnamelen > 0)
15879 *fnamelen += 1; /* the path separator is part of the path */
15880#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015881 while (tail > s && !after_pathsep(s, tail))
15882 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 }
15884
15885 /* ":8" - shortname */
15886 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
15887 {
15888 *usedlen += 2;
15889#ifdef WIN3264
15890 has_shortname = 1;
15891#endif
15892 }
15893
15894#ifdef WIN3264
15895 /* Check shortname after we have done 'heads' and before we do 'tails'
15896 */
15897 if (has_shortname)
15898 {
15899 pbuf = NULL;
15900 /* Copy the string if it is shortened by :h */
15901 if (*fnamelen < (int)STRLEN(*fnamep))
15902 {
15903 p = vim_strnsave(*fnamep, *fnamelen);
15904 if (p == 0)
15905 return -1;
15906 vim_free(*bufp);
15907 *bufp = *fnamep = p;
15908 }
15909
15910 /* Split into two implementations - makes it easier. First is where
15911 * there isn't a full name already, second is where there is.
15912 */
15913 if (!has_fullname && !vim_isAbsName(*fnamep))
15914 {
15915 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
15916 return -1;
15917 }
15918 else
15919 {
15920 int l;
15921
15922 /* Simple case, already have the full-name
15923 * Nearly always shorter, so try first time. */
15924 l = *fnamelen;
15925 if (!get_short_pathname(fnamep, bufp, &l))
15926 return -1;
15927
15928 if (l == 0)
15929 {
15930 /* Couldn't find the filename.. search the paths.
15931 */
15932 l = *fnamelen;
15933 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
15934 return -1;
15935 }
15936 *fnamelen = l;
15937 }
15938 }
15939#endif /* WIN3264 */
15940
15941 /* ":t" - tail, just the basename */
15942 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
15943 {
15944 *usedlen += 2;
15945 *fnamelen -= (int)(tail - *fnamep);
15946 *fnamep = tail;
15947 }
15948
15949 /* ":e" - extension, can be repeated */
15950 /* ":r" - root, without extension, can be repeated */
15951 while (src[*usedlen] == ':'
15952 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
15953 {
15954 /* find a '.' in the tail:
15955 * - for second :e: before the current fname
15956 * - otherwise: The last '.'
15957 */
15958 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
15959 s = *fnamep - 2;
15960 else
15961 s = *fnamep + *fnamelen - 1;
15962 for ( ; s > tail; --s)
15963 if (s[0] == '.')
15964 break;
15965 if (src[*usedlen + 1] == 'e') /* :e */
15966 {
15967 if (s > tail)
15968 {
15969 *fnamelen += (int)(*fnamep - (s + 1));
15970 *fnamep = s + 1;
15971#ifdef VMS
15972 /* cut version from the extension */
15973 s = *fnamep + *fnamelen - 1;
15974 for ( ; s > *fnamep; --s)
15975 if (s[0] == ';')
15976 break;
15977 if (s > *fnamep)
15978 *fnamelen = s - *fnamep;
15979#endif
15980 }
15981 else if (*fnamep <= tail)
15982 *fnamelen = 0;
15983 }
15984 else /* :r */
15985 {
15986 if (s > tail) /* remove one extension */
15987 *fnamelen = (int)(s - *fnamep);
15988 }
15989 *usedlen += 2;
15990 }
15991
15992 /* ":s?pat?foo?" - substitute */
15993 /* ":gs?pat?foo?" - global substitute */
15994 if (src[*usedlen] == ':'
15995 && (src[*usedlen + 1] == 's'
15996 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
15997 {
15998 char_u *str;
15999 char_u *pat;
16000 char_u *sub;
16001 int sep;
16002 char_u *flags;
16003 int didit = FALSE;
16004
16005 flags = (char_u *)"";
16006 s = src + *usedlen + 2;
16007 if (src[*usedlen + 1] == 'g')
16008 {
16009 flags = (char_u *)"g";
16010 ++s;
16011 }
16012
16013 sep = *s++;
16014 if (sep)
16015 {
16016 /* find end of pattern */
16017 p = vim_strchr(s, sep);
16018 if (p != NULL)
16019 {
16020 pat = vim_strnsave(s, (int)(p - s));
16021 if (pat != NULL)
16022 {
16023 s = p + 1;
16024 /* find end of substitution */
16025 p = vim_strchr(s, sep);
16026 if (p != NULL)
16027 {
16028 sub = vim_strnsave(s, (int)(p - s));
16029 str = vim_strnsave(*fnamep, *fnamelen);
16030 if (sub != NULL && str != NULL)
16031 {
16032 *usedlen = (int)(p + 1 - src);
16033 s = do_string_sub(str, pat, sub, flags);
16034 if (s != NULL)
16035 {
16036 *fnamep = s;
16037 *fnamelen = (int)STRLEN(s);
16038 vim_free(*bufp);
16039 *bufp = s;
16040 didit = TRUE;
16041 }
16042 }
16043 vim_free(sub);
16044 vim_free(str);
16045 }
16046 vim_free(pat);
16047 }
16048 }
16049 /* after using ":s", repeat all the modifiers */
16050 if (didit)
16051 goto repeat;
16052 }
16053 }
16054
16055 return valid;
16056}
16057
16058/*
16059 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
16060 * "flags" can be "g" to do a global substitute.
16061 * Returns an allocated string, NULL for error.
16062 */
16063 char_u *
16064do_string_sub(str, pat, sub, flags)
16065 char_u *str;
16066 char_u *pat;
16067 char_u *sub;
16068 char_u *flags;
16069{
16070 int sublen;
16071 regmatch_T regmatch;
16072 int i;
16073 int do_all;
16074 char_u *tail;
16075 garray_T ga;
16076 char_u *ret;
16077 char_u *save_cpo;
16078
16079 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
16080 save_cpo = p_cpo;
16081 p_cpo = (char_u *)"";
16082
16083 ga_init2(&ga, 1, 200);
16084
16085 do_all = (flags[0] == 'g');
16086
16087 regmatch.rm_ic = p_ic;
16088 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16089 if (regmatch.regprog != NULL)
16090 {
16091 tail = str;
16092 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
16093 {
16094 /*
16095 * Get some space for a temporary buffer to do the substitution
16096 * into. It will contain:
16097 * - The text up to where the match is.
16098 * - The substituted text.
16099 * - The text after the match.
16100 */
16101 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
16102 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
16103 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
16104 {
16105 ga_clear(&ga);
16106 break;
16107 }
16108
16109 /* copy the text up to where the match is */
16110 i = (int)(regmatch.startp[0] - tail);
16111 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
16112 /* add the substituted text */
16113 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
16114 + ga.ga_len + i, TRUE, TRUE, FALSE);
16115 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 /* avoid getting stuck on a match with an empty string */
16117 if (tail == regmatch.endp[0])
16118 {
16119 if (*tail == NUL)
16120 break;
16121 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
16122 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 }
16124 else
16125 {
16126 tail = regmatch.endp[0];
16127 if (*tail == NUL)
16128 break;
16129 }
16130 if (!do_all)
16131 break;
16132 }
16133
16134 if (ga.ga_data != NULL)
16135 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
16136
16137 vim_free(regmatch.regprog);
16138 }
16139
16140 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
16141 ga_clear(&ga);
16142 p_cpo = save_cpo;
16143
16144 return ret;
16145}
16146
16147#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */