blob: 50298e9f4b19481e3cddce27e407cf5362daaaed [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 */
Bram Moolenaara7043832005-01-21 11:56:39 +000069 char_u v_name[1]; /* name of variable (actually longer) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} var;
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072typedef var * VAR;
73
74/*
Bram Moolenaara7043832005-01-21 11:56:39 +000075 * In a hashtable item "hi_key" points to "v_name" in a variable.
76 * This avoids adding a pointer to the hashtable item.
77 * VAR2HIKEY() converts a var pointer to a hashitem key pointer.
78 * HIKEY2VAR() converts a hashitem key pointer to a var pointer.
79 * HI2VAR() converts a hashitem pointer to a var pointer.
80 */
81static var dumvar;
82#define VAR2HIKEY(v) ((v)->v_name)
83#define HIKEY2VAR(p) ((VAR)(p - (dumvar.v_name - (char_u *)&dumvar.tv)))
84#define HI2VAR(hi) HIKEY2VAR((hi)->hi_key)
85
86/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000087 * Structure to hold an item of a list: an internal variable without a name.
88 */
89struct listitem_S
90{
91 struct listitem_S *li_next; /* next item in list */
92 struct listitem_S *li_prev; /* previous item in list */
93 typeval li_tv; /* type and value of the variable */
94};
95
96typedef struct listitem_S listitem;
97
98/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000099 * Struct used by those that are using an item in a list.
100 */
101typedef struct listwatch_S
102{
103 listitem *lw_item; /* item being watched */
104 struct listwatch_S *lw_next; /* next watcher */
105} listwatch;
106
107/*
108 * Structure to hold info about a list.
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000109 */
110struct listvar_S
111{
112 int lv_refcount; /* reference count */
113 listitem *lv_first; /* first item, NULL if none */
114 listitem *lv_last; /* last item, NULL if none */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000115 listwatch *lv_watch; /* first watcher, NULL if none */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116};
117
118typedef struct listvar_S listvar;
119
Bram Moolenaare9a41262005-01-15 22:18:47 +0000120#define VAR_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000121
122/*
123 * Structure to hold an item of a Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000124 * The key is copied into "di_key" to avoid an extra alloc/free for it.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000125 */
126struct dictitem_S
127{
Bram Moolenaar8c711452005-01-14 21:53:12 +0000128 typeval di_tv; /* type and value of the variable */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000129 char_u di_key[1]; /* key (actually longer!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000130};
131
132typedef struct dictitem_S dictitem;
133
134/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000135 * In a hashtable item "hi_key" points to "di_key" in a dictitem.
136 * This avoids adding a pointer to the hashtable item.
137 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
138 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
139 * HI2DI() converts a hashitem pointer to a dictitem pointer.
140 */
141static dictitem dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +0000142#define DI2HIKEY(di) ((di)->di_key)
143#define HIKEY2DI(p) ((dictitem *)(p - (dumdi.di_key - (char_u *)&dumdi.di_tv)))
144#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000145
146/*
Bram Moolenaar8c711452005-01-14 21:53:12 +0000147 * Structure to hold info about a Dictionary.
148 */
149struct dictvar_S
150{
151 int dv_refcount; /* reference count */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000152 hashtable dv_hashtable; /* hashtable that refers to the items */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000153};
154
155typedef struct dictvar_S dictvar;
156
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000157/*
158 * Structure returned by get_lval() and used by set_var_lval().
159 * For a plain name:
160 * "name" points to the variable name.
161 * "exp_name" is NULL.
162 * "tv" is NULL
163 * For a magic braces name:
164 * "name" points to the expanded variable name.
165 * "exp_name" is non-NULL, to be freed later.
166 * "tv" is NULL
167 * For an index in a list:
168 * "name" points to the (expanded) variable name.
169 * "exp_name" NULL or non-NULL, to be freed later.
170 * "tv" points to the (first) list item value
171 * "li" points to the (first) list item
172 * "range", "n1", "n2" and "empty2" indicate what items are used.
173 * For an existing Dict item:
174 * "name" points to the (expanded) variable name.
175 * "exp_name" NULL or non-NULL, to be freed later.
176 * "tv" points to the dict item value
177 * "newkey" is NULL
178 * For a non-existing Dict item:
179 * "name" points to the (expanded) variable name.
180 * "exp_name" NULL or non-NULL, to be freed later.
181 * "tv" points to the Dictionary typeval
182 * "newkey" is the key for the new item.
183 */
184typedef struct lval_S
185{
186 char_u *ll_name; /* start of variable name (can be NULL) */
187 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
188 typeval *ll_tv; /* Typeval of item being used. If "newkey"
189 isn't NULL it's the Dict to which to add
190 the item. */
191 listitem *ll_li; /* The list item or NULL. */
192 listvar *ll_list; /* The list or NULL. */
193 int ll_range; /* TRUE when a [i:j] range was used */
194 long ll_n1; /* First index for list */
195 long ll_n2; /* Second index for list range */
196 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000197 dictvar *ll_dict; /* The Dictionary or NULL */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000198 dictitem *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000199 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000200} lval;
201
Bram Moolenaar8c711452005-01-14 21:53:12 +0000202
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000203static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000204static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000205static char *e_undefvar = N_("E121: Undefined variable: %s");
206static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000207static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +0000208static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000209static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000210static char *e_listreq = N_("E714: List required");
211static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000212static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000213static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
214static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
215static char *e_funcdict = N_("E717: Dictionary entry already exists");
216static char *e_funcref = N_("E718: Funcref required");
217static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
218static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000219
220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 * All user-defined global variables are stored in "variables".
222 */
Bram Moolenaara7043832005-01-21 11:56:39 +0000223hashtable variables;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
225/*
Bram Moolenaara7043832005-01-21 11:56:39 +0000226 * Array to hold the hashtable with variables local to each sourced script.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 */
Bram Moolenaara7043832005-01-21 11:56:39 +0000228static garray_T ga_scripts = {0, 0, sizeof(hashtable), 4, NULL};
229#define SCRIPT_VARS(id) (((hashtable *)ga_scripts.ga_data)[(id) - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231static int echo_attr = 0; /* attributes used for ":echo" */
232
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000233/* Values for trans_function_name() argument: */
234#define TFN_INT 1 /* internal function name OK */
235#define TFN_QUIET 2 /* no error messages */
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237/*
238 * Structure to hold info for a user function.
239 */
240typedef struct ufunc ufunc_T;
241
242struct ufunc
243{
244 ufunc_T *next; /* next function in list */
245 char_u *name; /* name of function; can start with <SNR>123_
246 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
247 int varargs; /* variable nr of arguments */
248 int flags;
249 int calls; /* nr of active calls */
250 garray_T args; /* arguments */
251 garray_T lines; /* function lines */
252 scid_T script_ID; /* ID of script where function was defined,
253 used for s: variables */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000254 int refcount; /* for numbered function: reference count */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255};
256
257/* function flags */
258#define FC_ABORT 1 /* abort function on error */
259#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000260#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262/*
263 * All user-defined functions are found in the forward-linked function list.
264 * The first function is pointed at by firstfunc.
265 */
266ufunc_T *firstfunc = NULL;
267
268#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
269#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
270
271/* structure to hold info for a function that is currently being executed. */
272struct funccall
273{
274 ufunc_T *func; /* function being called */
275 int linenr; /* next line to be executed */
276 int returned; /* ":return" used */
277 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000278 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 var a0_var; /* "a:0" variable */
280 var firstline; /* "a:firstline" variable */
281 var lastline; /* "a:lastline" variable */
Bram Moolenaara7043832005-01-21 11:56:39 +0000282 hashtable l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000283 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 linenr_T breakpoint; /* next line with breakpoint or zero */
285 int dbg_tick; /* debug_tick when breakpoint was set */
286 int level; /* top nesting level of executed function */
287};
288
289/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000290 * Info used by a ":for" loop.
291 */
292typedef struct forinfo_S
293{
294 int fi_semicolon; /* TRUE if ending in '; var]' */
295 int fi_varcount; /* nr of variables in the list */
296 listwatch fi_lw; /* keep an eye on the item used. */
297 listvar *fi_list; /* list being used */
298} forinfo;
299
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000300/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000301 * Struct used by trans_function_name()
302 */
303typedef struct
304{
305 dictvar *fd_dict; /* Dictionary used */
306 char_u *fd_newkey; /* new key in "dict" */
307 dictitem *fd_di; /* Dictionary item used */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000308} funcdict;
309
Bram Moolenaara7043832005-01-21 11:56:39 +0000310
311/*
312 * Initialize the global variables.
313 */
314 void
315eval_init()
316{
317 hash_init(&variables);
318}
319
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 * Return the name of the executed function.
322 */
323 char_u *
324func_name(cookie)
325 void *cookie;
326{
327 return ((struct funccall *)cookie)->func->name;
328}
329
330/*
331 * Return the address holding the next breakpoint line for a funccall cookie.
332 */
333 linenr_T *
334func_breakpoint(cookie)
335 void *cookie;
336{
337 return &((struct funccall *)cookie)->breakpoint;
338}
339
340/*
341 * Return the address holding the debug tick for a funccall cookie.
342 */
343 int *
344func_dbg_tick(cookie)
345 void *cookie;
346{
347 return &((struct funccall *)cookie)->dbg_tick;
348}
349
350/*
351 * Return the nesting level for a funccall cookie.
352 */
353 int
354func_level(cookie)
355 void *cookie;
356{
357 return ((struct funccall *)cookie)->level;
358}
359
360/* pointer to funccal for currently active function */
361struct funccall *current_funccal = NULL;
362
363/*
364 * Return TRUE when a function was ended by a ":return" command.
365 */
366 int
367current_func_returned()
368{
369 return current_funccal->returned;
370}
371
372
373/*
374 * Array to hold the value of v: variables.
375 */
376#include "version.h"
377
378/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000379#define VV_COMPAT 1 /* compatible, also used without "v:" */
380#define VV_RO 2 /* read-only */
381#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
Bram Moolenaare9a41262005-01-15 22:18:47 +0000383#define VV_NAME(s) s, sizeof(s) - 1
384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385struct vimvar
386{
387 char *name; /* name of variable, without v: */
388 int len; /* length of name */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000389 typeval tv; /* type and value */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000390 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391} vimvars[VV_LEN] =
Bram Moolenaare9a41262005-01-15 22:18:47 +0000392{
393 /*
394 * The order here must match the VV_ defines in vim.h!
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000395 * Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare9a41262005-01-15 22:18:47 +0000396 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000397 {VV_NAME("count"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
398 {VV_NAME("count1"), {VAR_NUMBER}, VV_RO},
399 {VV_NAME("prevcount"), {VAR_NUMBER}, VV_RO},
400 {VV_NAME("errmsg"), {VAR_STRING}, VV_COMPAT},
401 {VV_NAME("warningmsg"), {VAR_STRING}, 0},
402 {VV_NAME("statusmsg"), {VAR_STRING}, 0},
403 {VV_NAME("shell_error"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
404 {VV_NAME("this_session"), {VAR_STRING}, VV_COMPAT},
405 {VV_NAME("version"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
406 {VV_NAME("lnum"), {VAR_NUMBER}, VV_RO_SBX},
407 {VV_NAME("termresponse"), {VAR_STRING}, VV_RO},
408 {VV_NAME("fname"), {VAR_STRING}, VV_RO},
409 {VV_NAME("lang"), {VAR_STRING}, VV_RO},
410 {VV_NAME("lc_time"), {VAR_STRING}, VV_RO},
411 {VV_NAME("ctype"), {VAR_STRING}, VV_RO},
412 {VV_NAME("charconvert_from"), {VAR_STRING}, VV_RO},
413 {VV_NAME("charconvert_to"), {VAR_STRING}, VV_RO},
414 {VV_NAME("fname_in"), {VAR_STRING}, VV_RO},
415 {VV_NAME("fname_out"), {VAR_STRING}, VV_RO},
416 {VV_NAME("fname_new"), {VAR_STRING}, VV_RO},
417 {VV_NAME("fname_diff"), {VAR_STRING}, VV_RO},
418 {VV_NAME("cmdarg"), {VAR_STRING}, VV_RO},
419 {VV_NAME("foldstart"), {VAR_NUMBER}, VV_RO_SBX},
420 {VV_NAME("foldend"), {VAR_NUMBER}, VV_RO_SBX},
421 {VV_NAME("folddashes"), {VAR_STRING}, VV_RO_SBX},
422 {VV_NAME("foldlevel"), {VAR_NUMBER}, VV_RO_SBX},
423 {VV_NAME("progname"), {VAR_STRING}, VV_RO},
424 {VV_NAME("servername"), {VAR_STRING}, VV_RO},
425 {VV_NAME("dying"), {VAR_NUMBER}, VV_RO},
426 {VV_NAME("exception"), {VAR_STRING}, VV_RO},
427 {VV_NAME("throwpoint"), {VAR_STRING}, VV_RO},
428 {VV_NAME("register"), {VAR_STRING}, VV_RO},
429 {VV_NAME("cmdbang"), {VAR_NUMBER}, VV_RO},
430 {VV_NAME("insertmode"), {VAR_STRING}, VV_RO},
431 {VV_NAME("val"), {VAR_UNKNOWN}, VV_RO},
432 {VV_NAME("key"), {VAR_UNKNOWN}, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433};
434
Bram Moolenaare9a41262005-01-15 22:18:47 +0000435/* shorthand */
436#define vv_nr tv.vval.v_number
437#define vv_str tv.vval.v_string
438
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000439static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
440static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
441static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
442static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
443static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
444static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
445static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
446static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
447static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
448static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
449static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
450static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
451static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000452static listvar *list_alloc __ARGS((void));
453static void list_unref __ARGS((listvar *l));
454static void list_free __ARGS((listvar *l));
455static listitem *listitem_alloc __ARGS((void));
456static void listitem_free __ARGS((listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000457static void listitem_remove __ARGS((listvar *l, listitem *item));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000458static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000459static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000460static int dict_equal __ARGS((dictvar *d1, dictvar *d2, int ic));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000461static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000462static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000463static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000464static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000465static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000466static void list_append __ARGS((listvar *l, listitem *item));
467static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000468static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
469static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
470static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000471static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000472static void list_remove __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000473static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000474static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
475
Bram Moolenaar8c711452005-01-14 21:53:12 +0000476static dictvar *dict_alloc __ARGS((void));
477static void dict_unref __ARGS((dictvar *d));
478static void dict_free __ARGS((dictvar *d));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000479static dictitem *dictitem_alloc __ARGS((char_u *key));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000480static dictitem *dictitem_copy __ARGS((dictitem *org));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000481static void dictitem_remove __ARGS((dictvar *dict, dictitem *item));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000482static void dictitem_free __ARGS((dictitem *item));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000483static int dict_add __ARGS((dictvar *d, dictitem *item));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000484static long dict_len __ARGS((dictvar *d));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000485static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000486static char_u *dict2string __ARGS((typeval *tv));
487static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
488
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000489static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000490static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000491static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000492static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000494static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000495static 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));
496static 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 +0000497
498static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000499static void f_append __ARGS((typeval *argvars, typeval *rettv));
500static void f_argc __ARGS((typeval *argvars, typeval *rettv));
501static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
502static void f_argv __ARGS((typeval *argvars, typeval *rettv));
503static void f_browse __ARGS((typeval *argvars, typeval *rettv));
504static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000505static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
506static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
507static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000508static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
509static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
510static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
511static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
512static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000513static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000514static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
515static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
516static void f_col __ARGS((typeval *argvars, typeval *rettv));
517static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
518static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000519static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000520static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
521static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
522static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
523static void f_delete __ARGS((typeval *argvars, typeval *rettv));
524static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
525static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
526static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000527static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000529static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000530static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
531static void f_executable __ARGS((typeval *argvars, typeval *rettv));
532static void f_exists __ARGS((typeval *argvars, typeval *rettv));
533static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000534static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
536static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000537static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000538static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
539static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000540static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
541static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
542static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000543static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
544static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
545static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
546static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
547static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000548static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000549static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
550static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
551static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
552static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
553static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
554static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
555static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
556static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
557static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
558static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
559static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
560static void f_getline __ARGS((typeval *argvars, typeval *rettv));
561static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
562static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
563static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
564static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
565static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
566static void f_glob __ARGS((typeval *argvars, typeval *rettv));
567static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
568static void f_has __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000569static void f_has_key __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000570static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
571static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
572static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
573static void f_histget __ARGS((typeval *argvars, typeval *rettv));
574static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000575static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000576static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000577static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
578static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
579static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000580static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000581static void f_input __ARGS((typeval *argvars, typeval *rettv));
582static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
583static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
584static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
585static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000586static void f_insert __ARGS((typeval *argvars, typeval *rettv));
587static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000588static void f_items __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000589static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000590static void f_keys __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000591static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
592static void f_len __ARGS((typeval *argvars, typeval *rettv));
593static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
594static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595static void f_line __ARGS((typeval *argvars, typeval *rettv));
596static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
597static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
598static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000599static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
601static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000602static void f_match __ARGS((typeval *argvars, typeval *rettv));
603static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
604static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000605static void f_max __ARGS((typeval *argvars, typeval *rettv));
606static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000607static void f_mode __ARGS((typeval *argvars, typeval *rettv));
608static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
609static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
610static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000611static void f_range __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000612static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
613static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
614static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
615static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
616static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000617static void f_remove __ARGS((typeval *argvars, typeval *rettv));
618static void f_rename __ARGS((typeval *argvars, typeval *rettv));
619static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
620static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
621static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
622static void f_search __ARGS((typeval *argvars, typeval *rettv));
623static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000624static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
625static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000626static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
627static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000628static void f_setline __ARGS((typeval *argvars, typeval *rettv));
629static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000630static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000632static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000633static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000634#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000635static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000636#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000637static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
638static void f_string __ARGS((typeval *argvars, typeval *rettv));
639static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
640static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
641static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
642static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000643static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
644static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000645static void f_synID __ARGS((typeval *argvars, typeval *rettv));
646static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
647static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
648static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000649static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
650static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
651static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
652static void f_tr __ARGS((typeval *argvars, typeval *rettv));
653static void f_type __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000654static void f_values __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000655static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
656static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
657static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
658static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
659static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
660static void f_winline __ARGS((typeval *argvars, typeval *rettv));
661static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
662static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
663static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000664
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000665static win_T *find_win_by_nr __ARGS((typeval *vp));
666static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667static int get_env_len __ARGS((char_u **arg));
668static int get_id_len __ARGS((char_u **arg));
Bram Moolenaara7043832005-01-21 11:56:39 +0000669static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000670static 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 +0000671static int eval_isnamec __ARGS((int c));
672static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000673static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
674static typeval *alloc_tv __ARGS((void));
675static typeval *alloc_string_tv __ARGS((char_u *string));
676static void free_tv __ARGS((typeval *varp));
677static void clear_tv __ARGS((typeval *varp));
678static void init_tv __ARGS((typeval *varp));
679static long get_tv_number __ARGS((typeval *varp));
680static linenr_T get_tv_lnum __ARGS((typeval *argvars));
681static char_u *get_tv_string __ARGS((typeval *varp));
682static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaara7043832005-01-21 11:56:39 +0000683static VAR find_var __ARGS((char_u *name, hashtable **htp));
684static VAR find_var_in_ht __ARGS((hashtable *ht, char_u *varname));
685static hashtable *find_var_ht __ARGS((char_u *name, char_u **varname));
686static void delete_var __ARGS((hashtable *ht, hashitem *hi));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687static void list_one_var __ARGS((VAR v, char_u *prefix));
688static void list_vim_var __ARGS((int i));
689static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000690static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000691static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000692static void item_copy __ARGS((typeval *from, typeval *to, int deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000694static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict *fd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695static int eval_fname_script __ARGS((char_u *p));
696static int eval_fname_sid __ARGS((char_u *p));
697static void list_func_head __ARGS((ufunc_T *fp, int indent));
698static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
699static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000700static int function_exists __ARGS((char_u *name));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000701static void func_free __ARGS((ufunc_T *fp));
702static void func_unref __ARGS((char_u *name));
703static void func_ref __ARGS((char_u *name));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000704static 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 +0000705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706static 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 +0000707
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000708static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
709static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
710static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaara7043832005-01-21 11:56:39 +0000711static void list_hashtable_vars __ARGS((hashtable *ht, char_u *prefix));
712static void list_glob_vars __ARGS((void));
713static void list_buf_vars __ARGS((void));
714static void list_win_vars __ARGS((void));
715static void list_vim_vars __ARGS((void));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000717static 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 +0000718static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000719static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet));
720static void clear_lval __ARGS((lval *lp));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000721static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy, char_u *op));
722static int tv_op __ARGS((typeval *tv1, typeval *tv2, char_u *op));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000723static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000724static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
725static void list_fix_watch __ARGS((listvar *l, listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000726static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728/*
729 * Set an internal variable to a string value. Creates the variable if it does
730 * not already exist.
731 */
732 void
733set_internal_string_var(name, value)
734 char_u *name;
735 char_u *value;
736{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000737 char_u *val;
738 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
740 val = vim_strsave(value);
741 if (val != NULL)
742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000743 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000744 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000746 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000747 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 }
750}
751
752# if defined(FEAT_MBYTE) || defined(PROTO)
753 int
754eval_charconvert(enc_from, enc_to, fname_from, fname_to)
755 char_u *enc_from;
756 char_u *enc_to;
757 char_u *fname_from;
758 char_u *fname_to;
759{
760 int err = FALSE;
761
762 set_vim_var_string(VV_CC_FROM, enc_from, -1);
763 set_vim_var_string(VV_CC_TO, enc_to, -1);
764 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
765 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
766 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
767 err = TRUE;
768 set_vim_var_string(VV_CC_FROM, NULL, -1);
769 set_vim_var_string(VV_CC_TO, NULL, -1);
770 set_vim_var_string(VV_FNAME_IN, NULL, -1);
771 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
772
773 if (err)
774 return FAIL;
775 return OK;
776}
777# endif
778
779# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
780 int
781eval_printexpr(fname, args)
782 char_u *fname;
783 char_u *args;
784{
785 int err = FALSE;
786
787 set_vim_var_string(VV_FNAME_IN, fname, -1);
788 set_vim_var_string(VV_CMDARG, args, -1);
789 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
790 err = TRUE;
791 set_vim_var_string(VV_FNAME_IN, NULL, -1);
792 set_vim_var_string(VV_CMDARG, NULL, -1);
793
794 if (err)
795 {
796 mch_remove(fname);
797 return FAIL;
798 }
799 return OK;
800}
801# endif
802
803# if defined(FEAT_DIFF) || defined(PROTO)
804 void
805eval_diff(origfile, newfile, outfile)
806 char_u *origfile;
807 char_u *newfile;
808 char_u *outfile;
809{
810 int err = FALSE;
811
812 set_vim_var_string(VV_FNAME_IN, origfile, -1);
813 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
814 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
815 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
816 set_vim_var_string(VV_FNAME_IN, NULL, -1);
817 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
818 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
819}
820
821 void
822eval_patch(origfile, difffile, outfile)
823 char_u *origfile;
824 char_u *difffile;
825 char_u *outfile;
826{
827 int err;
828
829 set_vim_var_string(VV_FNAME_IN, origfile, -1);
830 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
831 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
832 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
833 set_vim_var_string(VV_FNAME_IN, NULL, -1);
834 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
835 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
836}
837# endif
838
839/*
840 * Top level evaluation function, returning a boolean.
841 * Sets "error" to TRUE if there was an error.
842 * Return TRUE or FALSE.
843 */
844 int
845eval_to_bool(arg, error, nextcmd, skip)
846 char_u *arg;
847 int *error;
848 char_u **nextcmd;
849 int skip; /* only parse, don't execute */
850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 int retval = FALSE;
853
854 if (skip)
855 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else
859 {
860 *error = FALSE;
861 if (!skip)
862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 retval = (get_tv_number(&tv) != 0);
864 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 }
867 if (skip)
868 --emsg_skip;
869
870 return retval;
871}
872
873/*
874 * Top level evaluation function, returning a string. If "skip" is TRUE,
875 * only parsing to "nextcmd" is done, without reporting errors. Return
876 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
877 */
878 char_u *
879eval_to_string_skip(arg, nextcmd, skip)
880 char_u *arg;
881 char_u **nextcmd;
882 int skip; /* only parse, don't execute */
883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 char_u *retval;
886
887 if (skip)
888 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 retval = NULL;
891 else
892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 retval = vim_strsave(get_tv_string(&tv));
894 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 if (skip)
897 --emsg_skip;
898
899 return retval;
900}
901
902/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000903 * Skip over an expression at "*pp".
904 * Return FAIL for an error, OK otherwise.
905 */
906 int
907skip_expr(pp)
908 char_u **pp;
909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000910 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000911
912 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000913 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000914}
915
916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 * Top level evaluation function, returning a string.
918 * Return pointer to allocated memory, or NULL for failure.
919 */
920 char_u *
921eval_to_string(arg, nextcmd)
922 char_u *arg;
923 char_u **nextcmd;
924{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 char_u *retval;
927
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 retval = NULL;
930 else
931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000932 retval = vim_strsave(get_tv_string(&tv));
933 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935
936 return retval;
937}
938
939/*
940 * Call eval_to_string() with "sandbox" set and not using local variables.
941 */
942 char_u *
943eval_to_string_safe(arg, nextcmd)
944 char_u *arg;
945 char_u **nextcmd;
946{
947 char_u *retval;
948 void *save_funccalp;
949
950 save_funccalp = save_funccal();
951 ++sandbox;
952 retval = eval_to_string(arg, nextcmd);
953 --sandbox;
954 restore_funccal(save_funccalp);
955 return retval;
956}
957
958#if 0 /* not used */
959/*
960 * Top level evaluation function, returning a string.
961 * Advances "arg" to the first non-blank after the evaluated expression.
962 * Return pointer to allocated memory, or NULL for failure.
963 * Doesn't give error messages.
964 */
965 char_u *
966eval_arg_to_string(arg)
967 char_u **arg;
968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000969 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 char_u *retval;
971 int ret;
972
973 ++emsg_off;
974
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (ret == FAIL)
977 retval = NULL;
978 else
979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000980 retval = vim_strsave(get_tv_string(&rettv));
981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983
984 --emsg_off;
985
986 return retval;
987}
988#endif
989
990/*
991 * Top level evaluation function, returning a number.
992 * Evaluates "expr" silently.
993 * Returns -1 for an error.
994 */
995 int
996eval_to_number(expr)
997 char_u *expr;
998{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000999 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001001 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003 ++emsg_off;
1004
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 retval = -1;
1007 else
1008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 retval = get_tv_number(&rettv);
1010 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 --emsg_off;
1013
1014 return retval;
1015}
1016
1017#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1018/*
1019 * Call some vimL function and return the result as a string
1020 * Uses argv[argc] for the function arguments.
1021 */
1022 char_u *
1023call_vim_function(func, argc, argv, safe)
1024 char_u *func;
1025 int argc;
1026 char_u **argv;
1027 int safe; /* use the sandbox */
1028{
1029 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001031 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 long n;
1033 int len;
1034 int i;
1035 int doesrange;
1036 void *save_funccalp = NULL;
1037
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001038 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (argvars == NULL)
1040 return NULL;
1041
1042 for (i = 0; i < argc; i++)
1043 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001044 /* Pass a NULL or empty argument as an empty string */
1045 if (argv[i] == NULL || *argv[i] == NUL)
1046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001047 argvars[i].v_type = VAR_STRING;
1048 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001049 continue;
1050 }
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 /* Recognize a number argument, the others must be strings. */
1053 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1054 if (len != 0 && len == (int)STRLEN(argv[i]))
1055 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001056 argvars[i].v_type = VAR_NUMBER;
1057 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 else
1060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001061 argvars[i].v_type = VAR_STRING;
1062 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064 }
1065
1066 if (safe)
1067 {
1068 save_funccalp = save_funccal();
1069 ++sandbox;
1070 }
1071
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001072 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1073 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001075 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001078 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 vim_free(argvars);
1080
1081 if (safe)
1082 {
1083 --sandbox;
1084 restore_funccal(save_funccalp);
1085 }
1086 return retval;
1087}
1088#endif
1089
1090/*
1091 * Save the current function call pointer, and set it to NULL.
1092 * Used when executing autocommands and for ":source".
1093 */
1094 void *
1095save_funccal()
1096{
1097 struct funccall *fc;
1098
1099 fc = current_funccal;
1100 current_funccal = NULL;
1101 return (void *)fc;
1102}
1103
1104 void
1105restore_funccal(fc)
1106 void *fc;
1107{
1108 current_funccal = (struct funccall *)fc;
1109}
1110
1111#ifdef FEAT_FOLDING
1112/*
1113 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1114 * it in "*cp". Doesn't give error messages.
1115 */
1116 int
1117eval_foldexpr(arg, cp)
1118 char_u *arg;
1119 int *cp;
1120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int retval;
1123 char_u *s;
1124
1125 ++emsg_off;
1126 ++sandbox;
1127 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 retval = 0;
1130 else
1131 {
1132 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 if (tv.v_type == VAR_NUMBER)
1134 retval = tv.vval.v_number;
1135 else if (tv.v_type == VAR_UNKNOWN
1136 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 retval = 0;
1138 else
1139 {
1140 /* If the result is a string, check if there is a non-digit before
1141 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (!VIM_ISDIGIT(*s) && *s != '-')
1144 *cp = *s++;
1145 retval = atol((char *)s);
1146 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 --emsg_off;
1150 --sandbox;
1151
1152 return retval;
1153}
1154#endif
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156/*
1157 * Expands out the 'magic' {}'s in a variable/function name.
1158 * Note that this can call itself recursively, to deal with
1159 * constructs like foo{bar}{baz}{bam}
1160 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1161 * "in_start" ^
1162 * "expr_start" ^
1163 * "expr_end" ^
1164 * "in_end" ^
1165 *
1166 * Returns a new allocated string, which the caller must free.
1167 * Returns NULL for failure.
1168 */
1169 static char_u *
1170make_expanded_name(in_start, expr_start, expr_end, in_end)
1171 char_u *in_start;
1172 char_u *expr_start;
1173 char_u *expr_end;
1174 char_u *in_end;
1175{
1176 char_u c1;
1177 char_u *retval = NULL;
1178 char_u *temp_result;
1179 char_u *nextcmd = NULL;
1180
1181 if (expr_end == NULL || in_end == NULL)
1182 return NULL;
1183 *expr_start = NUL;
1184 *expr_end = NUL;
1185 c1 = *in_end;
1186 *in_end = NUL;
1187
1188 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1189 if (temp_result != NULL && nextcmd == NULL)
1190 {
1191 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1192 + (in_end - expr_end) + 1));
1193
1194 if (retval != NULL)
1195 {
1196 STRCPY(retval, in_start);
1197 STRCAT(retval, temp_result);
1198 STRCAT(retval, expr_end + 1);
1199 }
1200 }
1201 vim_free(temp_result);
1202
1203 *in_end = c1; /* put char back for error messages */
1204 *expr_start = '{';
1205 *expr_end = '}';
1206
1207 if (retval != NULL)
1208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (expr_start != NULL)
1211 {
1212 /* Further expansion! */
1213 temp_result = make_expanded_name(retval, expr_start,
1214 expr_end, temp_result);
1215 vim_free(retval);
1216 retval = temp_result;
1217 }
1218 }
1219
1220 return retval;
1221
1222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 * ":let" list all variable values
1226 * ":let var1 var2" list variable values
1227 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001228 * ":let var += expr" assignment command.
1229 * ":let var -= expr" assignment command.
1230 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
1233 void
1234ex_let(eap)
1235 exarg_T *eap;
1236{
1237 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 char_u *expr = NULL;
1239 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 int var_count = 0;
1242 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001243 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001245 expr = skip_var_list(arg, &var_count, &semicolon);
1246 if (expr == NULL)
1247 return;
1248 expr = vim_strchr(expr, '=');
1249 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001251 if (*arg == '[')
1252 EMSG(_(e_invarg));
1253 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 /* ":let var1 var2" */
1255 arg = list_arg_vars(eap, arg);
1256 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001259 list_glob_vars();
1260 list_buf_vars();
1261 list_win_vars();
1262 list_vim_vars();
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 eap->nextcmd = check_nextcmd(arg);
1265 }
1266 else
1267 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 op[0] = '=';
1269 op[1] = NUL;
1270 if (expr > arg)
1271 {
1272 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1273 op[0] = expr[-1]; /* +=, -= or .= */
1274 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001275 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 if (eap->skip)
1278 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001279 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (eap->skip)
1281 {
1282 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 --emsg_skip;
1285 }
1286 else if (i != FAIL)
1287 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001288 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001289 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293}
1294
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295/*
1296 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1297 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001298 * When "nextchars" is not NULL it points to a string with characters that
1299 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1300 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001301 * Returns OK or FAIL;
1302 */
1303 static int
1304ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1305 char_u *arg_start;
1306 typeval *tv;
1307 int copy; /* copy values from "tv", don't move */
1308 int semicolon; /* from skip_var_list() */
1309 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001311{
1312 char_u *arg = arg_start;
1313 listvar *l;
1314 int i;
1315 listitem *item;
1316 typeval ltv;
1317
1318 if (*arg != '[')
1319 {
1320 /*
1321 * ":let var = expr" or ":for var in list"
1322 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001323 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 return FAIL;
1325 return OK;
1326 }
1327
1328 /*
1329 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1330 */
1331 l = tv->vval.v_list;
1332 if (tv->v_type != VAR_LIST || l == NULL)
1333 {
1334 EMSG(_(e_listreq));
1335 return FAIL;
1336 }
1337
1338 i = list_len(l);
1339 if (semicolon == 0 && var_count < i)
1340 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001341 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001342 return FAIL;
1343 }
1344 if (var_count - semicolon > i)
1345 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001346 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001347 return FAIL;
1348 }
1349
1350 item = l->lv_first;
1351 while (*arg != ']')
1352 {
1353 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001354 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355 item = item->li_next;
1356 if (arg == NULL)
1357 return FAIL;
1358
1359 arg = skipwhite(arg);
1360 if (*arg == ';')
1361 {
1362 /* Put the rest of the list (may be empty) in the var after ';'.
1363 * Create a new list for this. */
1364 l = list_alloc();
1365 if (l == NULL)
1366 return FAIL;
1367 while (item != NULL)
1368 {
1369 list_append_tv(l, &item->li_tv);
1370 item = item->li_next;
1371 }
1372
1373 ltv.v_type = VAR_LIST;
1374 ltv.vval.v_list = l;
1375 l->lv_refcount = 1;
1376
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1378 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001379 clear_tv(&ltv);
1380 if (arg == NULL)
1381 return FAIL;
1382 break;
1383 }
1384 else if (*arg != ',' && *arg != ']')
1385 {
1386 EMSG2(_(e_intern2), "ex_let_vars()");
1387 return FAIL;
1388 }
1389 }
1390
1391 return OK;
1392}
1393
1394/*
1395 * Skip over assignable variable "var" or list of variables "[var, var]".
1396 * Used for ":let varvar = expr" and ":for varvar in expr".
1397 * For "[var, var]" increment "*var_count" for each variable.
1398 * for "[var, var; var]" set "semicolon".
1399 * Return NULL for an error.
1400 */
1401 static char_u *
1402skip_var_list(arg, var_count, semicolon)
1403 char_u *arg;
1404 int *var_count;
1405 int *semicolon;
1406{
1407 char_u *p, *s;
1408
1409 if (*arg == '[')
1410 {
1411 /* "[var, var]": find the matching ']'. */
1412 p = arg;
1413 while (1)
1414 {
1415 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1416 s = skip_var_one(p);
1417 if (s == p)
1418 {
1419 EMSG2(_(e_invarg2), p);
1420 return NULL;
1421 }
1422 ++*var_count;
1423
1424 p = skipwhite(s);
1425 if (*p == ']')
1426 break;
1427 else if (*p == ';')
1428 {
1429 if (*semicolon == 1)
1430 {
1431 EMSG(_("Double ; in list of variables"));
1432 return NULL;
1433 }
1434 *semicolon = 1;
1435 }
1436 else if (*p != ',')
1437 {
1438 EMSG2(_(e_invarg2), p);
1439 return NULL;
1440 }
1441 }
1442 return p + 1;
1443 }
1444 else
1445 return skip_var_one(arg);
1446}
1447
1448 static char_u *
1449skip_var_one(arg)
1450 char_u *arg;
1451{
1452 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1453 ++arg;
1454 return find_name_end(arg, NULL, NULL, TRUE);
1455}
1456
Bram Moolenaara7043832005-01-21 11:56:39 +00001457/*
1458 * List variables for hashtable "ht" with prefix "prefix".
1459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460 static void
Bram Moolenaara7043832005-01-21 11:56:39 +00001461list_hashtable_vars(ht, prefix)
1462 hashtable *ht;
1463 char_u *prefix;
1464{
1465 hashitem *hi;
1466 int todo;
1467
1468 todo = ht->ht_used;
1469 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1470 {
1471 if (!HASHITEM_EMPTY(hi))
1472 {
1473 --todo;
1474 list_one_var(HI2VAR(hi), prefix);
1475 }
1476 }
1477}
1478
1479/*
1480 * List global variables.
1481 */
1482 static void
1483list_glob_vars()
1484{
1485 list_hashtable_vars(&variables, (char_u *)"");
1486}
1487
1488/*
1489 * List buffer variables.
1490 */
1491 static void
1492list_buf_vars()
1493{
1494 list_hashtable_vars(&curbuf->b_vars, (char_u *)"b:");
1495}
1496
1497/*
1498 * List window variables.
1499 */
1500 static void
1501list_win_vars()
1502{
1503 list_hashtable_vars(&curwin->w_vars, (char_u *)"w:");
1504}
1505
1506/*
1507 * List Vim variables.
1508 */
1509 static void
1510list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511{
1512 int i;
1513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 for (i = 0; i < VV_LEN && !got_int; ++i)
Bram Moolenaare9a41262005-01-15 22:18:47 +00001515 if (vimvars[i].tv.v_type == VAR_NUMBER || vimvars[i].vv_str != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 list_vim_var(i);
1517}
1518
1519/*
1520 * List variables in "arg".
1521 */
1522 static char_u *
1523list_arg_vars(eap, arg)
1524 exarg_T *eap;
1525 char_u *arg;
1526{
1527 int error = FALSE;
1528 char_u *temp_string = NULL;
1529 int arg_len;
1530 char_u *expr_start;
1531 char_u *expr_end;
1532 char_u *name_end;
1533 int c1 = 0, c2;
1534 int i;
1535 VAR varp;
1536 char_u *name;
1537
1538 while (!ends_excmd(*arg) && !got_int)
1539 {
1540 /* Find the end of the name. */
1541 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1542
1543 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1544 {
1545 emsg_severe = TRUE;
1546 EMSG(_(e_trailing));
1547 break;
1548 }
1549 if (!error && !eap->skip)
1550 {
1551 if (expr_start != NULL)
1552 {
1553 temp_string = make_expanded_name(arg, expr_start,
1554 expr_end, name_end);
1555 if (temp_string == NULL)
1556 {
1557 /*
1558 * Report an invalid expression in braces, unless
1559 * the expression evaluation has been cancelled due
1560 * to an aborting error, an interrupt, or an
1561 * exception.
1562 */
1563 if (!aborting())
1564 {
1565 emsg_severe = TRUE;
1566 EMSG2(_(e_invarg2), arg);
1567 break;
1568 }
1569 error = TRUE;
1570 arg = skipwhite(name_end);
1571 continue;
1572 }
1573 arg = temp_string;
1574 arg_len = STRLEN(temp_string);
1575 }
1576 else
1577 {
1578 c1 = *name_end;
1579 *name_end = NUL;
1580 arg_len = (int)(name_end - arg);
1581 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001582 if (arg_len == 2 && arg[1] == ':')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001584 switch (*arg)
1585 {
1586 case 'g': list_glob_vars(); break;
1587 case 'b': list_buf_vars(); break;
1588 case 'w': list_win_vars(); break;
1589 case 'v': list_vim_vars(); break;
1590 default:
1591 EMSG2(_("E738: Can't list variables for %s"), arg);
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 }
1594 else
1595 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001596 i = find_vim_var(arg, arg_len);
1597 if (i >= 0)
1598 list_vim_var(i);
1599 else if (STRCMP("b:changedtick", arg) == 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001601 char_u numbuf[NUMBUFLEN];
1602
1603 sprintf((char *)numbuf, "%ld",
1604 (long)curbuf->b_changedtick);
1605 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1606 VAR_NUMBER, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 }
1608 else
1609 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001610 varp = find_var(arg, NULL);
1611 if (varp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001613 /* Skip further arguments but do continue to
1614 * search for a trailing command. */
1615 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1616 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 }
1618 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001619 {
1620 name = vim_strchr(arg, ':');
1621 if (name != NULL)
1622 {
1623 /* "a:" vars have no name stored, use whole arg */
1624 if (arg[0] == 'a' && arg[1] == ':')
1625 c2 = NUL;
1626 else
1627 {
1628 c2 = *++name;
1629 *name = NUL;
1630 }
1631 list_one_var(varp, arg);
1632 if (c2 != NUL)
1633 *name = c2;
1634 }
1635 else
1636 list_one_var(varp, (char_u *)"");
1637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 }
1639 }
1640 if (expr_start != NULL)
1641 vim_free(temp_string);
1642 else
1643 *name_end = c1;
1644 }
1645 arg = skipwhite(name_end);
1646 }
1647
1648 return arg;
1649}
1650
1651/*
1652 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1653 * Returns a pointer to the char just after the var name.
1654 * Returns NULL if there is an error.
1655 */
1656 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 char_u *arg; /* points to variable name */
1659 typeval *tv; /* value to assign to variable */
1660 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663{
1664 int c1;
1665 char_u *name;
1666 char_u *p;
1667 char_u *arg_end = NULL;
1668 int len;
1669 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671
1672 /*
1673 * ":let $VAR = expr": Set environment variable.
1674 */
1675 if (*arg == '$')
1676 {
1677 /* Find the end of the name. */
1678 ++arg;
1679 name = arg;
1680 len = get_env_len(&arg);
1681 if (len == 0)
1682 EMSG2(_(e_invarg2), name - 1);
1683 else
1684 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 if (op != NULL && (*op == '+' || *op == '-'))
1686 EMSG2(_(e_letwrong), op);
1687 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 EMSG(_(e_letunexp));
1690 else
1691 {
1692 c1 = name[len];
1693 name[len] = NUL;
1694 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 if (op != NULL && *op == '.')
1696 {
1697 int mustfree = FALSE;
1698 char_u *s = vim_getenv(name, &mustfree);
1699
1700 if (s != NULL)
1701 {
1702 p = tofree = concat_str(s, p);
1703 if (mustfree)
1704 vim_free(s);
1705 }
1706 }
1707 if (p != NULL)
1708 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 if (STRICMP(name, "HOME") == 0)
1710 init_homedir();
1711 else if (didset_vim && STRICMP(name, "VIM") == 0)
1712 didset_vim = FALSE;
1713 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1714 didset_vimruntime = FALSE;
1715 name[len] = c1;
1716 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 }
1719 }
1720 }
1721
1722 /*
1723 * ":let &option = expr": Set option value.
1724 * ":let &l:option = expr": Set local option value.
1725 * ":let &g:option = expr": Set global option value.
1726 */
1727 else if (*arg == '&')
1728 {
1729 /* Find the end of the name. */
1730 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 if (p == NULL || (endchars != NULL
1732 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 EMSG(_(e_letunexp));
1734 else
1735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 long n;
1737 int opt_type;
1738 long numval;
1739 char_u *stringval = NULL;
1740 char_u *s;
1741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 c1 = *p;
1743 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001744
1745 n = get_tv_number(tv);
1746 s = get_tv_string(tv);
1747 if (op != NULL && *op != '=')
1748 {
1749 opt_type = get_option_value(arg, &numval,
1750 &stringval, opt_flags);
1751 if ((opt_type == 1 && *op == '.')
1752 || (opt_type == 0 && *op != '.'))
1753 EMSG2(_(e_letwrong), op);
1754 else
1755 {
1756 if (opt_type == 1) /* number */
1757 {
1758 if (*op == '+')
1759 n = numval + n;
1760 else
1761 n = numval - n;
1762 }
1763 else if (opt_type == 0 && stringval != NULL) /* string */
1764 {
1765 s = concat_str(stringval, s);
1766 vim_free(stringval);
1767 stringval = s;
1768 }
1769 }
1770 }
1771 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 *p = c1;
1773 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 }
1776 }
1777
1778 /*
1779 * ":let @r = expr": Set register contents.
1780 */
1781 else if (*arg == '@')
1782 {
1783 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001784 if (op != NULL && (*op == '+' || *op == '-'))
1785 EMSG2(_(e_letwrong), op);
1786 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 EMSG(_(e_letunexp));
1789 else
1790 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 char_u *tofree = NULL;
1792 char_u *s;
1793
1794 p = get_tv_string(tv);
1795 if (op != NULL && *op == '.')
1796 {
1797 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1798 if (s != NULL)
1799 {
1800 p = tofree = concat_str(s, p);
1801 vim_free(s);
1802 }
1803 }
1804 if (p != NULL)
1805 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 }
1809 }
1810
1811 /*
1812 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001813 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001815 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 lval lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1820 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001822 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1823 EMSG(_(e_letunexp));
1824 else
1825 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 arg_end = p;
1828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001830 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 }
1832
1833 else
1834 EMSG2(_(e_invarg2), arg);
1835
1836 return arg_end;
1837}
1838
1839/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001840 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1841 */
1842 static int
1843check_changedtick(arg)
1844 char_u *arg;
1845{
1846 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1847 {
1848 EMSG2(_(e_readonlyvar), arg);
1849 return TRUE;
1850 }
1851 return FALSE;
1852}
1853
1854/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 * Get an lval: variable, Dict item or List item that can be assigned a value
1856 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1857 * "name.key", "name.key[expr]" etc.
1858 * Indexing only works if "name" is an existing List or Dictionary.
1859 * "name" points to the start of the name.
1860 * If "rettv" is not NULL it points to the value to be assigned.
1861 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1862 * wrong; must end in space or cmd separator.
1863 *
1864 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001865 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001866 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 */
1868 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001869get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 typeval *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 lval *lp;
1873 int unlet;
1874 int skip;
1875 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 char_u *expr_start, *expr_end;
1879 int cc;
1880 VAR v;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001882 typeval var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001883 int empty1 = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001884 listitem *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001885 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001886 int len;
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 hashtable *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001889 /* Clear everything in "lp". */
1890 vim_memset(lp, 0, sizeof(lval));
1891
1892 if (skip)
1893 {
1894 /* When skipping just find the end of the name. */
1895 lp->ll_name = name;
1896 return find_name_end(name, NULL, NULL, TRUE);
1897 }
1898
1899 /* Find the end of the name. */
1900 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1901 if (expr_start != NULL)
1902 {
1903 /* Don't expand the name when we already know there is an error. */
1904 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1905 && *p != '[' && *p != '.')
1906 {
1907 EMSG(_(e_trailing));
1908 return NULL;
1909 }
1910
1911 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1912 if (lp->ll_exp_name == NULL)
1913 {
1914 /* Report an invalid expression in braces, unless the
1915 * expression evaluation has been cancelled due to an
1916 * aborting error, an interrupt, or an exception. */
1917 if (!aborting() && !quiet)
1918 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001919 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 EMSG2(_(e_invarg2), name);
1921 return NULL;
1922 }
1923 }
1924 lp->ll_name = lp->ll_exp_name;
1925 }
1926 else
1927 lp->ll_name = name;
1928
1929 /* Without [idx] or .key we are done. */
1930 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1931 return p;
1932
1933 cc = *p;
1934 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 if (v == NULL && !quiet)
1937 EMSG2(_(e_undefvar), lp->ll_name);
1938 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 if (v == NULL)
1940 return NULL;
1941
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 /*
1943 * Loop until no more [idx] or .key is following.
1944 */
1945 lp->ll_tv = &v->tv;
1946 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1949 && !(lp->ll_tv->v_type == VAR_DICT
1950 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 if (!quiet)
1953 EMSG(_("E689: Can only index a List or Dictionary"));
1954 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (!quiet)
1959 EMSG(_("E708: [:] must come last"));
1960 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001962
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 len = -1;
1964 if (*p == '.')
1965 {
1966 key = p + 1;
1967 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1968 ;
1969 if (len == 0)
1970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 if (!quiet)
1972 EMSG(_(e_emptykey));
1973 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 }
1975 p = key + len;
1976 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001977 else
1978 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001979 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001980 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 if (*p == ':')
1982 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 else
1984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001985 empty1 = FALSE;
1986 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 }
1989
1990 /* Optionally get the second index [ :expr]. */
1991 if (*p == ':')
1992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001997 if (!empty1)
1998 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002000 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 if (rettv != NULL && (rettv->v_type != VAR_LIST
2002 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 if (!quiet)
2005 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 if (!empty1)
2007 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 }
2010 p = skipwhite(p + 1);
2011 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 else
2014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 if (!empty1)
2019 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 }
2022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002023 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002027
Bram Moolenaar8c711452005-01-14 21:53:12 +00002028 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (!quiet)
2031 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002032 if (!empty1)
2033 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002035 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002037 }
2038
2039 /* Skip to past ']'. */
2040 ++p;
2041 }
2042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 {
2045 if (len == -1)
2046 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002048 key = get_tv_string(&var1);
2049 if (*key == NUL)
2050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 if (!quiet)
2052 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 }
2056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002057 lp->ll_list = NULL;
2058 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002059 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002062 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 if (len == -1)
2068 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 }
2071 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002075 if (len == -1)
2076 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002078 p = NULL;
2079 break;
2080 }
2081 if (len == -1)
2082 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 }
2085 else
2086 {
2087 /*
2088 * Get the number and item for the only or first index of the List.
2089 */
2090 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 else
2093 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 clear_tv(&var1);
2096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 lp->ll_list = lp->ll_tv->vval.v_list;
2099 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2100 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 if (!quiet)
2103 EMSGN(_(e_listidx), lp->ll_n1);
2104 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 }
2108
2109 /*
2110 * May need to find the item or absolute index for the second
2111 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 * When no index given: "lp->ll_empty2" is TRUE.
2113 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002118 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 if (ni == NULL)
2123 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (!quiet)
2125 EMSGN(_(e_listidx), lp->ll_n2);
2126 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002127 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 }
2130
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2132 if (lp->ll_n1 < 0)
2133 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2134 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (!quiet)
2137 EMSGN(_(e_listidx), lp->ll_n2);
2138 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002139 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002140 }
2141
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 }
2145
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 return p;
2147}
2148
2149/*
2150 * Clear an "lval" that was filled by get_lval().
2151 */
2152 static void
2153clear_lval(lp)
2154 lval *lp;
2155{
2156 vim_free(lp->ll_exp_name);
2157 vim_free(lp->ll_newkey);
2158}
2159
2160/*
2161 * Set a variable that was parsed by get_lval().
2162 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 */
2165 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 lval *lp;
2168 char_u *endp;
2169 typeval *rettv;
2170 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172{
2173 int cc;
2174 listitem *ni;
2175 listitem *ri;
2176 dictitem *di;
2177
2178 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 cc = *endp;
2183 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002184 if (op != NULL && *op != '=')
2185 {
2186 typeval tv;
2187
2188 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK)
2189 {
2190 if (tv_op(&tv, rettv, op) == OK)
2191 set_var(lp->ll_name, &tv, FALSE);
2192 clear_tv(&tv);
2193 }
2194 }
2195 else
2196 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 }
2200 else if (lp->ll_range)
2201 {
2202 /*
2203 * Assign the List values to the list items.
2204 */
2205 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 if (op != NULL && *op != '=')
2208 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2209 else
2210 {
2211 clear_tv(&lp->ll_li->li_tv);
2212 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2213 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 ri = ri->li_next;
2215 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2216 break;
2217 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 /* Need to add an empty item. */
2220 ni = listitem_alloc();
2221 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002222 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 ri = NULL;
2224 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002225 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 ni->li_tv.v_type = VAR_NUMBER;
2227 ni->li_tv.vval.v_number = 0;
2228 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002229 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 lp->ll_li = lp->ll_li->li_next;
2231 ++lp->ll_n1;
2232 }
2233 if (ri != NULL)
2234 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002235 else if (lp->ll_empty2
2236 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 : lp->ll_n1 != lp->ll_n2)
2238 EMSG(_("E711: List value has not enough items"));
2239 }
2240 else
2241 {
2242 /*
2243 * Assign to a List or Dictionary item.
2244 */
2245 if (lp->ll_newkey != NULL)
2246 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 if (op != NULL && *op != '=')
2248 {
2249 EMSG2(_(e_letwrong), op);
2250 return;
2251 }
2252
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002254 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (di == NULL)
2256 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002257 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2258 {
2259 vim_free(di);
2260 return;
2261 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002263 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 else if (op != NULL && *op != '=')
2265 {
2266 tv_op(lp->ll_tv, rettv, op);
2267 return;
2268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 /*
2273 * Assign the value to the variable or list item.
2274 */
2275 if (copy)
2276 copy_tv(rettv, lp->ll_tv);
2277 else
2278 {
2279 *lp->ll_tv = *rettv;
2280 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
2282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283}
2284
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002285/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002286 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2287 * Returns OK or FAIL.
2288 */
2289 static int
2290tv_op(tv1, tv2, op)
2291 typeval *tv1;
2292 typeval *tv2;
2293 char_u *op;
2294{
2295 long n;
2296 char_u numbuf[NUMBUFLEN];
2297 char_u *s;
2298
2299 /* Can't do anything with a Funcref or a Dict on the right. */
2300 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2301 {
2302 switch (tv1->v_type)
2303 {
2304 case VAR_DICT:
2305 case VAR_FUNC:
2306 break;
2307
2308 case VAR_LIST:
2309 if (*op != '+' || tv2->v_type != VAR_LIST)
2310 break;
2311 /* List += List */
2312 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2313 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2314 return OK;
2315
2316 case VAR_NUMBER:
2317 case VAR_STRING:
2318 if (tv2->v_type == VAR_LIST)
2319 break;
2320 if (*op == '+' || *op == '-')
2321 {
2322 /* nr += nr or nr -= nr*/
2323 n = get_tv_number(tv1);
2324 if (*op == '+')
2325 n += get_tv_number(tv2);
2326 else
2327 n -= get_tv_number(tv2);
2328 clear_tv(tv1);
2329 tv1->v_type = VAR_NUMBER;
2330 tv1->vval.v_number = n;
2331 }
2332 else
2333 {
2334 /* str .= str */
2335 s = get_tv_string(tv1);
2336 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2337 clear_tv(tv1);
2338 tv1->v_type = VAR_STRING;
2339 tv1->vval.v_string = s;
2340 }
2341 return OK;
2342 }
2343 }
2344
2345 EMSG2(_(e_letwrong), op);
2346 return FAIL;
2347}
2348
2349/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002350 * Add a watcher to a list.
2351 */
2352 static void
2353list_add_watch(l, lw)
2354 listvar *l;
2355 listwatch *lw;
2356{
2357 lw->lw_next = l->lv_watch;
2358 l->lv_watch = lw;
2359}
2360
2361/*
2362 * Remove a watches from a list.
2363 * No warning when it isn't found...
2364 */
2365 static void
2366list_rem_watch(l, lwrem)
2367 listvar *l;
2368 listwatch *lwrem;
2369{
2370 listwatch *lw, **lwp;
2371
2372 lwp = &l->lv_watch;
2373 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2374 {
2375 if (lw == lwrem)
2376 {
2377 *lwp = lw->lw_next;
2378 break;
2379 }
2380 lwp = &lw->lw_next;
2381 }
2382}
2383
2384/*
2385 * Just before removing an item from a list: advance watchers to the next
2386 * item.
2387 */
2388 static void
2389list_fix_watch(l, item)
2390 listvar *l;
2391 listitem *item;
2392{
2393 listwatch *lw;
2394
2395 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2396 if (lw->lw_item == item)
2397 lw->lw_item = item->li_next;
2398}
2399
2400/*
2401 * Evaluate the expression used in a ":for var in expr" command.
2402 * "arg" points to "var".
2403 * Set "*errp" to TRUE for an error, FALSE otherwise;
2404 * Return a pointer that holds the info. Null when there is an error.
2405 */
2406 void *
2407eval_for_line(arg, errp, nextcmdp, skip)
2408 char_u *arg;
2409 int *errp;
2410 char_u **nextcmdp;
2411 int skip;
2412{
2413 forinfo *fi;
2414 char_u *expr;
2415 typeval tv;
2416 listvar *l;
2417
2418 *errp = TRUE; /* default: there is an error */
2419
2420 fi = (forinfo *)alloc_clear(sizeof(forinfo));
2421 if (fi == NULL)
2422 return NULL;
2423
2424 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2425 if (expr == NULL)
2426 return fi;
2427
2428 expr = skipwhite(expr);
2429 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2430 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002431 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 return fi;
2433 }
2434
2435 if (skip)
2436 ++emsg_skip;
2437 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2438 {
2439 *errp = FALSE;
2440 if (!skip)
2441 {
2442 l = tv.vval.v_list;
2443 if (tv.v_type != VAR_LIST || l == NULL)
2444 EMSG(_(e_listreq));
2445 else
2446 {
2447 fi->fi_list = l;
2448 list_add_watch(l, &fi->fi_lw);
2449 fi->fi_lw.lw_item = l->lv_first;
2450 }
2451 }
2452 }
2453 if (skip)
2454 --emsg_skip;
2455
2456 return fi;
2457}
2458
2459/*
2460 * Use the first item in a ":for" list. Advance to the next.
2461 * Assign the values to the variable (list). "arg" points to the first one.
2462 * Return TRUE when a valid item was found, FALSE when at end of list or
2463 * something wrong.
2464 */
2465 int
2466next_for_item(fi_void, arg)
2467 void *fi_void;
2468 char_u *arg;
2469{
2470 forinfo *fi = (forinfo *)fi_void;
2471 int result;
2472 listitem *item;
2473
2474 item = fi->fi_lw.lw_item;
2475 if (item == NULL)
2476 result = FALSE;
2477 else
2478 {
2479 fi->fi_lw.lw_item = item->li_next;
2480 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2481 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2482 }
2483 return result;
2484}
2485
2486/*
2487 * Free the structure used to store info used by ":for".
2488 */
2489 void
2490free_for_info(fi_void)
2491 void *fi_void;
2492{
2493 forinfo *fi = (forinfo *)fi_void;
2494
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002495 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002496 list_rem_watch(fi->fi_list, &fi->fi_lw);
2497 vim_free(fi);
2498}
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2501
2502 void
2503set_context_for_expression(xp, arg, cmdidx)
2504 expand_T *xp;
2505 char_u *arg;
2506 cmdidx_T cmdidx;
2507{
2508 int got_eq = FALSE;
2509 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002512 if (cmdidx == CMD_let)
2513 {
2514 xp->xp_context = EXPAND_USER_VARS;
2515 if (vim_strchr(arg, '=') == NULL)
2516 {
2517 /* ":let var1 var2 ...": find last space. */
2518 for (p = arg + STRLEN(arg); p > arg; )
2519 {
2520 xp->xp_pattern = p;
2521 p = mb_ptr_back(arg, p);
2522 if (vim_iswhite(*p))
2523 break;
2524 }
2525 return;
2526 }
2527 }
2528 else
2529 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2530 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 while ((xp->xp_pattern = vim_strpbrk(arg,
2532 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2533 {
2534 c = *xp->xp_pattern;
2535 if (c == '&')
2536 {
2537 c = xp->xp_pattern[1];
2538 if (c == '&')
2539 {
2540 ++xp->xp_pattern;
2541 xp->xp_context = cmdidx != CMD_let || got_eq
2542 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2543 }
2544 else if (c != ' ')
2545 xp->xp_context = EXPAND_SETTINGS;
2546 }
2547 else if (c == '$')
2548 {
2549 /* environment variable */
2550 xp->xp_context = EXPAND_ENV_VARS;
2551 }
2552 else if (c == '=')
2553 {
2554 got_eq = TRUE;
2555 xp->xp_context = EXPAND_EXPRESSION;
2556 }
2557 else if (c == '<'
2558 && xp->xp_context == EXPAND_FUNCTIONS
2559 && vim_strchr(xp->xp_pattern, '(') == NULL)
2560 {
2561 /* Function name can start with "<SNR>" */
2562 break;
2563 }
2564 else if (cmdidx != CMD_let || got_eq)
2565 {
2566 if (c == '"') /* string */
2567 {
2568 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2569 if (c == '\\' && xp->xp_pattern[1] != NUL)
2570 ++xp->xp_pattern;
2571 xp->xp_context = EXPAND_NOTHING;
2572 }
2573 else if (c == '\'') /* literal string */
2574 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2577 /* skip */ ;
2578 xp->xp_context = EXPAND_NOTHING;
2579 }
2580 else if (c == '|')
2581 {
2582 if (xp->xp_pattern[1] == '|')
2583 {
2584 ++xp->xp_pattern;
2585 xp->xp_context = EXPAND_EXPRESSION;
2586 }
2587 else
2588 xp->xp_context = EXPAND_COMMANDS;
2589 }
2590 else
2591 xp->xp_context = EXPAND_EXPRESSION;
2592 }
2593 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594 /* Doesn't look like something valid, expand as an expression
2595 * anyway. */
2596 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 arg = xp->xp_pattern;
2598 if (*arg != NUL)
2599 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2600 /* skip */ ;
2601 }
2602 xp->xp_pattern = arg;
2603}
2604
2605#endif /* FEAT_CMDL_COMPL */
2606
2607/*
2608 * ":1,25call func(arg1, arg2)" function call.
2609 */
2610 void
2611ex_call(eap)
2612 exarg_T *eap;
2613{
2614 char_u *arg = eap->arg;
2615 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002617 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 int len;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002619 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 linenr_T lnum;
2621 int doesrange;
2622 int failed = FALSE;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002623 funcdict fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002625 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2626 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002627 if (tofree == NULL)
2628 return;
2629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002630 /* Increase refcount on dictionary, it could get deleted when evaluating
2631 * the arguments. */
2632 if (fudi.fd_dict != NULL)
2633 ++fudi.fd_dict->dv_refcount;
2634
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2636 len = STRLEN(tofree);
2637 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 if (*startarg != '(')
2643 {
2644 EMSG2(_("E107: Missing braces: %s"), name);
2645 goto end;
2646 }
2647
2648 /*
2649 * When skipping, evaluate the function once, to find the end of the
2650 * arguments.
2651 * When the function takes a range, this is discovered after the first
2652 * call, and the loop is broken.
2653 */
2654 if (eap->skip)
2655 {
2656 ++emsg_skip;
2657 lnum = eap->line2; /* do it once, also with an invalid range */
2658 }
2659 else
2660 lnum = eap->line1;
2661 for ( ; lnum <= eap->line2; ++lnum)
2662 {
2663 if (!eap->skip && eap->addr_count > 0)
2664 {
2665 curwin->w_cursor.lnum = lnum;
2666 curwin->w_cursor.col = 0;
2667 }
2668 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002670 eap->line1, eap->line2, &doesrange,
2671 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 failed = TRUE;
2674 break;
2675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (doesrange || eap->skip)
2678 break;
2679 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002680 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002682 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (aborting())
2684 break;
2685 }
2686 if (eap->skip)
2687 --emsg_skip;
2688
2689 if (!failed)
2690 {
2691 /* Check for trailing illegal characters and a following command. */
2692 if (!ends_excmd(*arg))
2693 {
2694 emsg_severe = TRUE;
2695 EMSG(_(e_trailing));
2696 }
2697 else
2698 eap->nextcmd = check_nextcmd(arg);
2699 }
2700
2701end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002702 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704}
2705
2706/*
2707 * ":unlet[!] var1 ... " command.
2708 */
2709 void
2710ex_unlet(eap)
2711 exarg_T *eap;
2712{
2713 char_u *arg = eap->arg;
2714 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int error = FALSE;
2716
2717 do
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lval lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 /* Parse the name and find the end. */
2722 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2723 if (lv.ll_name == NULL)
2724 error = TRUE; /* error but continue parsing */
2725 if (name_end == NULL || (!vim_iswhite(*name_end)
2726 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (name_end != NULL)
2729 {
2730 emsg_severe = TRUE;
2731 EMSG(_(e_trailing));
2732 }
2733 if (!(eap->skip || error))
2734 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 break;
2736 }
2737
2738 if (!error && !eap->skip)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2740 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!eap->skip)
2743 clear_lval(&lv);
2744
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 arg = skipwhite(name_end);
2746 } while (!ends_excmd(*arg));
2747
2748 eap->nextcmd = check_nextcmd(arg);
2749}
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753do_unlet_var(lp, name_end, forceit)
2754 lval *lp;
2755 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 int forceit;
2757{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 int ret = OK;
2759 int cc;
2760
2761 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 cc = *name_end;
2764 *name_end = NUL;
2765
2766 /* Normal name or expanded name. */
2767 if (check_changedtick(lp->ll_name))
2768 ret = FAIL;
2769 else if (do_unlet(lp->ll_name) == FAIL && !forceit)
2770 {
2771 EMSG2(_("E108: No such variable: \"%s\""), lp->ll_name);
2772 ret = FAIL;
2773 }
2774 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 else if (lp->ll_range)
2777 {
2778 listitem *li;
2779
2780 /* Delete a range of List items. */
2781 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2782 {
2783 li = lp->ll_li->li_next;
2784 listitem_remove(lp->ll_list, lp->ll_li);
2785 lp->ll_li = li;
2786 ++lp->ll_n1;
2787 }
2788 }
2789 else
2790 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* unlet a List item. */
2793 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002796 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 }
2798
2799 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800}
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802/*
2803 * "unlet" a variable. Return OK if it existed, FAIL if not.
2804 */
2805 int
2806do_unlet(name)
2807 char_u *name;
2808{
Bram Moolenaara7043832005-01-21 11:56:39 +00002809 hashtable *ht;
2810 hashitem *hi;
2811 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
Bram Moolenaara7043832005-01-21 11:56:39 +00002813 if (name[0] == 'a' && name[1] == ':')
2814 EMSG2(_(e_readonlyvar), name);
2815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002817 ht = find_var_ht(name, &varname);
2818 if (ht != NULL)
2819 {
2820 hi = hash_find(ht, varname);
2821 if (!HASHITEM_EMPTY(hi))
2822 {
2823 delete_var(ht, hi);
2824 return OK;
2825 }
2826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828 return FAIL;
2829}
2830
2831#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2832/*
2833 * Delete all "menutrans_" variables.
2834 */
2835 void
2836del_menutrans_vars()
2837{
Bram Moolenaara7043832005-01-21 11:56:39 +00002838 hashitem *hi;
2839 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
Bram Moolenaara7043832005-01-21 11:56:39 +00002841 hash_lock(&variables);
2842 todo = variables.ht_used;
2843 for (hi = variables.ht_array; todo > 0 && !got_int; ++hi)
2844 {
2845 if (!HASHITEM_EMPTY(hi))
2846 {
2847 --todo;
2848 if (STRNCMP(HI2VAR(hi)->v_name, "menutrans_", 10) == 0)
2849 delete_var(&variables, hi);
2850 }
2851 }
2852 hash_unlock(&variables);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853}
2854#endif
2855
2856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2857
2858/*
2859 * Local string buffer for the next two functions to store a variable name
2860 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2861 * get_user_var_name().
2862 */
2863
2864static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2865
2866static char_u *varnamebuf = NULL;
2867static int varnamebuflen = 0;
2868
2869/*
2870 * Function to concatenate a prefix and a variable name.
2871 */
2872 static char_u *
2873cat_prefix_varname(prefix, name)
2874 int prefix;
2875 char_u *name;
2876{
2877 int len;
2878
2879 len = (int)STRLEN(name) + 3;
2880 if (len > varnamebuflen)
2881 {
2882 vim_free(varnamebuf);
2883 len += 10; /* some additional space */
2884 varnamebuf = alloc(len);
2885 if (varnamebuf == NULL)
2886 {
2887 varnamebuflen = 0;
2888 return NULL;
2889 }
2890 varnamebuflen = len;
2891 }
2892 *varnamebuf = prefix;
2893 varnamebuf[1] = ':';
2894 STRCPY(varnamebuf + 2, name);
2895 return varnamebuf;
2896}
2897
2898/*
2899 * Function given to ExpandGeneric() to obtain the list of user defined
2900 * (global/buffer/window/built-in) variable names.
2901 */
2902/*ARGSUSED*/
2903 char_u *
2904get_user_var_name(xp, idx)
2905 expand_T *xp;
2906 int idx;
2907{
Bram Moolenaara7043832005-01-21 11:56:39 +00002908 static int gdone;
2909 static int bdone;
2910 static int wdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 static int vidx;
Bram Moolenaara7043832005-01-21 11:56:39 +00002912 static hashitem *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00002915 gdone = bdone = wdone = vidx = 0;
2916 if (gdone < variables.ht_used) /* Global variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002918 if (gdone++ == 0)
2919 hi = variables.ht_array;
2920 while (HASHITEM_EMPTY(hi))
2921 ++hi;
2922 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2923 return cat_prefix_varname('g', hi->hi_key);
2924 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002926 if (bdone < curbuf->b_vars.ht_used) /* Current buffer variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002928 if (bdone++ == 0)
2929 hi = curbuf->b_vars.ht_array;
2930 while (HASHITEM_EMPTY(hi))
2931 ++hi;
2932 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002934 if (bdone == curbuf->b_vars.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002936 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return (char_u *)"b:changedtick";
2938 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002939 if (wdone < curwin->w_vars.ht_used) /* Current window variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002941 if (bdone++ == 0)
2942 hi = curwin->w_vars.ht_array;
2943 while (HASHITEM_EMPTY(hi))
2944 ++hi;
2945 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 if (vidx < VV_LEN) /* Built-in variables */
2948 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2949
2950 vim_free(varnamebuf);
2951 varnamebuf = NULL;
2952 varnamebuflen = 0;
2953 return NULL;
2954}
2955
2956#endif /* FEAT_CMDL_COMPL */
2957
2958/*
2959 * types for expressions.
2960 */
2961typedef enum
2962{
2963 TYPE_UNKNOWN = 0
2964 , TYPE_EQUAL /* == */
2965 , TYPE_NEQUAL /* != */
2966 , TYPE_GREATER /* > */
2967 , TYPE_GEQUAL /* >= */
2968 , TYPE_SMALLER /* < */
2969 , TYPE_SEQUAL /* <= */
2970 , TYPE_MATCH /* =~ */
2971 , TYPE_NOMATCH /* !~ */
2972} exptype_T;
2973
2974/*
2975 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2978 */
2979
2980/*
2981 * Handle zero level expression.
2982 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 * Return OK or FAIL.
2985 */
2986 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002987eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002989 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 char_u **nextcmd;
2991 int evaluate;
2992{
2993 int ret;
2994 char_u *p;
2995
2996 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 if (ret == FAIL || !ends_excmd(*p))
2999 {
3000 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 /*
3003 * Report the invalid expression unless the expression evaluation has
3004 * been cancelled due to an aborting error, an interrupt, or an
3005 * exception.
3006 */
3007 if (!aborting())
3008 EMSG2(_(e_invexpr2), arg);
3009 ret = FAIL;
3010 }
3011 if (nextcmd != NULL)
3012 *nextcmd = check_nextcmd(p);
3013
3014 return ret;
3015}
3016
3017/*
3018 * Handle top level expression:
3019 * expr1 ? expr0 : expr0
3020 *
3021 * "arg" must point to the first non-white of the expression.
3022 * "arg" is advanced to the next non-white after the recognized expression.
3023 *
3024 * Return OK or FAIL.
3025 */
3026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003027eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003029 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 int evaluate;
3031{
3032 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003033 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035 /*
3036 * Get the first variable.
3037 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003038 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 return FAIL;
3040
3041 if ((*arg)[0] == '?')
3042 {
3043 result = FALSE;
3044 if (evaluate)
3045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050
3051 /*
3052 * Get the second variable.
3053 */
3054 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 return FAIL;
3057
3058 /*
3059 * Check for the ":".
3060 */
3061 if ((*arg)[0] != ':')
3062 {
3063 EMSG(_("E109: Missing ':' after '?'"));
3064 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 return FAIL;
3067 }
3068
3069 /*
3070 * Get the third variable.
3071 */
3072 *arg = skipwhite(*arg + 1);
3073 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3074 {
3075 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 return FAIL;
3078 }
3079 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
3082
3083 return OK;
3084}
3085
3086/*
3087 * Handle first level expression:
3088 * expr2 || expr2 || expr2 logical OR
3089 *
3090 * "arg" must point to the first non-white of the expression.
3091 * "arg" is advanced to the next non-white after the recognized expression.
3092 *
3093 * Return OK or FAIL.
3094 */
3095 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003096eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 int evaluate;
3100{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003101 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 long result;
3103 int first;
3104
3105 /*
3106 * Get the first variable.
3107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003108 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return FAIL;
3110
3111 /*
3112 * Repeat until there is no following "||".
3113 */
3114 first = TRUE;
3115 result = FALSE;
3116 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3117 {
3118 if (evaluate && first)
3119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003120 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 first = FALSE;
3124 }
3125
3126 /*
3127 * Get the second variable.
3128 */
3129 *arg = skipwhite(*arg + 2);
3130 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3131 return FAIL;
3132
3133 /*
3134 * Compute the result.
3135 */
3136 if (evaluate && !result)
3137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003138 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003140 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
3142 if (evaluate)
3143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003144 rettv->v_type = VAR_NUMBER;
3145 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147 }
3148
3149 return OK;
3150}
3151
3152/*
3153 * Handle second level expression:
3154 * expr3 && expr3 && expr3 logical AND
3155 *
3156 * "arg" must point to the first non-white of the expression.
3157 * "arg" is advanced to the next non-white after the recognized expression.
3158 *
3159 * Return OK or FAIL.
3160 */
3161 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003162eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003164 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int evaluate;
3166{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003167 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 long result;
3169 int first;
3170
3171 /*
3172 * Get the first variable.
3173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003174 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 return FAIL;
3176
3177 /*
3178 * Repeat until there is no following "&&".
3179 */
3180 first = TRUE;
3181 result = TRUE;
3182 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3183 {
3184 if (evaluate && first)
3185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003188 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 first = FALSE;
3190 }
3191
3192 /*
3193 * Get the second variable.
3194 */
3195 *arg = skipwhite(*arg + 2);
3196 if (eval4(arg, &var2, evaluate && result) == FAIL)
3197 return FAIL;
3198
3199 /*
3200 * Compute the result.
3201 */
3202 if (evaluate && result)
3203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003204 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003206 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 if (evaluate)
3209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 rettv->v_type = VAR_NUMBER;
3211 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
3213 }
3214
3215 return OK;
3216}
3217
3218/*
3219 * Handle third level expression:
3220 * var1 == var2
3221 * var1 =~ var2
3222 * var1 != var2
3223 * var1 !~ var2
3224 * var1 > var2
3225 * var1 >= var2
3226 * var1 < var2
3227 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003228 * var1 is var2
3229 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 *
3231 * "arg" must point to the first non-white of the expression.
3232 * "arg" is advanced to the next non-white after the recognized expression.
3233 *
3234 * Return OK or FAIL.
3235 */
3236 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 int evaluate;
3241{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003242 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 char_u *p;
3244 int i;
3245 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003246 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 int len = 2;
3248 long n1, n2;
3249 char_u *s1, *s2;
3250 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3251 regmatch_T regmatch;
3252 int ic;
3253 char_u *save_cpo;
3254
3255 /*
3256 * Get the first variable.
3257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003258 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 return FAIL;
3260
3261 p = *arg;
3262 switch (p[0])
3263 {
3264 case '=': if (p[1] == '=')
3265 type = TYPE_EQUAL;
3266 else if (p[1] == '~')
3267 type = TYPE_MATCH;
3268 break;
3269 case '!': if (p[1] == '=')
3270 type = TYPE_NEQUAL;
3271 else if (p[1] == '~')
3272 type = TYPE_NOMATCH;
3273 break;
3274 case '>': if (p[1] != '=')
3275 {
3276 type = TYPE_GREATER;
3277 len = 1;
3278 }
3279 else
3280 type = TYPE_GEQUAL;
3281 break;
3282 case '<': if (p[1] != '=')
3283 {
3284 type = TYPE_SMALLER;
3285 len = 1;
3286 }
3287 else
3288 type = TYPE_SEQUAL;
3289 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003290 case 'i': if (p[1] == 's')
3291 {
3292 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3293 len = 5;
3294 if (!vim_isIDc(p[len]))
3295 {
3296 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3297 type_is = TRUE;
3298 }
3299 }
3300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302
3303 /*
3304 * If there is a comparitive operator, use it.
3305 */
3306 if (type != TYPE_UNKNOWN)
3307 {
3308 /* extra question mark appended: ignore case */
3309 if (p[len] == '?')
3310 {
3311 ic = TRUE;
3312 ++len;
3313 }
3314 /* extra '#' appended: match case */
3315 else if (p[len] == '#')
3316 {
3317 ic = FALSE;
3318 ++len;
3319 }
3320 /* nothing appened: use 'ignorecase' */
3321 else
3322 ic = p_ic;
3323
3324 /*
3325 * Get the second variable.
3326 */
3327 *arg = skipwhite(p + len);
3328 if (eval5(arg, &var2, evaluate) == FAIL)
3329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003330 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 return FAIL;
3332 }
3333
3334 if (evaluate)
3335 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003336 if (type_is && rettv->v_type != var2.v_type)
3337 {
3338 /* For "is" a different type always means FALSE, for "notis"
3339 * it means TRUE. */
3340 n1 = (type == TYPE_NEQUAL);
3341 }
3342 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3343 {
3344 if (type_is)
3345 {
3346 n1 = (rettv->v_type == var2.v_type
3347 && rettv->vval.v_list == var2.vval.v_list);
3348 if (type == TYPE_NEQUAL)
3349 n1 = !n1;
3350 }
3351 else if (rettv->v_type != var2.v_type
3352 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3353 {
3354 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003355 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003356 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003357 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003358 clear_tv(rettv);
3359 clear_tv(&var2);
3360 return FAIL;
3361 }
3362 else
3363 {
3364 /* Compare two Lists for being equal or unequal. */
3365 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3366 if (type == TYPE_NEQUAL)
3367 n1 = !n1;
3368 }
3369 }
3370
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003371 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3372 {
3373 if (type_is)
3374 {
3375 n1 = (rettv->v_type == var2.v_type
3376 && rettv->vval.v_dict == var2.vval.v_dict);
3377 if (type == TYPE_NEQUAL)
3378 n1 = !n1;
3379 }
3380 else if (rettv->v_type != var2.v_type
3381 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3382 {
3383 if (rettv->v_type != var2.v_type)
3384 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3385 else
3386 EMSG(_("E736: Invalid operation for Dictionary"));
3387 clear_tv(rettv);
3388 clear_tv(&var2);
3389 return FAIL;
3390 }
3391 else
3392 {
3393 /* Compare two Dictionaries for being equal or unequal. */
3394 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3395 if (type == TYPE_NEQUAL)
3396 n1 = !n1;
3397 }
3398 }
3399
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003400 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3401 {
3402 if (rettv->v_type != var2.v_type
3403 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3404 {
3405 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003406 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003407 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003408 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003409 clear_tv(rettv);
3410 clear_tv(&var2);
3411 return FAIL;
3412 }
3413 else
3414 {
3415 /* Compare two Funcrefs for being equal or unequal. */
3416 if (rettv->vval.v_string == NULL
3417 || var2.vval.v_string == NULL)
3418 n1 = FALSE;
3419 else
3420 n1 = STRCMP(rettv->vval.v_string,
3421 var2.vval.v_string) == 0;
3422 if (type == TYPE_NEQUAL)
3423 n1 = !n1;
3424 }
3425 }
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 /*
3428 * If one of the two variables is a number, compare as a number.
3429 * When using "=~" or "!~", always compare as string.
3430 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003431 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003434 n1 = get_tv_number(rettv);
3435 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 switch (type)
3437 {
3438 case TYPE_EQUAL: n1 = (n1 == n2); break;
3439 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3440 case TYPE_GREATER: n1 = (n1 > n2); break;
3441 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3442 case TYPE_SMALLER: n1 = (n1 < n2); break;
3443 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3444 case TYPE_UNKNOWN:
3445 case TYPE_MATCH:
3446 case TYPE_NOMATCH: break; /* avoid gcc warning */
3447 }
3448 }
3449 else
3450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003451 s1 = get_tv_string_buf(rettv, buf1);
3452 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3454 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3455 else
3456 i = 0;
3457 n1 = FALSE;
3458 switch (type)
3459 {
3460 case TYPE_EQUAL: n1 = (i == 0); break;
3461 case TYPE_NEQUAL: n1 = (i != 0); break;
3462 case TYPE_GREATER: n1 = (i > 0); break;
3463 case TYPE_GEQUAL: n1 = (i >= 0); break;
3464 case TYPE_SMALLER: n1 = (i < 0); break;
3465 case TYPE_SEQUAL: n1 = (i <= 0); break;
3466
3467 case TYPE_MATCH:
3468 case TYPE_NOMATCH:
3469 /* avoid 'l' flag in 'cpoptions' */
3470 save_cpo = p_cpo;
3471 p_cpo = (char_u *)"";
3472 regmatch.regprog = vim_regcomp(s2,
3473 RE_MAGIC + RE_STRING);
3474 regmatch.rm_ic = ic;
3475 if (regmatch.regprog != NULL)
3476 {
3477 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3478 vim_free(regmatch.regprog);
3479 if (type == TYPE_NOMATCH)
3480 n1 = !n1;
3481 }
3482 p_cpo = save_cpo;
3483 break;
3484
3485 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3486 }
3487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 clear_tv(rettv);
3489 clear_tv(&var2);
3490 rettv->v_type = VAR_NUMBER;
3491 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493 }
3494
3495 return OK;
3496}
3497
3498/*
3499 * Handle fourth level expression:
3500 * + number addition
3501 * - number subtraction
3502 * . string concatenation
3503 *
3504 * "arg" must point to the first non-white of the expression.
3505 * "arg" is advanced to the next non-white after the recognized expression.
3506 *
3507 * Return OK or FAIL.
3508 */
3509 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003510eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003512 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 int evaluate;
3514{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003515 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003516 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 int op;
3518 long n1, n2;
3519 char_u *s1, *s2;
3520 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3521 char_u *p;
3522
3523 /*
3524 * Get the first variable.
3525 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 return FAIL;
3528
3529 /*
3530 * Repeat computing, until no '+', '-' or '.' is following.
3531 */
3532 for (;;)
3533 {
3534 op = **arg;
3535 if (op != '+' && op != '-' && op != '.')
3536 break;
3537
3538 /*
3539 * Get the second variable.
3540 */
3541 *arg = skipwhite(*arg + 1);
3542 if (eval6(arg, &var2, evaluate) == FAIL)
3543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return FAIL;
3546 }
3547
3548 if (evaluate)
3549 {
3550 /*
3551 * Compute the result.
3552 */
3553 if (op == '.')
3554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 s1 = get_tv_string_buf(rettv, buf1);
3556 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003557 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 clear_tv(rettv);
3559 rettv->v_type = VAR_STRING;
3560 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003562 else if (op == '+' && rettv->v_type == VAR_LIST
3563 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003564 {
3565 /* concatenate Lists */
3566 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3567 &var3) == FAIL)
3568 {
3569 clear_tv(rettv);
3570 clear_tv(&var2);
3571 return FAIL;
3572 }
3573 clear_tv(rettv);
3574 *rettv = var3;
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 else
3577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003578 n1 = get_tv_number(rettv);
3579 n2 = get_tv_number(&var2);
3580 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (op == '+')
3582 n1 = n1 + n2;
3583 else
3584 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003585 rettv->v_type = VAR_NUMBER;
3586 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590 }
3591 return OK;
3592}
3593
3594/*
3595 * Handle fifth level expression:
3596 * * number multiplication
3597 * / number division
3598 * % number modulo
3599 *
3600 * "arg" must point to the first non-white of the expression.
3601 * "arg" is advanced to the next non-white after the recognized expression.
3602 *
3603 * Return OK or FAIL.
3604 */
3605 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 int evaluate;
3610{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003611 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int op;
3613 long n1, n2;
3614
3615 /*
3616 * Get the first variable.
3617 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 return FAIL;
3620
3621 /*
3622 * Repeat computing, until no '*', '/' or '%' is following.
3623 */
3624 for (;;)
3625 {
3626 op = **arg;
3627 if (op != '*' && op != '/' && op != '%')
3628 break;
3629
3630 if (evaluate)
3631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003632 n1 = get_tv_number(rettv);
3633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635 else
3636 n1 = 0;
3637
3638 /*
3639 * Get the second variable.
3640 */
3641 *arg = skipwhite(*arg + 1);
3642 if (eval7(arg, &var2, evaluate) == FAIL)
3643 return FAIL;
3644
3645 if (evaluate)
3646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003647 n2 = get_tv_number(&var2);
3648 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 /*
3651 * Compute the result.
3652 */
3653 if (op == '*')
3654 n1 = n1 * n2;
3655 else if (op == '/')
3656 {
3657 if (n2 == 0) /* give an error message? */
3658 n1 = 0x7fffffffL;
3659 else
3660 n1 = n1 / n2;
3661 }
3662 else
3663 {
3664 if (n2 == 0) /* give an error message? */
3665 n1 = 0;
3666 else
3667 n1 = n1 % n2;
3668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 rettv->v_type = VAR_NUMBER;
3670 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 }
3673
3674 return OK;
3675}
3676
3677/*
3678 * Handle sixth level expression:
3679 * number number constant
3680 * "string" string contstant
3681 * 'string' literal string contstant
3682 * &option-name option value
3683 * @r register contents
3684 * identifier variable value
3685 * function() function call
3686 * $VAR environment variable
3687 * (expression) nested expression
3688 *
3689 * Also handle:
3690 * ! in front logical NOT
3691 * - in front unary minus
3692 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 * trailing [] subscript in String or List
3694 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 *
3696 * "arg" must point to the first non-white of the expression.
3697 * "arg" is advanced to the next non-white after the recognized expression.
3698 *
3699 * Return OK or FAIL.
3700 */
3701 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003702eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003704 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 int evaluate;
3706{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 long n;
3708 int len;
3709 char_u *s;
3710 int val;
3711 char_u *start_leader, *end_leader;
3712 int ret = OK;
3713 char_u *alias;
Bram Moolenaare9a41262005-01-15 22:18:47 +00003714 dictvar *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003718 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 /*
3723 * Skip '!' and '-' characters. They are handled later.
3724 */
3725 start_leader = *arg;
3726 while (**arg == '!' || **arg == '-' || **arg == '+')
3727 *arg = skipwhite(*arg + 1);
3728 end_leader = *arg;
3729
3730 switch (**arg)
3731 {
3732 /*
3733 * Number constant.
3734 */
3735 case '0':
3736 case '1':
3737 case '2':
3738 case '3':
3739 case '4':
3740 case '5':
3741 case '6':
3742 case '7':
3743 case '8':
3744 case '9':
3745 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3746 *arg += len;
3747 if (evaluate)
3748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003749 rettv->v_type = VAR_NUMBER;
3750 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 break;
3753
3754 /*
3755 * String constant: "string".
3756 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 break;
3759
3760 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003761 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003764 break;
3765
3766 /*
3767 * List: [expr, expr]
3768 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 break;
3771
3772 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003773 * Dictionary: {key: val, key: val}
3774 */
3775 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3776 break;
3777
3778 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003779 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003781 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 break;
3783
3784 /*
3785 * Environment variable: $VAR.
3786 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 break;
3789
3790 /*
3791 * Register contents: @r.
3792 */
3793 case '@': ++*arg;
3794 if (evaluate)
3795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 rettv->v_type = VAR_STRING;
3797 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 if (**arg != NUL)
3800 ++*arg;
3801 break;
3802
3803 /*
3804 * nested expression: (expression).
3805 */
3806 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (**arg == ')')
3809 ++*arg;
3810 else if (ret == OK)
3811 {
3812 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 ret = FAIL;
3815 }
3816 break;
3817
Bram Moolenaar8c711452005-01-14 21:53:12 +00003818 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 break;
3820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003821
3822 if (ret == NOTDONE)
3823 {
3824 /*
3825 * Must be a variable or function name.
3826 * Can also be a curly-braces kind of name: {expr}.
3827 */
3828 s = *arg;
Bram Moolenaara7043832005-01-21 11:56:39 +00003829 len = get_name_len(arg, &alias, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003830 if (alias != NULL)
3831 s = alias;
3832
3833 if (len == 0)
3834 ret = FAIL;
3835 else
3836 {
3837 if (**arg == '(') /* recursive! */
3838 {
3839 /* If "s" is the name of a variable of type VAR_FUNC
3840 * use its contents. */
3841 s = deref_func_name(s, &len);
3842
3843 /* Invoke the function. */
3844 ret = get_func_tv(s, len, rettv, arg,
3845 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003846 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003847 /* Stop the expression evaluation when immediately
3848 * aborting on error, or when an interrupt occurred or
3849 * an exception was thrown but not caught. */
3850 if (aborting())
3851 {
3852 if (ret == OK)
3853 clear_tv(rettv);
3854 ret = FAIL;
3855 }
3856 }
3857 else if (evaluate)
3858 ret = get_var_tv(s, len, rettv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003859 else
3860 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003861 }
3862
3863 if (alias != NULL)
3864 vim_free(alias);
3865 }
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 *arg = skipwhite(*arg);
3868
3869 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003870 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00003871 * Also handle function call with Funcref variable: func(expr)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003872 * Can all be combined: dict.func(expr)[idx]['func'](expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003874 selfdict = NULL;
3875 while (ret == OK
3876 && (**arg == '['
3877 || (**arg == '.' && rettv->v_type == VAR_DICT)
3878 || (**arg == '(' && rettv->v_type == VAR_FUNC))
3879 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003881 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003883 s = rettv->vval.v_string;
3884
3885 /* Invoke the function. Recursive! */
3886 ret = get_func_tv(s, STRLEN(s), rettv, arg,
3887 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3888 &len, evaluate, selfdict);
3889
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003890 /* Stop the expression evaluation when immediately aborting on
3891 * error, or when an interrupt occurred or an exception was thrown
3892 * but not caught. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003893 if (aborting())
3894 {
3895 if (ret == OK)
3896 clear_tv(rettv);
3897 ret = FAIL;
3898 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003899 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003900 selfdict = NULL;
3901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003902 else /* **arg == '[' || **arg == '.' */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003904 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003905 if (rettv->v_type == VAR_DICT)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003906 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003907 selfdict = rettv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003908 if (selfdict != NULL)
3909 ++selfdict->dv_refcount;
3910 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003911 else
3912 selfdict = NULL;
3913 if (eval_index(arg, rettv, evaluate) == FAIL)
3914 {
3915 clear_tv(rettv);
3916 ret = FAIL;
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003920 dict_unref(selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 /*
3923 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3924 */
3925 if (ret == OK && evaluate && end_leader > start_leader)
3926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003927 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 while (end_leader > start_leader)
3929 {
3930 --end_leader;
3931 if (*end_leader == '!')
3932 val = !val;
3933 else if (*end_leader == '-')
3934 val = -val;
3935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003936 clear_tv(rettv);
3937 rettv->v_type = VAR_NUMBER;
3938 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
3940
3941 return ret;
3942}
3943
3944/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003945 * Evaluate an "[expr]" or "[expr:expr]" index.
3946 * "*arg" points to the '['.
3947 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3948 */
3949 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003951 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003953 int evaluate;
3954{
3955 int empty1 = FALSE, empty2 = FALSE;
3956 typeval var1, var2;
3957 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003958 long len = -1;
3959 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003960 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003961 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003965 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003966 return FAIL;
3967 }
3968
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003971 /*
3972 * dict.name
3973 */
3974 key = *arg + 1;
3975 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3976 ;
3977 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003978 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003979 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003980 }
3981 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003983 /*
3984 * something[idx]
3985 *
3986 * Get the (first) variable from inside the [].
3987 */
3988 *arg = skipwhite(*arg + 1);
3989 if (**arg == ':')
3990 empty1 = TRUE;
3991 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3992 return FAIL;
3993
3994 /*
3995 * Get the second variable from inside the [:].
3996 */
3997 if (**arg == ':')
3998 {
3999 range = TRUE;
4000 *arg = skipwhite(*arg + 1);
4001 if (**arg == ']')
4002 empty2 = TRUE;
4003 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4004 {
4005 clear_tv(&var1);
4006 return FAIL;
4007 }
4008 }
4009
4010 /* Check for the ']'. */
4011 if (**arg != ']')
4012 {
4013 EMSG(_(e_missbrac));
4014 clear_tv(&var1);
4015 if (range)
4016 clear_tv(&var2);
4017 return FAIL;
4018 }
4019 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004020 }
4021
4022 if (evaluate)
4023 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004024 n1 = 0;
4025 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 n1 = get_tv_number(&var1);
4028 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004029 }
4030 if (range)
4031 {
4032 if (empty2)
4033 n2 = -1;
4034 else
4035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 n2 = get_tv_number(&var2);
4037 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004038 }
4039 }
4040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004042 {
4043 case VAR_NUMBER:
4044 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004046 len = (long)STRLEN(s);
4047 if (range)
4048 {
4049 /* The resulting variable is a substring. If the indexes
4050 * are out of range the result is empty. */
4051 if (n1 < 0)
4052 {
4053 n1 = len + n1;
4054 if (n1 < 0)
4055 n1 = 0;
4056 }
4057 if (n2 < 0)
4058 n2 = len + n2;
4059 else if (n2 >= len)
4060 n2 = len;
4061 if (n1 >= len || n2 < 0 || n1 > n2)
4062 s = NULL;
4063 else
4064 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4065 }
4066 else
4067 {
4068 /* The resulting variable is a string of a single
4069 * character. If the index is too big or negative the
4070 * result is empty. */
4071 if (n1 >= len || n1 < 0)
4072 s = NULL;
4073 else
4074 s = vim_strnsave(s + n1, 1);
4075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076 clear_tv(rettv);
4077 rettv->v_type = VAR_STRING;
4078 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004079 break;
4080
4081 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004083 if (n1 < 0)
4084 n1 = len + n1;
4085 if (!empty1 && (n1 < 0 || n1 >= len))
4086 {
4087 EMSGN(_(e_listidx), n1);
4088 return FAIL;
4089 }
4090 if (range)
4091 {
4092 listvar *l;
4093 listitem *item;
4094
4095 if (n2 < 0)
4096 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004097 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004098 {
4099 EMSGN(_(e_listidx), n2);
4100 return FAIL;
4101 }
4102 l = list_alloc();
4103 if (l == NULL)
4104 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004106 n1 <= n2; ++n1)
4107 {
4108 if (list_append_tv(l, &item->li_tv) == FAIL)
4109 {
4110 list_free(l);
4111 return FAIL;
4112 }
4113 item = item->li_next;
4114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
4116 rettv->v_type = VAR_LIST;
4117 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004118 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004119 }
4120 else
4121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 clear_tv(rettv);
4125 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004126 }
4127 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128
4129 case VAR_DICT:
4130 if (range)
4131 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004132 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004133 if (len == -1)
4134 clear_tv(&var1);
4135 return FAIL;
4136 }
4137 {
4138 dictitem *item;
4139
4140 if (len == -1)
4141 {
4142 key = get_tv_string(&var1);
4143 if (*key == NUL)
4144 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004145 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 clear_tv(&var1);
4147 return FAIL;
4148 }
4149 }
4150
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004151 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152
4153 if (item == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004154 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004155 if (len == -1)
4156 clear_tv(&var1);
4157 if (item == NULL)
4158 return FAIL;
4159
4160 copy_tv(&item->di_tv, &var1);
4161 clear_tv(rettv);
4162 *rettv = var1;
4163 }
4164 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 }
4166 }
4167
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004168 return OK;
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * Get an option value.
4173 * "arg" points to the '&' or '+' before the option name.
4174 * "arg" is advanced to character after the option name.
4175 * Return OK or FAIL.
4176 */
4177 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int evaluate;
4182{
4183 char_u *option_end;
4184 long numval;
4185 char_u *stringval;
4186 int opt_type;
4187 int c;
4188 int working = (**arg == '+'); /* has("+option") */
4189 int ret = OK;
4190 int opt_flags;
4191
4192 /*
4193 * Isolate the option name and find its value.
4194 */
4195 option_end = find_option_end(arg, &opt_flags);
4196 if (option_end == NULL)
4197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 EMSG2(_("E112: Option name missing: %s"), *arg);
4200 return FAIL;
4201 }
4202
4203 if (!evaluate)
4204 {
4205 *arg = option_end;
4206 return OK;
4207 }
4208
4209 c = *option_end;
4210 *option_end = NUL;
4211 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 if (opt_type == -3) /* invalid name */
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 EMSG2(_("E113: Unknown option: %s"), *arg);
4218 ret = FAIL;
4219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
4222 if (opt_type == -2) /* hidden string option */
4223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 rettv->v_type = VAR_STRING;
4225 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 else if (opt_type == -1) /* hidden number option */
4228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 rettv->v_type = VAR_NUMBER;
4230 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 else if (opt_type == 1) /* number option */
4233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 rettv->v_type = VAR_NUMBER;
4235 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 else /* string option */
4238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 rettv->v_type = VAR_STRING;
4240 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 }
4243 else if (working && (opt_type == -2 || opt_type == -1))
4244 ret = FAIL;
4245
4246 *option_end = c; /* put back for error messages */
4247 *arg = option_end;
4248
4249 return ret;
4250}
4251
4252/*
4253 * Allocate a variable for a string constant.
4254 * Return OK or FAIL.
4255 */
4256 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 int evaluate;
4261{
4262 char_u *p;
4263 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 int extra = 0;
4265
4266 /*
4267 * Find the end of the string, skipping backslashed characters.
4268 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004269 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 if (*p == '\\' && p[1] != NUL)
4272 {
4273 ++p;
4274 /* A "\<x>" form occupies at least 4 characters, and produces up
4275 * to 6 characters: reserve space for 2 extra */
4276 if (*p == '<')
4277 extra += 2;
4278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280
4281 if (*p != '"')
4282 {
4283 EMSG2(_("E114: Missing quote: %s"), *arg);
4284 return FAIL;
4285 }
4286
4287 /* If only parsing, set *arg and return here */
4288 if (!evaluate)
4289 {
4290 *arg = p + 1;
4291 return OK;
4292 }
4293
4294 /*
4295 * Copy the string into allocated memory, handling backslashed
4296 * characters.
4297 */
4298 name = alloc((unsigned)(p - *arg + extra));
4299 if (name == NULL)
4300 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301 rettv->v_type = VAR_STRING;
4302 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
4306 if (*p == '\\')
4307 {
4308 switch (*++p)
4309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 case 'b': *name++ = BS; ++p; break;
4311 case 'e': *name++ = ESC; ++p; break;
4312 case 'f': *name++ = FF; ++p; break;
4313 case 'n': *name++ = NL; ++p; break;
4314 case 'r': *name++ = CAR; ++p; break;
4315 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 case 'X': /* hex: "\x1", "\x12" */
4318 case 'x':
4319 case 'u': /* Unicode: "\u0023" */
4320 case 'U':
4321 if (vim_isxdigit(p[1]))
4322 {
4323 int n, nr;
4324 int c = toupper(*p);
4325
4326 if (c == 'X')
4327 n = 2;
4328 else
4329 n = 4;
4330 nr = 0;
4331 while (--n >= 0 && vim_isxdigit(p[1]))
4332 {
4333 ++p;
4334 nr = (nr << 4) + hex2nr(*p);
4335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004336 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#ifdef FEAT_MBYTE
4338 /* For "\u" store the number according to
4339 * 'encoding'. */
4340 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 else
4343#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004344 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 break;
4347
4348 /* octal: "\1", "\12", "\123" */
4349 case '0':
4350 case '1':
4351 case '2':
4352 case '3':
4353 case '4':
4354 case '5':
4355 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004356 case '7': *name = *p++ - '0';
4357 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359 *name = (*name << 3) + *p++ - '0';
4360 if (*p >= '0' && *p <= '7')
4361 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004363 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 break;
4365
4366 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (extra != 0)
4369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 break;
4372 }
4373 /* FALLTHROUGH */
4374
Bram Moolenaar8c711452005-01-14 21:53:12 +00004375 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 break;
4377 }
4378 }
4379 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004383 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *arg = p + 1;
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return OK;
4387}
4388
4389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 * Return OK or FAIL.
4392 */
4393 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int evaluate;
4398{
4399 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004400 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004401 int reduce = 0;
4402
4403 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004404 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004405 */
4406 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004408 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004410 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004411 break;
4412 ++reduce;
4413 ++p;
4414 }
4415 }
4416
Bram Moolenaar8c711452005-01-14 21:53:12 +00004417 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004419 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004420 return FAIL;
4421 }
4422
Bram Moolenaar8c711452005-01-14 21:53:12 +00004423 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004424 if (!evaluate)
4425 {
4426 *arg = p + 1;
4427 return OK;
4428 }
4429
4430 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004432 */
4433 str = alloc((unsigned)((p - *arg) - reduce));
4434 if (str == NULL)
4435 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 rettv->v_type = VAR_STRING;
4437 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004438
Bram Moolenaar8c711452005-01-14 21:53:12 +00004439 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004440 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004443 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004444 break;
4445 ++p;
4446 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004448 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004450 *arg = p + 1;
4451
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004452 return OK;
4453}
4454
4455/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 * Allocate a variable for a List and fill it from "*arg".
4457 * Return OK or FAIL.
4458 */
4459 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463 int evaluate;
4464{
4465 listvar *l = NULL;
4466 typeval tv;
4467 listitem *item;
4468
4469 if (evaluate)
4470 {
4471 l = list_alloc();
4472 if (l == NULL)
4473 return FAIL;
4474 }
4475
4476 *arg = skipwhite(*arg + 1);
4477 while (**arg != ']' && **arg != NUL)
4478 {
4479 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4480 goto failret;
4481 if (evaluate)
4482 {
4483 item = listitem_alloc();
4484 if (item != NULL)
4485 {
4486 item->li_tv = tv;
4487 list_append(l, item);
4488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004489 else
4490 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491 }
4492
4493 if (**arg == ']')
4494 break;
4495 if (**arg != ',')
4496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004498 goto failret;
4499 }
4500 *arg = skipwhite(*arg + 1);
4501 }
4502
4503 if (**arg != ']')
4504 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506failret:
4507 if (evaluate)
4508 list_free(l);
4509 return FAIL;
4510 }
4511
4512 *arg = skipwhite(*arg + 1);
4513 if (evaluate)
4514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515 rettv->v_type = VAR_LIST;
4516 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 ++l->lv_refcount;
4518 }
4519
4520 return OK;
4521}
4522
4523/*
4524 * Allocate an empty header for a list.
4525 */
4526 static listvar *
4527list_alloc()
4528{
4529 return (listvar *)alloc_clear(sizeof(listvar));
4530}
4531
4532/*
4533 * Unreference a list: decrement the reference count and free it when it
4534 * becomes zero.
4535 */
4536 static void
4537list_unref(l)
4538 listvar *l;
4539{
4540 if (l != NULL && --l->lv_refcount <= 0)
4541 list_free(l);
4542}
4543
4544/*
4545 * Free a list, including all items it points to.
4546 * Ignores the reference count.
4547 */
4548 static void
4549list_free(l)
4550 listvar *l;
4551{
4552 listitem *item;
4553 listitem *next;
4554
4555 for (item = l->lv_first; item != NULL; item = next)
4556 {
4557 next = item->li_next;
4558 listitem_free(item);
4559 }
4560 vim_free(l);
4561}
4562
4563/*
4564 * Allocate a list item.
4565 */
4566 static listitem *
4567listitem_alloc()
4568{
4569 return (listitem *)alloc(sizeof(listitem));
4570}
4571
4572/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004573 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 */
4575 static void
4576listitem_free(item)
4577 listitem *item;
4578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 vim_free(item);
4581}
4582
4583/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004584 * Remove a list item from a List and free it. Also clears the value.
4585 */
4586 static void
4587listitem_remove(l, item)
4588 listvar *l;
4589 listitem *item;
4590{
4591 list_remove(l, item, item);
4592 listitem_free(item);
4593}
4594
4595/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 * Get the number of items in a list.
4597 */
4598 static long
4599list_len(l)
4600 listvar *l;
4601{
4602 listitem *item;
4603 long len = 0;
4604
4605 if (l == NULL)
4606 return 0L;
4607 for (item = l->lv_first; item != NULL; item = item->li_next)
4608 ++len;
4609 return len;
4610}
4611
4612/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004613 * Return TRUE when two lists have exactly the same values.
4614 */
4615 static int
4616list_equal(l1, l2, ic)
4617 listvar *l1;
4618 listvar *l2;
4619 int ic; /* ignore case for strings */
4620{
4621 listitem *item1, *item2;
4622
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004623 if (list_len(l1) != list_len(l2))
4624 return FALSE;
4625
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004626 for (item1 = l1->lv_first, item2 = l2->lv_first;
4627 item1 != NULL && item2 != NULL;
4628 item1 = item1->li_next, item2 = item2->li_next)
4629 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4630 return FALSE;
4631 return item1 == NULL && item2 == NULL;
4632}
4633
4634/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004635 * Return TRUE when two dictionaries have exactly the same key/values.
4636 */
4637 static int
4638dict_equal(d1, d2, ic)
4639 dictvar *d1;
4640 dictvar *d2;
4641 int ic; /* ignore case for strings */
4642{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004643 hashitem *hi;
4644 dictitem *item2;
4645 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004646
4647 if (dict_len(d1) != dict_len(d2))
4648 return FALSE;
4649
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004650 todo = d1->dv_hashtable.ht_used;
4651 for (hi = d1->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004653 if (!HASHITEM_EMPTY(hi))
4654 {
4655 item2 = dict_find(d2, hi->hi_key, -1);
4656 if (item2 == NULL)
4657 return FALSE;
4658 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4659 return FALSE;
4660 --todo;
4661 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004662 }
4663 return TRUE;
4664}
4665
4666/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004667 * Return TRUE if "tv1" and "tv2" have the same value.
4668 * Compares the items just like "==" would compare them.
4669 */
4670 static int
4671tv_equal(tv1, tv2, ic)
4672 typeval *tv1;
4673 typeval *tv2;
4674 int ic; /* ignore case */
4675{
4676 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4677
4678 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4679 {
4680 /* recursive! */
4681 if (tv1->v_type != tv2->v_type
4682 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4683 return FALSE;
4684 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004685 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4686 {
4687 /* recursive! */
4688 if (tv1->v_type != tv2->v_type
4689 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4690 return FALSE;
4691 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004692 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4693 {
4694 if (tv1->v_type != tv2->v_type
4695 || tv1->vval.v_string == NULL
4696 || tv2->vval.v_string == NULL
4697 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4698 return FALSE;
4699 }
4700 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4701 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004702 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4703 * Don't consider "4x" to be equal to 4. */
4704 if ((tv1->v_type == VAR_STRING
4705 && !string_isa_number(tv1->vval.v_string))
4706 || (tv2->v_type == VAR_STRING
4707 && !string_isa_number(tv2->vval.v_string)))
4708 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004709 if (get_tv_number(tv1) != get_tv_number(tv2))
4710 return FALSE;
4711 }
4712 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4713 get_tv_string_buf(tv2, buf2)) != 0)
4714 return FALSE;
4715 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4716 get_tv_string_buf(tv2, buf2)) != 0)
4717 return FALSE;
4718 return TRUE;
4719}
4720
4721/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004722 * Return TRUE if "tv" is a number without other non-white characters.
4723 */
4724 static int
4725string_isa_number(s)
4726 char_u *s;
4727{
4728 int len;
4729
4730 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4731 return len > 0 && *skipwhite(s + len) == NUL;
4732}
4733
4734/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 * Locate item with index "n" in list "l" and return it.
4736 * A negative index is counted from the end; -1 is the last item.
4737 * Returns NULL when "n" is out of range.
4738 */
4739 static listitem *
4740list_find(l, n)
4741 listvar *l;
4742 long n;
4743{
4744 listitem *item;
4745 long idx;
4746
4747 if (l == NULL)
4748 return NULL;
4749 if (n < 0)
4750 {
4751 idx = -1; /* search from the end */
4752 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4753 --idx;
4754 }
4755 else
4756 {
4757 idx = 0; /* search from the start */
4758 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4759 ++idx;
4760 }
4761 if (idx != n)
4762 return NULL;
4763 return item;
4764}
4765
4766/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004767 * Locate "item" list "l" and return its index.
4768 * Returns -1 when "item" is not in the list.
4769 */
4770 static long
4771list_idx_of_item(l, item)
4772 listvar *l;
4773 listitem *item;
4774{
4775 long idx = 0;
4776 listitem *li;
4777
4778 if (l == NULL)
4779 return -1;
4780 idx = 0;
4781 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4782 ++idx;
4783 if (li == NULL)
4784 return -1;
4785 return idx;;
4786}
4787
4788/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004789 * Like list_find(), but also find an item just past the end.
4790 * "*ip" is the item to find.
4791 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4792 * Returns NULL when item not found or item is just past the end.
4793 */
4794 static listitem *
4795list_find_ext(l, ip)
4796 listvar *l;
4797 long *ip;
4798{
4799 long n;
4800 listitem *item;
4801
4802 if (*ip < 0)
4803 {
4804 /* Count from the end: -1 is before last item. */
4805 item = l->lv_last;
4806 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4807 item = item->li_prev;
4808 if (item == NULL)
4809 n = 1; /* error! */
4810 }
4811 else
4812 {
4813 item = l->lv_first;
4814 for (n = *ip; n > 0 && item != NULL; --n)
4815 item = item->li_next;
4816 }
4817 *ip = n;
4818 return item;
4819}
4820
4821/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 * Append item "item" to the end of list "l".
4823 */
4824 static void
4825list_append(l, item)
4826 listvar *l;
4827 listitem *item;
4828{
4829 if (l->lv_last == NULL)
4830 {
4831 /* empty list */
4832 l->lv_first = item;
4833 l->lv_last = item;
4834 item->li_prev = NULL;
4835 }
4836 else
4837 {
4838 l->lv_last->li_next = item;
4839 item->li_prev = l->lv_last;
4840 l->lv_last = item;
4841 }
4842 item->li_next = NULL;
4843}
4844
4845/*
4846 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004847 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 */
4849 static int
4850list_append_tv(l, tv)
4851 listvar *l;
4852 typeval *tv;
4853{
4854 listitem *ni = listitem_alloc();
4855
4856 if (ni == NULL)
4857 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 list_append(l, ni);
4860 return OK;
4861}
4862
4863/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004864 * Insert typeval "tv" in list "l" before "item".
4865 * If "item" is NULL append at the end.
4866 * Return FAIL when out of memory.
4867 */
4868 static int
4869list_insert_tv(l, tv, item)
4870 listvar *l;
4871 typeval *tv;
4872 listitem *item;
4873{
4874 listitem *ni = listitem_alloc();
4875
4876 if (ni == NULL)
4877 return FAIL;
4878 copy_tv(tv, &ni->li_tv);
4879 if (item == NULL)
4880 /* Append new item at end of list. */
4881 list_append(l, ni);
4882 else
4883 {
4884 /* Insert new item before existing item. */
4885 ni->li_prev = item->li_prev;
4886 ni->li_next = item;
4887 if (item->li_prev == NULL)
4888 l->lv_first = ni;
4889 else
4890 item->li_prev->li_next = ni;
4891 item->li_prev = ni;
4892 }
4893 return OK;
4894}
4895
4896/*
4897 * Extend "l1" with "l2".
4898 * If "bef" is NULL append at the end, otherwise insert before this item.
4899 * Returns FAIL when out of memory.
4900 */
4901 static int
4902list_extend(l1, l2, bef)
4903 listvar *l1;
4904 listvar *l2;
4905 listitem *bef;
4906{
4907 listitem *item;
4908
4909 for (item = l2->lv_first; item != NULL; item = item->li_next)
4910 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4911 return FAIL;
4912 return OK;
4913}
4914
4915/*
4916 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4917 * Return FAIL when out of memory.
4918 */
4919 static int
4920list_concat(l1, l2, tv)
4921 listvar *l1;
4922 listvar *l2;
4923 typeval *tv;
4924{
4925 listvar *l;
4926
4927 /* make a copy of the first list. */
4928 l = list_copy(l1, FALSE);
4929 if (l == NULL)
4930 return FAIL;
4931 tv->v_type = VAR_LIST;
4932 tv->vval.v_list = l;
4933
4934 /* append all items from the second list */
4935 return list_extend(l, l2, NULL);
4936}
4937
4938/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004939 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 * The refcount of the new list is set to 1.
4941 * Returns NULL when out of memory.
4942 */
4943 static listvar *
4944list_copy(orig, deep)
4945 listvar *orig;
4946 int deep;
4947{
4948 listvar *copy;
4949 listitem *item;
4950 listitem *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951
4952 if (orig == NULL)
4953 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954
4955 copy = list_alloc();
4956 if (copy != NULL)
4957 {
4958 for (item = orig->lv_first; item != NULL; item = item->li_next)
4959 {
4960 ni = listitem_alloc();
4961 if (ni == NULL)
4962 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004963 if (deep)
4964 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 list_append(copy, ni);
4968 }
4969 ++copy->lv_refcount;
4970 }
4971
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972 return copy;
4973}
4974
4975/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004976 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004977 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004979 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004980list_remove(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004983 listitem *item2;
4984{
4985 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004987 /* notify watchers */
4988 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004990 list_fix_watch(l, ip);
4991 if (ip == item2)
4992 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004994
4995 if (item2->li_next == NULL)
4996 l->lv_last = item->li_prev;
4997 else
4998 item2->li_next->li_prev = item->li_prev;
4999 if (item->li_prev == NULL)
5000 l->lv_first = item2->li_next;
5001 else
5002 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003}
5004
5005/*
5006 * Return an allocated string with the string representation of a list.
5007 * May return NULL.
5008 */
5009 static char_u *
5010list2string(tv)
5011 typeval *tv;
5012{
5013 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014
5015 if (tv->vval.v_list == NULL)
5016 return NULL;
5017 ga_init2(&ga, (int)sizeof(char), 80);
5018 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005019 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 ga_append(&ga, ']');
5021 ga_append(&ga, NUL);
5022 return (char_u *)ga.ga_data;
5023}
5024
5025/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005026 * Join list "l" into a string in "*gap", using separator "sep".
5027 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
5028 */
5029 static void
5030list_join(gap, l, sep, echo)
5031 garray_T *gap;
5032 listvar *l;
5033 char_u *sep;
5034 int echo;
5035{
5036 int first = TRUE;
5037 char_u *tofree;
5038 char_u numbuf[NUMBUFLEN];
5039 listitem *item;
5040 char_u *s;
5041
5042 for (item = l->lv_first; item != NULL; item = item->li_next)
5043 {
5044 if (first)
5045 first = FALSE;
5046 else
5047 ga_concat(gap, sep);
5048
5049 if (echo)
5050 s = echo_string(&item->li_tv, &tofree, numbuf);
5051 else
5052 s = tv2string(&item->li_tv, &tofree, numbuf);
5053 if (s != NULL)
5054 ga_concat(gap, s);
5055 vim_free(tofree);
5056 }
5057}
5058
5059/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 * Allocate an empty header for a dictionary.
5061 */
5062 static dictvar *
5063dict_alloc()
5064{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005065 dictvar *d;
5066
5067 d = (dictvar *)alloc(sizeof(dictvar));
5068 if (d != NULL)
5069 hash_init(&d->dv_hashtable);
5070 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071}
5072
5073/*
5074 * Unreference a Dictionary: decrement the reference count and free it when it
5075 * becomes zero.
5076 */
5077 static void
5078dict_unref(d)
5079 dictvar *d;
5080{
5081 if (d != NULL && --d->dv_refcount <= 0)
5082 dict_free(d);
5083}
5084
5085/*
5086 * Free a Dictionary, including all items it contains.
5087 * Ignores the reference count.
5088 */
5089 static void
5090dict_free(d)
5091 dictvar *d;
5092{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005093 int todo;
5094 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005096 /* Careful: we free the dictitems while they still appear in the
5097 * hashtable. Must not try to resize the hashtable! */
5098 todo = d->dv_hashtable.ht_used;
5099 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005101 if (!HASHITEM_EMPTY(hi))
5102 {
5103 dictitem_free(HI2DI(hi));
5104 --todo;
5105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 }
Bram Moolenaara7043832005-01-21 11:56:39 +00005107 hash_clear(&d->dv_hashtable);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 vim_free(d);
5109}
5110
5111/*
5112 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005113 * The "key" is copied to the new item.
5114 * Note that the value of the item "di_tv" still needs to be initialized!
5115 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 */
5117 static dictitem *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005118dictitem_alloc(key)
5119 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005121 dictitem *di;
5122
5123 di = (dictitem *)alloc(sizeof(dictitem) + STRLEN(key));
5124 if (di != NULL)
5125 STRCPY(di->di_key, key);
5126 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127}
5128
5129/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005130 * Make a copy of a Dictionary item.
5131 */
5132 static dictitem *
5133dictitem_copy(org)
5134 dictitem *org;
5135{
5136 dictitem *di;
5137
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005138 di = (dictitem *)alloc(sizeof(dictitem) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005139 if (di != NULL)
5140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005141 STRCPY(di->di_key, org->di_key);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005142 copy_tv(&org->di_tv, &di->di_tv);
5143 }
5144 return di;
5145}
5146
5147/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005148 * Remove item "item" from Dictionary "dict" and free it.
5149 */
5150 static void
5151dictitem_remove(dict, item)
5152 dictvar *dict;
5153 dictitem *item;
5154{
5155 hashitem *hi;
5156
5157 hi = hash_find(&dict->dv_hashtable, item->di_key);
5158 if (HASHITEM_EMPTY(hi))
5159 EMSG2(_(e_intern2), "dictitem_remove()");
5160 else
5161 hash_remove(&dict->dv_hashtable, hi);
5162 dictitem_free(item);
5163}
5164
5165/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 * Free a dict item. Also clears the value.
5167 */
5168 static void
5169dictitem_free(item)
5170 dictitem *item;
5171{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 clear_tv(&item->di_tv);
5173 vim_free(item);
5174}
5175
5176/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5178 * The refcount of the new dict is set to 1.
5179 * Returns NULL when out of memory.
5180 */
5181 static dictvar *
5182dict_copy(orig, deep)
5183 dictvar *orig;
5184 int deep;
5185{
5186 dictvar *copy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005187 dictitem *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005188 int todo;
5189 hashitem *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005190
5191 if (orig == NULL)
5192 return NULL;
5193
5194 copy = dict_alloc();
5195 if (copy != NULL)
5196 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005197 todo = orig->dv_hashtable.ht_used;
5198 for (hi = orig->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005202 --todo;
5203
5204 di = dictitem_alloc(hi->hi_key);
5205 if (di == NULL)
5206 break;
5207 if (deep)
5208 item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep);
5209 else
5210 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5211 if (dict_add(copy, di) == FAIL)
5212 {
5213 dictitem_free(di);
5214 break;
5215 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005217 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005218
Bram Moolenaare9a41262005-01-15 22:18:47 +00005219 ++copy->dv_refcount;
5220 }
5221
5222 return copy;
5223}
5224
5225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005227 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005229 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230dict_add(d, item)
5231 dictvar *d;
5232 dictitem *item;
5233{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005234 return hash_add(&d->dv_hashtable, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235}
5236
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 * Get the number of items in a Dictionary.
5239 */
5240 static long
5241dict_len(d)
5242 dictvar *d;
5243{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005244 if (d == NULL)
5245 return 0L;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005246 return d->dv_hashtable.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005247}
5248
5249/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 * Find item "key[len]" in Dictionary "d".
5251 * If "len" is negative use strlen(key).
5252 * Returns NULL when not found.
5253 */
5254 static dictitem *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005255dict_find(d, key, len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 dictvar *d;
5257 char_u *key;
5258 int len;
5259{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005260#define AKEYLEN 200
5261 char_u buf[AKEYLEN];
5262 char_u *akey;
5263 char_u *tofree = NULL;
5264 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005266 if (len < 0)
5267 akey = key;
5268 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005269 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005270 tofree = akey = vim_strnsave(key, len);
5271 if (akey == NULL)
5272 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005274 else
5275 {
5276 /* Avoid a malloc/free by using buf[]. */
5277 STRNCPY(buf, key, len);
5278 buf[len] = NUL;
5279 akey = buf;
5280 }
5281
5282 hi = hash_find(&d->dv_hashtable, akey);
5283 vim_free(tofree);
5284 if (HASHITEM_EMPTY(hi))
5285 return NULL;
5286 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287}
5288
5289/*
5290 * Return an allocated string with the string representation of a Dictionary.
5291 * May return NULL.
5292 */
5293 static char_u *
5294dict2string(tv)
5295 typeval *tv;
5296{
5297 garray_T ga;
5298 int first = TRUE;
5299 char_u *tofree;
5300 char_u numbuf[NUMBUFLEN];
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005301 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 char_u *s;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005303 dictvar *d;
5304 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005306 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 return NULL;
5308 ga_init2(&ga, (int)sizeof(char), 80);
5309 ga_append(&ga, '{');
5310
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005311 todo = d->dv_hashtable.ht_used;
5312 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005314 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005316 --todo;
5317
5318 if (first)
5319 first = FALSE;
5320 else
5321 ga_concat(&ga, (char_u *)", ");
5322
5323 tofree = string_quote(hi->hi_key, FALSE);
5324 if (tofree != NULL)
5325 {
5326 ga_concat(&ga, tofree);
5327 vim_free(tofree);
5328 }
5329 ga_concat(&ga, (char_u *)": ");
5330 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5331 if (s != NULL)
5332 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 vim_free(tofree);
5334 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005335 }
5336
5337 ga_append(&ga, '}');
5338 ga_append(&ga, NUL);
5339 return (char_u *)ga.ga_data;
5340}
5341
5342/*
5343 * Allocate a variable for a Dictionary and fill it from "*arg".
5344 * Return OK or FAIL. Returns NOTDONE for {expr}.
5345 */
5346 static int
5347get_dict_tv(arg, rettv, evaluate)
5348 char_u **arg;
5349 typeval *rettv;
5350 int evaluate;
5351{
5352 dictvar *d = NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005353 typeval tvkey;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 typeval tv;
5355 char_u *key;
5356 dictitem *item;
5357 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005358 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359
5360 /*
5361 * First check if it's not a curly-braces thing: {expr}.
5362 * Must do this without evaluating, otherwise a function may be called
5363 * twice. Unfortunately this means we need to call eval1() twice for the
5364 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005365 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005367 if (*start != '}')
5368 {
5369 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5370 return FAIL;
5371 if (*start == '}')
5372 return NOTDONE;
5373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374
5375 if (evaluate)
5376 {
5377 d = dict_alloc();
5378 if (d == NULL)
5379 return FAIL;
5380 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005381 tvkey.v_type = VAR_UNKNOWN;
5382 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383
5384 *arg = skipwhite(*arg + 1);
5385 while (**arg != '}' && **arg != NUL)
5386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005387 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 goto failret;
5389 if (**arg != ':')
5390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005391 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005392 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 goto failret;
5394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005395 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 if (*key == NUL)
5397 {
5398 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005399 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 goto failret;
5401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402
5403 *arg = skipwhite(*arg + 1);
5404 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005406 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 goto failret;
5408 }
5409 if (evaluate)
5410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005411 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 if (item != NULL)
5413 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005414 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005415 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416 clear_tv(&tv);
5417 goto failret;
5418 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005419 item = dictitem_alloc(key);
5420 clear_tv(&tvkey);
5421 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423 item->di_tv = tv;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005424 if (dict_add(d, item) == FAIL)
5425 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426 }
5427 }
5428
5429 if (**arg == '}')
5430 break;
5431 if (**arg != ',')
5432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005433 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 goto failret;
5435 }
5436 *arg = skipwhite(*arg + 1);
5437 }
5438
5439 if (**arg != '}')
5440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005441 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442failret:
5443 if (evaluate)
5444 dict_free(d);
5445 return FAIL;
5446 }
5447
5448 *arg = skipwhite(*arg + 1);
5449 if (evaluate)
5450 {
5451 rettv->v_type = VAR_DICT;
5452 rettv->vval.v_dict = d;
5453 ++d->dv_refcount;
5454 }
5455
5456 return OK;
5457}
5458
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 * Return a string with the string representation of a variable.
5461 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005462 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005463 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 * May return NULL;
5465 */
5466 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005467echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 typeval *tv;
5469 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005470 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005472 static int recurse = 0;
5473 char_u *r = NULL;
5474
5475 if (recurse >= VAR_MAXNEST)
5476 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005477 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005478 *tofree = NULL;
5479 return NULL;
5480 }
5481 ++recurse;
5482
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 switch (tv->v_type)
5484 {
5485 case VAR_FUNC:
5486 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005487 r = tv->vval.v_string;
5488 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 case VAR_LIST:
5490 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005491 r = *tofree;
5492 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 case VAR_DICT:
5494 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005495 r = *tofree;
5496 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 case VAR_STRING:
5498 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005499 *tofree = NULL;
5500 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005503 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005504 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005506
5507 --recurse;
5508 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005509}
5510
5511/*
5512 * Return a string with the string representation of a variable.
5513 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5514 * "numbuf" is used for a number.
5515 * Puts quotes around strings, so that they can be parsed back by eval().
5516 * May return NULL;
5517 */
5518 static char_u *
5519tv2string(tv, tofree, numbuf)
5520 typeval *tv;
5521 char_u **tofree;
5522 char_u *numbuf;
5523{
5524 switch (tv->v_type)
5525 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005526 case VAR_FUNC:
5527 *tofree = string_quote(tv->vval.v_string, TRUE);
5528 return *tofree;
5529 case VAR_STRING:
5530 *tofree = string_quote(tv->vval.v_string, FALSE);
5531 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005532 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005533 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005534 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005535 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005536 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005539 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540}
5541
5542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 * Return a string in ' quotes, doubling ' characters.
5544 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005545 */
5546 static char_u *
5547string_quote(str, function)
5548 char_u *str;
5549 int function;
5550{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005551 unsigned len = STRLEN(str) + (function ? 13 : 3);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005552 char_u *p, *r, *s;
5553
5554 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005556 ++len;
5557 s = r = alloc(len);
5558 if (r != NULL)
5559 {
5560 if (function)
5561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005563 r += 10;
5564 }
5565 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 *r++ = '\'';
5567 for (p = str; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005568 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 if (*p == '\'')
5570 *r++ = '\'';
5571 MB_COPY_CHAR(p, r);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005572 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005574 if (function)
5575 *r++ = ')';
5576 *r++ = NUL;
5577 }
5578 return s;
5579}
5580
5581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 * Get the value of an environment variable.
5583 * "arg" is pointing to the '$'. It is advanced to after the name.
5584 * If the environment variable was not set, silently assume it is empty.
5585 * Always return OK.
5586 */
5587 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005588get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 int evaluate;
5592{
5593 char_u *string = NULL;
5594 int len;
5595 int cc;
5596 char_u *name;
5597
5598 ++*arg;
5599 name = *arg;
5600 len = get_env_len(arg);
5601 if (evaluate)
5602 {
5603 if (len != 0)
5604 {
5605 cc = name[len];
5606 name[len] = NUL;
5607 /* first try mch_getenv(), fast for normal environment vars */
5608 string = mch_getenv(name);
5609 if (string != NULL && *string != NUL)
5610 string = vim_strsave(string);
5611 else
5612 {
5613 /* next try expanding things like $VIM and ${HOME} */
5614 string = expand_env_save(name - 1);
5615 if (string != NULL && *string == '$')
5616 {
5617 vim_free(string);
5618 string = NULL;
5619 }
5620 }
5621 name[len] = cc;
5622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626
5627 return OK;
5628}
5629
5630/*
5631 * Array with names and number of arguments of all internal functions
5632 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5633 */
5634static struct fst
5635{
5636 char *f_name; /* function name */
5637 char f_min_argc; /* minimal number of arguments */
5638 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 void (*f_func) __ARGS((typeval *args, typeval *rvar));
5640 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641} functions[] =
5642{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005643 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {"append", 2, 2, f_append},
5645 {"argc", 0, 0, f_argc},
5646 {"argidx", 0, 0, f_argidx},
5647 {"argv", 1, 1, f_argv},
5648 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005649 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {"bufexists", 1, 1, f_bufexists},
5651 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5652 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5653 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5654 {"buflisted", 1, 1, f_buflisted},
5655 {"bufloaded", 1, 1, f_bufloaded},
5656 {"bufname", 1, 1, f_bufname},
5657 {"bufnr", 1, 1, f_bufnr},
5658 {"bufwinnr", 1, 1, f_bufwinnr},
5659 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005660 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005661 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {"char2nr", 1, 1, f_char2nr},
5663 {"cindent", 1, 1, f_cindent},
5664 {"col", 1, 1, f_col},
5665 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005667 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {"cscope_connection",0,3, f_cscope_connection},
5669 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {"delete", 1, 1, f_delete},
5672 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005673 {"diff_filler", 1, 1, f_diff_filler},
5674 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005675 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005677 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {"eventhandler", 0, 0, f_eventhandler},
5679 {"executable", 1, 1, f_executable},
5680 {"exists", 1, 1, f_exists},
5681 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005682 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5684 {"filereadable", 1, 1, f_filereadable},
5685 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005686 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005687 {"finddir", 1, 3, f_finddir},
5688 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {"fnamemodify", 2, 2, f_fnamemodify},
5690 {"foldclosed", 1, 1, f_foldclosed},
5691 {"foldclosedend", 1, 1, f_foldclosedend},
5692 {"foldlevel", 1, 1, f_foldlevel},
5693 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005694 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005697 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {"getbufvar", 2, 2, f_getbufvar},
5699 {"getchar", 0, 1, f_getchar},
5700 {"getcharmod", 0, 0, f_getcharmod},
5701 {"getcmdline", 0, 0, f_getcmdline},
5702 {"getcmdpos", 0, 0, f_getcmdpos},
5703 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005704 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005705 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {"getfsize", 1, 1, f_getfsize},
5707 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005708 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005709 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {"getreg", 0, 1, f_getreg},
5711 {"getregtype", 0, 1, f_getregtype},
5712 {"getwinposx", 0, 0, f_getwinposx},
5713 {"getwinposy", 0, 0, f_getwinposy},
5714 {"getwinvar", 2, 2, f_getwinvar},
5715 {"glob", 1, 1, f_glob},
5716 {"globpath", 2, 2, f_globpath},
5717 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {"hasmapto", 1, 2, f_hasmapto},
5720 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5721 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5722 {"histadd", 2, 2, f_histadd},
5723 {"histdel", 1, 2, f_histdel},
5724 {"histget", 1, 2, f_histget},
5725 {"histnr", 1, 1, f_histnr},
5726 {"hlID", 1, 1, f_hlID},
5727 {"hlexists", 1, 1, f_hlexists},
5728 {"hostname", 0, 0, f_hostname},
5729 {"iconv", 3, 3, f_iconv},
5730 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005731 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {"input", 1, 2, f_input},
5733 {"inputdialog", 1, 3, f_inputdialog},
5734 {"inputrestore", 0, 0, f_inputrestore},
5735 {"inputsave", 0, 0, f_inputsave},
5736 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {"libcall", 3, 3, f_libcall},
5745 {"libcallnr", 3, 3, f_libcallnr},
5746 {"line", 1, 1, f_line},
5747 {"line2byte", 1, 1, f_line2byte},
5748 {"lispindent", 1, 1, f_lispindent},
5749 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {"maparg", 1, 2, f_maparg},
5752 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005753 {"match", 2, 4, f_match},
5754 {"matchend", 2, 4, f_matchend},
5755 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005756 {"max", 1, 1, f_max},
5757 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {"mode", 0, 0, f_mode},
5759 {"nextnonblank", 1, 1, f_nextnonblank},
5760 {"nr2char", 1, 1, f_nr2char},
5761 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {"remote_expr", 2, 3, f_remote_expr},
5764 {"remote_foreground", 1, 1, f_remote_foreground},
5765 {"remote_peek", 1, 2, f_remote_peek},
5766 {"remote_read", 1, 1, f_remote_read},
5767 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005768 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005770 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005772 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {"search", 1, 2, f_search},
5774 {"searchpair", 3, 5, f_searchpair},
5775 {"server2client", 2, 2, f_server2client},
5776 {"serverlist", 0, 0, f_serverlist},
5777 {"setbufvar", 3, 3, f_setbufvar},
5778 {"setcmdpos", 1, 1, f_setcmdpos},
5779 {"setline", 2, 2, f_setline},
5780 {"setreg", 2, 3, f_setreg},
5781 {"setwinvar", 3, 3, f_setwinvar},
5782 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005783 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef HAVE_STRFTIME
5786 {"strftime", 1, 2, f_strftime},
5787#endif
5788 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {"strlen", 1, 1, f_strlen},
5791 {"strpart", 2, 3, f_strpart},
5792 {"strridx", 2, 2, f_strridx},
5793 {"strtrans", 1, 1, f_strtrans},
5794 {"submatch", 1, 1, f_submatch},
5795 {"substitute", 4, 4, f_substitute},
5796 {"synID", 3, 3, f_synID},
5797 {"synIDattr", 2, 3, f_synIDattr},
5798 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005799 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {"tempname", 0, 0, f_tempname},
5801 {"tolower", 1, 1, f_tolower},
5802 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005803 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {"virtcol", 1, 1, f_virtcol},
5807 {"visualmode", 0, 1, f_visualmode},
5808 {"winbufnr", 1, 1, f_winbufnr},
5809 {"wincol", 0, 0, f_wincol},
5810 {"winheight", 1, 1, f_winheight},
5811 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005812 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {"winrestcmd", 0, 0, f_winrestcmd},
5814 {"winwidth", 1, 1, f_winwidth},
5815};
5816
5817#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5818
5819/*
5820 * Function given to ExpandGeneric() to obtain the list of internal
5821 * or user defined function names.
5822 */
5823 char_u *
5824get_function_name(xp, idx)
5825 expand_T *xp;
5826 int idx;
5827{
5828 static int intidx = -1;
5829 char_u *name;
5830
5831 if (idx == 0)
5832 intidx = -1;
5833 if (intidx < 0)
5834 {
5835 name = get_user_func_name(xp, idx);
5836 if (name != NULL)
5837 return name;
5838 }
5839 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5840 {
5841 STRCPY(IObuff, functions[intidx].f_name);
5842 STRCAT(IObuff, "(");
5843 if (functions[intidx].f_max_argc == 0)
5844 STRCAT(IObuff, ")");
5845 return IObuff;
5846 }
5847
5848 return NULL;
5849}
5850
5851/*
5852 * Function given to ExpandGeneric() to obtain the list of internal or
5853 * user defined variable or function names.
5854 */
5855/*ARGSUSED*/
5856 char_u *
5857get_expr_name(xp, idx)
5858 expand_T *xp;
5859 int idx;
5860{
5861 static int intidx = -1;
5862 char_u *name;
5863
5864 if (idx == 0)
5865 intidx = -1;
5866 if (intidx < 0)
5867 {
5868 name = get_function_name(xp, idx);
5869 if (name != NULL)
5870 return name;
5871 }
5872 return get_user_var_name(xp, ++intidx);
5873}
5874
5875#endif /* FEAT_CMDL_COMPL */
5876
5877/*
5878 * Find internal function in table above.
5879 * Return index, or -1 if not found
5880 */
5881 static int
5882find_internal_func(name)
5883 char_u *name; /* name of the function */
5884{
5885 int first = 0;
5886 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5887 int cmp;
5888 int x;
5889
5890 /*
5891 * Find the function name in the table. Binary search.
5892 */
5893 while (first <= last)
5894 {
5895 x = first + ((unsigned)(last - first) >> 1);
5896 cmp = STRCMP(name, functions[x].f_name);
5897 if (cmp < 0)
5898 last = x - 1;
5899 else if (cmp > 0)
5900 first = x + 1;
5901 else
5902 return x;
5903 }
5904 return -1;
5905}
5906
5907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5909 * name it contains, otherwise return "name".
5910 */
5911 static char_u *
5912deref_func_name(name, lenp)
5913 char_u *name;
5914 int *lenp;
5915{
5916 VAR v;
5917 int cc;
5918
5919 cc = name[*lenp];
5920 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00005921 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 name[*lenp] = cc;
5923 if (v != NULL && v->tv.v_type == VAR_FUNC)
5924 {
5925 if (v->tv.vval.v_string == NULL)
5926 {
5927 *lenp = 0;
5928 return (char_u *)""; /* just in case */
5929 }
5930 *lenp = STRLEN(v->tv.vval.v_string);
5931 return v->tv.vval.v_string;
5932 }
5933
5934 return name;
5935}
5936
5937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * Allocate a variable for the result of a function.
5939 * Return OK or FAIL.
5940 */
5941 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005942get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5943 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 char_u *name; /* name of the function */
5945 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 char_u **arg; /* argument, pointing to the '(' */
5948 linenr_T firstline; /* first line of range */
5949 linenr_T lastline; /* last line of range */
5950 int *doesrange; /* return: function handled range */
5951 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005952 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
5954 char_u *argp;
5955 int ret = OK;
5956#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 int argcount = 0; /* number of arguments found */
5959
5960 /*
5961 * Get the arguments.
5962 */
5963 argp = *arg;
5964 while (argcount < MAX_FUNC_ARGS)
5965 {
5966 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5967 if (*argp == ')' || *argp == ',' || *argp == NUL)
5968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5970 {
5971 ret = FAIL;
5972 break;
5973 }
5974 ++argcount;
5975 if (*argp != ',')
5976 break;
5977 }
5978 if (*argp == ')')
5979 ++argp;
5980 else
5981 ret = FAIL;
5982
5983 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005984 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005985 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 else if (!aborting())
5987 EMSG2(_("E116: Invalid arguments for function %s"), name);
5988
5989 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005990 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
5992 *arg = skipwhite(argp);
5993 return ret;
5994}
5995
5996
5997/*
5998 * Call a function with its resolved parameters
5999 * Return OK or FAIL.
6000 */
6001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006002call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006003 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 char_u *name; /* name of the function */
6005 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006006 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 linenr_T firstline; /* first line of range */
6010 linenr_T lastline; /* last line of range */
6011 int *doesrange; /* return: function handled range */
6012 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006013 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#define ERROR_UNKNOWN 0
6017#define ERROR_TOOMANY 1
6018#define ERROR_TOOFEW 2
6019#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020#define ERROR_DICT 4
6021#define ERROR_NONE 5
6022#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 int error = ERROR_NONE;
6024 int i;
6025 int llen;
6026 ufunc_T *fp;
6027 int cc;
6028#define FLEN_FIXED 40
6029 char_u fname_buf[FLEN_FIXED + 1];
6030 char_u *fname;
6031
6032 /*
6033 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6034 * Change <SNR>123_name() to K_SNR 123_name().
6035 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6036 */
6037 cc = name[len];
6038 name[len] = NUL;
6039 llen = eval_fname_script(name);
6040 if (llen > 0)
6041 {
6042 fname_buf[0] = K_SPECIAL;
6043 fname_buf[1] = KS_EXTRA;
6044 fname_buf[2] = (int)KE_SNR;
6045 i = 3;
6046 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6047 {
6048 if (current_SID <= 0)
6049 error = ERROR_SCRIPT;
6050 else
6051 {
6052 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6053 i = (int)STRLEN(fname_buf);
6054 }
6055 }
6056 if (i + STRLEN(name + llen) < FLEN_FIXED)
6057 {
6058 STRCPY(fname_buf + i, name + llen);
6059 fname = fname_buf;
6060 }
6061 else
6062 {
6063 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6064 if (fname == NULL)
6065 error = ERROR_OTHER;
6066 else
6067 {
6068 mch_memmove(fname, fname_buf, (size_t)i);
6069 STRCPY(fname + i, name + llen);
6070 }
6071 }
6072 }
6073 else
6074 fname = name;
6075
6076 *doesrange = FALSE;
6077
6078
6079 /* execute the function if no errors detected and executing */
6080 if (evaluate && error == ERROR_NONE)
6081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006082 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 error = ERROR_UNKNOWN;
6084
6085 if (!ASCII_ISLOWER(fname[0]))
6086 {
6087 /*
6088 * User defined function.
6089 */
6090 fp = find_func(fname);
6091#ifdef FEAT_AUTOCMD
6092 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
6093 fname, fname, TRUE, NULL)
6094#ifdef FEAT_EVAL
6095 && !aborting()
6096#endif
6097 )
6098 {
6099 /* executed an autocommand, search for function again */
6100 fp = find_func(fname);
6101 }
6102#endif
6103 if (fp != NULL)
6104 {
6105 if (fp->flags & FC_RANGE)
6106 *doesrange = TRUE;
6107 if (argcount < fp->args.ga_len)
6108 error = ERROR_TOOFEW;
6109 else if (!fp->varargs && argcount > fp->args.ga_len)
6110 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006111 else if ((fp->flags & FC_DICT) && selfdict == NULL)
6112 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 else
6114 {
6115 /*
6116 * Call the user function.
6117 * Save and restore search patterns, script variables and
6118 * redo buffer.
6119 */
6120 save_search_patterns();
6121 saveRedobuff();
6122 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006123 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006124 firstline, lastline,
6125 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 if (--fp->calls <= 0 && isdigit(*fp->name)
6127 && fp->refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 /* Function was unreferenced while being used, free it
6129 * now. */
6130 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 restoreRedobuff();
6132 restore_search_patterns();
6133 error = ERROR_NONE;
6134 }
6135 }
6136 }
6137 else
6138 {
6139 /*
6140 * Find the function name in the table, call its implementation.
6141 */
6142 i = find_internal_func(fname);
6143 if (i >= 0)
6144 {
6145 if (argcount < functions[i].f_min_argc)
6146 error = ERROR_TOOFEW;
6147 else if (argcount > functions[i].f_max_argc)
6148 error = ERROR_TOOMANY;
6149 else
6150 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006152 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 error = ERROR_NONE;
6154 }
6155 }
6156 }
6157 /*
6158 * The function call (or "FuncUndefined" autocommand sequence) might
6159 * have been aborted by an error, an interrupt, or an explicitly thrown
6160 * exception that has not been caught so far. This situation can be
6161 * tested for by calling aborting(). For an error in an internal
6162 * function or for the "E132" error in call_user_func(), however, the
6163 * throw point at which the "force_abort" flag (temporarily reset by
6164 * emsg()) is normally updated has not been reached yet. We need to
6165 * update that flag first to make aborting() reliable.
6166 */
6167 update_force_abort();
6168 }
6169 if (error == ERROR_NONE)
6170 ret = OK;
6171
6172 /*
6173 * Report an error unless the argument evaluation or function call has been
6174 * cancelled due to an aborting error, an interrupt, or an exception.
6175 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006176 if (!aborting())
6177 {
6178 switch (error)
6179 {
6180 case ERROR_UNKNOWN:
6181 EMSG2(_("E117: Unknown function: %s"), name);
6182 break;
6183 case ERROR_TOOMANY:
6184 EMSG2(_(e_toomanyarg), name);
6185 break;
6186 case ERROR_TOOFEW:
6187 EMSG2(_("E119: Not enough arguments for function: %s"),
6188 name);
6189 break;
6190 case ERROR_SCRIPT:
6191 EMSG2(_("E120: Using <SID> not in a script context: %s"),
6192 name);
6193 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194 case ERROR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006195 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196 name);
6197 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006198 }
6199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 name[len] = cc;
6202 if (fname != name && fname != fname_buf)
6203 vim_free(fname);
6204
6205 return ret;
6206}
6207
6208/*********************************************
6209 * Implementation of the built-in functions
6210 */
6211
6212/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006213 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
6215 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006216f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006218 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006222 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006223 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 l = argvars[0].vval.v_list;
6226 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006227 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 }
6229 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006230 EMSG(_(e_listreq));
6231}
6232
6233/*
6234 * "append(lnum, string/list)" function
6235 */
6236 static void
6237f_append(argvars, rettv)
6238 typeval *argvars;
6239 typeval *rettv;
6240{
6241 long lnum;
6242 listvar *l = NULL;
6243 listitem *li = NULL;
6244 typeval *tv;
6245 long added = 0;
6246
6247 rettv->vval.v_number = 1; /* Default: Failed */
6248 lnum = get_tv_lnum(argvars);
6249 if (lnum >= 0
6250 && lnum <= curbuf->b_ml.ml_line_count
6251 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006253 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006255 l = argvars[1].vval.v_list;
6256 if (l == NULL)
6257 return;
6258 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006260 for (;;)
6261 {
6262 if (l == NULL)
6263 tv = &argvars[1]; /* append a string */
6264 else if (li == NULL)
6265 break; /* end of list */
6266 else
6267 tv = &li->li_tv; /* append item from list */
6268 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6269 ++added;
6270 if (l == NULL)
6271 break;
6272 li = li->li_next;
6273 }
6274
6275 appended_lines_mark(lnum, added);
6276 if (curwin->w_cursor.lnum > lnum)
6277 curwin->w_cursor.lnum += added;
6278 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280}
6281
6282/*
6283 * "argc()" function
6284 */
6285/* ARGSUSED */
6286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006287f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006289 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006291 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292}
6293
6294/*
6295 * "argidx()" function
6296 */
6297/* ARGSUSED */
6298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006299f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006301 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006303 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304}
6305
6306/*
6307 * "argv(nr)" function
6308 */
6309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006310f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006312 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 int idx;
6315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006316 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006318 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 rettv->vval.v_string = NULL;
6321 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322}
6323
6324/*
6325 * "browse(save, title, initdir, default)" function
6326 */
6327/* ARGSUSED */
6328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006329f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006331 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333#ifdef FEAT_BROWSE
6334 int save;
6335 char_u *title;
6336 char_u *initdir;
6337 char_u *defname;
6338 char_u buf[NUMBUFLEN];
6339 char_u buf2[NUMBUFLEN];
6340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006341 save = get_tv_number(&argvars[0]);
6342 title = get_tv_string(&argvars[1]);
6343 initdir = get_tv_string_buf(&argvars[2], buf);
6344 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006346 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006347 do_browse(save ? BROWSE_SAVE : 0,
6348 title, defname, NULL, initdir, NULL, curbuf);
6349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006352 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006353}
6354
6355/*
6356 * "browsedir(title, initdir)" function
6357 */
6358/* ARGSUSED */
6359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006360f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006363{
6364#ifdef FEAT_BROWSE
6365 char_u *title;
6366 char_u *initdir;
6367 char_u buf[NUMBUFLEN];
6368
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006369 title = get_tv_string(&argvars[0]);
6370 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006373 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378}
6379
Bram Moolenaar0d660222005-01-07 21:51:51 +00006380static buf_T *find_buffer __ARGS((typeval *avar));
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382/*
6383 * Find a buffer by number or exact name.
6384 */
6385 static buf_T *
6386find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 if (avar->v_type == VAR_NUMBER)
6392 buf = buflist_findnr((int)avar->vval.v_number);
6393 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006396 if (buf == NULL)
6397 {
6398 /* No full path name match, try a match with a URL or a "nofile"
6399 * buffer, these don't use the full path. */
6400 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6401 if (buf->b_fname != NULL
6402 && (path_with_url(buf->b_fname)
6403#ifdef FEAT_QUICKFIX
6404 || bt_nofile(buf)
6405#endif
6406 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006408 break;
6409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 return buf;
6412}
6413
6414/*
6415 * "bufexists(expr)" function
6416 */
6417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006422 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
6424
6425/*
6426 * "buflisted(expr)" function
6427 */
6428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006429f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 buf_T *buf;
6434
6435 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006436 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437}
6438
6439/*
6440 * "bufloaded(expr)" function
6441 */
6442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006443f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006445 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 buf_T *buf;
6448
6449 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006450 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451}
6452
Bram Moolenaar0d660222005-01-07 21:51:51 +00006453static buf_T *get_buf_tv __ARGS((typeval *tv));
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455/*
6456 * Get buffer by number or pattern.
6457 */
6458 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006459get_buf_tv(tv)
6460 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 int save_magic;
6464 char_u *save_cpo;
6465 buf_T *buf;
6466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467 if (tv->v_type == VAR_NUMBER)
6468 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 if (name == NULL || *name == NUL)
6470 return curbuf;
6471 if (name[0] == '$' && name[1] == NUL)
6472 return lastbuf;
6473
6474 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6475 save_magic = p_magic;
6476 p_magic = TRUE;
6477 save_cpo = p_cpo;
6478 p_cpo = (char_u *)"";
6479
6480 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6481 TRUE, FALSE));
6482
6483 p_magic = save_magic;
6484 p_cpo = save_cpo;
6485
6486 /* If not found, try expanding the name, like done for bufexists(). */
6487 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006488 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 return buf;
6491}
6492
6493/*
6494 * "bufname(expr)" function
6495 */
6496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006497f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006499 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
6501 buf_T *buf;
6502
6503 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 buf = get_buf_tv(&argvars[0]);
6505 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 --emsg_off;
6511}
6512
6513/*
6514 * "bufnr(expr)" function
6515 */
6516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006517f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 buf_T *buf;
6522
6523 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006524 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006526 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 --emsg_off;
6530}
6531
6532/*
6533 * "bufwinnr(nr)" function
6534 */
6535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006536f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540#ifdef FEAT_WINDOWS
6541 win_T *wp;
6542 int winnr = 0;
6543#endif
6544 buf_T *buf;
6545
6546 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548#ifdef FEAT_WINDOWS
6549 for (wp = firstwin; wp; wp = wp->w_next)
6550 {
6551 ++winnr;
6552 if (wp->w_buffer == buf)
6553 break;
6554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006555 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006557 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558#endif
6559 --emsg_off;
6560}
6561
6562/*
6563 * "byte2line(byte)" function
6564 */
6565/*ARGSUSED*/
6566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006567f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
6571#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573#else
6574 long boff = 0;
6575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006580 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 (linenr_T)0, &boff);
6582#endif
6583}
6584
6585/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006586 * "byteidx()" function
6587 */
6588/*ARGSUSED*/
6589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006592 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006593{
6594#ifdef FEAT_MBYTE
6595 char_u *t;
6596#endif
6597 char_u *str;
6598 long idx;
6599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 str = get_tv_string(&argvars[0]);
6601 idx = get_tv_number(&argvars[1]);
6602 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006603 if (idx < 0)
6604 return;
6605
6606#ifdef FEAT_MBYTE
6607 t = str;
6608 for ( ; idx > 0; idx--)
6609 {
6610 if (*t == NUL) /* EOL reached */
6611 return;
6612 t += mb_ptr2len_check(t);
6613 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006614 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006615#else
6616 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006617 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006618#endif
6619}
6620
6621/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 * "call(func, arglist)" function
6623 */
6624 static void
6625f_call(argvars, rettv)
6626 typeval *argvars;
6627 typeval *rettv;
6628{
6629 char_u *func;
6630 typeval argv[MAX_FUNC_ARGS];
6631 int argc = 0;
6632 listitem *item;
6633 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 dictvar *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635
6636 rettv->vval.v_number = 0;
6637 if (argvars[1].v_type != VAR_LIST)
6638 {
6639 EMSG(_(e_listreq));
6640 return;
6641 }
6642 if (argvars[1].vval.v_list == NULL)
6643 return;
6644
6645 if (argvars[0].v_type == VAR_FUNC)
6646 func = argvars[0].vval.v_string;
6647 else
6648 func = get_tv_string(&argvars[0]);
6649
Bram Moolenaare9a41262005-01-15 22:18:47 +00006650 if (argvars[2].v_type != VAR_UNKNOWN)
6651 {
6652 if (argvars[2].v_type != VAR_DICT)
6653 {
6654 EMSG(_(e_dictreq));
6655 return;
6656 }
6657 selfdict = argvars[2].vval.v_dict;
6658 }
6659
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006660 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6661 item = item->li_next)
6662 {
6663 if (argc == MAX_FUNC_ARGS)
6664 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006665 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666 break;
6667 }
6668 /* Make a copy of each argument (is this really needed?) */
6669 copy_tv(&item->li_tv, &argv[argc++]);
6670 }
6671
6672 if (item == NULL)
6673 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006674 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6675 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676
6677 /* Free the arguments. */
6678 while (argc > 0)
6679 clear_tv(&argv[--argc]);
6680}
6681
6682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 * "char2nr(string)" function
6684 */
6685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006686f_char2nr(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#ifdef FEAT_MBYTE
6691 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006692 rettv->vval.v_number =
6693 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 else
6695#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006696 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698
6699/*
6700 * "cindent(lnum)" function
6701 */
6702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006705 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707#ifdef FEAT_CINDENT
6708 pos_T pos;
6709 linenr_T lnum;
6710
6711 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006712 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6714 {
6715 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006716 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 curwin->w_cursor = pos;
6718 }
6719 else
6720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006721 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
6724/*
6725 * "col(string)" function
6726 */
6727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006728f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006729 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006730 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 colnr_T col = 0;
6733 pos_T *fp;
6734
6735 fp = var2fpos(&argvars[0], FALSE);
6736 if (fp != NULL)
6737 {
6738 if (fp->col == MAXCOL)
6739 {
6740 /* '> can be MAXCOL, get the length of the line then */
6741 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6742 col = STRLEN(ml_get(fp->lnum)) + 1;
6743 else
6744 col = MAXCOL;
6745 }
6746 else
6747 {
6748 col = fp->col + 1;
6749#ifdef FEAT_VIRTUALEDIT
6750 /* col(".") when the cursor is on the NUL at the end of the line
6751 * because of "coladd" can be seen as an extra column. */
6752 if (virtual_active() && fp == &curwin->w_cursor)
6753 {
6754 char_u *p = ml_get_cursor();
6755
6756 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6757 curwin->w_virtcol - curwin->w_cursor.coladd))
6758 {
6759# ifdef FEAT_MBYTE
6760 int l;
6761
6762 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6763 col += l;
6764# else
6765 if (*p != NUL && p[1] == NUL)
6766 ++col;
6767# endif
6768 }
6769 }
6770#endif
6771 }
6772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006773 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774}
6775
6776/*
6777 * "confirm(message, buttons[, default [, type]])" function
6778 */
6779/*ARGSUSED*/
6780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006781f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006783 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6786 char_u *message;
6787 char_u *buttons = NULL;
6788 char_u buf[NUMBUFLEN];
6789 char_u buf2[NUMBUFLEN];
6790 int def = 1;
6791 int type = VIM_GENERIC;
6792 int c;
6793
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006797 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006800 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
Bram Moolenaara7043832005-01-21 11:56:39 +00006803 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006804 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 switch (TOUPPER_ASC(c))
6806 {
6807 case 'E': type = VIM_ERROR; break;
6808 case 'Q': type = VIM_QUESTION; break;
6809 case 'I': type = VIM_INFO; break;
6810 case 'W': type = VIM_WARNING; break;
6811 case 'G': type = VIM_GENERIC; break;
6812 }
6813 }
6814 }
6815 }
6816
6817 if (buttons == NULL || *buttons == NUL)
6818 buttons = (char_u *)_("&Ok");
6819
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006820 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 def, NULL);
6822#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006823 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824#endif
6825}
6826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827/*
6828 * "copy()" function
6829 */
6830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006831f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
6838/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006839 * "count()" function
6840 */
6841 static void
6842f_count(argvars, rettv)
6843 typeval *argvars;
6844 typeval *rettv;
6845{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006846 long n = 0;
6847 int ic = FALSE;
6848
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006850 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 listitem *li;
6852 listvar *l;
6853 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006854
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855 if ((l = argvars[0].vval.v_list) != NULL)
6856 {
6857 li = l->lv_first;
6858 if (argvars[2].v_type != VAR_UNKNOWN)
6859 {
6860 ic = get_tv_number(&argvars[2]);
6861 if (argvars[3].v_type != VAR_UNKNOWN)
6862 {
6863 idx = get_tv_number(&argvars[3]);
6864 li = list_find(l, idx);
6865 if (li == NULL)
6866 EMSGN(_(e_listidx), idx);
6867 }
6868 }
6869
6870 for ( ; li != NULL; li = li->li_next)
6871 if (tv_equal(&li->li_tv, &argvars[1], ic))
6872 ++n;
6873 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006875 else if (argvars[0].v_type == VAR_DICT)
6876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006877 int todo;
6878 dictvar *d;
6879 hashitem *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006881 if ((d = argvars[0].vval.v_dict) != NULL)
6882 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883 if (argvars[2].v_type != VAR_UNKNOWN)
6884 {
6885 ic = get_tv_number(&argvars[2]);
6886 if (argvars[3].v_type != VAR_UNKNOWN)
6887 EMSG(_(e_invarg));
6888 }
6889
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006890 todo = d->dv_hashtable.ht_used;
6891 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
6892 {
6893 if (!HASHITEM_EMPTY(hi))
6894 {
6895 --todo;
6896 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
6897 ++n;
6898 }
6899 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 }
6901 }
6902 else
6903 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006904 rettv->vval.v_number = n;
6905}
6906
6907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6909 *
6910 * Checks the existence of a cscope connection.
6911 */
6912/*ARGSUSED*/
6913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006915 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006916 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
6918#ifdef FEAT_CSCOPE
6919 int num = 0;
6920 char_u *dbpath = NULL;
6921 char_u *prepend = NULL;
6922 char_u buf[NUMBUFLEN];
6923
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006924 if (argvars[0].v_type != VAR_UNKNOWN
6925 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006927 num = (int)get_tv_number(&argvars[0]);
6928 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006929 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006933 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#endif
6937}
6938
6939/*
6940 * "cursor(lnum, col)" function
6941 *
6942 * Moves the cursor to the specified line and column
6943 */
6944/*ARGSUSED*/
6945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006946f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006947 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006948 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 long line, col;
6951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006952 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (line > 0)
6954 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006955 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (col > 0)
6957 curwin->w_cursor.col = col - 1;
6958#ifdef FEAT_VIRTUALEDIT
6959 curwin->w_cursor.coladd = 0;
6960#endif
6961
6962 /* Make sure the cursor is in a valid position. */
6963 check_cursor();
6964#ifdef FEAT_MBYTE
6965 /* Correct cursor for multi-byte character. */
6966 if (has_mbyte)
6967 mb_adjust_cursor();
6968#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006969
6970 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971}
6972
6973/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006974 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 */
6976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006977f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
6984/*
6985 * "delete()" function
6986 */
6987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006988f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006989 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006995 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996}
6997
6998/*
6999 * "did_filetype()" function
7000 */
7001/*ARGSUSED*/
7002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007004 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007005 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007008 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007010 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#endif
7012}
7013
7014/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007015 * "diff_filler()" function
7016 */
7017/*ARGSUSED*/
7018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007019f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007020 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007021 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007022{
7023#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007024 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007025#endif
7026}
7027
7028/*
7029 * "diff_hlID()" function
7030 */
7031/*ARGSUSED*/
7032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007033f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007034 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007035 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007036{
7037#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007039 static linenr_T prev_lnum = 0;
7040 static int changedtick = 0;
7041 static int fnum = 0;
7042 static int change_start = 0;
7043 static int change_end = 0;
7044 static enum hlf_value hlID = 0;
7045 int filler_lines;
7046 int col;
7047
7048 if (lnum != prev_lnum
7049 || changedtick != curbuf->b_changedtick
7050 || fnum != curbuf->b_fnum)
7051 {
7052 /* New line, buffer, change: need to get the values. */
7053 filler_lines = diff_check(curwin, lnum);
7054 if (filler_lines < 0)
7055 {
7056 if (filler_lines == -1)
7057 {
7058 change_start = MAXCOL;
7059 change_end = -1;
7060 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7061 hlID = HLF_ADD; /* added line */
7062 else
7063 hlID = HLF_CHD; /* changed line */
7064 }
7065 else
7066 hlID = HLF_ADD; /* added line */
7067 }
7068 else
7069 hlID = (enum hlf_value)0;
7070 prev_lnum = lnum;
7071 changedtick = curbuf->b_changedtick;
7072 fnum = curbuf->b_fnum;
7073 }
7074
7075 if (hlID == HLF_CHD || hlID == HLF_TXD)
7076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007077 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007078 if (col >= change_start && col <= change_end)
7079 hlID = HLF_TXD; /* changed text */
7080 else
7081 hlID = HLF_CHD; /* changed line */
7082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007084#endif
7085}
7086
7087/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007088 * "empty({expr})" function
7089 */
7090 static void
7091f_empty(argvars, rettv)
7092 typeval *argvars;
7093 typeval *rettv;
7094{
7095 int n;
7096
7097 switch (argvars[0].v_type)
7098 {
7099 case VAR_STRING:
7100 case VAR_FUNC:
7101 n = argvars[0].vval.v_string == NULL
7102 || *argvars[0].vval.v_string == NUL;
7103 break;
7104 case VAR_NUMBER:
7105 n = argvars[0].vval.v_number == 0;
7106 break;
7107 case VAR_LIST:
7108 n = argvars[0].vval.v_list == NULL
7109 || argvars[0].vval.v_list->lv_first == NULL;
7110 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 case VAR_DICT:
7112 n = argvars[0].vval.v_dict == NULL
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 || argvars[0].vval.v_dict->dv_hashtable.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007115 default:
7116 EMSG2(_(e_intern2), "f_empty()");
7117 n = 0;
7118 }
7119
7120 rettv->vval.v_number = n;
7121}
7122
7123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 * "escape({string}, {chars})" function
7125 */
7126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007127f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007128 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007129 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 char_u buf[NUMBUFLEN];
7132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007133 rettv->vval.v_string =
7134 vim_strsave_escaped(get_tv_string(&argvars[0]),
7135 get_tv_string_buf(&argvars[1], buf));
7136 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137}
7138
7139/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007140 * "eval()" function
7141 */
7142/*ARGSUSED*/
7143 static void
7144f_eval(argvars, rettv)
7145 typeval *argvars;
7146 typeval *rettv;
7147{
7148 char_u *s;
7149
7150 s = get_tv_string(&argvars[0]);
7151 s = skipwhite(s);
7152
7153 if (eval1(&s, rettv, TRUE) == FAIL)
7154 rettv->vval.v_number = 0;
7155 else if (*s != NUL)
7156 EMSG(_(e_trailing));
7157}
7158
7159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 * "eventhandler()" function
7161 */
7162/*ARGSUSED*/
7163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007164f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007165 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007168 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169}
7170
7171/*
7172 * "executable()" function
7173 */
7174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007175f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007176 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007177 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007179 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182/*
7183 * "exists()" function
7184 */
7185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007186f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007187 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 char_u *p;
7191 char_u *name;
7192 int n = FALSE;
7193 int len = 0;
7194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007195 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (*p == '$') /* environment variable */
7197 {
7198 /* first try "normal" environment variables (fast) */
7199 if (mch_getenv(p + 1) != NULL)
7200 n = TRUE;
7201 else
7202 {
7203 /* try expanding things like $VIM and ${HOME} */
7204 p = expand_env_save(p);
7205 if (p != NULL && *p != '$')
7206 n = TRUE;
7207 vim_free(p);
7208 }
7209 }
7210 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 else if (*p == '*') /* internal or user defined function */
7213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007214 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 }
7216 else if (*p == ':')
7217 {
7218 n = cmd_exists(p + 1);
7219 }
7220 else if (*p == '#')
7221 {
7222#ifdef FEAT_AUTOCMD
7223 name = p + 1;
7224 p = vim_strchr(name, '#');
7225 if (p != NULL)
7226 n = au_exists(name, p, p + 1);
7227 else
7228 n = au_exists(name, name + STRLEN(name), NULL);
7229#endif
7230 }
7231 else /* internal variable */
7232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 char_u *expr_start;
7234 char_u *expr_end;
7235 char_u *temp_string = NULL;
7236 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 name = p;
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007240 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (expr_start != NULL)
7242 {
7243 temp_string = make_expanded_name(name, expr_start, expr_end, s);
7244 if (temp_string != NULL)
7245 {
7246 len = STRLEN(temp_string);
7247 name = temp_string;
7248 }
7249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 if (len == 0)
7251 len = get_id_len(&p);
7252 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007253 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007258 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259}
7260
7261/*
7262 * "expand()" function
7263 */
7264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007265f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007266 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 char_u *s;
7270 int len;
7271 char_u *errormsg;
7272 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7273 expand_T xpc;
7274
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007275 rettv->v_type = VAR_STRING;
7276 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (*s == '%' || *s == '#' || *s == '<')
7278 {
7279 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007280 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 --emsg_off;
7282 }
7283 else
7284 {
7285 /* When the optional second argument is non-zero, don't remove matches
7286 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007287 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 flags |= WILD_KEEP_ALL;
7289 ExpandInit(&xpc);
7290 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007291 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 ExpandCleanup(&xpc);
7293 }
7294}
7295
7296/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007297 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007299 */
7300 static void
7301f_extend(argvars, rettv)
7302 typeval *argvars;
7303 typeval *rettv;
7304{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007305 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007307 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 listvar *l1, *l2;
7309 listitem *item;
7310 long before;
7311 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007312
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 l1 = argvars[0].vval.v_list;
7314 l2 = argvars[1].vval.v_list;
7315 if (l1 != NULL && l2 != NULL)
7316 {
7317 if (argvars[2].v_type != VAR_UNKNOWN)
7318 {
7319 n = before = get_tv_number(&argvars[2]);
7320 item = list_find_ext(l1, &n);
7321 if (n != 0)
7322 {
7323 EMSGN(_(e_listidx), before);
7324 return;
7325 }
7326 }
7327 else
7328 item = NULL;
7329 list_extend(l1, l2, item);
7330
7331 ++l1->lv_refcount;
7332 copy_tv(&argvars[0], rettv);
7333 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007334 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7336 {
7337 dictvar *d1, *d2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 dictitem *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 char_u *action;
7340 int i;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 hashitem *hi2;
7342 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343
7344 d1 = argvars[0].vval.v_dict;
7345 d2 = argvars[1].vval.v_dict;
7346 if (d1 != NULL && d2 != NULL)
7347 {
7348 /* Check the third argument. */
7349 if (argvars[2].v_type != VAR_UNKNOWN)
7350 {
7351 static char *(av[]) = {"keep", "force", "error"};
7352
7353 action = get_tv_string(&argvars[2]);
7354 for (i = 0; i < 3; ++i)
7355 if (STRCMP(action, av[i]) == 0)
7356 break;
7357 if (i == 3)
7358 {
7359 EMSGN(_(e_invarg2), action);
7360 return;
7361 }
7362 }
7363 else
7364 action = (char_u *)"force";
7365
7366 /* Go over all entries in the second dict and add them to the
7367 * first dict. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 todo = d2->dv_hashtable.ht_used;
7369 for (hi2 = d2->dv_hashtable.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 --todo;
7374 di1 = dict_find(d1, hi2->hi_key, -1);
7375 if (di1 == NULL)
7376 {
7377 di1 = dictitem_copy(HI2DI(hi2));
7378 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7379 dictitem_free(di1);
7380 }
7381 else if (*action == 'e')
7382 {
7383 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7384 break;
7385 }
7386 else if (*action == 'f')
7387 {
7388 clear_tv(&di1->di_tv);
7389 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 }
7392 }
7393
7394 ++d1->dv_refcount;
7395 copy_tv(&argvars[0], rettv);
7396 }
7397 }
7398 else
7399 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007400}
7401
7402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 * "filereadable()" function
7404 */
7405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007406f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007407 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007408 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 FILE *fd;
7411 char_u *p;
7412 int n;
7413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007414 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7416 {
7417 n = TRUE;
7418 fclose(fd);
7419 }
7420 else
7421 n = FALSE;
7422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426/*
7427 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7428 * rights to write into.
7429 */
7430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007433 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
7435 char_u *p;
7436 int retval = 0;
7437#if defined(UNIX) || defined(VMS)
7438 int perm = 0;
7439#endif
7440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#if defined(UNIX) || defined(VMS)
7443 perm = mch_getperm(p);
7444#endif
7445#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7446 if (
7447# ifdef WIN3264
7448 mch_writable(p) &&
7449# else
7450# if defined(UNIX) || defined(VMS)
7451 (perm & 0222) &&
7452# endif
7453# endif
7454 mch_access((char *)p, W_OK) == 0
7455 )
7456#endif
7457 {
7458 ++retval;
7459 if (mch_isdir(p))
7460 ++retval;
7461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
Bram Moolenaar0d660222005-01-07 21:51:51 +00007465static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007466
7467 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007468findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007469 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007471 int dir;
7472{
7473#ifdef FEAT_SEARCHPATH
7474 char_u *fname;
7475 char_u *fresult = NULL;
7476 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7477 char_u *p;
7478 char_u pathbuf[NUMBUFLEN];
7479 int count = 1;
7480 int first = TRUE;
7481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007483
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007487 if (*p != NUL)
7488 path = p;
7489
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007490 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007492 }
7493
7494 do
7495 {
7496 vim_free(fresult);
7497 fresult = find_file_in_path_option(first ? fname : NULL,
7498 first ? (int)STRLEN(fname) : 0,
7499 0, first, path, dir, NULL);
7500 first = FALSE;
7501 } while (--count > 0 && fresult != NULL);
7502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007503 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007504#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007505 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007506#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007508}
7509
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007510static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511static int filter_map_one __ARGS((typeval *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007512
7513/*
7514 * Implementation of map() and filter().
7515 */
7516 static void
7517filter_map(argvars, rettv, map)
7518 typeval *argvars;
7519 typeval *rettv;
7520 int map;
7521{
7522 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007523 char_u *expr;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007524 listitem *li, *nli;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007525 listvar *l = NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 dictitem *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00007527 hashtable *ht;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528 hashitem *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 dictvar *d = NULL;
7530 typeval save_val;
7531 typeval save_key;
7532 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 int todo;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534
7535 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007536 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007537 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007538 if ((l = argvars[0].vval.v_list) == NULL)
7539 return;
7540 }
7541 else if (argvars[0].v_type == VAR_DICT)
7542 {
7543 if ((d = argvars[0].vval.v_dict) == NULL)
7544 return;
7545 }
7546 else
7547 {
7548 EMSG2(_(e_listdictarg), map ? "map()" : "filter()");
7549 return;
7550 }
7551
7552 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
7553 save_val = vimvars[VV_VAL].tv;
7554
7555 if (argvars[0].v_type == VAR_DICT)
7556 {
7557 save_key = vimvars[VV_KEY].tv;
7558 vimvars[VV_KEY].tv.v_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559
Bram Moolenaara7043832005-01-21 11:56:39 +00007560 ht = &d->dv_hashtable;
7561 hash_lock(ht);
7562 todo = ht->ht_used;
7563 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 --todo;
7568 di = HI2DI(hi);
7569 vimvars[VV_KEY].tv.vval.v_string = vim_strsave(di->di_key);
7570 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
7571 break;
7572 if (!map && rem)
7573 dictitem_remove(d, di);
7574 clear_tv(&vimvars[VV_KEY].tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007575 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007576 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007577 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 clear_tv(&vimvars[VV_KEY].tv);
7580 vimvars[VV_KEY].tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 else
7583 {
7584 for (li = l->lv_first; li != NULL; li = nli)
7585 {
7586 nli = li->li_next;
7587 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7588 break;
7589 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007590 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 }
7592 }
7593
7594 clear_tv(&vimvars[VV_VAL].tv);
7595 vimvars[VV_VAL].tv = save_val;
7596
7597 copy_tv(&argvars[0], rettv);
7598}
7599
7600 static int
7601filter_map_one(tv, expr, map, remp)
7602 typeval *tv;
7603 char_u *expr;
7604 int map;
7605 int *remp;
7606{
7607 typeval rettv;
7608 char_u *s;
7609
7610 copy_tv(tv, &vimvars[VV_VAL].tv);
7611 s = expr;
7612 if (eval1(&s, &rettv, TRUE) == FAIL)
7613 return FAIL;
7614 if (*s != NUL) /* check for trailing chars after expr */
7615 {
7616 EMSG2(_(e_invexpr2), s);
7617 return FAIL;
7618 }
7619 if (map)
7620 {
7621 /* map(): replace the list item value */
7622 clear_tv(tv);
7623 *tv = rettv;
7624 }
7625 else
7626 {
7627 /* filter(): when expr is zero remove the item */
7628 *remp = (get_tv_number(&rettv) == 0);
7629 clear_tv(&rettv);
7630 }
7631 clear_tv(&vimvars[VV_VAL].tv);
7632 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007633}
7634
7635/*
7636 * "filter()" function
7637 */
7638 static void
7639f_filter(argvars, rettv)
7640 typeval *argvars;
7641 typeval *rettv;
7642{
7643 filter_map(argvars, rettv, FALSE);
7644}
7645
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007647 * "finddir({fname}[, {path}[, {count}]])" function
7648 */
7649 static void
7650f_finddir(argvars, rettv)
7651 typeval *argvars;
7652 typeval *rettv;
7653{
7654 findfilendir(argvars, rettv, TRUE);
7655}
7656
7657/*
7658 * "findfile({fname}[, {path}[, {count}]])" function
7659 */
7660 static void
7661f_findfile(argvars, rettv)
7662 typeval *argvars;
7663 typeval *rettv;
7664{
7665 findfilendir(argvars, rettv, FALSE);
7666}
7667
7668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 * "fnamemodify({fname}, {mods})" function
7670 */
7671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007674 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 char_u *fname;
7677 char_u *mods;
7678 int usedlen = 0;
7679 int len;
7680 char_u *fbuf = NULL;
7681 char_u buf[NUMBUFLEN];
7682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 fname = get_tv_string(&argvars[0]);
7684 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 len = (int)STRLEN(fname);
7686
7687 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 vim_free(fbuf);
7695}
7696
Bram Moolenaar0d660222005-01-07 21:51:51 +00007697static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
7699/*
7700 * "foldclosed()" function
7701 */
7702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 int end;
7707{
7708#ifdef FEAT_FOLDING
7709 linenr_T lnum;
7710 linenr_T first, last;
7711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7714 {
7715 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7716 {
7717 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 return;
7722 }
7723 }
7724#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726}
7727
7728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007729 * "foldclosed()" function
7730 */
7731 static void
7732f_foldclosed(argvars, rettv)
7733 typeval *argvars;
7734 typeval *rettv;
7735{
7736 foldclosed_both(argvars, rettv, FALSE);
7737}
7738
7739/*
7740 * "foldclosedend()" function
7741 */
7742 static void
7743f_foldclosedend(argvars, rettv)
7744 typeval *argvars;
7745 typeval *rettv;
7746{
7747 foldclosed_both(argvars, rettv, TRUE);
7748}
7749
7750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 * "foldlevel()" function
7752 */
7753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758#ifdef FEAT_FOLDING
7759 linenr_T lnum;
7760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 else
7765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767}
7768
7769/*
7770 * "foldtext()" function
7771 */
7772/*ARGSUSED*/
7773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778#ifdef FEAT_FOLDING
7779 linenr_T lnum;
7780 char_u *s;
7781 char_u *r;
7782 int len;
7783 char *txt;
7784#endif
7785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 rettv->v_type = VAR_STRING;
7787 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7790 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7791 <= curbuf->b_ml.ml_line_count
7792 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7796 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 {
7798 if (!linewhite(lnum))
7799 break;
7800 ++lnum;
7801 }
7802
7803 /* Find interesting text in this line. */
7804 s = skipwhite(ml_get(lnum));
7805 /* skip C comment-start */
7806 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007809 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007810 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007811 {
7812 s = skipwhite(ml_get(lnum + 1));
7813 if (*s == '*')
7814 s = skipwhite(s + 1);
7815 }
7816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 txt = _("+-%s%3ld lines: ");
7818 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007819 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 + 20 /* for %3ld */
7821 + STRLEN(s))); /* concatenated */
7822 if (r != NULL)
7823 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7825 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7826 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 len = (int)STRLEN(r);
7828 STRCAT(r, s);
7829 /* remove 'foldmarker' and 'commentstring' */
7830 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007831 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 }
7833 }
7834#endif
7835}
7836
7837/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007838 * "foldtextresult(lnum)" function
7839 */
7840/*ARGSUSED*/
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007845{
7846#ifdef FEAT_FOLDING
7847 linenr_T lnum;
7848 char_u *text;
7849 char_u buf[51];
7850 foldinfo_T foldinfo;
7851 int fold_count;
7852#endif
7853
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007854 rettv->v_type = VAR_STRING;
7855 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007856#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007858 fold_count = foldedCount(curwin, lnum, &foldinfo);
7859 if (fold_count > 0)
7860 {
7861 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7862 &foldinfo, buf);
7863 if (text == buf)
7864 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007866 }
7867#endif
7868}
7869
7870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 * "foreground()" function
7872 */
7873/*ARGSUSED*/
7874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#ifdef FEAT_GUI
7881 if (gui.in_use)
7882 gui_mch_set_foreground();
7883#else
7884# ifdef WIN32
7885 win32_set_foreground();
7886# endif
7887#endif
7888}
7889
7890/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 * "function()" function
7892 */
7893/*ARGSUSED*/
7894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898{
7899 char_u *s;
7900
Bram Moolenaara7043832005-01-21 11:56:39 +00007901 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007904 EMSG2(_(e_invarg2), s);
7905 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007906 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007907 else
7908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909 rettv->vval.v_string = vim_strsave(s);
7910 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 }
7912}
7913
7914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007915 * "get()" function
7916 */
7917 static void
7918f_get(argvars, rettv)
7919 typeval *argvars;
7920 typeval *rettv;
7921{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 listitem *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007923 listvar *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007924 dictitem *di;
7925 dictvar *d;
7926 typeval *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927
Bram Moolenaare9a41262005-01-15 22:18:47 +00007928 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007929 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007930 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 li = list_find(l, get_tv_number(&argvars[1]));
7933 if (li != NULL)
7934 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007935 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00007936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007937 else if (argvars[0].v_type == VAR_DICT)
7938 {
7939 if ((d = argvars[0].vval.v_dict) != NULL)
7940 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007941 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007942 if (di != NULL)
7943 tv = &di->di_tv;
7944 }
7945 }
7946 else
7947 EMSG2(_(e_listdictarg), "get()");
7948
7949 if (tv == NULL)
7950 {
7951 if (argvars[2].v_type == VAR_UNKNOWN)
7952 rettv->vval.v_number = 0;
7953 else
7954 copy_tv(&argvars[2], rettv);
7955 }
7956 else
7957 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007958}
7959
7960/*
7961 * "getbufvar()" function
7962 */
7963 static void
7964f_getbufvar(argvars, rettv)
7965 typeval *argvars;
7966 typeval *rettv;
7967{
7968 buf_T *buf;
7969 buf_T *save_curbuf;
7970 char_u *varname;
7971 VAR v;
7972
7973 ++emsg_off;
7974 buf = get_buf_tv(&argvars[0]);
7975 varname = get_tv_string(&argvars[1]);
7976
7977 rettv->v_type = VAR_STRING;
7978 rettv->vval.v_string = NULL;
7979
7980 if (buf != NULL && varname != NULL)
7981 {
7982 if (*varname == '&') /* buffer-local-option */
7983 {
7984 /* set curbuf to be our buf, temporarily */
7985 save_curbuf = curbuf;
7986 curbuf = buf;
7987
7988 get_option_tv(&varname, rettv, TRUE);
7989
7990 /* restore previous notion of curbuf */
7991 curbuf = save_curbuf;
7992 }
7993 else
7994 {
7995 /* look up the variable */
Bram Moolenaara7043832005-01-21 11:56:39 +00007996 v = find_var_in_ht(&buf->b_vars, varname);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007997 if (v != NULL)
7998 copy_tv(&v->tv, rettv);
7999 }
8000 }
8001
8002 --emsg_off;
8003}
8004
8005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 * "getchar()" function
8007 */
8008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008010 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013 varnumber_T n;
8014
8015 ++no_mapping;
8016 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 /* getchar(): blocking wait. */
8019 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 /* getchar(1): only check if char avail */
8022 n = vpeekc();
8023 else if (vpeekc() == NUL)
8024 /* getchar(0) and no char avail: return zero */
8025 n = 0;
8026 else
8027 /* getchar(0) and char avail: return char */
8028 n = safe_vgetc();
8029 --no_mapping;
8030 --allow_keys;
8031
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 if (IS_SPECIAL(n) || mod_mask != 0)
8034 {
8035 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8036 int i = 0;
8037
8038 /* Turn a special key into three bytes, plus modifier. */
8039 if (mod_mask != 0)
8040 {
8041 temp[i++] = K_SPECIAL;
8042 temp[i++] = KS_MODIFIER;
8043 temp[i++] = mod_mask;
8044 }
8045 if (IS_SPECIAL(n))
8046 {
8047 temp[i++] = K_SPECIAL;
8048 temp[i++] = K_SECOND(n);
8049 temp[i++] = K_THIRD(n);
8050 }
8051#ifdef FEAT_MBYTE
8052 else if (has_mbyte)
8053 i += (*mb_char2bytes)(n, temp + i);
8054#endif
8055 else
8056 temp[i++] = n;
8057 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058 rettv->v_type = VAR_STRING;
8059 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
8061}
8062
8063/*
8064 * "getcharmod()" function
8065 */
8066/*ARGSUSED*/
8067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073}
8074
8075/*
8076 * "getcmdline()" function
8077 */
8078/*ARGSUSED*/
8079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008080f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008084 rettv->v_type = VAR_STRING;
8085 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086}
8087
8088/*
8089 * "getcmdpos()" function
8090 */
8091/*ARGSUSED*/
8092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008095 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
8100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 * "getcwd()" function
8102 */
8103/*ARGSUSED*/
8104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008106 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 char_u cwd[MAXPATHL];
8110
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
8115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119#endif
8120 }
8121}
8122
8123/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008124 * "getfontname()" function
8125 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008126/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008129 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008130 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008131{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 rettv->v_type = VAR_STRING;
8133 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008134#ifdef FEAT_GUI
8135 if (gui.in_use)
8136 {
8137 GuiFont font;
8138 char_u *name = NULL;
8139
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008141 {
8142 /* Get the "Normal" font. Either the name saved by
8143 * hl_set_font_name() or from the font ID. */
8144 font = gui.norm_font;
8145 name = hl_get_font_name();
8146 }
8147 else
8148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008150 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8151 return;
8152 font = gui_mch_get_font(name, FALSE);
8153 if (font == NOFONT)
8154 return; /* Invalid font name, return empty string. */
8155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008158 gui_mch_free_font(font);
8159 }
8160#endif
8161}
8162
8163/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008164 * "getfperm({fname})" function
8165 */
8166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008168 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008170{
8171 char_u *fname;
8172 struct stat st;
8173 char_u *perm = NULL;
8174 char_u flags[] = "rwx";
8175 int i;
8176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008177 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008179 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008180 if (mch_stat((char *)fname, &st) >= 0)
8181 {
8182 perm = vim_strsave((char_u *)"---------");
8183 if (perm != NULL)
8184 {
8185 for (i = 0; i < 9; i++)
8186 {
8187 if (st.st_mode & (1 << (8 - i)))
8188 perm[i] = flags[i % 3];
8189 }
8190 }
8191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008193}
8194
8195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 * "getfsize({fname})" function
8197 */
8198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008200 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
8203 char_u *fname;
8204 struct stat st;
8205
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008206 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
8210 if (mch_stat((char *)fname, &st) >= 0)
8211 {
8212 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008213 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
8217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219}
8220
8221/*
8222 * "getftime({fname})" function
8223 */
8224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008226 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 char_u *fname;
8230 struct stat st;
8231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238}
8239
8240/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008241 * "getftype({fname})" function
8242 */
8243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008247{
8248 char_u *fname;
8249 struct stat st;
8250 char_u *type = NULL;
8251 char *t;
8252
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008254
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008256 if (mch_lstat((char *)fname, &st) >= 0)
8257 {
8258#ifdef S_ISREG
8259 if (S_ISREG(st.st_mode))
8260 t = "file";
8261 else if (S_ISDIR(st.st_mode))
8262 t = "dir";
8263# ifdef S_ISLNK
8264 else if (S_ISLNK(st.st_mode))
8265 t = "link";
8266# endif
8267# ifdef S_ISBLK
8268 else if (S_ISBLK(st.st_mode))
8269 t = "bdev";
8270# endif
8271# ifdef S_ISCHR
8272 else if (S_ISCHR(st.st_mode))
8273 t = "cdev";
8274# endif
8275# ifdef S_ISFIFO
8276 else if (S_ISFIFO(st.st_mode))
8277 t = "fifo";
8278# endif
8279# ifdef S_ISSOCK
8280 else if (S_ISSOCK(st.st_mode))
8281 t = "fifo";
8282# endif
8283 else
8284 t = "other";
8285#else
8286# ifdef S_IFMT
8287 switch (st.st_mode & S_IFMT)
8288 {
8289 case S_IFREG: t = "file"; break;
8290 case S_IFDIR: t = "dir"; break;
8291# ifdef S_IFLNK
8292 case S_IFLNK: t = "link"; break;
8293# endif
8294# ifdef S_IFBLK
8295 case S_IFBLK: t = "bdev"; break;
8296# endif
8297# ifdef S_IFCHR
8298 case S_IFCHR: t = "cdev"; break;
8299# endif
8300# ifdef S_IFIFO
8301 case S_IFIFO: t = "fifo"; break;
8302# endif
8303# ifdef S_IFSOCK
8304 case S_IFSOCK: t = "socket"; break;
8305# endif
8306 default: t = "other";
8307 }
8308# else
8309 if (mch_isdir(fname))
8310 t = "dir";
8311 else
8312 t = "file";
8313# endif
8314#endif
8315 type = vim_strsave((char_u *)t);
8316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008318}
8319
8320/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008321 * "getline(lnum)" function
8322 */
8323 static void
8324f_getline(argvars, rettv)
8325 typeval *argvars;
8326 typeval *rettv;
8327{
8328 linenr_T lnum;
8329 linenr_T end;
8330 char_u *p;
8331 listvar *l;
8332 listitem *li;
8333
8334 lnum = get_tv_lnum(argvars);
8335
8336 if (argvars[1].v_type == VAR_UNKNOWN)
8337 {
8338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8339 p = ml_get(lnum);
8340 else
8341 p = (char_u *)"";
8342
8343 rettv->v_type = VAR_STRING;
8344 rettv->vval.v_string = vim_strsave(p);
8345 }
8346 else
8347 {
8348 end = get_tv_lnum(&argvars[1]);
8349 if (end < lnum)
8350 {
8351 EMSG(_(e_invrange));
8352 rettv->vval.v_number = 0;
8353 }
8354 else
8355 {
8356 l = list_alloc();
8357 if (l != NULL)
8358 {
8359 if (lnum < 1)
8360 lnum = 1;
8361 if (end > curbuf->b_ml.ml_line_count)
8362 end = curbuf->b_ml.ml_line_count;
8363 while (lnum <= end)
8364 {
8365 li = listitem_alloc();
8366 if (li == NULL)
8367 break;
8368 list_append(l, li);
8369 li->li_tv.v_type = VAR_STRING;
8370 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8371 }
8372 rettv->vval.v_list = l;
8373 rettv->v_type = VAR_LIST;
8374 ++l->lv_refcount;
8375 }
8376 }
8377 }
8378}
8379
8380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 * "getreg()" function
8382 */
8383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008385 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
8388 char_u *strregname;
8389 int regname;
8390
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008391 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008392 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008394 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 regname = (strregname == NULL ? '"' : *strregname);
8396 if (regname == 0)
8397 regname = '"';
8398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399 rettv->v_type = VAR_STRING;
8400 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401}
8402
8403/*
8404 * "getregtype()" function
8405 */
8406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008407f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008408 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
8411 char_u *strregname;
8412 int regname;
8413 char_u buf[NUMBUFLEN + 2];
8414 long reglen = 0;
8415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008416 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 else
8419 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421
8422 regname = (strregname == NULL ? '"' : *strregname);
8423 if (regname == 0)
8424 regname = '"';
8425
8426 buf[0] = NUL;
8427 buf[1] = NUL;
8428 switch (get_reg_type(regname, &reglen))
8429 {
8430 case MLINE: buf[0] = 'V'; break;
8431 case MCHAR: buf[0] = 'v'; break;
8432#ifdef FEAT_VISUAL
8433 case MBLOCK:
8434 buf[0] = Ctrl_V;
8435 sprintf((char *)buf + 1, "%ld", reglen + 1);
8436 break;
8437#endif
8438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008439 rettv->v_type = VAR_STRING;
8440 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441}
8442
8443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 * "getwinposx()" function
8445 */
8446/*ARGSUSED*/
8447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008449 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#ifdef FEAT_GUI
8454 if (gui.in_use)
8455 {
8456 int x, y;
8457
8458 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008459 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461#endif
8462}
8463
8464/*
8465 * "getwinposy()" function
8466 */
8467/*ARGSUSED*/
8468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008470 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474#ifdef FEAT_GUI
8475 if (gui.in_use)
8476 {
8477 int x, y;
8478
8479 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 }
8482#endif
8483}
8484
8485/*
8486 * "getwinvar()" function
8487 */
8488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008490 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 win_T *win, *oldcurwin;
8494 char_u *varname;
8495 VAR v;
8496
8497 ++emsg_off;
8498 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 rettv->v_type = VAR_STRING;
8502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 if (win != NULL && varname != NULL)
8505 {
8506 if (*varname == '&') /* window-local-option */
8507 {
8508 /* set curwin to be our win, temporarily */
8509 oldcurwin = curwin;
8510 curwin = win;
8511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513
8514 /* restore previous notion of curwin */
8515 curwin = oldcurwin;
8516 }
8517 else
8518 {
8519 /* look up the variable */
Bram Moolenaara7043832005-01-21 11:56:39 +00008520 v = find_var_in_ht(&win->w_vars, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
8524 }
8525
8526 --emsg_off;
8527}
8528
8529/*
8530 * "glob()" function
8531 */
8532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 expand_T xpc;
8538
8539 ExpandInit(&xpc);
8540 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 rettv->v_type = VAR_STRING;
8542 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8544 ExpandCleanup(&xpc);
8545}
8546
8547/*
8548 * "globpath()" function
8549 */
8550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 char_u buf1[NUMBUFLEN];
8556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->v_type = VAR_STRING;
8558 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8559 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561
8562/*
8563 * "has()" function
8564 */
8565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008567 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569{
8570 int i;
8571 char_u *name;
8572 int n = FALSE;
8573 static char *(has_list[]) =
8574 {
8575#ifdef AMIGA
8576 "amiga",
8577# ifdef FEAT_ARP
8578 "arp",
8579# endif
8580#endif
8581#ifdef __BEOS__
8582 "beos",
8583#endif
8584#ifdef MSDOS
8585# ifdef DJGPP
8586 "dos32",
8587# else
8588 "dos16",
8589# endif
8590#endif
8591#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8592 "mac",
8593#endif
8594#if defined(MACOS_X_UNIX)
8595 "macunix",
8596#endif
8597#ifdef OS2
8598 "os2",
8599#endif
8600#ifdef __QNX__
8601 "qnx",
8602#endif
8603#ifdef RISCOS
8604 "riscos",
8605#endif
8606#ifdef UNIX
8607 "unix",
8608#endif
8609#ifdef VMS
8610 "vms",
8611#endif
8612#ifdef WIN16
8613 "win16",
8614#endif
8615#ifdef WIN32
8616 "win32",
8617#endif
8618#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8619 "win32unix",
8620#endif
8621#ifdef WIN64
8622 "win64",
8623#endif
8624#ifdef EBCDIC
8625 "ebcdic",
8626#endif
8627#ifndef CASE_INSENSITIVE_FILENAME
8628 "fname_case",
8629#endif
8630#ifdef FEAT_ARABIC
8631 "arabic",
8632#endif
8633#ifdef FEAT_AUTOCMD
8634 "autocmd",
8635#endif
8636#ifdef FEAT_BEVAL
8637 "balloon_eval",
8638#endif
8639#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8640 "builtin_terms",
8641# ifdef ALL_BUILTIN_TCAPS
8642 "all_builtin_terms",
8643# endif
8644#endif
8645#ifdef FEAT_BYTEOFF
8646 "byte_offset",
8647#endif
8648#ifdef FEAT_CINDENT
8649 "cindent",
8650#endif
8651#ifdef FEAT_CLIENTSERVER
8652 "clientserver",
8653#endif
8654#ifdef FEAT_CLIPBOARD
8655 "clipboard",
8656#endif
8657#ifdef FEAT_CMDL_COMPL
8658 "cmdline_compl",
8659#endif
8660#ifdef FEAT_CMDHIST
8661 "cmdline_hist",
8662#endif
8663#ifdef FEAT_COMMENTS
8664 "comments",
8665#endif
8666#ifdef FEAT_CRYPT
8667 "cryptv",
8668#endif
8669#ifdef FEAT_CSCOPE
8670 "cscope",
8671#endif
8672#ifdef DEBUG
8673 "debug",
8674#endif
8675#ifdef FEAT_CON_DIALOG
8676 "dialog_con",
8677#endif
8678#ifdef FEAT_GUI_DIALOG
8679 "dialog_gui",
8680#endif
8681#ifdef FEAT_DIFF
8682 "diff",
8683#endif
8684#ifdef FEAT_DIGRAPHS
8685 "digraphs",
8686#endif
8687#ifdef FEAT_DND
8688 "dnd",
8689#endif
8690#ifdef FEAT_EMACS_TAGS
8691 "emacs_tags",
8692#endif
8693 "eval", /* always present, of course! */
8694#ifdef FEAT_EX_EXTRA
8695 "ex_extra",
8696#endif
8697#ifdef FEAT_SEARCH_EXTRA
8698 "extra_search",
8699#endif
8700#ifdef FEAT_FKMAP
8701 "farsi",
8702#endif
8703#ifdef FEAT_SEARCHPATH
8704 "file_in_path",
8705#endif
8706#ifdef FEAT_FIND_ID
8707 "find_in_path",
8708#endif
8709#ifdef FEAT_FOLDING
8710 "folding",
8711#endif
8712#ifdef FEAT_FOOTER
8713 "footer",
8714#endif
8715#if !defined(USE_SYSTEM) && defined(UNIX)
8716 "fork",
8717#endif
8718#ifdef FEAT_GETTEXT
8719 "gettext",
8720#endif
8721#ifdef FEAT_GUI
8722 "gui",
8723#endif
8724#ifdef FEAT_GUI_ATHENA
8725# ifdef FEAT_GUI_NEXTAW
8726 "gui_neXtaw",
8727# else
8728 "gui_athena",
8729# endif
8730#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008731#ifdef FEAT_GUI_KDE
8732 "gui_kde",
8733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#ifdef FEAT_GUI_GTK
8735 "gui_gtk",
8736# ifdef HAVE_GTK2
8737 "gui_gtk2",
8738# endif
8739#endif
8740#ifdef FEAT_GUI_MAC
8741 "gui_mac",
8742#endif
8743#ifdef FEAT_GUI_MOTIF
8744 "gui_motif",
8745#endif
8746#ifdef FEAT_GUI_PHOTON
8747 "gui_photon",
8748#endif
8749#ifdef FEAT_GUI_W16
8750 "gui_win16",
8751#endif
8752#ifdef FEAT_GUI_W32
8753 "gui_win32",
8754#endif
8755#ifdef FEAT_HANGULIN
8756 "hangul_input",
8757#endif
8758#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8759 "iconv",
8760#endif
8761#ifdef FEAT_INS_EXPAND
8762 "insert_expand",
8763#endif
8764#ifdef FEAT_JUMPLIST
8765 "jumplist",
8766#endif
8767#ifdef FEAT_KEYMAP
8768 "keymap",
8769#endif
8770#ifdef FEAT_LANGMAP
8771 "langmap",
8772#endif
8773#ifdef FEAT_LIBCALL
8774 "libcall",
8775#endif
8776#ifdef FEAT_LINEBREAK
8777 "linebreak",
8778#endif
8779#ifdef FEAT_LISP
8780 "lispindent",
8781#endif
8782#ifdef FEAT_LISTCMDS
8783 "listcmds",
8784#endif
8785#ifdef FEAT_LOCALMAP
8786 "localmap",
8787#endif
8788#ifdef FEAT_MENU
8789 "menu",
8790#endif
8791#ifdef FEAT_SESSION
8792 "mksession",
8793#endif
8794#ifdef FEAT_MODIFY_FNAME
8795 "modify_fname",
8796#endif
8797#ifdef FEAT_MOUSE
8798 "mouse",
8799#endif
8800#ifdef FEAT_MOUSESHAPE
8801 "mouseshape",
8802#endif
8803#if defined(UNIX) || defined(VMS)
8804# ifdef FEAT_MOUSE_DEC
8805 "mouse_dec",
8806# endif
8807# ifdef FEAT_MOUSE_GPM
8808 "mouse_gpm",
8809# endif
8810# ifdef FEAT_MOUSE_JSB
8811 "mouse_jsbterm",
8812# endif
8813# ifdef FEAT_MOUSE_NET
8814 "mouse_netterm",
8815# endif
8816# ifdef FEAT_MOUSE_PTERM
8817 "mouse_pterm",
8818# endif
8819# ifdef FEAT_MOUSE_XTERM
8820 "mouse_xterm",
8821# endif
8822#endif
8823#ifdef FEAT_MBYTE
8824 "multi_byte",
8825#endif
8826#ifdef FEAT_MBYTE_IME
8827 "multi_byte_ime",
8828#endif
8829#ifdef FEAT_MULTI_LANG
8830 "multi_lang",
8831#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008832#ifdef FEAT_MZSCHEME
8833 "mzscheme",
8834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835#ifdef FEAT_OLE
8836 "ole",
8837#endif
8838#ifdef FEAT_OSFILETYPE
8839 "osfiletype",
8840#endif
8841#ifdef FEAT_PATH_EXTRA
8842 "path_extra",
8843#endif
8844#ifdef FEAT_PERL
8845#ifndef DYNAMIC_PERL
8846 "perl",
8847#endif
8848#endif
8849#ifdef FEAT_PYTHON
8850#ifndef DYNAMIC_PYTHON
8851 "python",
8852#endif
8853#endif
8854#ifdef FEAT_POSTSCRIPT
8855 "postscript",
8856#endif
8857#ifdef FEAT_PRINTER
8858 "printer",
8859#endif
8860#ifdef FEAT_QUICKFIX
8861 "quickfix",
8862#endif
8863#ifdef FEAT_RIGHTLEFT
8864 "rightleft",
8865#endif
8866#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8867 "ruby",
8868#endif
8869#ifdef FEAT_SCROLLBIND
8870 "scrollbind",
8871#endif
8872#ifdef FEAT_CMDL_INFO
8873 "showcmd",
8874 "cmdline_info",
8875#endif
8876#ifdef FEAT_SIGNS
8877 "signs",
8878#endif
8879#ifdef FEAT_SMARTINDENT
8880 "smartindent",
8881#endif
8882#ifdef FEAT_SNIFF
8883 "sniff",
8884#endif
8885#ifdef FEAT_STL_OPT
8886 "statusline",
8887#endif
8888#ifdef FEAT_SUN_WORKSHOP
8889 "sun_workshop",
8890#endif
8891#ifdef FEAT_NETBEANS_INTG
8892 "netbeans_intg",
8893#endif
8894#ifdef FEAT_SYN_HL
8895 "syntax",
8896#endif
8897#if defined(USE_SYSTEM) || !defined(UNIX)
8898 "system",
8899#endif
8900#ifdef FEAT_TAG_BINS
8901 "tag_binary",
8902#endif
8903#ifdef FEAT_TAG_OLDSTATIC
8904 "tag_old_static",
8905#endif
8906#ifdef FEAT_TAG_ANYWHITE
8907 "tag_any_white",
8908#endif
8909#ifdef FEAT_TCL
8910# ifndef DYNAMIC_TCL
8911 "tcl",
8912# endif
8913#endif
8914#ifdef TERMINFO
8915 "terminfo",
8916#endif
8917#ifdef FEAT_TERMRESPONSE
8918 "termresponse",
8919#endif
8920#ifdef FEAT_TEXTOBJ
8921 "textobjects",
8922#endif
8923#ifdef HAVE_TGETENT
8924 "tgetent",
8925#endif
8926#ifdef FEAT_TITLE
8927 "title",
8928#endif
8929#ifdef FEAT_TOOLBAR
8930 "toolbar",
8931#endif
8932#ifdef FEAT_USR_CMDS
8933 "user-commands", /* was accidentally included in 5.4 */
8934 "user_commands",
8935#endif
8936#ifdef FEAT_VIMINFO
8937 "viminfo",
8938#endif
8939#ifdef FEAT_VERTSPLIT
8940 "vertsplit",
8941#endif
8942#ifdef FEAT_VIRTUALEDIT
8943 "virtualedit",
8944#endif
8945#ifdef FEAT_VISUAL
8946 "visual",
8947#endif
8948#ifdef FEAT_VISUALEXTRA
8949 "visualextra",
8950#endif
8951#ifdef FEAT_VREPLACE
8952 "vreplace",
8953#endif
8954#ifdef FEAT_WILDIGN
8955 "wildignore",
8956#endif
8957#ifdef FEAT_WILDMENU
8958 "wildmenu",
8959#endif
8960#ifdef FEAT_WINDOWS
8961 "windows",
8962#endif
8963#ifdef FEAT_WAK
8964 "winaltkeys",
8965#endif
8966#ifdef FEAT_WRITEBACKUP
8967 "writebackup",
8968#endif
8969#ifdef FEAT_XIM
8970 "xim",
8971#endif
8972#ifdef FEAT_XFONTSET
8973 "xfontset",
8974#endif
8975#ifdef USE_XSMP
8976 "xsmp",
8977#endif
8978#ifdef USE_XSMP_INTERACT
8979 "xsmp_interact",
8980#endif
8981#ifdef FEAT_XCLIPBOARD
8982 "xterm_clipboard",
8983#endif
8984#ifdef FEAT_XTERM_SAVE
8985 "xterm_save",
8986#endif
8987#if defined(UNIX) && defined(FEAT_X11)
8988 "X11",
8989#endif
8990 NULL
8991 };
8992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 for (i = 0; has_list[i] != NULL; ++i)
8995 if (STRICMP(name, has_list[i]) == 0)
8996 {
8997 n = TRUE;
8998 break;
8999 }
9000
9001 if (n == FALSE)
9002 {
9003 if (STRNICMP(name, "patch", 5) == 0)
9004 n = has_patch(atoi((char *)name + 5));
9005 else if (STRICMP(name, "vim_starting") == 0)
9006 n = (starting != 0);
9007#ifdef DYNAMIC_TCL
9008 else if (STRICMP(name, "tcl") == 0)
9009 n = tcl_enabled(FALSE);
9010#endif
9011#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9012 else if (STRICMP(name, "iconv") == 0)
9013 n = iconv_enabled(FALSE);
9014#endif
9015#ifdef DYNAMIC_RUBY
9016 else if (STRICMP(name, "ruby") == 0)
9017 n = ruby_enabled(FALSE);
9018#endif
9019#ifdef DYNAMIC_PYTHON
9020 else if (STRICMP(name, "python") == 0)
9021 n = python_enabled(FALSE);
9022#endif
9023#ifdef DYNAMIC_PERL
9024 else if (STRICMP(name, "perl") == 0)
9025 n = perl_enabled(FALSE);
9026#endif
9027#ifdef FEAT_GUI
9028 else if (STRICMP(name, "gui_running") == 0)
9029 n = (gui.in_use || gui.starting);
9030# ifdef FEAT_GUI_W32
9031 else if (STRICMP(name, "gui_win32s") == 0)
9032 n = gui_is_win32s();
9033# endif
9034# ifdef FEAT_BROWSE
9035 else if (STRICMP(name, "browse") == 0)
9036 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9037# endif
9038#endif
9039#ifdef FEAT_SYN_HL
9040 else if (STRICMP(name, "syntax_items") == 0)
9041 n = syntax_present(curbuf);
9042#endif
9043#if defined(WIN3264)
9044 else if (STRICMP(name, "win95") == 0)
9045 n = mch_windows95();
9046#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009047#ifdef FEAT_NETBEANS_INTG
9048 else if (STRICMP(name, "netbeans_enabled") == 0)
9049 n = usingNetbeans;
9050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 }
9052
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054}
9055
9056/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009057 * "has_key()" function
9058 */
9059 static void
9060f_has_key(argvars, rettv)
9061 typeval *argvars;
9062 typeval *rettv;
9063{
9064 rettv->vval.v_number = 0;
9065 if (argvars[0].v_type != VAR_DICT)
9066 {
9067 EMSG(_(e_dictreq));
9068 return;
9069 }
9070 if (argvars[0].vval.v_dict == NULL)
9071 return;
9072
9073 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009074 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009075}
9076
9077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 * "hasmapto()" function
9079 */
9080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009082 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085 char_u *name;
9086 char_u *mode;
9087 char_u buf[NUMBUFLEN];
9088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009090 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 mode = (char_u *)"nvo";
9092 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
9095 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099}
9100
9101/*
9102 * "histadd()" function
9103 */
9104/*ARGSUSED*/
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009107 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110#ifdef FEAT_CMDHIST
9111 int histype;
9112 char_u *str;
9113 char_u buf[NUMBUFLEN];
9114#endif
9115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (check_restricted() || check_secure())
9118 return;
9119#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (histype >= 0)
9122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 if (*str != NUL)
9125 {
9126 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 return;
9129 }
9130 }
9131#endif
9132}
9133
9134/*
9135 * "histdel()" function
9136 */
9137/*ARGSUSED*/
9138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009140 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009141 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142{
9143#ifdef FEAT_CMDHIST
9144 int n;
9145 char_u buf[NUMBUFLEN];
9146
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009147 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9153 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
9155 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9157 get_tv_string_buf(&argvars[1], buf));
9158 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161#endif
9162}
9163
9164/*
9165 * "histget()" function
9166 */
9167/*ARGSUSED*/
9168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
9173#ifdef FEAT_CMDHIST
9174 int type;
9175 int idx;
9176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009178 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 idx = get_history_idx(type);
9180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 idx = (int)get_tv_number(&argvars[1]);
9182 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
9190 * "histnr()" function
9191 */
9192/*ARGSUSED*/
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009195 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 int i;
9199
9200#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (i >= HIST_CMD && i < HIST_COUNT)
9203 i = get_history_idx(i);
9204 else
9205#endif
9206 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208}
9209
9210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 * "highlightID(name)" function
9212 */
9213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009215 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220
9221/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009222 * "highlight_exists()" function
9223 */
9224 static void
9225f_hlexists(argvars, rettv)
9226 typeval *argvars;
9227 typeval *rettv;
9228{
9229 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9230}
9231
9232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 * "hostname()" function
9234 */
9235/*ARGSUSED*/
9236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009238 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 char_u hostname[256];
9242
9243 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 rettv->v_type = VAR_STRING;
9245 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246}
9247
9248/*
9249 * iconv() function
9250 */
9251/*ARGSUSED*/
9252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009254 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257#ifdef FEAT_MBYTE
9258 char_u buf1[NUMBUFLEN];
9259 char_u buf2[NUMBUFLEN];
9260 char_u *from, *to, *str;
9261 vimconv_T vimconv;
9262#endif
9263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009264 rettv->v_type = VAR_STRING;
9265 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
9267#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009268 str = get_tv_string(&argvars[0]);
9269 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9270 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 vimconv.vc_type = CONV_NONE;
9272 convert_setup(&vimconv, from, to);
9273
9274 /* If the encodings are equal, no conversion needed. */
9275 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
9280 convert_setup(&vimconv, NULL, NULL);
9281 vim_free(from);
9282 vim_free(to);
9283#endif
9284}
9285
9286/*
9287 * "indent()" function
9288 */
9289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009291 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
9294 linenr_T lnum;
9295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301}
9302
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009303/*
9304 * "index()" function
9305 */
9306 static void
9307f_index(argvars, rettv)
9308 typeval *argvars;
9309 typeval *rettv;
9310{
9311 listvar *l;
9312 listitem *item;
9313 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009314 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009315 int ic = FALSE;
9316
9317 rettv->vval.v_number = -1;
9318 if (argvars[0].v_type != VAR_LIST)
9319 {
9320 EMSG(_(e_listreq));
9321 return;
9322 }
9323 l = argvars[0].vval.v_list;
9324 if (l != NULL)
9325 {
9326 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009327 {
9328 min_idx = get_tv_number(&argvars[2]);
9329 if (argvars[3].v_type != VAR_UNKNOWN)
9330 ic = get_tv_number(&argvars[3]);
9331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332
9333 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009334 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009335 {
9336 rettv->vval.v_number = idx;
9337 break;
9338 }
9339 }
9340}
9341
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342static int inputsecret_flag = 0;
9343
9344/*
9345 * "input()" function
9346 * Also handles inputsecret() when inputsecret is set.
9347 */
9348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009350 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 char_u *p = NULL;
9355 int c;
9356 char_u buf[NUMBUFLEN];
9357 int cmd_silent_save = cmd_silent;
9358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361#ifdef NO_CONSOLE_INPUT
9362 /* While starting up, there is no place to enter text. */
9363 if (no_console_input())
9364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 return;
9367 }
9368#endif
9369
9370 cmd_silent = FALSE; /* Want to see the prompt. */
9371 if (prompt != NULL)
9372 {
9373 /* Only the part of the message after the last NL is considered as
9374 * prompt for the command line */
9375 p = vim_strrchr(prompt, '\n');
9376 if (p == NULL)
9377 p = prompt;
9378 else
9379 {
9380 ++p;
9381 c = *p;
9382 *p = NUL;
9383 msg_start();
9384 msg_clr_eos();
9385 msg_puts_attr(prompt, echo_attr);
9386 msg_didout = FALSE;
9387 msg_starthere();
9388 *p = c;
9389 }
9390 cmdline_row = msg_row;
9391 }
9392
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009393 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9398
9399 /* since the user typed this, no need to wait for return */
9400 need_wait_return = FALSE;
9401 msg_didout = FALSE;
9402 cmd_silent = cmd_silent_save;
9403}
9404
9405/*
9406 * "inputdialog()" function
9407 */
9408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009410 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
9413#if defined(FEAT_GUI_TEXTDIALOG)
9414 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9415 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9416 {
9417 char_u *message;
9418 char_u buf[NUMBUFLEN];
9419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009421 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 IObuff[IOSIZE - 1] = NUL;
9425 }
9426 else
9427 IObuff[0] = NUL;
9428 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9429 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 else
9432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009433 if (argvars[1].v_type != VAR_UNKNOWN
9434 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_string = vim_strsave(
9436 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 }
9442 else
9443#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
9447static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9448
9449/*
9450 * "inputrestore()" function
9451 */
9452/*ARGSUSED*/
9453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009455 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
9458 if (ga_userinput.ga_len > 0)
9459 {
9460 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9462 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465 else if (p_verbose > 1)
9466 {
9467 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 }
9470}
9471
9472/*
9473 * "inputsave()" function
9474 */
9475/*ARGSUSED*/
9476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009478 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
9481 /* Add an entry to the stack of typehead storage. */
9482 if (ga_grow(&ga_userinput, 1) == OK)
9483 {
9484 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9485 + ga_userinput.ga_len);
9486 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 }
9489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491}
9492
9493/*
9494 * "inputsecret()" function
9495 */
9496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009498 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 ++cmdline_star;
9502 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 --cmdline_star;
9505 --inputsecret_flag;
9506}
9507
9508/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009509 * "insert()" function
9510 */
9511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515{
9516 long before = 0;
9517 long n;
9518 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009519 listvar *l;
9520
9521 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009522 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009523 else if ((l = argvars[0].vval.v_list) != NULL)
9524 {
9525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009528 n = before;
9529 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009530 if (n > 0)
9531 EMSGN(_(e_listidx), before);
9532 else
9533 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009534 list_insert_tv(l, &argvars[1], item);
9535 ++l->lv_refcount;
9536 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009537 }
9538 }
9539}
9540
9541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 * "isdirectory()" function
9543 */
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009546 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550}
9551
Bram Moolenaar8c711452005-01-14 21:53:12 +00009552static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
9553
9554/*
9555 * Turn a dict into a list:
9556 * "what" == 0: list of keys
9557 * "what" == 1: list of values
9558 * "what" == 2: list of items
9559 */
9560 static void
9561dict_list(argvars, rettv, what)
9562 typeval *argvars;
9563 typeval *rettv;
9564 int what;
9565{
9566 listvar *l;
9567 listvar *l2;
9568 dictitem *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009569 hashitem *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009570 listitem *li;
9571 listitem *li2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009572 dictvar *d;
9573 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009574
9575 rettv->vval.v_number = 0;
9576 if (argvars[0].v_type != VAR_DICT)
9577 {
9578 EMSG(_(e_dictreq));
9579 return;
9580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009581 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009582 return;
9583
9584 l = list_alloc();
9585 if (l == NULL)
9586 return;
9587 rettv->v_type = VAR_LIST;
9588 rettv->vval.v_list = l;
9589 ++l->lv_refcount;
9590
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009591 todo = d->dv_hashtable.ht_used;
9592 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009594 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00009595 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009596 --todo;
9597 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009598
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009599 li = listitem_alloc();
9600 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009601 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009602 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009603
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009604 if (what == 0)
9605 {
9606 /* keys() */
9607 li->li_tv.v_type = VAR_STRING;
9608 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9609 }
9610 else if (what == 1)
9611 {
9612 /* values() */
9613 copy_tv(&di->di_tv, &li->li_tv);
9614 }
9615 else
9616 {
9617 /* items() */
9618 l2 = list_alloc();
9619 li->li_tv.v_type = VAR_LIST;
9620 li->li_tv.vval.v_list = l2;
9621 if (l2 == NULL)
9622 break;
9623 ++l2->lv_refcount;
9624
9625 li2 = listitem_alloc();
9626 if (li2 == NULL)
9627 break;
9628 list_append(l2, li2);
9629 li2->li_tv.v_type = VAR_STRING;
9630 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9631
9632 li2 = listitem_alloc();
9633 if (li2 == NULL)
9634 break;
9635 list_append(l2, li2);
9636 copy_tv(&di->di_tv, &li2->li_tv);
9637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00009638 }
9639 }
9640}
9641
9642/*
9643 * "items(dict)" function
9644 */
9645 static void
9646f_items(argvars, rettv)
9647 typeval *argvars;
9648 typeval *rettv;
9649{
9650 dict_list(argvars, rettv, 2);
9651}
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009654 * "join()" function
9655 */
9656 static void
9657f_join(argvars, rettv)
9658 typeval *argvars;
9659 typeval *rettv;
9660{
9661 garray_T ga;
9662 char_u *sep;
9663
9664 rettv->vval.v_number = 0;
9665 if (argvars[0].v_type != VAR_LIST)
9666 {
9667 EMSG(_(e_listreq));
9668 return;
9669 }
9670 if (argvars[0].vval.v_list == NULL)
9671 return;
9672 if (argvars[1].v_type == VAR_UNKNOWN)
9673 sep = (char_u *)" ";
9674 else
9675 sep = get_tv_string(&argvars[1]);
9676
9677 ga_init2(&ga, (int)sizeof(char), 80);
9678 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9679 ga_append(&ga, NUL);
9680
9681 rettv->v_type = VAR_STRING;
9682 rettv->vval.v_string = (char_u *)ga.ga_data;
9683}
9684
9685/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009686 * "keys()" function
9687 */
9688 static void
9689f_keys(argvars, rettv)
9690 typeval *argvars;
9691 typeval *rettv;
9692{
9693 dict_list(argvars, rettv, 0);
9694}
9695
9696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 * "last_buffer_nr()" function.
9698 */
9699/*ARGSUSED*/
9700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
9705 int n = 0;
9706 buf_T *buf;
9707
9708 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9709 if (n < buf->b_fnum)
9710 n = buf->b_fnum;
9711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009713}
9714
9715/*
9716 * "len()" function
9717 */
9718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009720 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009722{
9723 switch (argvars[0].v_type)
9724 {
9725 case VAR_STRING:
9726 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = (varnumber_T)STRLEN(
9728 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009729 break;
9730 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009732 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009733 case VAR_DICT:
9734 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9735 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009736 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009737 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009738 break;
9739 }
9740}
9741
Bram Moolenaar0d660222005-01-07 21:51:51 +00009742static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009743
9744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009746 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009748 int type;
9749{
9750#ifdef FEAT_LIBCALL
9751 char_u *string_in;
9752 char_u **string_result;
9753 int nr_result;
9754#endif
9755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009757 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009761
9762 if (check_restricted() || check_secure())
9763 return;
9764
9765#ifdef FEAT_LIBCALL
9766 /* The first two args must be strings, otherwise its meaningless */
9767 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9768 {
9769 if (argvars[2].v_type == VAR_NUMBER)
9770 string_in = NULL;
9771 else
9772 string_in = argvars[2].vval.v_string;
9773 if (type == VAR_NUMBER)
9774 string_result = NULL;
9775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009777 if (mch_libcall(argvars[0].vval.v_string,
9778 argvars[1].vval.v_string,
9779 string_in,
9780 argvars[2].vval.v_number,
9781 string_result,
9782 &nr_result) == OK
9783 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009785 }
9786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009790 * "libcall()" function
9791 */
9792 static void
9793f_libcall(argvars, rettv)
9794 typeval *argvars;
9795 typeval *rettv;
9796{
9797 libcall_common(argvars, rettv, VAR_STRING);
9798}
9799
9800/*
9801 * "libcallnr()" function
9802 */
9803 static void
9804f_libcallnr(argvars, rettv)
9805 typeval *argvars;
9806 typeval *rettv;
9807{
9808 libcall_common(argvars, rettv, VAR_NUMBER);
9809}
9810
9811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 * "line(string)" function
9813 */
9814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009816 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 linenr_T lnum = 0;
9820 pos_T *fp;
9821
9822 fp = var2fpos(&argvars[0], TRUE);
9823 if (fp != NULL)
9824 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828/*
9829 * "line2byte(lnum)" function
9830 */
9831/*ARGSUSED*/
9832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009834 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839#else
9840 linenr_T lnum;
9841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
9847 if (rettv->vval.v_number >= 0)
9848 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849#endif
9850}
9851
9852/*
9853 * "lispindent(lnum)" function
9854 */
9855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009857 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859{
9860#ifdef FEAT_LISP
9861 pos_T pos;
9862 linenr_T lnum;
9863
9864 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9867 {
9868 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 curwin->w_cursor = pos;
9871 }
9872 else
9873#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875}
9876
9877/*
9878 * "localtime()" function
9879 */
9880/*ARGSUSED*/
9881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009883 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
Bram Moolenaar0d660222005-01-07 21:51:51 +00009889static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009893 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 int exact;
9896{
9897 char_u *keys;
9898 char_u *which;
9899 char_u buf[NUMBUFLEN];
9900 char_u *keys_buf = NULL;
9901 char_u *rhs;
9902 int mode;
9903 garray_T ga;
9904
9905 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906 rettv->v_type = VAR_STRING;
9907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (*keys == NUL)
9911 return;
9912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009913 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 else
9916 which = (char_u *)"";
9917 mode = get_map_mode(&which, 0);
9918
9919 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9920 rhs = check_map(keys, mode, exact);
9921 vim_free(keys_buf);
9922 if (rhs != NULL)
9923 {
9924 ga_init(&ga);
9925 ga.ga_itemsize = 1;
9926 ga.ga_growsize = 40;
9927
9928 while (*rhs != NUL)
9929 ga_concat(&ga, str2special(&rhs, FALSE));
9930
9931 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 }
9934}
9935
9936/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009937 * "map()" function
9938 */
9939 static void
9940f_map(argvars, rettv)
9941 typeval *argvars;
9942 typeval *rettv;
9943{
9944 filter_map(argvars, rettv, TRUE);
9945}
9946
9947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009948 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 */
9950 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009951f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009952 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009955 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956}
9957
9958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009959 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 */
9961 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009962f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009963 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009966 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
9968
Bram Moolenaar0d660222005-01-07 21:51:51 +00009969static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
9971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009973 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 int type;
9976{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009977 char_u *str = NULL;
9978 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 char_u *pat;
9980 regmatch_T regmatch;
9981 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009982 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 char_u *save_cpo;
9984 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009985 long nth = 1;
9986 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009987 listvar *l = NULL;
9988 listitem *li = NULL;
9989 long idx = 0;
9990 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991
9992 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9993 save_cpo = p_cpo;
9994 p_cpo = (char_u *)"";
9995
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 if (type == 2)
9997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->v_type = VAR_STRING;
9999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010004 if (argvars[0].v_type == VAR_LIST)
10005 {
10006 if ((l = argvars[0].vval.v_list) == NULL)
10007 goto theend;
10008 li = l->lv_first;
10009 }
10010 else
10011 expr = str = get_tv_string(&argvars[0]);
10012
10013 pat = get_tv_string_buf(&argvars[1], patbuf);
10014
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010018 if (l != NULL)
10019 {
10020 li = list_find(l, start);
10021 if (li == NULL)
10022 goto theend;
10023 if (start < 0)
10024 {
10025 listitem *ni;
10026
10027 /* Need to compute the index. */
10028 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
10029 ++idx;
10030 }
10031 else
10032 idx = start;
10033 }
10034 else
10035 {
10036 if (start < 0)
10037 start = 0;
10038 if (start > (long)STRLEN(str))
10039 goto theend;
10040 str += start;
10041 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010042
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010043 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 }
10046
10047 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10048 if (regmatch.regprog != NULL)
10049 {
10050 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010051
10052 while (1)
10053 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010054 if (l != NULL)
10055 {
10056 if (li == NULL)
10057 {
10058 match = FALSE;
10059 break;
10060 }
10061 str = echo_string(&li->li_tv, &tofree, strbuf);
10062 }
10063
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010064 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010065
10066 if (l != NULL)
10067 vim_free(tofree);
10068 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010069 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010070 if (l == NULL && !match)
10071 break;
10072
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010073 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010074 if (l != NULL)
10075 {
10076 li = li->li_next;
10077 ++idx;
10078 }
10079 else
10080 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010081#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010082 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010083#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010084 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010085#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010086 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010087 }
10088
10089 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 {
10091 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010092 {
10093 if (l != NULL)
10094 copy_tv(&li->li_tv, rettv);
10095 else
10096 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010098 }
10099 else if (l != NULL)
10100 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 else
10102 {
10103 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 (varnumber_T)(regmatch.startp[0] - str);
10106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 }
10111 }
10112 vim_free(regmatch.regprog);
10113 }
10114
10115theend:
10116 p_cpo = save_cpo;
10117}
10118
10119/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010120 * "match()" function
10121 */
10122 static void
10123f_match(argvars, rettv)
10124 typeval *argvars;
10125 typeval *rettv;
10126{
10127 find_some_match(argvars, rettv, 1);
10128}
10129
10130/*
10131 * "matchend()" function
10132 */
10133 static void
10134f_matchend(argvars, rettv)
10135 typeval *argvars;
10136 typeval *rettv;
10137{
10138 find_some_match(argvars, rettv, 0);
10139}
10140
10141/*
10142 * "matchstr()" function
10143 */
10144 static void
10145f_matchstr(argvars, rettv)
10146 typeval *argvars;
10147 typeval *rettv;
10148{
10149 find_some_match(argvars, rettv, 2);
10150}
10151
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010152static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
10153
10154 static void
10155max_min(argvars, rettv, domax)
10156 typeval *argvars;
10157 typeval *rettv;
10158 int domax;
10159{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010160 long n = 0;
10161 long i;
10162
10163 if (argvars[0].v_type == VAR_LIST)
10164 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010165 listvar *l;
10166 listitem *li;
10167
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010168 l = argvars[0].vval.v_list;
10169 if (l != NULL)
10170 {
10171 li = l->lv_first;
10172 if (li != NULL)
10173 {
10174 n = get_tv_number(&li->li_tv);
10175 while (1)
10176 {
10177 li = li->li_next;
10178 if (li == NULL)
10179 break;
10180 i = get_tv_number(&li->li_tv);
10181 if (domax ? i > n : i < n)
10182 n = i;
10183 }
10184 }
10185 }
10186 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 else if (argvars[0].v_type == VAR_DICT)
10188 {
10189 dictvar *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010190 int first = TRUE;
10191 hashitem *hi;
10192 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010193
10194 d = argvars[0].vval.v_dict;
10195 if (d != NULL)
10196 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010197 todo = d->dv_hashtable.ht_used;
10198 for (hi = d->dv_hashtable.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010202 --todo;
10203 i = get_tv_number(&HI2DI(hi)->di_tv);
10204 if (first)
10205 {
10206 n = i;
10207 first = FALSE;
10208 }
10209 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010210 n = i;
10211 }
10212 }
10213 }
10214 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010215 else
10216 EMSG(_(e_listreq));
10217 rettv->vval.v_number = n;
10218}
10219
10220/*
10221 * "max()" function
10222 */
10223 static void
10224f_max(argvars, rettv)
10225 typeval *argvars;
10226 typeval *rettv;
10227{
10228 max_min(argvars, rettv, TRUE);
10229}
10230
10231/*
10232 * "min()" function
10233 */
10234 static void
10235f_min(argvars, rettv)
10236 typeval *argvars;
10237 typeval *rettv;
10238{
10239 max_min(argvars, rettv, FALSE);
10240}
10241
Bram Moolenaar0d660222005-01-07 21:51:51 +000010242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 * "mode()" function
10244 */
10245/*ARGSUSED*/
10246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010248 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
10251 char_u buf[2];
10252
10253#ifdef FEAT_VISUAL
10254 if (VIsual_active)
10255 {
10256 if (VIsual_select)
10257 buf[0] = VIsual_mode + 's' - 'v';
10258 else
10259 buf[0] = VIsual_mode;
10260 }
10261 else
10262#endif
10263 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10264 buf[0] = 'r';
10265 else if (State & INSERT)
10266 {
10267 if (State & REPLACE_FLAG)
10268 buf[0] = 'R';
10269 else
10270 buf[0] = 'i';
10271 }
10272 else if (State & CMDLINE)
10273 buf[0] = 'c';
10274 else
10275 buf[0] = 'n';
10276
10277 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278 rettv->vval.v_string = vim_strsave(buf);
10279 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280}
10281
10282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010283 * "nextnonblank()" function
10284 */
10285 static void
10286f_nextnonblank(argvars, rettv)
10287 typeval *argvars;
10288 typeval *rettv;
10289{
10290 linenr_T lnum;
10291
10292 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10293 {
10294 if (lnum > curbuf->b_ml.ml_line_count)
10295 {
10296 lnum = 0;
10297 break;
10298 }
10299 if (*skipwhite(ml_get(lnum)) != NUL)
10300 break;
10301 }
10302 rettv->vval.v_number = lnum;
10303}
10304
10305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 * "nr2char()" function
10307 */
10308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010310 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312{
10313 char_u buf[NUMBUFLEN];
10314
10315#ifdef FEAT_MBYTE
10316 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 else
10319#endif
10320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 buf[1] = NUL;
10323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 rettv->v_type = VAR_STRING;
10325 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010326}
10327
10328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329 * "prevnonblank()" function
10330 */
10331 static void
10332f_prevnonblank(argvars, rettv)
10333 typeval *argvars;
10334 typeval *rettv;
10335{
10336 linenr_T lnum;
10337
10338 lnum = get_tv_lnum(argvars);
10339 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10340 lnum = 0;
10341 else
10342 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10343 --lnum;
10344 rettv->vval.v_number = lnum;
10345}
10346
Bram Moolenaar8c711452005-01-14 21:53:12 +000010347/*
10348 * "range()" function
10349 */
10350 static void
10351f_range(argvars, rettv)
10352 typeval *argvars;
10353 typeval *rettv;
10354{
10355 long start;
10356 long end;
10357 long stride = 1;
10358 long i;
10359 listvar *l;
10360 listitem *li;
10361
10362 start = get_tv_number(&argvars[0]);
10363 if (argvars[1].v_type == VAR_UNKNOWN)
10364 {
10365 end = start - 1;
10366 start = 0;
10367 }
10368 else
10369 {
10370 end = get_tv_number(&argvars[1]);
10371 if (argvars[2].v_type != VAR_UNKNOWN)
10372 stride = get_tv_number(&argvars[2]);
10373 }
10374
10375 rettv->vval.v_number = 0;
10376 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010377 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010378 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010379 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010380 else
10381 {
10382 l = list_alloc();
10383 if (l != NULL)
10384 {
10385 rettv->v_type = VAR_LIST;
10386 rettv->vval.v_list = l;
10387 ++l->lv_refcount;
10388
10389 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10390 {
10391 li = listitem_alloc();
10392 if (li == NULL)
10393 break;
10394 li->li_tv.v_type = VAR_NUMBER;
10395 li->li_tv.vval.v_number = i;
10396 list_append(l, li);
10397 }
10398 }
10399 }
10400}
10401
Bram Moolenaar0d660222005-01-07 21:51:51 +000010402#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10403static void make_connection __ARGS((void));
10404static int check_connection __ARGS((void));
10405
10406 static void
10407make_connection()
10408{
10409 if (X_DISPLAY == NULL
10410# ifdef FEAT_GUI
10411 && !gui.in_use
10412# endif
10413 )
10414 {
10415 x_force_connect = TRUE;
10416 setup_term_clip();
10417 x_force_connect = FALSE;
10418 }
10419}
10420
10421 static int
10422check_connection()
10423{
10424 make_connection();
10425 if (X_DISPLAY == NULL)
10426 {
10427 EMSG(_("E240: No connection to Vim server"));
10428 return FAIL;
10429 }
10430 return OK;
10431}
10432#endif
10433
10434#ifdef FEAT_CLIENTSERVER
10435static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
10436
10437 static void
10438remote_common(argvars, rettv, expr)
10439 typeval *argvars;
10440 typeval *rettv;
10441 int expr;
10442{
10443 char_u *server_name;
10444 char_u *keys;
10445 char_u *r = NULL;
10446 char_u buf[NUMBUFLEN];
10447# ifdef WIN32
10448 HWND w;
10449# else
10450 Window w;
10451# endif
10452
10453 if (check_restricted() || check_secure())
10454 return;
10455
10456# ifdef FEAT_X11
10457 if (check_connection() == FAIL)
10458 return;
10459# endif
10460
10461 server_name = get_tv_string(&argvars[0]);
10462 keys = get_tv_string_buf(&argvars[1], buf);
10463# ifdef WIN32
10464 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10465# else
10466 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10467 < 0)
10468# endif
10469 {
10470 if (r != NULL)
10471 EMSG(r); /* sending worked but evaluation failed */
10472 else
10473 EMSG2(_("E241: Unable to send to %s"), server_name);
10474 return;
10475 }
10476
10477 rettv->vval.v_string = r;
10478
10479 if (argvars[2].v_type != VAR_UNKNOWN)
10480 {
10481 var v;
10482 char_u str[30];
10483
10484 sprintf((char *)str, "0x%x", (unsigned int)w);
10485 v.tv.v_type = VAR_STRING;
10486 v.tv.vval.v_string = vim_strsave(str);
10487 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
10488 vim_free(v.tv.vval.v_string);
10489 }
10490}
10491#endif
10492
10493/*
10494 * "remote_expr()" function
10495 */
10496/*ARGSUSED*/
10497 static void
10498f_remote_expr(argvars, rettv)
10499 typeval *argvars;
10500 typeval *rettv;
10501{
10502 rettv->v_type = VAR_STRING;
10503 rettv->vval.v_string = NULL;
10504#ifdef FEAT_CLIENTSERVER
10505 remote_common(argvars, rettv, TRUE);
10506#endif
10507}
10508
10509/*
10510 * "remote_foreground()" function
10511 */
10512/*ARGSUSED*/
10513 static void
10514f_remote_foreground(argvars, rettv)
10515 typeval *argvars;
10516 typeval *rettv;
10517{
10518 rettv->vval.v_number = 0;
10519#ifdef FEAT_CLIENTSERVER
10520# ifdef WIN32
10521 /* On Win32 it's done in this application. */
10522 serverForeground(get_tv_string(&argvars[0]));
10523# else
10524 /* Send a foreground() expression to the server. */
10525 argvars[1].v_type = VAR_STRING;
10526 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10527 argvars[2].v_type = VAR_UNKNOWN;
10528 remote_common(argvars, rettv, TRUE);
10529 vim_free(argvars[1].vval.v_string);
10530# endif
10531#endif
10532}
10533
10534/*ARGSUSED*/
10535 static void
10536f_remote_peek(argvars, rettv)
10537 typeval *argvars;
10538 typeval *rettv;
10539{
10540#ifdef FEAT_CLIENTSERVER
10541 var v;
10542 char_u *s = NULL;
10543# ifdef WIN32
10544 int n = 0;
10545# endif
10546
10547 if (check_restricted() || check_secure())
10548 {
10549 rettv->vval.v_number = -1;
10550 return;
10551 }
10552# ifdef WIN32
10553 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10554 if (n == 0)
10555 rettv->vval.v_number = -1;
10556 else
10557 {
10558 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10559 rettv->vval.v_number = (s != NULL);
10560 }
10561# else
10562 rettv->vval.v_number = 0;
10563 if (check_connection() == FAIL)
10564 return;
10565
10566 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10567 serverStrToWin(get_tv_string(&argvars[0])), &s);
10568# endif
10569
10570 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10571 {
10572 v.tv.v_type = VAR_STRING;
10573 v.tv.vval.v_string = vim_strsave(s);
10574 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
10575 vim_free(v.tv.vval.v_string);
10576 }
10577#else
10578 rettv->vval.v_number = -1;
10579#endif
10580}
10581
10582/*ARGSUSED*/
10583 static void
10584f_remote_read(argvars, rettv)
10585 typeval *argvars;
10586 typeval *rettv;
10587{
10588 char_u *r = NULL;
10589
10590#ifdef FEAT_CLIENTSERVER
10591 if (!check_restricted() && !check_secure())
10592 {
10593# ifdef WIN32
10594 /* The server's HWND is encoded in the 'id' parameter */
10595 int n = 0;
10596
10597 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10598 if (n != 0)
10599 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10600 if (r == NULL)
10601# else
10602 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10603 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10604# endif
10605 EMSG(_("E277: Unable to read a server reply"));
10606 }
10607#endif
10608 rettv->v_type = VAR_STRING;
10609 rettv->vval.v_string = r;
10610}
10611
10612/*
10613 * "remote_send()" function
10614 */
10615/*ARGSUSED*/
10616 static void
10617f_remote_send(argvars, rettv)
10618 typeval *argvars;
10619 typeval *rettv;
10620{
10621 rettv->v_type = VAR_STRING;
10622 rettv->vval.v_string = NULL;
10623#ifdef FEAT_CLIENTSERVER
10624 remote_common(argvars, rettv, FALSE);
10625#endif
10626}
10627
10628/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010629 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010630 */
10631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010633 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010635{
10636 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010637 listitem *item, *item2;
10638 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010639 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010640 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010641 char_u *key;
10642 dictvar *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010643 dictitem *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010644
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010645 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010646 if (argvars[0].v_type == VAR_DICT)
10647 {
10648 if (argvars[2].v_type != VAR_UNKNOWN)
10649 EMSG2(_(e_toomanyarg), "remove()");
10650 else if ((d = argvars[0].vval.v_dict) != NULL)
10651 {
10652 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010653 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010654 if (di == NULL)
10655 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010656 else
10657 {
10658 *rettv = di->di_tv;
10659 init_tv(&di->di_tv);
10660 dictitem_remove(d, di);
10661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010662 }
10663 }
10664 else if (argvars[0].v_type != VAR_LIST)
10665 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010666 else if ((l = argvars[0].vval.v_list) != NULL)
10667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010669 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010670 if (item == NULL)
10671 EMSGN(_(e_listidx), idx);
10672 else
10673 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010674 if (argvars[2].v_type == VAR_UNKNOWN)
10675 {
10676 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010677 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010678 *rettv = item->li_tv;
10679 vim_free(item);
10680 }
10681 else
10682 {
10683 /* Remove range of items, return list with values. */
10684 end = get_tv_number(&argvars[2]);
10685 item2 = list_find(l, end);
10686 if (item2 == NULL)
10687 EMSGN(_(e_listidx), end);
10688 else
10689 {
10690 for (li = item; li != item2 && li != NULL; li = li->li_next)
10691 ;
10692 if (li == NULL) /* didn't find "item2" after "item" */
10693 EMSG(_(e_invrange));
10694 else
10695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010696 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010697 l = list_alloc();
10698 if (l != NULL)
10699 {
10700 rettv->v_type = VAR_LIST;
10701 rettv->vval.v_list = l;
10702 l->lv_first = item;
10703 l->lv_last = item2;
10704 l->lv_refcount = 1;
10705 item->li_prev = NULL;
10706 item2->li_next = NULL;
10707 }
10708 }
10709 }
10710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010711 }
10712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713}
10714
10715/*
10716 * "rename({from}, {to})" function
10717 */
10718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010720 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
10723 char_u buf[NUMBUFLEN];
10724
10725 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10729 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730}
10731
10732/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010733 * "repeat()" function
10734 */
10735/*ARGSUSED*/
10736 static void
10737f_repeat(argvars, rettv)
10738 typeval *argvars;
10739 typeval *rettv;
10740{
10741 char_u *p;
10742 int n;
10743 int slen;
10744 int len;
10745 char_u *r;
10746 int i;
10747 listvar *l;
10748
10749 n = get_tv_number(&argvars[1]);
10750 if (argvars[0].v_type == VAR_LIST)
10751 {
10752 l = list_alloc();
10753 if (l != NULL && argvars[0].vval.v_list != NULL)
10754 {
10755 l->lv_refcount = 1;
10756 while (n-- > 0)
10757 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
10758 break;
10759 }
10760 rettv->v_type = VAR_LIST;
10761 rettv->vval.v_list = l;
10762 }
10763 else
10764 {
10765 p = get_tv_string(&argvars[0]);
10766 rettv->v_type = VAR_STRING;
10767 rettv->vval.v_string = NULL;
10768
10769 slen = (int)STRLEN(p);
10770 len = slen * n;
10771 if (len <= 0)
10772 return;
10773
10774 r = alloc(len + 1);
10775 if (r != NULL)
10776 {
10777 for (i = 0; i < n; i++)
10778 mch_memmove(r + i * slen, p, (size_t)slen);
10779 r[len] = NUL;
10780 }
10781
10782 rettv->vval.v_string = r;
10783 }
10784}
10785
10786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 * "resolve()" function
10788 */
10789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010791 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793{
10794 char_u *p;
10795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797#ifdef FEAT_SHORTCUT
10798 {
10799 char_u *v = NULL;
10800
10801 v = mch_resolve_shortcut(p);
10802 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 }
10807#else
10808# ifdef HAVE_READLINK
10809 {
10810 char_u buf[MAXPATHL + 1];
10811 char_u *cpy;
10812 int len;
10813 char_u *remain = NULL;
10814 char_u *q;
10815 int is_relative_to_current = FALSE;
10816 int has_trailing_pathsep = FALSE;
10817 int limit = 100;
10818
10819 p = vim_strsave(p);
10820
10821 if (p[0] == '.' && (vim_ispathsep(p[1])
10822 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10823 is_relative_to_current = TRUE;
10824
10825 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010826 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 has_trailing_pathsep = TRUE;
10828
10829 q = getnextcomp(p);
10830 if (*q != NUL)
10831 {
10832 /* Separate the first path component in "p", and keep the
10833 * remainder (beginning with the path separator). */
10834 remain = vim_strsave(q - 1);
10835 q[-1] = NUL;
10836 }
10837
10838 for (;;)
10839 {
10840 for (;;)
10841 {
10842 len = readlink((char *)p, (char *)buf, MAXPATHL);
10843 if (len <= 0)
10844 break;
10845 buf[len] = NUL;
10846
10847 if (limit-- == 0)
10848 {
10849 vim_free(p);
10850 vim_free(remain);
10851 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 goto fail;
10854 }
10855
10856 /* Ensure that the result will have a trailing path separator
10857 * if the argument has one. */
10858 if (remain == NULL && has_trailing_pathsep)
10859 add_pathsep(buf);
10860
10861 /* Separate the first path component in the link value and
10862 * concatenate the remainders. */
10863 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10864 if (*q != NUL)
10865 {
10866 if (remain == NULL)
10867 remain = vim_strsave(q - 1);
10868 else
10869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010870 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 if (cpy != NULL)
10872 {
10873 STRCAT(cpy, remain);
10874 vim_free(remain);
10875 remain = cpy;
10876 }
10877 }
10878 q[-1] = NUL;
10879 }
10880
10881 q = gettail(p);
10882 if (q > p && *q == NUL)
10883 {
10884 /* Ignore trailing path separator. */
10885 q[-1] = NUL;
10886 q = gettail(p);
10887 }
10888 if (q > p && !mch_isFullName(buf))
10889 {
10890 /* symlink is relative to directory of argument */
10891 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10892 if (cpy != NULL)
10893 {
10894 STRCPY(cpy, p);
10895 STRCPY(gettail(cpy), buf);
10896 vim_free(p);
10897 p = cpy;
10898 }
10899 }
10900 else
10901 {
10902 vim_free(p);
10903 p = vim_strsave(buf);
10904 }
10905 }
10906
10907 if (remain == NULL)
10908 break;
10909
10910 /* Append the first path component of "remain" to "p". */
10911 q = getnextcomp(remain + 1);
10912 len = q - remain - (*q != NUL);
10913 cpy = vim_strnsave(p, STRLEN(p) + len);
10914 if (cpy != NULL)
10915 {
10916 STRNCAT(cpy, remain, len);
10917 vim_free(p);
10918 p = cpy;
10919 }
10920 /* Shorten "remain". */
10921 if (*q != NUL)
10922 STRCPY(remain, q - 1);
10923 else
10924 {
10925 vim_free(remain);
10926 remain = NULL;
10927 }
10928 }
10929
10930 /* If the result is a relative path name, make it explicitly relative to
10931 * the current directory if and only if the argument had this form. */
10932 if (!vim_ispathsep(*p))
10933 {
10934 if (is_relative_to_current
10935 && *p != NUL
10936 && !(p[0] == '.'
10937 && (p[1] == NUL
10938 || vim_ispathsep(p[1])
10939 || (p[1] == '.'
10940 && (p[2] == NUL
10941 || vim_ispathsep(p[2]))))))
10942 {
10943 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010944 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 if (cpy != NULL)
10946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 vim_free(p);
10948 p = cpy;
10949 }
10950 }
10951 else if (!is_relative_to_current)
10952 {
10953 /* Strip leading "./". */
10954 q = p;
10955 while (q[0] == '.' && vim_ispathsep(q[1]))
10956 q += 2;
10957 if (q > p)
10958 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10959 }
10960 }
10961
10962 /* Ensure that the result will have no trailing path separator
10963 * if the argument had none. But keep "/" or "//". */
10964 if (!has_trailing_pathsep)
10965 {
10966 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010967 if (after_pathsep(p, q))
10968 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 }
10970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 }
10973# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975# endif
10976#endif
10977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
10980#ifdef HAVE_READLINK
10981fail:
10982#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984}
10985
10986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 */
10989 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010991 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 listvar *l;
10995 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996
Bram Moolenaar0d660222005-01-07 21:51:51 +000010997 rettv->vval.v_number = 0;
10998 if (argvars[0].v_type != VAR_LIST)
10999 EMSG2(_(e_listarg), "reverse()");
11000 else if ((l = argvars[0].vval.v_list) != NULL)
11001 {
11002 li = l->lv_last;
11003 l->lv_first = l->lv_last = li;
11004 while (li != NULL)
11005 {
11006 ni = li->li_prev;
11007 list_append(l, li);
11008 li = ni;
11009 }
11010 rettv->vval.v_list = l;
11011 rettv->v_type = VAR_LIST;
11012 ++l->lv_refcount;
11013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014}
11015
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011016#define SP_NOMOVE 1 /* don't move cursor */
11017#define SP_REPEAT 2 /* repeat to find outer pair */
11018#define SP_RETCOUNT 4 /* return matchcount */
11019
Bram Moolenaar0d660222005-01-07 21:51:51 +000011020static int get_search_arg __ARGS((typeval *varp, int *flagsp));
11021
11022/*
11023 * Get flags for a search function.
11024 * Possibly sets "p_ws".
11025 * Returns BACKWARD, FORWARD or zero (for an error).
11026 */
11027 static int
11028get_search_arg(varp, flagsp)
11029 typeval *varp;
11030 int *flagsp;
11031{
11032 int dir = FORWARD;
11033 char_u *flags;
11034 char_u nbuf[NUMBUFLEN];
11035 int mask;
11036
11037 if (varp->v_type != VAR_UNKNOWN)
11038 {
11039 flags = get_tv_string_buf(varp, nbuf);
11040 while (*flags != NUL)
11041 {
11042 switch (*flags)
11043 {
11044 case 'b': dir = BACKWARD; break;
11045 case 'w': p_ws = TRUE; break;
11046 case 'W': p_ws = FALSE; break;
11047 default: mask = 0;
11048 if (flagsp != NULL)
11049 switch (*flags)
11050 {
11051 case 'n': mask = SP_NOMOVE; break;
11052 case 'r': mask = SP_REPEAT; break;
11053 case 'm': mask = SP_RETCOUNT; break;
11054 }
11055 if (mask == 0)
11056 {
11057 EMSG2(_(e_invarg2), flags);
11058 dir = 0;
11059 }
11060 else
11061 *flagsp |= mask;
11062 }
11063 if (dir == 0)
11064 break;
11065 ++flags;
11066 }
11067 }
11068 return dir;
11069}
11070
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071/*
11072 * "search()" function
11073 */
11074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u *pat;
11080 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011081 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 int save_p_ws = p_ws;
11083 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011084 int flags = 0;
11085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011089 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11090 if (dir == 0)
11091 goto theend;
11092 if ((flags & ~SP_NOMOVE) != 0)
11093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011095 goto theend;
11096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011098 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11100 SEARCH_KEEP, RE_SEARCH) != FAIL)
11101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 curwin->w_cursor = pos;
11104 /* "/$" will put the cursor after the end of the line, may need to
11105 * correct that here */
11106 check_cursor();
11107 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011108
11109 /* If 'n' flag is used: restore cursor position. */
11110 if (flags & SP_NOMOVE)
11111 curwin->w_cursor = save_cursor;
11112theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 p_ws = save_p_ws;
11114}
11115
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116/*
11117 * "searchpair()" function
11118 */
11119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011121 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123{
11124 char_u *spat, *mpat, *epat;
11125 char_u *skip;
11126 char_u *pat, *pat2, *pat3;
11127 pos_T pos;
11128 pos_T firstpos;
11129 pos_T save_cursor;
11130 pos_T save_pos;
11131 int save_p_ws = p_ws;
11132 char_u *save_cpo;
11133 int dir;
11134 int flags = 0;
11135 char_u nbuf1[NUMBUFLEN];
11136 char_u nbuf2[NUMBUFLEN];
11137 char_u nbuf3[NUMBUFLEN];
11138 int n;
11139 int r;
11140 int nest = 1;
11141 int err;
11142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144
11145 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11146 save_cpo = p_cpo;
11147 p_cpo = (char_u *)"";
11148
11149 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 spat = get_tv_string(&argvars[0]);
11151 mpat = get_tv_string_buf(&argvars[1], nbuf1);
11152 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
11154 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11155 * start/middle/end (pat3, for the top pair). */
11156 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
11157 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
11158 if (pat2 == NULL || pat3 == NULL)
11159 goto theend;
11160 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
11161 if (*mpat == NUL)
11162 STRCPY(pat3, pat2);
11163 else
11164 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11165 spat, epat, mpat);
11166
11167 /* Handle the optional fourth argument: flags */
11168 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011169 if (dir == 0)
11170 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171
11172 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173 if (argvars[3].v_type == VAR_UNKNOWN
11174 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 skip = (char_u *)"";
11176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178
11179 save_cursor = curwin->w_cursor;
11180 pos = curwin->w_cursor;
11181 firstpos.lnum = 0;
11182 pat = pat3;
11183 for (;;)
11184 {
11185 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11186 SEARCH_KEEP, RE_SEARCH);
11187 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11188 /* didn't find it or found the first match again: FAIL */
11189 break;
11190
11191 if (firstpos.lnum == 0)
11192 firstpos = pos;
11193
11194 /* If the skip pattern matches, ignore this match. */
11195 if (*skip != NUL)
11196 {
11197 save_pos = curwin->w_cursor;
11198 curwin->w_cursor = pos;
11199 r = eval_to_bool(skip, &err, NULL, FALSE);
11200 curwin->w_cursor = save_pos;
11201 if (err)
11202 {
11203 /* Evaluating {skip} caused an error, break here. */
11204 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 break;
11207 }
11208 if (r)
11209 continue;
11210 }
11211
11212 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11213 {
11214 /* Found end when searching backwards or start when searching
11215 * forward: nested pair. */
11216 ++nest;
11217 pat = pat2; /* nested, don't search for middle */
11218 }
11219 else
11220 {
11221 /* Found end when searching forward or start when searching
11222 * backward: end of (nested) pair; or found middle in outer pair. */
11223 if (--nest == 1)
11224 pat = pat3; /* outer level, search for middle */
11225 }
11226
11227 if (nest == 0)
11228 {
11229 /* Found the match: return matchcount or line number. */
11230 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 curwin->w_cursor = pos;
11235 if (!(flags & SP_REPEAT))
11236 break;
11237 nest = 1; /* search for next unmatched */
11238 }
11239 }
11240
11241 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 curwin->w_cursor = save_cursor;
11244
11245theend:
11246 vim_free(pat2);
11247 vim_free(pat3);
11248 p_ws = save_p_ws;
11249 p_cpo = save_cpo;
11250}
11251
Bram Moolenaar0d660222005-01-07 21:51:51 +000011252/*ARGSUSED*/
11253 static void
11254f_server2client(argvars, rettv)
11255 typeval *argvars;
11256 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011258#ifdef FEAT_CLIENTSERVER
11259 char_u buf[NUMBUFLEN];
11260 char_u *server = get_tv_string(&argvars[0]);
11261 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262
Bram Moolenaar0d660222005-01-07 21:51:51 +000011263 rettv->vval.v_number = -1;
11264 if (check_restricted() || check_secure())
11265 return;
11266# ifdef FEAT_X11
11267 if (check_connection() == FAIL)
11268 return;
11269# endif
11270
11271 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011273 EMSG(_("E258: Unable to send to client"));
11274 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011276 rettv->vval.v_number = 0;
11277#else
11278 rettv->vval.v_number = -1;
11279#endif
11280}
11281
11282/*ARGSUSED*/
11283 static void
11284f_serverlist(argvars, rettv)
11285 typeval *argvars;
11286 typeval *rettv;
11287{
11288 char_u *r = NULL;
11289
11290#ifdef FEAT_CLIENTSERVER
11291# ifdef WIN32
11292 r = serverGetVimNames();
11293# else
11294 make_connection();
11295 if (X_DISPLAY != NULL)
11296 r = serverGetVimNames(X_DISPLAY);
11297# endif
11298#endif
11299 rettv->v_type = VAR_STRING;
11300 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301}
11302
11303/*
11304 * "setbufvar()" function
11305 */
11306/*ARGSUSED*/
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011309 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
11312 buf_T *buf;
11313#ifdef FEAT_AUTOCMD
11314 aco_save_T aco;
11315#else
11316 buf_T *save_curbuf;
11317#endif
11318 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011319 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 char_u nbuf[NUMBUFLEN];
11321
11322 if (check_restricted() || check_secure())
11323 return;
11324 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 buf = get_buf_tv(&argvars[0]);
11326 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 varp = &argvars[2];
11328
11329 if (buf != NULL && varname != NULL && varp != NULL)
11330 {
11331 /* set curbuf to be our buf, temporarily */
11332#ifdef FEAT_AUTOCMD
11333 aucmd_prepbuf(&aco, buf);
11334#else
11335 save_curbuf = curbuf;
11336 curbuf = buf;
11337#endif
11338
11339 if (*varname == '&')
11340 {
11341 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 set_option_value(varname, get_tv_number(varp),
11343 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 }
11345 else
11346 {
11347 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11348 if (bufvarname != NULL)
11349 {
11350 STRCPY(bufvarname, "b:");
11351 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011352 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 vim_free(bufvarname);
11354 }
11355 }
11356
11357 /* reset notion of buffer */
11358#ifdef FEAT_AUTOCMD
11359 aucmd_restbuf(&aco);
11360#else
11361 curbuf = save_curbuf;
11362#endif
11363 }
11364 --emsg_off;
11365}
11366
11367/*
11368 * "setcmdpos()" function
11369 */
11370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011372 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->vval.v_number = set_cmdline_pos(
11376 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377}
11378
11379/*
11380 * "setline()" function
11381 */
11382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011384 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386{
11387 linenr_T lnum;
11388 char_u *line;
11389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 lnum = get_tv_lnum(argvars);
11391 line = get_tv_string(&argvars[1]);
11392 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393
11394 if (lnum >= 1
11395 && lnum <= curbuf->b_ml.ml_line_count
11396 && u_savesub(lnum) == OK
11397 && ml_replace(lnum, line, TRUE) == OK)
11398 {
11399 changed_bytes(lnum, 0);
11400 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 }
11403}
11404
11405/*
11406 * "setreg()" function
11407 */
11408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011410 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
11413 int regname;
11414 char_u *strregname;
11415 char_u *stropt;
11416 int append;
11417 char_u yank_type;
11418 long block_len;
11419
11420 block_len = -1;
11421 yank_type = MAUTO;
11422 append = FALSE;
11423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 strregname = get_tv_string(argvars);
11425 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426
11427 regname = (strregname == NULL ? '"' : *strregname);
11428 if (regname == 0 || regname == '@')
11429 regname = '"';
11430 else if (regname == '=')
11431 return;
11432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 switch (*stropt)
11437 {
11438 case 'a': case 'A': /* append */
11439 append = TRUE;
11440 break;
11441 case 'v': case 'c': /* character-wise selection */
11442 yank_type = MCHAR;
11443 break;
11444 case 'V': case 'l': /* line-wise selection */
11445 yank_type = MLINE;
11446 break;
11447#ifdef FEAT_VISUAL
11448 case 'b': case Ctrl_V: /* block-wise selection */
11449 yank_type = MBLOCK;
11450 if (VIM_ISDIGIT(stropt[1]))
11451 {
11452 ++stropt;
11453 block_len = getdigits(&stropt) - 1;
11454 --stropt;
11455 }
11456 break;
11457#endif
11458 }
11459 }
11460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464}
11465
11466
11467/*
11468 * "setwinvar(expr)" function
11469 */
11470/*ARGSUSED*/
11471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011473 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476 win_T *win;
11477#ifdef FEAT_WINDOWS
11478 win_T *save_curwin;
11479#endif
11480 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 char_u nbuf[NUMBUFLEN];
11483
11484 if (check_restricted() || check_secure())
11485 return;
11486 ++emsg_off;
11487 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 varp = &argvars[2];
11490
11491 if (win != NULL && varname != NULL && varp != NULL)
11492 {
11493#ifdef FEAT_WINDOWS
11494 /* set curwin to be our win, temporarily */
11495 save_curwin = curwin;
11496 curwin = win;
11497 curbuf = curwin->w_buffer;
11498#endif
11499
11500 if (*varname == '&')
11501 {
11502 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 set_option_value(varname, get_tv_number(varp),
11504 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 }
11506 else
11507 {
11508 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11509 if (winvarname != NULL)
11510 {
11511 STRCPY(winvarname, "w:");
11512 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011513 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 vim_free(winvarname);
11515 }
11516 }
11517
11518#ifdef FEAT_WINDOWS
11519 /* Restore current window, if it's still valid (autocomands can make
11520 * it invalid). */
11521 if (win_valid(save_curwin))
11522 {
11523 curwin = save_curwin;
11524 curbuf = curwin->w_buffer;
11525 }
11526#endif
11527 }
11528 --emsg_off;
11529}
11530
11531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 */
11534 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011536 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541 p = get_tv_string(&argvars[0]);
11542 rettv->vval.v_string = vim_strsave(p);
11543 simplify_filename(rettv->vval.v_string); /* simplify in place */
11544 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545}
11546
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547static int
11548#ifdef __BORLANDC__
11549 _RTLENTRYF
11550#endif
11551 item_compare __ARGS((const void *s1, const void *s2));
11552static int
11553#ifdef __BORLANDC__
11554 _RTLENTRYF
11555#endif
11556 item_compare2 __ARGS((const void *s1, const void *s2));
11557
11558static int item_compare_ic;
11559static char_u *item_compare_func;
11560#define ITEM_COMPARE_FAIL 999
11561
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011563 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565 static int
11566#ifdef __BORLANDC__
11567_RTLENTRYF
11568#endif
11569item_compare(s1, s2)
11570 const void *s1;
11571 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011573 char_u *p1, *p2;
11574 char_u *tofree1, *tofree2;
11575 int res;
11576 char_u numbuf1[NUMBUFLEN];
11577 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
Bram Moolenaar0d660222005-01-07 21:51:51 +000011579 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
11580 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
11581 if (item_compare_ic)
11582 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584 res = STRCMP(p1, p2);
11585 vim_free(tofree1);
11586 vim_free(tofree2);
11587 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588}
11589
11590 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591#ifdef __BORLANDC__
11592_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594item_compare2(s1, s2)
11595 const void *s1;
11596 const void *s2;
11597{
11598 int res;
11599 typeval rettv;
11600 typeval argv[2];
11601 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602
Bram Moolenaar0d660222005-01-07 21:51:51 +000011603 /* copy the values (is this really needed?) */
11604 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
11605 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
11606
11607 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11608 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011609 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011610 clear_tv(&argv[0]);
11611 clear_tv(&argv[1]);
11612
11613 if (res == FAIL)
11614 res = ITEM_COMPARE_FAIL;
11615 else
11616 res = get_tv_number(&rettv);
11617 clear_tv(&rettv);
11618 return res;
11619}
11620
11621/*
11622 * "sort({list})" function
11623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011625f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011626 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629 listvar *l;
11630 listitem *li;
11631 listitem **ptrs;
11632 long len;
11633 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635 rettv->vval.v_number = 0;
11636 if (argvars[0].v_type != VAR_LIST)
11637 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 else
11639 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011640 l = argvars[0].vval.v_list;
11641 if (l == NULL)
11642 return;
11643 rettv->vval.v_list = l;
11644 rettv->v_type = VAR_LIST;
11645 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647 len = list_len(l);
11648 if (len <= 1)
11649 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651 item_compare_ic = FALSE;
11652 item_compare_func = NULL;
11653 if (argvars[1].v_type != VAR_UNKNOWN)
11654 {
11655 if (argvars[1].v_type == VAR_FUNC)
11656 item_compare_func = argvars[0].vval.v_string;
11657 else
11658 {
11659 i = get_tv_number(&argvars[1]);
11660 if (i == 1)
11661 item_compare_ic = TRUE;
11662 else
11663 item_compare_func = get_tv_string(&argvars[1]);
11664 }
11665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 /* Make an array with each entry pointing to an item in the List. */
11668 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
11669 if (ptrs == NULL)
11670 return;
11671 i = 0;
11672 for (li = l->lv_first; li != NULL; li = li->li_next)
11673 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674
Bram Moolenaar0d660222005-01-07 21:51:51 +000011675 /* test the compare function */
11676 if (item_compare_func != NULL
11677 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11678 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011679 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681 {
11682 /* Sort the array with item pointers. */
11683 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
11684 item_compare_func == NULL ? item_compare : item_compare2);
11685
11686 /* Clear the List and append the items in the sorted order. */
11687 l->lv_first = l->lv_last = NULL;
11688 for (i = 0; i < len; ++i)
11689 list_append(l, ptrs[i]);
11690 }
11691
11692 vim_free(ptrs);
11693 }
11694}
11695
11696 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011697f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698 typeval *argvars;
11699 typeval *rettv;
11700{
11701 char_u *str;
11702 char_u *end;
11703 char_u *pat;
11704 regmatch_T regmatch;
11705 char_u patbuf[NUMBUFLEN];
11706 char_u *save_cpo;
11707 int match;
11708 listitem *ni;
11709 listvar *l;
11710 colnr_T col = 0;
11711
11712 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11713 save_cpo = p_cpo;
11714 p_cpo = (char_u *)"";
11715
11716 str = get_tv_string(&argvars[0]);
11717 if (argvars[1].v_type == VAR_UNKNOWN)
11718 pat = (char_u *)"[\\x01- ]\\+";
11719 else
11720 pat = get_tv_string_buf(&argvars[1], patbuf);
11721
11722 l = list_alloc();
11723 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011725 rettv->v_type = VAR_LIST;
11726 rettv->vval.v_list = l;
11727 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11730 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011732 regmatch.rm_ic = FALSE;
11733 while (*str != NUL)
11734 {
11735 match = vim_regexec_nl(&regmatch, str, col);
11736 if (match)
11737 end = regmatch.startp[0];
11738 else
11739 end = str + STRLEN(str);
11740 if (end > str)
11741 {
11742 ni = listitem_alloc();
11743 if (ni == NULL)
11744 break;
11745 ni->li_tv.v_type = VAR_STRING;
11746 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
11747 list_append(l, ni);
11748 }
11749 if (!match)
11750 break;
11751 /* Advance to just after the match. */
11752 if (regmatch.endp[0] > str)
11753 col = 0;
11754 else
11755 {
11756 /* Don't get stuck at the same match. */
11757#ifdef FEAT_MBYTE
11758 col = mb_ptr2len_check(regmatch.endp[0]);
11759#else
11760 col = 1;
11761#endif
11762 }
11763 str = regmatch.endp[0];
11764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765
Bram Moolenaar0d660222005-01-07 21:51:51 +000011766 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768
Bram Moolenaar0d660222005-01-07 21:51:51 +000011769 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770}
11771
11772#ifdef HAVE_STRFTIME
11773/*
11774 * "strftime({format}[, {time}])" function
11775 */
11776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011778 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780{
11781 char_u result_buf[256];
11782 struct tm *curtime;
11783 time_t seconds;
11784 char_u *p;
11785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011789 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 seconds = time(NULL);
11791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 curtime = localtime(&seconds);
11794 /* MSVC returns NULL for an invalid value of seconds. */
11795 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 else
11798 {
11799# ifdef FEAT_MBYTE
11800 vimconv_T conv;
11801 char_u *enc;
11802
11803 conv.vc_type = CONV_NONE;
11804 enc = enc_locale();
11805 convert_setup(&conv, p_enc, enc);
11806 if (conv.vc_type != CONV_NONE)
11807 p = string_convert(&conv, p, NULL);
11808# endif
11809 if (p != NULL)
11810 (void)strftime((char *)result_buf, sizeof(result_buf),
11811 (char *)p, curtime);
11812 else
11813 result_buf[0] = NUL;
11814
11815# ifdef FEAT_MBYTE
11816 if (conv.vc_type != CONV_NONE)
11817 vim_free(p);
11818 convert_setup(&conv, enc, p_enc);
11819 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 else
11822# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824
11825# ifdef FEAT_MBYTE
11826 /* Release conversion descriptors */
11827 convert_setup(&conv, NULL, NULL);
11828 vim_free(enc);
11829# endif
11830 }
11831}
11832#endif
11833
11834/*
11835 * "stridx()" function
11836 */
11837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011839 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
11842 char_u buf[NUMBUFLEN];
11843 char_u *needle;
11844 char_u *haystack;
11845 char_u *pos;
11846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 needle = get_tv_string(&argvars[1]);
11848 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11850
11851 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855}
11856
11857/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011858 * "string()" function
11859 */
11860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011862 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011864{
11865 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011866 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011869 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011870 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872}
11873
11874/*
11875 * "strlen()" function
11876 */
11877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011879 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882 rettv->vval.v_number = (varnumber_T)(STRLEN(
11883 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884}
11885
11886/*
11887 * "strpart()" function
11888 */
11889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011891 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
11894 char_u *p;
11895 int n;
11896 int len;
11897 int slen;
11898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 slen = (int)STRLEN(p);
11901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 else
11906 len = slen - n; /* default len: all bytes that are available. */
11907
11908 /*
11909 * Only return the overlap between the specified part and the actual
11910 * string.
11911 */
11912 if (n < 0)
11913 {
11914 len += n;
11915 n = 0;
11916 }
11917 else if (n > slen)
11918 n = slen;
11919 if (len < 0)
11920 len = 0;
11921 else if (n + len > slen)
11922 len = slen - n;
11923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->v_type = VAR_STRING;
11925 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926}
11927
11928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011929 * "strridx()" function
11930 */
11931 static void
11932f_strridx(argvars, rettv)
11933 typeval *argvars;
11934 typeval *rettv;
11935{
11936 char_u buf[NUMBUFLEN];
11937 char_u *needle;
11938 char_u *haystack;
11939 char_u *rest;
11940 char_u *lastmatch = NULL;
11941
11942 needle = get_tv_string(&argvars[1]);
11943 haystack = get_tv_string_buf(&argvars[0], buf);
11944 if (*needle == NUL)
11945 /* Empty string matches past the end. */
11946 lastmatch = haystack + STRLEN(haystack);
11947 else
11948 for (rest = haystack; *rest != '\0'; ++rest)
11949 {
11950 rest = (char_u *)strstr((char *)rest, (char *)needle);
11951 if (rest == NULL)
11952 break;
11953 lastmatch = rest;
11954 }
11955
11956 if (lastmatch == NULL)
11957 rettv->vval.v_number = -1;
11958 else
11959 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11960}
11961
11962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 * "strtrans()" function
11964 */
11965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011967 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->v_type = VAR_STRING;
11971 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972}
11973
11974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975 * "submatch()" function
11976 */
11977 static void
11978f_submatch(argvars, rettv)
11979 typeval *argvars;
11980 typeval *rettv;
11981{
11982 rettv->v_type = VAR_STRING;
11983 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11984}
11985
11986/*
11987 * "substitute()" function
11988 */
11989 static void
11990f_substitute(argvars, rettv)
11991 typeval *argvars;
11992 typeval *rettv;
11993{
11994 char_u patbuf[NUMBUFLEN];
11995 char_u subbuf[NUMBUFLEN];
11996 char_u flagsbuf[NUMBUFLEN];
11997
11998 rettv->v_type = VAR_STRING;
11999 rettv->vval.v_string = do_string_sub(
12000 get_tv_string(&argvars[0]),
12001 get_tv_string_buf(&argvars[1], patbuf),
12002 get_tv_string_buf(&argvars[2], subbuf),
12003 get_tv_string_buf(&argvars[3], flagsbuf));
12004}
12005
12006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 * "synID(line, col, trans)" function
12008 */
12009/*ARGSUSED*/
12010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012012 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014{
12015 int id = 0;
12016#ifdef FEAT_SYN_HL
12017 long line;
12018 long col;
12019 int trans;
12020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 line = get_tv_lnum(argvars);
12022 col = get_tv_number(&argvars[1]) - 1;
12023 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
12025 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12026 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12027 id = syn_get_id(line, col, trans);
12028#endif
12029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031}
12032
12033/*
12034 * "synIDattr(id, what [, mode])" function
12035 */
12036/*ARGSUSED*/
12037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012039 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041{
12042 char_u *p = NULL;
12043#ifdef FEAT_SYN_HL
12044 int id;
12045 char_u *what;
12046 char_u *mode;
12047 char_u modebuf[NUMBUFLEN];
12048 int modec;
12049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 id = get_tv_number(&argvars[0]);
12051 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 modec = TOLOWER_ASC(mode[0]);
12056 if (modec != 't' && modec != 'c'
12057#ifdef FEAT_GUI
12058 && modec != 'g'
12059#endif
12060 )
12061 modec = 0; /* replace invalid with current */
12062 }
12063 else
12064 {
12065#ifdef FEAT_GUI
12066 if (gui.in_use)
12067 modec = 'g';
12068 else
12069#endif
12070 if (t_colors > 1)
12071 modec = 'c';
12072 else
12073 modec = 't';
12074 }
12075
12076
12077 switch (TOLOWER_ASC(what[0]))
12078 {
12079 case 'b':
12080 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12081 p = highlight_color(id, what, modec);
12082 else /* bold */
12083 p = highlight_has_attr(id, HL_BOLD, modec);
12084 break;
12085
12086 case 'f': /* fg[#] */
12087 p = highlight_color(id, what, modec);
12088 break;
12089
12090 case 'i':
12091 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12092 p = highlight_has_attr(id, HL_INVERSE, modec);
12093 else /* italic */
12094 p = highlight_has_attr(id, HL_ITALIC, modec);
12095 break;
12096
12097 case 'n': /* name */
12098 p = get_highlight_name(NULL, id - 1);
12099 break;
12100
12101 case 'r': /* reverse */
12102 p = highlight_has_attr(id, HL_INVERSE, modec);
12103 break;
12104
12105 case 's': /* standout */
12106 p = highlight_has_attr(id, HL_STANDOUT, modec);
12107 break;
12108
12109 case 'u': /* underline */
12110 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12111 break;
12112 }
12113
12114 if (p != NULL)
12115 p = vim_strsave(p);
12116#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->v_type = VAR_STRING;
12118 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119}
12120
12121/*
12122 * "synIDtrans(id)" function
12123 */
12124/*ARGSUSED*/
12125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012127 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129{
12130 int id;
12131
12132#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134
12135 if (id > 0)
12136 id = syn_get_final_id(id);
12137 else
12138#endif
12139 id = 0;
12140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142}
12143
12144/*
12145 * "system()" function
12146 */
12147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012149 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012152 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012154 char_u *infile = NULL;
12155 char_u buf[NUMBUFLEN];
12156 int err = FALSE;
12157 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012159 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012160 {
12161 /*
12162 * Write the string to a temp file, to be used for input of the shell
12163 * command.
12164 */
12165 if ((infile = vim_tempname('i')) == NULL)
12166 {
12167 EMSG(_(e_notmp));
12168 return;
12169 }
12170
12171 fd = mch_fopen((char *)infile, WRITEBIN);
12172 if (fd == NULL)
12173 {
12174 EMSG2(_(e_notopen), infile);
12175 goto done;
12176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012178 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12179 err = TRUE;
12180 if (fclose(fd) != 0)
12181 err = TRUE;
12182 if (err)
12183 {
12184 EMSG(_("E677: Error writing temp file"));
12185 goto done;
12186 }
12187 }
12188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012190
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191#ifdef USE_CR
12192 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012193 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 {
12195 char_u *s;
12196
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012197 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 {
12199 if (*s == CAR)
12200 *s = NL;
12201 }
12202 }
12203#else
12204# ifdef USE_CRNL
12205 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012206 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 {
12208 char_u *s, *d;
12209
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012210 d = res;
12211 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 {
12213 if (s[0] == CAR && s[1] == NL)
12214 ++s;
12215 *d++ = *s;
12216 }
12217 *d = NUL;
12218 }
12219# endif
12220#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012221
12222done:
12223 if (infile != NULL)
12224 {
12225 mch_remove(infile);
12226 vim_free(infile);
12227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 rettv->v_type = VAR_STRING;
12229 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230}
12231
12232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 * "tempname()" function
12234 */
12235/*ARGSUSED*/
12236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012238 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012239 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240{
12241 static int x = 'A';
12242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245
12246 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12247 * names. Skip 'I' and 'O', they are used for shell redirection. */
12248 do
12249 {
12250 if (x == 'Z')
12251 x = '0';
12252 else if (x == '9')
12253 x = 'A';
12254 else
12255 {
12256#ifdef EBCDIC
12257 if (x == 'I')
12258 x = 'J';
12259 else if (x == 'R')
12260 x = 'S';
12261 else
12262#endif
12263 ++x;
12264 }
12265 } while (x == 'I' || x == 'O');
12266}
12267
12268/*
12269 * "tolower(string)" function
12270 */
12271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012273 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275{
12276 char_u *p;
12277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 p = vim_strsave(get_tv_string(&argvars[0]));
12279 rettv->v_type = VAR_STRING;
12280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281
12282 if (p != NULL)
12283 while (*p != NUL)
12284 {
12285#ifdef FEAT_MBYTE
12286 int l;
12287
12288 if (enc_utf8)
12289 {
12290 int c, lc;
12291
12292 c = utf_ptr2char(p);
12293 lc = utf_tolower(c);
12294 l = utf_ptr2len_check(p);
12295 /* TODO: reallocate string when byte count changes. */
12296 if (utf_char2len(lc) == l)
12297 utf_char2bytes(lc, p);
12298 p += l;
12299 }
12300 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12301 p += l; /* skip multi-byte character */
12302 else
12303#endif
12304 {
12305 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12306 ++p;
12307 }
12308 }
12309}
12310
12311/*
12312 * "toupper(string)" function
12313 */
12314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012316 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318{
12319 char_u *p;
12320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 p = vim_strsave(get_tv_string(&argvars[0]));
12322 rettv->v_type = VAR_STRING;
12323 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324
12325 if (p != NULL)
12326 while (*p != NUL)
12327 {
12328#ifdef FEAT_MBYTE
12329 int l;
12330
12331 if (enc_utf8)
12332 {
12333 int c, uc;
12334
12335 c = utf_ptr2char(p);
12336 uc = utf_toupper(c);
12337 l = utf_ptr2len_check(p);
12338 /* TODO: reallocate string when byte count changes. */
12339 if (utf_char2len(uc) == l)
12340 utf_char2bytes(uc, p);
12341 p += l;
12342 }
12343 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12344 p += l; /* skip multi-byte character */
12345 else
12346#endif
12347 {
12348 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12349 p++;
12350 }
12351 }
12352}
12353
12354/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012355 * "tr(string, fromstr, tostr)" function
12356 */
12357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012359 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012361{
12362 char_u *instr;
12363 char_u *fromstr;
12364 char_u *tostr;
12365 char_u *p;
12366#ifdef FEAT_MBYTE
12367 int inlen;
12368 int fromlen;
12369 int tolen;
12370 int idx;
12371 char_u *cpstr;
12372 int cplen;
12373 int first = TRUE;
12374#endif
12375 char_u buf[NUMBUFLEN];
12376 char_u buf2[NUMBUFLEN];
12377 garray_T ga;
12378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 instr = get_tv_string(&argvars[0]);
12380 fromstr = get_tv_string_buf(&argvars[1], buf);
12381 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012382
12383 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->v_type = VAR_STRING;
12385 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012386 ga_init2(&ga, (int)sizeof(char), 80);
12387
12388#ifdef FEAT_MBYTE
12389 if (!has_mbyte)
12390#endif
12391 /* not multi-byte: fromstr and tostr must be the same length */
12392 if (STRLEN(fromstr) != STRLEN(tostr))
12393 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012394#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012395error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012396#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012397 EMSG2(_(e_invarg2), fromstr);
12398 ga_clear(&ga);
12399 return;
12400 }
12401
12402 /* fromstr and tostr have to contain the same number of chars */
12403 while (*instr != NUL)
12404 {
12405#ifdef FEAT_MBYTE
12406 if (has_mbyte)
12407 {
12408 inlen = mb_ptr2len_check(instr);
12409 cpstr = instr;
12410 cplen = inlen;
12411 idx = 0;
12412 for (p = fromstr; *p != NUL; p += fromlen)
12413 {
12414 fromlen = mb_ptr2len_check(p);
12415 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12416 {
12417 for (p = tostr; *p != NUL; p += tolen)
12418 {
12419 tolen = mb_ptr2len_check(p);
12420 if (idx-- == 0)
12421 {
12422 cplen = tolen;
12423 cpstr = p;
12424 break;
12425 }
12426 }
12427 if (*p == NUL) /* tostr is shorter than fromstr */
12428 goto error;
12429 break;
12430 }
12431 ++idx;
12432 }
12433
12434 if (first && cpstr == instr)
12435 {
12436 /* Check that fromstr and tostr have the same number of
12437 * (multi-byte) characters. Done only once when a character
12438 * of instr doesn't appear in fromstr. */
12439 first = FALSE;
12440 for (p = tostr; *p != NUL; p += tolen)
12441 {
12442 tolen = mb_ptr2len_check(p);
12443 --idx;
12444 }
12445 if (idx != 0)
12446 goto error;
12447 }
12448
12449 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012450 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012451 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012452
12453 instr += inlen;
12454 }
12455 else
12456#endif
12457 {
12458 /* When not using multi-byte chars we can do it faster. */
12459 p = vim_strchr(fromstr, *instr);
12460 if (p != NULL)
12461 ga_append(&ga, tostr[p - fromstr]);
12462 else
12463 ga_append(&ga, *instr);
12464 ++instr;
12465 }
12466 }
12467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012469}
12470
12471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 * "type(expr)" function
12473 */
12474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012479 int n;
12480
12481 switch (argvars[0].v_type)
12482 {
12483 case VAR_NUMBER: n = 0; break;
12484 case VAR_STRING: n = 1; break;
12485 case VAR_FUNC: n = 2; break;
12486 case VAR_LIST: n = 3; break;
12487 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12488 }
12489 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490}
12491
12492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012493 * "values(dict)" function
12494 */
12495 static void
12496f_values(argvars, rettv)
12497 typeval *argvars;
12498 typeval *rettv;
12499{
12500 dict_list(argvars, rettv, 1);
12501}
12502
12503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 * "virtcol(string)" function
12505 */
12506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012508 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
12511 colnr_T vcol = 0;
12512 pos_T *fp;
12513
12514 fp = var2fpos(&argvars[0], FALSE);
12515 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12516 {
12517 getvvcol(curwin, fp, NULL, NULL, &vcol);
12518 ++vcol;
12519 }
12520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522}
12523
12524/*
12525 * "visualmode()" function
12526 */
12527/*ARGSUSED*/
12528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012530 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
12533#ifdef FEAT_VISUAL
12534 char_u str[2];
12535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 str[0] = curbuf->b_visual_mode_eval;
12538 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540
12541 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012542 if ((argvars[0].v_type == VAR_NUMBER
12543 && argvars[0].vval.v_number != 0)
12544 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012545 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 curbuf->b_visual_mode_eval = NUL;
12547#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012548 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549#endif
12550}
12551
12552/*
12553 * "winbufnr(nr)" function
12554 */
12555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012557 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559{
12560 win_T *wp;
12561
12562 wp = find_win_by_nr(&argvars[0]);
12563 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567}
12568
12569/*
12570 * "wincol()" function
12571 */
12572/*ARGSUSED*/
12573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012574f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012575 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577{
12578 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580}
12581
12582/*
12583 * "winheight(nr)" function
12584 */
12585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012587 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012588 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589{
12590 win_T *wp;
12591
12592 wp = find_win_by_nr(&argvars[0]);
12593 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597}
12598
12599/*
12600 * "winline()" function
12601 */
12602/*ARGSUSED*/
12603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012605 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012606 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607{
12608 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610}
12611
12612/*
12613 * "winnr()" function
12614 */
12615/* ARGSUSED */
12616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012618 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620{
12621 int nr = 1;
12622#ifdef FEAT_WINDOWS
12623 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012624 win_T *twin = curwin;
12625 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012627 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012630 if (STRCMP(arg, "$") == 0)
12631 twin = lastwin;
12632 else if (STRCMP(arg, "#") == 0)
12633 {
12634 twin = prevwin;
12635 if (prevwin == NULL)
12636 nr = 0;
12637 }
12638 else
12639 {
12640 EMSG2(_(e_invexpr2), arg);
12641 nr = 0;
12642 }
12643 }
12644
12645 if (nr > 0)
12646 for (wp = firstwin; wp != twin; wp = wp->w_next)
12647 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650}
12651
12652/*
12653 * "winrestcmd()" function
12654 */
12655/* ARGSUSED */
12656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012658 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660{
12661#ifdef FEAT_WINDOWS
12662 win_T *wp;
12663 int winnr = 1;
12664 garray_T ga;
12665 char_u buf[50];
12666
12667 ga_init2(&ga, (int)sizeof(char), 70);
12668 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12669 {
12670 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12671 ga_concat(&ga, buf);
12672# ifdef FEAT_VERTSPLIT
12673 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12674 ga_concat(&ga, buf);
12675# endif
12676 ++winnr;
12677 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012678 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685}
12686
12687/*
12688 * "winwidth(nr)" function
12689 */
12690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012692 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694{
12695 win_T *wp;
12696
12697 wp = find_win_by_nr(&argvars[0]);
12698 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 else
12701#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705#endif
12706}
12707
12708 static win_T *
12709find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711{
12712#ifdef FEAT_WINDOWS
12713 win_T *wp;
12714#endif
12715 int nr;
12716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718
12719#ifdef FEAT_WINDOWS
12720 if (nr == 0)
12721 return curwin;
12722
12723 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12724 if (--nr <= 0)
12725 break;
12726 return wp;
12727#else
12728 if (nr == 0 || nr == 1)
12729 return curwin;
12730 return NULL;
12731#endif
12732}
12733
12734/*
12735 * Translate a String variable into a position.
12736 */
12737 static pos_T *
12738var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012739 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 int lnum; /* TRUE when $ is last line */
12741{
12742 char_u *name;
12743 static pos_T pos;
12744 pos_T *pp;
12745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 if (name[0] == '.') /* cursor */
12748 return &curwin->w_cursor;
12749 if (name[0] == '\'') /* mark */
12750 {
12751 pp = getmark(name[1], FALSE);
12752 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
12753 return NULL;
12754 return pp;
12755 }
12756 if (name[0] == '$') /* last column or line */
12757 {
12758 if (lnum)
12759 {
12760 pos.lnum = curbuf->b_ml.ml_line_count;
12761 pos.col = 0;
12762 }
12763 else
12764 {
12765 pos.lnum = curwin->w_cursor.lnum;
12766 pos.col = (colnr_T)STRLEN(ml_get_curline());
12767 }
12768 return &pos;
12769 }
12770 return NULL;
12771}
12772
12773/*
12774 * Get the length of an environment variable name.
12775 * Advance "arg" to the first character after the name.
12776 * Return 0 for error.
12777 */
12778 static int
12779get_env_len(arg)
12780 char_u **arg;
12781{
12782 char_u *p;
12783 int len;
12784
12785 for (p = *arg; vim_isIDc(*p); ++p)
12786 ;
12787 if (p == *arg) /* no name found */
12788 return 0;
12789
12790 len = (int)(p - *arg);
12791 *arg = p;
12792 return len;
12793}
12794
12795/*
12796 * Get the length of the name of a function or internal variable.
12797 * "arg" is advanced to the first non-white character after the name.
12798 * Return 0 if something is wrong.
12799 */
12800 static int
12801get_id_len(arg)
12802 char_u **arg;
12803{
12804 char_u *p;
12805 int len;
12806
12807 /* Find the end of the name. */
12808 for (p = *arg; eval_isnamec(*p); ++p)
12809 ;
12810 if (p == *arg) /* no name found */
12811 return 0;
12812
12813 len = (int)(p - *arg);
12814 *arg = skipwhite(p);
12815
12816 return len;
12817}
12818
12819/*
Bram Moolenaara7043832005-01-21 11:56:39 +000012820 * Get the length of the name of a variable or function.
12821 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 * "arg" is advanced to the first non-white character after the name.
12823 * Return 0 if something is wrong.
12824 * If the name contains 'magic' {}'s, expand them and return the
12825 * expanded name in an allocated string via 'alias' - caller must free.
12826 */
12827 static int
Bram Moolenaara7043832005-01-21 11:56:39 +000012828get_name_len(arg, alias, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 char_u **arg;
12830 char_u **alias;
12831 int evaluate;
12832{
12833 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 char_u *p;
12835 char_u *expr_start;
12836 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837
12838 *alias = NULL; /* default to no alias */
12839
12840 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
12841 && (*arg)[2] == (int)KE_SNR)
12842 {
12843 /* hard coded <SNR>, already translated */
12844 *arg += 3;
12845 return get_id_len(arg) + 3;
12846 }
12847 len = eval_fname_script(*arg);
12848 if (len > 0)
12849 {
12850 /* literal "<SID>", "s:" or "<SNR>" */
12851 *arg += len;
12852 }
12853
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 if (expr_start != NULL)
12859 {
12860 char_u *temp_string;
12861
12862 if (!evaluate)
12863 {
12864 len += (int)(p - *arg);
12865 *arg = skipwhite(p);
12866 return len;
12867 }
12868
12869 /*
12870 * Include any <SID> etc in the expanded string:
12871 * Thus the -len here.
12872 */
12873 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
12874 if (temp_string == NULL)
12875 return 0;
12876 *alias = temp_string;
12877 *arg = skipwhite(p);
12878 return (int)STRLEN(temp_string);
12879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880
12881 len += get_id_len(arg);
12882 if (len == 0)
12883 EMSG2(_(e_invexpr2), *arg);
12884
12885 return len;
12886}
12887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888/*
12889 * Find the end of a variable or function name, taking care of magic braces.
12890 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
12891 * start and end of the first magic braces item.
12892 * Return a pointer to just after the name. Equal to "arg" if there is no
12893 * valid name.
12894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 char_u *arg;
12898 char_u **expr_start;
12899 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012900 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 int mb_nest = 0;
12903 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 char_u *p;
12905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 *expr_start = NULL;
12909 *expr_end = NULL;
12910 }
12911
12912 for (p = arg; *p != NUL
12913 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012914 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000012915 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 || mb_nest != 0
12917 || br_nest != 0); ++p)
12918 {
12919 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 if (*p == '[')
12922 ++br_nest;
12923 else if (*p == ']')
12924 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 if (*p == '{')
12929 {
12930 mb_nest++;
12931 if (expr_start != NULL && *expr_start == NULL)
12932 *expr_start = p;
12933 }
12934 else if (*p == '}')
12935 {
12936 mb_nest--;
12937 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12938 *expr_end = p;
12939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 }
12942
12943 return p;
12944}
12945
12946/*
12947 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000012948 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 */
12950 static int
12951eval_isnamec(c)
12952 int c;
12953{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012954 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955}
12956
12957/*
12958 * Find a v: variable.
12959 * Return it's index, or -1 if not found.
12960 */
12961 static int
12962find_vim_var(name, len)
12963 char_u *name;
12964 int len; /* length of "name" */
12965{
12966 char_u *vname;
12967 int vlen;
12968 int i;
12969
12970 /*
12971 * Ignore "v:" for old built-in variables, require it for new ones.
12972 */
12973 if (name[0] == 'v' && name[1] == ':')
12974 {
12975 vname = name + 2;
12976 vlen = len - 2;
12977 }
12978 else
12979 {
12980 vname = name;
12981 vlen = len;
12982 }
12983 for (i = 0; i < VV_LEN; ++i)
12984 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
12985 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
12986 return i;
12987 return -1;
12988}
12989
12990/*
12991 * Set number v: variable to "val".
12992 */
12993 void
12994set_vim_var_nr(idx, val)
12995 int idx;
12996 long val;
12997{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012998 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999}
13000
13001/*
13002 * Get number v: variable value;
13003 */
13004 long
13005get_vim_var_nr(idx)
13006 int idx;
13007{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013008 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009}
13010
13011/*
13012 * Set v:count, v:count1 and v:prevcount.
13013 */
13014 void
13015set_vcount(count, count1)
13016 long count;
13017 long count1;
13018{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013019 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
13020 vimvars[VV_COUNT].vv_nr = count;
13021 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022}
13023
13024/*
13025 * Set string v: variable to a copy of "val".
13026 */
13027 void
13028set_vim_var_string(idx, val, len)
13029 int idx;
13030 char_u *val;
13031 int len; /* length of "val" to use or -1 (whole string) */
13032{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013033 /* Need to do this (at least) once, since we can't initialize a union.
13034 * Will always be invoked when "v:progname" is set. */
13035 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
13036
Bram Moolenaare9a41262005-01-15 22:18:47 +000013037 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013039 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013041 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013043 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044}
13045
13046/*
13047 * Set v:register if needed.
13048 */
13049 void
13050set_reg_var(c)
13051 int c;
13052{
13053 char_u regname;
13054
13055 if (c == 0 || c == ' ')
13056 regname = '"';
13057 else
13058 regname = c;
13059 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013060 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 set_vim_var_string(VV_REG, &regname, 1);
13062}
13063
13064/*
13065 * Get or set v:exception. If "oldval" == NULL, return the current value.
13066 * Otherwise, restore the value to "oldval" and return NULL.
13067 * Must always be called in pairs to save and restore v:exception! Does not
13068 * take care of memory allocations.
13069 */
13070 char_u *
13071v_exception(oldval)
13072 char_u *oldval;
13073{
13074 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013075 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaare9a41262005-01-15 22:18:47 +000013077 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 return NULL;
13079}
13080
13081/*
13082 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
13083 * Otherwise, restore the value to "oldval" and return NULL.
13084 * Must always be called in pairs to save and restore v:throwpoint! Does not
13085 * take care of memory allocations.
13086 */
13087 char_u *
13088v_throwpoint(oldval)
13089 char_u *oldval;
13090{
13091 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013092 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093
Bram Moolenaare9a41262005-01-15 22:18:47 +000013094 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095 return NULL;
13096}
13097
13098#if defined(FEAT_AUTOCMD) || defined(PROTO)
13099/*
13100 * Set v:cmdarg.
13101 * If "eap" != NULL, use "eap" to generate the value and return the old value.
13102 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
13103 * Must always be called in pairs!
13104 */
13105 char_u *
13106set_cmdarg(eap, oldarg)
13107 exarg_T *eap;
13108 char_u *oldarg;
13109{
13110 char_u *oldval;
13111 char_u *newval;
13112 unsigned len;
13113
Bram Moolenaare9a41262005-01-15 22:18:47 +000013114 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013115 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013117 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000013118 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013119 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 }
13121
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013122 if (eap->force_bin == FORCE_BIN)
13123 len = 6;
13124 else if (eap->force_bin == FORCE_NOBIN)
13125 len = 8;
13126 else
13127 len = 0;
13128 if (eap->force_ff != 0)
13129 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
13130# ifdef FEAT_MBYTE
13131 if (eap->force_enc != 0)
13132 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
13133# endif
13134
13135 newval = alloc(len + 1);
13136 if (newval == NULL)
13137 return NULL;
13138
13139 if (eap->force_bin == FORCE_BIN)
13140 sprintf((char *)newval, " ++bin");
13141 else if (eap->force_bin == FORCE_NOBIN)
13142 sprintf((char *)newval, " ++nobin");
13143 else
13144 *newval = NUL;
13145 if (eap->force_ff != 0)
13146 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
13147 eap->cmd + eap->force_ff);
13148# ifdef FEAT_MBYTE
13149 if (eap->force_enc != 0)
13150 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
13151 eap->cmd + eap->force_enc);
13152# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000013153 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013154 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155}
13156#endif
13157
13158/*
13159 * Get the value of internal variable "name".
13160 * Return OK or FAIL.
13161 */
13162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 char_u *name;
13165 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167{
13168 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013169 typeval *tv = NULL;
13170 typeval atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 VAR v;
13172 int cc;
13173 int i;
13174
13175 /* truncate the name, so that we can use strcmp() */
13176 cc = name[len];
13177 name[len] = NUL;
13178
13179 /*
13180 * Check for "b:changedtick".
13181 */
13182 if (STRCMP(name, "b:changedtick") == 0)
13183 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013184 atv.v_type = VAR_NUMBER;
13185 atv.vval.v_number = curbuf->b_changedtick;
13186 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 }
13188
13189 /*
13190 * Check for built-in v: variables.
13191 */
13192 else if ((i = find_vim_var(name, len)) >= 0)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013193 tv = &vimvars[i].tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194
13195 /*
13196 * Check for user-defined variables.
13197 */
13198 else
13199 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013200 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 if (v != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013202 tv = &v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 }
13204
Bram Moolenaare9a41262005-01-15 22:18:47 +000013205 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 if (rettv != NULL)
13208 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 ret = FAIL;
13210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013212 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213
13214 name[len] = cc;
13215
13216 return ret;
13217}
13218
13219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013220 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13221 * value).
13222 */
13223 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013225{
13226 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
13227}
13228
13229/*
13230 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 * The string "s" must have been allocated, it is consumed.
13232 * Return NULL for out of memory, the variable otherwise.
13233 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013234 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 char_u *s;
13237{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv = alloc_tv();
13241 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->v_type = VAR_STRING;
13244 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 }
13246 else
13247 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249}
13250
13251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013252 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 */
13254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013256 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257{
13258 if (varp != NULL)
13259 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013260 switch (varp->v_type)
13261 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013263 func_unref(varp->vval.v_string);
13264 /*FALLTHROUGH*/
13265 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013266 vim_free(varp->vval.v_string);
13267 break;
13268 case VAR_LIST:
13269 list_unref(varp->vval.v_list);
13270 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013271 case VAR_DICT:
13272 dict_unref(varp->vval.v_dict);
13273 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013274 default:
13275 break;
13276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 vim_free(varp);
13278 }
13279}
13280
13281/*
13282 * Free the memory for a variable value and set the value to NULL or 0.
13283 */
13284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013286 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287{
13288 if (varp != NULL)
13289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013290 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013292 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013293 func_unref(varp->vval.v_string);
13294 /*FALLTHROUGH*/
13295 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013296 vim_free(varp->vval.v_string);
13297 varp->vval.v_string = NULL;
13298 break;
13299 case VAR_LIST:
13300 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013301 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013302 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013303 case VAR_DICT:
13304 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013305 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013306 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 varp->vval.v_number = 0;
13309 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 case VAR_UNKNOWN:
13311 break;
13312 default:
13313 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 }
13316}
13317
13318/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319 * Set the value of a variable to NULL without freeing items.
13320 */
13321 static void
13322init_tv(varp)
13323 typeval *varp;
13324{
13325 if (varp != NULL)
13326 vim_memset(varp, 0, sizeof(typeval));
13327}
13328
13329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 * Get the number value of a variable.
13331 * If it is a String variable, uses vim_str2nr().
13332 */
13333 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013335 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013337 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013339 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013341 case VAR_NUMBER:
13342 n = (long)(varp->vval.v_number);
13343 break;
13344 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013345 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013346 break;
13347 case VAR_STRING:
13348 if (varp->vval.v_string != NULL)
13349 vim_str2nr(varp->vval.v_string, NULL, NULL,
13350 TRUE, TRUE, &n, NULL);
13351 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013352 case VAR_LIST:
13353 EMSG(_("E703: Using a List as a number"));
13354 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013355 case VAR_DICT:
13356 EMSG(_("E728: Using a Dictionary as a number"));
13357 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013358 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013359 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013362 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363}
13364
13365/*
13366 * Get the lnum from the first argument. Also accepts ".", "$", etc.
13367 */
13368 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013370 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013372 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 linenr_T lnum;
13374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 if (lnum == 0) /* no valid number, try using line() */
13377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv.v_type = VAR_NUMBER;
13379 f_line(argvars, &rettv);
13380 lnum = rettv.vval.v_number;
13381 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 }
13383 return lnum;
13384}
13385
13386/*
13387 * Get the string value of a variable.
13388 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000013389 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
13390 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 * If the String variable has never been set, return an empty string.
13392 * Never returns NULL;
13393 */
13394 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013396 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397{
13398 static char_u mybuf[NUMBUFLEN];
13399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013400 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401}
13402
13403 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013405 typeval *varp;
13406 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013408 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013410 case VAR_NUMBER:
13411 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
13412 return buf;
13413 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013414 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013415 break;
13416 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013417 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013418 break;
13419 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013420 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013421 break;
13422 case VAR_STRING:
13423 if (varp->vval.v_string != NULL)
13424 return varp->vval.v_string;
13425 break;
13426 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013427 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013430 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431}
13432
13433/*
13434 * Find variable "name" in the list of variables.
13435 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013436 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000013437 * When "htp" is not NULL we are writing to the variable, set "htp" to the
13438 * hashtable used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 */
13440 static VAR
Bram Moolenaara7043832005-01-21 11:56:39 +000013441find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 char_u *name;
Bram Moolenaara7043832005-01-21 11:56:39 +000013443 hashtable **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444{
13445 int i;
13446 char_u *varname;
Bram Moolenaara7043832005-01-21 11:56:39 +000013447 hashtable *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 if (name[0] == 'a' && name[1] == ':')
13450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013451 /* Function arguments "a:".
13452 * NOTE: We use a typecast, because function arguments don't have a
13453 * name. The caller must not try to access the name! */
Bram Moolenaara7043832005-01-21 11:56:39 +000013454 if (htp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 {
13456 EMSG2(_(e_readonlyvar), name);
13457 return NULL;
13458 }
13459 name += 2;
13460 if (current_funccal == NULL)
13461 return NULL;
13462 if (VIM_ISDIGIT(*name))
13463 {
13464 i = atol((char *)name);
13465 if (i == 0) /* a:0 */
13466 return &current_funccal->a0_var;
13467 i += current_funccal->func->args.ga_len;
13468 if (i > current_funccal->argcount) /* a:999 */
13469 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 }
13472 if (STRCMP(name, "firstline") == 0)
13473 return &(current_funccal->firstline);
13474 if (STRCMP(name, "lastline") == 0)
13475 return &(current_funccal->lastline);
13476 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
13477 if (STRCMP(name, ((char_u **)
13478 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 return NULL;
13481 }
13482
Bram Moolenaara7043832005-01-21 11:56:39 +000013483 ht = find_var_ht(name, &varname);
13484 if (htp != NULL)
13485 *htp = ht;
13486 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 return NULL;
Bram Moolenaara7043832005-01-21 11:56:39 +000013488 return find_var_in_ht(ht, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489}
13490
13491/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013492 * Find variable "varname" in hashtable "ht".
13493 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 */
Bram Moolenaara7043832005-01-21 11:56:39 +000013495 static VAR
13496find_var_in_ht(ht, varname)
13497 hashtable *ht;
13498 char_u *varname;
13499{
13500 hashitem *hi;
13501
13502 hi = hash_find(ht, varname);
13503 if (HASHITEM_EMPTY(hi))
13504 return NULL;
13505 return HI2VAR(hi);
13506}
13507
13508/*
13509 * Find the hashtable used for a variable name.
13510 * Set "varname" to the start of name without ':'.
13511 */
13512 static hashtable *
13513find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 char_u *name;
13515 char_u **varname;
13516{
13517 if (name[1] != ':')
13518 {
13519 /* If not "x:name" there must not be any ":" in the name. */
13520 if (vim_strchr(name, ':') != NULL)
13521 return NULL;
13522 *varname = name;
13523 if (current_funccal == NULL)
13524 return &variables; /* global variable */
13525 return &current_funccal->l_vars; /* local function variable */
13526 }
13527 *varname = name + 2;
13528 if (*name == 'b') /* buffer variable */
13529 return &curbuf->b_vars;
13530 if (*name == 'w') /* window variable */
13531 return &curwin->w_vars;
13532 if (*name == 'g') /* global variable */
13533 return &variables;
13534 if (*name == 'l' && current_funccal != NULL)/* local function variable */
13535 return &current_funccal->l_vars;
13536 if (*name == 's' /* script variable */
13537 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
13538 return &SCRIPT_VARS(current_SID);
13539 return NULL;
13540}
13541
13542/*
13543 * Get the string value of a (global/local) variable.
13544 * Returns NULL when it doesn't exist.
13545 */
13546 char_u *
13547get_var_value(name)
13548 char_u *name;
13549{
13550 VAR v;
13551
Bram Moolenaara7043832005-01-21 11:56:39 +000013552 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 if (v == NULL)
13554 return NULL;
Bram Moolenaara7043832005-01-21 11:56:39 +000013555 return get_tv_string(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556}
13557
13558/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013559 * Allocate a new hashtable for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 * sourcing this script and when executing functions defined in the script.
13561 */
13562 void
13563new_script_vars(id)
13564 scid_T id;
13565{
Bram Moolenaara7043832005-01-21 11:56:39 +000013566 int i;
13567 hashtable *ht;
13568
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
13570 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013571 /* Re-allocating ga_data means that an ht_array pointing to
13572 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
13573 * at its init value. */
13574 for (i = 1; i <= ga_scripts.ga_len; ++i)
13575 {
13576 ht = &SCRIPT_VARS(i);
13577 if (ht->ht_mask == HT_INIT_SIZE - 1)
13578 ht->ht_array = ht->ht_smallarray;
13579 }
13580
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 while (ga_scripts.ga_len < id)
13582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 }
13586 }
13587}
13588
13589/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013590 * Initialize hashtable with variables for use.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 */
13592 void
Bram Moolenaara7043832005-01-21 11:56:39 +000013593vars_init(ht)
13594 hashtable *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
Bram Moolenaara7043832005-01-21 11:56:39 +000013596 hash_init(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597}
13598
13599/*
13600 * Clean up a list of internal variables.
13601 */
13602 void
Bram Moolenaara7043832005-01-21 11:56:39 +000013603vars_clear(ht)
13604 hashtable *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605{
Bram Moolenaara7043832005-01-21 11:56:39 +000013606 int todo;
13607 hashitem *hi;
13608 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609
Bram Moolenaara7043832005-01-21 11:56:39 +000013610 todo = ht->ht_used;
13611 for (hi = ht->ht_array; todo > 0; ++hi)
13612 {
13613 if (!HASHITEM_EMPTY(hi))
13614 {
13615 --todo;
13616
13617 /* Free the variable. Don't remove it from the hashtable,
13618 * ht_array might change then. hash_clear() takes care of it
13619 * later. */
13620 v = HI2VAR(hi);
13621 clear_tv(&v->tv);
13622 vim_free(v);
13623 }
13624 }
13625 hash_clear(ht);
13626 hash_init(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627}
13628
Bram Moolenaara7043832005-01-21 11:56:39 +000013629/*
13630 * Delete a variable from hashtable "ht" at item "hi".
13631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000013633delete_var(ht, hi)
13634 hashtable *ht;
13635 hashitem *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636{
Bram Moolenaara7043832005-01-21 11:56:39 +000013637 VAR v = HI2VAR(hi);
13638
13639 hash_remove(ht, hi);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 clear_tv(&v->tv);
Bram Moolenaara7043832005-01-21 11:56:39 +000013641 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642}
13643
13644/*
13645 * List the value of one internal variable.
13646 */
13647 static void
13648list_one_var(v, prefix)
13649 VAR v;
13650 char_u *prefix;
13651{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013652 char_u *tofree;
13653 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013654 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013655
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013656 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013657 list_one_var_a(prefix, v->v_name, v->tv.v_type,
13658 s == NULL ? (char_u *)"" : s);
13659 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
13662/*
13663 * List the value of one "v:" variable.
13664 */
13665 static void
13666list_vim_var(i)
13667 int i; /* index in vimvars[] */
13668{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013669 char_u *tofree;
13670 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 char_u numbuf[NUMBUFLEN];
13672
Bram Moolenaare9a41262005-01-15 22:18:47 +000013673 s = echo_string(&vimvars[i].tv, &tofree, numbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
Bram Moolenaare9a41262005-01-15 22:18:47 +000013675 vimvars[i].tv.v_type, s == NULL ? (char_u *)"" : s);
13676 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677}
13678
13679 static void
13680list_one_var_a(prefix, name, type, string)
13681 char_u *prefix;
13682 char_u *name;
13683 int type;
13684 char_u *string;
13685{
13686 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13687 if (name != NULL) /* "a:" vars don't have a name stored */
13688 msg_puts(name);
13689 msg_putchar(' ');
13690 msg_advance(22);
13691 if (type == VAR_NUMBER)
13692 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013693 else if (type == VAR_FUNC)
13694 msg_putchar('*');
13695 else if (type == VAR_LIST)
13696 {
13697 msg_putchar('[');
13698 if (*string == '[')
13699 ++string;
13700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013701 else if (type == VAR_DICT)
13702 {
13703 msg_putchar('{');
13704 if (*string == '{')
13705 ++string;
13706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 else
13708 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013709
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013711
13712 if (type == VAR_FUNC)
13713 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714}
13715
13716/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 * If the variable already exists, the value is updated.
13719 * Otherwise the variable is created.
13720 */
13721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 typeval *tv;
13725 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726{
13727 int i;
13728 VAR v;
13729 char_u *varname;
Bram Moolenaara7043832005-01-21 11:56:39 +000013730 hashtable *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
13732 /*
13733 * Handle setting internal v: variables.
13734 */
13735 i = find_vim_var(name, (int)STRLEN(name));
13736 if (i >= 0)
13737 {
13738 if (vimvars[i].flags & VV_RO)
13739 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000013740 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
13741 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 else
13743 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013744 if (vimvars[i].tv.v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013746 vim_free(vimvars[i].vv_str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013748 vimvars[i].vv_str = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013749 else
13750 {
13751 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013752 vimvars[i].vv_str = tv->vval.v_string;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 }
13756 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013757 vimvars[i].vv_nr = get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 }
13759 return;
13760 }
13761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763 {
13764 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13765 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13766 ? name[2] : name[0]))
13767 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013768 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013769 return;
13770 }
13771 if (function_exists(name))
13772 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013773 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013774 return;
13775 }
13776 }
13777
Bram Moolenaara7043832005-01-21 11:56:39 +000013778 if (name[0] == 'a' && name[1] == ':')
13779 {
13780 EMSG2(_(e_readonlyvar), name);
13781 return;
13782 }
13783
13784 ht = find_var_ht(name, &varname);
13785 if (ht == NULL)
13786 {
13787 EMSG2(_("E461: Illegal variable name: %s"), name);
13788 return;
13789 }
13790
13791 v = find_var_in_ht(ht, varname);
13792 if (v != NULL) /* existing variable, need to clear the value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013795 && !((v->tv.v_type == VAR_STRING
13796 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 && (tv->v_type == VAR_STRING
13798 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013799 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013800 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013801 return;
13802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 }
13805 else /* add a new variable */
13806 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013807 v = (VAR)alloc((unsigned)(sizeof(var) + STRLEN(varname)));
13808 if (v == NULL)
13809 return;
13810 STRCPY(v->v_name, varname);
13811 if (hash_add(ht, VAR2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013813 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 return;
13815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 }
Bram Moolenaara7043832005-01-21 11:56:39 +000013817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 if (copy || tv->v_type == VAR_NUMBER)
13819 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013820 else
13821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 v->tv = *tv;
13823 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825}
13826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013827/*
13828 * Copy the values from typeval "from" to typeval "to".
13829 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013830 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013834 typeval *from;
13835 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013837 to->v_type = from->v_type;
13838 switch (from->v_type)
13839 {
13840 case VAR_NUMBER:
13841 to->vval.v_number = from->vval.v_number;
13842 break;
13843 case VAR_STRING:
13844 case VAR_FUNC:
13845 if (from->vval.v_string == NULL)
13846 to->vval.v_string = NULL;
13847 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013848 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013849 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013850 if (from->v_type == VAR_FUNC)
13851 func_ref(to->vval.v_string);
13852 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013853 break;
13854 case VAR_LIST:
13855 if (from->vval.v_list == NULL)
13856 to->vval.v_list = NULL;
13857 else
13858 {
13859 to->vval.v_list = from->vval.v_list;
13860 ++to->vval.v_list->lv_refcount;
13861 }
13862 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013863 case VAR_DICT:
13864 if (from->vval.v_dict == NULL)
13865 to->vval.v_dict = NULL;
13866 else
13867 {
13868 to->vval.v_dict = from->vval.v_dict;
13869 ++to->vval.v_dict->dv_refcount;
13870 }
13871 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013872 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013874 break;
13875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876}
13877
13878/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013879 * Make a copy of an item.
13880 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
13881 */
13882 static void
13883item_copy(from, to, deep)
13884 typeval *from;
13885 typeval *to;
13886 int deep;
13887{
13888 static int recurse = 0;
13889
13890 if (recurse >= VAR_MAXNEST)
13891 {
13892 EMSG(_("E698: variable nested too deep for making a copy"));
13893 return;
13894 }
13895 ++recurse;
13896
13897 switch (from->v_type)
13898 {
13899 case VAR_NUMBER:
13900 case VAR_STRING:
13901 case VAR_FUNC:
13902 copy_tv(from, to);
13903 break;
13904 case VAR_LIST:
13905 to->v_type = VAR_LIST;
13906 to->vval.v_list = list_copy(from->vval.v_list, deep);
13907 break;
13908 case VAR_DICT:
13909 to->v_type = VAR_DICT;
13910 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
13911 break;
13912 default:
13913 EMSG2(_(e_intern2), "item_copy()");
13914 }
13915 --recurse;
13916}
13917
13918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 * ":echo expr1 ..." print each argument separated with a space, add a
13920 * newline at the end.
13921 * ":echon expr1 ..." print each argument plain.
13922 */
13923 void
13924ex_echo(eap)
13925 exarg_T *eap;
13926{
13927 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013929 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 char_u *p;
13931 int needclr = TRUE;
13932 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013933 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934
13935 if (eap->skip)
13936 ++emsg_skip;
13937 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
13938 {
13939 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 {
13942 /*
13943 * Report the invalid expression unless the expression evaluation
13944 * has been cancelled due to an aborting error, an interrupt, or an
13945 * exception.
13946 */
13947 if (!aborting())
13948 EMSG2(_(e_invexpr2), p);
13949 break;
13950 }
13951 if (!eap->skip)
13952 {
13953 if (atstart)
13954 {
13955 atstart = FALSE;
13956 /* Call msg_start() after eval1(), evaluating the expression
13957 * may cause a message to appear. */
13958 if (eap->cmdidx == CMD_echo)
13959 msg_start();
13960 }
13961 else if (eap->cmdidx == CMD_echo)
13962 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013963 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013964 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 if (*p == '\n' || *p == '\r' || *p == TAB)
13966 {
13967 if (*p != TAB && needclr)
13968 {
13969 /* remove any text still there from the command */
13970 msg_clr_eos();
13971 needclr = FALSE;
13972 }
13973 msg_putchar_attr(*p, echo_attr);
13974 }
13975 else
13976 {
13977#ifdef FEAT_MBYTE
13978 if (has_mbyte)
13979 {
13980 int i = (*mb_ptr2len_check)(p);
13981
13982 (void)msg_outtrans_len_attr(p, i, echo_attr);
13983 p += i - 1;
13984 }
13985 else
13986#endif
13987 (void)msg_outtrans_len_attr(p, 1, echo_attr);
13988 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013989 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 arg = skipwhite(arg);
13993 }
13994 eap->nextcmd = check_nextcmd(arg);
13995
13996 if (eap->skip)
13997 --emsg_skip;
13998 else
13999 {
14000 /* remove text that may still be there from the command */
14001 if (needclr)
14002 msg_clr_eos();
14003 if (eap->cmdidx == CMD_echo)
14004 msg_end();
14005 }
14006}
14007
14008/*
14009 * ":echohl {name}".
14010 */
14011 void
14012ex_echohl(eap)
14013 exarg_T *eap;
14014{
14015 int id;
14016
14017 id = syn_name2id(eap->arg);
14018 if (id == 0)
14019 echo_attr = 0;
14020 else
14021 echo_attr = syn_id2attr(id);
14022}
14023
14024/*
14025 * ":execute expr1 ..." execute the result of an expression.
14026 * ":echomsg expr1 ..." Print a message
14027 * ":echoerr expr1 ..." Print an error
14028 * Each gets spaces around each argument and a newline at the end for
14029 * echo commands
14030 */
14031 void
14032ex_execute(eap)
14033 exarg_T *eap;
14034{
14035 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 int ret = OK;
14038 char_u *p;
14039 garray_T ga;
14040 int len;
14041 int save_did_emsg;
14042
14043 ga_init2(&ga, 1, 80);
14044
14045 if (eap->skip)
14046 ++emsg_skip;
14047 while (*arg != NUL && *arg != '|' && *arg != '\n')
14048 {
14049 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 {
14052 /*
14053 * Report the invalid expression unless the expression evaluation
14054 * has been cancelled due to an aborting error, an interrupt, or an
14055 * exception.
14056 */
14057 if (!aborting())
14058 EMSG2(_(e_invexpr2), p);
14059 ret = FAIL;
14060 break;
14061 }
14062
14063 if (!eap->skip)
14064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 len = (int)STRLEN(p);
14067 if (ga_grow(&ga, len + 2) == FAIL)
14068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 ret = FAIL;
14071 break;
14072 }
14073 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 ga.ga_len += len;
14077 }
14078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 arg = skipwhite(arg);
14081 }
14082
14083 if (ret != FAIL && ga.ga_data != NULL)
14084 {
14085 if (eap->cmdidx == CMD_echomsg)
14086 MSG_ATTR(ga.ga_data, echo_attr);
14087 else if (eap->cmdidx == CMD_echoerr)
14088 {
14089 /* We don't want to abort following commands, restore did_emsg. */
14090 save_did_emsg = did_emsg;
14091 EMSG((char_u *)ga.ga_data);
14092 if (!force_abort)
14093 did_emsg = save_did_emsg;
14094 }
14095 else if (eap->cmdidx == CMD_execute)
14096 do_cmdline((char_u *)ga.ga_data,
14097 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
14098 }
14099
14100 ga_clear(&ga);
14101
14102 if (eap->skip)
14103 --emsg_skip;
14104
14105 eap->nextcmd = check_nextcmd(arg);
14106}
14107
14108/*
14109 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
14110 * "arg" points to the "&" or '+' when called, to "option" when returning.
14111 * Returns NULL when no option name found. Otherwise pointer to the char
14112 * after the option name.
14113 */
14114 static char_u *
14115find_option_end(arg, opt_flags)
14116 char_u **arg;
14117 int *opt_flags;
14118{
14119 char_u *p = *arg;
14120
14121 ++p;
14122 if (*p == 'g' && p[1] == ':')
14123 {
14124 *opt_flags = OPT_GLOBAL;
14125 p += 2;
14126 }
14127 else if (*p == 'l' && p[1] == ':')
14128 {
14129 *opt_flags = OPT_LOCAL;
14130 p += 2;
14131 }
14132 else
14133 *opt_flags = 0;
14134
14135 if (!ASCII_ISALPHA(*p))
14136 return NULL;
14137 *arg = p;
14138
14139 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
14140 p += 4; /* termcap option */
14141 else
14142 while (ASCII_ISALPHA(*p))
14143 ++p;
14144 return p;
14145}
14146
14147/*
14148 * ":function"
14149 */
14150 void
14151ex_function(eap)
14152 exarg_T *eap;
14153{
14154 char_u *theline;
14155 int j;
14156 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 char_u *name = NULL;
14159 char_u *p;
14160 char_u *arg;
14161 garray_T newargs;
14162 garray_T newlines;
14163 int varargs = FALSE;
14164 int mustend = FALSE;
14165 int flags = 0;
14166 ufunc_T *fp;
14167 int indent;
14168 int nesting;
14169 char_u *skip_until = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014170 VAR v;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014171 funcdict fudi;
14172 static int func_nr = 0; /* number for nameless function */
14173 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174
14175 /*
14176 * ":function" without argument: list functions.
14177 */
14178 if (ends_excmd(*eap->arg))
14179 {
14180 if (!eap->skip)
14181 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014182 if (!isdigit(*fp->name))
14183 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 eap->nextcmd = check_nextcmd(eap->arg);
14185 return;
14186 }
14187
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014188 /*
14189 * Get the function name. There are these situations:
14190 * func normal function name
14191 * "name" == func, "fudi.fd_dict" == NULL
14192 * dict.func new dictionary entry
14193 * "name" == NULL, "fudi.fd_dict" set,
14194 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
14195 * dict.func existing dict entry with a Funcref
14196 * "name" == fname, "fudi.fd_dict" set,
14197 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14198 * dict.func existing dict entry that's not a Funcref
14199 * "name" == NULL, "fudi.fd_dict" set,
14200 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014203 name = trans_function_name(&p, eap->skip, 0, &fudi);
14204 paren = (vim_strchr(p, '(') != NULL);
14205 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 {
14207 /*
14208 * Return on an invalid expression in braces, unless the expression
14209 * evaluation has been cancelled due to an aborting error, an
14210 * interrupt, or an exception.
14211 */
14212 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014214 if (!eap->skip && fudi.fd_newkey != NULL)
14215 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014216 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219 else
14220 eap->skip = TRUE;
14221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 /* An error in a function call during evaluation of an expression in magic
14223 * braces should not cause the function not to be defined. */
14224 saved_did_emsg = did_emsg;
14225 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226
14227 /*
14228 * ":function func" with only function name: list function.
14229 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014230 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 {
14232 if (!ends_excmd(*skipwhite(p)))
14233 {
14234 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014235 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 }
14237 eap->nextcmd = check_nextcmd(p);
14238 if (eap->nextcmd != NULL)
14239 *p = NUL;
14240 if (!eap->skip && !got_int)
14241 {
14242 fp = find_func(name);
14243 if (fp != NULL)
14244 {
14245 list_func_head(fp, TRUE);
14246 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
14247 {
14248 msg_putchar('\n');
14249 msg_outnum((long)(j + 1));
14250 if (j < 9)
14251 msg_putchar(' ');
14252 if (j < 99)
14253 msg_putchar(' ');
14254 msg_prt_line(FUNCLINE(fp, j));
14255 out_flush(); /* show a line at a time */
14256 ui_breakcheck();
14257 }
14258 if (!got_int)
14259 {
14260 msg_putchar('\n');
14261 msg_puts((char_u *)" endfunction");
14262 }
14263 }
14264 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014265 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014267 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 }
14269
14270 /*
14271 * ":function name(arg1, arg2)" Define function.
14272 */
14273 p = skipwhite(p);
14274 if (*p != '(')
14275 {
14276 if (!eap->skip)
14277 {
14278 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014279 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 }
14281 /* attempt to continue by skipping some text */
14282 if (vim_strchr(p, '(') != NULL)
14283 p = vim_strchr(p, '(');
14284 }
14285 p = skipwhite(p + 1);
14286
14287 ga_init2(&newargs, (int)sizeof(char_u *), 3);
14288 ga_init2(&newlines, (int)sizeof(char_u *), 3);
14289
14290 /*
14291 * Isolate the arguments: "arg1, arg2, ...)"
14292 */
14293 while (*p != ')')
14294 {
14295 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
14296 {
14297 varargs = TRUE;
14298 p += 3;
14299 mustend = TRUE;
14300 }
14301 else
14302 {
14303 arg = p;
14304 while (ASCII_ISALNUM(*p) || *p == '_')
14305 ++p;
14306 if (arg == p || isdigit(*arg)
14307 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
14308 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
14309 {
14310 if (!eap->skip)
14311 EMSG2(_("E125: Illegal argument: %s"), arg);
14312 break;
14313 }
14314 if (ga_grow(&newargs, 1) == FAIL)
14315 goto erret;
14316 c = *p;
14317 *p = NUL;
14318 arg = vim_strsave(arg);
14319 if (arg == NULL)
14320 goto erret;
14321 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
14322 *p = c;
14323 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 if (*p == ',')
14325 ++p;
14326 else
14327 mustend = TRUE;
14328 }
14329 p = skipwhite(p);
14330 if (mustend && *p != ')')
14331 {
14332 if (!eap->skip)
14333 EMSG2(_(e_invarg2), eap->arg);
14334 break;
14335 }
14336 }
14337 ++p; /* skip the ')' */
14338
Bram Moolenaare9a41262005-01-15 22:18:47 +000014339 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 for (;;)
14341 {
14342 p = skipwhite(p);
14343 if (STRNCMP(p, "range", 5) == 0)
14344 {
14345 flags |= FC_RANGE;
14346 p += 5;
14347 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014348 else if (STRNCMP(p, "dict", 4) == 0)
14349 {
14350 flags |= FC_DICT;
14351 p += 4;
14352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 else if (STRNCMP(p, "abort", 5) == 0)
14354 {
14355 flags |= FC_ABORT;
14356 p += 5;
14357 }
14358 else
14359 break;
14360 }
14361
14362 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
14363 EMSG(_(e_trailing));
14364
14365 /*
14366 * Read the body of the function, until ":endfunction" is found.
14367 */
14368 if (KeyTyped)
14369 {
14370 /* Check if the function already exists, don't let the user type the
14371 * whole function before telling him it doesn't work! For a script we
14372 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014373 if (!eap->skip && !eap->forceit)
14374 {
14375 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
14376 EMSG(_(e_funcdict));
14377 else if (name != NULL && find_func(name) != NULL)
14378 EMSG2(_(e_funcexts), name);
14379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380
14381 msg_putchar('\n'); /* don't overwrite the function name */
14382 cmdline_row = msg_row;
14383 }
14384
14385 indent = 2;
14386 nesting = 0;
14387 for (;;)
14388 {
14389 msg_scroll = TRUE;
14390 need_wait_return = FALSE;
14391 if (eap->getline == NULL)
14392 theline = getcmdline(':', 0L, indent);
14393 else
14394 theline = eap->getline(':', eap->cookie, indent);
14395 if (KeyTyped)
14396 lines_left = Rows - 1;
14397 if (theline == NULL)
14398 {
14399 EMSG(_("E126: Missing :endfunction"));
14400 goto erret;
14401 }
14402
14403 if (skip_until != NULL)
14404 {
14405 /* between ":append" and "." and between ":python <<EOF" and "EOF"
14406 * don't check for ":endfunc". */
14407 if (STRCMP(theline, skip_until) == 0)
14408 {
14409 vim_free(skip_until);
14410 skip_until = NULL;
14411 }
14412 }
14413 else
14414 {
14415 /* skip ':' and blanks*/
14416 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
14417 ;
14418
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014419 /* Check for "endfunction". */
14420 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 {
14422 vim_free(theline);
14423 break;
14424 }
14425
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014426 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 * at "end". */
14428 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
14429 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014430 else if (STRNCMP(p, "if", 2) == 0
14431 || STRNCMP(p, "wh", 2) == 0
14432 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 || STRNCMP(p, "try", 3) == 0)
14434 indent += 2;
14435
14436 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014437 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014439 if (*p == '!')
14440 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 p += eval_fname_script(p);
14442 if (ASCII_ISALPHA(*p))
14443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014444 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 if (*skipwhite(p) == '(')
14446 {
14447 ++nesting;
14448 indent += 2;
14449 }
14450 }
14451 }
14452
14453 /* Check for ":append" or ":insert". */
14454 p = skip_range(p, NULL);
14455 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
14456 || (p[0] == 'i'
14457 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
14458 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
14459 skip_until = vim_strsave((char_u *)".");
14460
14461 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
14462 arg = skipwhite(skiptowhite(p));
14463 if (arg[0] == '<' && arg[1] =='<'
14464 && ((p[0] == 'p' && p[1] == 'y'
14465 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
14466 || (p[0] == 'p' && p[1] == 'e'
14467 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
14468 || (p[0] == 't' && p[1] == 'c'
14469 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
14470 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
14471 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014472 || (p[0] == 'm' && p[1] == 'z'
14473 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 ))
14475 {
14476 /* ":python <<" continues until a dot, like ":append" */
14477 p = skipwhite(arg + 2);
14478 if (*p == NUL)
14479 skip_until = vim_strsave((char_u *)".");
14480 else
14481 skip_until = vim_strsave(p);
14482 }
14483 }
14484
14485 /* Add the line to the function. */
14486 if (ga_grow(&newlines, 1) == FAIL)
14487 goto erret;
14488 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
14489 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 }
14491
14492 /* Don't define the function when skipping commands or when an error was
14493 * detected. */
14494 if (eap->skip || did_emsg)
14495 goto erret;
14496
14497 /*
14498 * If there are no errors, add the function
14499 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014500 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014501 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014502 v = find_var(name, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014503 if (v != NULL && v->tv.v_type == VAR_FUNC)
14504 {
14505 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
14506 goto erret;
14507 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014508
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014509 fp = find_func(name);
14510 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014512 if (!eap->forceit)
14513 {
14514 EMSG2(_(e_funcexts), name);
14515 goto erret;
14516 }
14517 if (fp->calls > 0)
14518 {
14519 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
14520 name);
14521 goto erret;
14522 }
14523 /* redefine existing function */
14524 ga_clear_strings(&(fp->args));
14525 ga_clear_strings(&(fp->lines));
14526 vim_free(name);
14527 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 }
14530 else
14531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014532 char numbuf[20];
14533
14534 fp = NULL;
14535 if (fudi.fd_newkey == NULL && !eap->forceit)
14536 {
14537 EMSG(_(e_funcdict));
14538 goto erret;
14539 }
14540
14541 /* Give the function a sequential number. Can only be used with a
14542 * Funcref! */
14543 vim_free(name);
14544 sprintf(numbuf, "%d", ++func_nr);
14545 name = vim_strsave((char_u *)numbuf);
14546 if (name == NULL)
14547 goto erret;
14548 }
14549
14550 if (fp == NULL)
14551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14553 if (fp == NULL)
14554 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014555
14556 if (fudi.fd_dict != NULL)
14557 {
14558 if (fudi.fd_di == NULL)
14559 {
14560 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014561 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014562 if (fudi.fd_di == NULL)
14563 {
14564 vim_free(fp);
14565 goto erret;
14566 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014567 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
14568 {
14569 vim_free(fudi.fd_di);
14570 goto erret;
14571 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014572 }
14573 else
14574 /* overwrite existing dict entry */
14575 clear_tv(&fudi.fd_di->di_tv);
14576 fudi.fd_di->di_tv.v_type = VAR_FUNC;
14577 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
14578 fp->refcount = 1;
14579 }
14580
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014582 fp->name = name;
14583 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 fp->next = firstfunc;
14585 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 }
14587 fp->args = newargs;
14588 fp->lines = newlines;
14589 fp->varargs = varargs;
14590 fp->flags = flags;
14591 fp->calls = 0;
14592 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014593 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594
14595erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 ga_clear_strings(&newargs);
14597 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014598ret_free:
14599 vim_free(skip_until);
14600 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603}
14604
14605/*
14606 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000014607 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014609 * flags:
14610 * TFN_INT: internal function name OK
14611 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 * Advances "pp" to just after the function name (if no error).
14613 */
14614 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014615trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 char_u **pp;
14617 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014618 int flags;
14619 funcdict *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014621 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 char_u *start;
14623 char_u *end;
14624 int lead;
14625 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 int len;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014627 lval lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014628
14629 if (fdp != NULL)
14630 vim_memset(fdp, 0, sizeof(funcdict));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000014632
14633 /* Check for hard coded <SNR>: already translated function ID (from a user
14634 * command). */
14635 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
14636 && (*pp)[2] == (int)KE_SNR)
14637 {
14638 *pp += 3;
14639 len = get_id_len(pp) + 3;
14640 return vim_strnsave(start, len);
14641 }
14642
14643 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
14644 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000014646 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014648
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014649 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014650 if (end == start)
14651 {
14652 if (!skip)
14653 EMSG(_("E129: Function name required"));
14654 goto theend;
14655 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014656 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014657 {
14658 /*
14659 * Report an invalid expression in braces, unless the expression
14660 * evaluation has been cancelled due to an aborting error, an
14661 * interrupt, or an exception.
14662 */
14663 if (!aborting())
14664 {
14665 if (end != NULL)
14666 EMSG2(_(e_invarg2), start);
14667 }
14668 else
14669 *pp = find_name_end(start, NULL, NULL, TRUE);
14670 goto theend;
14671 }
14672
14673 if (lv.ll_tv != NULL)
14674 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014675 if (fdp != NULL)
14676 {
14677 fdp->fd_dict = lv.ll_dict;
14678 fdp->fd_newkey = lv.ll_newkey;
14679 lv.ll_newkey = NULL;
14680 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014682 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14683 {
14684 name = vim_strsave(lv.ll_tv->vval.v_string);
14685 *pp = end;
14686 }
14687 else
14688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014689 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
14690 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014691 EMSG(_(e_funcref));
14692 else
14693 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014694 name = NULL;
14695 }
14696 goto theend;
14697 }
14698
14699 if (lv.ll_name == NULL)
14700 {
14701 /* Error found, but continue after the function name. */
14702 *pp = end;
14703 goto theend;
14704 }
14705
14706 if (lv.ll_exp_name != NULL)
14707 len = STRLEN(lv.ll_exp_name);
14708 else
Bram Moolenaara7043832005-01-21 11:56:39 +000014709 {
14710 if (lead == 2) /* skip over "s:" */
14711 lv.ll_name += 2;
14712 len = (int)(end - lv.ll_name);
14713 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014714
14715 /*
14716 * Copy the function name to allocated memory.
14717 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14718 * Accept <SNR>123_name() outside a script.
14719 */
14720 if (skip)
14721 lead = 0; /* do nothing */
14722 else if (lead > 0)
14723 {
14724 lead = 3;
14725 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14726 {
14727 if (current_SID <= 0)
14728 {
14729 EMSG(_(e_usingsid));
14730 goto theend;
14731 }
14732 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14733 lead += (int)STRLEN(sid_buf);
14734 }
14735 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014736 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014737 {
14738 EMSG2(_("E128: Function name must start with a capital: %s"),
14739 lv.ll_name);
14740 goto theend;
14741 }
14742 name = alloc((unsigned)(len + lead + 1));
14743 if (name != NULL)
14744 {
14745 if (lead > 0)
14746 {
14747 name[0] = K_SPECIAL;
14748 name[1] = KS_EXTRA;
14749 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000014750 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014751 STRCPY(name + 3, sid_buf);
14752 }
14753 mch_memmove(name + lead, lv.ll_name, (size_t)len);
14754 name[len + lead] = NUL;
14755 }
14756 *pp = end;
14757
14758theend:
14759 clear_lval(&lv);
14760 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761}
14762
14763/*
14764 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14765 * Return 2 if "p" starts with "s:".
14766 * Return 0 otherwise.
14767 */
14768 static int
14769eval_fname_script(p)
14770 char_u *p;
14771{
14772 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
14773 || STRNICMP(p + 1, "SNR>", 4) == 0))
14774 return 5;
14775 if (p[0] == 's' && p[1] == ':')
14776 return 2;
14777 return 0;
14778}
14779
14780/*
14781 * Return TRUE if "p" starts with "<SID>" or "s:".
14782 * Only works if eval_fname_script() returned non-zero for "p"!
14783 */
14784 static int
14785eval_fname_sid(p)
14786 char_u *p;
14787{
14788 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
14789}
14790
14791/*
14792 * List the head of the function: "name(arg1, arg2)".
14793 */
14794 static void
14795list_func_head(fp, indent)
14796 ufunc_T *fp;
14797 int indent;
14798{
14799 int j;
14800
14801 msg_start();
14802 if (indent)
14803 MSG_PUTS(" ");
14804 MSG_PUTS("function ");
14805 if (fp->name[0] == K_SPECIAL)
14806 {
14807 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
14808 msg_puts(fp->name + 3);
14809 }
14810 else
14811 msg_puts(fp->name);
14812 msg_putchar('(');
14813 for (j = 0; j < fp->args.ga_len; ++j)
14814 {
14815 if (j)
14816 MSG_PUTS(", ");
14817 msg_puts(FUNCARG(fp, j));
14818 }
14819 if (fp->varargs)
14820 {
14821 if (j)
14822 MSG_PUTS(", ");
14823 MSG_PUTS("...");
14824 }
14825 msg_putchar(')');
14826}
14827
14828/*
14829 * Find a function by name, return pointer to it in ufuncs.
14830 * Return NULL for unknown function.
14831 */
14832 static ufunc_T *
14833find_func(name)
14834 char_u *name;
14835{
14836 ufunc_T *fp;
14837
14838 for (fp = firstfunc; fp != NULL; fp = fp->next)
14839 if (STRCMP(name, fp->name) == 0)
14840 break;
14841 return fp;
14842}
14843
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014844/*
14845 * Return TRUE if a function "name" exists.
14846 */
14847 static int
14848function_exists(name)
14849 char_u *name;
14850{
14851 char_u *p = name;
14852 int n = FALSE;
14853
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014854 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014855 if (p != NULL)
14856 {
14857 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14858 n = (find_func(p) != NULL);
14859 else if (ASCII_ISLOWER(*p))
14860 n = (find_internal_func(p) >= 0);
14861 vim_free(p);
14862 }
14863 return n;
14864}
14865
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
14867
14868/*
14869 * Function given to ExpandGeneric() to obtain the list of user defined
14870 * function names.
14871 */
14872 char_u *
14873get_user_func_name(xp, idx)
14874 expand_T *xp;
14875 int idx;
14876{
14877 static ufunc_T *fp = NULL;
14878
14879 if (idx == 0)
14880 fp = firstfunc;
14881 if (fp != NULL)
14882 {
14883 if (STRLEN(fp->name) + 4 >= IOSIZE)
14884 return fp->name; /* prevents overflow */
14885
14886 cat_func_name(IObuff, fp);
14887 if (xp->xp_context != EXPAND_USER_FUNC)
14888 {
14889 STRCAT(IObuff, "(");
14890 if (!fp->varargs && fp->args.ga_len == 0)
14891 STRCAT(IObuff, ")");
14892 }
14893
14894 fp = fp->next;
14895 return IObuff;
14896 }
14897 return NULL;
14898}
14899
14900#endif /* FEAT_CMDL_COMPL */
14901
14902/*
14903 * Copy the function name of "fp" to buffer "buf".
14904 * "buf" must be able to hold the function name plus three bytes.
14905 * Takes care of script-local function names.
14906 */
14907 static void
14908cat_func_name(buf, fp)
14909 char_u *buf;
14910 ufunc_T *fp;
14911{
14912 if (fp->name[0] == K_SPECIAL)
14913 {
14914 STRCPY(buf, "<SNR>");
14915 STRCAT(buf, fp->name + 3);
14916 }
14917 else
14918 STRCPY(buf, fp->name);
14919}
14920
14921/*
14922 * ":delfunction {name}"
14923 */
14924 void
14925ex_delfunction(eap)
14926 exarg_T *eap;
14927{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014928 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 char_u *p;
14930 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014931 funcdict fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932
14933 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014934 name = trans_function_name(&p, eap->skip, 0, &fudi);
14935 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014937 {
14938 if (fudi.fd_dict != NULL && !eap->skip)
14939 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 if (!ends_excmd(*skipwhite(p)))
14943 {
14944 vim_free(name);
14945 EMSG(_(e_trailing));
14946 return;
14947 }
14948 eap->nextcmd = check_nextcmd(p);
14949 if (eap->nextcmd != NULL)
14950 *p = NUL;
14951
14952 if (!eap->skip)
14953 fp = find_func(name);
14954 vim_free(name);
14955
14956 if (!eap->skip)
14957 {
14958 if (fp == NULL)
14959 {
14960 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14961 return;
14962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014963 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 {
14965 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14966 return;
14967 }
14968
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014969 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014971 /* Delete the dict item that refers to the function, it will
14972 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014973 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014975 else
14976 func_free(fp);
14977 }
14978}
14979
14980/*
14981 * Free a function and remove it from the list of functions.
14982 */
14983 static void
14984func_free(fp)
14985 ufunc_T *fp;
14986{
14987 ufunc_T *pfp;
14988
14989 /* clear this function */
14990 vim_free(fp->name);
14991 ga_clear_strings(&(fp->args));
14992 ga_clear_strings(&(fp->lines));
14993
14994 /* remove the function from the function list */
14995 if (firstfunc == fp)
14996 firstfunc = fp->next;
14997 else
14998 {
14999 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
15000 if (pfp->next == fp)
15001 {
15002 pfp->next = fp->next;
15003 break;
15004 }
15005 }
15006 vim_free(fp);
15007}
15008
15009/*
15010 * Unreference a Function: decrement the reference count and free it when it
15011 * becomes zero. Only for numbered functions.
15012 */
15013 static void
15014func_unref(name)
15015 char_u *name;
15016{
15017 ufunc_T *fp;
15018
15019 if (name != NULL && isdigit(*name))
15020 {
15021 fp = find_func(name);
15022 if (fp == NULL)
15023 EMSG2(_(e_intern2), "func_unref()");
15024 else if (--fp->refcount <= 0)
15025 {
15026 /* Only delete it when it's not being used. Otherwise it's done
15027 * when "calls" becomes zero. */
15028 if (fp->calls == 0)
15029 func_free(fp);
15030 }
15031 }
15032}
15033
15034/*
15035 * Count a reference to a Function.
15036 */
15037 static void
15038func_ref(name)
15039 char_u *name;
15040{
15041 ufunc_T *fp;
15042
15043 if (name != NULL && isdigit(*name))
15044 {
15045 fp = find_func(name);
15046 if (fp == NULL)
15047 EMSG2(_(e_intern2), "func_ref()");
15048 else
15049 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 }
15051}
15052
15053/*
15054 * Call a user function.
15055 */
15056 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000015057call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 ufunc_T *fp; /* pointer to function */
15059 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015061 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 linenr_T firstline; /* first line of range */
15063 linenr_T lastline; /* last line of range */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015064 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065{
15066 char_u *save_sourcing_name;
15067 linenr_T save_sourcing_lnum;
15068 scid_T save_current_SID;
15069 struct funccall fc;
15070 struct funccall *save_fcp = current_funccal;
15071 int save_did_emsg;
15072 static int depth = 0;
15073
15074 /* If depth of calling is getting too high, don't execute the function */
15075 if (depth >= p_mfd)
15076 {
15077 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078 rettv->v_type = VAR_NUMBER;
15079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 return;
15081 }
15082 ++depth;
15083
15084 line_breakcheck(); /* check for CTRL-C hit */
15085
15086 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015087 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 fc.func = fp;
15089 fc.argcount = argcount;
15090 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015091 fc.rettv = rettv;
15092 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 fc.linenr = 0;
15094 fc.returned = FALSE;
15095 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015096 fc.a0_var.tv.v_type = VAR_NUMBER;
15097 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
Bram Moolenaara7043832005-01-21 11:56:39 +000015098 fc.a0_var.v_name[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015100 fc.firstline.tv.v_type = VAR_NUMBER;
15101 fc.firstline.tv.vval.v_number = firstline;
Bram Moolenaara7043832005-01-21 11:56:39 +000015102 fc.firstline.v_name[0] = NUL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015103 fc.lastline.tv.v_type = VAR_NUMBER;
15104 fc.lastline.tv.vval.v_number = lastline;
Bram Moolenaara7043832005-01-21 11:56:39 +000015105 fc.lastline.v_name[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 /* Check if this function has a breakpoint. */
15107 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
15108 fc.dbg_tick = debug_tick;
15109
Bram Moolenaara7043832005-01-21 11:56:39 +000015110 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015111 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015112 VAR v = (VAR)alloc((unsigned)(sizeof(var) + 4));
Bram Moolenaare9a41262005-01-15 22:18:47 +000015113
Bram Moolenaara7043832005-01-21 11:56:39 +000015114 if (v != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015115 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015116 STRCPY(v->v_name, "self");
15117 hash_add(&fc.l_vars, VAR2HIKEY(v));
Bram Moolenaare9a41262005-01-15 22:18:47 +000015118 v->tv.v_type = VAR_DICT;
15119 v->tv.vval.v_dict = selfdict;
15120 ++selfdict->dv_refcount;
15121 }
15122 }
15123
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124 /* Don't redraw while executing the function. */
15125 ++RedrawingDisabled;
15126 save_sourcing_name = sourcing_name;
15127 save_sourcing_lnum = sourcing_lnum;
15128 sourcing_lnum = 1;
15129 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
15130 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
15131 if (sourcing_name != NULL)
15132 {
15133 if (save_sourcing_name != NULL
15134 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
15135 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
15136 else
15137 STRCPY(sourcing_name, "function ");
15138 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
15139
15140 if (p_verbose >= 12)
15141 {
15142 ++no_wait_return;
15143 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15144 msg_str((char_u *)_("calling %s"), sourcing_name);
15145 if (p_verbose >= 14)
15146 {
15147 int i;
15148 char_u buf[MSG_BUF_LEN];
15149
15150 msg_puts((char_u *)"(");
15151 for (i = 0; i < argcount; ++i)
15152 {
15153 if (i > 0)
15154 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015155 if (argvars[i].v_type == VAR_NUMBER)
15156 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 else
15158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 buf, MSG_BUF_LEN);
15161 msg_puts((char_u *)"\"");
15162 msg_puts(buf);
15163 msg_puts((char_u *)"\"");
15164 }
15165 }
15166 msg_puts((char_u *)")");
15167 }
15168 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15169 cmdline_row = msg_row;
15170 --no_wait_return;
15171 }
15172 }
15173 save_current_SID = current_SID;
15174 current_SID = fp->script_ID;
15175 save_did_emsg = did_emsg;
15176 did_emsg = FALSE;
15177
15178 /* call do_cmdline() to execute the lines */
15179 do_cmdline(NULL, get_func_line, (void *)&fc,
15180 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
15181
15182 --RedrawingDisabled;
15183
15184 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015185 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187 clear_tv(rettv);
15188 rettv->v_type = VAR_NUMBER;
15189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 }
15191
15192 /* when being verbose, mention the return value */
15193 if (p_verbose >= 12)
15194 {
15195 char_u *sn, *val;
15196
15197 ++no_wait_return;
15198 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15199
15200 /* Make sure the output fits in IObuff. */
15201 sn = sourcing_name;
15202 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
15203 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
15204
15205 if (aborting())
15206 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015209 (long)fc.rettv->vval.v_number);
15210 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213 if (STRLEN(val) > IOSIZE / 2 - 50)
15214 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
15215 smsg((char_u *)_("%s returning \"%s\""), sn, val);
15216 }
15217 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15218 cmdline_row = msg_row;
15219 --no_wait_return;
15220 }
15221
15222 vim_free(sourcing_name);
15223 sourcing_name = save_sourcing_name;
15224 sourcing_lnum = save_sourcing_lnum;
15225 current_SID = save_current_SID;
15226
15227 if (p_verbose >= 12 && sourcing_name != NULL)
15228 {
15229 ++no_wait_return;
15230 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15231 msg_str((char_u *)_("continuing in %s"), sourcing_name);
15232 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15233 cmdline_row = msg_row;
15234 --no_wait_return;
15235 }
15236
15237 did_emsg |= save_did_emsg;
15238 current_funccal = save_fcp;
15239
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 --depth;
15242}
15243
15244/*
15245 * ":return [expr]"
15246 */
15247 void
15248ex_return(eap)
15249 exarg_T *eap;
15250{
15251 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015252 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 int returning = FALSE;
15254
15255 if (current_funccal == NULL)
15256 {
15257 EMSG(_("E133: :return not inside a function"));
15258 return;
15259 }
15260
15261 if (eap->skip)
15262 ++emsg_skip;
15263
15264 eap->nextcmd = NULL;
15265 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 {
15268 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 }
15273 /* It's safer to return also on error. */
15274 else if (!eap->skip)
15275 {
15276 /*
15277 * Return unless the expression evaluation has been cancelled due to an
15278 * aborting error, an interrupt, or an exception.
15279 */
15280 if (!aborting())
15281 returning = do_return(eap, FALSE, TRUE, NULL);
15282 }
15283
15284 /* When skipping or the return gets pending, advance to the next command
15285 * in this line (!returning). Otherwise, ignore the rest of the line.
15286 * Following lines will be ignored by get_func_line(). */
15287 if (returning)
15288 eap->nextcmd = NULL;
15289 else if (eap->nextcmd == NULL) /* no argument */
15290 eap->nextcmd = check_nextcmd(arg);
15291
15292 if (eap->skip)
15293 --emsg_skip;
15294}
15295
15296/*
15297 * Return from a function. Possibly makes the return pending. Also called
15298 * for a pending return at the ":endtry" or after returning from an extra
15299 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300 * when called due to a ":return" command. "rettv" may point to a typeval
15301 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 * FALSE when the return gets pending.
15303 */
15304 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015305do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 exarg_T *eap;
15307 int reanimate;
15308 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310{
15311 int idx;
15312 struct condstack *cstack = eap->cstack;
15313
15314 if (reanimate)
15315 /* Undo the return. */
15316 current_funccal->returned = FALSE;
15317
15318 /*
15319 * Cleanup (and inactivate) conditionals, but stop when a try conditional
15320 * not in its finally clause (which then is to be executed next) is found.
15321 * In this case, make the ":return" pending for execution at the ":endtry".
15322 * Otherwise, return normally.
15323 */
15324 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
15325 if (idx >= 0)
15326 {
15327 cstack->cs_pending[idx] = CSTP_RETURN;
15328
15329 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330 /* A pending return again gets pending. "rettv" points to an
15331 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 else
15335 {
15336 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 {
15343 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
15345 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 else
15347 EMSG(_(e_outofmem));
15348 }
15349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015350 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351
15352 if (reanimate)
15353 {
15354 /* The pending return value could be overwritten by a ":return"
15355 * without argument in a finally clause; reset the default
15356 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 current_funccal->rettv->v_type = VAR_NUMBER;
15358 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 }
15360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 }
15363 else
15364 {
15365 current_funccal->returned = TRUE;
15366
15367 /* If the return is carried out now, store the return value. For
15368 * a return immediately after reanimation, the value is already
15369 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 clear_tv(current_funccal->rettv);
15373 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 }
15377 }
15378
15379 return idx < 0;
15380}
15381
15382/*
15383 * Free the variable with a pending return value.
15384 */
15385 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386discard_pending_return(rettv)
15387 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390}
15391
15392/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 * is an allocated string. Used by report_pending() for verbose messages.
15395 */
15396 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397get_return_cmd(rettv)
15398 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 char_u *s;
15401 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015402 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 if (rettv == NULL)
15405 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015407 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408
15409 STRCPY(IObuff, ":return ");
15410 STRNCPY(IObuff + 8, s, IOSIZE - 8);
15411 if (STRLEN(s) + 8 >= IOSIZE)
15412 STRCPY(IObuff + IOSIZE - 4, "...");
15413 vim_free(tofree);
15414 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415}
15416
15417/*
15418 * Get next function line.
15419 * Called by do_cmdline() to get the next line.
15420 * Returns allocated string, or NULL for end of function.
15421 */
15422/* ARGSUSED */
15423 char_u *
15424get_func_line(c, cookie, indent)
15425 int c; /* not used */
15426 void *cookie;
15427 int indent; /* not used */
15428{
15429 struct funccall *fcp = (struct funccall *)cookie;
15430 char_u *retval;
15431 garray_T *gap; /* growarray with function lines */
15432
15433 /* If breakpoints have been added/deleted need to check for it. */
15434 if (fcp->dbg_tick != debug_tick)
15435 {
15436 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15437 sourcing_lnum);
15438 fcp->dbg_tick = debug_tick;
15439 }
15440
15441 gap = &fcp->func->lines;
15442 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15443 retval = NULL;
15444 else if (fcp->returned || fcp->linenr >= gap->ga_len)
15445 retval = NULL;
15446 else
15447 {
15448 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
15449 sourcing_lnum = fcp->linenr;
15450 }
15451
15452 /* Did we encounter a breakpoint? */
15453 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
15454 {
15455 dbg_breakpoint(fcp->func->name, sourcing_lnum);
15456 /* Find next breakpoint. */
15457 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15458 sourcing_lnum);
15459 fcp->dbg_tick = debug_tick;
15460 }
15461
15462 return retval;
15463}
15464
15465/*
15466 * Return TRUE if the currently active function should be ended, because a
15467 * return was encountered or an error occured. Used inside a ":while".
15468 */
15469 int
15470func_has_ended(cookie)
15471 void *cookie;
15472{
15473 struct funccall *fcp = (struct funccall *)cookie;
15474
15475 /* Ignore the "abort" flag if the abortion behavior has been changed due to
15476 * an error inside a try conditional. */
15477 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15478 || fcp->returned);
15479}
15480
15481/*
15482 * return TRUE if cookie indicates a function which "abort"s on errors.
15483 */
15484 int
15485func_has_abort(cookie)
15486 void *cookie;
15487{
15488 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
15489}
15490
15491#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
15492typedef enum
15493{
15494 VAR_FLAVOUR_DEFAULT,
15495 VAR_FLAVOUR_SESSION,
15496 VAR_FLAVOUR_VIMINFO
15497} var_flavour_T;
15498
15499static var_flavour_T var_flavour __ARGS((char_u *varname));
15500
15501 static var_flavour_T
15502var_flavour(varname)
15503 char_u *varname;
15504{
15505 char_u *p = varname;
15506
15507 if (ASCII_ISUPPER(*p))
15508 {
15509 while (*(++p))
15510 if (ASCII_ISLOWER(*p))
15511 return VAR_FLAVOUR_SESSION;
15512 return VAR_FLAVOUR_VIMINFO;
15513 }
15514 else
15515 return VAR_FLAVOUR_DEFAULT;
15516}
15517#endif
15518
15519#if defined(FEAT_VIMINFO) || defined(PROTO)
15520/*
15521 * Restore global vars that start with a capital from the viminfo file
15522 */
15523 int
15524read_viminfo_varlist(virp, writing)
15525 vir_T *virp;
15526 int writing;
15527{
15528 char_u *tab;
15529 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015530 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531
15532 if (!writing && (find_viminfo_parameter('!') != NULL))
15533 {
15534 tab = vim_strchr(virp->vir_line + 1, '\t');
15535 if (tab != NULL)
15536 {
15537 *tab++ = '\0'; /* isolate the variable name */
15538 if (*tab == 'S') /* string var */
15539 is_string = TRUE;
15540
15541 tab = vim_strchr(tab, '\t');
15542 if (tab != NULL)
15543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 if (is_string)
15545 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015546 tv.v_type = VAR_STRING;
15547 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 }
15550 else
15551 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015552 tv.v_type = VAR_NUMBER;
15553 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015555 set_var(virp->vir_line + 1, &tv, FALSE);
15556 if (is_string)
15557 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 }
15559 }
15560 }
15561
15562 return viminfo_readline(virp);
15563}
15564
15565/*
15566 * Write global vars that start with a capital to the viminfo file
15567 */
15568 void
15569write_viminfo_varlist(fp)
15570 FILE *fp;
15571{
Bram Moolenaara7043832005-01-21 11:56:39 +000015572 hashitem *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 VAR this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000015574 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015575 char *s;
15576 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015577 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578
15579 if (find_viminfo_parameter('!') == NULL)
15580 return;
15581
15582 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000015583
15584 todo = variables.ht_used;
15585 for (hi = variables.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015587 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015589 --todo;
15590 this_var = HI2VAR(hi);
15591 if (var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015593 switch (this_var->tv.v_type)
15594 {
15595 case VAR_STRING: s = "STR"; break;
15596 case VAR_NUMBER: s = "NUM"; break;
15597 default: continue;
15598 }
15599 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
15600 viminfo_writestring(fp, echo_string(&this_var->tv,
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015601 &tofree, numbuf));
Bram Moolenaara7043832005-01-21 11:56:39 +000015602 vim_free(tofree);
15603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604 }
15605 }
15606}
15607#endif
15608
15609#if defined(FEAT_SESSION) || defined(PROTO)
15610 int
15611store_session_globals(fd)
15612 FILE *fd;
15613{
Bram Moolenaara7043832005-01-21 11:56:39 +000015614 hashitem *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 VAR this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000015616 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 char_u *p, *t;
15618
Bram Moolenaara7043832005-01-21 11:56:39 +000015619 todo = variables.ht_used;
15620 for (hi = variables.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015624 --todo;
15625 this_var = HI2VAR(hi);
15626 if ((this_var->tv.v_type == VAR_NUMBER
15627 || this_var->tv.v_type == VAR_STRING)
15628 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015629 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015630 /* Escape special characters with a backslash. Turn a LF and
15631 * CR into \n and \r. */
15632 p = vim_strsave_escaped(get_tv_string(&this_var->tv),
15633 (char_u *)"\\\"\n\r");
15634 if (p == NULL) /* out of memory */
15635 break;
15636 for (t = p; *t != NUL; ++t)
15637 if (*t == '\n')
15638 *t = 'n';
15639 else if (*t == '\r')
15640 *t = 'r';
15641 if ((fprintf(fd, "let %s = %c%s%c",
15642 this_var->v_name,
15643 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
15644 p,
15645 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
15646 || put_eol(fd) == FAIL)
15647 {
15648 vim_free(p);
15649 return FAIL;
15650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 vim_free(p);
15652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 }
15654 }
15655 return OK;
15656}
15657#endif
15658
15659#endif /* FEAT_EVAL */
15660
15661#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
15662
15663
15664#ifdef WIN3264
15665/*
15666 * Functions for ":8" filename modifier: get 8.3 version of a filename.
15667 */
15668static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15669static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
15670static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15671
15672/*
15673 * Get the short pathname of a file.
15674 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
15675 */
15676 static int
15677get_short_pathname(fnamep, bufp, fnamelen)
15678 char_u **fnamep;
15679 char_u **bufp;
15680 int *fnamelen;
15681{
15682 int l,len;
15683 char_u *newbuf;
15684
15685 len = *fnamelen;
15686
15687 l = GetShortPathName(*fnamep, *fnamep, len);
15688 if (l > len - 1)
15689 {
15690 /* If that doesn't work (not enough space), then save the string
15691 * and try again with a new buffer big enough
15692 */
15693 newbuf = vim_strnsave(*fnamep, l);
15694 if (newbuf == NULL)
15695 return 0;
15696
15697 vim_free(*bufp);
15698 *fnamep = *bufp = newbuf;
15699
15700 l = GetShortPathName(*fnamep,*fnamep,l+1);
15701
15702 /* Really should always succeed, as the buffer is big enough */
15703 }
15704
15705 *fnamelen = l;
15706 return 1;
15707}
15708
15709/*
15710 * Create a short path name. Returns the length of the buffer it needs.
15711 * Doesn't copy over the end of the buffer passed in.
15712 */
15713 static int
15714shortpath_for_invalid_fname(fname, bufp, fnamelen)
15715 char_u **fname;
15716 char_u **bufp;
15717 int *fnamelen;
15718{
15719 char_u *s, *p, *pbuf2, *pbuf3;
15720 char_u ch;
15721 int l,len,len2,plen,slen;
15722
15723 /* Make a copy */
15724 len2 = *fnamelen;
15725 pbuf2 = vim_strnsave(*fname, len2);
15726 pbuf3 = NULL;
15727
15728 s = pbuf2 + len2 - 1; /* Find the end */
15729 slen = 1;
15730 plen = len2;
15731
15732 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015733 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 {
15735 --s;
15736 ++slen;
15737 --plen;
15738 }
15739
15740 do
15741 {
15742 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015743 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 {
15745 --s;
15746 ++slen;
15747 --plen;
15748 }
15749 if (s <= pbuf2)
15750 break;
15751
15752 /* Remeber the character that is about to be blatted */
15753 ch = *s;
15754 *s = 0; /* get_short_pathname requires a null-terminated string */
15755
15756 /* Try it in situ */
15757 p = pbuf2;
15758 if (!get_short_pathname(&p, &pbuf3, &plen))
15759 {
15760 vim_free(pbuf2);
15761 return -1;
15762 }
15763 *s = ch; /* Preserve the string */
15764 } while (plen == 0);
15765
15766 if (plen > 0)
15767 {
15768 /* Remeber the length of the new string. */
15769 *fnamelen = len = plen + slen;
15770 vim_free(*bufp);
15771 if (len > len2)
15772 {
15773 /* If there's not enough space in the currently allocated string,
15774 * then copy it to a buffer big enough.
15775 */
15776 *fname= *bufp = vim_strnsave(p, len);
15777 if (*fname == NULL)
15778 return -1;
15779 }
15780 else
15781 {
15782 /* Transfer pbuf2 to being the main buffer (it's big enough) */
15783 *fname = *bufp = pbuf2;
15784 if (p != pbuf2)
15785 strncpy(*fname, p, plen);
15786 pbuf2 = NULL;
15787 }
15788 /* Concat the next bit */
15789 strncpy(*fname + plen, s, slen);
15790 (*fname)[len] = '\0';
15791 }
15792 vim_free(pbuf3);
15793 vim_free(pbuf2);
15794 return 0;
15795}
15796
15797/*
15798 * Get a pathname for a partial path.
15799 */
15800 static int
15801shortpath_for_partial(fnamep, bufp, fnamelen)
15802 char_u **fnamep;
15803 char_u **bufp;
15804 int *fnamelen;
15805{
15806 int sepcount, len, tflen;
15807 char_u *p;
15808 char_u *pbuf, *tfname;
15809 int hasTilde;
15810
15811 /* Count up the path seperators from the RHS.. so we know which part
15812 * of the path to return.
15813 */
15814 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015815 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 if (vim_ispathsep(*p))
15817 ++sepcount;
15818
15819 /* Need full path first (use expand_env() to remove a "~/") */
15820 hasTilde = (**fnamep == '~');
15821 if (hasTilde)
15822 pbuf = tfname = expand_env_save(*fnamep);
15823 else
15824 pbuf = tfname = FullName_save(*fnamep, FALSE);
15825
15826 len = tflen = STRLEN(tfname);
15827
15828 if (!get_short_pathname(&tfname, &pbuf, &len))
15829 return -1;
15830
15831 if (len == 0)
15832 {
15833 /* Don't have a valid filename, so shorten the rest of the
15834 * path if we can. This CAN give us invalid 8.3 filenames, but
15835 * there's not a lot of point in guessing what it might be.
15836 */
15837 len = tflen;
15838 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
15839 return -1;
15840 }
15841
15842 /* Count the paths backward to find the beginning of the desired string. */
15843 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015844 {
15845#ifdef FEAT_MBYTE
15846 if (has_mbyte)
15847 p -= mb_head_off(tfname, p);
15848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 if (vim_ispathsep(*p))
15850 {
15851 if (sepcount == 0 || (hasTilde && sepcount == 1))
15852 break;
15853 else
15854 sepcount --;
15855 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 if (hasTilde)
15858 {
15859 --p;
15860 if (p >= tfname)
15861 *p = '~';
15862 else
15863 return -1;
15864 }
15865 else
15866 ++p;
15867
15868 /* Copy in the string - p indexes into tfname - allocated at pbuf */
15869 vim_free(*bufp);
15870 *fnamelen = (int)STRLEN(p);
15871 *bufp = pbuf;
15872 *fnamep = p;
15873
15874 return 0;
15875}
15876#endif /* WIN3264 */
15877
15878/*
15879 * Adjust a filename, according to a string of modifiers.
15880 * *fnamep must be NUL terminated when called. When returning, the length is
15881 * determined by *fnamelen.
15882 * Returns valid flags.
15883 * When there is an error, *fnamep is set to NULL.
15884 */
15885 int
15886modify_fname(src, usedlen, fnamep, bufp, fnamelen)
15887 char_u *src; /* string with modifiers */
15888 int *usedlen; /* characters after src that are used */
15889 char_u **fnamep; /* file name so far */
15890 char_u **bufp; /* buffer for allocated file name or NULL */
15891 int *fnamelen; /* length of fnamep */
15892{
15893 int valid = 0;
15894 char_u *tail;
15895 char_u *s, *p, *pbuf;
15896 char_u dirname[MAXPATHL];
15897 int c;
15898 int has_fullname = 0;
15899#ifdef WIN3264
15900 int has_shortname = 0;
15901#endif
15902
15903repeat:
15904 /* ":p" - full path/file_name */
15905 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
15906 {
15907 has_fullname = 1;
15908
15909 valid |= VALID_PATH;
15910 *usedlen += 2;
15911
15912 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
15913 if ((*fnamep)[0] == '~'
15914#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
15915 && ((*fnamep)[1] == '/'
15916# ifdef BACKSLASH_IN_FILENAME
15917 || (*fnamep)[1] == '\\'
15918# endif
15919 || (*fnamep)[1] == NUL)
15920
15921#endif
15922 )
15923 {
15924 *fnamep = expand_env_save(*fnamep);
15925 vim_free(*bufp); /* free any allocated file name */
15926 *bufp = *fnamep;
15927 if (*fnamep == NULL)
15928 return -1;
15929 }
15930
15931 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015932 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 {
15934 if (vim_ispathsep(*p)
15935 && p[1] == '.'
15936 && (p[2] == NUL
15937 || vim_ispathsep(p[2])
15938 || (p[2] == '.'
15939 && (p[3] == NUL || vim_ispathsep(p[3])))))
15940 break;
15941 }
15942
15943 /* FullName_save() is slow, don't use it when not needed. */
15944 if (*p != NUL || !vim_isAbsName(*fnamep))
15945 {
15946 *fnamep = FullName_save(*fnamep, *p != NUL);
15947 vim_free(*bufp); /* free any allocated file name */
15948 *bufp = *fnamep;
15949 if (*fnamep == NULL)
15950 return -1;
15951 }
15952
15953 /* Append a path separator to a directory. */
15954 if (mch_isdir(*fnamep))
15955 {
15956 /* Make room for one or two extra characters. */
15957 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
15958 vim_free(*bufp); /* free any allocated file name */
15959 *bufp = *fnamep;
15960 if (*fnamep == NULL)
15961 return -1;
15962 add_pathsep(*fnamep);
15963 }
15964 }
15965
15966 /* ":." - path relative to the current directory */
15967 /* ":~" - path relative to the home directory */
15968 /* ":8" - shortname path - postponed till after */
15969 while (src[*usedlen] == ':'
15970 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
15971 {
15972 *usedlen += 2;
15973 if (c == '8')
15974 {
15975#ifdef WIN3264
15976 has_shortname = 1; /* Postpone this. */
15977#endif
15978 continue;
15979 }
15980 pbuf = NULL;
15981 /* Need full path first (use expand_env() to remove a "~/") */
15982 if (!has_fullname)
15983 {
15984 if (c == '.' && **fnamep == '~')
15985 p = pbuf = expand_env_save(*fnamep);
15986 else
15987 p = pbuf = FullName_save(*fnamep, FALSE);
15988 }
15989 else
15990 p = *fnamep;
15991
15992 has_fullname = 0;
15993
15994 if (p != NULL)
15995 {
15996 if (c == '.')
15997 {
15998 mch_dirname(dirname, MAXPATHL);
15999 s = shorten_fname(p, dirname);
16000 if (s != NULL)
16001 {
16002 *fnamep = s;
16003 if (pbuf != NULL)
16004 {
16005 vim_free(*bufp); /* free any allocated file name */
16006 *bufp = pbuf;
16007 pbuf = NULL;
16008 }
16009 }
16010 }
16011 else
16012 {
16013 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
16014 /* Only replace it when it starts with '~' */
16015 if (*dirname == '~')
16016 {
16017 s = vim_strsave(dirname);
16018 if (s != NULL)
16019 {
16020 *fnamep = s;
16021 vim_free(*bufp);
16022 *bufp = s;
16023 }
16024 }
16025 }
16026 vim_free(pbuf);
16027 }
16028 }
16029
16030 tail = gettail(*fnamep);
16031 *fnamelen = (int)STRLEN(*fnamep);
16032
16033 /* ":h" - head, remove "/file_name", can be repeated */
16034 /* Don't remove the first "/" or "c:\" */
16035 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
16036 {
16037 valid |= VALID_HEAD;
16038 *usedlen += 2;
16039 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016040 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 --tail;
16042 *fnamelen = (int)(tail - *fnamep);
16043#ifdef VMS
16044 if (*fnamelen > 0)
16045 *fnamelen += 1; /* the path separator is part of the path */
16046#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016047 while (tail > s && !after_pathsep(s, tail))
16048 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 }
16050
16051 /* ":8" - shortname */
16052 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
16053 {
16054 *usedlen += 2;
16055#ifdef WIN3264
16056 has_shortname = 1;
16057#endif
16058 }
16059
16060#ifdef WIN3264
16061 /* Check shortname after we have done 'heads' and before we do 'tails'
16062 */
16063 if (has_shortname)
16064 {
16065 pbuf = NULL;
16066 /* Copy the string if it is shortened by :h */
16067 if (*fnamelen < (int)STRLEN(*fnamep))
16068 {
16069 p = vim_strnsave(*fnamep, *fnamelen);
16070 if (p == 0)
16071 return -1;
16072 vim_free(*bufp);
16073 *bufp = *fnamep = p;
16074 }
16075
16076 /* Split into two implementations - makes it easier. First is where
16077 * there isn't a full name already, second is where there is.
16078 */
16079 if (!has_fullname && !vim_isAbsName(*fnamep))
16080 {
16081 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
16082 return -1;
16083 }
16084 else
16085 {
16086 int l;
16087
16088 /* Simple case, already have the full-name
16089 * Nearly always shorter, so try first time. */
16090 l = *fnamelen;
16091 if (!get_short_pathname(fnamep, bufp, &l))
16092 return -1;
16093
16094 if (l == 0)
16095 {
16096 /* Couldn't find the filename.. search the paths.
16097 */
16098 l = *fnamelen;
16099 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
16100 return -1;
16101 }
16102 *fnamelen = l;
16103 }
16104 }
16105#endif /* WIN3264 */
16106
16107 /* ":t" - tail, just the basename */
16108 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
16109 {
16110 *usedlen += 2;
16111 *fnamelen -= (int)(tail - *fnamep);
16112 *fnamep = tail;
16113 }
16114
16115 /* ":e" - extension, can be repeated */
16116 /* ":r" - root, without extension, can be repeated */
16117 while (src[*usedlen] == ':'
16118 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
16119 {
16120 /* find a '.' in the tail:
16121 * - for second :e: before the current fname
16122 * - otherwise: The last '.'
16123 */
16124 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
16125 s = *fnamep - 2;
16126 else
16127 s = *fnamep + *fnamelen - 1;
16128 for ( ; s > tail; --s)
16129 if (s[0] == '.')
16130 break;
16131 if (src[*usedlen + 1] == 'e') /* :e */
16132 {
16133 if (s > tail)
16134 {
16135 *fnamelen += (int)(*fnamep - (s + 1));
16136 *fnamep = s + 1;
16137#ifdef VMS
16138 /* cut version from the extension */
16139 s = *fnamep + *fnamelen - 1;
16140 for ( ; s > *fnamep; --s)
16141 if (s[0] == ';')
16142 break;
16143 if (s > *fnamep)
16144 *fnamelen = s - *fnamep;
16145#endif
16146 }
16147 else if (*fnamep <= tail)
16148 *fnamelen = 0;
16149 }
16150 else /* :r */
16151 {
16152 if (s > tail) /* remove one extension */
16153 *fnamelen = (int)(s - *fnamep);
16154 }
16155 *usedlen += 2;
16156 }
16157
16158 /* ":s?pat?foo?" - substitute */
16159 /* ":gs?pat?foo?" - global substitute */
16160 if (src[*usedlen] == ':'
16161 && (src[*usedlen + 1] == 's'
16162 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
16163 {
16164 char_u *str;
16165 char_u *pat;
16166 char_u *sub;
16167 int sep;
16168 char_u *flags;
16169 int didit = FALSE;
16170
16171 flags = (char_u *)"";
16172 s = src + *usedlen + 2;
16173 if (src[*usedlen + 1] == 'g')
16174 {
16175 flags = (char_u *)"g";
16176 ++s;
16177 }
16178
16179 sep = *s++;
16180 if (sep)
16181 {
16182 /* find end of pattern */
16183 p = vim_strchr(s, sep);
16184 if (p != NULL)
16185 {
16186 pat = vim_strnsave(s, (int)(p - s));
16187 if (pat != NULL)
16188 {
16189 s = p + 1;
16190 /* find end of substitution */
16191 p = vim_strchr(s, sep);
16192 if (p != NULL)
16193 {
16194 sub = vim_strnsave(s, (int)(p - s));
16195 str = vim_strnsave(*fnamep, *fnamelen);
16196 if (sub != NULL && str != NULL)
16197 {
16198 *usedlen = (int)(p + 1 - src);
16199 s = do_string_sub(str, pat, sub, flags);
16200 if (s != NULL)
16201 {
16202 *fnamep = s;
16203 *fnamelen = (int)STRLEN(s);
16204 vim_free(*bufp);
16205 *bufp = s;
16206 didit = TRUE;
16207 }
16208 }
16209 vim_free(sub);
16210 vim_free(str);
16211 }
16212 vim_free(pat);
16213 }
16214 }
16215 /* after using ":s", repeat all the modifiers */
16216 if (didit)
16217 goto repeat;
16218 }
16219 }
16220
16221 return valid;
16222}
16223
16224/*
16225 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
16226 * "flags" can be "g" to do a global substitute.
16227 * Returns an allocated string, NULL for error.
16228 */
16229 char_u *
16230do_string_sub(str, pat, sub, flags)
16231 char_u *str;
16232 char_u *pat;
16233 char_u *sub;
16234 char_u *flags;
16235{
16236 int sublen;
16237 regmatch_T regmatch;
16238 int i;
16239 int do_all;
16240 char_u *tail;
16241 garray_T ga;
16242 char_u *ret;
16243 char_u *save_cpo;
16244
16245 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
16246 save_cpo = p_cpo;
16247 p_cpo = (char_u *)"";
16248
16249 ga_init2(&ga, 1, 200);
16250
16251 do_all = (flags[0] == 'g');
16252
16253 regmatch.rm_ic = p_ic;
16254 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16255 if (regmatch.regprog != NULL)
16256 {
16257 tail = str;
16258 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
16259 {
16260 /*
16261 * Get some space for a temporary buffer to do the substitution
16262 * into. It will contain:
16263 * - The text up to where the match is.
16264 * - The substituted text.
16265 * - The text after the match.
16266 */
16267 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
16268 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
16269 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
16270 {
16271 ga_clear(&ga);
16272 break;
16273 }
16274
16275 /* copy the text up to where the match is */
16276 i = (int)(regmatch.startp[0] - tail);
16277 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
16278 /* add the substituted text */
16279 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
16280 + ga.ga_len + i, TRUE, TRUE, FALSE);
16281 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 /* avoid getting stuck on a match with an empty string */
16283 if (tail == regmatch.endp[0])
16284 {
16285 if (*tail == NUL)
16286 break;
16287 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
16288 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 }
16290 else
16291 {
16292 tail = regmatch.endp[0];
16293 if (*tail == NUL)
16294 break;
16295 }
16296 if (!do_all)
16297 break;
16298 }
16299
16300 if (ga.ga_data != NULL)
16301 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
16302
16303 vim_free(regmatch.regprog);
16304 }
16305
16306 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
16307 ga_clear(&ga);
16308 p_cpo = save_cpo;
16309
16310 return ret;
16311}
16312
16313#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */