blob: f2ce3f3e63a1c5bb9448a72148e1a68260d496f9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
33#if SIZEOF_INT <= 3 /* use long if int is smaller than 32 bits */
34typedef long varnumber_T;
35#else
36typedef int varnumber_T;
37#endif
38
39/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000040 * Structure to hold an internal variable without a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 */
42typedef struct
43{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000044 char v_type; /* see below: VAR_NUMBER, VAR_STRING, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 union
46 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000047 varnumber_T v_number; /* number value */
48 char_u *v_string; /* string value (can be NULL!) */
49 struct listvar_S *v_list; /* list value (can be NULL!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +000050 struct dictvar_S *v_dict; /* dict value (can be NULL!) */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000051 } vval;
52} typeval;
53
54/* Values for "v_type". */
55#define VAR_UNKNOWN 0
56#define VAR_NUMBER 1 /* "v_number" is used */
57#define VAR_STRING 2 /* "v_string" is used */
58#define VAR_FUNC 3 /* "v_string" is function name */
59#define VAR_LIST 4 /* "v_list" is used */
Bram Moolenaar8c711452005-01-14 21:53:12 +000060#define VAR_DICT 5 /* "v_dict" is used */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000061
62/*
63 * Structure to hold an internal variable with a name.
64 * The "tv" must come first, so that this can be used as a "typeval" as well.
65 */
66typedef struct
67{
68 typeval tv; /* type and value of the variable */
69 char_u *v_name; /* name of variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} var;
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072typedef var * VAR;
73
74/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000075 * Structure to hold an item of a list: an internal variable without a name.
76 */
77struct listitem_S
78{
79 struct listitem_S *li_next; /* next item in list */
80 struct listitem_S *li_prev; /* previous item in list */
81 typeval li_tv; /* type and value of the variable */
82};
83
84typedef struct listitem_S listitem;
85
86/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000087 * Struct used by those that are using an item in a list.
88 */
89typedef struct listwatch_S
90{
91 listitem *lw_item; /* item being watched */
92 struct listwatch_S *lw_next; /* next watcher */
93} listwatch;
94
95/*
96 * Structure to hold info about a list.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000097 */
98struct listvar_S
99{
100 int lv_refcount; /* reference count */
101 listitem *lv_first; /* first item, NULL if none */
102 listitem *lv_last; /* last item, NULL if none */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000103 listwatch *lv_watch; /* first watcher, NULL if none */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000104};
105
106typedef struct listvar_S listvar;
107
Bram Moolenaare9a41262005-01-15 22:18:47 +0000108#define VAR_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000109
110/*
111 * Structure to hold an item of a Dictionary.
112 */
113struct dictitem_S
114{
115 struct dictitem_S *di_next; /* next item in list */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000116 char_u *di_key; /* key (never NULL!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000117 typeval di_tv; /* type and value of the variable */
118};
119
120typedef struct dictitem_S dictitem;
121
122/*
123 * Structure to hold info about a Dictionary.
124 */
125struct dictvar_S
126{
127 int dv_refcount; /* reference count */
128 dictitem *dv_first; /* first item, NULL if none */
129};
130
131typedef struct dictvar_S dictvar;
132
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000133/*
134 * Structure returned by get_lval() and used by set_var_lval().
135 * For a plain name:
136 * "name" points to the variable name.
137 * "exp_name" is NULL.
138 * "tv" is NULL
139 * For a magic braces name:
140 * "name" points to the expanded variable name.
141 * "exp_name" is non-NULL, to be freed later.
142 * "tv" is NULL
143 * For an index in a list:
144 * "name" points to the (expanded) variable name.
145 * "exp_name" NULL or non-NULL, to be freed later.
146 * "tv" points to the (first) list item value
147 * "li" points to the (first) list item
148 * "range", "n1", "n2" and "empty2" indicate what items are used.
149 * For an existing Dict item:
150 * "name" points to the (expanded) variable name.
151 * "exp_name" NULL or non-NULL, to be freed later.
152 * "tv" points to the dict item value
153 * "newkey" is NULL
154 * For a non-existing Dict item:
155 * "name" points to the (expanded) variable name.
156 * "exp_name" NULL or non-NULL, to be freed later.
157 * "tv" points to the Dictionary typeval
158 * "newkey" is the key for the new item.
159 */
160typedef struct lval_S
161{
162 char_u *ll_name; /* start of variable name (can be NULL) */
163 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
164 typeval *ll_tv; /* Typeval of item being used. If "newkey"
165 isn't NULL it's the Dict to which to add
166 the item. */
167 listitem *ll_li; /* The list item or NULL. */
168 listvar *ll_list; /* The list or NULL. */
169 int ll_range; /* TRUE when a [i:j] range was used */
170 long ll_n1; /* First index for list */
171 long ll_n2; /* Second index for list range */
172 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000173 dictvar *ll_dict; /* The Dictionary or NULL */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000174 dictitem *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000175 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
176 dictitem **ll_pdi; /* di_next field pointing to found dictitem */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000177} lval;
178
Bram Moolenaar8c711452005-01-14 21:53:12 +0000179
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000181static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000182static char *e_undefvar = N_("E121: Undefined variable: %s");
183static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000184static char *e_intern2 = N_("E685: Internal error: %s");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000185static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000187static char *e_emptykey = N_("E713: Empty key in Dictionary");
188static char *e_listreq = N_("E714: List required");
189static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000190static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000191static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
192static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
193static char *e_funcdict = N_("E717: Dictionary entry already exists");
194static char *e_funcref = N_("E718: Funcref required");
195static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
196static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000197
198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 * All user-defined global variables are stored in "variables".
200 */
201garray_T variables = {0, 0, sizeof(var), 4, NULL};
202
203/*
204 * Array to hold an array with variables local to each sourced script.
205 */
206static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
207#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
208
209
210#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
Bram Moolenaare9a41262005-01-15 22:18:47 +0000211#define VAR_GAP_ENTRY(idx, gap) (((VAR)((gap)->ga_data))[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
213#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
214
215static int echo_attr = 0; /* attributes used for ":echo" */
216
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000217/* Values for trans_function_name() argument: */
218#define TFN_INT 1 /* internal function name OK */
219#define TFN_QUIET 2 /* no error messages */
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/*
222 * Structure to hold info for a user function.
223 */
224typedef struct ufunc ufunc_T;
225
226struct ufunc
227{
228 ufunc_T *next; /* next function in list */
229 char_u *name; /* name of function; can start with <SNR>123_
230 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
231 int varargs; /* variable nr of arguments */
232 int flags;
233 int calls; /* nr of active calls */
234 garray_T args; /* arguments */
235 garray_T lines; /* function lines */
236 scid_T script_ID; /* ID of script where function was defined,
237 used for s: variables */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000238 int refcount; /* for numbered function: reference count */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239};
240
241/* function flags */
242#define FC_ABORT 1 /* abort function on error */
243#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000244#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245
246/*
247 * All user-defined functions are found in the forward-linked function list.
248 * The first function is pointed at by firstfunc.
249 */
250ufunc_T *firstfunc = NULL;
251
252#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
253#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
254
255/* structure to hold info for a function that is currently being executed. */
256struct funccall
257{
258 ufunc_T *func; /* function being called */
259 int linenr; /* next line to be executed */
260 int returned; /* ":return" used */
261 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000262 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 var a0_var; /* "a:0" variable */
264 var firstline; /* "a:firstline" variable */
265 var lastline; /* "a:lastline" variable */
266 garray_T l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000267 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 linenr_T breakpoint; /* next line with breakpoint or zero */
269 int dbg_tick; /* debug_tick when breakpoint was set */
270 int level; /* top nesting level of executed function */
271};
272
273/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274 * Info used by a ":for" loop.
275 */
276typedef struct forinfo_S
277{
278 int fi_semicolon; /* TRUE if ending in '; var]' */
279 int fi_varcount; /* nr of variables in the list */
280 listwatch fi_lw; /* keep an eye on the item used. */
281 listvar *fi_list; /* list being used */
282} forinfo;
283
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000284/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000285 * Struct used by trans_function_name()
286 */
287typedef struct
288{
289 dictvar *fd_dict; /* Dictionary used */
290 char_u *fd_newkey; /* new key in "dict" */
291 dictitem *fd_di; /* Dictionary item used */
292 dictitem **fd_pdi; /* field that points to "fd_di" */
293} funcdict;
294
295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 * Return the name of the executed function.
297 */
298 char_u *
299func_name(cookie)
300 void *cookie;
301{
302 return ((struct funccall *)cookie)->func->name;
303}
304
305/*
306 * Return the address holding the next breakpoint line for a funccall cookie.
307 */
308 linenr_T *
309func_breakpoint(cookie)
310 void *cookie;
311{
312 return &((struct funccall *)cookie)->breakpoint;
313}
314
315/*
316 * Return the address holding the debug tick for a funccall cookie.
317 */
318 int *
319func_dbg_tick(cookie)
320 void *cookie;
321{
322 return &((struct funccall *)cookie)->dbg_tick;
323}
324
325/*
326 * Return the nesting level for a funccall cookie.
327 */
328 int
329func_level(cookie)
330 void *cookie;
331{
332 return ((struct funccall *)cookie)->level;
333}
334
335/* pointer to funccal for currently active function */
336struct funccall *current_funccal = NULL;
337
338/*
339 * Return TRUE when a function was ended by a ":return" command.
340 */
341 int
342current_func_returned()
343{
344 return current_funccal->returned;
345}
346
347
348/*
349 * Array to hold the value of v: variables.
350 */
351#include "version.h"
352
353/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000354#define VV_COMPAT 1 /* compatible, also used without "v:" */
355#define VV_RO 2 /* read-only */
356#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaare9a41262005-01-15 22:18:47 +0000358#define VV_NAME(s) s, sizeof(s) - 1
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360struct vimvar
361{
362 char *name; /* name of variable, without v: */
363 int len; /* length of name */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000364 typeval tv; /* type and value */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000365 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366} vimvars[VV_LEN] =
Bram Moolenaare9a41262005-01-15 22:18:47 +0000367{
368 /*
369 * The order here must match the VV_ defines in vim.h!
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000370 * Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare9a41262005-01-15 22:18:47 +0000371 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000372 {VV_NAME("count"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
373 {VV_NAME("count1"), {VAR_NUMBER}, VV_RO},
374 {VV_NAME("prevcount"), {VAR_NUMBER}, VV_RO},
375 {VV_NAME("errmsg"), {VAR_STRING}, VV_COMPAT},
376 {VV_NAME("warningmsg"), {VAR_STRING}, 0},
377 {VV_NAME("statusmsg"), {VAR_STRING}, 0},
378 {VV_NAME("shell_error"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
379 {VV_NAME("this_session"), {VAR_STRING}, VV_COMPAT},
380 {VV_NAME("version"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
381 {VV_NAME("lnum"), {VAR_NUMBER}, VV_RO_SBX},
382 {VV_NAME("termresponse"), {VAR_STRING}, VV_RO},
383 {VV_NAME("fname"), {VAR_STRING}, VV_RO},
384 {VV_NAME("lang"), {VAR_STRING}, VV_RO},
385 {VV_NAME("lc_time"), {VAR_STRING}, VV_RO},
386 {VV_NAME("ctype"), {VAR_STRING}, VV_RO},
387 {VV_NAME("charconvert_from"), {VAR_STRING}, VV_RO},
388 {VV_NAME("charconvert_to"), {VAR_STRING}, VV_RO},
389 {VV_NAME("fname_in"), {VAR_STRING}, VV_RO},
390 {VV_NAME("fname_out"), {VAR_STRING}, VV_RO},
391 {VV_NAME("fname_new"), {VAR_STRING}, VV_RO},
392 {VV_NAME("fname_diff"), {VAR_STRING}, VV_RO},
393 {VV_NAME("cmdarg"), {VAR_STRING}, VV_RO},
394 {VV_NAME("foldstart"), {VAR_NUMBER}, VV_RO_SBX},
395 {VV_NAME("foldend"), {VAR_NUMBER}, VV_RO_SBX},
396 {VV_NAME("folddashes"), {VAR_STRING}, VV_RO_SBX},
397 {VV_NAME("foldlevel"), {VAR_NUMBER}, VV_RO_SBX},
398 {VV_NAME("progname"), {VAR_STRING}, VV_RO},
399 {VV_NAME("servername"), {VAR_STRING}, VV_RO},
400 {VV_NAME("dying"), {VAR_NUMBER}, VV_RO},
401 {VV_NAME("exception"), {VAR_STRING}, VV_RO},
402 {VV_NAME("throwpoint"), {VAR_STRING}, VV_RO},
403 {VV_NAME("register"), {VAR_STRING}, VV_RO},
404 {VV_NAME("cmdbang"), {VAR_NUMBER}, VV_RO},
405 {VV_NAME("insertmode"), {VAR_STRING}, VV_RO},
406 {VV_NAME("val"), {VAR_UNKNOWN}, VV_RO},
407 {VV_NAME("key"), {VAR_UNKNOWN}, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408};
409
Bram Moolenaare9a41262005-01-15 22:18:47 +0000410/* shorthand */
411#define vv_nr tv.vval.v_number
412#define vv_str tv.vval.v_string
413
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000414static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
415static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
416static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
417static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
418static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
419static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
420static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
421static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
422static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
423static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
424static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
425static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
426static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000427static listvar *list_alloc __ARGS((void));
428static void list_unref __ARGS((listvar *l));
429static void list_free __ARGS((listvar *l));
430static listitem *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000432static void listitem_remove __ARGS((listvar *l, listitem *item));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000433static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000434static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000435static int dict_equal __ARGS((dictvar *d1, dictvar *d2, int ic));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000436static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000437static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000438static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000439static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000440static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000441static void list_append __ARGS((listvar *l, listitem *item));
442static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000443static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
444static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
445static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000446static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000447static void list_remove __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000448static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000449static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
450
Bram Moolenaar8c711452005-01-14 21:53:12 +0000451static dictvar *dict_alloc __ARGS((void));
452static void dict_unref __ARGS((dictvar *d));
453static void dict_free __ARGS((dictvar *d));
454static dictitem *dictitem_alloc __ARGS((void));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000455static dictitem *dictitem_copy __ARGS((dictitem *org));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000456static void dictitem_free __ARGS((dictitem *item));
457static void dict_add __ARGS((dictvar *d, dictitem *item));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000458static long dict_len __ARGS((dictvar *d));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000459static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len, dictitem ***pdi));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000460static char_u *dict2string __ARGS((typeval *tv));
461static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
462
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000463static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000464static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000465static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000466static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000468static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000469static 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));
470static 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 +0000471
472static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000473static void f_append __ARGS((typeval *argvars, typeval *rettv));
474static void f_argc __ARGS((typeval *argvars, typeval *rettv));
475static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
476static void f_argv __ARGS((typeval *argvars, typeval *rettv));
477static void f_browse __ARGS((typeval *argvars, typeval *rettv));
478static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000479static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
480static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
481static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000482static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
483static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
484static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
485static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
486static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000487static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000488static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
489static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
490static void f_col __ARGS((typeval *argvars, typeval *rettv));
491static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
492static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000493static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000494static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
495static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
496static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
497static void f_delete __ARGS((typeval *argvars, typeval *rettv));
498static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
499static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
500static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000501static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000502static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000503static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000504static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
505static void f_executable __ARGS((typeval *argvars, typeval *rettv));
506static void f_exists __ARGS((typeval *argvars, typeval *rettv));
507static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000508static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000509static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
510static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000511static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000512static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
513static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000514static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
515static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
516static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000517static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
518static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
519static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
520static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
521static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000522static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000523static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
524static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
525static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
526static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
527static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
528static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
529static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
530static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
531static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
532static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
533static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
534static void f_getline __ARGS((typeval *argvars, typeval *rettv));
535static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
536static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
537static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
538static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
539static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
540static void f_glob __ARGS((typeval *argvars, typeval *rettv));
541static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
542static void f_has __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000543static void f_has_key __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
545static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
546static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
547static void f_histget __ARGS((typeval *argvars, typeval *rettv));
548static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000549static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000550static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000551static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
552static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
553static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000554static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000555static void f_input __ARGS((typeval *argvars, typeval *rettv));
556static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
557static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
558static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
559static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000560static void f_insert __ARGS((typeval *argvars, typeval *rettv));
561static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000562static void f_items __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000563static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000564static void f_keys __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000565static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
566static void f_len __ARGS((typeval *argvars, typeval *rettv));
567static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
568static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000569static void f_line __ARGS((typeval *argvars, typeval *rettv));
570static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
571static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
572static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000573static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000574static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
575static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000576static void f_match __ARGS((typeval *argvars, typeval *rettv));
577static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
578static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000579static void f_max __ARGS((typeval *argvars, typeval *rettv));
580static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000581static void f_mode __ARGS((typeval *argvars, typeval *rettv));
582static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
583static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
584static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000585static void f_range __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
587static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
588static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
589static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
590static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000591static void f_remove __ARGS((typeval *argvars, typeval *rettv));
592static void f_rename __ARGS((typeval *argvars, typeval *rettv));
593static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
594static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
595static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
596static void f_search __ARGS((typeval *argvars, typeval *rettv));
597static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
599static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000600static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
601static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000602static void f_setline __ARGS((typeval *argvars, typeval *rettv));
603static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000604static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000605static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000606static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000607static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000608#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000609static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000610#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000611static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
612static void f_string __ARGS((typeval *argvars, typeval *rettv));
613static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
614static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
615static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
616static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000617static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
618static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000619static void f_synID __ARGS((typeval *argvars, typeval *rettv));
620static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
621static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
622static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000623static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
624static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
625static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
626static void f_tr __ARGS((typeval *argvars, typeval *rettv));
627static void f_type __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000628static void f_values __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000629static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
630static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
631static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
632static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
633static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
634static void f_winline __ARGS((typeval *argvars, typeval *rettv));
635static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
636static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
637static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000638
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000639static win_T *find_win_by_nr __ARGS((typeval *vp));
640static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641static int get_env_len __ARGS((char_u **arg));
642static int get_id_len __ARGS((char_u **arg));
643static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000644static 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 +0000645static int eval_isnamec __ARGS((int c));
646static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000647static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
648static typeval *alloc_tv __ARGS((void));
649static typeval *alloc_string_tv __ARGS((char_u *string));
650static void free_tv __ARGS((typeval *varp));
651static void clear_tv __ARGS((typeval *varp));
652static void init_tv __ARGS((typeval *varp));
653static long get_tv_number __ARGS((typeval *varp));
654static linenr_T get_tv_lnum __ARGS((typeval *argvars));
655static char_u *get_tv_string __ARGS((typeval *varp));
656static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657static VAR find_var __ARGS((char_u *name, int writing));
658static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
659static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000660static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661static void list_one_var __ARGS((VAR v, char_u *prefix));
662static void list_vim_var __ARGS((int i));
663static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000664static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000665static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000666static void item_copy __ARGS((typeval *from, typeval *to, int deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000668static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict *fd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669static int eval_fname_script __ARGS((char_u *p));
670static int eval_fname_sid __ARGS((char_u *p));
671static void list_func_head __ARGS((ufunc_T *fp, int indent));
672static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
673static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000674static int function_exists __ARGS((char_u *name));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000675static void func_free __ARGS((ufunc_T *fp));
676static void func_unref __ARGS((char_u *name));
677static void func_ref __ARGS((char_u *name));
Bram Moolenaare9a41262005-01-15 22:18:47 +0000678static 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 +0000679
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000680#define get_var_string(p) get_tv_string(&(p)->tv)
681#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
682#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684static 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 +0000685
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000686static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
687static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
688static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689static void list_all_vars __ARGS((void));
690static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000691static 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 +0000692static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000693static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet));
694static void clear_lval __ARGS((lval *lp));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000695static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy, char_u *op));
696static int tv_op __ARGS((typeval *tv1, typeval *tv2, char_u *op));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000697static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000698static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
699static void list_fix_watch __ARGS((listvar *l, listitem *item));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000700static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702/*
703 * Set an internal variable to a string value. Creates the variable if it does
704 * not already exist.
705 */
706 void
707set_internal_string_var(name, value)
708 char_u *name;
709 char_u *value;
710{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000711 char_u *val;
712 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
714 val = vim_strsave(value);
715 if (val != NULL)
716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000718 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000720 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000721 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723 }
724}
725
726# if defined(FEAT_MBYTE) || defined(PROTO)
727 int
728eval_charconvert(enc_from, enc_to, fname_from, fname_to)
729 char_u *enc_from;
730 char_u *enc_to;
731 char_u *fname_from;
732 char_u *fname_to;
733{
734 int err = FALSE;
735
736 set_vim_var_string(VV_CC_FROM, enc_from, -1);
737 set_vim_var_string(VV_CC_TO, enc_to, -1);
738 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
739 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
740 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
741 err = TRUE;
742 set_vim_var_string(VV_CC_FROM, NULL, -1);
743 set_vim_var_string(VV_CC_TO, NULL, -1);
744 set_vim_var_string(VV_FNAME_IN, NULL, -1);
745 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
746
747 if (err)
748 return FAIL;
749 return OK;
750}
751# endif
752
753# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
754 int
755eval_printexpr(fname, args)
756 char_u *fname;
757 char_u *args;
758{
759 int err = FALSE;
760
761 set_vim_var_string(VV_FNAME_IN, fname, -1);
762 set_vim_var_string(VV_CMDARG, args, -1);
763 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
764 err = TRUE;
765 set_vim_var_string(VV_FNAME_IN, NULL, -1);
766 set_vim_var_string(VV_CMDARG, NULL, -1);
767
768 if (err)
769 {
770 mch_remove(fname);
771 return FAIL;
772 }
773 return OK;
774}
775# endif
776
777# if defined(FEAT_DIFF) || defined(PROTO)
778 void
779eval_diff(origfile, newfile, outfile)
780 char_u *origfile;
781 char_u *newfile;
782 char_u *outfile;
783{
784 int err = FALSE;
785
786 set_vim_var_string(VV_FNAME_IN, origfile, -1);
787 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
788 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
789 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
790 set_vim_var_string(VV_FNAME_IN, NULL, -1);
791 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
792 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
793}
794
795 void
796eval_patch(origfile, difffile, outfile)
797 char_u *origfile;
798 char_u *difffile;
799 char_u *outfile;
800{
801 int err;
802
803 set_vim_var_string(VV_FNAME_IN, origfile, -1);
804 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
805 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
806 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
807 set_vim_var_string(VV_FNAME_IN, NULL, -1);
808 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
809 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
810}
811# endif
812
813/*
814 * Top level evaluation function, returning a boolean.
815 * Sets "error" to TRUE if there was an error.
816 * Return TRUE or FALSE.
817 */
818 int
819eval_to_bool(arg, error, nextcmd, skip)
820 char_u *arg;
821 int *error;
822 char_u **nextcmd;
823 int skip; /* only parse, don't execute */
824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 int retval = FALSE;
827
828 if (skip)
829 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 else
833 {
834 *error = FALSE;
835 if (!skip)
836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837 retval = (get_tv_number(&tv) != 0);
838 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 }
841 if (skip)
842 --emsg_skip;
843
844 return retval;
845}
846
847/*
848 * Top level evaluation function, returning a string. If "skip" is TRUE,
849 * only parsing to "nextcmd" is done, without reporting errors. Return
850 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
851 */
852 char_u *
853eval_to_string_skip(arg, nextcmd, skip)
854 char_u *arg;
855 char_u **nextcmd;
856 int skip; /* only parse, don't execute */
857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 char_u *retval;
860
861 if (skip)
862 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 retval = NULL;
865 else
866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867 retval = vim_strsave(get_tv_string(&tv));
868 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 if (skip)
871 --emsg_skip;
872
873 return retval;
874}
875
876/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000877 * Skip over an expression at "*pp".
878 * Return FAIL for an error, OK otherwise.
879 */
880 int
881skip_expr(pp)
882 char_u **pp;
883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000885
886 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000888}
889
890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 * Top level evaluation function, returning a string.
892 * Return pointer to allocated memory, or NULL for failure.
893 */
894 char_u *
895eval_to_string(arg, nextcmd)
896 char_u *arg;
897 char_u **nextcmd;
898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 char_u *retval;
901
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 retval = NULL;
904 else
905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000906 retval = vim_strsave(get_tv_string(&tv));
907 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909
910 return retval;
911}
912
913/*
914 * Call eval_to_string() with "sandbox" set and not using local variables.
915 */
916 char_u *
917eval_to_string_safe(arg, nextcmd)
918 char_u *arg;
919 char_u **nextcmd;
920{
921 char_u *retval;
922 void *save_funccalp;
923
924 save_funccalp = save_funccal();
925 ++sandbox;
926 retval = eval_to_string(arg, nextcmd);
927 --sandbox;
928 restore_funccal(save_funccalp);
929 return retval;
930}
931
932#if 0 /* not used */
933/*
934 * Top level evaluation function, returning a string.
935 * Advances "arg" to the first non-blank after the evaluated expression.
936 * Return pointer to allocated memory, or NULL for failure.
937 * Doesn't give error messages.
938 */
939 char_u *
940eval_arg_to_string(arg)
941 char_u **arg;
942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 char_u *retval;
945 int ret;
946
947 ++emsg_off;
948
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 if (ret == FAIL)
951 retval = NULL;
952 else
953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 retval = vim_strsave(get_tv_string(&rettv));
955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
958 --emsg_off;
959
960 return retval;
961}
962#endif
963
964/*
965 * Top level evaluation function, returning a number.
966 * Evaluates "expr" silently.
967 * Returns -1 for an error.
968 */
969 int
970eval_to_number(expr)
971 char_u *expr;
972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000975 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 ++emsg_off;
978
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000979 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 retval = -1;
981 else
982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000983 retval = get_tv_number(&rettv);
984 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986 --emsg_off;
987
988 return retval;
989}
990
991#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
992/*
993 * Call some vimL function and return the result as a string
994 * Uses argv[argc] for the function arguments.
995 */
996 char_u *
997call_vim_function(func, argc, argv, safe)
998 char_u *func;
999 int argc;
1000 char_u **argv;
1001 int safe; /* use the sandbox */
1002{
1003 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001004 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 long n;
1007 int len;
1008 int i;
1009 int doesrange;
1010 void *save_funccalp = NULL;
1011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (argvars == NULL)
1014 return NULL;
1015
1016 for (i = 0; i < argc; i++)
1017 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001018 /* Pass a NULL or empty argument as an empty string */
1019 if (argv[i] == NULL || *argv[i] == NUL)
1020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 argvars[i].v_type = VAR_STRING;
1022 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001023 continue;
1024 }
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /* Recognize a number argument, the others must be strings. */
1027 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1028 if (len != 0 && len == (int)STRLEN(argv[i]))
1029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 argvars[i].v_type = VAR_NUMBER;
1031 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 else
1034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001035 argvars[i].v_type = VAR_STRING;
1036 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 }
1039
1040 if (safe)
1041 {
1042 save_funccalp = save_funccal();
1043 ++sandbox;
1044 }
1045
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001046 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1047 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001049 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001050 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001052 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 vim_free(argvars);
1054
1055 if (safe)
1056 {
1057 --sandbox;
1058 restore_funccal(save_funccalp);
1059 }
1060 return retval;
1061}
1062#endif
1063
1064/*
1065 * Save the current function call pointer, and set it to NULL.
1066 * Used when executing autocommands and for ":source".
1067 */
1068 void *
1069save_funccal()
1070{
1071 struct funccall *fc;
1072
1073 fc = current_funccal;
1074 current_funccal = NULL;
1075 return (void *)fc;
1076}
1077
1078 void
1079restore_funccal(fc)
1080 void *fc;
1081{
1082 current_funccal = (struct funccall *)fc;
1083}
1084
1085#ifdef FEAT_FOLDING
1086/*
1087 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1088 * it in "*cp". Doesn't give error messages.
1089 */
1090 int
1091eval_foldexpr(arg, cp)
1092 char_u *arg;
1093 int *cp;
1094{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int retval;
1097 char_u *s;
1098
1099 ++emsg_off;
1100 ++sandbox;
1101 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 retval = 0;
1104 else
1105 {
1106 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001107 if (tv.v_type == VAR_NUMBER)
1108 retval = tv.vval.v_number;
1109 else if (tv.v_type == VAR_UNKNOWN
1110 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 retval = 0;
1112 else
1113 {
1114 /* If the result is a string, check if there is a non-digit before
1115 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (!VIM_ISDIGIT(*s) && *s != '-')
1118 *cp = *s++;
1119 retval = atol((char *)s);
1120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 --emsg_off;
1124 --sandbox;
1125
1126 return retval;
1127}
1128#endif
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130/*
1131 * Expands out the 'magic' {}'s in a variable/function name.
1132 * Note that this can call itself recursively, to deal with
1133 * constructs like foo{bar}{baz}{bam}
1134 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1135 * "in_start" ^
1136 * "expr_start" ^
1137 * "expr_end" ^
1138 * "in_end" ^
1139 *
1140 * Returns a new allocated string, which the caller must free.
1141 * Returns NULL for failure.
1142 */
1143 static char_u *
1144make_expanded_name(in_start, expr_start, expr_end, in_end)
1145 char_u *in_start;
1146 char_u *expr_start;
1147 char_u *expr_end;
1148 char_u *in_end;
1149{
1150 char_u c1;
1151 char_u *retval = NULL;
1152 char_u *temp_result;
1153 char_u *nextcmd = NULL;
1154
1155 if (expr_end == NULL || in_end == NULL)
1156 return NULL;
1157 *expr_start = NUL;
1158 *expr_end = NUL;
1159 c1 = *in_end;
1160 *in_end = NUL;
1161
1162 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1163 if (temp_result != NULL && nextcmd == NULL)
1164 {
1165 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1166 + (in_end - expr_end) + 1));
1167
1168 if (retval != NULL)
1169 {
1170 STRCPY(retval, in_start);
1171 STRCAT(retval, temp_result);
1172 STRCAT(retval, expr_end + 1);
1173 }
1174 }
1175 vim_free(temp_result);
1176
1177 *in_end = c1; /* put char back for error messages */
1178 *expr_start = '{';
1179 *expr_end = '}';
1180
1181 if (retval != NULL)
1182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (expr_start != NULL)
1185 {
1186 /* Further expansion! */
1187 temp_result = make_expanded_name(retval, expr_start,
1188 expr_end, temp_result);
1189 vim_free(retval);
1190 retval = temp_result;
1191 }
1192 }
1193
1194 return retval;
1195
1196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 * ":let" list all variable values
1200 * ":let var1 var2" list variable values
1201 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001202 * ":let var += expr" assignment command.
1203 * ":let var -= expr" assignment command.
1204 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
1207 void
1208ex_let(eap)
1209 exarg_T *eap;
1210{
1211 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 char_u *expr = NULL;
1213 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 int var_count = 0;
1216 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001217 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001219 expr = skip_var_list(arg, &var_count, &semicolon);
1220 if (expr == NULL)
1221 return;
1222 expr = vim_strchr(expr, '=');
1223 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001225 if (*arg == '[')
1226 EMSG(_(e_invarg));
1227 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 /* ":let var1 var2" */
1229 arg = list_arg_vars(eap, arg);
1230 else if (!eap->skip)
1231 /* ":let" */
1232 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 eap->nextcmd = check_nextcmd(arg);
1234 }
1235 else
1236 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 op[0] = '=';
1238 op[1] = NUL;
1239 if (expr > arg)
1240 {
1241 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1242 op[0] = expr[-1]; /* +=, -= or .= */
1243 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (eap->skip)
1247 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001248 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (eap->skip)
1250 {
1251 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 --emsg_skip;
1254 }
1255 else if (i != FAIL)
1256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001257 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001258 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 }
1262}
1263
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001264/*
1265 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1266 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001267 * When "nextchars" is not NULL it points to a string with characters that
1268 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1269 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001270 * Returns OK or FAIL;
1271 */
1272 static int
1273ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1274 char_u *arg_start;
1275 typeval *tv;
1276 int copy; /* copy values from "tv", don't move */
1277 int semicolon; /* from skip_var_list() */
1278 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001279 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001280{
1281 char_u *arg = arg_start;
1282 listvar *l;
1283 int i;
1284 listitem *item;
1285 typeval ltv;
1286
1287 if (*arg != '[')
1288 {
1289 /*
1290 * ":let var = expr" or ":for var in list"
1291 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001292 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001293 return FAIL;
1294 return OK;
1295 }
1296
1297 /*
1298 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1299 */
1300 l = tv->vval.v_list;
1301 if (tv->v_type != VAR_LIST || l == NULL)
1302 {
1303 EMSG(_(e_listreq));
1304 return FAIL;
1305 }
1306
1307 i = list_len(l);
1308 if (semicolon == 0 && var_count < i)
1309 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001310 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001311 return FAIL;
1312 }
1313 if (var_count - semicolon > i)
1314 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001315 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001316 return FAIL;
1317 }
1318
1319 item = l->lv_first;
1320 while (*arg != ']')
1321 {
1322 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001323 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 item = item->li_next;
1325 if (arg == NULL)
1326 return FAIL;
1327
1328 arg = skipwhite(arg);
1329 if (*arg == ';')
1330 {
1331 /* Put the rest of the list (may be empty) in the var after ';'.
1332 * Create a new list for this. */
1333 l = list_alloc();
1334 if (l == NULL)
1335 return FAIL;
1336 while (item != NULL)
1337 {
1338 list_append_tv(l, &item->li_tv);
1339 item = item->li_next;
1340 }
1341
1342 ltv.v_type = VAR_LIST;
1343 ltv.vval.v_list = l;
1344 l->lv_refcount = 1;
1345
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1347 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001348 clear_tv(&ltv);
1349 if (arg == NULL)
1350 return FAIL;
1351 break;
1352 }
1353 else if (*arg != ',' && *arg != ']')
1354 {
1355 EMSG2(_(e_intern2), "ex_let_vars()");
1356 return FAIL;
1357 }
1358 }
1359
1360 return OK;
1361}
1362
1363/*
1364 * Skip over assignable variable "var" or list of variables "[var, var]".
1365 * Used for ":let varvar = expr" and ":for varvar in expr".
1366 * For "[var, var]" increment "*var_count" for each variable.
1367 * for "[var, var; var]" set "semicolon".
1368 * Return NULL for an error.
1369 */
1370 static char_u *
1371skip_var_list(arg, var_count, semicolon)
1372 char_u *arg;
1373 int *var_count;
1374 int *semicolon;
1375{
1376 char_u *p, *s;
1377
1378 if (*arg == '[')
1379 {
1380 /* "[var, var]": find the matching ']'. */
1381 p = arg;
1382 while (1)
1383 {
1384 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1385 s = skip_var_one(p);
1386 if (s == p)
1387 {
1388 EMSG2(_(e_invarg2), p);
1389 return NULL;
1390 }
1391 ++*var_count;
1392
1393 p = skipwhite(s);
1394 if (*p == ']')
1395 break;
1396 else if (*p == ';')
1397 {
1398 if (*semicolon == 1)
1399 {
1400 EMSG(_("Double ; in list of variables"));
1401 return NULL;
1402 }
1403 *semicolon = 1;
1404 }
1405 else if (*p != ',')
1406 {
1407 EMSG2(_(e_invarg2), p);
1408 return NULL;
1409 }
1410 }
1411 return p + 1;
1412 }
1413 else
1414 return skip_var_one(arg);
1415}
1416
1417 static char_u *
1418skip_var_one(arg)
1419 char_u *arg;
1420{
1421 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1422 ++arg;
1423 return find_name_end(arg, NULL, NULL, TRUE);
1424}
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 static void
1427list_all_vars()
1428{
1429 int i;
1430
1431 /*
1432 * List all variables.
1433 */
1434 for (i = 0; i < variables.ga_len && !got_int; ++i)
1435 if (VAR_ENTRY(i).v_name != NULL)
1436 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1437 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1438 if (BVAR_ENTRY(i).v_name != NULL)
1439 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1440 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1441 if (WVAR_ENTRY(i).v_name != NULL)
1442 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1443 for (i = 0; i < VV_LEN && !got_int; ++i)
Bram Moolenaare9a41262005-01-15 22:18:47 +00001444 if (vimvars[i].tv.v_type == VAR_NUMBER || vimvars[i].vv_str != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 list_vim_var(i);
1446}
1447
1448/*
1449 * List variables in "arg".
1450 */
1451 static char_u *
1452list_arg_vars(eap, arg)
1453 exarg_T *eap;
1454 char_u *arg;
1455{
1456 int error = FALSE;
1457 char_u *temp_string = NULL;
1458 int arg_len;
1459 char_u *expr_start;
1460 char_u *expr_end;
1461 char_u *name_end;
1462 int c1 = 0, c2;
1463 int i;
1464 VAR varp;
1465 char_u *name;
1466
1467 while (!ends_excmd(*arg) && !got_int)
1468 {
1469 /* Find the end of the name. */
1470 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1471
1472 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1473 {
1474 emsg_severe = TRUE;
1475 EMSG(_(e_trailing));
1476 break;
1477 }
1478 if (!error && !eap->skip)
1479 {
1480 if (expr_start != NULL)
1481 {
1482 temp_string = make_expanded_name(arg, expr_start,
1483 expr_end, name_end);
1484 if (temp_string == NULL)
1485 {
1486 /*
1487 * Report an invalid expression in braces, unless
1488 * the expression evaluation has been cancelled due
1489 * to an aborting error, an interrupt, or an
1490 * exception.
1491 */
1492 if (!aborting())
1493 {
1494 emsg_severe = TRUE;
1495 EMSG2(_(e_invarg2), arg);
1496 break;
1497 }
1498 error = TRUE;
1499 arg = skipwhite(name_end);
1500 continue;
1501 }
1502 arg = temp_string;
1503 arg_len = STRLEN(temp_string);
1504 }
1505 else
1506 {
1507 c1 = *name_end;
1508 *name_end = NUL;
1509 arg_len = (int)(name_end - arg);
1510 }
1511 i = find_vim_var(arg, arg_len);
1512 if (i >= 0)
1513 list_vim_var(i);
1514 else if (STRCMP("b:changedtick", arg) == 0)
1515 {
1516 char_u numbuf[NUMBUFLEN];
1517
1518 sprintf((char *)numbuf, "%ld",
1519 (long)curbuf->b_changedtick);
1520 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1521 VAR_NUMBER, numbuf);
1522 }
1523 else
1524 {
1525 varp = find_var(arg, FALSE);
1526 if (varp == NULL)
1527 {
1528 /* Skip further arguments but do continue to
1529 * search for a trailing command. */
1530 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1531 error = TRUE;
1532 }
1533 else
1534 {
1535 name = vim_strchr(arg, ':');
1536 if (name != NULL)
1537 {
1538 /* "a:" vars have no name stored, use whole arg */
1539 if (arg[0] == 'a' && arg[1] == ':')
1540 c2 = NUL;
1541 else
1542 {
1543 c2 = *++name;
1544 *name = NUL;
1545 }
1546 list_one_var(varp, arg);
1547 if (c2 != NUL)
1548 *name = c2;
1549 }
1550 else
1551 list_one_var(varp, (char_u *)"");
1552 }
1553 }
1554 if (expr_start != NULL)
1555 vim_free(temp_string);
1556 else
1557 *name_end = c1;
1558 }
1559 arg = skipwhite(name_end);
1560 }
1561
1562 return arg;
1563}
1564
1565/*
1566 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1567 * Returns a pointer to the char just after the var name.
1568 * Returns NULL if there is an error.
1569 */
1570 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 char_u *arg; /* points to variable name */
1573 typeval *tv; /* value to assign to variable */
1574 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577{
1578 int c1;
1579 char_u *name;
1580 char_u *p;
1581 char_u *arg_end = NULL;
1582 int len;
1583 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585
1586 /*
1587 * ":let $VAR = expr": Set environment variable.
1588 */
1589 if (*arg == '$')
1590 {
1591 /* Find the end of the name. */
1592 ++arg;
1593 name = arg;
1594 len = get_env_len(&arg);
1595 if (len == 0)
1596 EMSG2(_(e_invarg2), name - 1);
1597 else
1598 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 if (op != NULL && (*op == '+' || *op == '-'))
1600 EMSG2(_(e_letwrong), op);
1601 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 EMSG(_(e_letunexp));
1604 else
1605 {
1606 c1 = name[len];
1607 name[len] = NUL;
1608 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 if (op != NULL && *op == '.')
1610 {
1611 int mustfree = FALSE;
1612 char_u *s = vim_getenv(name, &mustfree);
1613
1614 if (s != NULL)
1615 {
1616 p = tofree = concat_str(s, p);
1617 if (mustfree)
1618 vim_free(s);
1619 }
1620 }
1621 if (p != NULL)
1622 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623 if (STRICMP(name, "HOME") == 0)
1624 init_homedir();
1625 else if (didset_vim && STRICMP(name, "VIM") == 0)
1626 didset_vim = FALSE;
1627 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1628 didset_vimruntime = FALSE;
1629 name[len] = c1;
1630 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 }
1633 }
1634 }
1635
1636 /*
1637 * ":let &option = expr": Set option value.
1638 * ":let &l:option = expr": Set local option value.
1639 * ":let &g:option = expr": Set global option value.
1640 */
1641 else if (*arg == '&')
1642 {
1643 /* Find the end of the name. */
1644 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 if (p == NULL || (endchars != NULL
1646 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 EMSG(_(e_letunexp));
1648 else
1649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650 long n;
1651 int opt_type;
1652 long numval;
1653 char_u *stringval = NULL;
1654 char_u *s;
1655
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 c1 = *p;
1657 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658
1659 n = get_tv_number(tv);
1660 s = get_tv_string(tv);
1661 if (op != NULL && *op != '=')
1662 {
1663 opt_type = get_option_value(arg, &numval,
1664 &stringval, opt_flags);
1665 if ((opt_type == 1 && *op == '.')
1666 || (opt_type == 0 && *op != '.'))
1667 EMSG2(_(e_letwrong), op);
1668 else
1669 {
1670 if (opt_type == 1) /* number */
1671 {
1672 if (*op == '+')
1673 n = numval + n;
1674 else
1675 n = numval - n;
1676 }
1677 else if (opt_type == 0 && stringval != NULL) /* string */
1678 {
1679 s = concat_str(stringval, s);
1680 vim_free(stringval);
1681 stringval = s;
1682 }
1683 }
1684 }
1685 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 *p = c1;
1687 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 }
1690 }
1691
1692 /*
1693 * ":let @r = expr": Set register contents.
1694 */
1695 else if (*arg == '@')
1696 {
1697 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 if (op != NULL && (*op == '+' || *op == '-'))
1699 EMSG2(_(e_letwrong), op);
1700 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702 EMSG(_(e_letunexp));
1703 else
1704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001705 char_u *tofree = NULL;
1706 char_u *s;
1707
1708 p = get_tv_string(tv);
1709 if (op != NULL && *op == '.')
1710 {
1711 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1712 if (s != NULL)
1713 {
1714 p = tofree = concat_str(s, p);
1715 vim_free(s);
1716 }
1717 }
1718 if (p != NULL)
1719 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 }
1723 }
1724
1725 /*
1726 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001727 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001728 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001729 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001731 lval lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001733 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1734 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001736 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1737 EMSG(_(e_letunexp));
1738 else
1739 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001741 arg_end = p;
1742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001744 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 }
1746
1747 else
1748 EMSG2(_(e_invarg2), arg);
1749
1750 return arg_end;
1751}
1752
1753/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001754 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1755 */
1756 static int
1757check_changedtick(arg)
1758 char_u *arg;
1759{
1760 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1761 {
1762 EMSG2(_(e_readonlyvar), arg);
1763 return TRUE;
1764 }
1765 return FALSE;
1766}
1767
1768/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001769 * Get an lval: variable, Dict item or List item that can be assigned a value
1770 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1771 * "name.key", "name.key[expr]" etc.
1772 * Indexing only works if "name" is an existing List or Dictionary.
1773 * "name" points to the start of the name.
1774 * If "rettv" is not NULL it points to the value to be assigned.
1775 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1776 * wrong; must end in space or cmd separator.
1777 *
1778 * Returns a pointer to just after the name, including indexes.
1779 * When an evaluation error occurs "lp->name" is NULL;
1780 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 */
1782 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 typeval *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001786 lval *lp;
1787 int unlet;
1788 int skip;
1789 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 char_u *expr_start, *expr_end;
1793 int cc;
1794 VAR v;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001796 typeval var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797 int empty1 = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001798 listitem *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001799 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001800 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001802 /* Clear everything in "lp". */
1803 vim_memset(lp, 0, sizeof(lval));
1804
1805 if (skip)
1806 {
1807 /* When skipping just find the end of the name. */
1808 lp->ll_name = name;
1809 return find_name_end(name, NULL, NULL, TRUE);
1810 }
1811
1812 /* Find the end of the name. */
1813 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1814 if (expr_start != NULL)
1815 {
1816 /* Don't expand the name when we already know there is an error. */
1817 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1818 && *p != '[' && *p != '.')
1819 {
1820 EMSG(_(e_trailing));
1821 return NULL;
1822 }
1823
1824 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1825 if (lp->ll_exp_name == NULL)
1826 {
1827 /* Report an invalid expression in braces, unless the
1828 * expression evaluation has been cancelled due to an
1829 * aborting error, an interrupt, or an exception. */
1830 if (!aborting() && !quiet)
1831 {
1832 if (unlet)
1833 emsg_severe = TRUE;
1834 EMSG2(_(e_invarg2), name);
1835 return NULL;
1836 }
1837 }
1838 lp->ll_name = lp->ll_exp_name;
1839 }
1840 else
1841 lp->ll_name = name;
1842
1843 /* Without [idx] or .key we are done. */
1844 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1845 return p;
1846
1847 cc = *p;
1848 *p = NUL;
1849 v = find_var(lp->ll_name, TRUE);
1850 if (v == NULL && !quiet)
1851 EMSG2(_(e_undefvar), lp->ll_name);
1852 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (v == NULL)
1854 return NULL;
1855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 /*
1857 * Loop until no more [idx] or .key is following.
1858 */
1859 lp->ll_tv = &v->tv;
1860 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1863 && !(lp->ll_tv->v_type == VAR_DICT
1864 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001866 if (!quiet)
1867 EMSG(_("E689: Can only index a List or Dictionary"));
1868 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001870 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 if (!quiet)
1873 EMSG(_("E708: [:] must come last"));
1874 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001876
Bram Moolenaar8c711452005-01-14 21:53:12 +00001877 len = -1;
1878 if (*p == '.')
1879 {
1880 key = p + 1;
1881 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1882 ;
1883 if (len == 0)
1884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001885 if (!quiet)
1886 EMSG(_(e_emptykey));
1887 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001888 }
1889 p = key + len;
1890 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001891 else
1892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001893 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001894 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001895 if (*p == ':')
1896 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001897 else
1898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001899 empty1 = FALSE;
1900 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001902 }
1903
1904 /* Optionally get the second index [ :expr]. */
1905 if (*p == ':')
1906 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001911 if (!empty1)
1912 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 if (rettv != NULL && (rettv->v_type != VAR_LIST
1916 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 if (!quiet)
1919 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001920 if (!empty1)
1921 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001923 }
1924 p = skipwhite(p + 1);
1925 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001926 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001927 else
1928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001930 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1931 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001932 if (!empty1)
1933 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 }
1936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001938 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001939 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001941
Bram Moolenaar8c711452005-01-14 21:53:12 +00001942 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 if (!quiet)
1945 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 if (!empty1)
1947 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001949 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001951 }
1952
1953 /* Skip to past ']'. */
1954 ++p;
1955 }
1956
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001958 {
1959 if (len == -1)
1960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 key = get_tv_string(&var1);
1963 if (*key == NUL)
1964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 if (!quiet)
1966 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 }
1970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 lp->ll_list = NULL;
1972 lp->ll_dict = lp->ll_tv->vval.v_dict;
1973 lp->ll_di = dict_find(lp->ll_dict, key, len, &lp->ll_pdi);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001975 {
1976 /* Key does not exist in dict: may need toadd it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 if (len == -1)
1982 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 }
1985 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001987 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 if (len == -1)
1990 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 p = NULL;
1993 break;
1994 }
1995 if (len == -1)
1996 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 }
1999 else
2000 {
2001 /*
2002 * Get the number and item for the only or first index of the List.
2003 */
2004 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 else
2007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 clear_tv(&var1);
2010 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 lp->ll_list = lp->ll_tv->vval.v_list;
2013 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2014 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 if (!quiet)
2017 EMSGN(_(e_listidx), lp->ll_n1);
2018 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 }
2022
2023 /*
2024 * May need to find the item or absolute index for the second
2025 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 * When no index given: "lp->ll_empty2" is TRUE.
2027 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002028 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002032 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002036 if (ni == NULL)
2037 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 if (!quiet)
2039 EMSGN(_(e_listidx), lp->ll_n2);
2040 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002043 }
2044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2046 if (lp->ll_n1 < 0)
2047 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2048 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 if (!quiet)
2051 EMSGN(_(e_listidx), lp->ll_n2);
2052 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002053 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002054 }
2055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 }
2059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 return p;
2061}
2062
2063/*
2064 * Clear an "lval" that was filled by get_lval().
2065 */
2066 static void
2067clear_lval(lp)
2068 lval *lp;
2069{
2070 vim_free(lp->ll_exp_name);
2071 vim_free(lp->ll_newkey);
2072}
2073
2074/*
2075 * Set a variable that was parsed by get_lval().
2076 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 */
2079 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002080set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 lval *lp;
2082 char_u *endp;
2083 typeval *rettv;
2084 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086{
2087 int cc;
2088 listitem *ni;
2089 listitem *ri;
2090 dictitem *di;
2091
2092 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 cc = *endp;
2097 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 if (op != NULL && *op != '=')
2099 {
2100 typeval tv;
2101
2102 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK)
2103 {
2104 if (tv_op(&tv, rettv, op) == OK)
2105 set_var(lp->ll_name, &tv, FALSE);
2106 clear_tv(&tv);
2107 }
2108 }
2109 else
2110 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 }
2114 else if (lp->ll_range)
2115 {
2116 /*
2117 * Assign the List values to the list items.
2118 */
2119 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002120 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121 if (op != NULL && *op != '=')
2122 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2123 else
2124 {
2125 clear_tv(&lp->ll_li->li_tv);
2126 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2127 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 ri = ri->li_next;
2129 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2130 break;
2131 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 /* Need to add an empty item. */
2134 ni = listitem_alloc();
2135 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002136 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 ri = NULL;
2138 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002139 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 ni->li_tv.v_type = VAR_NUMBER;
2141 ni->li_tv.vval.v_number = 0;
2142 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002143 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 lp->ll_li = lp->ll_li->li_next;
2145 ++lp->ll_n1;
2146 }
2147 if (ri != NULL)
2148 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 else if (lp->ll_empty2
2150 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 : lp->ll_n1 != lp->ll_n2)
2152 EMSG(_("E711: List value has not enough items"));
2153 }
2154 else
2155 {
2156 /*
2157 * Assign to a List or Dictionary item.
2158 */
2159 if (lp->ll_newkey != NULL)
2160 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 if (op != NULL && *op != '=')
2162 {
2163 EMSG2(_(e_letwrong), op);
2164 return;
2165 }
2166
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 /* Need to add an item to the Dictionary. */
2168 di = dictitem_alloc();
2169 if (di == NULL)
2170 return;
2171 di->di_key = lp->ll_newkey;
2172 lp->ll_newkey = NULL;
2173 dict_add(lp->ll_tv->vval.v_dict, di);
2174 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 else if (op != NULL && *op != '=')
2177 {
2178 tv_op(lp->ll_tv, rettv, op);
2179 return;
2180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 /*
2185 * Assign the value to the variable or list item.
2186 */
2187 if (copy)
2188 copy_tv(rettv, lp->ll_tv);
2189 else
2190 {
2191 *lp->ll_tv = *rettv;
2192 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 }
2194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195}
2196
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002197/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2199 * Returns OK or FAIL.
2200 */
2201 static int
2202tv_op(tv1, tv2, op)
2203 typeval *tv1;
2204 typeval *tv2;
2205 char_u *op;
2206{
2207 long n;
2208 char_u numbuf[NUMBUFLEN];
2209 char_u *s;
2210
2211 /* Can't do anything with a Funcref or a Dict on the right. */
2212 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2213 {
2214 switch (tv1->v_type)
2215 {
2216 case VAR_DICT:
2217 case VAR_FUNC:
2218 break;
2219
2220 case VAR_LIST:
2221 if (*op != '+' || tv2->v_type != VAR_LIST)
2222 break;
2223 /* List += List */
2224 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2225 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2226 return OK;
2227
2228 case VAR_NUMBER:
2229 case VAR_STRING:
2230 if (tv2->v_type == VAR_LIST)
2231 break;
2232 if (*op == '+' || *op == '-')
2233 {
2234 /* nr += nr or nr -= nr*/
2235 n = get_tv_number(tv1);
2236 if (*op == '+')
2237 n += get_tv_number(tv2);
2238 else
2239 n -= get_tv_number(tv2);
2240 clear_tv(tv1);
2241 tv1->v_type = VAR_NUMBER;
2242 tv1->vval.v_number = n;
2243 }
2244 else
2245 {
2246 /* str .= str */
2247 s = get_tv_string(tv1);
2248 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2249 clear_tv(tv1);
2250 tv1->v_type = VAR_STRING;
2251 tv1->vval.v_string = s;
2252 }
2253 return OK;
2254 }
2255 }
2256
2257 EMSG2(_(e_letwrong), op);
2258 return FAIL;
2259}
2260
2261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262 * Add a watcher to a list.
2263 */
2264 static void
2265list_add_watch(l, lw)
2266 listvar *l;
2267 listwatch *lw;
2268{
2269 lw->lw_next = l->lv_watch;
2270 l->lv_watch = lw;
2271}
2272
2273/*
2274 * Remove a watches from a list.
2275 * No warning when it isn't found...
2276 */
2277 static void
2278list_rem_watch(l, lwrem)
2279 listvar *l;
2280 listwatch *lwrem;
2281{
2282 listwatch *lw, **lwp;
2283
2284 lwp = &l->lv_watch;
2285 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2286 {
2287 if (lw == lwrem)
2288 {
2289 *lwp = lw->lw_next;
2290 break;
2291 }
2292 lwp = &lw->lw_next;
2293 }
2294}
2295
2296/*
2297 * Just before removing an item from a list: advance watchers to the next
2298 * item.
2299 */
2300 static void
2301list_fix_watch(l, item)
2302 listvar *l;
2303 listitem *item;
2304{
2305 listwatch *lw;
2306
2307 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2308 if (lw->lw_item == item)
2309 lw->lw_item = item->li_next;
2310}
2311
2312/*
2313 * Evaluate the expression used in a ":for var in expr" command.
2314 * "arg" points to "var".
2315 * Set "*errp" to TRUE for an error, FALSE otherwise;
2316 * Return a pointer that holds the info. Null when there is an error.
2317 */
2318 void *
2319eval_for_line(arg, errp, nextcmdp, skip)
2320 char_u *arg;
2321 int *errp;
2322 char_u **nextcmdp;
2323 int skip;
2324{
2325 forinfo *fi;
2326 char_u *expr;
2327 typeval tv;
2328 listvar *l;
2329
2330 *errp = TRUE; /* default: there is an error */
2331
2332 fi = (forinfo *)alloc_clear(sizeof(forinfo));
2333 if (fi == NULL)
2334 return NULL;
2335
2336 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2337 if (expr == NULL)
2338 return fi;
2339
2340 expr = skipwhite(expr);
2341 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2342 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002343 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344 return fi;
2345 }
2346
2347 if (skip)
2348 ++emsg_skip;
2349 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2350 {
2351 *errp = FALSE;
2352 if (!skip)
2353 {
2354 l = tv.vval.v_list;
2355 if (tv.v_type != VAR_LIST || l == NULL)
2356 EMSG(_(e_listreq));
2357 else
2358 {
2359 fi->fi_list = l;
2360 list_add_watch(l, &fi->fi_lw);
2361 fi->fi_lw.lw_item = l->lv_first;
2362 }
2363 }
2364 }
2365 if (skip)
2366 --emsg_skip;
2367
2368 return fi;
2369}
2370
2371/*
2372 * Use the first item in a ":for" list. Advance to the next.
2373 * Assign the values to the variable (list). "arg" points to the first one.
2374 * Return TRUE when a valid item was found, FALSE when at end of list or
2375 * something wrong.
2376 */
2377 int
2378next_for_item(fi_void, arg)
2379 void *fi_void;
2380 char_u *arg;
2381{
2382 forinfo *fi = (forinfo *)fi_void;
2383 int result;
2384 listitem *item;
2385
2386 item = fi->fi_lw.lw_item;
2387 if (item == NULL)
2388 result = FALSE;
2389 else
2390 {
2391 fi->fi_lw.lw_item = item->li_next;
2392 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2393 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2394 }
2395 return result;
2396}
2397
2398/*
2399 * Free the structure used to store info used by ":for".
2400 */
2401 void
2402free_for_info(fi_void)
2403 void *fi_void;
2404{
2405 forinfo *fi = (forinfo *)fi_void;
2406
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002407 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002408 list_rem_watch(fi->fi_list, &fi->fi_lw);
2409 vim_free(fi);
2410}
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2413
2414 void
2415set_context_for_expression(xp, arg, cmdidx)
2416 expand_T *xp;
2417 char_u *arg;
2418 cmdidx_T cmdidx;
2419{
2420 int got_eq = FALSE;
2421 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 if (cmdidx == CMD_let)
2425 {
2426 xp->xp_context = EXPAND_USER_VARS;
2427 if (vim_strchr(arg, '=') == NULL)
2428 {
2429 /* ":let var1 var2 ...": find last space. */
2430 for (p = arg + STRLEN(arg); p > arg; )
2431 {
2432 xp->xp_pattern = p;
2433 p = mb_ptr_back(arg, p);
2434 if (vim_iswhite(*p))
2435 break;
2436 }
2437 return;
2438 }
2439 }
2440 else
2441 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2442 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 while ((xp->xp_pattern = vim_strpbrk(arg,
2444 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2445 {
2446 c = *xp->xp_pattern;
2447 if (c == '&')
2448 {
2449 c = xp->xp_pattern[1];
2450 if (c == '&')
2451 {
2452 ++xp->xp_pattern;
2453 xp->xp_context = cmdidx != CMD_let || got_eq
2454 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2455 }
2456 else if (c != ' ')
2457 xp->xp_context = EXPAND_SETTINGS;
2458 }
2459 else if (c == '$')
2460 {
2461 /* environment variable */
2462 xp->xp_context = EXPAND_ENV_VARS;
2463 }
2464 else if (c == '=')
2465 {
2466 got_eq = TRUE;
2467 xp->xp_context = EXPAND_EXPRESSION;
2468 }
2469 else if (c == '<'
2470 && xp->xp_context == EXPAND_FUNCTIONS
2471 && vim_strchr(xp->xp_pattern, '(') == NULL)
2472 {
2473 /* Function name can start with "<SNR>" */
2474 break;
2475 }
2476 else if (cmdidx != CMD_let || got_eq)
2477 {
2478 if (c == '"') /* string */
2479 {
2480 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2481 if (c == '\\' && xp->xp_pattern[1] != NUL)
2482 ++xp->xp_pattern;
2483 xp->xp_context = EXPAND_NOTHING;
2484 }
2485 else if (c == '\'') /* literal string */
2486 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2489 /* skip */ ;
2490 xp->xp_context = EXPAND_NOTHING;
2491 }
2492 else if (c == '|')
2493 {
2494 if (xp->xp_pattern[1] == '|')
2495 {
2496 ++xp->xp_pattern;
2497 xp->xp_context = EXPAND_EXPRESSION;
2498 }
2499 else
2500 xp->xp_context = EXPAND_COMMANDS;
2501 }
2502 else
2503 xp->xp_context = EXPAND_EXPRESSION;
2504 }
2505 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506 /* Doesn't look like something valid, expand as an expression
2507 * anyway. */
2508 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 arg = xp->xp_pattern;
2510 if (*arg != NUL)
2511 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2512 /* skip */ ;
2513 }
2514 xp->xp_pattern = arg;
2515}
2516
2517#endif /* FEAT_CMDL_COMPL */
2518
2519/*
2520 * ":1,25call func(arg1, arg2)" function call.
2521 */
2522 void
2523ex_call(eap)
2524 exarg_T *eap;
2525{
2526 char_u *arg = eap->arg;
2527 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 int len;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002531 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 linenr_T lnum;
2533 int doesrange;
2534 int failed = FALSE;
2535
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 tofree = trans_function_name(&arg, eap->skip, TFN_INT, NULL);
2537 if (tofree == NULL)
2538 return;
2539
2540 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2541 len = STRLEN(tofree);
2542 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
2544 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 if (*startarg != '(')
2548 {
2549 EMSG2(_("E107: Missing braces: %s"), name);
2550 goto end;
2551 }
2552
2553 /*
2554 * When skipping, evaluate the function once, to find the end of the
2555 * arguments.
2556 * When the function takes a range, this is discovered after the first
2557 * call, and the loop is broken.
2558 */
2559 if (eap->skip)
2560 {
2561 ++emsg_skip;
2562 lnum = eap->line2; /* do it once, also with an invalid range */
2563 }
2564 else
2565 lnum = eap->line1;
2566 for ( ; lnum <= eap->line2; ++lnum)
2567 {
2568 if (!eap->skip && eap->addr_count > 0)
2569 {
2570 curwin->w_cursor.lnum = lnum;
2571 curwin->w_cursor.col = 0;
2572 }
2573 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002574 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
2575 eap->line1, eap->line2, &doesrange, !eap->skip, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 failed = TRUE;
2578 break;
2579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (doesrange || eap->skip)
2582 break;
2583 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002584 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002586 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (aborting())
2588 break;
2589 }
2590 if (eap->skip)
2591 --emsg_skip;
2592
2593 if (!failed)
2594 {
2595 /* Check for trailing illegal characters and a following command. */
2596 if (!ends_excmd(*arg))
2597 {
2598 emsg_severe = TRUE;
2599 EMSG(_(e_trailing));
2600 }
2601 else
2602 eap->nextcmd = check_nextcmd(arg);
2603 }
2604
2605end:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002606 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607}
2608
2609/*
2610 * ":unlet[!] var1 ... " command.
2611 */
2612 void
2613ex_unlet(eap)
2614 exarg_T *eap;
2615{
2616 char_u *arg = eap->arg;
2617 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 int error = FALSE;
2619
2620 do
2621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lval lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /* Parse the name and find the end. */
2625 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2626 if (lv.ll_name == NULL)
2627 error = TRUE; /* error but continue parsing */
2628 if (name_end == NULL || (!vim_iswhite(*name_end)
2629 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (name_end != NULL)
2632 {
2633 emsg_severe = TRUE;
2634 EMSG(_(e_trailing));
2635 }
2636 if (!(eap->skip || error))
2637 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 break;
2639 }
2640
2641 if (!error && !eap->skip)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2643 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (!eap->skip)
2646 clear_lval(&lv);
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 arg = skipwhite(name_end);
2649 } while (!ends_excmd(*arg));
2650
2651 eap->nextcmd = check_nextcmd(arg);
2652}
2653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656do_unlet_var(lp, name_end, forceit)
2657 lval *lp;
2658 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 int forceit;
2660{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 int ret = OK;
2662 int cc;
2663
2664 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 cc = *name_end;
2667 *name_end = NUL;
2668
2669 /* Normal name or expanded name. */
2670 if (check_changedtick(lp->ll_name))
2671 ret = FAIL;
2672 else if (do_unlet(lp->ll_name) == FAIL && !forceit)
2673 {
2674 EMSG2(_("E108: No such variable: \"%s\""), lp->ll_name);
2675 ret = FAIL;
2676 }
2677 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 else if (lp->ll_range)
2680 {
2681 listitem *li;
2682
2683 /* Delete a range of List items. */
2684 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2685 {
2686 li = lp->ll_li->li_next;
2687 listitem_remove(lp->ll_list, lp->ll_li);
2688 lp->ll_li = li;
2689 ++lp->ll_n1;
2690 }
2691 }
2692 else
2693 {
2694 clear_tv(lp->ll_tv);
2695 if (lp->ll_list != NULL)
2696 {
2697 /* unlet a List item. */
2698 listitem_remove(lp->ll_list, lp->ll_li);
2699 }
2700 else
2701 {
2702 /* unlet a Dictionary item. */
2703 *lp->ll_pdi = lp->ll_di->di_next;
2704 dictitem_free(lp->ll_di);
2705 }
2706 }
2707
2708 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709}
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711/*
2712 * "unlet" a variable. Return OK if it existed, FAIL if not.
2713 */
2714 int
2715do_unlet(name)
2716 char_u *name;
2717{
2718 VAR v;
2719
2720 v = find_var(name, TRUE);
2721 if (v != NULL)
2722 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002723 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return OK;
2725 }
2726 return FAIL;
2727}
2728
2729#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2730/*
2731 * Delete all "menutrans_" variables.
2732 */
2733 void
2734del_menutrans_vars()
2735{
2736 int i;
2737
2738 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002739 if (VAR_ENTRY(i).v_name != NULL
2740 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2741 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742}
2743#endif
2744
2745#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2746
2747/*
2748 * Local string buffer for the next two functions to store a variable name
2749 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2750 * get_user_var_name().
2751 */
2752
2753static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2754
2755static char_u *varnamebuf = NULL;
2756static int varnamebuflen = 0;
2757
2758/*
2759 * Function to concatenate a prefix and a variable name.
2760 */
2761 static char_u *
2762cat_prefix_varname(prefix, name)
2763 int prefix;
2764 char_u *name;
2765{
2766 int len;
2767
2768 len = (int)STRLEN(name) + 3;
2769 if (len > varnamebuflen)
2770 {
2771 vim_free(varnamebuf);
2772 len += 10; /* some additional space */
2773 varnamebuf = alloc(len);
2774 if (varnamebuf == NULL)
2775 {
2776 varnamebuflen = 0;
2777 return NULL;
2778 }
2779 varnamebuflen = len;
2780 }
2781 *varnamebuf = prefix;
2782 varnamebuf[1] = ':';
2783 STRCPY(varnamebuf + 2, name);
2784 return varnamebuf;
2785}
2786
2787/*
2788 * Function given to ExpandGeneric() to obtain the list of user defined
2789 * (global/buffer/window/built-in) variable names.
2790 */
2791/*ARGSUSED*/
2792 char_u *
2793get_user_var_name(xp, idx)
2794 expand_T *xp;
2795 int idx;
2796{
2797 static int gidx;
2798 static int bidx;
2799 static int widx;
2800 static int vidx;
2801 char_u *name;
2802
2803 if (idx == 0)
2804 gidx = bidx = widx = vidx = 0;
2805 if (gidx < variables.ga_len) /* Global variables */
2806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002807 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 && gidx < variables.ga_len)
2809 /* skip */;
2810 if (name != NULL)
2811 {
2812 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2813 return cat_prefix_varname('g', name);
2814 else
2815 return name;
2816 }
2817 }
2818 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2819 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002820 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 && bidx < curbuf->b_vars.ga_len)
2822 /* skip */;
2823 if (name != NULL)
2824 return cat_prefix_varname('b', name);
2825 }
2826 if (bidx == curbuf->b_vars.ga_len)
2827 {
2828 ++bidx;
2829 return (char_u *)"b:changedtick";
2830 }
2831 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2832 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002833 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 && widx < curwin->w_vars.ga_len)
2835 /* skip */;
2836 if (name != NULL)
2837 return cat_prefix_varname('w', name);
2838 }
2839 if (vidx < VV_LEN) /* Built-in variables */
2840 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2841
2842 vim_free(varnamebuf);
2843 varnamebuf = NULL;
2844 varnamebuflen = 0;
2845 return NULL;
2846}
2847
2848#endif /* FEAT_CMDL_COMPL */
2849
2850/*
2851 * types for expressions.
2852 */
2853typedef enum
2854{
2855 TYPE_UNKNOWN = 0
2856 , TYPE_EQUAL /* == */
2857 , TYPE_NEQUAL /* != */
2858 , TYPE_GREATER /* > */
2859 , TYPE_GEQUAL /* >= */
2860 , TYPE_SMALLER /* < */
2861 , TYPE_SEQUAL /* <= */
2862 , TYPE_MATCH /* =~ */
2863 , TYPE_NOMATCH /* !~ */
2864} exptype_T;
2865
2866/*
2867 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002868 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2870 */
2871
2872/*
2873 * Handle zero level expression.
2874 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002875 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * Return OK or FAIL.
2877 */
2878 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002881 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 char_u **nextcmd;
2883 int evaluate;
2884{
2885 int ret;
2886 char_u *p;
2887
2888 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 if (ret == FAIL || !ends_excmd(*p))
2891 {
2892 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 /*
2895 * Report the invalid expression unless the expression evaluation has
2896 * been cancelled due to an aborting error, an interrupt, or an
2897 * exception.
2898 */
2899 if (!aborting())
2900 EMSG2(_(e_invexpr2), arg);
2901 ret = FAIL;
2902 }
2903 if (nextcmd != NULL)
2904 *nextcmd = check_nextcmd(p);
2905
2906 return ret;
2907}
2908
2909/*
2910 * Handle top level expression:
2911 * expr1 ? expr0 : expr0
2912 *
2913 * "arg" must point to the first non-white of the expression.
2914 * "arg" is advanced to the next non-white after the recognized expression.
2915 *
2916 * Return OK or FAIL.
2917 */
2918 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 int evaluate;
2923{
2924 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002925 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927 /*
2928 * Get the first variable.
2929 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 return FAIL;
2932
2933 if ((*arg)[0] == '?')
2934 {
2935 result = FALSE;
2936 if (evaluate)
2937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
2942
2943 /*
2944 * Get the second variable.
2945 */
2946 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 return FAIL;
2949
2950 /*
2951 * Check for the ":".
2952 */
2953 if ((*arg)[0] != ':')
2954 {
2955 EMSG(_("E109: Missing ':' after '?'"));
2956 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 return FAIL;
2959 }
2960
2961 /*
2962 * Get the third variable.
2963 */
2964 *arg = skipwhite(*arg + 1);
2965 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2966 {
2967 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 return FAIL;
2970 }
2971 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002972 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974
2975 return OK;
2976}
2977
2978/*
2979 * Handle first level expression:
2980 * expr2 || expr2 || expr2 logical OR
2981 *
2982 * "arg" must point to the first non-white of the expression.
2983 * "arg" is advanced to the next non-white after the recognized expression.
2984 *
2985 * Return OK or FAIL.
2986 */
2987 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002988eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002990 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 int evaluate;
2992{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002993 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 long result;
2995 int first;
2996
2997 /*
2998 * Get the first variable.
2999 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003000 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 return FAIL;
3002
3003 /*
3004 * Repeat until there is no following "||".
3005 */
3006 first = TRUE;
3007 result = FALSE;
3008 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3009 {
3010 if (evaluate && first)
3011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003012 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003014 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 first = FALSE;
3016 }
3017
3018 /*
3019 * Get the second variable.
3020 */
3021 *arg = skipwhite(*arg + 2);
3022 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3023 return FAIL;
3024
3025 /*
3026 * Compute the result.
3027 */
3028 if (evaluate && !result)
3029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003030 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003032 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034 if (evaluate)
3035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003036 rettv->v_type = VAR_NUMBER;
3037 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039 }
3040
3041 return OK;
3042}
3043
3044/*
3045 * Handle second level expression:
3046 * expr3 && expr3 && expr3 logical AND
3047 *
3048 * "arg" must point to the first non-white of the expression.
3049 * "arg" is advanced to the next non-white after the recognized expression.
3050 *
3051 * Return OK or FAIL.
3052 */
3053 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 int evaluate;
3058{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003059 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 long result;
3061 int first;
3062
3063 /*
3064 * Get the first variable.
3065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 return FAIL;
3068
3069 /*
3070 * Repeat until there is no following "&&".
3071 */
3072 first = TRUE;
3073 result = TRUE;
3074 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3075 {
3076 if (evaluate && first)
3077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 first = FALSE;
3082 }
3083
3084 /*
3085 * Get the second variable.
3086 */
3087 *arg = skipwhite(*arg + 2);
3088 if (eval4(arg, &var2, evaluate && result) == FAIL)
3089 return FAIL;
3090
3091 /*
3092 * Compute the result.
3093 */
3094 if (evaluate && result)
3095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003096 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100 if (evaluate)
3101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003102 rettv->v_type = VAR_NUMBER;
3103 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105 }
3106
3107 return OK;
3108}
3109
3110/*
3111 * Handle third level expression:
3112 * var1 == var2
3113 * var1 =~ var2
3114 * var1 != var2
3115 * var1 !~ var2
3116 * var1 > var2
3117 * var1 >= var2
3118 * var1 < var2
3119 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003120 * var1 is var2
3121 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 *
3123 * "arg" must point to the first non-white of the expression.
3124 * "arg" is advanced to the next non-white after the recognized expression.
3125 *
3126 * Return OK or FAIL.
3127 */
3128 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003129eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003131 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int evaluate;
3133{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003134 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 char_u *p;
3136 int i;
3137 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003138 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 int len = 2;
3140 long n1, n2;
3141 char_u *s1, *s2;
3142 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3143 regmatch_T regmatch;
3144 int ic;
3145 char_u *save_cpo;
3146
3147 /*
3148 * Get the first variable.
3149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003150 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 return FAIL;
3152
3153 p = *arg;
3154 switch (p[0])
3155 {
3156 case '=': if (p[1] == '=')
3157 type = TYPE_EQUAL;
3158 else if (p[1] == '~')
3159 type = TYPE_MATCH;
3160 break;
3161 case '!': if (p[1] == '=')
3162 type = TYPE_NEQUAL;
3163 else if (p[1] == '~')
3164 type = TYPE_NOMATCH;
3165 break;
3166 case '>': if (p[1] != '=')
3167 {
3168 type = TYPE_GREATER;
3169 len = 1;
3170 }
3171 else
3172 type = TYPE_GEQUAL;
3173 break;
3174 case '<': if (p[1] != '=')
3175 {
3176 type = TYPE_SMALLER;
3177 len = 1;
3178 }
3179 else
3180 type = TYPE_SEQUAL;
3181 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003182 case 'i': if (p[1] == 's')
3183 {
3184 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3185 len = 5;
3186 if (!vim_isIDc(p[len]))
3187 {
3188 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3189 type_is = TRUE;
3190 }
3191 }
3192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194
3195 /*
3196 * If there is a comparitive operator, use it.
3197 */
3198 if (type != TYPE_UNKNOWN)
3199 {
3200 /* extra question mark appended: ignore case */
3201 if (p[len] == '?')
3202 {
3203 ic = TRUE;
3204 ++len;
3205 }
3206 /* extra '#' appended: match case */
3207 else if (p[len] == '#')
3208 {
3209 ic = FALSE;
3210 ++len;
3211 }
3212 /* nothing appened: use 'ignorecase' */
3213 else
3214 ic = p_ic;
3215
3216 /*
3217 * Get the second variable.
3218 */
3219 *arg = skipwhite(p + len);
3220 if (eval5(arg, &var2, evaluate) == FAIL)
3221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 return FAIL;
3224 }
3225
3226 if (evaluate)
3227 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003228 if (type_is && rettv->v_type != var2.v_type)
3229 {
3230 /* For "is" a different type always means FALSE, for "notis"
3231 * it means TRUE. */
3232 n1 = (type == TYPE_NEQUAL);
3233 }
3234 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3235 {
3236 if (type_is)
3237 {
3238 n1 = (rettv->v_type == var2.v_type
3239 && rettv->vval.v_list == var2.vval.v_list);
3240 if (type == TYPE_NEQUAL)
3241 n1 = !n1;
3242 }
3243 else if (rettv->v_type != var2.v_type
3244 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3245 {
3246 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003247 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003248 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003249 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003250 clear_tv(rettv);
3251 clear_tv(&var2);
3252 return FAIL;
3253 }
3254 else
3255 {
3256 /* Compare two Lists for being equal or unequal. */
3257 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3258 if (type == TYPE_NEQUAL)
3259 n1 = !n1;
3260 }
3261 }
3262
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003263 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3264 {
3265 if (type_is)
3266 {
3267 n1 = (rettv->v_type == var2.v_type
3268 && rettv->vval.v_dict == var2.vval.v_dict);
3269 if (type == TYPE_NEQUAL)
3270 n1 = !n1;
3271 }
3272 else if (rettv->v_type != var2.v_type
3273 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3274 {
3275 if (rettv->v_type != var2.v_type)
3276 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3277 else
3278 EMSG(_("E736: Invalid operation for Dictionary"));
3279 clear_tv(rettv);
3280 clear_tv(&var2);
3281 return FAIL;
3282 }
3283 else
3284 {
3285 /* Compare two Dictionaries for being equal or unequal. */
3286 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3287 if (type == TYPE_NEQUAL)
3288 n1 = !n1;
3289 }
3290 }
3291
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003292 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3293 {
3294 if (rettv->v_type != var2.v_type
3295 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3296 {
3297 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003298 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003299 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003300 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003301 clear_tv(rettv);
3302 clear_tv(&var2);
3303 return FAIL;
3304 }
3305 else
3306 {
3307 /* Compare two Funcrefs for being equal or unequal. */
3308 if (rettv->vval.v_string == NULL
3309 || var2.vval.v_string == NULL)
3310 n1 = FALSE;
3311 else
3312 n1 = STRCMP(rettv->vval.v_string,
3313 var2.vval.v_string) == 0;
3314 if (type == TYPE_NEQUAL)
3315 n1 = !n1;
3316 }
3317 }
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 /*
3320 * If one of the two variables is a number, compare as a number.
3321 * When using "=~" or "!~", always compare as string.
3322 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003323 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003326 n1 = get_tv_number(rettv);
3327 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 switch (type)
3329 {
3330 case TYPE_EQUAL: n1 = (n1 == n2); break;
3331 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3332 case TYPE_GREATER: n1 = (n1 > n2); break;
3333 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3334 case TYPE_SMALLER: n1 = (n1 < n2); break;
3335 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3336 case TYPE_UNKNOWN:
3337 case TYPE_MATCH:
3338 case TYPE_NOMATCH: break; /* avoid gcc warning */
3339 }
3340 }
3341 else
3342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003343 s1 = get_tv_string_buf(rettv, buf1);
3344 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3346 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3347 else
3348 i = 0;
3349 n1 = FALSE;
3350 switch (type)
3351 {
3352 case TYPE_EQUAL: n1 = (i == 0); break;
3353 case TYPE_NEQUAL: n1 = (i != 0); break;
3354 case TYPE_GREATER: n1 = (i > 0); break;
3355 case TYPE_GEQUAL: n1 = (i >= 0); break;
3356 case TYPE_SMALLER: n1 = (i < 0); break;
3357 case TYPE_SEQUAL: n1 = (i <= 0); break;
3358
3359 case TYPE_MATCH:
3360 case TYPE_NOMATCH:
3361 /* avoid 'l' flag in 'cpoptions' */
3362 save_cpo = p_cpo;
3363 p_cpo = (char_u *)"";
3364 regmatch.regprog = vim_regcomp(s2,
3365 RE_MAGIC + RE_STRING);
3366 regmatch.rm_ic = ic;
3367 if (regmatch.regprog != NULL)
3368 {
3369 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3370 vim_free(regmatch.regprog);
3371 if (type == TYPE_NOMATCH)
3372 n1 = !n1;
3373 }
3374 p_cpo = save_cpo;
3375 break;
3376
3377 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3378 }
3379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 clear_tv(rettv);
3381 clear_tv(&var2);
3382 rettv->v_type = VAR_NUMBER;
3383 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 }
3386
3387 return OK;
3388}
3389
3390/*
3391 * Handle fourth level expression:
3392 * + number addition
3393 * - number subtraction
3394 * . string concatenation
3395 *
3396 * "arg" must point to the first non-white of the expression.
3397 * "arg" is advanced to the next non-white after the recognized expression.
3398 *
3399 * Return OK or FAIL.
3400 */
3401 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 int evaluate;
3406{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003408 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 int op;
3410 long n1, n2;
3411 char_u *s1, *s2;
3412 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3413 char_u *p;
3414
3415 /*
3416 * Get the first variable.
3417 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 return FAIL;
3420
3421 /*
3422 * Repeat computing, until no '+', '-' or '.' is following.
3423 */
3424 for (;;)
3425 {
3426 op = **arg;
3427 if (op != '+' && op != '-' && op != '.')
3428 break;
3429
3430 /*
3431 * Get the second variable.
3432 */
3433 *arg = skipwhite(*arg + 1);
3434 if (eval6(arg, &var2, evaluate) == FAIL)
3435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return FAIL;
3438 }
3439
3440 if (evaluate)
3441 {
3442 /*
3443 * Compute the result.
3444 */
3445 if (op == '.')
3446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003447 s1 = get_tv_string_buf(rettv, buf1);
3448 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003449 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 clear_tv(rettv);
3451 rettv->v_type = VAR_STRING;
3452 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003454 else if (op == '+' && rettv->v_type == VAR_LIST
3455 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003456 {
3457 /* concatenate Lists */
3458 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3459 &var3) == FAIL)
3460 {
3461 clear_tv(rettv);
3462 clear_tv(&var2);
3463 return FAIL;
3464 }
3465 clear_tv(rettv);
3466 *rettv = var3;
3467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else
3469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 n1 = get_tv_number(rettv);
3471 n2 = get_tv_number(&var2);
3472 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (op == '+')
3474 n1 = n1 + n2;
3475 else
3476 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003477 rettv->v_type = VAR_NUMBER;
3478 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482 }
3483 return OK;
3484}
3485
3486/*
3487 * Handle fifth level expression:
3488 * * number multiplication
3489 * / number division
3490 * % number modulo
3491 *
3492 * "arg" must point to the first non-white of the expression.
3493 * "arg" is advanced to the next non-white after the recognized expression.
3494 *
3495 * Return OK or FAIL.
3496 */
3497 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003498eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 int evaluate;
3502{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 int op;
3505 long n1, n2;
3506
3507 /*
3508 * Get the first variable.
3509 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003510 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return FAIL;
3512
3513 /*
3514 * Repeat computing, until no '*', '/' or '%' is following.
3515 */
3516 for (;;)
3517 {
3518 op = **arg;
3519 if (op != '*' && op != '/' && op != '%')
3520 break;
3521
3522 if (evaluate)
3523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 n1 = get_tv_number(rettv);
3525 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 else
3528 n1 = 0;
3529
3530 /*
3531 * Get the second variable.
3532 */
3533 *arg = skipwhite(*arg + 1);
3534 if (eval7(arg, &var2, evaluate) == FAIL)
3535 return FAIL;
3536
3537 if (evaluate)
3538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 n2 = get_tv_number(&var2);
3540 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
3542 /*
3543 * Compute the result.
3544 */
3545 if (op == '*')
3546 n1 = n1 * n2;
3547 else if (op == '/')
3548 {
3549 if (n2 == 0) /* give an error message? */
3550 n1 = 0x7fffffffL;
3551 else
3552 n1 = n1 / n2;
3553 }
3554 else
3555 {
3556 if (n2 == 0) /* give an error message? */
3557 n1 = 0;
3558 else
3559 n1 = n1 % n2;
3560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003561 rettv->v_type = VAR_NUMBER;
3562 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 }
3565
3566 return OK;
3567}
3568
3569/*
3570 * Handle sixth level expression:
3571 * number number constant
3572 * "string" string contstant
3573 * 'string' literal string contstant
3574 * &option-name option value
3575 * @r register contents
3576 * identifier variable value
3577 * function() function call
3578 * $VAR environment variable
3579 * (expression) nested expression
3580 *
3581 * Also handle:
3582 * ! in front logical NOT
3583 * - in front unary minus
3584 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 * trailing [] subscript in String or List
3586 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *
3588 * "arg" must point to the first non-white of the expression.
3589 * "arg" is advanced to the next non-white after the recognized expression.
3590 *
3591 * Return OK or FAIL.
3592 */
3593 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 int evaluate;
3598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 long n;
3600 int len;
3601 char_u *s;
3602 int val;
3603 char_u *start_leader, *end_leader;
3604 int ret = OK;
3605 char_u *alias;
Bram Moolenaare9a41262005-01-15 22:18:47 +00003606 dictvar *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
3608 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003610 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 /*
3615 * Skip '!' and '-' characters. They are handled later.
3616 */
3617 start_leader = *arg;
3618 while (**arg == '!' || **arg == '-' || **arg == '+')
3619 *arg = skipwhite(*arg + 1);
3620 end_leader = *arg;
3621
3622 switch (**arg)
3623 {
3624 /*
3625 * Number constant.
3626 */
3627 case '0':
3628 case '1':
3629 case '2':
3630 case '3':
3631 case '4':
3632 case '5':
3633 case '6':
3634 case '7':
3635 case '8':
3636 case '9':
3637 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3638 *arg += len;
3639 if (evaluate)
3640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 rettv->v_type = VAR_NUMBER;
3642 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 break;
3645
3646 /*
3647 * String constant: "string".
3648 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 break;
3651
3652 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003656 break;
3657
3658 /*
3659 * List: [expr, expr]
3660 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 break;
3663
3664 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 * Dictionary: {key: val, key: val}
3666 */
3667 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3668 break;
3669
3670 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003671 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003673 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 break;
3675
3676 /*
3677 * Environment variable: $VAR.
3678 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003679 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 break;
3681
3682 /*
3683 * Register contents: @r.
3684 */
3685 case '@': ++*arg;
3686 if (evaluate)
3687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 rettv->v_type = VAR_STRING;
3689 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 if (**arg != NUL)
3692 ++*arg;
3693 break;
3694
3695 /*
3696 * nested expression: (expression).
3697 */
3698 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (**arg == ')')
3701 ++*arg;
3702 else if (ret == OK)
3703 {
3704 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 ret = FAIL;
3707 }
3708 break;
3709
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 break;
3712 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003713
3714 if (ret == NOTDONE)
3715 {
3716 /*
3717 * Must be a variable or function name.
3718 * Can also be a curly-braces kind of name: {expr}.
3719 */
3720 s = *arg;
3721 len = get_func_len(arg, &alias, evaluate);
3722 if (alias != NULL)
3723 s = alias;
3724
3725 if (len == 0)
3726 ret = FAIL;
3727 else
3728 {
3729 if (**arg == '(') /* recursive! */
3730 {
3731 /* If "s" is the name of a variable of type VAR_FUNC
3732 * use its contents. */
3733 s = deref_func_name(s, &len);
3734
3735 /* Invoke the function. */
3736 ret = get_func_tv(s, len, rettv, arg,
3737 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003738 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003739 /* Stop the expression evaluation when immediately
3740 * aborting on error, or when an interrupt occurred or
3741 * an exception was thrown but not caught. */
3742 if (aborting())
3743 {
3744 if (ret == OK)
3745 clear_tv(rettv);
3746 ret = FAIL;
3747 }
3748 }
3749 else if (evaluate)
3750 ret = get_var_tv(s, len, rettv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003751 else
3752 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003753 }
3754
3755 if (alias != NULL)
3756 vim_free(alias);
3757 }
3758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 *arg = skipwhite(*arg);
3760
3761 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00003763 * Also handle function call with Funcref variable: func(expr)
3764 * Can all be combined: dict.func(expr)[idx].func(expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003766 selfdict = NULL;
3767 while (ret == OK
3768 && (**arg == '['
3769 || (**arg == '.' && rettv->v_type == VAR_DICT)
3770 || (**arg == '(' && rettv->v_type == VAR_FUNC))
3771 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003773 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003775 s = rettv->vval.v_string;
3776
3777 /* Invoke the function. Recursive! */
3778 ret = get_func_tv(s, STRLEN(s), rettv, arg,
3779 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3780 &len, evaluate, selfdict);
3781
3782 /* Stop the expression evaluation when immediately
3783 * aborting on error, or when an interrupt occurred or
3784 * an exception was thrown but not caught. */
3785 if (aborting())
3786 {
3787 if (ret == OK)
3788 clear_tv(rettv);
3789 ret = FAIL;
3790 }
3791 selfdict = NULL;
3792 }
3793 else
3794 {
3795 if (rettv->v_type == VAR_DICT)
3796 selfdict = rettv->vval.v_dict;
3797 else
3798 selfdict = NULL;
3799 if (eval_index(arg, rettv, evaluate) == FAIL)
3800 {
3801 clear_tv(rettv);
3802 ret = FAIL;
3803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806
3807 /*
3808 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3809 */
3810 if (ret == OK && evaluate && end_leader > start_leader)
3811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 while (end_leader > start_leader)
3814 {
3815 --end_leader;
3816 if (*end_leader == '!')
3817 val = !val;
3818 else if (*end_leader == '-')
3819 val = -val;
3820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 clear_tv(rettv);
3822 rettv->v_type = VAR_NUMBER;
3823 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825
3826 return ret;
3827}
3828
3829/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003830 * Evaluate an "[expr]" or "[expr:expr]" index.
3831 * "*arg" points to the '['.
3832 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3833 */
3834 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003836 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003838 int evaluate;
3839{
3840 int empty1 = FALSE, empty2 = FALSE;
3841 typeval var1, var2;
3842 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003843 long len = -1;
3844 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003846 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003849 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003850 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003851 return FAIL;
3852 }
3853
Bram Moolenaar8c711452005-01-14 21:53:12 +00003854 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 /*
3857 * dict.name
3858 */
3859 key = *arg + 1;
3860 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3861 ;
3862 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003863 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003864 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003865 }
3866 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003868 /*
3869 * something[idx]
3870 *
3871 * Get the (first) variable from inside the [].
3872 */
3873 *arg = skipwhite(*arg + 1);
3874 if (**arg == ':')
3875 empty1 = TRUE;
3876 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3877 return FAIL;
3878
3879 /*
3880 * Get the second variable from inside the [:].
3881 */
3882 if (**arg == ':')
3883 {
3884 range = TRUE;
3885 *arg = skipwhite(*arg + 1);
3886 if (**arg == ']')
3887 empty2 = TRUE;
3888 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3889 {
3890 clear_tv(&var1);
3891 return FAIL;
3892 }
3893 }
3894
3895 /* Check for the ']'. */
3896 if (**arg != ']')
3897 {
3898 EMSG(_(e_missbrac));
3899 clear_tv(&var1);
3900 if (range)
3901 clear_tv(&var2);
3902 return FAIL;
3903 }
3904 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003905 }
3906
3907 if (evaluate)
3908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003909 n1 = 0;
3910 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912 n1 = get_tv_number(&var1);
3913 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003914 }
3915 if (range)
3916 {
3917 if (empty2)
3918 n2 = -1;
3919 else
3920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003921 n2 = get_tv_number(&var2);
3922 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003923 }
3924 }
3925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003927 {
3928 case VAR_NUMBER:
3929 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003931 len = (long)STRLEN(s);
3932 if (range)
3933 {
3934 /* The resulting variable is a substring. If the indexes
3935 * are out of range the result is empty. */
3936 if (n1 < 0)
3937 {
3938 n1 = len + n1;
3939 if (n1 < 0)
3940 n1 = 0;
3941 }
3942 if (n2 < 0)
3943 n2 = len + n2;
3944 else if (n2 >= len)
3945 n2 = len;
3946 if (n1 >= len || n2 < 0 || n1 > n2)
3947 s = NULL;
3948 else
3949 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3950 }
3951 else
3952 {
3953 /* The resulting variable is a string of a single
3954 * character. If the index is too big or negative the
3955 * result is empty. */
3956 if (n1 >= len || n1 < 0)
3957 s = NULL;
3958 else
3959 s = vim_strnsave(s + n1, 1);
3960 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 clear_tv(rettv);
3962 rettv->v_type = VAR_STRING;
3963 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964 break;
3965
3966 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 if (n1 < 0)
3969 n1 = len + n1;
3970 if (!empty1 && (n1 < 0 || n1 >= len))
3971 {
3972 EMSGN(_(e_listidx), n1);
3973 return FAIL;
3974 }
3975 if (range)
3976 {
3977 listvar *l;
3978 listitem *item;
3979
3980 if (n2 < 0)
3981 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003982 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003983 {
3984 EMSGN(_(e_listidx), n2);
3985 return FAIL;
3986 }
3987 l = list_alloc();
3988 if (l == NULL)
3989 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003991 n1 <= n2; ++n1)
3992 {
3993 if (list_append_tv(l, &item->li_tv) == FAIL)
3994 {
3995 list_free(l);
3996 return FAIL;
3997 }
3998 item = item->li_next;
3999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000 clear_tv(rettv);
4001 rettv->v_type = VAR_LIST;
4002 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004003 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004004 }
4005 else
4006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004009 clear_tv(rettv);
4010 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004011 }
4012 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013
4014 case VAR_DICT:
4015 if (range)
4016 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004017 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018 if (len == -1)
4019 clear_tv(&var1);
4020 return FAIL;
4021 }
4022 {
4023 dictitem *item;
4024
4025 if (len == -1)
4026 {
4027 key = get_tv_string(&var1);
4028 if (*key == NUL)
4029 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004030 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 clear_tv(&var1);
4032 return FAIL;
4033 }
4034 }
4035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004036 item = dict_find(rettv->vval.v_dict, key, (int)len, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037
4038 if (item == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004039 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040 if (len == -1)
4041 clear_tv(&var1);
4042 if (item == NULL)
4043 return FAIL;
4044
4045 copy_tv(&item->di_tv, &var1);
4046 clear_tv(rettv);
4047 *rettv = var1;
4048 }
4049 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004050 }
4051 }
4052
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004053 return OK;
4054}
4055
4056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 * Get an option value.
4058 * "arg" points to the '&' or '+' before the option name.
4059 * "arg" is advanced to character after the option name.
4060 * Return OK or FAIL.
4061 */
4062 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 int evaluate;
4067{
4068 char_u *option_end;
4069 long numval;
4070 char_u *stringval;
4071 int opt_type;
4072 int c;
4073 int working = (**arg == '+'); /* has("+option") */
4074 int ret = OK;
4075 int opt_flags;
4076
4077 /*
4078 * Isolate the option name and find its value.
4079 */
4080 option_end = find_option_end(arg, &opt_flags);
4081 if (option_end == NULL)
4082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 EMSG2(_("E112: Option name missing: %s"), *arg);
4085 return FAIL;
4086 }
4087
4088 if (!evaluate)
4089 {
4090 *arg = option_end;
4091 return OK;
4092 }
4093
4094 c = *option_end;
4095 *option_end = NUL;
4096 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 if (opt_type == -3) /* invalid name */
4100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 EMSG2(_("E113: Unknown option: %s"), *arg);
4103 ret = FAIL;
4104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 if (opt_type == -2) /* hidden string option */
4108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 rettv->v_type = VAR_STRING;
4110 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 else if (opt_type == -1) /* hidden number option */
4113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 rettv->v_type = VAR_NUMBER;
4115 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 else if (opt_type == 1) /* number option */
4118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 rettv->v_type = VAR_NUMBER;
4120 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 else /* string option */
4123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 rettv->v_type = VAR_STRING;
4125 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 }
4128 else if (working && (opt_type == -2 || opt_type == -1))
4129 ret = FAIL;
4130
4131 *option_end = c; /* put back for error messages */
4132 *arg = option_end;
4133
4134 return ret;
4135}
4136
4137/*
4138 * Allocate a variable for a string constant.
4139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int evaluate;
4146{
4147 char_u *p;
4148 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int extra = 0;
4150
4151 /*
4152 * Find the end of the string, skipping backslashed characters.
4153 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004154 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
4156 if (*p == '\\' && p[1] != NUL)
4157 {
4158 ++p;
4159 /* A "\<x>" form occupies at least 4 characters, and produces up
4160 * to 6 characters: reserve space for 2 extra */
4161 if (*p == '<')
4162 extra += 2;
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165
4166 if (*p != '"')
4167 {
4168 EMSG2(_("E114: Missing quote: %s"), *arg);
4169 return FAIL;
4170 }
4171
4172 /* If only parsing, set *arg and return here */
4173 if (!evaluate)
4174 {
4175 *arg = p + 1;
4176 return OK;
4177 }
4178
4179 /*
4180 * Copy the string into allocated memory, handling backslashed
4181 * characters.
4182 */
4183 name = alloc((unsigned)(p - *arg + extra));
4184 if (name == NULL)
4185 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004186 rettv->v_type = VAR_STRING;
4187 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar8c711452005-01-14 21:53:12 +00004189 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
4191 if (*p == '\\')
4192 {
4193 switch (*++p)
4194 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004195 case 'b': *name++ = BS; ++p; break;
4196 case 'e': *name++ = ESC; ++p; break;
4197 case 'f': *name++ = FF; ++p; break;
4198 case 'n': *name++ = NL; ++p; break;
4199 case 'r': *name++ = CAR; ++p; break;
4200 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 case 'X': /* hex: "\x1", "\x12" */
4203 case 'x':
4204 case 'u': /* Unicode: "\u0023" */
4205 case 'U':
4206 if (vim_isxdigit(p[1]))
4207 {
4208 int n, nr;
4209 int c = toupper(*p);
4210
4211 if (c == 'X')
4212 n = 2;
4213 else
4214 n = 4;
4215 nr = 0;
4216 while (--n >= 0 && vim_isxdigit(p[1]))
4217 {
4218 ++p;
4219 nr = (nr << 4) + hex2nr(*p);
4220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004221 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef FEAT_MBYTE
4223 /* For "\u" store the number according to
4224 * 'encoding'. */
4225 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004226 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 else
4228#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004229 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 break;
4232
4233 /* octal: "\1", "\12", "\123" */
4234 case '0':
4235 case '1':
4236 case '2':
4237 case '3':
4238 case '4':
4239 case '5':
4240 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 case '7': *name = *p++ - '0';
4242 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004244 *name = (*name << 3) + *p++ - '0';
4245 if (*p >= '0' && *p <= '7')
4246 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 break;
4250
4251 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (extra != 0)
4254 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004255 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 break;
4257 }
4258 /* FALLTHROUGH */
4259
Bram Moolenaar8c711452005-01-14 21:53:12 +00004260 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 break;
4262 }
4263 }
4264 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004265 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004268 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 *arg = p + 1;
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 return OK;
4272}
4273
4274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004275 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 * Return OK or FAIL.
4277 */
4278 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int evaluate;
4283{
4284 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004285 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004286 int reduce = 0;
4287
4288 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004289 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004290 */
4291 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4292 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004293 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004294 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004295 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004296 break;
4297 ++reduce;
4298 ++p;
4299 }
4300 }
4301
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004303 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004305 return FAIL;
4306 }
4307
Bram Moolenaar8c711452005-01-14 21:53:12 +00004308 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004309 if (!evaluate)
4310 {
4311 *arg = p + 1;
4312 return OK;
4313 }
4314
4315 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004316 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004317 */
4318 str = alloc((unsigned)((p - *arg) - reduce));
4319 if (str == NULL)
4320 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004321 rettv->v_type = VAR_STRING;
4322 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004323
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004325 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004327 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004328 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004329 break;
4330 ++p;
4331 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004332 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004333 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004334 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004335 *arg = p + 1;
4336
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004337 return OK;
4338}
4339
4340/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004341 * Allocate a variable for a List and fill it from "*arg".
4342 * Return OK or FAIL.
4343 */
4344 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004346 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004348 int evaluate;
4349{
4350 listvar *l = NULL;
4351 typeval tv;
4352 listitem *item;
4353
4354 if (evaluate)
4355 {
4356 l = list_alloc();
4357 if (l == NULL)
4358 return FAIL;
4359 }
4360
4361 *arg = skipwhite(*arg + 1);
4362 while (**arg != ']' && **arg != NUL)
4363 {
4364 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4365 goto failret;
4366 if (evaluate)
4367 {
4368 item = listitem_alloc();
4369 if (item != NULL)
4370 {
4371 item->li_tv = tv;
4372 list_append(l, item);
4373 }
4374 }
4375
4376 if (**arg == ']')
4377 break;
4378 if (**arg != ',')
4379 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004381 goto failret;
4382 }
4383 *arg = skipwhite(*arg + 1);
4384 }
4385
4386 if (**arg != ']')
4387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389failret:
4390 if (evaluate)
4391 list_free(l);
4392 return FAIL;
4393 }
4394
4395 *arg = skipwhite(*arg + 1);
4396 if (evaluate)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 rettv->v_type = VAR_LIST;
4399 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004400 ++l->lv_refcount;
4401 }
4402
4403 return OK;
4404}
4405
4406/*
4407 * Allocate an empty header for a list.
4408 */
4409 static listvar *
4410list_alloc()
4411{
4412 return (listvar *)alloc_clear(sizeof(listvar));
4413}
4414
4415/*
4416 * Unreference a list: decrement the reference count and free it when it
4417 * becomes zero.
4418 */
4419 static void
4420list_unref(l)
4421 listvar *l;
4422{
4423 if (l != NULL && --l->lv_refcount <= 0)
4424 list_free(l);
4425}
4426
4427/*
4428 * Free a list, including all items it points to.
4429 * Ignores the reference count.
4430 */
4431 static void
4432list_free(l)
4433 listvar *l;
4434{
4435 listitem *item;
4436 listitem *next;
4437
4438 for (item = l->lv_first; item != NULL; item = next)
4439 {
4440 next = item->li_next;
4441 listitem_free(item);
4442 }
4443 vim_free(l);
4444}
4445
4446/*
4447 * Allocate a list item.
4448 */
4449 static listitem *
4450listitem_alloc()
4451{
4452 return (listitem *)alloc(sizeof(listitem));
4453}
4454
4455/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004456 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 */
4458 static void
4459listitem_free(item)
4460 listitem *item;
4461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463 vim_free(item);
4464}
4465
4466/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004467 * Remove a list item from a List and free it. Also clears the value.
4468 */
4469 static void
4470listitem_remove(l, item)
4471 listvar *l;
4472 listitem *item;
4473{
4474 list_remove(l, item, item);
4475 listitem_free(item);
4476}
4477
4478/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004479 * Get the number of items in a list.
4480 */
4481 static long
4482list_len(l)
4483 listvar *l;
4484{
4485 listitem *item;
4486 long len = 0;
4487
4488 if (l == NULL)
4489 return 0L;
4490 for (item = l->lv_first; item != NULL; item = item->li_next)
4491 ++len;
4492 return len;
4493}
4494
4495/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 * Return TRUE when two lists have exactly the same values.
4497 */
4498 static int
4499list_equal(l1, l2, ic)
4500 listvar *l1;
4501 listvar *l2;
4502 int ic; /* ignore case for strings */
4503{
4504 listitem *item1, *item2;
4505
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004506 if (list_len(l1) != list_len(l2))
4507 return FALSE;
4508
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 for (item1 = l1->lv_first, item2 = l2->lv_first;
4510 item1 != NULL && item2 != NULL;
4511 item1 = item1->li_next, item2 = item2->li_next)
4512 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4513 return FALSE;
4514 return item1 == NULL && item2 == NULL;
4515}
4516
4517/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 * Return TRUE when two dictionaries have exactly the same key/values.
4519 */
4520 static int
4521dict_equal(d1, d2, ic)
4522 dictvar *d1;
4523 dictvar *d2;
4524 int ic; /* ignore case for strings */
4525{
4526 dictitem *item1, *item2;
4527
4528 if (dict_len(d1) != dict_len(d2))
4529 return FALSE;
4530
4531 for (item1 = d1->dv_first; item1 != NULL; item1 = item1->di_next)
4532 {
4533 item2 = dict_find(d2, item1->di_key, -1, NULL);
4534 if (item2 == NULL)
4535 return FALSE;
4536 if (!tv_equal(&item1->di_tv, &item2->di_tv, ic))
4537 return FALSE;
4538 }
4539 return TRUE;
4540}
4541
4542/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 * Return TRUE if "tv1" and "tv2" have the same value.
4544 * Compares the items just like "==" would compare them.
4545 */
4546 static int
4547tv_equal(tv1, tv2, ic)
4548 typeval *tv1;
4549 typeval *tv2;
4550 int ic; /* ignore case */
4551{
4552 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4553
4554 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4555 {
4556 /* recursive! */
4557 if (tv1->v_type != tv2->v_type
4558 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4559 return FALSE;
4560 }
4561 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4562 {
4563 if (tv1->v_type != tv2->v_type
4564 || tv1->vval.v_string == NULL
4565 || tv2->vval.v_string == NULL
4566 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4567 return FALSE;
4568 }
4569 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4570 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004571 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4572 * Don't consider "4x" to be equal to 4. */
4573 if ((tv1->v_type == VAR_STRING
4574 && !string_isa_number(tv1->vval.v_string))
4575 || (tv2->v_type == VAR_STRING
4576 && !string_isa_number(tv2->vval.v_string)))
4577 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 if (get_tv_number(tv1) != get_tv_number(tv2))
4579 return FALSE;
4580 }
4581 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4582 get_tv_string_buf(tv2, buf2)) != 0)
4583 return FALSE;
4584 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4585 get_tv_string_buf(tv2, buf2)) != 0)
4586 return FALSE;
4587 return TRUE;
4588}
4589
4590/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004591 * Return TRUE if "tv" is a number without other non-white characters.
4592 */
4593 static int
4594string_isa_number(s)
4595 char_u *s;
4596{
4597 int len;
4598
4599 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4600 return len > 0 && *skipwhite(s + len) == NUL;
4601}
4602
4603/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 * Locate item with index "n" in list "l" and return it.
4605 * A negative index is counted from the end; -1 is the last item.
4606 * Returns NULL when "n" is out of range.
4607 */
4608 static listitem *
4609list_find(l, n)
4610 listvar *l;
4611 long n;
4612{
4613 listitem *item;
4614 long idx;
4615
4616 if (l == NULL)
4617 return NULL;
4618 if (n < 0)
4619 {
4620 idx = -1; /* search from the end */
4621 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4622 --idx;
4623 }
4624 else
4625 {
4626 idx = 0; /* search from the start */
4627 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4628 ++idx;
4629 }
4630 if (idx != n)
4631 return NULL;
4632 return item;
4633}
4634
4635/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004636 * Locate "item" list "l" and return its index.
4637 * Returns -1 when "item" is not in the list.
4638 */
4639 static long
4640list_idx_of_item(l, item)
4641 listvar *l;
4642 listitem *item;
4643{
4644 long idx = 0;
4645 listitem *li;
4646
4647 if (l == NULL)
4648 return -1;
4649 idx = 0;
4650 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4651 ++idx;
4652 if (li == NULL)
4653 return -1;
4654 return idx;;
4655}
4656
4657/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004658 * Like list_find(), but also find an item just past the end.
4659 * "*ip" is the item to find.
4660 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4661 * Returns NULL when item not found or item is just past the end.
4662 */
4663 static listitem *
4664list_find_ext(l, ip)
4665 listvar *l;
4666 long *ip;
4667{
4668 long n;
4669 listitem *item;
4670
4671 if (*ip < 0)
4672 {
4673 /* Count from the end: -1 is before last item. */
4674 item = l->lv_last;
4675 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4676 item = item->li_prev;
4677 if (item == NULL)
4678 n = 1; /* error! */
4679 }
4680 else
4681 {
4682 item = l->lv_first;
4683 for (n = *ip; n > 0 && item != NULL; --n)
4684 item = item->li_next;
4685 }
4686 *ip = n;
4687 return item;
4688}
4689
4690/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 * Append item "item" to the end of list "l".
4692 */
4693 static void
4694list_append(l, item)
4695 listvar *l;
4696 listitem *item;
4697{
4698 if (l->lv_last == NULL)
4699 {
4700 /* empty list */
4701 l->lv_first = item;
4702 l->lv_last = item;
4703 item->li_prev = NULL;
4704 }
4705 else
4706 {
4707 l->lv_last->li_next = item;
4708 item->li_prev = l->lv_last;
4709 l->lv_last = item;
4710 }
4711 item->li_next = NULL;
4712}
4713
4714/*
4715 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004716 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 */
4718 static int
4719list_append_tv(l, tv)
4720 listvar *l;
4721 typeval *tv;
4722{
4723 listitem *ni = listitem_alloc();
4724
4725 if (ni == NULL)
4726 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 list_append(l, ni);
4729 return OK;
4730}
4731
4732/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004733 * Insert typeval "tv" in list "l" before "item".
4734 * If "item" is NULL append at the end.
4735 * Return FAIL when out of memory.
4736 */
4737 static int
4738list_insert_tv(l, tv, item)
4739 listvar *l;
4740 typeval *tv;
4741 listitem *item;
4742{
4743 listitem *ni = listitem_alloc();
4744
4745 if (ni == NULL)
4746 return FAIL;
4747 copy_tv(tv, &ni->li_tv);
4748 if (item == NULL)
4749 /* Append new item at end of list. */
4750 list_append(l, ni);
4751 else
4752 {
4753 /* Insert new item before existing item. */
4754 ni->li_prev = item->li_prev;
4755 ni->li_next = item;
4756 if (item->li_prev == NULL)
4757 l->lv_first = ni;
4758 else
4759 item->li_prev->li_next = ni;
4760 item->li_prev = ni;
4761 }
4762 return OK;
4763}
4764
4765/*
4766 * Extend "l1" with "l2".
4767 * If "bef" is NULL append at the end, otherwise insert before this item.
4768 * Returns FAIL when out of memory.
4769 */
4770 static int
4771list_extend(l1, l2, bef)
4772 listvar *l1;
4773 listvar *l2;
4774 listitem *bef;
4775{
4776 listitem *item;
4777
4778 for (item = l2->lv_first; item != NULL; item = item->li_next)
4779 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4780 return FAIL;
4781 return OK;
4782}
4783
4784/*
4785 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4786 * Return FAIL when out of memory.
4787 */
4788 static int
4789list_concat(l1, l2, tv)
4790 listvar *l1;
4791 listvar *l2;
4792 typeval *tv;
4793{
4794 listvar *l;
4795
4796 /* make a copy of the first list. */
4797 l = list_copy(l1, FALSE);
4798 if (l == NULL)
4799 return FAIL;
4800 tv->v_type = VAR_LIST;
4801 tv->vval.v_list = l;
4802
4803 /* append all items from the second list */
4804 return list_extend(l, l2, NULL);
4805}
4806
4807/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004808 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809 * The refcount of the new list is set to 1.
4810 * Returns NULL when out of memory.
4811 */
4812 static listvar *
4813list_copy(orig, deep)
4814 listvar *orig;
4815 int deep;
4816{
4817 listvar *copy;
4818 listitem *item;
4819 listitem *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820
4821 if (orig == NULL)
4822 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823
4824 copy = list_alloc();
4825 if (copy != NULL)
4826 {
4827 for (item = orig->lv_first; item != NULL; item = item->li_next)
4828 {
4829 ni = listitem_alloc();
4830 if (ni == NULL)
4831 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004832 if (deep)
4833 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836 list_append(copy, ni);
4837 }
4838 ++copy->lv_refcount;
4839 }
4840
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841 return copy;
4842}
4843
4844/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004845 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004846 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004848 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004849list_remove(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004852 listitem *item2;
4853{
4854 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004856 /* notify watchers */
4857 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004859 list_fix_watch(l, ip);
4860 if (ip == item2)
4861 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004863
4864 if (item2->li_next == NULL)
4865 l->lv_last = item->li_prev;
4866 else
4867 item2->li_next->li_prev = item->li_prev;
4868 if (item->li_prev == NULL)
4869 l->lv_first = item2->li_next;
4870 else
4871 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872}
4873
4874/*
4875 * Return an allocated string with the string representation of a list.
4876 * May return NULL.
4877 */
4878 static char_u *
4879list2string(tv)
4880 typeval *tv;
4881{
4882 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883
4884 if (tv->vval.v_list == NULL)
4885 return NULL;
4886 ga_init2(&ga, (int)sizeof(char), 80);
4887 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004888 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 ga_append(&ga, ']');
4890 ga_append(&ga, NUL);
4891 return (char_u *)ga.ga_data;
4892}
4893
4894/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004895 * Join list "l" into a string in "*gap", using separator "sep".
4896 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4897 */
4898 static void
4899list_join(gap, l, sep, echo)
4900 garray_T *gap;
4901 listvar *l;
4902 char_u *sep;
4903 int echo;
4904{
4905 int first = TRUE;
4906 char_u *tofree;
4907 char_u numbuf[NUMBUFLEN];
4908 listitem *item;
4909 char_u *s;
4910
4911 for (item = l->lv_first; item != NULL; item = item->li_next)
4912 {
4913 if (first)
4914 first = FALSE;
4915 else
4916 ga_concat(gap, sep);
4917
4918 if (echo)
4919 s = echo_string(&item->li_tv, &tofree, numbuf);
4920 else
4921 s = tv2string(&item->li_tv, &tofree, numbuf);
4922 if (s != NULL)
4923 ga_concat(gap, s);
4924 vim_free(tofree);
4925 }
4926}
4927
4928/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929 * Allocate an empty header for a dictionary.
4930 */
4931 static dictvar *
4932dict_alloc()
4933{
4934 return (dictvar *)alloc_clear(sizeof(dictvar));
4935}
4936
4937/*
4938 * Unreference a Dictionary: decrement the reference count and free it when it
4939 * becomes zero.
4940 */
4941 static void
4942dict_unref(d)
4943 dictvar *d;
4944{
4945 if (d != NULL && --d->dv_refcount <= 0)
4946 dict_free(d);
4947}
4948
4949/*
4950 * Free a Dictionary, including all items it contains.
4951 * Ignores the reference count.
4952 */
4953 static void
4954dict_free(d)
4955 dictvar *d;
4956{
4957 dictitem *item;
4958 dictitem *next;
4959
4960 for (item = d->dv_first; item != NULL; item = next)
4961 {
4962 next = item->di_next;
4963 dictitem_free(item);
4964 }
4965 vim_free(d);
4966}
4967
4968/*
4969 * Allocate a Dictionary item.
4970 */
4971 static dictitem *
4972dictitem_alloc()
4973{
4974 return (dictitem *)alloc(sizeof(dictitem));
4975}
4976
4977/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004978 * Make a copy of a Dictionary item.
4979 */
4980 static dictitem *
4981dictitem_copy(org)
4982 dictitem *org;
4983{
4984 dictitem *di;
4985
4986 di = (dictitem *)alloc(sizeof(dictitem));
4987 if (di != NULL)
4988 {
4989 di->di_key = vim_strsave(org->di_key);
4990 if (di->di_key == NULL)
4991 {
4992 vim_free(di);
4993 return NULL;
4994 }
4995 copy_tv(&org->di_tv, &di->di_tv);
4996 }
4997 return di;
4998}
4999
5000/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 * Free a dict item. Also clears the value.
5002 */
5003 static void
5004dictitem_free(item)
5005 dictitem *item;
5006{
5007 vim_free(item->di_key);
5008 clear_tv(&item->di_tv);
5009 vim_free(item);
5010}
5011
5012/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005013 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5014 * The refcount of the new dict is set to 1.
5015 * Returns NULL when out of memory.
5016 */
5017 static dictvar *
5018dict_copy(orig, deep)
5019 dictvar *orig;
5020 int deep;
5021{
5022 dictvar *copy;
5023 dictitem *item;
5024 dictitem *di;
5025
5026 if (orig == NULL)
5027 return NULL;
5028
5029 copy = dict_alloc();
5030 if (copy != NULL)
5031 {
5032 for (item = orig->dv_first; item != NULL; item = item->di_next)
5033 {
5034 di = dictitem_alloc();
5035 if (di == NULL)
5036 break;
5037 di->di_key = vim_strsave(item->di_key);
5038 if (di->di_key == NULL)
5039 {
5040 vim_free(di);
5041 break;
5042 }
5043 if (deep)
5044 item_copy(&item->di_tv, &di->di_tv, deep);
5045 else
5046 copy_tv(&item->di_tv, &di->di_tv);
5047 dict_add(copy, di);
5048 }
5049 ++copy->dv_refcount;
5050 }
5051
5052 return copy;
5053}
5054
5055/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Add item "item" to Dictionary "d".
5057 */
5058 static void
5059dict_add(d, item)
5060 dictvar *d;
5061 dictitem *item;
5062{
5063 item->di_next = d->dv_first;
5064 d->dv_first = item;
5065}
5066
5067#if 0 /* not currently used */
5068static void dict_set_item __ARGS((dictvar *d, int type, char *key, void *val));
5069
5070/*
5071 * Add an item to Dictionary "d" with type "type", key "key" and value "val".
5072 * If it already exists it is overwritten.
5073 * The key and value are copied to allocated memory.
5074 */
5075 static void
5076dict_set_item(d, type, key, val)
5077 dictvar *d;
5078 int type;
5079 char *key;
5080 void *val;
5081{
5082 dictitem *di;
5083 char_u *dkey;
5084
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005085 di = dict_find(d, (char_u *)key, -1, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 if (di == NULL)
5087 {
5088 dkey = vim_strsave((char_u *)key);
5089 if (dkey != NULL)
5090 {
5091 di = dictitem_alloc();
5092 if (di == NULL)
5093 vim_free(dkey);
5094 else
5095 di->di_key = dkey;
5096 }
5097 }
5098 else
5099 clear_tv(&di->di_tv);
5100
5101 if (di != NULL)
5102 {
5103 di->di_tv.v_type = type;
5104 switch (type)
5105 {
5106 case VAR_NUMBER:
5107 di->di_tv.vval.v_number = (varnumber_T)val;
5108 break;
5109 case VAR_FUNC:
5110 case VAR_STRING:
5111 di->di_tv.vval.v_string = vim_strsave((char_u *)val);
5112 break;
5113 default:
5114 EMSG2(_(e_intern2), "dict_set_item()");
5115 }
5116 dict_add(d, di);
5117 }
5118}
5119#endif
5120
5121/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005122 * Get the number of items in a Dictionary.
5123 */
5124 static long
5125dict_len(d)
5126 dictvar *d;
5127{
5128 dictitem *item;
5129 long len = 0;
5130
5131 if (d == NULL)
5132 return 0L;
5133 for (item = d->dv_first; item != NULL; item = item->di_next)
5134 ++len;
5135 return len;
5136}
5137
5138/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 * Find item "key[len]" in Dictionary "d".
5140 * If "len" is negative use strlen(key).
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005141 * Sets "*pdi" to pointer to found item, unless "pdi" is NULL.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 * Returns NULL when not found.
5143 */
5144 static dictitem *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005145dict_find(d, key, len, pdi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 dictvar *d;
5147 char_u *key;
5148 int len;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005149 dictitem ***pdi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150{
5151 static dictitem *di;
5152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005153 if (pdi != NULL)
5154 *pdi = &d->dv_first;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 for (di = d->dv_first; di != NULL; di = di->di_next)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005156 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 if (len < 0
5158 ? STRCMP(di->di_key, key) == 0
5159 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL)
5160 return di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005161 if (pdi != NULL)
5162 *pdi = &di->di_next;
5163 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 return NULL;
5165}
5166
5167/*
5168 * Return an allocated string with the string representation of a Dictionary.
5169 * May return NULL.
5170 */
5171 static char_u *
5172dict2string(tv)
5173 typeval *tv;
5174{
5175 garray_T ga;
5176 int first = TRUE;
5177 char_u *tofree;
5178 char_u numbuf[NUMBUFLEN];
5179 dictitem *item;
5180 char_u *s;
5181
5182 if (tv->vval.v_dict == NULL)
5183 return NULL;
5184 ga_init2(&ga, (int)sizeof(char), 80);
5185 ga_append(&ga, '{');
5186
5187 for (item = tv->vval.v_dict->dv_first; item != NULL; item = item->di_next)
5188 {
5189 if (first)
5190 first = FALSE;
5191 else
5192 ga_concat(&ga, (char_u *)", ");
5193
5194 tofree = string_quote(item->di_key, FALSE);
5195 if (tofree != NULL)
5196 {
5197 ga_concat(&ga, tofree);
5198 vim_free(tofree);
5199 }
5200 ga_concat(&ga, (char_u *)": ");
5201 s = tv2string(&item->di_tv, &tofree, numbuf);
5202 if (s != NULL)
5203 ga_concat(&ga, s);
5204 vim_free(tofree);
5205 }
5206
5207 ga_append(&ga, '}');
5208 ga_append(&ga, NUL);
5209 return (char_u *)ga.ga_data;
5210}
5211
5212/*
5213 * Allocate a variable for a Dictionary and fill it from "*arg".
5214 * Return OK or FAIL. Returns NOTDONE for {expr}.
5215 */
5216 static int
5217get_dict_tv(arg, rettv, evaluate)
5218 char_u **arg;
5219 typeval *rettv;
5220 int evaluate;
5221{
5222 dictvar *d = NULL;
5223 typeval tv;
5224 char_u *key;
5225 dictitem *item;
5226 char_u *start = skipwhite(*arg + 1);
5227
5228 /*
5229 * First check if it's not a curly-braces thing: {expr}.
5230 * Must do this without evaluating, otherwise a function may be called
5231 * twice. Unfortunately this means we need to call eval1() twice for the
5232 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005233 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005235 if (*start != '}')
5236 {
5237 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5238 return FAIL;
5239 if (*start == '}')
5240 return NOTDONE;
5241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242
5243 if (evaluate)
5244 {
5245 d = dict_alloc();
5246 if (d == NULL)
5247 return FAIL;
5248 }
5249
5250 *arg = skipwhite(*arg + 1);
5251 while (**arg != '}' && **arg != NUL)
5252 {
5253 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5254 goto failret;
5255 if (**arg != ':')
5256 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005257 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 clear_tv(&tv);
5259 goto failret;
5260 }
5261 key = get_tv_string(&tv);
5262 if (*key == NUL)
5263 {
5264 EMSG(_(e_emptykey));
5265 clear_tv(&tv);
5266 goto failret;
5267 }
5268 key = vim_strsave(key);
5269 clear_tv(&tv);
5270 if (key == NULL)
5271 goto failret;
5272
5273 *arg = skipwhite(*arg + 1);
5274 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5275 {
5276 vim_free(key);
5277 goto failret;
5278 }
5279 if (evaluate)
5280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005281 item = dict_find(d, key, -1, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (item != NULL)
5283 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005284 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 vim_free(key);
5286 clear_tv(&tv);
5287 goto failret;
5288 }
5289 item = dictitem_alloc();
5290 if (item == NULL)
5291 vim_free(key);
5292 else
5293 {
5294 item->di_key = key;
5295 item->di_tv = tv;
5296 dict_add(d, item);
5297 }
5298 }
5299
5300 if (**arg == '}')
5301 break;
5302 if (**arg != ',')
5303 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005304 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 goto failret;
5306 }
5307 *arg = skipwhite(*arg + 1);
5308 }
5309
5310 if (**arg != '}')
5311 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313failret:
5314 if (evaluate)
5315 dict_free(d);
5316 return FAIL;
5317 }
5318
5319 *arg = skipwhite(*arg + 1);
5320 if (evaluate)
5321 {
5322 rettv->v_type = VAR_DICT;
5323 rettv->vval.v_dict = d;
5324 ++d->dv_refcount;
5325 }
5326
5327 return OK;
5328}
5329
Bram Moolenaar8c711452005-01-14 21:53:12 +00005330/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 * Return a string with the string representation of a variable.
5332 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005333 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005334 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 * May return NULL;
5336 */
5337 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005338echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 typeval *tv;
5340 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005341 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005343 static int recurse = 0;
5344 char_u *r = NULL;
5345
5346 if (recurse >= VAR_MAXNEST)
5347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005348 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005349 *tofree = NULL;
5350 return NULL;
5351 }
5352 ++recurse;
5353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 switch (tv->v_type)
5355 {
5356 case VAR_FUNC:
5357 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005358 r = tv->vval.v_string;
5359 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 case VAR_LIST:
5361 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005362 r = *tofree;
5363 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 case VAR_DICT:
5365 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005366 r = *tofree;
5367 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 case VAR_STRING:
5369 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005370 *tofree = NULL;
5371 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005374 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005375 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005377
5378 --recurse;
5379 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005380}
5381
5382/*
5383 * Return a string with the string representation of a variable.
5384 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5385 * "numbuf" is used for a number.
5386 * Puts quotes around strings, so that they can be parsed back by eval().
5387 * May return NULL;
5388 */
5389 static char_u *
5390tv2string(tv, tofree, numbuf)
5391 typeval *tv;
5392 char_u **tofree;
5393 char_u *numbuf;
5394{
5395 switch (tv->v_type)
5396 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005397 case VAR_FUNC:
5398 *tofree = string_quote(tv->vval.v_string, TRUE);
5399 return *tofree;
5400 case VAR_STRING:
5401 *tofree = string_quote(tv->vval.v_string, FALSE);
5402 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005403 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005404 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005406 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005407 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005410 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411}
5412
5413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414 * Return a string in ' quotes, doubling ' characters.
5415 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005416 */
5417 static char_u *
5418string_quote(str, function)
5419 char_u *str;
5420 int function;
5421{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005422 unsigned len = STRLEN(str) + (function ? 13 : 3);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005423 char_u *p, *r, *s;
5424
5425 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005427 ++len;
5428 s = r = alloc(len);
5429 if (r != NULL)
5430 {
5431 if (function)
5432 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005434 r += 10;
5435 }
5436 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 *r++ = '\'';
5438 for (p = str; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005439 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005440 if (*p == '\'')
5441 *r++ = '\'';
5442 MB_COPY_CHAR(p, r);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005445 if (function)
5446 *r++ = ')';
5447 *r++ = NUL;
5448 }
5449 return s;
5450}
5451
5452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 * Get the value of an environment variable.
5454 * "arg" is pointing to the '$'. It is advanced to after the name.
5455 * If the environment variable was not set, silently assume it is empty.
5456 * Always return OK.
5457 */
5458 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 int evaluate;
5463{
5464 char_u *string = NULL;
5465 int len;
5466 int cc;
5467 char_u *name;
5468
5469 ++*arg;
5470 name = *arg;
5471 len = get_env_len(arg);
5472 if (evaluate)
5473 {
5474 if (len != 0)
5475 {
5476 cc = name[len];
5477 name[len] = NUL;
5478 /* first try mch_getenv(), fast for normal environment vars */
5479 string = mch_getenv(name);
5480 if (string != NULL && *string != NUL)
5481 string = vim_strsave(string);
5482 else
5483 {
5484 /* next try expanding things like $VIM and ${HOME} */
5485 string = expand_env_save(name - 1);
5486 if (string != NULL && *string == '$')
5487 {
5488 vim_free(string);
5489 string = NULL;
5490 }
5491 }
5492 name[len] = cc;
5493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 rettv->v_type = VAR_STRING;
5495 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497
5498 return OK;
5499}
5500
5501/*
5502 * Array with names and number of arguments of all internal functions
5503 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5504 */
5505static struct fst
5506{
5507 char *f_name; /* function name */
5508 char f_min_argc; /* minimal number of arguments */
5509 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 void (*f_func) __ARGS((typeval *args, typeval *rvar));
5511 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512} functions[] =
5513{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005514 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {"append", 2, 2, f_append},
5516 {"argc", 0, 0, f_argc},
5517 {"argidx", 0, 0, f_argidx},
5518 {"argv", 1, 1, f_argv},
5519 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005520 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {"bufexists", 1, 1, f_bufexists},
5522 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5523 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5524 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5525 {"buflisted", 1, 1, f_buflisted},
5526 {"bufloaded", 1, 1, f_bufloaded},
5527 {"bufname", 1, 1, f_bufname},
5528 {"bufnr", 1, 1, f_bufnr},
5529 {"bufwinnr", 1, 1, f_bufwinnr},
5530 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005531 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005532 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {"char2nr", 1, 1, f_char2nr},
5534 {"cindent", 1, 1, f_cindent},
5535 {"col", 1, 1, f_col},
5536 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005538 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {"cscope_connection",0,3, f_cscope_connection},
5540 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {"delete", 1, 1, f_delete},
5543 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005544 {"diff_filler", 1, 1, f_diff_filler},
5545 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005546 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005548 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {"eventhandler", 0, 0, f_eventhandler},
5550 {"executable", 1, 1, f_executable},
5551 {"exists", 1, 1, f_exists},
5552 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005553 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5555 {"filereadable", 1, 1, f_filereadable},
5556 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005557 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005558 {"finddir", 1, 3, f_finddir},
5559 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {"fnamemodify", 2, 2, f_fnamemodify},
5561 {"foldclosed", 1, 1, f_foldclosed},
5562 {"foldclosedend", 1, 1, f_foldclosedend},
5563 {"foldlevel", 1, 1, f_foldlevel},
5564 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005565 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005568 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {"getbufvar", 2, 2, f_getbufvar},
5570 {"getchar", 0, 1, f_getchar},
5571 {"getcharmod", 0, 0, f_getcharmod},
5572 {"getcmdline", 0, 0, f_getcmdline},
5573 {"getcmdpos", 0, 0, f_getcmdpos},
5574 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005575 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005576 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {"getfsize", 1, 1, f_getfsize},
5578 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005579 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005580 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {"getreg", 0, 1, f_getreg},
5582 {"getregtype", 0, 1, f_getregtype},
5583 {"getwinposx", 0, 0, f_getwinposx},
5584 {"getwinposy", 0, 0, f_getwinposy},
5585 {"getwinvar", 2, 2, f_getwinvar},
5586 {"glob", 1, 1, f_glob},
5587 {"globpath", 2, 2, f_globpath},
5588 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005589 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {"hasmapto", 1, 2, f_hasmapto},
5591 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5592 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5593 {"histadd", 2, 2, f_histadd},
5594 {"histdel", 1, 2, f_histdel},
5595 {"histget", 1, 2, f_histget},
5596 {"histnr", 1, 1, f_histnr},
5597 {"hlID", 1, 1, f_hlID},
5598 {"hlexists", 1, 1, f_hlexists},
5599 {"hostname", 0, 0, f_hostname},
5600 {"iconv", 3, 3, f_iconv},
5601 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {"input", 1, 2, f_input},
5604 {"inputdialog", 1, 3, f_inputdialog},
5605 {"inputrestore", 0, 0, f_inputrestore},
5606 {"inputsave", 0, 0, f_inputsave},
5607 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {"libcall", 3, 3, f_libcall},
5616 {"libcallnr", 3, 3, f_libcallnr},
5617 {"line", 1, 1, f_line},
5618 {"line2byte", 1, 1, f_line2byte},
5619 {"lispindent", 1, 1, f_lispindent},
5620 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005621 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {"maparg", 1, 2, f_maparg},
5623 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005624 {"match", 2, 4, f_match},
5625 {"matchend", 2, 4, f_matchend},
5626 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005627 {"max", 1, 1, f_max},
5628 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {"mode", 0, 0, f_mode},
5630 {"nextnonblank", 1, 1, f_nextnonblank},
5631 {"nr2char", 1, 1, f_nr2char},
5632 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {"remote_expr", 2, 3, f_remote_expr},
5635 {"remote_foreground", 1, 1, f_remote_foreground},
5636 {"remote_peek", 1, 2, f_remote_peek},
5637 {"remote_read", 1, 1, f_remote_read},
5638 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005641 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005643 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {"search", 1, 2, f_search},
5645 {"searchpair", 3, 5, f_searchpair},
5646 {"server2client", 2, 2, f_server2client},
5647 {"serverlist", 0, 0, f_serverlist},
5648 {"setbufvar", 3, 3, f_setbufvar},
5649 {"setcmdpos", 1, 1, f_setcmdpos},
5650 {"setline", 2, 2, f_setline},
5651 {"setreg", 2, 3, f_setreg},
5652 {"setwinvar", 3, 3, f_setwinvar},
5653 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005654 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656#ifdef HAVE_STRFTIME
5657 {"strftime", 1, 2, f_strftime},
5658#endif
5659 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {"strlen", 1, 1, f_strlen},
5662 {"strpart", 2, 3, f_strpart},
5663 {"strridx", 2, 2, f_strridx},
5664 {"strtrans", 1, 1, f_strtrans},
5665 {"submatch", 1, 1, f_submatch},
5666 {"substitute", 4, 4, f_substitute},
5667 {"synID", 3, 3, f_synID},
5668 {"synIDattr", 2, 3, f_synIDattr},
5669 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005670 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {"tempname", 0, 0, f_tempname},
5672 {"tolower", 1, 1, f_tolower},
5673 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005674 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {"virtcol", 1, 1, f_virtcol},
5678 {"visualmode", 0, 1, f_visualmode},
5679 {"winbufnr", 1, 1, f_winbufnr},
5680 {"wincol", 0, 0, f_wincol},
5681 {"winheight", 1, 1, f_winheight},
5682 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005683 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {"winrestcmd", 0, 0, f_winrestcmd},
5685 {"winwidth", 1, 1, f_winwidth},
5686};
5687
5688#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5689
5690/*
5691 * Function given to ExpandGeneric() to obtain the list of internal
5692 * or user defined function names.
5693 */
5694 char_u *
5695get_function_name(xp, idx)
5696 expand_T *xp;
5697 int idx;
5698{
5699 static int intidx = -1;
5700 char_u *name;
5701
5702 if (idx == 0)
5703 intidx = -1;
5704 if (intidx < 0)
5705 {
5706 name = get_user_func_name(xp, idx);
5707 if (name != NULL)
5708 return name;
5709 }
5710 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5711 {
5712 STRCPY(IObuff, functions[intidx].f_name);
5713 STRCAT(IObuff, "(");
5714 if (functions[intidx].f_max_argc == 0)
5715 STRCAT(IObuff, ")");
5716 return IObuff;
5717 }
5718
5719 return NULL;
5720}
5721
5722/*
5723 * Function given to ExpandGeneric() to obtain the list of internal or
5724 * user defined variable or function names.
5725 */
5726/*ARGSUSED*/
5727 char_u *
5728get_expr_name(xp, idx)
5729 expand_T *xp;
5730 int idx;
5731{
5732 static int intidx = -1;
5733 char_u *name;
5734
5735 if (idx == 0)
5736 intidx = -1;
5737 if (intidx < 0)
5738 {
5739 name = get_function_name(xp, idx);
5740 if (name != NULL)
5741 return name;
5742 }
5743 return get_user_var_name(xp, ++intidx);
5744}
5745
5746#endif /* FEAT_CMDL_COMPL */
5747
5748/*
5749 * Find internal function in table above.
5750 * Return index, or -1 if not found
5751 */
5752 static int
5753find_internal_func(name)
5754 char_u *name; /* name of the function */
5755{
5756 int first = 0;
5757 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5758 int cmp;
5759 int x;
5760
5761 /*
5762 * Find the function name in the table. Binary search.
5763 */
5764 while (first <= last)
5765 {
5766 x = first + ((unsigned)(last - first) >> 1);
5767 cmp = STRCMP(name, functions[x].f_name);
5768 if (cmp < 0)
5769 last = x - 1;
5770 else if (cmp > 0)
5771 first = x + 1;
5772 else
5773 return x;
5774 }
5775 return -1;
5776}
5777
5778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5780 * name it contains, otherwise return "name".
5781 */
5782 static char_u *
5783deref_func_name(name, lenp)
5784 char_u *name;
5785 int *lenp;
5786{
5787 VAR v;
5788 int cc;
5789
5790 cc = name[*lenp];
5791 name[*lenp] = NUL;
5792 v = find_var(name, FALSE);
5793 name[*lenp] = cc;
5794 if (v != NULL && v->tv.v_type == VAR_FUNC)
5795 {
5796 if (v->tv.vval.v_string == NULL)
5797 {
5798 *lenp = 0;
5799 return (char_u *)""; /* just in case */
5800 }
5801 *lenp = STRLEN(v->tv.vval.v_string);
5802 return v->tv.vval.v_string;
5803 }
5804
5805 return name;
5806}
5807
5808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 * Allocate a variable for the result of a function.
5810 * Return OK or FAIL.
5811 */
5812 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005813get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5814 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 char_u *name; /* name of the function */
5816 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005817 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 char_u **arg; /* argument, pointing to the '(' */
5819 linenr_T firstline; /* first line of range */
5820 linenr_T lastline; /* last line of range */
5821 int *doesrange; /* return: function handled range */
5822 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005823 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 char_u *argp;
5826 int ret = OK;
5827#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 int argcount = 0; /* number of arguments found */
5830
5831 /*
5832 * Get the arguments.
5833 */
5834 argp = *arg;
5835 while (argcount < MAX_FUNC_ARGS)
5836 {
5837 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5838 if (*argp == ')' || *argp == ',' || *argp == NUL)
5839 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5841 {
5842 ret = FAIL;
5843 break;
5844 }
5845 ++argcount;
5846 if (*argp != ',')
5847 break;
5848 }
5849 if (*argp == ')')
5850 ++argp;
5851 else
5852 ret = FAIL;
5853
5854 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005855 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005856 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 else if (!aborting())
5858 EMSG2(_("E116: Invalid arguments for function %s"), name);
5859
5860 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005861 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 *arg = skipwhite(argp);
5864 return ret;
5865}
5866
5867
5868/*
5869 * Call a function with its resolved parameters
5870 * Return OK or FAIL.
5871 */
5872 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005873call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 char_u *name; /* name of the function */
5876 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005877 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 linenr_T firstline; /* first line of range */
5881 linenr_T lastline; /* last line of range */
5882 int *doesrange; /* return: function handled range */
5883 int evaluate;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005884 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#define ERROR_UNKNOWN 0
5888#define ERROR_TOOMANY 1
5889#define ERROR_TOOFEW 2
5890#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00005891#define ERROR_DICT 4
5892#define ERROR_NONE 5
5893#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 int error = ERROR_NONE;
5895 int i;
5896 int llen;
5897 ufunc_T *fp;
5898 int cc;
5899#define FLEN_FIXED 40
5900 char_u fname_buf[FLEN_FIXED + 1];
5901 char_u *fname;
5902
5903 /*
5904 * In a script change <SID>name() and s:name() to K_SNR 123_name().
5905 * Change <SNR>123_name() to K_SNR 123_name().
5906 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
5907 */
5908 cc = name[len];
5909 name[len] = NUL;
5910 llen = eval_fname_script(name);
5911 if (llen > 0)
5912 {
5913 fname_buf[0] = K_SPECIAL;
5914 fname_buf[1] = KS_EXTRA;
5915 fname_buf[2] = (int)KE_SNR;
5916 i = 3;
5917 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
5918 {
5919 if (current_SID <= 0)
5920 error = ERROR_SCRIPT;
5921 else
5922 {
5923 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
5924 i = (int)STRLEN(fname_buf);
5925 }
5926 }
5927 if (i + STRLEN(name + llen) < FLEN_FIXED)
5928 {
5929 STRCPY(fname_buf + i, name + llen);
5930 fname = fname_buf;
5931 }
5932 else
5933 {
5934 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
5935 if (fname == NULL)
5936 error = ERROR_OTHER;
5937 else
5938 {
5939 mch_memmove(fname, fname_buf, (size_t)i);
5940 STRCPY(fname + i, name + llen);
5941 }
5942 }
5943 }
5944 else
5945 fname = name;
5946
5947 *doesrange = FALSE;
5948
5949
5950 /* execute the function if no errors detected and executing */
5951 if (evaluate && error == ERROR_NONE)
5952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005953 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 error = ERROR_UNKNOWN;
5955
5956 if (!ASCII_ISLOWER(fname[0]))
5957 {
5958 /*
5959 * User defined function.
5960 */
5961 fp = find_func(fname);
5962#ifdef FEAT_AUTOCMD
5963 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
5964 fname, fname, TRUE, NULL)
5965#ifdef FEAT_EVAL
5966 && !aborting()
5967#endif
5968 )
5969 {
5970 /* executed an autocommand, search for function again */
5971 fp = find_func(fname);
5972 }
5973#endif
5974 if (fp != NULL)
5975 {
5976 if (fp->flags & FC_RANGE)
5977 *doesrange = TRUE;
5978 if (argcount < fp->args.ga_len)
5979 error = ERROR_TOOFEW;
5980 else if (!fp->varargs && argcount > fp->args.ga_len)
5981 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005982 else if ((fp->flags & FC_DICT) && selfdict == NULL)
5983 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 else
5985 {
5986 /*
5987 * Call the user function.
5988 * Save and restore search patterns, script variables and
5989 * redo buffer.
5990 */
5991 save_search_patterns();
5992 saveRedobuff();
5993 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005994 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 firstline, lastline,
5996 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005997 if (--fp->calls <= 0 && isdigit(*fp->name))
5998 /* Function was unreferenced while being used, free it
5999 * now. */
6000 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 restoreRedobuff();
6002 restore_search_patterns();
6003 error = ERROR_NONE;
6004 }
6005 }
6006 }
6007 else
6008 {
6009 /*
6010 * Find the function name in the table, call its implementation.
6011 */
6012 i = find_internal_func(fname);
6013 if (i >= 0)
6014 {
6015 if (argcount < functions[i].f_min_argc)
6016 error = ERROR_TOOFEW;
6017 else if (argcount > functions[i].f_max_argc)
6018 error = ERROR_TOOMANY;
6019 else
6020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006022 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 error = ERROR_NONE;
6024 }
6025 }
6026 }
6027 /*
6028 * The function call (or "FuncUndefined" autocommand sequence) might
6029 * have been aborted by an error, an interrupt, or an explicitly thrown
6030 * exception that has not been caught so far. This situation can be
6031 * tested for by calling aborting(). For an error in an internal
6032 * function or for the "E132" error in call_user_func(), however, the
6033 * throw point at which the "force_abort" flag (temporarily reset by
6034 * emsg()) is normally updated has not been reached yet. We need to
6035 * update that flag first to make aborting() reliable.
6036 */
6037 update_force_abort();
6038 }
6039 if (error == ERROR_NONE)
6040 ret = OK;
6041
6042 /*
6043 * Report an error unless the argument evaluation or function call has been
6044 * cancelled due to an aborting error, an interrupt, or an exception.
6045 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006046 if (!aborting())
6047 {
6048 switch (error)
6049 {
6050 case ERROR_UNKNOWN:
6051 EMSG2(_("E117: Unknown function: %s"), name);
6052 break;
6053 case ERROR_TOOMANY:
6054 EMSG2(_(e_toomanyarg), name);
6055 break;
6056 case ERROR_TOOFEW:
6057 EMSG2(_("E119: Not enough arguments for function: %s"),
6058 name);
6059 break;
6060 case ERROR_SCRIPT:
6061 EMSG2(_("E120: Using <SID> not in a script context: %s"),
6062 name);
6063 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006064 case ERROR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00006066 name);
6067 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068 }
6069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 name[len] = cc;
6072 if (fname != name && fname != fname_buf)
6073 vim_free(fname);
6074
6075 return ret;
6076}
6077
6078/*********************************************
6079 * Implementation of the built-in functions
6080 */
6081
6082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006083 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 */
6085 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006086f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006088 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006092 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 l = argvars[0].vval.v_list;
6096 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006097 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 }
6099 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006100 EMSG(_(e_listreq));
6101}
6102
6103/*
6104 * "append(lnum, string/list)" function
6105 */
6106 static void
6107f_append(argvars, rettv)
6108 typeval *argvars;
6109 typeval *rettv;
6110{
6111 long lnum;
6112 listvar *l = NULL;
6113 listitem *li = NULL;
6114 typeval *tv;
6115 long added = 0;
6116
6117 rettv->vval.v_number = 1; /* Default: Failed */
6118 lnum = get_tv_lnum(argvars);
6119 if (lnum >= 0
6120 && lnum <= curbuf->b_ml.ml_line_count
6121 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006123 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006125 l = argvars[1].vval.v_list;
6126 if (l == NULL)
6127 return;
6128 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006130 for (;;)
6131 {
6132 if (l == NULL)
6133 tv = &argvars[1]; /* append a string */
6134 else if (li == NULL)
6135 break; /* end of list */
6136 else
6137 tv = &li->li_tv; /* append item from list */
6138 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6139 ++added;
6140 if (l == NULL)
6141 break;
6142 li = li->li_next;
6143 }
6144
6145 appended_lines_mark(lnum, added);
6146 if (curwin->w_cursor.lnum > lnum)
6147 curwin->w_cursor.lnum += added;
6148 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
6150}
6151
6152/*
6153 * "argc()" function
6154 */
6155/* ARGSUSED */
6156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
6163
6164/*
6165 * "argidx()" function
6166 */
6167/* ARGSUSED */
6168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006169f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006171 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006173 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174}
6175
6176/*
6177 * "argv(nr)" function
6178 */
6179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006180f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 int idx;
6185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006186 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006188 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006190 rettv->vval.v_string = NULL;
6191 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192}
6193
6194/*
6195 * "browse(save, title, initdir, default)" function
6196 */
6197/* ARGSUSED */
6198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006199f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006201 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203#ifdef FEAT_BROWSE
6204 int save;
6205 char_u *title;
6206 char_u *initdir;
6207 char_u *defname;
6208 char_u buf[NUMBUFLEN];
6209 char_u buf2[NUMBUFLEN];
6210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006211 save = get_tv_number(&argvars[0]);
6212 title = get_tv_string(&argvars[1]);
6213 initdir = get_tv_string_buf(&argvars[2], buf);
6214 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006216 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006217 do_browse(save ? BROWSE_SAVE : 0,
6218 title, defname, NULL, initdir, NULL, curbuf);
6219#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006220 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006222 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006223}
6224
6225/*
6226 * "browsedir(title, initdir)" function
6227 */
6228/* ARGSUSED */
6229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006230f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006232 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006233{
6234#ifdef FEAT_BROWSE
6235 char_u *title;
6236 char_u *initdir;
6237 char_u buf[NUMBUFLEN];
6238
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006239 title = get_tv_string(&argvars[0]);
6240 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006241
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006242 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006243 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006245 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006247 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248}
6249
Bram Moolenaar0d660222005-01-07 21:51:51 +00006250static buf_T *find_buffer __ARGS((typeval *avar));
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252/*
6253 * Find a buffer by number or exact name.
6254 */
6255 static buf_T *
6256find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 if (avar->v_type == VAR_NUMBER)
6262 buf = buflist_findnr((int)avar->vval.v_number);
6263 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006265 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006266 if (buf == NULL)
6267 {
6268 /* No full path name match, try a match with a URL or a "nofile"
6269 * buffer, these don't use the full path. */
6270 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6271 if (buf->b_fname != NULL
6272 && (path_with_url(buf->b_fname)
6273#ifdef FEAT_QUICKFIX
6274 || bt_nofile(buf)
6275#endif
6276 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006278 break;
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281 return buf;
6282}
6283
6284/*
6285 * "bufexists(expr)" function
6286 */
6287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006288f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006290 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006292 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293}
6294
6295/*
6296 * "buflisted(expr)" function
6297 */
6298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006299f_buflisted(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{
6303 buf_T *buf;
6304
6305 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006306 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307}
6308
6309/*
6310 * "bufloaded(expr)" function
6311 */
6312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006313f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006315 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 buf_T *buf;
6318
6319 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321}
6322
Bram Moolenaar0d660222005-01-07 21:51:51 +00006323static buf_T *get_buf_tv __ARGS((typeval *tv));
6324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325/*
6326 * Get buffer by number or pattern.
6327 */
6328 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006329get_buf_tv(tv)
6330 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006332 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 int save_magic;
6334 char_u *save_cpo;
6335 buf_T *buf;
6336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006337 if (tv->v_type == VAR_NUMBER)
6338 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (name == NULL || *name == NUL)
6340 return curbuf;
6341 if (name[0] == '$' && name[1] == NUL)
6342 return lastbuf;
6343
6344 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6345 save_magic = p_magic;
6346 p_magic = TRUE;
6347 save_cpo = p_cpo;
6348 p_cpo = (char_u *)"";
6349
6350 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6351 TRUE, FALSE));
6352
6353 p_magic = save_magic;
6354 p_cpo = save_cpo;
6355
6356 /* If not found, try expanding the name, like done for bufexists(). */
6357 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006358 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 return buf;
6361}
6362
6363/*
6364 * "bufname(expr)" function
6365 */
6366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006367f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006369 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 buf_T *buf;
6372
6373 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374 buf = get_buf_tv(&argvars[0]);
6375 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006377 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006379 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 --emsg_off;
6381}
6382
6383/*
6384 * "bufnr(expr)" function
6385 */
6386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006387f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006389 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 buf_T *buf;
6392
6393 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006394 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006396 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 --emsg_off;
6400}
6401
6402/*
6403 * "bufwinnr(nr)" function
6404 */
6405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006406f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410#ifdef FEAT_WINDOWS
6411 win_T *wp;
6412 int winnr = 0;
6413#endif
6414 buf_T *buf;
6415
6416 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006417 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#ifdef FEAT_WINDOWS
6419 for (wp = firstwin; wp; wp = wp->w_next)
6420 {
6421 ++winnr;
6422 if (wp->w_buffer == buf)
6423 break;
6424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428#endif
6429 --emsg_off;
6430}
6431
6432/*
6433 * "byte2line(byte)" function
6434 */
6435/*ARGSUSED*/
6436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006437f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006439 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443#else
6444 long boff = 0;
6445
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006446 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006448 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006450 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 (linenr_T)0, &boff);
6452#endif
6453}
6454
6455/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006456 * "byteidx()" function
6457 */
6458/*ARGSUSED*/
6459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006463{
6464#ifdef FEAT_MBYTE
6465 char_u *t;
6466#endif
6467 char_u *str;
6468 long idx;
6469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006470 str = get_tv_string(&argvars[0]);
6471 idx = get_tv_number(&argvars[1]);
6472 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006473 if (idx < 0)
6474 return;
6475
6476#ifdef FEAT_MBYTE
6477 t = str;
6478 for ( ; idx > 0; idx--)
6479 {
6480 if (*t == NUL) /* EOL reached */
6481 return;
6482 t += mb_ptr2len_check(t);
6483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006484 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006485#else
6486 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006488#endif
6489}
6490
6491/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492 * "call(func, arglist)" function
6493 */
6494 static void
6495f_call(argvars, rettv)
6496 typeval *argvars;
6497 typeval *rettv;
6498{
6499 char_u *func;
6500 typeval argv[MAX_FUNC_ARGS];
6501 int argc = 0;
6502 listitem *item;
6503 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504 dictvar *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505
6506 rettv->vval.v_number = 0;
6507 if (argvars[1].v_type != VAR_LIST)
6508 {
6509 EMSG(_(e_listreq));
6510 return;
6511 }
6512 if (argvars[1].vval.v_list == NULL)
6513 return;
6514
6515 if (argvars[0].v_type == VAR_FUNC)
6516 func = argvars[0].vval.v_string;
6517 else
6518 func = get_tv_string(&argvars[0]);
6519
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 if (argvars[2].v_type != VAR_UNKNOWN)
6521 {
6522 if (argvars[2].v_type != VAR_DICT)
6523 {
6524 EMSG(_(e_dictreq));
6525 return;
6526 }
6527 selfdict = argvars[2].vval.v_dict;
6528 }
6529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6531 item = item->li_next)
6532 {
6533 if (argc == MAX_FUNC_ARGS)
6534 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006535 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 break;
6537 }
6538 /* Make a copy of each argument (is this really needed?) */
6539 copy_tv(&item->li_tv, &argv[argc++]);
6540 }
6541
6542 if (item == NULL)
6543 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6545 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546
6547 /* Free the arguments. */
6548 while (argc > 0)
6549 clear_tv(&argv[--argc]);
6550}
6551
6552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 * "char2nr(string)" function
6554 */
6555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006556f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006558 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560#ifdef FEAT_MBYTE
6561 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562 rettv->vval.v_number =
6563 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 else
6565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006566 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567}
6568
6569/*
6570 * "cindent(lnum)" function
6571 */
6572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006573f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577#ifdef FEAT_CINDENT
6578 pos_T pos;
6579 linenr_T lnum;
6580
6581 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006582 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6584 {
6585 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 curwin->w_cursor = pos;
6588 }
6589 else
6590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592}
6593
6594/*
6595 * "col(string)" function
6596 */
6597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006598f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 colnr_T col = 0;
6603 pos_T *fp;
6604
6605 fp = var2fpos(&argvars[0], FALSE);
6606 if (fp != NULL)
6607 {
6608 if (fp->col == MAXCOL)
6609 {
6610 /* '> can be MAXCOL, get the length of the line then */
6611 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6612 col = STRLEN(ml_get(fp->lnum)) + 1;
6613 else
6614 col = MAXCOL;
6615 }
6616 else
6617 {
6618 col = fp->col + 1;
6619#ifdef FEAT_VIRTUALEDIT
6620 /* col(".") when the cursor is on the NUL at the end of the line
6621 * because of "coladd" can be seen as an extra column. */
6622 if (virtual_active() && fp == &curwin->w_cursor)
6623 {
6624 char_u *p = ml_get_cursor();
6625
6626 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6627 curwin->w_virtcol - curwin->w_cursor.coladd))
6628 {
6629# ifdef FEAT_MBYTE
6630 int l;
6631
6632 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6633 col += l;
6634# else
6635 if (*p != NUL && p[1] == NUL)
6636 ++col;
6637# endif
6638 }
6639 }
6640#endif
6641 }
6642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006643 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
6646/*
6647 * "confirm(message, buttons[, default [, type]])" function
6648 */
6649/*ARGSUSED*/
6650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006651f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006653 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6656 char_u *message;
6657 char_u *buttons = NULL;
6658 char_u buf[NUMBUFLEN];
6659 char_u buf2[NUMBUFLEN];
6660 int def = 1;
6661 int type = VIM_GENERIC;
6662 int c;
6663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006664 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006667 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006670 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
6673 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 switch (TOUPPER_ASC(c))
6676 {
6677 case 'E': type = VIM_ERROR; break;
6678 case 'Q': type = VIM_QUESTION; break;
6679 case 'I': type = VIM_INFO; break;
6680 case 'W': type = VIM_WARNING; break;
6681 case 'G': type = VIM_GENERIC; break;
6682 }
6683 }
6684 }
6685 }
6686
6687 if (buttons == NULL || *buttons == NUL)
6688 buttons = (char_u *)_("&Ok");
6689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006690 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 def, NULL);
6692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006693 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694#endif
6695}
6696
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697/*
6698 * "copy()" function
6699 */
6700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006701f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006705 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006709 * "count()" function
6710 */
6711 static void
6712f_count(argvars, rettv)
6713 typeval *argvars;
6714 typeval *rettv;
6715{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006716 long n = 0;
6717 int ic = FALSE;
6718
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006720 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721 listitem *li;
6722 listvar *l;
6723 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 if ((l = argvars[0].vval.v_list) != NULL)
6726 {
6727 li = l->lv_first;
6728 if (argvars[2].v_type != VAR_UNKNOWN)
6729 {
6730 ic = get_tv_number(&argvars[2]);
6731 if (argvars[3].v_type != VAR_UNKNOWN)
6732 {
6733 idx = get_tv_number(&argvars[3]);
6734 li = list_find(l, idx);
6735 if (li == NULL)
6736 EMSGN(_(e_listidx), idx);
6737 }
6738 }
6739
6740 for ( ; li != NULL; li = li->li_next)
6741 if (tv_equal(&li->li_tv, &argvars[1], ic))
6742 ++n;
6743 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006744 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 else if (argvars[0].v_type == VAR_DICT)
6746 {
6747 if (argvars[0].vval.v_dict != NULL)
6748 {
6749 dictitem *di;
6750
6751 di = argvars[0].vval.v_dict->dv_first;
6752 if (argvars[2].v_type != VAR_UNKNOWN)
6753 {
6754 ic = get_tv_number(&argvars[2]);
6755 if (argvars[3].v_type != VAR_UNKNOWN)
6756 EMSG(_(e_invarg));
6757 }
6758
6759 for ( ; di != NULL; di = di->di_next)
6760 if (tv_equal(&di->di_tv, &argvars[1], ic))
6761 ++n;
6762 }
6763 }
6764 else
6765 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006766 rettv->vval.v_number = n;
6767}
6768
6769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6771 *
6772 * Checks the existence of a cscope connection.
6773 */
6774/*ARGSUSED*/
6775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006776f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006778 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780#ifdef FEAT_CSCOPE
6781 int num = 0;
6782 char_u *dbpath = NULL;
6783 char_u *prepend = NULL;
6784 char_u buf[NUMBUFLEN];
6785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006786 if (argvars[0].v_type != VAR_UNKNOWN
6787 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006789 num = (int)get_tv_number(&argvars[0]);
6790 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006792 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006797 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798#endif
6799}
6800
6801/*
6802 * "cursor(lnum, col)" function
6803 *
6804 * Moves the cursor to the specified line and column
6805 */
6806/*ARGSUSED*/
6807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006808f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006809 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 long line, col;
6813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (line > 0)
6816 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (col > 0)
6819 curwin->w_cursor.col = col - 1;
6820#ifdef FEAT_VIRTUALEDIT
6821 curwin->w_cursor.coladd = 0;
6822#endif
6823
6824 /* Make sure the cursor is in a valid position. */
6825 check_cursor();
6826#ifdef FEAT_MBYTE
6827 /* Correct cursor for multi-byte character. */
6828 if (has_mbyte)
6829 mb_adjust_cursor();
6830#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006831
6832 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833}
6834
6835/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 */
6838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006839f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844}
6845
6846/*
6847 * "delete()" function
6848 */
6849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006850f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006855 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858}
6859
6860/*
6861 * "did_filetype()" function
6862 */
6863/*ARGSUSED*/
6864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006865f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006870 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006872 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873#endif
6874}
6875
6876/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00006877 * "diff_filler()" function
6878 */
6879/*ARGSUSED*/
6880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006884{
6885#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00006887#endif
6888}
6889
6890/*
6891 * "diff_hlID()" function
6892 */
6893/*ARGSUSED*/
6894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006897 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006898{
6899#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00006901 static linenr_T prev_lnum = 0;
6902 static int changedtick = 0;
6903 static int fnum = 0;
6904 static int change_start = 0;
6905 static int change_end = 0;
6906 static enum hlf_value hlID = 0;
6907 int filler_lines;
6908 int col;
6909
6910 if (lnum != prev_lnum
6911 || changedtick != curbuf->b_changedtick
6912 || fnum != curbuf->b_fnum)
6913 {
6914 /* New line, buffer, change: need to get the values. */
6915 filler_lines = diff_check(curwin, lnum);
6916 if (filler_lines < 0)
6917 {
6918 if (filler_lines == -1)
6919 {
6920 change_start = MAXCOL;
6921 change_end = -1;
6922 if (diff_find_change(curwin, lnum, &change_start, &change_end))
6923 hlID = HLF_ADD; /* added line */
6924 else
6925 hlID = HLF_CHD; /* changed line */
6926 }
6927 else
6928 hlID = HLF_ADD; /* added line */
6929 }
6930 else
6931 hlID = (enum hlf_value)0;
6932 prev_lnum = lnum;
6933 changedtick = curbuf->b_changedtick;
6934 fnum = curbuf->b_fnum;
6935 }
6936
6937 if (hlID == HLF_CHD || hlID == HLF_TXD)
6938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006939 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006940 if (col >= change_start && col <= change_end)
6941 hlID = HLF_TXD; /* changed text */
6942 else
6943 hlID = HLF_CHD; /* changed line */
6944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006945 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006946#endif
6947}
6948
6949/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006950 * "empty({expr})" function
6951 */
6952 static void
6953f_empty(argvars, rettv)
6954 typeval *argvars;
6955 typeval *rettv;
6956{
6957 int n;
6958
6959 switch (argvars[0].v_type)
6960 {
6961 case VAR_STRING:
6962 case VAR_FUNC:
6963 n = argvars[0].vval.v_string == NULL
6964 || *argvars[0].vval.v_string == NUL;
6965 break;
6966 case VAR_NUMBER:
6967 n = argvars[0].vval.v_number == 0;
6968 break;
6969 case VAR_LIST:
6970 n = argvars[0].vval.v_list == NULL
6971 || argvars[0].vval.v_list->lv_first == NULL;
6972 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 case VAR_DICT:
6974 n = argvars[0].vval.v_dict == NULL
6975 || argvars[0].vval.v_dict->dv_first == NULL;
6976 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006977 default:
6978 EMSG2(_(e_intern2), "f_empty()");
6979 n = 0;
6980 }
6981
6982 rettv->vval.v_number = n;
6983}
6984
6985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 * "escape({string}, {chars})" function
6987 */
6988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006990 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006991 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 char_u buf[NUMBUFLEN];
6994
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006995 rettv->vval.v_string =
6996 vim_strsave_escaped(get_tv_string(&argvars[0]),
6997 get_tv_string_buf(&argvars[1], buf));
6998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999}
7000
7001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002 * "eval()" function
7003 */
7004/*ARGSUSED*/
7005 static void
7006f_eval(argvars, rettv)
7007 typeval *argvars;
7008 typeval *rettv;
7009{
7010 char_u *s;
7011
7012 s = get_tv_string(&argvars[0]);
7013 s = skipwhite(s);
7014
7015 if (eval1(&s, rettv, TRUE) == FAIL)
7016 rettv->vval.v_number = 0;
7017 else if (*s != NUL)
7018 EMSG(_(e_trailing));
7019}
7020
7021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 * "eventhandler()" function
7023 */
7024/*ARGSUSED*/
7025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007026f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007027 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007028 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031}
7032
7033/*
7034 * "executable()" function
7035 */
7036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007037f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007038 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007039 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007041 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044/*
7045 * "exists()" function
7046 */
7047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007048f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007049 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007050 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 char_u *p;
7053 char_u *name;
7054 int n = FALSE;
7055 int len = 0;
7056
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007057 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 if (*p == '$') /* environment variable */
7059 {
7060 /* first try "normal" environment variables (fast) */
7061 if (mch_getenv(p + 1) != NULL)
7062 n = TRUE;
7063 else
7064 {
7065 /* try expanding things like $VIM and ${HOME} */
7066 p = expand_env_save(p);
7067 if (p != NULL && *p != '$')
7068 n = TRUE;
7069 vim_free(p);
7070 }
7071 }
7072 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else if (*p == '*') /* internal or user defined function */
7075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007076 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
7078 else if (*p == ':')
7079 {
7080 n = cmd_exists(p + 1);
7081 }
7082 else if (*p == '#')
7083 {
7084#ifdef FEAT_AUTOCMD
7085 name = p + 1;
7086 p = vim_strchr(name, '#');
7087 if (p != NULL)
7088 n = au_exists(name, p, p + 1);
7089 else
7090 n = au_exists(name, name + STRLEN(name), NULL);
7091#endif
7092 }
7093 else /* internal variable */
7094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 char_u *expr_start;
7096 char_u *expr_end;
7097 char_u *temp_string = NULL;
7098 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 name = p;
7100
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007102 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 if (expr_start != NULL)
7104 {
7105 temp_string = make_expanded_name(name, expr_start, expr_end, s);
7106 if (temp_string != NULL)
7107 {
7108 len = STRLEN(temp_string);
7109 name = temp_string;
7110 }
7111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 if (len == 0)
7113 len = get_id_len(&p);
7114 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007115 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007120 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121}
7122
7123/*
7124 * "expand()" function
7125 */
7126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007127f_expand(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 *s;
7132 int len;
7133 char_u *errormsg;
7134 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7135 expand_T xpc;
7136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007137 rettv->v_type = VAR_STRING;
7138 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (*s == '%' || *s == '#' || *s == '<')
7140 {
7141 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007142 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 --emsg_off;
7144 }
7145 else
7146 {
7147 /* When the optional second argument is non-zero, don't remove matches
7148 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 flags |= WILD_KEEP_ALL;
7151 ExpandInit(&xpc);
7152 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 ExpandCleanup(&xpc);
7155 }
7156}
7157
7158/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007159 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007161 */
7162 static void
7163f_extend(argvars, rettv)
7164 typeval *argvars;
7165 typeval *rettv;
7166{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007167 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007168 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007169 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 listvar *l1, *l2;
7171 listitem *item;
7172 long before;
7173 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007174
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 l1 = argvars[0].vval.v_list;
7176 l2 = argvars[1].vval.v_list;
7177 if (l1 != NULL && l2 != NULL)
7178 {
7179 if (argvars[2].v_type != VAR_UNKNOWN)
7180 {
7181 n = before = get_tv_number(&argvars[2]);
7182 item = list_find_ext(l1, &n);
7183 if (n != 0)
7184 {
7185 EMSGN(_(e_listidx), before);
7186 return;
7187 }
7188 }
7189 else
7190 item = NULL;
7191 list_extend(l1, l2, item);
7192
7193 ++l1->lv_refcount;
7194 copy_tv(&argvars[0], rettv);
7195 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007196 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7198 {
7199 dictvar *d1, *d2;
7200 dictitem *d1i, *d2i;
7201 char_u *action;
7202 int i;
7203
7204 d1 = argvars[0].vval.v_dict;
7205 d2 = argvars[1].vval.v_dict;
7206 if (d1 != NULL && d2 != NULL)
7207 {
7208 /* Check the third argument. */
7209 if (argvars[2].v_type != VAR_UNKNOWN)
7210 {
7211 static char *(av[]) = {"keep", "force", "error"};
7212
7213 action = get_tv_string(&argvars[2]);
7214 for (i = 0; i < 3; ++i)
7215 if (STRCMP(action, av[i]) == 0)
7216 break;
7217 if (i == 3)
7218 {
7219 EMSGN(_(e_invarg2), action);
7220 return;
7221 }
7222 }
7223 else
7224 action = (char_u *)"force";
7225
7226 /* Go over all entries in the second dict and add them to the
7227 * first dict. */
7228 for (d2i = d2->dv_first; d2i != NULL; d2i = d2i->di_next)
7229 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007230 d1i = dict_find(d1, d2i->di_key, -1, NULL);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231 if (d1i == NULL)
7232 {
7233 d1i = dictitem_copy(d2i);
7234 if (d1i != NULL)
7235 dict_add(d1, d1i);
7236 }
7237 else if (*action == 'e')
7238 {
7239 EMSG2(_("Key already exists: %s"), d2i->di_key);
7240 break;
7241 }
7242 else if (*action == 'f')
7243 {
7244 clear_tv(&d1i->di_tv);
7245 copy_tv(&d2i->di_tv, &d1i->di_tv);
7246 }
7247 }
7248
7249 ++d1->dv_refcount;
7250 copy_tv(&argvars[0], rettv);
7251 }
7252 }
7253 else
7254 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007255}
7256
7257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 * "filereadable()" function
7259 */
7260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007261f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007262 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007263 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 FILE *fd;
7266 char_u *p;
7267 int n;
7268
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7271 {
7272 n = TRUE;
7273 fclose(fd);
7274 }
7275 else
7276 n = FALSE;
7277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279}
7280
7281/*
7282 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7283 * rights to write into.
7284 */
7285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007286f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007287 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007288 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 char_u *p;
7291 int retval = 0;
7292#if defined(UNIX) || defined(VMS)
7293 int perm = 0;
7294#endif
7295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007296 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297#if defined(UNIX) || defined(VMS)
7298 perm = mch_getperm(p);
7299#endif
7300#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7301 if (
7302# ifdef WIN3264
7303 mch_writable(p) &&
7304# else
7305# if defined(UNIX) || defined(VMS)
7306 (perm & 0222) &&
7307# endif
7308# endif
7309 mch_access((char *)p, W_OK) == 0
7310 )
7311#endif
7312 {
7313 ++retval;
7314 if (mch_isdir(p))
7315 ++retval;
7316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007317 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
Bram Moolenaar0d660222005-01-07 21:51:51 +00007320static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007321
7322 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007323findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007324 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007325 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007326 int dir;
7327{
7328#ifdef FEAT_SEARCHPATH
7329 char_u *fname;
7330 char_u *fresult = NULL;
7331 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7332 char_u *p;
7333 char_u pathbuf[NUMBUFLEN];
7334 int count = 1;
7335 int first = TRUE;
7336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007337 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007338
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007339 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007341 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007342 if (*p != NUL)
7343 path = p;
7344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007347 }
7348
7349 do
7350 {
7351 vim_free(fresult);
7352 fresult = find_file_in_path_option(first ? fname : NULL,
7353 first ? (int)STRLEN(fname) : 0,
7354 0, first, path, dir, NULL);
7355 first = FALSE;
7356 } while (--count > 0 && fresult != NULL);
7357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007359#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007360 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007362 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007363}
7364
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007365static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366static int filter_map_one __ARGS((typeval *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007367
7368/*
7369 * Implementation of map() and filter().
7370 */
7371 static void
7372filter_map(argvars, rettv, map)
7373 typeval *argvars;
7374 typeval *rettv;
7375 int map;
7376{
7377 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 char_u *expr;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007379 listitem *li, *nli;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 listvar *l = NULL;
7381 dictitem *di, **pdi;
7382 dictvar *d = NULL;
7383 typeval save_val;
7384 typeval save_key;
7385 int rem;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007386
7387 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007389 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 if ((l = argvars[0].vval.v_list) == NULL)
7391 return;
7392 }
7393 else if (argvars[0].v_type == VAR_DICT)
7394 {
7395 if ((d = argvars[0].vval.v_dict) == NULL)
7396 return;
7397 }
7398 else
7399 {
7400 EMSG2(_(e_listdictarg), map ? "map()" : "filter()");
7401 return;
7402 }
7403
7404 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
7405 save_val = vimvars[VV_VAL].tv;
7406
7407 if (argvars[0].v_type == VAR_DICT)
7408 {
7409 save_key = vimvars[VV_KEY].tv;
7410 vimvars[VV_KEY].tv.v_type = VAR_STRING;
7411 pdi = &d->dv_first;
7412 for (di = d->dv_first; di != NULL; di = *pdi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007413 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 vimvars[VV_KEY].tv.vval.v_string = vim_strsave(di->di_key);
7415 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007416 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 if (!map && rem)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007418 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 *pdi = di->di_next;
7420 dictitem_free(di);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007421 }
7422 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 pdi = &di->di_next;
7424 clear_tv(&vimvars[VV_KEY].tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007425 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 clear_tv(&vimvars[VV_KEY].tv);
7427 vimvars[VV_KEY].tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 else
7430 {
7431 for (li = l->lv_first; li != NULL; li = nli)
7432 {
7433 nli = li->li_next;
7434 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7435 break;
7436 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007437 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007438 }
7439 }
7440
7441 clear_tv(&vimvars[VV_VAL].tv);
7442 vimvars[VV_VAL].tv = save_val;
7443
7444 copy_tv(&argvars[0], rettv);
7445}
7446
7447 static int
7448filter_map_one(tv, expr, map, remp)
7449 typeval *tv;
7450 char_u *expr;
7451 int map;
7452 int *remp;
7453{
7454 typeval rettv;
7455 char_u *s;
7456
7457 copy_tv(tv, &vimvars[VV_VAL].tv);
7458 s = expr;
7459 if (eval1(&s, &rettv, TRUE) == FAIL)
7460 return FAIL;
7461 if (*s != NUL) /* check for trailing chars after expr */
7462 {
7463 EMSG2(_(e_invexpr2), s);
7464 return FAIL;
7465 }
7466 if (map)
7467 {
7468 /* map(): replace the list item value */
7469 clear_tv(tv);
7470 *tv = rettv;
7471 }
7472 else
7473 {
7474 /* filter(): when expr is zero remove the item */
7475 *remp = (get_tv_number(&rettv) == 0);
7476 clear_tv(&rettv);
7477 }
7478 clear_tv(&vimvars[VV_VAL].tv);
7479 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007480}
7481
7482/*
7483 * "filter()" function
7484 */
7485 static void
7486f_filter(argvars, rettv)
7487 typeval *argvars;
7488 typeval *rettv;
7489{
7490 filter_map(argvars, rettv, FALSE);
7491}
7492
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007494 * "finddir({fname}[, {path}[, {count}]])" function
7495 */
7496 static void
7497f_finddir(argvars, rettv)
7498 typeval *argvars;
7499 typeval *rettv;
7500{
7501 findfilendir(argvars, rettv, TRUE);
7502}
7503
7504/*
7505 * "findfile({fname}[, {path}[, {count}]])" function
7506 */
7507 static void
7508f_findfile(argvars, rettv)
7509 typeval *argvars;
7510 typeval *rettv;
7511{
7512 findfilendir(argvars, rettv, FALSE);
7513}
7514
7515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 * "fnamemodify({fname}, {mods})" function
7517 */
7518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007520 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007521 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 char_u *fname;
7524 char_u *mods;
7525 int usedlen = 0;
7526 int len;
7527 char_u *fbuf = NULL;
7528 char_u buf[NUMBUFLEN];
7529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007530 fname = get_tv_string(&argvars[0]);
7531 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 len = (int)STRLEN(fname);
7533
7534 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 vim_free(fbuf);
7542}
7543
Bram Moolenaar0d660222005-01-07 21:51:51 +00007544static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
7546/*
7547 * "foldclosed()" function
7548 */
7549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007551 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 int end;
7554{
7555#ifdef FEAT_FOLDING
7556 linenr_T lnum;
7557 linenr_T first, last;
7558
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007559 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7561 {
7562 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7563 {
7564 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 return;
7569 }
7570 }
7571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573}
7574
7575/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 * "foldclosed()" function
7577 */
7578 static void
7579f_foldclosed(argvars, rettv)
7580 typeval *argvars;
7581 typeval *rettv;
7582{
7583 foldclosed_both(argvars, rettv, FALSE);
7584}
7585
7586/*
7587 * "foldclosedend()" function
7588 */
7589 static void
7590f_foldclosedend(argvars, rettv)
7591 typeval *argvars;
7592 typeval *rettv;
7593{
7594 foldclosed_both(argvars, rettv, TRUE);
7595}
7596
7597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 * "foldlevel()" function
7599 */
7600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605#ifdef FEAT_FOLDING
7606 linenr_T lnum;
7607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 else
7612#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614}
7615
7616/*
7617 * "foldtext()" function
7618 */
7619/*ARGSUSED*/
7620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
7625#ifdef FEAT_FOLDING
7626 linenr_T lnum;
7627 char_u *s;
7628 char_u *r;
7629 int len;
7630 char *txt;
7631#endif
7632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007633 rettv->v_type = VAR_STRING;
7634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7637 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7638 <= curbuf->b_ml.ml_line_count
7639 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {
7641 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7643 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
7645 if (!linewhite(lnum))
7646 break;
7647 ++lnum;
7648 }
7649
7650 /* Find interesting text in this line. */
7651 s = skipwhite(ml_get(lnum));
7652 /* skip C comment-start */
7653 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007656 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007658 {
7659 s = skipwhite(ml_get(lnum + 1));
7660 if (*s == '*')
7661 s = skipwhite(s + 1);
7662 }
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 txt = _("+-%s%3ld lines: ");
7665 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 + 20 /* for %3ld */
7668 + STRLEN(s))); /* concatenated */
7669 if (r != NULL)
7670 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7672 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7673 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 len = (int)STRLEN(r);
7675 STRCAT(r, s);
7676 /* remove 'foldmarker' and 'commentstring' */
7677 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 }
7681#endif
7682}
7683
7684/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007685 * "foldtextresult(lnum)" function
7686 */
7687/*ARGSUSED*/
7688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007692{
7693#ifdef FEAT_FOLDING
7694 linenr_T lnum;
7695 char_u *text;
7696 char_u buf[51];
7697 foldinfo_T foldinfo;
7698 int fold_count;
7699#endif
7700
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701 rettv->v_type = VAR_STRING;
7702 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007703#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007704 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007705 fold_count = foldedCount(curwin, lnum, &foldinfo);
7706 if (fold_count > 0)
7707 {
7708 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7709 &foldinfo, buf);
7710 if (text == buf)
7711 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007713 }
7714#endif
7715}
7716
7717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 * "foreground()" function
7719 */
7720/*ARGSUSED*/
7721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727#ifdef FEAT_GUI
7728 if (gui.in_use)
7729 gui_mch_set_foreground();
7730#else
7731# ifdef WIN32
7732 win32_set_foreground();
7733# endif
7734#endif
7735}
7736
7737/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007738 * "function()" function
7739 */
7740/*ARGSUSED*/
7741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007743 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745{
7746 char_u *s;
7747
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007748 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007749 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007750 EMSG2(_(e_invarg2), s);
7751 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007752 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753 else
7754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_string = vim_strsave(s);
7756 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007757 }
7758}
7759
7760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007761 * "get()" function
7762 */
7763 static void
7764f_get(argvars, rettv)
7765 typeval *argvars;
7766 typeval *rettv;
7767{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007768 listitem *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007769 listvar *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007770 dictitem *di;
7771 dictvar *d;
7772 typeval *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007773
Bram Moolenaare9a41262005-01-15 22:18:47 +00007774 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007775 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007776 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007777 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007778 li = list_find(l, get_tv_number(&argvars[1]));
7779 if (li != NULL)
7780 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00007782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007783 else if (argvars[0].v_type == VAR_DICT)
7784 {
7785 if ((d = argvars[0].vval.v_dict) != NULL)
7786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007787 di = dict_find(d, get_tv_string(&argvars[1]), -1, NULL);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788 if (di != NULL)
7789 tv = &di->di_tv;
7790 }
7791 }
7792 else
7793 EMSG2(_(e_listdictarg), "get()");
7794
7795 if (tv == NULL)
7796 {
7797 if (argvars[2].v_type == VAR_UNKNOWN)
7798 rettv->vval.v_number = 0;
7799 else
7800 copy_tv(&argvars[2], rettv);
7801 }
7802 else
7803 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007804}
7805
7806/*
7807 * "getbufvar()" function
7808 */
7809 static void
7810f_getbufvar(argvars, rettv)
7811 typeval *argvars;
7812 typeval *rettv;
7813{
7814 buf_T *buf;
7815 buf_T *save_curbuf;
7816 char_u *varname;
7817 VAR v;
7818
7819 ++emsg_off;
7820 buf = get_buf_tv(&argvars[0]);
7821 varname = get_tv_string(&argvars[1]);
7822
7823 rettv->v_type = VAR_STRING;
7824 rettv->vval.v_string = NULL;
7825
7826 if (buf != NULL && varname != NULL)
7827 {
7828 if (*varname == '&') /* buffer-local-option */
7829 {
7830 /* set curbuf to be our buf, temporarily */
7831 save_curbuf = curbuf;
7832 curbuf = buf;
7833
7834 get_option_tv(&varname, rettv, TRUE);
7835
7836 /* restore previous notion of curbuf */
7837 curbuf = save_curbuf;
7838 }
7839 else
7840 {
7841 /* look up the variable */
7842 v = find_var_in_ga(&buf->b_vars, varname);
7843 if (v != NULL)
7844 copy_tv(&v->tv, rettv);
7845 }
7846 }
7847
7848 --emsg_off;
7849}
7850
7851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 * "getchar()" function
7853 */
7854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 varnumber_T n;
7860
7861 ++no_mapping;
7862 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 /* getchar(): blocking wait. */
7865 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 /* getchar(1): only check if char avail */
7868 n = vpeekc();
7869 else if (vpeekc() == NUL)
7870 /* getchar(0) and no char avail: return zero */
7871 n = 0;
7872 else
7873 /* getchar(0) and char avail: return char */
7874 n = safe_vgetc();
7875 --no_mapping;
7876 --allow_keys;
7877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (IS_SPECIAL(n) || mod_mask != 0)
7880 {
7881 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
7882 int i = 0;
7883
7884 /* Turn a special key into three bytes, plus modifier. */
7885 if (mod_mask != 0)
7886 {
7887 temp[i++] = K_SPECIAL;
7888 temp[i++] = KS_MODIFIER;
7889 temp[i++] = mod_mask;
7890 }
7891 if (IS_SPECIAL(n))
7892 {
7893 temp[i++] = K_SPECIAL;
7894 temp[i++] = K_SECOND(n);
7895 temp[i++] = K_THIRD(n);
7896 }
7897#ifdef FEAT_MBYTE
7898 else if (has_mbyte)
7899 i += (*mb_char2bytes)(n, temp + i);
7900#endif
7901 else
7902 temp[i++] = n;
7903 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007904 rettv->v_type = VAR_STRING;
7905 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 }
7907}
7908
7909/*
7910 * "getcharmod()" function
7911 */
7912/*ARGSUSED*/
7913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007914f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919}
7920
7921/*
7922 * "getcmdline()" function
7923 */
7924/*ARGSUSED*/
7925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 rettv->v_type = VAR_STRING;
7931 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932}
7933
7934/*
7935 * "getcmdpos()" function
7936 */
7937/*ARGSUSED*/
7938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944}
7945
7946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 * "getcwd()" function
7948 */
7949/*ARGSUSED*/
7950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007952 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 char_u cwd[MAXPATHL];
7956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 else
7961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965#endif
7966 }
7967}
7968
7969/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007970 * "getfontname()" function
7971 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007972/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007974f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978 rettv->v_type = VAR_STRING;
7979 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007980#ifdef FEAT_GUI
7981 if (gui.in_use)
7982 {
7983 GuiFont font;
7984 char_u *name = NULL;
7985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007986 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007987 {
7988 /* Get the "Normal" font. Either the name saved by
7989 * hl_set_font_name() or from the font ID. */
7990 font = gui.norm_font;
7991 name = hl_get_font_name();
7992 }
7993 else
7994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007996 if (STRCMP(name, "*") == 0) /* don't use font dialog */
7997 return;
7998 font = gui_mch_get_font(name, FALSE);
7999 if (font == NOFONT)
8000 return; /* Invalid font name, return empty string. */
8001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008004 gui_mch_free_font(font);
8005 }
8006#endif
8007}
8008
8009/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008010 * "getfperm({fname})" function
8011 */
8012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008014 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008016{
8017 char_u *fname;
8018 struct stat st;
8019 char_u *perm = NULL;
8020 char_u flags[] = "rwx";
8021 int i;
8022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008024
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008026 if (mch_stat((char *)fname, &st) >= 0)
8027 {
8028 perm = vim_strsave((char_u *)"---------");
8029 if (perm != NULL)
8030 {
8031 for (i = 0; i < 9; i++)
8032 {
8033 if (st.st_mode & (1 << (8 - i)))
8034 perm[i] = flags[i % 3];
8035 }
8036 }
8037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008039}
8040
8041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 * "getfsize({fname})" function
8043 */
8044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008046 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 char_u *fname;
8050 struct stat st;
8051
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008052 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055
8056 if (mch_stat((char *)fname, &st) >= 0)
8057 {
8058 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065}
8066
8067/*
8068 * "getftime({fname})" function
8069 */
8070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008072 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 char_u *fname;
8076 struct stat st;
8077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008078 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084}
8085
8086/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008087 * "getftype({fname})" function
8088 */
8089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008091 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008093{
8094 char_u *fname;
8095 struct stat st;
8096 char_u *type = NULL;
8097 char *t;
8098
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008102 if (mch_lstat((char *)fname, &st) >= 0)
8103 {
8104#ifdef S_ISREG
8105 if (S_ISREG(st.st_mode))
8106 t = "file";
8107 else if (S_ISDIR(st.st_mode))
8108 t = "dir";
8109# ifdef S_ISLNK
8110 else if (S_ISLNK(st.st_mode))
8111 t = "link";
8112# endif
8113# ifdef S_ISBLK
8114 else if (S_ISBLK(st.st_mode))
8115 t = "bdev";
8116# endif
8117# ifdef S_ISCHR
8118 else if (S_ISCHR(st.st_mode))
8119 t = "cdev";
8120# endif
8121# ifdef S_ISFIFO
8122 else if (S_ISFIFO(st.st_mode))
8123 t = "fifo";
8124# endif
8125# ifdef S_ISSOCK
8126 else if (S_ISSOCK(st.st_mode))
8127 t = "fifo";
8128# endif
8129 else
8130 t = "other";
8131#else
8132# ifdef S_IFMT
8133 switch (st.st_mode & S_IFMT)
8134 {
8135 case S_IFREG: t = "file"; break;
8136 case S_IFDIR: t = "dir"; break;
8137# ifdef S_IFLNK
8138 case S_IFLNK: t = "link"; break;
8139# endif
8140# ifdef S_IFBLK
8141 case S_IFBLK: t = "bdev"; break;
8142# endif
8143# ifdef S_IFCHR
8144 case S_IFCHR: t = "cdev"; break;
8145# endif
8146# ifdef S_IFIFO
8147 case S_IFIFO: t = "fifo"; break;
8148# endif
8149# ifdef S_IFSOCK
8150 case S_IFSOCK: t = "socket"; break;
8151# endif
8152 default: t = "other";
8153 }
8154# else
8155 if (mch_isdir(fname))
8156 t = "dir";
8157 else
8158 t = "file";
8159# endif
8160#endif
8161 type = vim_strsave((char_u *)t);
8162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008163 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008164}
8165
8166/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008167 * "getline(lnum)" function
8168 */
8169 static void
8170f_getline(argvars, rettv)
8171 typeval *argvars;
8172 typeval *rettv;
8173{
8174 linenr_T lnum;
8175 linenr_T end;
8176 char_u *p;
8177 listvar *l;
8178 listitem *li;
8179
8180 lnum = get_tv_lnum(argvars);
8181
8182 if (argvars[1].v_type == VAR_UNKNOWN)
8183 {
8184 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8185 p = ml_get(lnum);
8186 else
8187 p = (char_u *)"";
8188
8189 rettv->v_type = VAR_STRING;
8190 rettv->vval.v_string = vim_strsave(p);
8191 }
8192 else
8193 {
8194 end = get_tv_lnum(&argvars[1]);
8195 if (end < lnum)
8196 {
8197 EMSG(_(e_invrange));
8198 rettv->vval.v_number = 0;
8199 }
8200 else
8201 {
8202 l = list_alloc();
8203 if (l != NULL)
8204 {
8205 if (lnum < 1)
8206 lnum = 1;
8207 if (end > curbuf->b_ml.ml_line_count)
8208 end = curbuf->b_ml.ml_line_count;
8209 while (lnum <= end)
8210 {
8211 li = listitem_alloc();
8212 if (li == NULL)
8213 break;
8214 list_append(l, li);
8215 li->li_tv.v_type = VAR_STRING;
8216 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8217 }
8218 rettv->vval.v_list = l;
8219 rettv->v_type = VAR_LIST;
8220 ++l->lv_refcount;
8221 }
8222 }
8223 }
8224}
8225
8226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 * "getreg()" function
8228 */
8229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 char_u *strregname;
8235 int regname;
8236
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008237 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008240 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 regname = (strregname == NULL ? '"' : *strregname);
8242 if (regname == 0)
8243 regname = '"';
8244
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245 rettv->v_type = VAR_STRING;
8246 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
8248
8249/*
8250 * "getregtype()" function
8251 */
8252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008254 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 char_u *strregname;
8258 int regname;
8259 char_u buf[NUMBUFLEN + 2];
8260 long reglen = 0;
8261
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 else
8265 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008266 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
8268 regname = (strregname == NULL ? '"' : *strregname);
8269 if (regname == 0)
8270 regname = '"';
8271
8272 buf[0] = NUL;
8273 buf[1] = NUL;
8274 switch (get_reg_type(regname, &reglen))
8275 {
8276 case MLINE: buf[0] = 'V'; break;
8277 case MCHAR: buf[0] = 'v'; break;
8278#ifdef FEAT_VISUAL
8279 case MBLOCK:
8280 buf[0] = Ctrl_V;
8281 sprintf((char *)buf + 1, "%ld", reglen + 1);
8282 break;
8283#endif
8284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285 rettv->v_type = VAR_STRING;
8286 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287}
8288
8289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 * "getwinposx()" function
8291 */
8292/*ARGSUSED*/
8293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008295 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008296 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299#ifdef FEAT_GUI
8300 if (gui.in_use)
8301 {
8302 int x, y;
8303
8304 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307#endif
8308}
8309
8310/*
8311 * "getwinposy()" function
8312 */
8313/*ARGSUSED*/
8314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008315f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320#ifdef FEAT_GUI
8321 if (gui.in_use)
8322 {
8323 int x, y;
8324
8325 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328#endif
8329}
8330
8331/*
8332 * "getwinvar()" function
8333 */
8334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008335f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008336 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338{
8339 win_T *win, *oldcurwin;
8340 char_u *varname;
8341 VAR v;
8342
8343 ++emsg_off;
8344 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347 rettv->v_type = VAR_STRING;
8348 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
8350 if (win != NULL && varname != NULL)
8351 {
8352 if (*varname == '&') /* window-local-option */
8353 {
8354 /* set curwin to be our win, temporarily */
8355 oldcurwin = curwin;
8356 curwin = win;
8357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
8360 /* restore previous notion of curwin */
8361 curwin = oldcurwin;
8362 }
8363 else
8364 {
8365 /* look up the variable */
8366 v = find_var_in_ga(&win->w_vars, varname);
8367 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 }
8370 }
8371
8372 --emsg_off;
8373}
8374
8375/*
8376 * "glob()" function
8377 */
8378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008380 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008381 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382{
8383 expand_T xpc;
8384
8385 ExpandInit(&xpc);
8386 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 rettv->v_type = VAR_STRING;
8388 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8390 ExpandCleanup(&xpc);
8391}
8392
8393/*
8394 * "globpath()" function
8395 */
8396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008397f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008398 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400{
8401 char_u buf1[NUMBUFLEN];
8402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403 rettv->v_type = VAR_STRING;
8404 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8405 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406}
8407
8408/*
8409 * "has()" function
8410 */
8411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008413 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
8416 int i;
8417 char_u *name;
8418 int n = FALSE;
8419 static char *(has_list[]) =
8420 {
8421#ifdef AMIGA
8422 "amiga",
8423# ifdef FEAT_ARP
8424 "arp",
8425# endif
8426#endif
8427#ifdef __BEOS__
8428 "beos",
8429#endif
8430#ifdef MSDOS
8431# ifdef DJGPP
8432 "dos32",
8433# else
8434 "dos16",
8435# endif
8436#endif
8437#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8438 "mac",
8439#endif
8440#if defined(MACOS_X_UNIX)
8441 "macunix",
8442#endif
8443#ifdef OS2
8444 "os2",
8445#endif
8446#ifdef __QNX__
8447 "qnx",
8448#endif
8449#ifdef RISCOS
8450 "riscos",
8451#endif
8452#ifdef UNIX
8453 "unix",
8454#endif
8455#ifdef VMS
8456 "vms",
8457#endif
8458#ifdef WIN16
8459 "win16",
8460#endif
8461#ifdef WIN32
8462 "win32",
8463#endif
8464#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8465 "win32unix",
8466#endif
8467#ifdef WIN64
8468 "win64",
8469#endif
8470#ifdef EBCDIC
8471 "ebcdic",
8472#endif
8473#ifndef CASE_INSENSITIVE_FILENAME
8474 "fname_case",
8475#endif
8476#ifdef FEAT_ARABIC
8477 "arabic",
8478#endif
8479#ifdef FEAT_AUTOCMD
8480 "autocmd",
8481#endif
8482#ifdef FEAT_BEVAL
8483 "balloon_eval",
8484#endif
8485#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8486 "builtin_terms",
8487# ifdef ALL_BUILTIN_TCAPS
8488 "all_builtin_terms",
8489# endif
8490#endif
8491#ifdef FEAT_BYTEOFF
8492 "byte_offset",
8493#endif
8494#ifdef FEAT_CINDENT
8495 "cindent",
8496#endif
8497#ifdef FEAT_CLIENTSERVER
8498 "clientserver",
8499#endif
8500#ifdef FEAT_CLIPBOARD
8501 "clipboard",
8502#endif
8503#ifdef FEAT_CMDL_COMPL
8504 "cmdline_compl",
8505#endif
8506#ifdef FEAT_CMDHIST
8507 "cmdline_hist",
8508#endif
8509#ifdef FEAT_COMMENTS
8510 "comments",
8511#endif
8512#ifdef FEAT_CRYPT
8513 "cryptv",
8514#endif
8515#ifdef FEAT_CSCOPE
8516 "cscope",
8517#endif
8518#ifdef DEBUG
8519 "debug",
8520#endif
8521#ifdef FEAT_CON_DIALOG
8522 "dialog_con",
8523#endif
8524#ifdef FEAT_GUI_DIALOG
8525 "dialog_gui",
8526#endif
8527#ifdef FEAT_DIFF
8528 "diff",
8529#endif
8530#ifdef FEAT_DIGRAPHS
8531 "digraphs",
8532#endif
8533#ifdef FEAT_DND
8534 "dnd",
8535#endif
8536#ifdef FEAT_EMACS_TAGS
8537 "emacs_tags",
8538#endif
8539 "eval", /* always present, of course! */
8540#ifdef FEAT_EX_EXTRA
8541 "ex_extra",
8542#endif
8543#ifdef FEAT_SEARCH_EXTRA
8544 "extra_search",
8545#endif
8546#ifdef FEAT_FKMAP
8547 "farsi",
8548#endif
8549#ifdef FEAT_SEARCHPATH
8550 "file_in_path",
8551#endif
8552#ifdef FEAT_FIND_ID
8553 "find_in_path",
8554#endif
8555#ifdef FEAT_FOLDING
8556 "folding",
8557#endif
8558#ifdef FEAT_FOOTER
8559 "footer",
8560#endif
8561#if !defined(USE_SYSTEM) && defined(UNIX)
8562 "fork",
8563#endif
8564#ifdef FEAT_GETTEXT
8565 "gettext",
8566#endif
8567#ifdef FEAT_GUI
8568 "gui",
8569#endif
8570#ifdef FEAT_GUI_ATHENA
8571# ifdef FEAT_GUI_NEXTAW
8572 "gui_neXtaw",
8573# else
8574 "gui_athena",
8575# endif
8576#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008577#ifdef FEAT_GUI_KDE
8578 "gui_kde",
8579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#ifdef FEAT_GUI_GTK
8581 "gui_gtk",
8582# ifdef HAVE_GTK2
8583 "gui_gtk2",
8584# endif
8585#endif
8586#ifdef FEAT_GUI_MAC
8587 "gui_mac",
8588#endif
8589#ifdef FEAT_GUI_MOTIF
8590 "gui_motif",
8591#endif
8592#ifdef FEAT_GUI_PHOTON
8593 "gui_photon",
8594#endif
8595#ifdef FEAT_GUI_W16
8596 "gui_win16",
8597#endif
8598#ifdef FEAT_GUI_W32
8599 "gui_win32",
8600#endif
8601#ifdef FEAT_HANGULIN
8602 "hangul_input",
8603#endif
8604#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8605 "iconv",
8606#endif
8607#ifdef FEAT_INS_EXPAND
8608 "insert_expand",
8609#endif
8610#ifdef FEAT_JUMPLIST
8611 "jumplist",
8612#endif
8613#ifdef FEAT_KEYMAP
8614 "keymap",
8615#endif
8616#ifdef FEAT_LANGMAP
8617 "langmap",
8618#endif
8619#ifdef FEAT_LIBCALL
8620 "libcall",
8621#endif
8622#ifdef FEAT_LINEBREAK
8623 "linebreak",
8624#endif
8625#ifdef FEAT_LISP
8626 "lispindent",
8627#endif
8628#ifdef FEAT_LISTCMDS
8629 "listcmds",
8630#endif
8631#ifdef FEAT_LOCALMAP
8632 "localmap",
8633#endif
8634#ifdef FEAT_MENU
8635 "menu",
8636#endif
8637#ifdef FEAT_SESSION
8638 "mksession",
8639#endif
8640#ifdef FEAT_MODIFY_FNAME
8641 "modify_fname",
8642#endif
8643#ifdef FEAT_MOUSE
8644 "mouse",
8645#endif
8646#ifdef FEAT_MOUSESHAPE
8647 "mouseshape",
8648#endif
8649#if defined(UNIX) || defined(VMS)
8650# ifdef FEAT_MOUSE_DEC
8651 "mouse_dec",
8652# endif
8653# ifdef FEAT_MOUSE_GPM
8654 "mouse_gpm",
8655# endif
8656# ifdef FEAT_MOUSE_JSB
8657 "mouse_jsbterm",
8658# endif
8659# ifdef FEAT_MOUSE_NET
8660 "mouse_netterm",
8661# endif
8662# ifdef FEAT_MOUSE_PTERM
8663 "mouse_pterm",
8664# endif
8665# ifdef FEAT_MOUSE_XTERM
8666 "mouse_xterm",
8667# endif
8668#endif
8669#ifdef FEAT_MBYTE
8670 "multi_byte",
8671#endif
8672#ifdef FEAT_MBYTE_IME
8673 "multi_byte_ime",
8674#endif
8675#ifdef FEAT_MULTI_LANG
8676 "multi_lang",
8677#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008678#ifdef FEAT_MZSCHEME
8679 "mzscheme",
8680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681#ifdef FEAT_OLE
8682 "ole",
8683#endif
8684#ifdef FEAT_OSFILETYPE
8685 "osfiletype",
8686#endif
8687#ifdef FEAT_PATH_EXTRA
8688 "path_extra",
8689#endif
8690#ifdef FEAT_PERL
8691#ifndef DYNAMIC_PERL
8692 "perl",
8693#endif
8694#endif
8695#ifdef FEAT_PYTHON
8696#ifndef DYNAMIC_PYTHON
8697 "python",
8698#endif
8699#endif
8700#ifdef FEAT_POSTSCRIPT
8701 "postscript",
8702#endif
8703#ifdef FEAT_PRINTER
8704 "printer",
8705#endif
8706#ifdef FEAT_QUICKFIX
8707 "quickfix",
8708#endif
8709#ifdef FEAT_RIGHTLEFT
8710 "rightleft",
8711#endif
8712#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8713 "ruby",
8714#endif
8715#ifdef FEAT_SCROLLBIND
8716 "scrollbind",
8717#endif
8718#ifdef FEAT_CMDL_INFO
8719 "showcmd",
8720 "cmdline_info",
8721#endif
8722#ifdef FEAT_SIGNS
8723 "signs",
8724#endif
8725#ifdef FEAT_SMARTINDENT
8726 "smartindent",
8727#endif
8728#ifdef FEAT_SNIFF
8729 "sniff",
8730#endif
8731#ifdef FEAT_STL_OPT
8732 "statusline",
8733#endif
8734#ifdef FEAT_SUN_WORKSHOP
8735 "sun_workshop",
8736#endif
8737#ifdef FEAT_NETBEANS_INTG
8738 "netbeans_intg",
8739#endif
8740#ifdef FEAT_SYN_HL
8741 "syntax",
8742#endif
8743#if defined(USE_SYSTEM) || !defined(UNIX)
8744 "system",
8745#endif
8746#ifdef FEAT_TAG_BINS
8747 "tag_binary",
8748#endif
8749#ifdef FEAT_TAG_OLDSTATIC
8750 "tag_old_static",
8751#endif
8752#ifdef FEAT_TAG_ANYWHITE
8753 "tag_any_white",
8754#endif
8755#ifdef FEAT_TCL
8756# ifndef DYNAMIC_TCL
8757 "tcl",
8758# endif
8759#endif
8760#ifdef TERMINFO
8761 "terminfo",
8762#endif
8763#ifdef FEAT_TERMRESPONSE
8764 "termresponse",
8765#endif
8766#ifdef FEAT_TEXTOBJ
8767 "textobjects",
8768#endif
8769#ifdef HAVE_TGETENT
8770 "tgetent",
8771#endif
8772#ifdef FEAT_TITLE
8773 "title",
8774#endif
8775#ifdef FEAT_TOOLBAR
8776 "toolbar",
8777#endif
8778#ifdef FEAT_USR_CMDS
8779 "user-commands", /* was accidentally included in 5.4 */
8780 "user_commands",
8781#endif
8782#ifdef FEAT_VIMINFO
8783 "viminfo",
8784#endif
8785#ifdef FEAT_VERTSPLIT
8786 "vertsplit",
8787#endif
8788#ifdef FEAT_VIRTUALEDIT
8789 "virtualedit",
8790#endif
8791#ifdef FEAT_VISUAL
8792 "visual",
8793#endif
8794#ifdef FEAT_VISUALEXTRA
8795 "visualextra",
8796#endif
8797#ifdef FEAT_VREPLACE
8798 "vreplace",
8799#endif
8800#ifdef FEAT_WILDIGN
8801 "wildignore",
8802#endif
8803#ifdef FEAT_WILDMENU
8804 "wildmenu",
8805#endif
8806#ifdef FEAT_WINDOWS
8807 "windows",
8808#endif
8809#ifdef FEAT_WAK
8810 "winaltkeys",
8811#endif
8812#ifdef FEAT_WRITEBACKUP
8813 "writebackup",
8814#endif
8815#ifdef FEAT_XIM
8816 "xim",
8817#endif
8818#ifdef FEAT_XFONTSET
8819 "xfontset",
8820#endif
8821#ifdef USE_XSMP
8822 "xsmp",
8823#endif
8824#ifdef USE_XSMP_INTERACT
8825 "xsmp_interact",
8826#endif
8827#ifdef FEAT_XCLIPBOARD
8828 "xterm_clipboard",
8829#endif
8830#ifdef FEAT_XTERM_SAVE
8831 "xterm_save",
8832#endif
8833#if defined(UNIX) && defined(FEAT_X11)
8834 "X11",
8835#endif
8836 NULL
8837 };
8838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 for (i = 0; has_list[i] != NULL; ++i)
8841 if (STRICMP(name, has_list[i]) == 0)
8842 {
8843 n = TRUE;
8844 break;
8845 }
8846
8847 if (n == FALSE)
8848 {
8849 if (STRNICMP(name, "patch", 5) == 0)
8850 n = has_patch(atoi((char *)name + 5));
8851 else if (STRICMP(name, "vim_starting") == 0)
8852 n = (starting != 0);
8853#ifdef DYNAMIC_TCL
8854 else if (STRICMP(name, "tcl") == 0)
8855 n = tcl_enabled(FALSE);
8856#endif
8857#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
8858 else if (STRICMP(name, "iconv") == 0)
8859 n = iconv_enabled(FALSE);
8860#endif
8861#ifdef DYNAMIC_RUBY
8862 else if (STRICMP(name, "ruby") == 0)
8863 n = ruby_enabled(FALSE);
8864#endif
8865#ifdef DYNAMIC_PYTHON
8866 else if (STRICMP(name, "python") == 0)
8867 n = python_enabled(FALSE);
8868#endif
8869#ifdef DYNAMIC_PERL
8870 else if (STRICMP(name, "perl") == 0)
8871 n = perl_enabled(FALSE);
8872#endif
8873#ifdef FEAT_GUI
8874 else if (STRICMP(name, "gui_running") == 0)
8875 n = (gui.in_use || gui.starting);
8876# ifdef FEAT_GUI_W32
8877 else if (STRICMP(name, "gui_win32s") == 0)
8878 n = gui_is_win32s();
8879# endif
8880# ifdef FEAT_BROWSE
8881 else if (STRICMP(name, "browse") == 0)
8882 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
8883# endif
8884#endif
8885#ifdef FEAT_SYN_HL
8886 else if (STRICMP(name, "syntax_items") == 0)
8887 n = syntax_present(curbuf);
8888#endif
8889#if defined(WIN3264)
8890 else if (STRICMP(name, "win95") == 0)
8891 n = mch_windows95();
8892#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00008893#ifdef FEAT_NETBEANS_INTG
8894 else if (STRICMP(name, "netbeans_enabled") == 0)
8895 n = usingNetbeans;
8896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 }
8898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900}
8901
8902/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008903 * "has_key()" function
8904 */
8905 static void
8906f_has_key(argvars, rettv)
8907 typeval *argvars;
8908 typeval *rettv;
8909{
8910 rettv->vval.v_number = 0;
8911 if (argvars[0].v_type != VAR_DICT)
8912 {
8913 EMSG(_(e_dictreq));
8914 return;
8915 }
8916 if (argvars[0].vval.v_dict == NULL)
8917 return;
8918
8919 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008920 get_tv_string(&argvars[1]), -1, NULL) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921}
8922
8923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 * "hasmapto()" function
8925 */
8926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008928 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 char_u *name;
8932 char_u *mode;
8933 char_u buf[NUMBUFLEN];
8934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008936 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 mode = (char_u *)"nvo";
8938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
8941 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945}
8946
8947/*
8948 * "histadd()" function
8949 */
8950/*ARGSUSED*/
8951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008953 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956#ifdef FEAT_CMDHIST
8957 int histype;
8958 char_u *str;
8959 char_u buf[NUMBUFLEN];
8960#endif
8961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (check_restricted() || check_secure())
8964 return;
8965#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 if (histype >= 0)
8968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 if (*str != NUL)
8971 {
8972 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 return;
8975 }
8976 }
8977#endif
8978}
8979
8980/*
8981 * "histdel()" function
8982 */
8983/*ARGSUSED*/
8984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989#ifdef FEAT_CMDHIST
8990 int n;
8991 char_u buf[NUMBUFLEN];
8992
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008996 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
8999 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 else
9001 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9003 get_tv_string_buf(&argvars[1], buf));
9004 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007#endif
9008}
9009
9010/*
9011 * "histget()" function
9012 */
9013/*ARGSUSED*/
9014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
9019#ifdef FEAT_CMDHIST
9020 int type;
9021 int idx;
9022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009024 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 idx = get_history_idx(type);
9026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 idx = (int)get_tv_number(&argvars[1]);
9028 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033}
9034
9035/*
9036 * "histnr()" function
9037 */
9038/*ARGSUSED*/
9039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009041 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
9044 int i;
9045
9046#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (i >= HIST_CMD && i < HIST_COUNT)
9049 i = get_history_idx(i);
9050 else
9051#endif
9052 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054}
9055
9056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 * "highlightID(name)" function
9058 */
9059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009061 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065}
9066
9067/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009068 * "highlight_exists()" function
9069 */
9070 static void
9071f_hlexists(argvars, rettv)
9072 typeval *argvars;
9073 typeval *rettv;
9074{
9075 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9076}
9077
9078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 * "hostname()" function
9080 */
9081/*ARGSUSED*/
9082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009084 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087 char_u hostname[256];
9088
9089 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->v_type = VAR_STRING;
9091 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092}
9093
9094/*
9095 * iconv() function
9096 */
9097/*ARGSUSED*/
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009100 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103#ifdef FEAT_MBYTE
9104 char_u buf1[NUMBUFLEN];
9105 char_u buf2[NUMBUFLEN];
9106 char_u *from, *to, *str;
9107 vimconv_T vimconv;
9108#endif
9109
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->v_type = VAR_STRING;
9111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112
9113#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 str = get_tv_string(&argvars[0]);
9115 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9116 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 vimconv.vc_type = CONV_NONE;
9118 convert_setup(&vimconv, from, to);
9119
9120 /* If the encodings are equal, no conversion needed. */
9121 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125
9126 convert_setup(&vimconv, NULL, NULL);
9127 vim_free(from);
9128 vim_free(to);
9129#endif
9130}
9131
9132/*
9133 * "indent()" function
9134 */
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009137 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 linenr_T lnum;
9141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009149/*
9150 * "index()" function
9151 */
9152 static void
9153f_index(argvars, rettv)
9154 typeval *argvars;
9155 typeval *rettv;
9156{
9157 listvar *l;
9158 listitem *item;
9159 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009160 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009161 int ic = FALSE;
9162
9163 rettv->vval.v_number = -1;
9164 if (argvars[0].v_type != VAR_LIST)
9165 {
9166 EMSG(_(e_listreq));
9167 return;
9168 }
9169 l = argvars[0].vval.v_list;
9170 if (l != NULL)
9171 {
9172 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009173 {
9174 min_idx = get_tv_number(&argvars[2]);
9175 if (argvars[3].v_type != VAR_UNKNOWN)
9176 ic = get_tv_number(&argvars[3]);
9177 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009178
9179 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009180 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009181 {
9182 rettv->vval.v_number = idx;
9183 break;
9184 }
9185 }
9186}
9187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188static int inputsecret_flag = 0;
9189
9190/*
9191 * "input()" function
9192 * Also handles inputsecret() when inputsecret is set.
9193 */
9194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009196 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 char_u *p = NULL;
9201 int c;
9202 char_u buf[NUMBUFLEN];
9203 int cmd_silent_save = cmd_silent;
9204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
9207#ifdef NO_CONSOLE_INPUT
9208 /* While starting up, there is no place to enter text. */
9209 if (no_console_input())
9210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 return;
9213 }
9214#endif
9215
9216 cmd_silent = FALSE; /* Want to see the prompt. */
9217 if (prompt != NULL)
9218 {
9219 /* Only the part of the message after the last NL is considered as
9220 * prompt for the command line */
9221 p = vim_strrchr(prompt, '\n');
9222 if (p == NULL)
9223 p = prompt;
9224 else
9225 {
9226 ++p;
9227 c = *p;
9228 *p = NUL;
9229 msg_start();
9230 msg_clr_eos();
9231 msg_puts_attr(prompt, echo_attr);
9232 msg_didout = FALSE;
9233 msg_starthere();
9234 *p = c;
9235 }
9236 cmdline_row = msg_row;
9237 }
9238
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009239 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9244
9245 /* since the user typed this, no need to wait for return */
9246 need_wait_return = FALSE;
9247 msg_didout = FALSE;
9248 cmd_silent = cmd_silent_save;
9249}
9250
9251/*
9252 * "inputdialog()" function
9253 */
9254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009256 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259#if defined(FEAT_GUI_TEXTDIALOG)
9260 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9261 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9262 {
9263 char_u *message;
9264 char_u buf[NUMBUFLEN];
9265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009266 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009267 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 IObuff[IOSIZE - 1] = NUL;
9271 }
9272 else
9273 IObuff[0] = NUL;
9274 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9275 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 else
9278 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009279 if (argvars[1].v_type != VAR_UNKNOWN
9280 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 rettv->vval.v_string = vim_strsave(
9282 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 }
9288 else
9289#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291}
9292
9293static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9294
9295/*
9296 * "inputrestore()" function
9297 */
9298/*ARGSUSED*/
9299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009301 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 if (ga_userinput.ga_len > 0)
9305 {
9306 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9308 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 }
9311 else if (p_verbose > 1)
9312 {
9313 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 }
9316}
9317
9318/*
9319 * "inputsave()" function
9320 */
9321/*ARGSUSED*/
9322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009324 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
9327 /* Add an entry to the stack of typehead storage. */
9328 if (ga_grow(&ga_userinput, 1) == OK)
9329 {
9330 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9331 + ga_userinput.ga_len);
9332 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 }
9335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337}
9338
9339/*
9340 * "inputsecret()" function
9341 */
9342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009344 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 ++cmdline_star;
9348 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 --cmdline_star;
9351 --inputsecret_flag;
9352}
9353
9354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009355 * "insert()" function
9356 */
9357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009361{
9362 long before = 0;
9363 long n;
9364 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009365 listvar *l;
9366
9367 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009368 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009369 else if ((l = argvars[0].vval.v_list) != NULL)
9370 {
9371 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009373
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009374 n = before;
9375 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009376 if (n > 0)
9377 EMSGN(_(e_listidx), before);
9378 else
9379 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009380 list_insert_tv(l, &argvars[1], item);
9381 ++l->lv_refcount;
9382 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009383 }
9384 }
9385}
9386
9387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 * "isdirectory()" function
9389 */
9390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009392 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396}
9397
Bram Moolenaar8c711452005-01-14 21:53:12 +00009398static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
9399
9400/*
9401 * Turn a dict into a list:
9402 * "what" == 0: list of keys
9403 * "what" == 1: list of values
9404 * "what" == 2: list of items
9405 */
9406 static void
9407dict_list(argvars, rettv, what)
9408 typeval *argvars;
9409 typeval *rettv;
9410 int what;
9411{
9412 listvar *l;
9413 listvar *l2;
9414 dictitem *di;
9415 listitem *li;
9416 listitem *li2;
9417
9418 rettv->vval.v_number = 0;
9419 if (argvars[0].v_type != VAR_DICT)
9420 {
9421 EMSG(_(e_dictreq));
9422 return;
9423 }
9424 if (argvars[0].vval.v_dict == NULL)
9425 return;
9426
9427 l = list_alloc();
9428 if (l == NULL)
9429 return;
9430 rettv->v_type = VAR_LIST;
9431 rettv->vval.v_list = l;
9432 ++l->lv_refcount;
9433
9434 for (di = argvars[0].vval.v_dict->dv_first; di != NULL; di = di->di_next)
9435 {
9436 li = listitem_alloc();
9437 if (li == NULL)
9438 break;
9439 list_append(l, li);
9440
9441 if (what == 0)
9442 {
9443 /* keys() */
9444 li->li_tv.v_type = VAR_STRING;
9445 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9446 }
9447 else if (what == 1)
9448 {
9449 /* values() */
9450 copy_tv(&di->di_tv, &li->li_tv);
9451 }
9452 else
9453 {
9454 /* items() */
9455 l2 = list_alloc();
9456 li->li_tv.v_type = VAR_LIST;
9457 li->li_tv.vval.v_list = l2;
9458 if (l2 == NULL)
9459 break;
9460 ++l2->lv_refcount;
9461
9462 li2 = listitem_alloc();
9463 if (li2 == NULL)
9464 break;
9465 list_append(l2, li2);
9466 li2->li_tv.v_type = VAR_STRING;
9467 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9468
9469 li2 = listitem_alloc();
9470 if (li2 == NULL)
9471 break;
9472 list_append(l2, li2);
9473 copy_tv(&di->di_tv, &li2->li_tv);
9474 }
9475 }
9476}
9477
9478/*
9479 * "items(dict)" function
9480 */
9481 static void
9482f_items(argvars, rettv)
9483 typeval *argvars;
9484 typeval *rettv;
9485{
9486 dict_list(argvars, rettv, 2);
9487}
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009490 * "join()" function
9491 */
9492 static void
9493f_join(argvars, rettv)
9494 typeval *argvars;
9495 typeval *rettv;
9496{
9497 garray_T ga;
9498 char_u *sep;
9499
9500 rettv->vval.v_number = 0;
9501 if (argvars[0].v_type != VAR_LIST)
9502 {
9503 EMSG(_(e_listreq));
9504 return;
9505 }
9506 if (argvars[0].vval.v_list == NULL)
9507 return;
9508 if (argvars[1].v_type == VAR_UNKNOWN)
9509 sep = (char_u *)" ";
9510 else
9511 sep = get_tv_string(&argvars[1]);
9512
9513 ga_init2(&ga, (int)sizeof(char), 80);
9514 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9515 ga_append(&ga, NUL);
9516
9517 rettv->v_type = VAR_STRING;
9518 rettv->vval.v_string = (char_u *)ga.ga_data;
9519}
9520
9521/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009522 * "keys()" function
9523 */
9524 static void
9525f_keys(argvars, rettv)
9526 typeval *argvars;
9527 typeval *rettv;
9528{
9529 dict_list(argvars, rettv, 0);
9530}
9531
9532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 * "last_buffer_nr()" function.
9534 */
9535/*ARGSUSED*/
9536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009538 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
9541 int n = 0;
9542 buf_T *buf;
9543
9544 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9545 if (n < buf->b_fnum)
9546 n = buf->b_fnum;
9547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549}
9550
9551/*
9552 * "len()" function
9553 */
9554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009556 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009558{
9559 switch (argvars[0].v_type)
9560 {
9561 case VAR_STRING:
9562 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->vval.v_number = (varnumber_T)STRLEN(
9564 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009565 break;
9566 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009568 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009569 case VAR_DICT:
9570 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009572 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009573 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 break;
9575 }
9576}
9577
Bram Moolenaar0d660222005-01-07 21:51:51 +00009578static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009579
9580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009584 int type;
9585{
9586#ifdef FEAT_LIBCALL
9587 char_u *string_in;
9588 char_u **string_result;
9589 int nr_result;
9590#endif
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009593 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009595 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597
9598 if (check_restricted() || check_secure())
9599 return;
9600
9601#ifdef FEAT_LIBCALL
9602 /* The first two args must be strings, otherwise its meaningless */
9603 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9604 {
9605 if (argvars[2].v_type == VAR_NUMBER)
9606 string_in = NULL;
9607 else
9608 string_in = argvars[2].vval.v_string;
9609 if (type == VAR_NUMBER)
9610 string_result = NULL;
9611 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 if (mch_libcall(argvars[0].vval.v_string,
9614 argvars[1].vval.v_string,
9615 string_in,
9616 argvars[2].vval.v_number,
9617 string_result,
9618 &nr_result) == OK
9619 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 }
9622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623}
9624
9625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009626 * "libcall()" function
9627 */
9628 static void
9629f_libcall(argvars, rettv)
9630 typeval *argvars;
9631 typeval *rettv;
9632{
9633 libcall_common(argvars, rettv, VAR_STRING);
9634}
9635
9636/*
9637 * "libcallnr()" function
9638 */
9639 static void
9640f_libcallnr(argvars, rettv)
9641 typeval *argvars;
9642 typeval *rettv;
9643{
9644 libcall_common(argvars, rettv, VAR_NUMBER);
9645}
9646
9647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 * "line(string)" function
9649 */
9650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655 linenr_T lnum = 0;
9656 pos_T *fp;
9657
9658 fp = var2fpos(&argvars[0], TRUE);
9659 if (fp != NULL)
9660 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
9664/*
9665 * "line2byte(lnum)" function
9666 */
9667/*ARGSUSED*/
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009670 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#else
9676 linenr_T lnum;
9677
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
9683 if (rettv->vval.v_number >= 0)
9684 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#endif
9686}
9687
9688/*
9689 * "lispindent(lnum)" function
9690 */
9691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696#ifdef FEAT_LISP
9697 pos_T pos;
9698 linenr_T lnum;
9699
9700 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9703 {
9704 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 curwin->w_cursor = pos;
9707 }
9708 else
9709#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712
9713/*
9714 * "localtime()" function
9715 */
9716/*ARGSUSED*/
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009719 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
9727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009729 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 int exact;
9732{
9733 char_u *keys;
9734 char_u *which;
9735 char_u buf[NUMBUFLEN];
9736 char_u *keys_buf = NULL;
9737 char_u *rhs;
9738 int mode;
9739 garray_T ga;
9740
9741 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->v_type = VAR_STRING;
9743 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 if (*keys == NUL)
9747 return;
9748
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009749 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 else
9752 which = (char_u *)"";
9753 mode = get_map_mode(&which, 0);
9754
9755 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9756 rhs = check_map(keys, mode, exact);
9757 vim_free(keys_buf);
9758 if (rhs != NULL)
9759 {
9760 ga_init(&ga);
9761 ga.ga_itemsize = 1;
9762 ga.ga_growsize = 40;
9763
9764 while (*rhs != NUL)
9765 ga_concat(&ga, str2special(&rhs, FALSE));
9766
9767 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 }
9770}
9771
9772/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009773 * "map()" function
9774 */
9775 static void
9776f_map(argvars, rettv)
9777 typeval *argvars;
9778 typeval *rettv;
9779{
9780 filter_map(argvars, rettv, TRUE);
9781}
9782
9783/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009784 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 */
9786 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009787f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009788 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009791 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009795 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 */
9797 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009798f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009799 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009802 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
Bram Moolenaar0d660222005-01-07 21:51:51 +00009805static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009809 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 int type;
9812{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009813 char_u *str = NULL;
9814 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 char_u *pat;
9816 regmatch_T regmatch;
9817 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009818 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 char_u *save_cpo;
9820 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009821 long nth = 1;
9822 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009823 listvar *l = NULL;
9824 listitem *li = NULL;
9825 long idx = 0;
9826 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
9828 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9829 save_cpo = p_cpo;
9830 p_cpo = (char_u *)"";
9831
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 if (type == 2)
9833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834 rettv->v_type = VAR_STRING;
9835 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 }
9837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009840 if (argvars[0].v_type == VAR_LIST)
9841 {
9842 if ((l = argvars[0].vval.v_list) == NULL)
9843 goto theend;
9844 li = l->lv_first;
9845 }
9846 else
9847 expr = str = get_tv_string(&argvars[0]);
9848
9849 pat = get_tv_string_buf(&argvars[1], patbuf);
9850
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009851 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009854 if (l != NULL)
9855 {
9856 li = list_find(l, start);
9857 if (li == NULL)
9858 goto theend;
9859 if (start < 0)
9860 {
9861 listitem *ni;
9862
9863 /* Need to compute the index. */
9864 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
9865 ++idx;
9866 }
9867 else
9868 idx = start;
9869 }
9870 else
9871 {
9872 if (start < 0)
9873 start = 0;
9874 if (start > (long)STRLEN(str))
9875 goto theend;
9876 str += start;
9877 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009878
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009879 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 }
9882
9883 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9884 if (regmatch.regprog != NULL)
9885 {
9886 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009887
9888 while (1)
9889 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009890 if (l != NULL)
9891 {
9892 if (li == NULL)
9893 {
9894 match = FALSE;
9895 break;
9896 }
9897 str = echo_string(&li->li_tv, &tofree, strbuf);
9898 }
9899
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009900 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009901
9902 if (l != NULL)
9903 vim_free(tofree);
9904 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009906 if (l == NULL && !match)
9907 break;
9908
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009909 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009910 if (l != NULL)
9911 {
9912 li = li->li_next;
9913 ++idx;
9914 }
9915 else
9916 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009917#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009918 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009919#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009920 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009921#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009922 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009923 }
9924
9925 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 {
9927 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009928 {
9929 if (l != NULL)
9930 copy_tv(&li->li_tv, rettv);
9931 else
9932 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009934 }
9935 else if (l != NULL)
9936 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 {
9939 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 (varnumber_T)(regmatch.startp[0] - str);
9942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 }
9947 }
9948 vim_free(regmatch.regprog);
9949 }
9950
9951theend:
9952 p_cpo = save_cpo;
9953}
9954
9955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009956 * "match()" function
9957 */
9958 static void
9959f_match(argvars, rettv)
9960 typeval *argvars;
9961 typeval *rettv;
9962{
9963 find_some_match(argvars, rettv, 1);
9964}
9965
9966/*
9967 * "matchend()" function
9968 */
9969 static void
9970f_matchend(argvars, rettv)
9971 typeval *argvars;
9972 typeval *rettv;
9973{
9974 find_some_match(argvars, rettv, 0);
9975}
9976
9977/*
9978 * "matchstr()" function
9979 */
9980 static void
9981f_matchstr(argvars, rettv)
9982 typeval *argvars;
9983 typeval *rettv;
9984{
9985 find_some_match(argvars, rettv, 2);
9986}
9987
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009988static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
9989
9990 static void
9991max_min(argvars, rettv, domax)
9992 typeval *argvars;
9993 typeval *rettv;
9994 int domax;
9995{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009996 long n = 0;
9997 long i;
9998
9999 if (argvars[0].v_type == VAR_LIST)
10000 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 listvar *l;
10002 listitem *li;
10003
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010004 l = argvars[0].vval.v_list;
10005 if (l != NULL)
10006 {
10007 li = l->lv_first;
10008 if (li != NULL)
10009 {
10010 n = get_tv_number(&li->li_tv);
10011 while (1)
10012 {
10013 li = li->li_next;
10014 if (li == NULL)
10015 break;
10016 i = get_tv_number(&li->li_tv);
10017 if (domax ? i > n : i < n)
10018 n = i;
10019 }
10020 }
10021 }
10022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010023 else if (argvars[0].v_type == VAR_DICT)
10024 {
10025 dictvar *d;
10026 dictitem *di;
10027
10028 d = argvars[0].vval.v_dict;
10029 if (d != NULL)
10030 {
10031 di = d->dv_first;
10032 if (di != NULL)
10033 {
10034 n = get_tv_number(&di->di_tv);
10035 while (1)
10036 {
10037 di = di->di_next;
10038 if (di == NULL)
10039 break;
10040 i = get_tv_number(&di->di_tv);
10041 if (domax ? i > n : i < n)
10042 n = i;
10043 }
10044 }
10045 }
10046 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010047 else
10048 EMSG(_(e_listreq));
10049 rettv->vval.v_number = n;
10050}
10051
10052/*
10053 * "max()" function
10054 */
10055 static void
10056f_max(argvars, rettv)
10057 typeval *argvars;
10058 typeval *rettv;
10059{
10060 max_min(argvars, rettv, TRUE);
10061}
10062
10063/*
10064 * "min()" function
10065 */
10066 static void
10067f_min(argvars, rettv)
10068 typeval *argvars;
10069 typeval *rettv;
10070{
10071 max_min(argvars, rettv, FALSE);
10072}
10073
Bram Moolenaar0d660222005-01-07 21:51:51 +000010074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 * "mode()" function
10076 */
10077/*ARGSUSED*/
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010080 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 char_u buf[2];
10084
10085#ifdef FEAT_VISUAL
10086 if (VIsual_active)
10087 {
10088 if (VIsual_select)
10089 buf[0] = VIsual_mode + 's' - 'v';
10090 else
10091 buf[0] = VIsual_mode;
10092 }
10093 else
10094#endif
10095 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10096 buf[0] = 'r';
10097 else if (State & INSERT)
10098 {
10099 if (State & REPLACE_FLAG)
10100 buf[0] = 'R';
10101 else
10102 buf[0] = 'i';
10103 }
10104 else if (State & CMDLINE)
10105 buf[0] = 'c';
10106 else
10107 buf[0] = 'n';
10108
10109 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_string = vim_strsave(buf);
10111 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112}
10113
10114/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010115 * "nextnonblank()" function
10116 */
10117 static void
10118f_nextnonblank(argvars, rettv)
10119 typeval *argvars;
10120 typeval *rettv;
10121{
10122 linenr_T lnum;
10123
10124 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10125 {
10126 if (lnum > curbuf->b_ml.ml_line_count)
10127 {
10128 lnum = 0;
10129 break;
10130 }
10131 if (*skipwhite(ml_get(lnum)) != NUL)
10132 break;
10133 }
10134 rettv->vval.v_number = lnum;
10135}
10136
10137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 * "nr2char()" function
10139 */
10140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010142 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
10145 char_u buf[NUMBUFLEN];
10146
10147#ifdef FEAT_MBYTE
10148 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else
10151#endif
10152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 buf[1] = NUL;
10155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 rettv->v_type = VAR_STRING;
10157 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010158}
10159
10160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010161 * "prevnonblank()" function
10162 */
10163 static void
10164f_prevnonblank(argvars, rettv)
10165 typeval *argvars;
10166 typeval *rettv;
10167{
10168 linenr_T lnum;
10169
10170 lnum = get_tv_lnum(argvars);
10171 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10172 lnum = 0;
10173 else
10174 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10175 --lnum;
10176 rettv->vval.v_number = lnum;
10177}
10178
Bram Moolenaar8c711452005-01-14 21:53:12 +000010179/*
10180 * "range()" function
10181 */
10182 static void
10183f_range(argvars, rettv)
10184 typeval *argvars;
10185 typeval *rettv;
10186{
10187 long start;
10188 long end;
10189 long stride = 1;
10190 long i;
10191 listvar *l;
10192 listitem *li;
10193
10194 start = get_tv_number(&argvars[0]);
10195 if (argvars[1].v_type == VAR_UNKNOWN)
10196 {
10197 end = start - 1;
10198 start = 0;
10199 }
10200 else
10201 {
10202 end = get_tv_number(&argvars[1]);
10203 if (argvars[2].v_type != VAR_UNKNOWN)
10204 stride = get_tv_number(&argvars[2]);
10205 }
10206
10207 rettv->vval.v_number = 0;
10208 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010209 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010210 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010211 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010212 else
10213 {
10214 l = list_alloc();
10215 if (l != NULL)
10216 {
10217 rettv->v_type = VAR_LIST;
10218 rettv->vval.v_list = l;
10219 ++l->lv_refcount;
10220
10221 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10222 {
10223 li = listitem_alloc();
10224 if (li == NULL)
10225 break;
10226 li->li_tv.v_type = VAR_NUMBER;
10227 li->li_tv.vval.v_number = i;
10228 list_append(l, li);
10229 }
10230 }
10231 }
10232}
10233
Bram Moolenaar0d660222005-01-07 21:51:51 +000010234#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10235static void make_connection __ARGS((void));
10236static int check_connection __ARGS((void));
10237
10238 static void
10239make_connection()
10240{
10241 if (X_DISPLAY == NULL
10242# ifdef FEAT_GUI
10243 && !gui.in_use
10244# endif
10245 )
10246 {
10247 x_force_connect = TRUE;
10248 setup_term_clip();
10249 x_force_connect = FALSE;
10250 }
10251}
10252
10253 static int
10254check_connection()
10255{
10256 make_connection();
10257 if (X_DISPLAY == NULL)
10258 {
10259 EMSG(_("E240: No connection to Vim server"));
10260 return FAIL;
10261 }
10262 return OK;
10263}
10264#endif
10265
10266#ifdef FEAT_CLIENTSERVER
10267static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
10268
10269 static void
10270remote_common(argvars, rettv, expr)
10271 typeval *argvars;
10272 typeval *rettv;
10273 int expr;
10274{
10275 char_u *server_name;
10276 char_u *keys;
10277 char_u *r = NULL;
10278 char_u buf[NUMBUFLEN];
10279# ifdef WIN32
10280 HWND w;
10281# else
10282 Window w;
10283# endif
10284
10285 if (check_restricted() || check_secure())
10286 return;
10287
10288# ifdef FEAT_X11
10289 if (check_connection() == FAIL)
10290 return;
10291# endif
10292
10293 server_name = get_tv_string(&argvars[0]);
10294 keys = get_tv_string_buf(&argvars[1], buf);
10295# ifdef WIN32
10296 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10297# else
10298 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10299 < 0)
10300# endif
10301 {
10302 if (r != NULL)
10303 EMSG(r); /* sending worked but evaluation failed */
10304 else
10305 EMSG2(_("E241: Unable to send to %s"), server_name);
10306 return;
10307 }
10308
10309 rettv->vval.v_string = r;
10310
10311 if (argvars[2].v_type != VAR_UNKNOWN)
10312 {
10313 var v;
10314 char_u str[30];
10315
10316 sprintf((char *)str, "0x%x", (unsigned int)w);
10317 v.tv.v_type = VAR_STRING;
10318 v.tv.vval.v_string = vim_strsave(str);
10319 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
10320 vim_free(v.tv.vval.v_string);
10321 }
10322}
10323#endif
10324
10325/*
10326 * "remote_expr()" function
10327 */
10328/*ARGSUSED*/
10329 static void
10330f_remote_expr(argvars, rettv)
10331 typeval *argvars;
10332 typeval *rettv;
10333{
10334 rettv->v_type = VAR_STRING;
10335 rettv->vval.v_string = NULL;
10336#ifdef FEAT_CLIENTSERVER
10337 remote_common(argvars, rettv, TRUE);
10338#endif
10339}
10340
10341/*
10342 * "remote_foreground()" function
10343 */
10344/*ARGSUSED*/
10345 static void
10346f_remote_foreground(argvars, rettv)
10347 typeval *argvars;
10348 typeval *rettv;
10349{
10350 rettv->vval.v_number = 0;
10351#ifdef FEAT_CLIENTSERVER
10352# ifdef WIN32
10353 /* On Win32 it's done in this application. */
10354 serverForeground(get_tv_string(&argvars[0]));
10355# else
10356 /* Send a foreground() expression to the server. */
10357 argvars[1].v_type = VAR_STRING;
10358 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10359 argvars[2].v_type = VAR_UNKNOWN;
10360 remote_common(argvars, rettv, TRUE);
10361 vim_free(argvars[1].vval.v_string);
10362# endif
10363#endif
10364}
10365
10366/*ARGSUSED*/
10367 static void
10368f_remote_peek(argvars, rettv)
10369 typeval *argvars;
10370 typeval *rettv;
10371{
10372#ifdef FEAT_CLIENTSERVER
10373 var v;
10374 char_u *s = NULL;
10375# ifdef WIN32
10376 int n = 0;
10377# endif
10378
10379 if (check_restricted() || check_secure())
10380 {
10381 rettv->vval.v_number = -1;
10382 return;
10383 }
10384# ifdef WIN32
10385 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10386 if (n == 0)
10387 rettv->vval.v_number = -1;
10388 else
10389 {
10390 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10391 rettv->vval.v_number = (s != NULL);
10392 }
10393# else
10394 rettv->vval.v_number = 0;
10395 if (check_connection() == FAIL)
10396 return;
10397
10398 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10399 serverStrToWin(get_tv_string(&argvars[0])), &s);
10400# endif
10401
10402 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10403 {
10404 v.tv.v_type = VAR_STRING;
10405 v.tv.vval.v_string = vim_strsave(s);
10406 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
10407 vim_free(v.tv.vval.v_string);
10408 }
10409#else
10410 rettv->vval.v_number = -1;
10411#endif
10412}
10413
10414/*ARGSUSED*/
10415 static void
10416f_remote_read(argvars, rettv)
10417 typeval *argvars;
10418 typeval *rettv;
10419{
10420 char_u *r = NULL;
10421
10422#ifdef FEAT_CLIENTSERVER
10423 if (!check_restricted() && !check_secure())
10424 {
10425# ifdef WIN32
10426 /* The server's HWND is encoded in the 'id' parameter */
10427 int n = 0;
10428
10429 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10430 if (n != 0)
10431 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10432 if (r == NULL)
10433# else
10434 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10435 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10436# endif
10437 EMSG(_("E277: Unable to read a server reply"));
10438 }
10439#endif
10440 rettv->v_type = VAR_STRING;
10441 rettv->vval.v_string = r;
10442}
10443
10444/*
10445 * "remote_send()" function
10446 */
10447/*ARGSUSED*/
10448 static void
10449f_remote_send(argvars, rettv)
10450 typeval *argvars;
10451 typeval *rettv;
10452{
10453 rettv->v_type = VAR_STRING;
10454 rettv->vval.v_string = NULL;
10455#ifdef FEAT_CLIENTSERVER
10456 remote_common(argvars, rettv, FALSE);
10457#endif
10458}
10459
10460/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010461 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010462 */
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010465 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010467{
10468 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010469 listitem *item, *item2;
10470 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010471 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010472 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010473 char_u *key;
10474 dictvar *d;
10475 dictitem *di, **pdi;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010476
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010477 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010478 if (argvars[0].v_type == VAR_DICT)
10479 {
10480 if (argvars[2].v_type != VAR_UNKNOWN)
10481 EMSG2(_(e_toomanyarg), "remove()");
10482 else if ((d = argvars[0].vval.v_dict) != NULL)
10483 {
10484 key = get_tv_string(&argvars[1]);
10485 pdi = &d->dv_first;
10486 for (di = d->dv_first; di != NULL; pdi = &di->di_next, di = *pdi)
10487 if (STRCMP(di->di_key, key) == 0)
10488 {
10489 *pdi = di->di_next;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010490 *rettv = di->di_tv;
10491 init_tv(&di->di_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010492 dictitem_free(di);
10493 break;
10494 }
10495 if (di == NULL)
10496 EMSG2(_(e_dictkey), key);
10497 }
10498 }
10499 else if (argvars[0].v_type != VAR_LIST)
10500 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010501 else if ((l = argvars[0].vval.v_list) != NULL)
10502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010504 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010505 if (item == NULL)
10506 EMSGN(_(e_listidx), idx);
10507 else
10508 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010509 if (argvars[2].v_type == VAR_UNKNOWN)
10510 {
10511 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010512 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010513 *rettv = item->li_tv;
10514 vim_free(item);
10515 }
10516 else
10517 {
10518 /* Remove range of items, return list with values. */
10519 end = get_tv_number(&argvars[2]);
10520 item2 = list_find(l, end);
10521 if (item2 == NULL)
10522 EMSGN(_(e_listidx), end);
10523 else
10524 {
10525 for (li = item; li != item2 && li != NULL; li = li->li_next)
10526 ;
10527 if (li == NULL) /* didn't find "item2" after "item" */
10528 EMSG(_(e_invrange));
10529 else
10530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010531 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010532 l = list_alloc();
10533 if (l != NULL)
10534 {
10535 rettv->v_type = VAR_LIST;
10536 rettv->vval.v_list = l;
10537 l->lv_first = item;
10538 l->lv_last = item2;
10539 l->lv_refcount = 1;
10540 item->li_prev = NULL;
10541 item2->li_next = NULL;
10542 }
10543 }
10544 }
10545 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010546 }
10547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548}
10549
10550/*
10551 * "rename({from}, {to})" function
10552 */
10553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010555 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557{
10558 char_u buf[NUMBUFLEN];
10559
10560 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10564 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565}
10566
10567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010568 * "repeat()" function
10569 */
10570/*ARGSUSED*/
10571 static void
10572f_repeat(argvars, rettv)
10573 typeval *argvars;
10574 typeval *rettv;
10575{
10576 char_u *p;
10577 int n;
10578 int slen;
10579 int len;
10580 char_u *r;
10581 int i;
10582 listvar *l;
10583
10584 n = get_tv_number(&argvars[1]);
10585 if (argvars[0].v_type == VAR_LIST)
10586 {
10587 l = list_alloc();
10588 if (l != NULL && argvars[0].vval.v_list != NULL)
10589 {
10590 l->lv_refcount = 1;
10591 while (n-- > 0)
10592 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
10593 break;
10594 }
10595 rettv->v_type = VAR_LIST;
10596 rettv->vval.v_list = l;
10597 }
10598 else
10599 {
10600 p = get_tv_string(&argvars[0]);
10601 rettv->v_type = VAR_STRING;
10602 rettv->vval.v_string = NULL;
10603
10604 slen = (int)STRLEN(p);
10605 len = slen * n;
10606 if (len <= 0)
10607 return;
10608
10609 r = alloc(len + 1);
10610 if (r != NULL)
10611 {
10612 for (i = 0; i < n; i++)
10613 mch_memmove(r + i * slen, p, (size_t)slen);
10614 r[len] = NUL;
10615 }
10616
10617 rettv->vval.v_string = r;
10618 }
10619}
10620
10621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 * "resolve()" function
10623 */
10624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010625f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010626 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628{
10629 char_u *p;
10630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632#ifdef FEAT_SHORTCUT
10633 {
10634 char_u *v = NULL;
10635
10636 v = mch_resolve_shortcut(p);
10637 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 }
10642#else
10643# ifdef HAVE_READLINK
10644 {
10645 char_u buf[MAXPATHL + 1];
10646 char_u *cpy;
10647 int len;
10648 char_u *remain = NULL;
10649 char_u *q;
10650 int is_relative_to_current = FALSE;
10651 int has_trailing_pathsep = FALSE;
10652 int limit = 100;
10653
10654 p = vim_strsave(p);
10655
10656 if (p[0] == '.' && (vim_ispathsep(p[1])
10657 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10658 is_relative_to_current = TRUE;
10659
10660 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010661 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 has_trailing_pathsep = TRUE;
10663
10664 q = getnextcomp(p);
10665 if (*q != NUL)
10666 {
10667 /* Separate the first path component in "p", and keep the
10668 * remainder (beginning with the path separator). */
10669 remain = vim_strsave(q - 1);
10670 q[-1] = NUL;
10671 }
10672
10673 for (;;)
10674 {
10675 for (;;)
10676 {
10677 len = readlink((char *)p, (char *)buf, MAXPATHL);
10678 if (len <= 0)
10679 break;
10680 buf[len] = NUL;
10681
10682 if (limit-- == 0)
10683 {
10684 vim_free(p);
10685 vim_free(remain);
10686 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 goto fail;
10689 }
10690
10691 /* Ensure that the result will have a trailing path separator
10692 * if the argument has one. */
10693 if (remain == NULL && has_trailing_pathsep)
10694 add_pathsep(buf);
10695
10696 /* Separate the first path component in the link value and
10697 * concatenate the remainders. */
10698 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10699 if (*q != NUL)
10700 {
10701 if (remain == NULL)
10702 remain = vim_strsave(q - 1);
10703 else
10704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010705 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 if (cpy != NULL)
10707 {
10708 STRCAT(cpy, remain);
10709 vim_free(remain);
10710 remain = cpy;
10711 }
10712 }
10713 q[-1] = NUL;
10714 }
10715
10716 q = gettail(p);
10717 if (q > p && *q == NUL)
10718 {
10719 /* Ignore trailing path separator. */
10720 q[-1] = NUL;
10721 q = gettail(p);
10722 }
10723 if (q > p && !mch_isFullName(buf))
10724 {
10725 /* symlink is relative to directory of argument */
10726 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10727 if (cpy != NULL)
10728 {
10729 STRCPY(cpy, p);
10730 STRCPY(gettail(cpy), buf);
10731 vim_free(p);
10732 p = cpy;
10733 }
10734 }
10735 else
10736 {
10737 vim_free(p);
10738 p = vim_strsave(buf);
10739 }
10740 }
10741
10742 if (remain == NULL)
10743 break;
10744
10745 /* Append the first path component of "remain" to "p". */
10746 q = getnextcomp(remain + 1);
10747 len = q - remain - (*q != NUL);
10748 cpy = vim_strnsave(p, STRLEN(p) + len);
10749 if (cpy != NULL)
10750 {
10751 STRNCAT(cpy, remain, len);
10752 vim_free(p);
10753 p = cpy;
10754 }
10755 /* Shorten "remain". */
10756 if (*q != NUL)
10757 STRCPY(remain, q - 1);
10758 else
10759 {
10760 vim_free(remain);
10761 remain = NULL;
10762 }
10763 }
10764
10765 /* If the result is a relative path name, make it explicitly relative to
10766 * the current directory if and only if the argument had this form. */
10767 if (!vim_ispathsep(*p))
10768 {
10769 if (is_relative_to_current
10770 && *p != NUL
10771 && !(p[0] == '.'
10772 && (p[1] == NUL
10773 || vim_ispathsep(p[1])
10774 || (p[1] == '.'
10775 && (p[2] == NUL
10776 || vim_ispathsep(p[2]))))))
10777 {
10778 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010779 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 if (cpy != NULL)
10781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 vim_free(p);
10783 p = cpy;
10784 }
10785 }
10786 else if (!is_relative_to_current)
10787 {
10788 /* Strip leading "./". */
10789 q = p;
10790 while (q[0] == '.' && vim_ispathsep(q[1]))
10791 q += 2;
10792 if (q > p)
10793 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10794 }
10795 }
10796
10797 /* Ensure that the result will have no trailing path separator
10798 * if the argument had none. But keep "/" or "//". */
10799 if (!has_trailing_pathsep)
10800 {
10801 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010802 if (after_pathsep(p, q))
10803 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 }
10808# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810# endif
10811#endif
10812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
10815#ifdef HAVE_READLINK
10816fail:
10817#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819}
10820
10821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 */
10824 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010825f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010826 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 listvar *l;
10830 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832 rettv->vval.v_number = 0;
10833 if (argvars[0].v_type != VAR_LIST)
10834 EMSG2(_(e_listarg), "reverse()");
10835 else if ((l = argvars[0].vval.v_list) != NULL)
10836 {
10837 li = l->lv_last;
10838 l->lv_first = l->lv_last = li;
10839 while (li != NULL)
10840 {
10841 ni = li->li_prev;
10842 list_append(l, li);
10843 li = ni;
10844 }
10845 rettv->vval.v_list = l;
10846 rettv->v_type = VAR_LIST;
10847 ++l->lv_refcount;
10848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849}
10850
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010851#define SP_NOMOVE 1 /* don't move cursor */
10852#define SP_REPEAT 2 /* repeat to find outer pair */
10853#define SP_RETCOUNT 4 /* return matchcount */
10854
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855static int get_search_arg __ARGS((typeval *varp, int *flagsp));
10856
10857/*
10858 * Get flags for a search function.
10859 * Possibly sets "p_ws".
10860 * Returns BACKWARD, FORWARD or zero (for an error).
10861 */
10862 static int
10863get_search_arg(varp, flagsp)
10864 typeval *varp;
10865 int *flagsp;
10866{
10867 int dir = FORWARD;
10868 char_u *flags;
10869 char_u nbuf[NUMBUFLEN];
10870 int mask;
10871
10872 if (varp->v_type != VAR_UNKNOWN)
10873 {
10874 flags = get_tv_string_buf(varp, nbuf);
10875 while (*flags != NUL)
10876 {
10877 switch (*flags)
10878 {
10879 case 'b': dir = BACKWARD; break;
10880 case 'w': p_ws = TRUE; break;
10881 case 'W': p_ws = FALSE; break;
10882 default: mask = 0;
10883 if (flagsp != NULL)
10884 switch (*flags)
10885 {
10886 case 'n': mask = SP_NOMOVE; break;
10887 case 'r': mask = SP_REPEAT; break;
10888 case 'm': mask = SP_RETCOUNT; break;
10889 }
10890 if (mask == 0)
10891 {
10892 EMSG2(_(e_invarg2), flags);
10893 dir = 0;
10894 }
10895 else
10896 *flagsp |= mask;
10897 }
10898 if (dir == 0)
10899 break;
10900 ++flags;
10901 }
10902 }
10903 return dir;
10904}
10905
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906/*
10907 * "search()" function
10908 */
10909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010911 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
10914 char_u *pat;
10915 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010916 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 int save_p_ws = p_ws;
10918 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010919 int flags = 0;
10920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010924 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
10925 if (dir == 0)
10926 goto theend;
10927 if ((flags & ~SP_NOMOVE) != 0)
10928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010930 goto theend;
10931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010933 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
10935 SEARCH_KEEP, RE_SEARCH) != FAIL)
10936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 curwin->w_cursor = pos;
10939 /* "/$" will put the cursor after the end of the line, may need to
10940 * correct that here */
10941 check_cursor();
10942 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010943
10944 /* If 'n' flag is used: restore cursor position. */
10945 if (flags & SP_NOMOVE)
10946 curwin->w_cursor = save_cursor;
10947theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 p_ws = save_p_ws;
10949}
10950
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951/*
10952 * "searchpair()" function
10953 */
10954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958{
10959 char_u *spat, *mpat, *epat;
10960 char_u *skip;
10961 char_u *pat, *pat2, *pat3;
10962 pos_T pos;
10963 pos_T firstpos;
10964 pos_T save_cursor;
10965 pos_T save_pos;
10966 int save_p_ws = p_ws;
10967 char_u *save_cpo;
10968 int dir;
10969 int flags = 0;
10970 char_u nbuf1[NUMBUFLEN];
10971 char_u nbuf2[NUMBUFLEN];
10972 char_u nbuf3[NUMBUFLEN];
10973 int n;
10974 int r;
10975 int nest = 1;
10976 int err;
10977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
10980 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10981 save_cpo = p_cpo;
10982 p_cpo = (char_u *)"";
10983
10984 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 spat = get_tv_string(&argvars[0]);
10986 mpat = get_tv_string_buf(&argvars[1], nbuf1);
10987 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988
10989 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10990 * start/middle/end (pat3, for the top pair). */
10991 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
10992 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
10993 if (pat2 == NULL || pat3 == NULL)
10994 goto theend;
10995 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
10996 if (*mpat == NUL)
10997 STRCPY(pat3, pat2);
10998 else
10999 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11000 spat, epat, mpat);
11001
11002 /* Handle the optional fourth argument: flags */
11003 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011004 if (dir == 0)
11005 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006
11007 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011008 if (argvars[3].v_type == VAR_UNKNOWN
11009 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 skip = (char_u *)"";
11011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013
11014 save_cursor = curwin->w_cursor;
11015 pos = curwin->w_cursor;
11016 firstpos.lnum = 0;
11017 pat = pat3;
11018 for (;;)
11019 {
11020 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11021 SEARCH_KEEP, RE_SEARCH);
11022 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11023 /* didn't find it or found the first match again: FAIL */
11024 break;
11025
11026 if (firstpos.lnum == 0)
11027 firstpos = pos;
11028
11029 /* If the skip pattern matches, ignore this match. */
11030 if (*skip != NUL)
11031 {
11032 save_pos = curwin->w_cursor;
11033 curwin->w_cursor = pos;
11034 r = eval_to_bool(skip, &err, NULL, FALSE);
11035 curwin->w_cursor = save_pos;
11036 if (err)
11037 {
11038 /* Evaluating {skip} caused an error, break here. */
11039 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 break;
11042 }
11043 if (r)
11044 continue;
11045 }
11046
11047 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11048 {
11049 /* Found end when searching backwards or start when searching
11050 * forward: nested pair. */
11051 ++nest;
11052 pat = pat2; /* nested, don't search for middle */
11053 }
11054 else
11055 {
11056 /* Found end when searching forward or start when searching
11057 * backward: end of (nested) pair; or found middle in outer pair. */
11058 if (--nest == 1)
11059 pat = pat3; /* outer level, search for middle */
11060 }
11061
11062 if (nest == 0)
11063 {
11064 /* Found the match: return matchcount or line number. */
11065 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 curwin->w_cursor = pos;
11070 if (!(flags & SP_REPEAT))
11071 break;
11072 nest = 1; /* search for next unmatched */
11073 }
11074 }
11075
11076 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 curwin->w_cursor = save_cursor;
11079
11080theend:
11081 vim_free(pat2);
11082 vim_free(pat3);
11083 p_ws = save_p_ws;
11084 p_cpo = save_cpo;
11085}
11086
Bram Moolenaar0d660222005-01-07 21:51:51 +000011087/*ARGSUSED*/
11088 static void
11089f_server2client(argvars, rettv)
11090 typeval *argvars;
11091 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011093#ifdef FEAT_CLIENTSERVER
11094 char_u buf[NUMBUFLEN];
11095 char_u *server = get_tv_string(&argvars[0]);
11096 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaar0d660222005-01-07 21:51:51 +000011098 rettv->vval.v_number = -1;
11099 if (check_restricted() || check_secure())
11100 return;
11101# ifdef FEAT_X11
11102 if (check_connection() == FAIL)
11103 return;
11104# endif
11105
11106 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011108 EMSG(_("E258: Unable to send to client"));
11109 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011111 rettv->vval.v_number = 0;
11112#else
11113 rettv->vval.v_number = -1;
11114#endif
11115}
11116
11117/*ARGSUSED*/
11118 static void
11119f_serverlist(argvars, rettv)
11120 typeval *argvars;
11121 typeval *rettv;
11122{
11123 char_u *r = NULL;
11124
11125#ifdef FEAT_CLIENTSERVER
11126# ifdef WIN32
11127 r = serverGetVimNames();
11128# else
11129 make_connection();
11130 if (X_DISPLAY != NULL)
11131 r = serverGetVimNames(X_DISPLAY);
11132# endif
11133#endif
11134 rettv->v_type = VAR_STRING;
11135 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
11139 * "setbufvar()" function
11140 */
11141/*ARGSUSED*/
11142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011144 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
11147 buf_T *buf;
11148#ifdef FEAT_AUTOCMD
11149 aco_save_T aco;
11150#else
11151 buf_T *save_curbuf;
11152#endif
11153 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011154 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 char_u nbuf[NUMBUFLEN];
11156
11157 if (check_restricted() || check_secure())
11158 return;
11159 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 buf = get_buf_tv(&argvars[0]);
11161 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 varp = &argvars[2];
11163
11164 if (buf != NULL && varname != NULL && varp != NULL)
11165 {
11166 /* set curbuf to be our buf, temporarily */
11167#ifdef FEAT_AUTOCMD
11168 aucmd_prepbuf(&aco, buf);
11169#else
11170 save_curbuf = curbuf;
11171 curbuf = buf;
11172#endif
11173
11174 if (*varname == '&')
11175 {
11176 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 set_option_value(varname, get_tv_number(varp),
11178 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 }
11180 else
11181 {
11182 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11183 if (bufvarname != NULL)
11184 {
11185 STRCPY(bufvarname, "b:");
11186 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011187 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 vim_free(bufvarname);
11189 }
11190 }
11191
11192 /* reset notion of buffer */
11193#ifdef FEAT_AUTOCMD
11194 aucmd_restbuf(&aco);
11195#else
11196 curbuf = save_curbuf;
11197#endif
11198 }
11199 --emsg_off;
11200}
11201
11202/*
11203 * "setcmdpos()" function
11204 */
11205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011207 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = set_cmdline_pos(
11211 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212}
11213
11214/*
11215 * "setline()" function
11216 */
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011219 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 linenr_T lnum;
11223 char_u *line;
11224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 lnum = get_tv_lnum(argvars);
11226 line = get_tv_string(&argvars[1]);
11227 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228
11229 if (lnum >= 1
11230 && lnum <= curbuf->b_ml.ml_line_count
11231 && u_savesub(lnum) == OK
11232 && ml_replace(lnum, line, TRUE) == OK)
11233 {
11234 changed_bytes(lnum, 0);
11235 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238}
11239
11240/*
11241 * "setreg()" function
11242 */
11243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011245 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
11248 int regname;
11249 char_u *strregname;
11250 char_u *stropt;
11251 int append;
11252 char_u yank_type;
11253 long block_len;
11254
11255 block_len = -1;
11256 yank_type = MAUTO;
11257 append = FALSE;
11258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 strregname = get_tv_string(argvars);
11260 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
11262 regname = (strregname == NULL ? '"' : *strregname);
11263 if (regname == 0 || regname == '@')
11264 regname = '"';
11265 else if (regname == '=')
11266 return;
11267
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 switch (*stropt)
11272 {
11273 case 'a': case 'A': /* append */
11274 append = TRUE;
11275 break;
11276 case 'v': case 'c': /* character-wise selection */
11277 yank_type = MCHAR;
11278 break;
11279 case 'V': case 'l': /* line-wise selection */
11280 yank_type = MLINE;
11281 break;
11282#ifdef FEAT_VISUAL
11283 case 'b': case Ctrl_V: /* block-wise selection */
11284 yank_type = MBLOCK;
11285 if (VIM_ISDIGIT(stropt[1]))
11286 {
11287 ++stropt;
11288 block_len = getdigits(&stropt) - 1;
11289 --stropt;
11290 }
11291 break;
11292#endif
11293 }
11294 }
11295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301
11302/*
11303 * "setwinvar(expr)" function
11304 */
11305/*ARGSUSED*/
11306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
11311 win_T *win;
11312#ifdef FEAT_WINDOWS
11313 win_T *save_curwin;
11314#endif
11315 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011316 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 char_u nbuf[NUMBUFLEN];
11318
11319 if (check_restricted() || check_secure())
11320 return;
11321 ++emsg_off;
11322 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 varp = &argvars[2];
11325
11326 if (win != NULL && varname != NULL && varp != NULL)
11327 {
11328#ifdef FEAT_WINDOWS
11329 /* set curwin to be our win, temporarily */
11330 save_curwin = curwin;
11331 curwin = win;
11332 curbuf = curwin->w_buffer;
11333#endif
11334
11335 if (*varname == '&')
11336 {
11337 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 set_option_value(varname, get_tv_number(varp),
11339 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 }
11341 else
11342 {
11343 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11344 if (winvarname != NULL)
11345 {
11346 STRCPY(winvarname, "w:");
11347 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011348 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 vim_free(winvarname);
11350 }
11351 }
11352
11353#ifdef FEAT_WINDOWS
11354 /* Restore current window, if it's still valid (autocomands can make
11355 * it invalid). */
11356 if (win_valid(save_curwin))
11357 {
11358 curwin = save_curwin;
11359 curbuf = curwin->w_buffer;
11360 }
11361#endif
11362 }
11363 --emsg_off;
11364}
11365
11366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011367 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 */
11369 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011371 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011374 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376 p = get_tv_string(&argvars[0]);
11377 rettv->vval.v_string = vim_strsave(p);
11378 simplify_filename(rettv->vval.v_string); /* simplify in place */
11379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380}
11381
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382static int
11383#ifdef __BORLANDC__
11384 _RTLENTRYF
11385#endif
11386 item_compare __ARGS((const void *s1, const void *s2));
11387static int
11388#ifdef __BORLANDC__
11389 _RTLENTRYF
11390#endif
11391 item_compare2 __ARGS((const void *s1, const void *s2));
11392
11393static int item_compare_ic;
11394static char_u *item_compare_func;
11395#define ITEM_COMPARE_FAIL 999
11396
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011398 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011400 static int
11401#ifdef __BORLANDC__
11402_RTLENTRYF
11403#endif
11404item_compare(s1, s2)
11405 const void *s1;
11406 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011408 char_u *p1, *p2;
11409 char_u *tofree1, *tofree2;
11410 int res;
11411 char_u numbuf1[NUMBUFLEN];
11412 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413
Bram Moolenaar0d660222005-01-07 21:51:51 +000011414 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
11415 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
11416 if (item_compare_ic)
11417 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419 res = STRCMP(p1, p2);
11420 vim_free(tofree1);
11421 vim_free(tofree2);
11422 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423}
11424
11425 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011426#ifdef __BORLANDC__
11427_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011429item_compare2(s1, s2)
11430 const void *s1;
11431 const void *s2;
11432{
11433 int res;
11434 typeval rettv;
11435 typeval argv[2];
11436 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
Bram Moolenaar0d660222005-01-07 21:51:51 +000011438 /* copy the values (is this really needed?) */
11439 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
11440 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
11441
11442 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11443 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011444 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011445 clear_tv(&argv[0]);
11446 clear_tv(&argv[1]);
11447
11448 if (res == FAIL)
11449 res = ITEM_COMPARE_FAIL;
11450 else
11451 res = get_tv_number(&rettv);
11452 clear_tv(&rettv);
11453 return res;
11454}
11455
11456/*
11457 * "sort({list})" function
11458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011460f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011461 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011464 listvar *l;
11465 listitem *li;
11466 listitem **ptrs;
11467 long len;
11468 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469
Bram Moolenaar0d660222005-01-07 21:51:51 +000011470 rettv->vval.v_number = 0;
11471 if (argvars[0].v_type != VAR_LIST)
11472 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 else
11474 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011475 l = argvars[0].vval.v_list;
11476 if (l == NULL)
11477 return;
11478 rettv->vval.v_list = l;
11479 rettv->v_type = VAR_LIST;
11480 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
Bram Moolenaar0d660222005-01-07 21:51:51 +000011482 len = list_len(l);
11483 if (len <= 1)
11484 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485
Bram Moolenaar0d660222005-01-07 21:51:51 +000011486 item_compare_ic = FALSE;
11487 item_compare_func = NULL;
11488 if (argvars[1].v_type != VAR_UNKNOWN)
11489 {
11490 if (argvars[1].v_type == VAR_FUNC)
11491 item_compare_func = argvars[0].vval.v_string;
11492 else
11493 {
11494 i = get_tv_number(&argvars[1]);
11495 if (i == 1)
11496 item_compare_ic = TRUE;
11497 else
11498 item_compare_func = get_tv_string(&argvars[1]);
11499 }
11500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502 /* Make an array with each entry pointing to an item in the List. */
11503 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
11504 if (ptrs == NULL)
11505 return;
11506 i = 0;
11507 for (li = l->lv_first; li != NULL; li = li->li_next)
11508 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509
Bram Moolenaar0d660222005-01-07 21:51:51 +000011510 /* test the compare function */
11511 if (item_compare_func != NULL
11512 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11513 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011514 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011516 {
11517 /* Sort the array with item pointers. */
11518 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
11519 item_compare_func == NULL ? item_compare : item_compare2);
11520
11521 /* Clear the List and append the items in the sorted order. */
11522 l->lv_first = l->lv_last = NULL;
11523 for (i = 0; i < len; ++i)
11524 list_append(l, ptrs[i]);
11525 }
11526
11527 vim_free(ptrs);
11528 }
11529}
11530
11531 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533 typeval *argvars;
11534 typeval *rettv;
11535{
11536 char_u *str;
11537 char_u *end;
11538 char_u *pat;
11539 regmatch_T regmatch;
11540 char_u patbuf[NUMBUFLEN];
11541 char_u *save_cpo;
11542 int match;
11543 listitem *ni;
11544 listvar *l;
11545 colnr_T col = 0;
11546
11547 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11548 save_cpo = p_cpo;
11549 p_cpo = (char_u *)"";
11550
11551 str = get_tv_string(&argvars[0]);
11552 if (argvars[1].v_type == VAR_UNKNOWN)
11553 pat = (char_u *)"[\\x01- ]\\+";
11554 else
11555 pat = get_tv_string_buf(&argvars[1], patbuf);
11556
11557 l = list_alloc();
11558 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011560 rettv->v_type = VAR_LIST;
11561 rettv->vval.v_list = l;
11562 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563
Bram Moolenaar0d660222005-01-07 21:51:51 +000011564 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11565 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567 regmatch.rm_ic = FALSE;
11568 while (*str != NUL)
11569 {
11570 match = vim_regexec_nl(&regmatch, str, col);
11571 if (match)
11572 end = regmatch.startp[0];
11573 else
11574 end = str + STRLEN(str);
11575 if (end > str)
11576 {
11577 ni = listitem_alloc();
11578 if (ni == NULL)
11579 break;
11580 ni->li_tv.v_type = VAR_STRING;
11581 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
11582 list_append(l, ni);
11583 }
11584 if (!match)
11585 break;
11586 /* Advance to just after the match. */
11587 if (regmatch.endp[0] > str)
11588 col = 0;
11589 else
11590 {
11591 /* Don't get stuck at the same match. */
11592#ifdef FEAT_MBYTE
11593 col = mb_ptr2len_check(regmatch.endp[0]);
11594#else
11595 col = 1;
11596#endif
11597 }
11598 str = regmatch.endp[0];
11599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600
Bram Moolenaar0d660222005-01-07 21:51:51 +000011601 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603
Bram Moolenaar0d660222005-01-07 21:51:51 +000011604 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605}
11606
11607#ifdef HAVE_STRFTIME
11608/*
11609 * "strftime({format}[, {time}])" function
11610 */
11611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011613 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615{
11616 char_u result_buf[256];
11617 struct tm *curtime;
11618 time_t seconds;
11619 char_u *p;
11620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011624 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 seconds = time(NULL);
11626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 curtime = localtime(&seconds);
11629 /* MSVC returns NULL for an invalid value of seconds. */
11630 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 else
11633 {
11634# ifdef FEAT_MBYTE
11635 vimconv_T conv;
11636 char_u *enc;
11637
11638 conv.vc_type = CONV_NONE;
11639 enc = enc_locale();
11640 convert_setup(&conv, p_enc, enc);
11641 if (conv.vc_type != CONV_NONE)
11642 p = string_convert(&conv, p, NULL);
11643# endif
11644 if (p != NULL)
11645 (void)strftime((char *)result_buf, sizeof(result_buf),
11646 (char *)p, curtime);
11647 else
11648 result_buf[0] = NUL;
11649
11650# ifdef FEAT_MBYTE
11651 if (conv.vc_type != CONV_NONE)
11652 vim_free(p);
11653 convert_setup(&conv, enc, p_enc);
11654 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 else
11657# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659
11660# ifdef FEAT_MBYTE
11661 /* Release conversion descriptors */
11662 convert_setup(&conv, NULL, NULL);
11663 vim_free(enc);
11664# endif
11665 }
11666}
11667#endif
11668
11669/*
11670 * "stridx()" function
11671 */
11672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011674 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
11677 char_u buf[NUMBUFLEN];
11678 char_u *needle;
11679 char_u *haystack;
11680 char_u *pos;
11681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 needle = get_tv_string(&argvars[1]);
11683 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11685
11686 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690}
11691
11692/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693 * "string()" function
11694 */
11695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011697 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011699{
11700 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011701 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011704 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011705 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708
11709/*
11710 * "strlen()" function
11711 */
11712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011714 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717 rettv->vval.v_number = (varnumber_T)(STRLEN(
11718 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719}
11720
11721/*
11722 * "strpart()" function
11723 */
11724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011726 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
11729 char_u *p;
11730 int n;
11731 int len;
11732 int slen;
11733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 slen = (int)STRLEN(p);
11736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011738 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 else
11741 len = slen - n; /* default len: all bytes that are available. */
11742
11743 /*
11744 * Only return the overlap between the specified part and the actual
11745 * string.
11746 */
11747 if (n < 0)
11748 {
11749 len += n;
11750 n = 0;
11751 }
11752 else if (n > slen)
11753 n = slen;
11754 if (len < 0)
11755 len = 0;
11756 else if (n + len > slen)
11757 len = slen - n;
11758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 rettv->v_type = VAR_STRING;
11760 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761}
11762
11763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011764 * "strridx()" function
11765 */
11766 static void
11767f_strridx(argvars, rettv)
11768 typeval *argvars;
11769 typeval *rettv;
11770{
11771 char_u buf[NUMBUFLEN];
11772 char_u *needle;
11773 char_u *haystack;
11774 char_u *rest;
11775 char_u *lastmatch = NULL;
11776
11777 needle = get_tv_string(&argvars[1]);
11778 haystack = get_tv_string_buf(&argvars[0], buf);
11779 if (*needle == NUL)
11780 /* Empty string matches past the end. */
11781 lastmatch = haystack + STRLEN(haystack);
11782 else
11783 for (rest = haystack; *rest != '\0'; ++rest)
11784 {
11785 rest = (char_u *)strstr((char *)rest, (char *)needle);
11786 if (rest == NULL)
11787 break;
11788 lastmatch = rest;
11789 }
11790
11791 if (lastmatch == NULL)
11792 rettv->vval.v_number = -1;
11793 else
11794 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11795}
11796
11797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 * "strtrans()" function
11799 */
11800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011802 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->v_type = VAR_STRING;
11806 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807}
11808
11809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011810 * "submatch()" function
11811 */
11812 static void
11813f_submatch(argvars, rettv)
11814 typeval *argvars;
11815 typeval *rettv;
11816{
11817 rettv->v_type = VAR_STRING;
11818 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11819}
11820
11821/*
11822 * "substitute()" function
11823 */
11824 static void
11825f_substitute(argvars, rettv)
11826 typeval *argvars;
11827 typeval *rettv;
11828{
11829 char_u patbuf[NUMBUFLEN];
11830 char_u subbuf[NUMBUFLEN];
11831 char_u flagsbuf[NUMBUFLEN];
11832
11833 rettv->v_type = VAR_STRING;
11834 rettv->vval.v_string = do_string_sub(
11835 get_tv_string(&argvars[0]),
11836 get_tv_string_buf(&argvars[1], patbuf),
11837 get_tv_string_buf(&argvars[2], subbuf),
11838 get_tv_string_buf(&argvars[3], flagsbuf));
11839}
11840
11841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 * "synID(line, col, trans)" function
11843 */
11844/*ARGSUSED*/
11845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
11850 int id = 0;
11851#ifdef FEAT_SYN_HL
11852 long line;
11853 long col;
11854 int trans;
11855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 line = get_tv_lnum(argvars);
11857 col = get_tv_number(&argvars[1]) - 1;
11858 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859
11860 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
11861 && col >= 0 && col < (long)STRLEN(ml_get(line)))
11862 id = syn_get_id(line, col, trans);
11863#endif
11864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
11869 * "synIDattr(id, what [, mode])" function
11870 */
11871/*ARGSUSED*/
11872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
11877 char_u *p = NULL;
11878#ifdef FEAT_SYN_HL
11879 int id;
11880 char_u *what;
11881 char_u *mode;
11882 char_u modebuf[NUMBUFLEN];
11883 int modec;
11884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 id = get_tv_number(&argvars[0]);
11886 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 modec = TOLOWER_ASC(mode[0]);
11891 if (modec != 't' && modec != 'c'
11892#ifdef FEAT_GUI
11893 && modec != 'g'
11894#endif
11895 )
11896 modec = 0; /* replace invalid with current */
11897 }
11898 else
11899 {
11900#ifdef FEAT_GUI
11901 if (gui.in_use)
11902 modec = 'g';
11903 else
11904#endif
11905 if (t_colors > 1)
11906 modec = 'c';
11907 else
11908 modec = 't';
11909 }
11910
11911
11912 switch (TOLOWER_ASC(what[0]))
11913 {
11914 case 'b':
11915 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11916 p = highlight_color(id, what, modec);
11917 else /* bold */
11918 p = highlight_has_attr(id, HL_BOLD, modec);
11919 break;
11920
11921 case 'f': /* fg[#] */
11922 p = highlight_color(id, what, modec);
11923 break;
11924
11925 case 'i':
11926 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11927 p = highlight_has_attr(id, HL_INVERSE, modec);
11928 else /* italic */
11929 p = highlight_has_attr(id, HL_ITALIC, modec);
11930 break;
11931
11932 case 'n': /* name */
11933 p = get_highlight_name(NULL, id - 1);
11934 break;
11935
11936 case 'r': /* reverse */
11937 p = highlight_has_attr(id, HL_INVERSE, modec);
11938 break;
11939
11940 case 's': /* standout */
11941 p = highlight_has_attr(id, HL_STANDOUT, modec);
11942 break;
11943
11944 case 'u': /* underline */
11945 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11946 break;
11947 }
11948
11949 if (p != NULL)
11950 p = vim_strsave(p);
11951#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 rettv->v_type = VAR_STRING;
11953 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954}
11955
11956/*
11957 * "synIDtrans(id)" function
11958 */
11959/*ARGSUSED*/
11960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011962 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964{
11965 int id;
11966
11967#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969
11970 if (id > 0)
11971 id = syn_get_final_id(id);
11972 else
11973#endif
11974 id = 0;
11975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
11980 * "system()" function
11981 */
11982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011984 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011987 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011989 char_u *infile = NULL;
11990 char_u buf[NUMBUFLEN];
11991 int err = FALSE;
11992 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011995 {
11996 /*
11997 * Write the string to a temp file, to be used for input of the shell
11998 * command.
11999 */
12000 if ((infile = vim_tempname('i')) == NULL)
12001 {
12002 EMSG(_(e_notmp));
12003 return;
12004 }
12005
12006 fd = mch_fopen((char *)infile, WRITEBIN);
12007 if (fd == NULL)
12008 {
12009 EMSG2(_(e_notopen), infile);
12010 goto done;
12011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012013 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12014 err = TRUE;
12015 if (fclose(fd) != 0)
12016 err = TRUE;
12017 if (err)
12018 {
12019 EMSG(_("E677: Error writing temp file"));
12020 goto done;
12021 }
12022 }
12023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012025
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026#ifdef USE_CR
12027 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012028 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 {
12030 char_u *s;
12031
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012032 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 {
12034 if (*s == CAR)
12035 *s = NL;
12036 }
12037 }
12038#else
12039# ifdef USE_CRNL
12040 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012041 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 {
12043 char_u *s, *d;
12044
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012045 d = res;
12046 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 {
12048 if (s[0] == CAR && s[1] == NL)
12049 ++s;
12050 *d++ = *s;
12051 }
12052 *d = NUL;
12053 }
12054# endif
12055#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012056
12057done:
12058 if (infile != NULL)
12059 {
12060 mch_remove(infile);
12061 vim_free(infile);
12062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->v_type = VAR_STRING;
12064 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065}
12066
12067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 * "tempname()" function
12069 */
12070/*ARGSUSED*/
12071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012073 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076 static int x = 'A';
12077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 rettv->v_type = VAR_STRING;
12079 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080
12081 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12082 * names. Skip 'I' and 'O', they are used for shell redirection. */
12083 do
12084 {
12085 if (x == 'Z')
12086 x = '0';
12087 else if (x == '9')
12088 x = 'A';
12089 else
12090 {
12091#ifdef EBCDIC
12092 if (x == 'I')
12093 x = 'J';
12094 else if (x == 'R')
12095 x = 'S';
12096 else
12097#endif
12098 ++x;
12099 }
12100 } while (x == 'I' || x == 'O');
12101}
12102
12103/*
12104 * "tolower(string)" function
12105 */
12106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012108 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110{
12111 char_u *p;
12112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 p = vim_strsave(get_tv_string(&argvars[0]));
12114 rettv->v_type = VAR_STRING;
12115 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116
12117 if (p != NULL)
12118 while (*p != NUL)
12119 {
12120#ifdef FEAT_MBYTE
12121 int l;
12122
12123 if (enc_utf8)
12124 {
12125 int c, lc;
12126
12127 c = utf_ptr2char(p);
12128 lc = utf_tolower(c);
12129 l = utf_ptr2len_check(p);
12130 /* TODO: reallocate string when byte count changes. */
12131 if (utf_char2len(lc) == l)
12132 utf_char2bytes(lc, p);
12133 p += l;
12134 }
12135 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12136 p += l; /* skip multi-byte character */
12137 else
12138#endif
12139 {
12140 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12141 ++p;
12142 }
12143 }
12144}
12145
12146/*
12147 * "toupper(string)" function
12148 */
12149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153{
12154 char_u *p;
12155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 p = vim_strsave(get_tv_string(&argvars[0]));
12157 rettv->v_type = VAR_STRING;
12158 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
12160 if (p != NULL)
12161 while (*p != NUL)
12162 {
12163#ifdef FEAT_MBYTE
12164 int l;
12165
12166 if (enc_utf8)
12167 {
12168 int c, uc;
12169
12170 c = utf_ptr2char(p);
12171 uc = utf_toupper(c);
12172 l = utf_ptr2len_check(p);
12173 /* TODO: reallocate string when byte count changes. */
12174 if (utf_char2len(uc) == l)
12175 utf_char2bytes(uc, p);
12176 p += l;
12177 }
12178 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12179 p += l; /* skip multi-byte character */
12180 else
12181#endif
12182 {
12183 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12184 p++;
12185 }
12186 }
12187}
12188
12189/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012190 * "tr(string, fromstr, tostr)" function
12191 */
12192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012194 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012196{
12197 char_u *instr;
12198 char_u *fromstr;
12199 char_u *tostr;
12200 char_u *p;
12201#ifdef FEAT_MBYTE
12202 int inlen;
12203 int fromlen;
12204 int tolen;
12205 int idx;
12206 char_u *cpstr;
12207 int cplen;
12208 int first = TRUE;
12209#endif
12210 char_u buf[NUMBUFLEN];
12211 char_u buf2[NUMBUFLEN];
12212 garray_T ga;
12213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 instr = get_tv_string(&argvars[0]);
12215 fromstr = get_tv_string_buf(&argvars[1], buf);
12216 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012217
12218 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->v_type = VAR_STRING;
12220 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012221 ga_init2(&ga, (int)sizeof(char), 80);
12222
12223#ifdef FEAT_MBYTE
12224 if (!has_mbyte)
12225#endif
12226 /* not multi-byte: fromstr and tostr must be the same length */
12227 if (STRLEN(fromstr) != STRLEN(tostr))
12228 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012229#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012230error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012231#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012232 EMSG2(_(e_invarg2), fromstr);
12233 ga_clear(&ga);
12234 return;
12235 }
12236
12237 /* fromstr and tostr have to contain the same number of chars */
12238 while (*instr != NUL)
12239 {
12240#ifdef FEAT_MBYTE
12241 if (has_mbyte)
12242 {
12243 inlen = mb_ptr2len_check(instr);
12244 cpstr = instr;
12245 cplen = inlen;
12246 idx = 0;
12247 for (p = fromstr; *p != NUL; p += fromlen)
12248 {
12249 fromlen = mb_ptr2len_check(p);
12250 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12251 {
12252 for (p = tostr; *p != NUL; p += tolen)
12253 {
12254 tolen = mb_ptr2len_check(p);
12255 if (idx-- == 0)
12256 {
12257 cplen = tolen;
12258 cpstr = p;
12259 break;
12260 }
12261 }
12262 if (*p == NUL) /* tostr is shorter than fromstr */
12263 goto error;
12264 break;
12265 }
12266 ++idx;
12267 }
12268
12269 if (first && cpstr == instr)
12270 {
12271 /* Check that fromstr and tostr have the same number of
12272 * (multi-byte) characters. Done only once when a character
12273 * of instr doesn't appear in fromstr. */
12274 first = FALSE;
12275 for (p = tostr; *p != NUL; p += tolen)
12276 {
12277 tolen = mb_ptr2len_check(p);
12278 --idx;
12279 }
12280 if (idx != 0)
12281 goto error;
12282 }
12283
12284 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012285 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012286 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012287
12288 instr += inlen;
12289 }
12290 else
12291#endif
12292 {
12293 /* When not using multi-byte chars we can do it faster. */
12294 p = vim_strchr(fromstr, *instr);
12295 if (p != NULL)
12296 ga_append(&ga, tostr[p - fromstr]);
12297 else
12298 ga_append(&ga, *instr);
12299 ++instr;
12300 }
12301 }
12302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012304}
12305
12306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 * "type(expr)" function
12308 */
12309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012311 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012314 int n;
12315
12316 switch (argvars[0].v_type)
12317 {
12318 case VAR_NUMBER: n = 0; break;
12319 case VAR_STRING: n = 1; break;
12320 case VAR_FUNC: n = 2; break;
12321 case VAR_LIST: n = 3; break;
12322 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12323 }
12324 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325}
12326
12327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012328 * "values(dict)" function
12329 */
12330 static void
12331f_values(argvars, rettv)
12332 typeval *argvars;
12333 typeval *rettv;
12334{
12335 dict_list(argvars, rettv, 1);
12336}
12337
12338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 * "virtcol(string)" function
12340 */
12341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012343 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345{
12346 colnr_T vcol = 0;
12347 pos_T *fp;
12348
12349 fp = var2fpos(&argvars[0], FALSE);
12350 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12351 {
12352 getvvcol(curwin, fp, NULL, NULL, &vcol);
12353 ++vcol;
12354 }
12355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012356 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357}
12358
12359/*
12360 * "visualmode()" function
12361 */
12362/*ARGSUSED*/
12363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012365 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367{
12368#ifdef FEAT_VISUAL
12369 char_u str[2];
12370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 str[0] = curbuf->b_visual_mode_eval;
12373 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
12376 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012377 if ((argvars[0].v_type == VAR_NUMBER
12378 && argvars[0].vval.v_number != 0)
12379 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 curbuf->b_visual_mode_eval = NUL;
12382#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384#endif
12385}
12386
12387/*
12388 * "winbufnr(nr)" function
12389 */
12390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012392 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
12395 win_T *wp;
12396
12397 wp = find_win_by_nr(&argvars[0]);
12398 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402}
12403
12404/*
12405 * "wincol()" function
12406 */
12407/*ARGSUSED*/
12408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012410 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412{
12413 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012414 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415}
12416
12417/*
12418 * "winheight(nr)" function
12419 */
12420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012422 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424{
12425 win_T *wp;
12426
12427 wp = find_win_by_nr(&argvars[0]);
12428 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432}
12433
12434/*
12435 * "winline()" function
12436 */
12437/*ARGSUSED*/
12438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012440 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
12443 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445}
12446
12447/*
12448 * "winnr()" function
12449 */
12450/* ARGSUSED */
12451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012453 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455{
12456 int nr = 1;
12457#ifdef FEAT_WINDOWS
12458 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012459 win_T *twin = curwin;
12460 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012462 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012465 if (STRCMP(arg, "$") == 0)
12466 twin = lastwin;
12467 else if (STRCMP(arg, "#") == 0)
12468 {
12469 twin = prevwin;
12470 if (prevwin == NULL)
12471 nr = 0;
12472 }
12473 else
12474 {
12475 EMSG2(_(e_invexpr2), arg);
12476 nr = 0;
12477 }
12478 }
12479
12480 if (nr > 0)
12481 for (wp = firstwin; wp != twin; wp = wp->w_next)
12482 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485}
12486
12487/*
12488 * "winrestcmd()" function
12489 */
12490/* ARGSUSED */
12491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012493 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495{
12496#ifdef FEAT_WINDOWS
12497 win_T *wp;
12498 int winnr = 1;
12499 garray_T ga;
12500 char_u buf[50];
12501
12502 ga_init2(&ga, (int)sizeof(char), 70);
12503 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12504 {
12505 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12506 ga_concat(&ga, buf);
12507# ifdef FEAT_VERTSPLIT
12508 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12509 ga_concat(&ga, buf);
12510# endif
12511 ++winnr;
12512 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012513 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520}
12521
12522/*
12523 * "winwidth(nr)" function
12524 */
12525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012527 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529{
12530 win_T *wp;
12531
12532 wp = find_win_by_nr(&argvars[0]);
12533 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 else
12536#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012537 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540#endif
12541}
12542
12543 static win_T *
12544find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012545 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546{
12547#ifdef FEAT_WINDOWS
12548 win_T *wp;
12549#endif
12550 int nr;
12551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553
12554#ifdef FEAT_WINDOWS
12555 if (nr == 0)
12556 return curwin;
12557
12558 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12559 if (--nr <= 0)
12560 break;
12561 return wp;
12562#else
12563 if (nr == 0 || nr == 1)
12564 return curwin;
12565 return NULL;
12566#endif
12567}
12568
12569/*
12570 * Translate a String variable into a position.
12571 */
12572 static pos_T *
12573var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012574 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 int lnum; /* TRUE when $ is last line */
12576{
12577 char_u *name;
12578 static pos_T pos;
12579 pos_T *pp;
12580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012581 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582 if (name[0] == '.') /* cursor */
12583 return &curwin->w_cursor;
12584 if (name[0] == '\'') /* mark */
12585 {
12586 pp = getmark(name[1], FALSE);
12587 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
12588 return NULL;
12589 return pp;
12590 }
12591 if (name[0] == '$') /* last column or line */
12592 {
12593 if (lnum)
12594 {
12595 pos.lnum = curbuf->b_ml.ml_line_count;
12596 pos.col = 0;
12597 }
12598 else
12599 {
12600 pos.lnum = curwin->w_cursor.lnum;
12601 pos.col = (colnr_T)STRLEN(ml_get_curline());
12602 }
12603 return &pos;
12604 }
12605 return NULL;
12606}
12607
12608/*
12609 * Get the length of an environment variable name.
12610 * Advance "arg" to the first character after the name.
12611 * Return 0 for error.
12612 */
12613 static int
12614get_env_len(arg)
12615 char_u **arg;
12616{
12617 char_u *p;
12618 int len;
12619
12620 for (p = *arg; vim_isIDc(*p); ++p)
12621 ;
12622 if (p == *arg) /* no name found */
12623 return 0;
12624
12625 len = (int)(p - *arg);
12626 *arg = p;
12627 return len;
12628}
12629
12630/*
12631 * Get the length of the name of a function or internal variable.
12632 * "arg" is advanced to the first non-white character after the name.
12633 * Return 0 if something is wrong.
12634 */
12635 static int
12636get_id_len(arg)
12637 char_u **arg;
12638{
12639 char_u *p;
12640 int len;
12641
12642 /* Find the end of the name. */
12643 for (p = *arg; eval_isnamec(*p); ++p)
12644 ;
12645 if (p == *arg) /* no name found */
12646 return 0;
12647
12648 len = (int)(p - *arg);
12649 *arg = skipwhite(p);
12650
12651 return len;
12652}
12653
12654/*
12655 * Get the length of the name of a function.
12656 * "arg" is advanced to the first non-white character after the name.
12657 * Return 0 if something is wrong.
12658 * If the name contains 'magic' {}'s, expand them and return the
12659 * expanded name in an allocated string via 'alias' - caller must free.
12660 */
12661 static int
12662get_func_len(arg, alias, evaluate)
12663 char_u **arg;
12664 char_u **alias;
12665 int evaluate;
12666{
12667 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 char_u *p;
12669 char_u *expr_start;
12670 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671
12672 *alias = NULL; /* default to no alias */
12673
12674 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
12675 && (*arg)[2] == (int)KE_SNR)
12676 {
12677 /* hard coded <SNR>, already translated */
12678 *arg += 3;
12679 return get_id_len(arg) + 3;
12680 }
12681 len = eval_fname_script(*arg);
12682 if (len > 0)
12683 {
12684 /* literal "<SID>", "s:" or "<SNR>" */
12685 *arg += len;
12686 }
12687
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 if (expr_start != NULL)
12693 {
12694 char_u *temp_string;
12695
12696 if (!evaluate)
12697 {
12698 len += (int)(p - *arg);
12699 *arg = skipwhite(p);
12700 return len;
12701 }
12702
12703 /*
12704 * Include any <SID> etc in the expanded string:
12705 * Thus the -len here.
12706 */
12707 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
12708 if (temp_string == NULL)
12709 return 0;
12710 *alias = temp_string;
12711 *arg = skipwhite(p);
12712 return (int)STRLEN(temp_string);
12713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714
12715 len += get_id_len(arg);
12716 if (len == 0)
12717 EMSG2(_(e_invexpr2), *arg);
12718
12719 return len;
12720}
12721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722/*
12723 * Find the end of a variable or function name, taking care of magic braces.
12724 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
12725 * start and end of the first magic braces item.
12726 * Return a pointer to just after the name. Equal to "arg" if there is no
12727 * valid name.
12728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 char_u *arg;
12732 char_u **expr_start;
12733 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012734 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 int mb_nest = 0;
12737 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 char_u *p;
12739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 *expr_start = NULL;
12743 *expr_end = NULL;
12744 }
12745
12746 for (p = arg; *p != NUL
12747 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012748 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000012749 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 || mb_nest != 0
12751 || br_nest != 0); ++p)
12752 {
12753 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 if (*p == '[')
12756 ++br_nest;
12757 else if (*p == ']')
12758 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 if (*p == '{')
12763 {
12764 mb_nest++;
12765 if (expr_start != NULL && *expr_start == NULL)
12766 *expr_start = p;
12767 }
12768 else if (*p == '}')
12769 {
12770 mb_nest--;
12771 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12772 *expr_end = p;
12773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 }
12776
12777 return p;
12778}
12779
12780/*
12781 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000012782 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 */
12784 static int
12785eval_isnamec(c)
12786 int c;
12787{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012788 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789}
12790
12791/*
12792 * Find a v: variable.
12793 * Return it's index, or -1 if not found.
12794 */
12795 static int
12796find_vim_var(name, len)
12797 char_u *name;
12798 int len; /* length of "name" */
12799{
12800 char_u *vname;
12801 int vlen;
12802 int i;
12803
12804 /*
12805 * Ignore "v:" for old built-in variables, require it for new ones.
12806 */
12807 if (name[0] == 'v' && name[1] == ':')
12808 {
12809 vname = name + 2;
12810 vlen = len - 2;
12811 }
12812 else
12813 {
12814 vname = name;
12815 vlen = len;
12816 }
12817 for (i = 0; i < VV_LEN; ++i)
12818 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
12819 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
12820 return i;
12821 return -1;
12822}
12823
12824/*
12825 * Set number v: variable to "val".
12826 */
12827 void
12828set_vim_var_nr(idx, val)
12829 int idx;
12830 long val;
12831{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012832 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833}
12834
12835/*
12836 * Get number v: variable value;
12837 */
12838 long
12839get_vim_var_nr(idx)
12840 int idx;
12841{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012842 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843}
12844
12845/*
12846 * Set v:count, v:count1 and v:prevcount.
12847 */
12848 void
12849set_vcount(count, count1)
12850 long count;
12851 long count1;
12852{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012853 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
12854 vimvars[VV_COUNT].vv_nr = count;
12855 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856}
12857
12858/*
12859 * Set string v: variable to a copy of "val".
12860 */
12861 void
12862set_vim_var_string(idx, val, len)
12863 int idx;
12864 char_u *val;
12865 int len; /* length of "val" to use or -1 (whole string) */
12866{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012867 /* Need to do this (at least) once, since we can't initialize a union.
12868 * Will always be invoked when "v:progname" is set. */
12869 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
12870
Bram Moolenaare9a41262005-01-15 22:18:47 +000012871 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012873 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012875 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012877 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
12880/*
12881 * Set v:register if needed.
12882 */
12883 void
12884set_reg_var(c)
12885 int c;
12886{
12887 char_u regname;
12888
12889 if (c == 0 || c == ' ')
12890 regname = '"';
12891 else
12892 regname = c;
12893 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012894 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 set_vim_var_string(VV_REG, &regname, 1);
12896}
12897
12898/*
12899 * Get or set v:exception. If "oldval" == NULL, return the current value.
12900 * Otherwise, restore the value to "oldval" and return NULL.
12901 * Must always be called in pairs to save and restore v:exception! Does not
12902 * take care of memory allocations.
12903 */
12904 char_u *
12905v_exception(oldval)
12906 char_u *oldval;
12907{
12908 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012909 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910
Bram Moolenaare9a41262005-01-15 22:18:47 +000012911 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 return NULL;
12913}
12914
12915/*
12916 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
12917 * Otherwise, restore the value to "oldval" and return NULL.
12918 * Must always be called in pairs to save and restore v:throwpoint! Does not
12919 * take care of memory allocations.
12920 */
12921 char_u *
12922v_throwpoint(oldval)
12923 char_u *oldval;
12924{
12925 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012926 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927
Bram Moolenaare9a41262005-01-15 22:18:47 +000012928 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 return NULL;
12930}
12931
12932#if defined(FEAT_AUTOCMD) || defined(PROTO)
12933/*
12934 * Set v:cmdarg.
12935 * If "eap" != NULL, use "eap" to generate the value and return the old value.
12936 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
12937 * Must always be called in pairs!
12938 */
12939 char_u *
12940set_cmdarg(eap, oldarg)
12941 exarg_T *eap;
12942 char_u *oldarg;
12943{
12944 char_u *oldval;
12945 char_u *newval;
12946 unsigned len;
12947
Bram Moolenaare9a41262005-01-15 22:18:47 +000012948 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012949 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012951 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012952 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 }
12955
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012956 if (eap->force_bin == FORCE_BIN)
12957 len = 6;
12958 else if (eap->force_bin == FORCE_NOBIN)
12959 len = 8;
12960 else
12961 len = 0;
12962 if (eap->force_ff != 0)
12963 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
12964# ifdef FEAT_MBYTE
12965 if (eap->force_enc != 0)
12966 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
12967# endif
12968
12969 newval = alloc(len + 1);
12970 if (newval == NULL)
12971 return NULL;
12972
12973 if (eap->force_bin == FORCE_BIN)
12974 sprintf((char *)newval, " ++bin");
12975 else if (eap->force_bin == FORCE_NOBIN)
12976 sprintf((char *)newval, " ++nobin");
12977 else
12978 *newval = NUL;
12979 if (eap->force_ff != 0)
12980 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
12981 eap->cmd + eap->force_ff);
12982# ifdef FEAT_MBYTE
12983 if (eap->force_enc != 0)
12984 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
12985 eap->cmd + eap->force_enc);
12986# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000012987 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012988 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989}
12990#endif
12991
12992/*
12993 * Get the value of internal variable "name".
12994 * Return OK or FAIL.
12995 */
12996 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012997get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 char_u *name;
12999 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001{
13002 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013003 typeval *tv = NULL;
13004 typeval atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 VAR v;
13006 int cc;
13007 int i;
13008
13009 /* truncate the name, so that we can use strcmp() */
13010 cc = name[len];
13011 name[len] = NUL;
13012
13013 /*
13014 * Check for "b:changedtick".
13015 */
13016 if (STRCMP(name, "b:changedtick") == 0)
13017 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013018 atv.v_type = VAR_NUMBER;
13019 atv.vval.v_number = curbuf->b_changedtick;
13020 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 }
13022
13023 /*
13024 * Check for built-in v: variables.
13025 */
13026 else if ((i = find_vim_var(name, len)) >= 0)
13027 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013028 tv = &vimvars[i].tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 }
13030
13031 /*
13032 * Check for user-defined variables.
13033 */
13034 else
13035 {
13036 v = find_var(name, FALSE);
13037 if (v != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013038 tv = &v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 }
13040
Bram Moolenaare9a41262005-01-15 22:18:47 +000013041 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 if (rettv != NULL)
13044 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 ret = FAIL;
13046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013048 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049
13050 name[len] = cc;
13051
13052 return ret;
13053}
13054
13055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013056 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13057 * value).
13058 */
13059 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013061{
13062 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
13063}
13064
13065/*
13066 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 * The string "s" must have been allocated, it is consumed.
13068 * Return NULL for out of memory, the variable otherwise.
13069 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013070 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 char_u *s;
13073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 rettv = alloc_tv();
13077 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->v_type = VAR_STRING;
13080 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 }
13082 else
13083 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013084 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085}
13086
13087/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013088 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 */
13090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013092 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093{
13094 if (varp != NULL)
13095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013096 switch (varp->v_type)
13097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013098 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013099 func_unref(varp->vval.v_string);
13100 /*FALLTHROUGH*/
13101 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013102 vim_free(varp->vval.v_string);
13103 break;
13104 case VAR_LIST:
13105 list_unref(varp->vval.v_list);
13106 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013107 case VAR_DICT:
13108 dict_unref(varp->vval.v_dict);
13109 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013110 default:
13111 break;
13112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 vim_free(varp);
13114 }
13115}
13116
13117/*
13118 * Free the memory for a variable value and set the value to NULL or 0.
13119 */
13120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013122 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123{
13124 if (varp != NULL)
13125 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013128 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013129 func_unref(varp->vval.v_string);
13130 /*FALLTHROUGH*/
13131 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013132 vim_free(varp->vval.v_string);
13133 varp->vval.v_string = NULL;
13134 break;
13135 case VAR_LIST:
13136 list_unref(varp->vval.v_list);
13137 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013138 case VAR_DICT:
13139 dict_unref(varp->vval.v_dict);
13140 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013142 varp->vval.v_number = 0;
13143 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144 case VAR_UNKNOWN:
13145 break;
13146 default:
13147 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 }
13150}
13151
13152/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 * Set the value of a variable to NULL without freeing items.
13154 */
13155 static void
13156init_tv(varp)
13157 typeval *varp;
13158{
13159 if (varp != NULL)
13160 vim_memset(varp, 0, sizeof(typeval));
13161}
13162
13163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 * Get the number value of a variable.
13165 * If it is a String variable, uses vim_str2nr().
13166 */
13167 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013171 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013173 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013175 case VAR_NUMBER:
13176 n = (long)(varp->vval.v_number);
13177 break;
13178 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013179 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013180 break;
13181 case VAR_STRING:
13182 if (varp->vval.v_string != NULL)
13183 vim_str2nr(varp->vval.v_string, NULL, NULL,
13184 TRUE, TRUE, &n, NULL);
13185 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013186 case VAR_LIST:
13187 EMSG(_("E703: Using a List as a number"));
13188 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013189 case VAR_DICT:
13190 EMSG(_("E728: Using a Dictionary as a number"));
13191 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013193 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013196 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197}
13198
13199/*
13200 * Get the lnum from the first argument. Also accepts ".", "$", etc.
13201 */
13202 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 linenr_T lnum;
13208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 if (lnum == 0) /* no valid number, try using line() */
13211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212 rettv.v_type = VAR_NUMBER;
13213 f_line(argvars, &rettv);
13214 lnum = rettv.vval.v_number;
13215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 }
13217 return lnum;
13218}
13219
13220/*
13221 * Get the string value of a variable.
13222 * If it is a Number variable, the number is converted into a string.
13223 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
13224 * get_var_string_buf() uses a given buffer.
13225 * If the String variable has never been set, return an empty string.
13226 * Never returns NULL;
13227 */
13228 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013230 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231{
13232 static char_u mybuf[NUMBUFLEN];
13233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
13237 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013239 typeval *varp;
13240 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013242 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 case VAR_NUMBER:
13245 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
13246 return buf;
13247 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013248 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013249 break;
13250 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013251 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013252 break;
13253 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013254 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013255 break;
13256 case VAR_STRING:
13257 if (varp->vval.v_string != NULL)
13258 return varp->vval.v_string;
13259 break;
13260 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013264 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265}
13266
13267/*
13268 * Find variable "name" in the list of variables.
13269 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013270 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 */
13272 static VAR
13273find_var(name, writing)
13274 char_u *name;
13275 int writing;
13276{
13277 int i;
13278 char_u *varname;
13279 garray_T *gap;
13280
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 if (name[0] == 'a' && name[1] == ':')
13282 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013283 /* Function arguments "a:".
13284 * NOTE: We use a typecast, because function arguments don't have a
13285 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 if (writing)
13287 {
13288 EMSG2(_(e_readonlyvar), name);
13289 return NULL;
13290 }
13291 name += 2;
13292 if (current_funccal == NULL)
13293 return NULL;
13294 if (VIM_ISDIGIT(*name))
13295 {
13296 i = atol((char *)name);
13297 if (i == 0) /* a:0 */
13298 return &current_funccal->a0_var;
13299 i += current_funccal->func->args.ga_len;
13300 if (i > current_funccal->argcount) /* a:999 */
13301 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013302 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 }
13304 if (STRCMP(name, "firstline") == 0)
13305 return &(current_funccal->firstline);
13306 if (STRCMP(name, "lastline") == 0)
13307 return &(current_funccal->lastline);
13308 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
13309 if (STRCMP(name, ((char_u **)
13310 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 return NULL;
13313 }
13314
13315 gap = find_var_ga(name, &varname);
13316 if (gap == NULL)
13317 return NULL;
13318 return find_var_in_ga(gap, varname);
13319}
13320
13321 static VAR
13322find_var_in_ga(gap, varname)
13323 garray_T *gap;
13324 char_u *varname;
13325{
13326 int i;
13327
13328 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013329 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
13330 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 break;
13332 if (i < 0)
13333 return NULL;
13334 return &VAR_GAP_ENTRY(i, gap);
13335}
13336
13337/*
13338 * Find the growarray and start of name without ':' for a variable name.
13339 */
13340 static garray_T *
13341find_var_ga(name, varname)
13342 char_u *name;
13343 char_u **varname;
13344{
13345 if (name[1] != ':')
13346 {
13347 /* If not "x:name" there must not be any ":" in the name. */
13348 if (vim_strchr(name, ':') != NULL)
13349 return NULL;
13350 *varname = name;
13351 if (current_funccal == NULL)
13352 return &variables; /* global variable */
13353 return &current_funccal->l_vars; /* local function variable */
13354 }
13355 *varname = name + 2;
13356 if (*name == 'b') /* buffer variable */
13357 return &curbuf->b_vars;
13358 if (*name == 'w') /* window variable */
13359 return &curwin->w_vars;
13360 if (*name == 'g') /* global variable */
13361 return &variables;
13362 if (*name == 'l' && current_funccal != NULL)/* local function variable */
13363 return &current_funccal->l_vars;
13364 if (*name == 's' /* script variable */
13365 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
13366 return &SCRIPT_VARS(current_SID);
13367 return NULL;
13368}
13369
13370/*
13371 * Get the string value of a (global/local) variable.
13372 * Returns NULL when it doesn't exist.
13373 */
13374 char_u *
13375get_var_value(name)
13376 char_u *name;
13377{
13378 VAR v;
13379
13380 v = find_var(name, FALSE);
13381 if (v == NULL)
13382 return NULL;
13383 return get_var_string(v);
13384}
13385
13386/*
13387 * Allocate a new growarry for a sourced script. It will be used while
13388 * sourcing this script and when executing functions defined in the script.
13389 */
13390 void
13391new_script_vars(id)
13392 scid_T id;
13393{
13394 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
13395 {
13396 while (ga_scripts.ga_len < id)
13397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013398 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 }
13401 }
13402}
13403
13404/*
13405 * Initialize internal variables for use.
13406 */
13407 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013408vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 garray_T *gap;
13410{
13411 ga_init2(gap, (int)sizeof(var), 4);
13412}
13413
13414/*
13415 * Clean up a list of internal variables.
13416 */
13417 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013418vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 garray_T *gap;
13420{
13421 int i;
13422
13423 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013424 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 ga_clear(gap);
13426}
13427
13428 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013429clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 VAR v;
13431{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013432 vim_free(v->v_name);
13433 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
13438 * List the value of one internal variable.
13439 */
13440 static void
13441list_one_var(v, prefix)
13442 VAR v;
13443 char_u *prefix;
13444{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013445 char_u *tofree;
13446 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013447 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013448
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013449 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013450 list_one_var_a(prefix, v->v_name, v->tv.v_type,
13451 s == NULL ? (char_u *)"" : s);
13452 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453}
13454
13455/*
13456 * List the value of one "v:" variable.
13457 */
13458 static void
13459list_vim_var(i)
13460 int i; /* index in vimvars[] */
13461{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013462 char_u *tofree;
13463 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 char_u numbuf[NUMBUFLEN];
13465
Bram Moolenaare9a41262005-01-15 22:18:47 +000013466 s = echo_string(&vimvars[i].tv, &tofree, numbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
Bram Moolenaare9a41262005-01-15 22:18:47 +000013468 vimvars[i].tv.v_type, s == NULL ? (char_u *)"" : s);
13469 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470}
13471
13472 static void
13473list_one_var_a(prefix, name, type, string)
13474 char_u *prefix;
13475 char_u *name;
13476 int type;
13477 char_u *string;
13478{
13479 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13480 if (name != NULL) /* "a:" vars don't have a name stored */
13481 msg_puts(name);
13482 msg_putchar(' ');
13483 msg_advance(22);
13484 if (type == VAR_NUMBER)
13485 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486 else if (type == VAR_FUNC)
13487 msg_putchar('*');
13488 else if (type == VAR_LIST)
13489 {
13490 msg_putchar('[');
13491 if (*string == '[')
13492 ++string;
13493 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013494 else if (type == VAR_DICT)
13495 {
13496 msg_putchar('{');
13497 if (*string == '{')
13498 ++string;
13499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 else
13501 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013502
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013504
13505 if (type == VAR_FUNC)
13506 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507}
13508
13509/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 * If the variable already exists, the value is updated.
13512 * Otherwise the variable is created.
13513 */
13514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 typeval *tv;
13518 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519{
13520 int i;
13521 VAR v;
13522 char_u *varname;
13523 garray_T *gap;
13524
13525 /*
13526 * Handle setting internal v: variables.
13527 */
13528 i = find_vim_var(name, (int)STRLEN(name));
13529 if (i >= 0)
13530 {
13531 if (vimvars[i].flags & VV_RO)
13532 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000013533 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
13534 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 else
13536 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013537 if (vimvars[i].tv.v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013539 vim_free(vimvars[i].vv_str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013541 vimvars[i].vv_str = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013542 else
13543 {
13544 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013545 vimvars[i].vv_str = tv->vval.v_string;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 }
13549 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013550 vimvars[i].vv_nr = get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 }
13552 return;
13553 }
13554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013556 {
13557 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13558 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13559 ? name[2] : name[0]))
13560 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013561 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013562 return;
13563 }
13564 if (function_exists(name))
13565 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013566 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013567 return;
13568 }
13569 }
13570
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 v = find_var(name, TRUE);
13572 if (v != NULL) /* existing variable, only need to free string */
13573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013575 && !((v->tv.v_type == VAR_STRING
13576 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 && (tv->v_type == VAR_STRING
13578 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013580 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581 return;
13582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 }
13585 else /* add a new variable */
13586 {
13587 gap = find_var_ga(name, &varname);
13588 if (gap == NULL) /* illegal name */
13589 {
13590 EMSG2(_("E461: Illegal variable name: %s"), name);
13591 return;
13592 }
13593
13594 /* Try to use an empty entry */
13595 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013596 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 break;
13598 if (i < 0) /* need to allocate more room */
13599 {
13600 if (ga_grow(gap, 1) == FAIL)
13601 return;
13602 i = gap->ga_len;
13603 }
13604 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013605 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 return;
13607 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610 if (copy || tv->v_type == VAR_NUMBER)
13611 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013612 else
13613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 v->tv = *tv;
13615 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617}
13618
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013619/*
13620 * Copy the values from typeval "from" to typeval "to".
13621 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013622 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013626 typeval *from;
13627 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013629 to->v_type = from->v_type;
13630 switch (from->v_type)
13631 {
13632 case VAR_NUMBER:
13633 to->vval.v_number = from->vval.v_number;
13634 break;
13635 case VAR_STRING:
13636 case VAR_FUNC:
13637 if (from->vval.v_string == NULL)
13638 to->vval.v_string = NULL;
13639 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013642 if (from->v_type == VAR_FUNC)
13643 func_ref(to->vval.v_string);
13644 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013645 break;
13646 case VAR_LIST:
13647 if (from->vval.v_list == NULL)
13648 to->vval.v_list = NULL;
13649 else
13650 {
13651 to->vval.v_list = from->vval.v_list;
13652 ++to->vval.v_list->lv_refcount;
13653 }
13654 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013655 case VAR_DICT:
13656 if (from->vval.v_dict == NULL)
13657 to->vval.v_dict = NULL;
13658 else
13659 {
13660 to->vval.v_dict = from->vval.v_dict;
13661 ++to->vval.v_dict->dv_refcount;
13662 }
13663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013664 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013666 break;
13667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668}
13669
13670/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013671 * Make a copy of an item.
13672 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
13673 */
13674 static void
13675item_copy(from, to, deep)
13676 typeval *from;
13677 typeval *to;
13678 int deep;
13679{
13680 static int recurse = 0;
13681
13682 if (recurse >= VAR_MAXNEST)
13683 {
13684 EMSG(_("E698: variable nested too deep for making a copy"));
13685 return;
13686 }
13687 ++recurse;
13688
13689 switch (from->v_type)
13690 {
13691 case VAR_NUMBER:
13692 case VAR_STRING:
13693 case VAR_FUNC:
13694 copy_tv(from, to);
13695 break;
13696 case VAR_LIST:
13697 to->v_type = VAR_LIST;
13698 to->vval.v_list = list_copy(from->vval.v_list, deep);
13699 break;
13700 case VAR_DICT:
13701 to->v_type = VAR_DICT;
13702 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
13703 break;
13704 default:
13705 EMSG2(_(e_intern2), "item_copy()");
13706 }
13707 --recurse;
13708}
13709
13710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 * ":echo expr1 ..." print each argument separated with a space, add a
13712 * newline at the end.
13713 * ":echon expr1 ..." print each argument plain.
13714 */
13715 void
13716ex_echo(eap)
13717 exarg_T *eap;
13718{
13719 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013721 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 char_u *p;
13723 int needclr = TRUE;
13724 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013725 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726
13727 if (eap->skip)
13728 ++emsg_skip;
13729 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
13730 {
13731 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 {
13734 /*
13735 * Report the invalid expression unless the expression evaluation
13736 * has been cancelled due to an aborting error, an interrupt, or an
13737 * exception.
13738 */
13739 if (!aborting())
13740 EMSG2(_(e_invexpr2), p);
13741 break;
13742 }
13743 if (!eap->skip)
13744 {
13745 if (atstart)
13746 {
13747 atstart = FALSE;
13748 /* Call msg_start() after eval1(), evaluating the expression
13749 * may cause a message to appear. */
13750 if (eap->cmdidx == CMD_echo)
13751 msg_start();
13752 }
13753 else if (eap->cmdidx == CMD_echo)
13754 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013755 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013756 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 if (*p == '\n' || *p == '\r' || *p == TAB)
13758 {
13759 if (*p != TAB && needclr)
13760 {
13761 /* remove any text still there from the command */
13762 msg_clr_eos();
13763 needclr = FALSE;
13764 }
13765 msg_putchar_attr(*p, echo_attr);
13766 }
13767 else
13768 {
13769#ifdef FEAT_MBYTE
13770 if (has_mbyte)
13771 {
13772 int i = (*mb_ptr2len_check)(p);
13773
13774 (void)msg_outtrans_len_attr(p, i, echo_attr);
13775 p += i - 1;
13776 }
13777 else
13778#endif
13779 (void)msg_outtrans_len_attr(p, 1, echo_attr);
13780 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013781 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 arg = skipwhite(arg);
13785 }
13786 eap->nextcmd = check_nextcmd(arg);
13787
13788 if (eap->skip)
13789 --emsg_skip;
13790 else
13791 {
13792 /* remove text that may still be there from the command */
13793 if (needclr)
13794 msg_clr_eos();
13795 if (eap->cmdidx == CMD_echo)
13796 msg_end();
13797 }
13798}
13799
13800/*
13801 * ":echohl {name}".
13802 */
13803 void
13804ex_echohl(eap)
13805 exarg_T *eap;
13806{
13807 int id;
13808
13809 id = syn_name2id(eap->arg);
13810 if (id == 0)
13811 echo_attr = 0;
13812 else
13813 echo_attr = syn_id2attr(id);
13814}
13815
13816/*
13817 * ":execute expr1 ..." execute the result of an expression.
13818 * ":echomsg expr1 ..." Print a message
13819 * ":echoerr expr1 ..." Print an error
13820 * Each gets spaces around each argument and a newline at the end for
13821 * echo commands
13822 */
13823 void
13824ex_execute(eap)
13825 exarg_T *eap;
13826{
13827 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013828 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 int ret = OK;
13830 char_u *p;
13831 garray_T ga;
13832 int len;
13833 int save_did_emsg;
13834
13835 ga_init2(&ga, 1, 80);
13836
13837 if (eap->skip)
13838 ++emsg_skip;
13839 while (*arg != NUL && *arg != '|' && *arg != '\n')
13840 {
13841 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013842 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 {
13844 /*
13845 * Report the invalid expression unless the expression evaluation
13846 * has been cancelled due to an aborting error, an interrupt, or an
13847 * exception.
13848 */
13849 if (!aborting())
13850 EMSG2(_(e_invexpr2), p);
13851 ret = FAIL;
13852 break;
13853 }
13854
13855 if (!eap->skip)
13856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013857 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 len = (int)STRLEN(p);
13859 if (ga_grow(&ga, len + 2) == FAIL)
13860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 ret = FAIL;
13863 break;
13864 }
13865 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 ga.ga_len += len;
13869 }
13870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 arg = skipwhite(arg);
13873 }
13874
13875 if (ret != FAIL && ga.ga_data != NULL)
13876 {
13877 if (eap->cmdidx == CMD_echomsg)
13878 MSG_ATTR(ga.ga_data, echo_attr);
13879 else if (eap->cmdidx == CMD_echoerr)
13880 {
13881 /* We don't want to abort following commands, restore did_emsg. */
13882 save_did_emsg = did_emsg;
13883 EMSG((char_u *)ga.ga_data);
13884 if (!force_abort)
13885 did_emsg = save_did_emsg;
13886 }
13887 else if (eap->cmdidx == CMD_execute)
13888 do_cmdline((char_u *)ga.ga_data,
13889 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
13890 }
13891
13892 ga_clear(&ga);
13893
13894 if (eap->skip)
13895 --emsg_skip;
13896
13897 eap->nextcmd = check_nextcmd(arg);
13898}
13899
13900/*
13901 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
13902 * "arg" points to the "&" or '+' when called, to "option" when returning.
13903 * Returns NULL when no option name found. Otherwise pointer to the char
13904 * after the option name.
13905 */
13906 static char_u *
13907find_option_end(arg, opt_flags)
13908 char_u **arg;
13909 int *opt_flags;
13910{
13911 char_u *p = *arg;
13912
13913 ++p;
13914 if (*p == 'g' && p[1] == ':')
13915 {
13916 *opt_flags = OPT_GLOBAL;
13917 p += 2;
13918 }
13919 else if (*p == 'l' && p[1] == ':')
13920 {
13921 *opt_flags = OPT_LOCAL;
13922 p += 2;
13923 }
13924 else
13925 *opt_flags = 0;
13926
13927 if (!ASCII_ISALPHA(*p))
13928 return NULL;
13929 *arg = p;
13930
13931 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
13932 p += 4; /* termcap option */
13933 else
13934 while (ASCII_ISALPHA(*p))
13935 ++p;
13936 return p;
13937}
13938
13939/*
13940 * ":function"
13941 */
13942 void
13943ex_function(eap)
13944 exarg_T *eap;
13945{
13946 char_u *theline;
13947 int j;
13948 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 char_u *name = NULL;
13951 char_u *p;
13952 char_u *arg;
13953 garray_T newargs;
13954 garray_T newlines;
13955 int varargs = FALSE;
13956 int mustend = FALSE;
13957 int flags = 0;
13958 ufunc_T *fp;
13959 int indent;
13960 int nesting;
13961 char_u *skip_until = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013962 VAR v;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013963 funcdict fudi;
13964 static int func_nr = 0; /* number for nameless function */
13965 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966
13967 /*
13968 * ":function" without argument: list functions.
13969 */
13970 if (ends_excmd(*eap->arg))
13971 {
13972 if (!eap->skip)
13973 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013974 if (!isdigit(*fp->name))
13975 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 eap->nextcmd = check_nextcmd(eap->arg);
13977 return;
13978 }
13979
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013980 /*
13981 * Get the function name. There are these situations:
13982 * func normal function name
13983 * "name" == func, "fudi.fd_dict" == NULL
13984 * dict.func new dictionary entry
13985 * "name" == NULL, "fudi.fd_dict" set,
13986 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
13987 * dict.func existing dict entry with a Funcref
13988 * "name" == fname, "fudi.fd_dict" set,
13989 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
13990 * dict.func existing dict entry that's not a Funcref
13991 * "name" == NULL, "fudi.fd_dict" set,
13992 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
13993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013995 name = trans_function_name(&p, eap->skip, 0, &fudi);
13996 paren = (vim_strchr(p, '(') != NULL);
13997 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 {
13999 /*
14000 * Return on an invalid expression in braces, unless the expression
14001 * evaluation has been cancelled due to an aborting error, an
14002 * interrupt, or an exception.
14003 */
14004 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014005 {
14006 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 else
14010 eap->skip = TRUE;
14011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 /* An error in a function call during evaluation of an expression in magic
14013 * braces should not cause the function not to be defined. */
14014 saved_did_emsg = did_emsg;
14015 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016
14017 /*
14018 * ":function func" with only function name: list function.
14019 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014020 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 {
14022 if (!ends_excmd(*skipwhite(p)))
14023 {
14024 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014025 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 }
14027 eap->nextcmd = check_nextcmd(p);
14028 if (eap->nextcmd != NULL)
14029 *p = NUL;
14030 if (!eap->skip && !got_int)
14031 {
14032 fp = find_func(name);
14033 if (fp != NULL)
14034 {
14035 list_func_head(fp, TRUE);
14036 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
14037 {
14038 msg_putchar('\n');
14039 msg_outnum((long)(j + 1));
14040 if (j < 9)
14041 msg_putchar(' ');
14042 if (j < 99)
14043 msg_putchar(' ');
14044 msg_prt_line(FUNCLINE(fp, j));
14045 out_flush(); /* show a line at a time */
14046 ui_breakcheck();
14047 }
14048 if (!got_int)
14049 {
14050 msg_putchar('\n');
14051 msg_puts((char_u *)" endfunction");
14052 }
14053 }
14054 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014055 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014057 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 }
14059
14060 /*
14061 * ":function name(arg1, arg2)" Define function.
14062 */
14063 p = skipwhite(p);
14064 if (*p != '(')
14065 {
14066 if (!eap->skip)
14067 {
14068 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014069 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 }
14071 /* attempt to continue by skipping some text */
14072 if (vim_strchr(p, '(') != NULL)
14073 p = vim_strchr(p, '(');
14074 }
14075 p = skipwhite(p + 1);
14076
14077 ga_init2(&newargs, (int)sizeof(char_u *), 3);
14078 ga_init2(&newlines, (int)sizeof(char_u *), 3);
14079
14080 /*
14081 * Isolate the arguments: "arg1, arg2, ...)"
14082 */
14083 while (*p != ')')
14084 {
14085 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
14086 {
14087 varargs = TRUE;
14088 p += 3;
14089 mustend = TRUE;
14090 }
14091 else
14092 {
14093 arg = p;
14094 while (ASCII_ISALNUM(*p) || *p == '_')
14095 ++p;
14096 if (arg == p || isdigit(*arg)
14097 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
14098 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
14099 {
14100 if (!eap->skip)
14101 EMSG2(_("E125: Illegal argument: %s"), arg);
14102 break;
14103 }
14104 if (ga_grow(&newargs, 1) == FAIL)
14105 goto erret;
14106 c = *p;
14107 *p = NUL;
14108 arg = vim_strsave(arg);
14109 if (arg == NULL)
14110 goto erret;
14111 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
14112 *p = c;
14113 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 if (*p == ',')
14115 ++p;
14116 else
14117 mustend = TRUE;
14118 }
14119 p = skipwhite(p);
14120 if (mustend && *p != ')')
14121 {
14122 if (!eap->skip)
14123 EMSG2(_(e_invarg2), eap->arg);
14124 break;
14125 }
14126 }
14127 ++p; /* skip the ')' */
14128
Bram Moolenaare9a41262005-01-15 22:18:47 +000014129 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 for (;;)
14131 {
14132 p = skipwhite(p);
14133 if (STRNCMP(p, "range", 5) == 0)
14134 {
14135 flags |= FC_RANGE;
14136 p += 5;
14137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014138 else if (STRNCMP(p, "dict", 4) == 0)
14139 {
14140 flags |= FC_DICT;
14141 p += 4;
14142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 else if (STRNCMP(p, "abort", 5) == 0)
14144 {
14145 flags |= FC_ABORT;
14146 p += 5;
14147 }
14148 else
14149 break;
14150 }
14151
14152 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
14153 EMSG(_(e_trailing));
14154
14155 /*
14156 * Read the body of the function, until ":endfunction" is found.
14157 */
14158 if (KeyTyped)
14159 {
14160 /* Check if the function already exists, don't let the user type the
14161 * whole function before telling him it doesn't work! For a script we
14162 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014163 if (!eap->skip && !eap->forceit)
14164 {
14165 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
14166 EMSG(_(e_funcdict));
14167 else if (name != NULL && find_func(name) != NULL)
14168 EMSG2(_(e_funcexts), name);
14169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170
14171 msg_putchar('\n'); /* don't overwrite the function name */
14172 cmdline_row = msg_row;
14173 }
14174
14175 indent = 2;
14176 nesting = 0;
14177 for (;;)
14178 {
14179 msg_scroll = TRUE;
14180 need_wait_return = FALSE;
14181 if (eap->getline == NULL)
14182 theline = getcmdline(':', 0L, indent);
14183 else
14184 theline = eap->getline(':', eap->cookie, indent);
14185 if (KeyTyped)
14186 lines_left = Rows - 1;
14187 if (theline == NULL)
14188 {
14189 EMSG(_("E126: Missing :endfunction"));
14190 goto erret;
14191 }
14192
14193 if (skip_until != NULL)
14194 {
14195 /* between ":append" and "." and between ":python <<EOF" and "EOF"
14196 * don't check for ":endfunc". */
14197 if (STRCMP(theline, skip_until) == 0)
14198 {
14199 vim_free(skip_until);
14200 skip_until = NULL;
14201 }
14202 }
14203 else
14204 {
14205 /* skip ':' and blanks*/
14206 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
14207 ;
14208
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014209 /* Check for "endfunction". */
14210 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 {
14212 vim_free(theline);
14213 break;
14214 }
14215
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014216 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 * at "end". */
14218 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
14219 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014220 else if (STRNCMP(p, "if", 2) == 0
14221 || STRNCMP(p, "wh", 2) == 0
14222 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 || STRNCMP(p, "try", 3) == 0)
14224 indent += 2;
14225
14226 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014227 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014229 if (*p == '!')
14230 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 p += eval_fname_script(p);
14232 if (ASCII_ISALPHA(*p))
14233 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014234 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 if (*skipwhite(p) == '(')
14236 {
14237 ++nesting;
14238 indent += 2;
14239 }
14240 }
14241 }
14242
14243 /* Check for ":append" or ":insert". */
14244 p = skip_range(p, NULL);
14245 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
14246 || (p[0] == 'i'
14247 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
14248 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
14249 skip_until = vim_strsave((char_u *)".");
14250
14251 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
14252 arg = skipwhite(skiptowhite(p));
14253 if (arg[0] == '<' && arg[1] =='<'
14254 && ((p[0] == 'p' && p[1] == 'y'
14255 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
14256 || (p[0] == 'p' && p[1] == 'e'
14257 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
14258 || (p[0] == 't' && p[1] == 'c'
14259 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
14260 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
14261 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014262 || (p[0] == 'm' && p[1] == 'z'
14263 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 ))
14265 {
14266 /* ":python <<" continues until a dot, like ":append" */
14267 p = skipwhite(arg + 2);
14268 if (*p == NUL)
14269 skip_until = vim_strsave((char_u *)".");
14270 else
14271 skip_until = vim_strsave(p);
14272 }
14273 }
14274
14275 /* Add the line to the function. */
14276 if (ga_grow(&newlines, 1) == FAIL)
14277 goto erret;
14278 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
14279 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 }
14281
14282 /* Don't define the function when skipping commands or when an error was
14283 * detected. */
14284 if (eap->skip || did_emsg)
14285 goto erret;
14286
14287 /*
14288 * If there are no errors, add the function
14289 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014290 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014291 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014292 v = find_var(name, FALSE);
14293 if (v != NULL && v->tv.v_type == VAR_FUNC)
14294 {
14295 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
14296 goto erret;
14297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014298
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014299 fp = find_func(name);
14300 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014302 if (!eap->forceit)
14303 {
14304 EMSG2(_(e_funcexts), name);
14305 goto erret;
14306 }
14307 if (fp->calls > 0)
14308 {
14309 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
14310 name);
14311 goto erret;
14312 }
14313 /* redefine existing function */
14314 ga_clear_strings(&(fp->args));
14315 ga_clear_strings(&(fp->lines));
14316 vim_free(name);
14317 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 }
14320 else
14321 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014322 char numbuf[20];
14323
14324 fp = NULL;
14325 if (fudi.fd_newkey == NULL && !eap->forceit)
14326 {
14327 EMSG(_(e_funcdict));
14328 goto erret;
14329 }
14330
14331 /* Give the function a sequential number. Can only be used with a
14332 * Funcref! */
14333 vim_free(name);
14334 sprintf(numbuf, "%d", ++func_nr);
14335 name = vim_strsave((char_u *)numbuf);
14336 if (name == NULL)
14337 goto erret;
14338 }
14339
14340 if (fp == NULL)
14341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14343 if (fp == NULL)
14344 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014345
14346 if (fudi.fd_dict != NULL)
14347 {
14348 if (fudi.fd_di == NULL)
14349 {
14350 /* add new dict entry */
14351 fudi.fd_di = dictitem_alloc();
14352 if (fudi.fd_di == NULL)
14353 {
14354 vim_free(fp);
14355 goto erret;
14356 }
14357 fudi.fd_di->di_key = fudi.fd_newkey;
14358 fudi.fd_newkey = NULL;
14359 dict_add(fudi.fd_dict, fudi.fd_di);
14360 }
14361 else
14362 /* overwrite existing dict entry */
14363 clear_tv(&fudi.fd_di->di_tv);
14364 fudi.fd_di->di_tv.v_type = VAR_FUNC;
14365 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
14366 fp->refcount = 1;
14367 }
14368
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014370 fp->name = name;
14371 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 fp->next = firstfunc;
14373 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 }
14375 fp->args = newargs;
14376 fp->lines = newlines;
14377 fp->varargs = varargs;
14378 fp->flags = flags;
14379 fp->calls = 0;
14380 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014381 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382
14383erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 ga_clear_strings(&newargs);
14385 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014386ret_free:
14387 vim_free(skip_until);
14388 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391}
14392
14393/*
14394 * Get a function name, translating "<SID>" and "<SNR>".
14395 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014396 * flags:
14397 * TFN_INT: internal function name OK
14398 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 * Advances "pp" to just after the function name (if no error).
14400 */
14401 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014402trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 char_u **pp;
14404 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014405 int flags;
14406 funcdict *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014408 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 char_u *start;
14410 char_u *end;
14411 int lead;
14412 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413 int len;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014414 lval lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014415
14416 if (fdp != NULL)
14417 vim_memset(fdp, 0, sizeof(funcdict));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418
14419 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
14420 start = *pp;
14421 lead = eval_fname_script(start);
14422 if (lead > 0)
14423 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014425 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014426 if (end == start)
14427 {
14428 if (!skip)
14429 EMSG(_("E129: Function name required"));
14430 goto theend;
14431 }
14432 if (end == NULL || (lv.ll_tv != NULL && (lead > 0 || lv.ll_range)))
14433 {
14434 /*
14435 * Report an invalid expression in braces, unless the expression
14436 * evaluation has been cancelled due to an aborting error, an
14437 * interrupt, or an exception.
14438 */
14439 if (!aborting())
14440 {
14441 if (end != NULL)
14442 EMSG2(_(e_invarg2), start);
14443 }
14444 else
14445 *pp = find_name_end(start, NULL, NULL, TRUE);
14446 goto theend;
14447 }
14448
14449 if (lv.ll_tv != NULL)
14450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014451 if (fdp != NULL)
14452 {
14453 fdp->fd_dict = lv.ll_dict;
14454 fdp->fd_newkey = lv.ll_newkey;
14455 lv.ll_newkey = NULL;
14456 fdp->fd_di = lv.ll_di;
14457 fdp->fd_pdi = lv.ll_pdi;
14458 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014459 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14460 {
14461 name = vim_strsave(lv.ll_tv->vval.v_string);
14462 *pp = end;
14463 }
14464 else
14465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014466 if (!skip && !(flags & TFN_QUIET)
14467 && (fdp == NULL || lv.ll_dict == NULL))
14468 EMSG(_(e_funcref));
14469 else
14470 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014471 name = NULL;
14472 }
14473 goto theend;
14474 }
14475
14476 if (lv.ll_name == NULL)
14477 {
14478 /* Error found, but continue after the function name. */
14479 *pp = end;
14480 goto theend;
14481 }
14482
14483 if (lv.ll_exp_name != NULL)
14484 len = STRLEN(lv.ll_exp_name);
14485 else
14486 len = (int)(end - start);
14487
14488 /*
14489 * Copy the function name to allocated memory.
14490 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14491 * Accept <SNR>123_name() outside a script.
14492 */
14493 if (skip)
14494 lead = 0; /* do nothing */
14495 else if (lead > 0)
14496 {
14497 lead = 3;
14498 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14499 {
14500 if (current_SID <= 0)
14501 {
14502 EMSG(_(e_usingsid));
14503 goto theend;
14504 }
14505 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14506 lead += (int)STRLEN(sid_buf);
14507 }
14508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014509 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014510 {
14511 EMSG2(_("E128: Function name must start with a capital: %s"),
14512 lv.ll_name);
14513 goto theend;
14514 }
14515 name = alloc((unsigned)(len + lead + 1));
14516 if (name != NULL)
14517 {
14518 if (lead > 0)
14519 {
14520 name[0] = K_SPECIAL;
14521 name[1] = KS_EXTRA;
14522 name[2] = (int)KE_SNR;
14523 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14524 STRCPY(name + 3, sid_buf);
14525 }
14526 mch_memmove(name + lead, lv.ll_name, (size_t)len);
14527 name[len + lead] = NUL;
14528 }
14529 *pp = end;
14530
14531theend:
14532 clear_lval(&lv);
14533 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534}
14535
14536/*
14537 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14538 * Return 2 if "p" starts with "s:".
14539 * Return 0 otherwise.
14540 */
14541 static int
14542eval_fname_script(p)
14543 char_u *p;
14544{
14545 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
14546 || STRNICMP(p + 1, "SNR>", 4) == 0))
14547 return 5;
14548 if (p[0] == 's' && p[1] == ':')
14549 return 2;
14550 return 0;
14551}
14552
14553/*
14554 * Return TRUE if "p" starts with "<SID>" or "s:".
14555 * Only works if eval_fname_script() returned non-zero for "p"!
14556 */
14557 static int
14558eval_fname_sid(p)
14559 char_u *p;
14560{
14561 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
14562}
14563
14564/*
14565 * List the head of the function: "name(arg1, arg2)".
14566 */
14567 static void
14568list_func_head(fp, indent)
14569 ufunc_T *fp;
14570 int indent;
14571{
14572 int j;
14573
14574 msg_start();
14575 if (indent)
14576 MSG_PUTS(" ");
14577 MSG_PUTS("function ");
14578 if (fp->name[0] == K_SPECIAL)
14579 {
14580 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
14581 msg_puts(fp->name + 3);
14582 }
14583 else
14584 msg_puts(fp->name);
14585 msg_putchar('(');
14586 for (j = 0; j < fp->args.ga_len; ++j)
14587 {
14588 if (j)
14589 MSG_PUTS(", ");
14590 msg_puts(FUNCARG(fp, j));
14591 }
14592 if (fp->varargs)
14593 {
14594 if (j)
14595 MSG_PUTS(", ");
14596 MSG_PUTS("...");
14597 }
14598 msg_putchar(')');
14599}
14600
14601/*
14602 * Find a function by name, return pointer to it in ufuncs.
14603 * Return NULL for unknown function.
14604 */
14605 static ufunc_T *
14606find_func(name)
14607 char_u *name;
14608{
14609 ufunc_T *fp;
14610
14611 for (fp = firstfunc; fp != NULL; fp = fp->next)
14612 if (STRCMP(name, fp->name) == 0)
14613 break;
14614 return fp;
14615}
14616
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014617/*
14618 * Return TRUE if a function "name" exists.
14619 */
14620 static int
14621function_exists(name)
14622 char_u *name;
14623{
14624 char_u *p = name;
14625 int n = FALSE;
14626
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014627 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014628 if (p != NULL)
14629 {
14630 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14631 n = (find_func(p) != NULL);
14632 else if (ASCII_ISLOWER(*p))
14633 n = (find_internal_func(p) >= 0);
14634 vim_free(p);
14635 }
14636 return n;
14637}
14638
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
14640
14641/*
14642 * Function given to ExpandGeneric() to obtain the list of user defined
14643 * function names.
14644 */
14645 char_u *
14646get_user_func_name(xp, idx)
14647 expand_T *xp;
14648 int idx;
14649{
14650 static ufunc_T *fp = NULL;
14651
14652 if (idx == 0)
14653 fp = firstfunc;
14654 if (fp != NULL)
14655 {
14656 if (STRLEN(fp->name) + 4 >= IOSIZE)
14657 return fp->name; /* prevents overflow */
14658
14659 cat_func_name(IObuff, fp);
14660 if (xp->xp_context != EXPAND_USER_FUNC)
14661 {
14662 STRCAT(IObuff, "(");
14663 if (!fp->varargs && fp->args.ga_len == 0)
14664 STRCAT(IObuff, ")");
14665 }
14666
14667 fp = fp->next;
14668 return IObuff;
14669 }
14670 return NULL;
14671}
14672
14673#endif /* FEAT_CMDL_COMPL */
14674
14675/*
14676 * Copy the function name of "fp" to buffer "buf".
14677 * "buf" must be able to hold the function name plus three bytes.
14678 * Takes care of script-local function names.
14679 */
14680 static void
14681cat_func_name(buf, fp)
14682 char_u *buf;
14683 ufunc_T *fp;
14684{
14685 if (fp->name[0] == K_SPECIAL)
14686 {
14687 STRCPY(buf, "<SNR>");
14688 STRCAT(buf, fp->name + 3);
14689 }
14690 else
14691 STRCPY(buf, fp->name);
14692}
14693
14694/*
14695 * ":delfunction {name}"
14696 */
14697 void
14698ex_delfunction(eap)
14699 exarg_T *eap;
14700{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014701 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 char_u *p;
14703 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014704 funcdict fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705
14706 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014707 name = trans_function_name(&p, eap->skip, 0, &fudi);
14708 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014710 {
14711 if (fudi.fd_dict != NULL && !eap->skip)
14712 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 if (!ends_excmd(*skipwhite(p)))
14716 {
14717 vim_free(name);
14718 EMSG(_(e_trailing));
14719 return;
14720 }
14721 eap->nextcmd = check_nextcmd(p);
14722 if (eap->nextcmd != NULL)
14723 *p = NUL;
14724
14725 if (!eap->skip)
14726 fp = find_func(name);
14727 vim_free(name);
14728
14729 if (!eap->skip)
14730 {
14731 if (fp == NULL)
14732 {
14733 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14734 return;
14735 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014736 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 {
14738 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14739 return;
14740 }
14741
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014742 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014744 /* Delete the dict item that refers to the function, it will
14745 * invoke func_unref() and possibly delete the function. */
14746 *fudi.fd_pdi = fudi.fd_di->di_next;
14747 dictitem_free(fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014749 else
14750 func_free(fp);
14751 }
14752}
14753
14754/*
14755 * Free a function and remove it from the list of functions.
14756 */
14757 static void
14758func_free(fp)
14759 ufunc_T *fp;
14760{
14761 ufunc_T *pfp;
14762
14763 /* clear this function */
14764 vim_free(fp->name);
14765 ga_clear_strings(&(fp->args));
14766 ga_clear_strings(&(fp->lines));
14767
14768 /* remove the function from the function list */
14769 if (firstfunc == fp)
14770 firstfunc = fp->next;
14771 else
14772 {
14773 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
14774 if (pfp->next == fp)
14775 {
14776 pfp->next = fp->next;
14777 break;
14778 }
14779 }
14780 vim_free(fp);
14781}
14782
14783/*
14784 * Unreference a Function: decrement the reference count and free it when it
14785 * becomes zero. Only for numbered functions.
14786 */
14787 static void
14788func_unref(name)
14789 char_u *name;
14790{
14791 ufunc_T *fp;
14792
14793 if (name != NULL && isdigit(*name))
14794 {
14795 fp = find_func(name);
14796 if (fp == NULL)
14797 EMSG2(_(e_intern2), "func_unref()");
14798 else if (--fp->refcount <= 0)
14799 {
14800 /* Only delete it when it's not being used. Otherwise it's done
14801 * when "calls" becomes zero. */
14802 if (fp->calls == 0)
14803 func_free(fp);
14804 }
14805 }
14806}
14807
14808/*
14809 * Count a reference to a Function.
14810 */
14811 static void
14812func_ref(name)
14813 char_u *name;
14814{
14815 ufunc_T *fp;
14816
14817 if (name != NULL && isdigit(*name))
14818 {
14819 fp = find_func(name);
14820 if (fp == NULL)
14821 EMSG2(_(e_intern2), "func_ref()");
14822 else
14823 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 }
14825}
14826
14827/*
14828 * Call a user function.
14829 */
14830 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000014831call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 ufunc_T *fp; /* pointer to function */
14833 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014834 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 linenr_T firstline; /* first line of range */
14837 linenr_T lastline; /* last line of range */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014838 dictvar *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839{
14840 char_u *save_sourcing_name;
14841 linenr_T save_sourcing_lnum;
14842 scid_T save_current_SID;
14843 struct funccall fc;
14844 struct funccall *save_fcp = current_funccal;
14845 int save_did_emsg;
14846 static int depth = 0;
14847
14848 /* If depth of calling is getting too high, don't execute the function */
14849 if (depth >= p_mfd)
14850 {
14851 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->v_type = VAR_NUMBER;
14853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 return;
14855 }
14856 ++depth;
14857
14858 line_breakcheck(); /* check for CTRL-C hit */
14859
14860 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014861 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 fc.func = fp;
14863 fc.argcount = argcount;
14864 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865 fc.rettv = rettv;
14866 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 fc.linenr = 0;
14868 fc.returned = FALSE;
14869 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014870 fc.a0_var.tv.v_type = VAR_NUMBER;
14871 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
14872 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014874 fc.firstline.tv.v_type = VAR_NUMBER;
14875 fc.firstline.tv.vval.v_number = firstline;
14876 fc.firstline.v_name = NULL;
14877 fc.lastline.tv.v_type = VAR_NUMBER;
14878 fc.lastline.tv.vval.v_number = lastline;
14879 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 /* Check if this function has a breakpoint. */
14881 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
14882 fc.dbg_tick = debug_tick;
14883
Bram Moolenaare9a41262005-01-15 22:18:47 +000014884 if (selfdict != NULL && ga_grow(&fc.l_vars, 1) != FAIL)
14885 {
14886 VAR v = &VAR_GAP_ENTRY(0, &fc.l_vars);
14887
14888 /* Set the "self" local variable. */
14889 if ((v->v_name = vim_strsave((char_u *)"self")) != NULL)
14890 {
14891 ++fc.l_vars.ga_len;
14892 v->tv.v_type = VAR_DICT;
14893 v->tv.vval.v_dict = selfdict;
14894 ++selfdict->dv_refcount;
14895 }
14896 }
14897
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 /* Don't redraw while executing the function. */
14899 ++RedrawingDisabled;
14900 save_sourcing_name = sourcing_name;
14901 save_sourcing_lnum = sourcing_lnum;
14902 sourcing_lnum = 1;
14903 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
14904 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
14905 if (sourcing_name != NULL)
14906 {
14907 if (save_sourcing_name != NULL
14908 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
14909 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
14910 else
14911 STRCPY(sourcing_name, "function ");
14912 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
14913
14914 if (p_verbose >= 12)
14915 {
14916 ++no_wait_return;
14917 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14918 msg_str((char_u *)_("calling %s"), sourcing_name);
14919 if (p_verbose >= 14)
14920 {
14921 int i;
14922 char_u buf[MSG_BUF_LEN];
14923
14924 msg_puts((char_u *)"(");
14925 for (i = 0; i < argcount; ++i)
14926 {
14927 if (i > 0)
14928 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014929 if (argvars[i].v_type == VAR_NUMBER)
14930 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 else
14932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934 buf, MSG_BUF_LEN);
14935 msg_puts((char_u *)"\"");
14936 msg_puts(buf);
14937 msg_puts((char_u *)"\"");
14938 }
14939 }
14940 msg_puts((char_u *)")");
14941 }
14942 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14943 cmdline_row = msg_row;
14944 --no_wait_return;
14945 }
14946 }
14947 save_current_SID = current_SID;
14948 current_SID = fp->script_ID;
14949 save_did_emsg = did_emsg;
14950 did_emsg = FALSE;
14951
14952 /* call do_cmdline() to execute the lines */
14953 do_cmdline(NULL, get_func_line, (void *)&fc,
14954 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
14955
14956 --RedrawingDisabled;
14957
14958 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 clear_tv(rettv);
14962 rettv->v_type = VAR_NUMBER;
14963 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 }
14965
14966 /* when being verbose, mention the return value */
14967 if (p_verbose >= 12)
14968 {
14969 char_u *sn, *val;
14970
14971 ++no_wait_return;
14972 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14973
14974 /* Make sure the output fits in IObuff. */
14975 sn = sourcing_name;
14976 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
14977 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
14978
14979 if (aborting())
14980 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014981 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 (long)fc.rettv->vval.v_number);
14984 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 if (STRLEN(val) > IOSIZE / 2 - 50)
14988 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
14989 smsg((char_u *)_("%s returning \"%s\""), sn, val);
14990 }
14991 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14992 cmdline_row = msg_row;
14993 --no_wait_return;
14994 }
14995
14996 vim_free(sourcing_name);
14997 sourcing_name = save_sourcing_name;
14998 sourcing_lnum = save_sourcing_lnum;
14999 current_SID = save_current_SID;
15000
15001 if (p_verbose >= 12 && sourcing_name != NULL)
15002 {
15003 ++no_wait_return;
15004 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15005 msg_str((char_u *)_("continuing in %s"), sourcing_name);
15006 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15007 cmdline_row = msg_row;
15008 --no_wait_return;
15009 }
15010
15011 did_emsg |= save_did_emsg;
15012 current_funccal = save_fcp;
15013
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015014 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 --depth;
15016}
15017
15018/*
15019 * ":return [expr]"
15020 */
15021 void
15022ex_return(eap)
15023 exarg_T *eap;
15024{
15025 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 int returning = FALSE;
15028
15029 if (current_funccal == NULL)
15030 {
15031 EMSG(_("E133: :return not inside a function"));
15032 return;
15033 }
15034
15035 if (eap->skip)
15036 ++emsg_skip;
15037
15038 eap->nextcmd = NULL;
15039 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 {
15042 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 }
15047 /* It's safer to return also on error. */
15048 else if (!eap->skip)
15049 {
15050 /*
15051 * Return unless the expression evaluation has been cancelled due to an
15052 * aborting error, an interrupt, or an exception.
15053 */
15054 if (!aborting())
15055 returning = do_return(eap, FALSE, TRUE, NULL);
15056 }
15057
15058 /* When skipping or the return gets pending, advance to the next command
15059 * in this line (!returning). Otherwise, ignore the rest of the line.
15060 * Following lines will be ignored by get_func_line(). */
15061 if (returning)
15062 eap->nextcmd = NULL;
15063 else if (eap->nextcmd == NULL) /* no argument */
15064 eap->nextcmd = check_nextcmd(arg);
15065
15066 if (eap->skip)
15067 --emsg_skip;
15068}
15069
15070/*
15071 * Return from a function. Possibly makes the return pending. Also called
15072 * for a pending return at the ":endtry" or after returning from an extra
15073 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015074 * when called due to a ":return" command. "rettv" may point to a typeval
15075 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 * FALSE when the return gets pending.
15077 */
15078 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 exarg_T *eap;
15081 int reanimate;
15082 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084{
15085 int idx;
15086 struct condstack *cstack = eap->cstack;
15087
15088 if (reanimate)
15089 /* Undo the return. */
15090 current_funccal->returned = FALSE;
15091
15092 /*
15093 * Cleanup (and inactivate) conditionals, but stop when a try conditional
15094 * not in its finally clause (which then is to be executed next) is found.
15095 * In this case, make the ":return" pending for execution at the ":endtry".
15096 * Otherwise, return normally.
15097 */
15098 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
15099 if (idx >= 0)
15100 {
15101 cstack->cs_pending[idx] = CSTP_RETURN;
15102
15103 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015104 /* A pending return again gets pending. "rettv" points to an
15105 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015107 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 else
15109 {
15110 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015113 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015115 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 {
15117 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015118 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
15119 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 else
15121 EMSG(_(e_outofmem));
15122 }
15123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125
15126 if (reanimate)
15127 {
15128 /* The pending return value could be overwritten by a ":return"
15129 * without argument in a finally clause; reset the default
15130 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131 current_funccal->rettv->v_type = VAR_NUMBER;
15132 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 }
15134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 }
15137 else
15138 {
15139 current_funccal->returned = TRUE;
15140
15141 /* If the return is carried out now, store the return value. For
15142 * a return immediately after reanimation, the value is already
15143 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146 clear_tv(current_funccal->rettv);
15147 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 }
15151 }
15152
15153 return idx < 0;
15154}
15155
15156/*
15157 * Free the variable with a pending return value.
15158 */
15159 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015160discard_pending_return(rettv)
15161 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164}
15165
15166/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 * is an allocated string. Used by report_pending() for verbose messages.
15169 */
15170 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015171get_return_cmd(rettv)
15172 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015174 char_u *s;
15175 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015176 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 if (rettv == NULL)
15179 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015181 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015182
15183 STRCPY(IObuff, ":return ");
15184 STRNCPY(IObuff + 8, s, IOSIZE - 8);
15185 if (STRLEN(s) + 8 >= IOSIZE)
15186 STRCPY(IObuff + IOSIZE - 4, "...");
15187 vim_free(tofree);
15188 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189}
15190
15191/*
15192 * Get next function line.
15193 * Called by do_cmdline() to get the next line.
15194 * Returns allocated string, or NULL for end of function.
15195 */
15196/* ARGSUSED */
15197 char_u *
15198get_func_line(c, cookie, indent)
15199 int c; /* not used */
15200 void *cookie;
15201 int indent; /* not used */
15202{
15203 struct funccall *fcp = (struct funccall *)cookie;
15204 char_u *retval;
15205 garray_T *gap; /* growarray with function lines */
15206
15207 /* If breakpoints have been added/deleted need to check for it. */
15208 if (fcp->dbg_tick != debug_tick)
15209 {
15210 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15211 sourcing_lnum);
15212 fcp->dbg_tick = debug_tick;
15213 }
15214
15215 gap = &fcp->func->lines;
15216 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15217 retval = NULL;
15218 else if (fcp->returned || fcp->linenr >= gap->ga_len)
15219 retval = NULL;
15220 else
15221 {
15222 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
15223 sourcing_lnum = fcp->linenr;
15224 }
15225
15226 /* Did we encounter a breakpoint? */
15227 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
15228 {
15229 dbg_breakpoint(fcp->func->name, sourcing_lnum);
15230 /* Find next breakpoint. */
15231 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15232 sourcing_lnum);
15233 fcp->dbg_tick = debug_tick;
15234 }
15235
15236 return retval;
15237}
15238
15239/*
15240 * Return TRUE if the currently active function should be ended, because a
15241 * return was encountered or an error occured. Used inside a ":while".
15242 */
15243 int
15244func_has_ended(cookie)
15245 void *cookie;
15246{
15247 struct funccall *fcp = (struct funccall *)cookie;
15248
15249 /* Ignore the "abort" flag if the abortion behavior has been changed due to
15250 * an error inside a try conditional. */
15251 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15252 || fcp->returned);
15253}
15254
15255/*
15256 * return TRUE if cookie indicates a function which "abort"s on errors.
15257 */
15258 int
15259func_has_abort(cookie)
15260 void *cookie;
15261{
15262 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
15263}
15264
15265#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
15266typedef enum
15267{
15268 VAR_FLAVOUR_DEFAULT,
15269 VAR_FLAVOUR_SESSION,
15270 VAR_FLAVOUR_VIMINFO
15271} var_flavour_T;
15272
15273static var_flavour_T var_flavour __ARGS((char_u *varname));
15274
15275 static var_flavour_T
15276var_flavour(varname)
15277 char_u *varname;
15278{
15279 char_u *p = varname;
15280
15281 if (ASCII_ISUPPER(*p))
15282 {
15283 while (*(++p))
15284 if (ASCII_ISLOWER(*p))
15285 return VAR_FLAVOUR_SESSION;
15286 return VAR_FLAVOUR_VIMINFO;
15287 }
15288 else
15289 return VAR_FLAVOUR_DEFAULT;
15290}
15291#endif
15292
15293#if defined(FEAT_VIMINFO) || defined(PROTO)
15294/*
15295 * Restore global vars that start with a capital from the viminfo file
15296 */
15297 int
15298read_viminfo_varlist(virp, writing)
15299 vir_T *virp;
15300 int writing;
15301{
15302 char_u *tab;
15303 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015304 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305
15306 if (!writing && (find_viminfo_parameter('!') != NULL))
15307 {
15308 tab = vim_strchr(virp->vir_line + 1, '\t');
15309 if (tab != NULL)
15310 {
15311 *tab++ = '\0'; /* isolate the variable name */
15312 if (*tab == 'S') /* string var */
15313 is_string = TRUE;
15314
15315 tab = vim_strchr(tab, '\t');
15316 if (tab != NULL)
15317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 if (is_string)
15319 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015320 tv.v_type = VAR_STRING;
15321 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 }
15324 else
15325 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015326 tv.v_type = VAR_NUMBER;
15327 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015329 set_var(virp->vir_line + 1, &tv, FALSE);
15330 if (is_string)
15331 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 }
15333 }
15334 }
15335
15336 return viminfo_readline(virp);
15337}
15338
15339/*
15340 * Write global vars that start with a capital to the viminfo file
15341 */
15342 void
15343write_viminfo_varlist(fp)
15344 FILE *fp;
15345{
15346 garray_T *gap = &variables; /* global variable */
15347 VAR this_var;
15348 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 char *s;
15350 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015351 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352
15353 if (find_viminfo_parameter('!') == NULL)
15354 return;
15355
15356 fprintf(fp, _("\n# global variables:\n"));
15357 for (i = gap->ga_len; --i >= 0; )
15358 {
15359 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015360 if (this_var->v_name != NULL
15361 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 switch (this_var->tv.v_type)
15364 {
15365 case VAR_STRING: s = "STR"; break;
15366 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015367 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368 }
15369 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015370 viminfo_writestring(fp, echo_string(&this_var->tv,
15371 &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 }
15374 }
15375}
15376#endif
15377
15378#if defined(FEAT_SESSION) || defined(PROTO)
15379 int
15380store_session_globals(fd)
15381 FILE *fd;
15382{
15383 garray_T *gap = &variables; /* global variable */
15384 VAR this_var;
15385 int i;
15386 char_u *p, *t;
15387
15388 for (i = gap->ga_len; --i >= 0; )
15389 {
15390 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015391 if (this_var->v_name != NULL
15392 && (this_var->tv.v_type == VAR_NUMBER
15393 || this_var->tv.v_type == VAR_STRING)
15394 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015396 /* Escape special characters with a backslash. Turn a LF and
15397 * CR into \n and \r. */
15398 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015400 if (p == NULL) /* out of memory */
15401 continue;
15402 for (t = p; *t != NUL; ++t)
15403 if (*t == '\n')
15404 *t = 'n';
15405 else if (*t == '\r')
15406 *t = 'r';
15407 if ((fprintf(fd, "let %s = %c%s%c",
15408 this_var->v_name,
15409 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
15410 p,
15411 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
15412 || put_eol(fd) == FAIL)
15413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015417 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 }
15419 }
15420 return OK;
15421}
15422#endif
15423
15424#endif /* FEAT_EVAL */
15425
15426#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
15427
15428
15429#ifdef WIN3264
15430/*
15431 * Functions for ":8" filename modifier: get 8.3 version of a filename.
15432 */
15433static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15434static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
15435static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15436
15437/*
15438 * Get the short pathname of a file.
15439 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
15440 */
15441 static int
15442get_short_pathname(fnamep, bufp, fnamelen)
15443 char_u **fnamep;
15444 char_u **bufp;
15445 int *fnamelen;
15446{
15447 int l,len;
15448 char_u *newbuf;
15449
15450 len = *fnamelen;
15451
15452 l = GetShortPathName(*fnamep, *fnamep, len);
15453 if (l > len - 1)
15454 {
15455 /* If that doesn't work (not enough space), then save the string
15456 * and try again with a new buffer big enough
15457 */
15458 newbuf = vim_strnsave(*fnamep, l);
15459 if (newbuf == NULL)
15460 return 0;
15461
15462 vim_free(*bufp);
15463 *fnamep = *bufp = newbuf;
15464
15465 l = GetShortPathName(*fnamep,*fnamep,l+1);
15466
15467 /* Really should always succeed, as the buffer is big enough */
15468 }
15469
15470 *fnamelen = l;
15471 return 1;
15472}
15473
15474/*
15475 * Create a short path name. Returns the length of the buffer it needs.
15476 * Doesn't copy over the end of the buffer passed in.
15477 */
15478 static int
15479shortpath_for_invalid_fname(fname, bufp, fnamelen)
15480 char_u **fname;
15481 char_u **bufp;
15482 int *fnamelen;
15483{
15484 char_u *s, *p, *pbuf2, *pbuf3;
15485 char_u ch;
15486 int l,len,len2,plen,slen;
15487
15488 /* Make a copy */
15489 len2 = *fnamelen;
15490 pbuf2 = vim_strnsave(*fname, len2);
15491 pbuf3 = NULL;
15492
15493 s = pbuf2 + len2 - 1; /* Find the end */
15494 slen = 1;
15495 plen = len2;
15496
15497 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015498 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 {
15500 --s;
15501 ++slen;
15502 --plen;
15503 }
15504
15505 do
15506 {
15507 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015508 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 {
15510 --s;
15511 ++slen;
15512 --plen;
15513 }
15514 if (s <= pbuf2)
15515 break;
15516
15517 /* Remeber the character that is about to be blatted */
15518 ch = *s;
15519 *s = 0; /* get_short_pathname requires a null-terminated string */
15520
15521 /* Try it in situ */
15522 p = pbuf2;
15523 if (!get_short_pathname(&p, &pbuf3, &plen))
15524 {
15525 vim_free(pbuf2);
15526 return -1;
15527 }
15528 *s = ch; /* Preserve the string */
15529 } while (plen == 0);
15530
15531 if (plen > 0)
15532 {
15533 /* Remeber the length of the new string. */
15534 *fnamelen = len = plen + slen;
15535 vim_free(*bufp);
15536 if (len > len2)
15537 {
15538 /* If there's not enough space in the currently allocated string,
15539 * then copy it to a buffer big enough.
15540 */
15541 *fname= *bufp = vim_strnsave(p, len);
15542 if (*fname == NULL)
15543 return -1;
15544 }
15545 else
15546 {
15547 /* Transfer pbuf2 to being the main buffer (it's big enough) */
15548 *fname = *bufp = pbuf2;
15549 if (p != pbuf2)
15550 strncpy(*fname, p, plen);
15551 pbuf2 = NULL;
15552 }
15553 /* Concat the next bit */
15554 strncpy(*fname + plen, s, slen);
15555 (*fname)[len] = '\0';
15556 }
15557 vim_free(pbuf3);
15558 vim_free(pbuf2);
15559 return 0;
15560}
15561
15562/*
15563 * Get a pathname for a partial path.
15564 */
15565 static int
15566shortpath_for_partial(fnamep, bufp, fnamelen)
15567 char_u **fnamep;
15568 char_u **bufp;
15569 int *fnamelen;
15570{
15571 int sepcount, len, tflen;
15572 char_u *p;
15573 char_u *pbuf, *tfname;
15574 int hasTilde;
15575
15576 /* Count up the path seperators from the RHS.. so we know which part
15577 * of the path to return.
15578 */
15579 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015580 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 if (vim_ispathsep(*p))
15582 ++sepcount;
15583
15584 /* Need full path first (use expand_env() to remove a "~/") */
15585 hasTilde = (**fnamep == '~');
15586 if (hasTilde)
15587 pbuf = tfname = expand_env_save(*fnamep);
15588 else
15589 pbuf = tfname = FullName_save(*fnamep, FALSE);
15590
15591 len = tflen = STRLEN(tfname);
15592
15593 if (!get_short_pathname(&tfname, &pbuf, &len))
15594 return -1;
15595
15596 if (len == 0)
15597 {
15598 /* Don't have a valid filename, so shorten the rest of the
15599 * path if we can. This CAN give us invalid 8.3 filenames, but
15600 * there's not a lot of point in guessing what it might be.
15601 */
15602 len = tflen;
15603 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
15604 return -1;
15605 }
15606
15607 /* Count the paths backward to find the beginning of the desired string. */
15608 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015609 {
15610#ifdef FEAT_MBYTE
15611 if (has_mbyte)
15612 p -= mb_head_off(tfname, p);
15613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 if (vim_ispathsep(*p))
15615 {
15616 if (sepcount == 0 || (hasTilde && sepcount == 1))
15617 break;
15618 else
15619 sepcount --;
15620 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 if (hasTilde)
15623 {
15624 --p;
15625 if (p >= tfname)
15626 *p = '~';
15627 else
15628 return -1;
15629 }
15630 else
15631 ++p;
15632
15633 /* Copy in the string - p indexes into tfname - allocated at pbuf */
15634 vim_free(*bufp);
15635 *fnamelen = (int)STRLEN(p);
15636 *bufp = pbuf;
15637 *fnamep = p;
15638
15639 return 0;
15640}
15641#endif /* WIN3264 */
15642
15643/*
15644 * Adjust a filename, according to a string of modifiers.
15645 * *fnamep must be NUL terminated when called. When returning, the length is
15646 * determined by *fnamelen.
15647 * Returns valid flags.
15648 * When there is an error, *fnamep is set to NULL.
15649 */
15650 int
15651modify_fname(src, usedlen, fnamep, bufp, fnamelen)
15652 char_u *src; /* string with modifiers */
15653 int *usedlen; /* characters after src that are used */
15654 char_u **fnamep; /* file name so far */
15655 char_u **bufp; /* buffer for allocated file name or NULL */
15656 int *fnamelen; /* length of fnamep */
15657{
15658 int valid = 0;
15659 char_u *tail;
15660 char_u *s, *p, *pbuf;
15661 char_u dirname[MAXPATHL];
15662 int c;
15663 int has_fullname = 0;
15664#ifdef WIN3264
15665 int has_shortname = 0;
15666#endif
15667
15668repeat:
15669 /* ":p" - full path/file_name */
15670 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
15671 {
15672 has_fullname = 1;
15673
15674 valid |= VALID_PATH;
15675 *usedlen += 2;
15676
15677 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
15678 if ((*fnamep)[0] == '~'
15679#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
15680 && ((*fnamep)[1] == '/'
15681# ifdef BACKSLASH_IN_FILENAME
15682 || (*fnamep)[1] == '\\'
15683# endif
15684 || (*fnamep)[1] == NUL)
15685
15686#endif
15687 )
15688 {
15689 *fnamep = expand_env_save(*fnamep);
15690 vim_free(*bufp); /* free any allocated file name */
15691 *bufp = *fnamep;
15692 if (*fnamep == NULL)
15693 return -1;
15694 }
15695
15696 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015697 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 {
15699 if (vim_ispathsep(*p)
15700 && p[1] == '.'
15701 && (p[2] == NUL
15702 || vim_ispathsep(p[2])
15703 || (p[2] == '.'
15704 && (p[3] == NUL || vim_ispathsep(p[3])))))
15705 break;
15706 }
15707
15708 /* FullName_save() is slow, don't use it when not needed. */
15709 if (*p != NUL || !vim_isAbsName(*fnamep))
15710 {
15711 *fnamep = FullName_save(*fnamep, *p != NUL);
15712 vim_free(*bufp); /* free any allocated file name */
15713 *bufp = *fnamep;
15714 if (*fnamep == NULL)
15715 return -1;
15716 }
15717
15718 /* Append a path separator to a directory. */
15719 if (mch_isdir(*fnamep))
15720 {
15721 /* Make room for one or two extra characters. */
15722 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
15723 vim_free(*bufp); /* free any allocated file name */
15724 *bufp = *fnamep;
15725 if (*fnamep == NULL)
15726 return -1;
15727 add_pathsep(*fnamep);
15728 }
15729 }
15730
15731 /* ":." - path relative to the current directory */
15732 /* ":~" - path relative to the home directory */
15733 /* ":8" - shortname path - postponed till after */
15734 while (src[*usedlen] == ':'
15735 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
15736 {
15737 *usedlen += 2;
15738 if (c == '8')
15739 {
15740#ifdef WIN3264
15741 has_shortname = 1; /* Postpone this. */
15742#endif
15743 continue;
15744 }
15745 pbuf = NULL;
15746 /* Need full path first (use expand_env() to remove a "~/") */
15747 if (!has_fullname)
15748 {
15749 if (c == '.' && **fnamep == '~')
15750 p = pbuf = expand_env_save(*fnamep);
15751 else
15752 p = pbuf = FullName_save(*fnamep, FALSE);
15753 }
15754 else
15755 p = *fnamep;
15756
15757 has_fullname = 0;
15758
15759 if (p != NULL)
15760 {
15761 if (c == '.')
15762 {
15763 mch_dirname(dirname, MAXPATHL);
15764 s = shorten_fname(p, dirname);
15765 if (s != NULL)
15766 {
15767 *fnamep = s;
15768 if (pbuf != NULL)
15769 {
15770 vim_free(*bufp); /* free any allocated file name */
15771 *bufp = pbuf;
15772 pbuf = NULL;
15773 }
15774 }
15775 }
15776 else
15777 {
15778 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
15779 /* Only replace it when it starts with '~' */
15780 if (*dirname == '~')
15781 {
15782 s = vim_strsave(dirname);
15783 if (s != NULL)
15784 {
15785 *fnamep = s;
15786 vim_free(*bufp);
15787 *bufp = s;
15788 }
15789 }
15790 }
15791 vim_free(pbuf);
15792 }
15793 }
15794
15795 tail = gettail(*fnamep);
15796 *fnamelen = (int)STRLEN(*fnamep);
15797
15798 /* ":h" - head, remove "/file_name", can be repeated */
15799 /* Don't remove the first "/" or "c:\" */
15800 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
15801 {
15802 valid |= VALID_HEAD;
15803 *usedlen += 2;
15804 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015805 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 --tail;
15807 *fnamelen = (int)(tail - *fnamep);
15808#ifdef VMS
15809 if (*fnamelen > 0)
15810 *fnamelen += 1; /* the path separator is part of the path */
15811#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015812 while (tail > s && !after_pathsep(s, tail))
15813 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 }
15815
15816 /* ":8" - shortname */
15817 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
15818 {
15819 *usedlen += 2;
15820#ifdef WIN3264
15821 has_shortname = 1;
15822#endif
15823 }
15824
15825#ifdef WIN3264
15826 /* Check shortname after we have done 'heads' and before we do 'tails'
15827 */
15828 if (has_shortname)
15829 {
15830 pbuf = NULL;
15831 /* Copy the string if it is shortened by :h */
15832 if (*fnamelen < (int)STRLEN(*fnamep))
15833 {
15834 p = vim_strnsave(*fnamep, *fnamelen);
15835 if (p == 0)
15836 return -1;
15837 vim_free(*bufp);
15838 *bufp = *fnamep = p;
15839 }
15840
15841 /* Split into two implementations - makes it easier. First is where
15842 * there isn't a full name already, second is where there is.
15843 */
15844 if (!has_fullname && !vim_isAbsName(*fnamep))
15845 {
15846 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
15847 return -1;
15848 }
15849 else
15850 {
15851 int l;
15852
15853 /* Simple case, already have the full-name
15854 * Nearly always shorter, so try first time. */
15855 l = *fnamelen;
15856 if (!get_short_pathname(fnamep, bufp, &l))
15857 return -1;
15858
15859 if (l == 0)
15860 {
15861 /* Couldn't find the filename.. search the paths.
15862 */
15863 l = *fnamelen;
15864 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
15865 return -1;
15866 }
15867 *fnamelen = l;
15868 }
15869 }
15870#endif /* WIN3264 */
15871
15872 /* ":t" - tail, just the basename */
15873 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
15874 {
15875 *usedlen += 2;
15876 *fnamelen -= (int)(tail - *fnamep);
15877 *fnamep = tail;
15878 }
15879
15880 /* ":e" - extension, can be repeated */
15881 /* ":r" - root, without extension, can be repeated */
15882 while (src[*usedlen] == ':'
15883 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
15884 {
15885 /* find a '.' in the tail:
15886 * - for second :e: before the current fname
15887 * - otherwise: The last '.'
15888 */
15889 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
15890 s = *fnamep - 2;
15891 else
15892 s = *fnamep + *fnamelen - 1;
15893 for ( ; s > tail; --s)
15894 if (s[0] == '.')
15895 break;
15896 if (src[*usedlen + 1] == 'e') /* :e */
15897 {
15898 if (s > tail)
15899 {
15900 *fnamelen += (int)(*fnamep - (s + 1));
15901 *fnamep = s + 1;
15902#ifdef VMS
15903 /* cut version from the extension */
15904 s = *fnamep + *fnamelen - 1;
15905 for ( ; s > *fnamep; --s)
15906 if (s[0] == ';')
15907 break;
15908 if (s > *fnamep)
15909 *fnamelen = s - *fnamep;
15910#endif
15911 }
15912 else if (*fnamep <= tail)
15913 *fnamelen = 0;
15914 }
15915 else /* :r */
15916 {
15917 if (s > tail) /* remove one extension */
15918 *fnamelen = (int)(s - *fnamep);
15919 }
15920 *usedlen += 2;
15921 }
15922
15923 /* ":s?pat?foo?" - substitute */
15924 /* ":gs?pat?foo?" - global substitute */
15925 if (src[*usedlen] == ':'
15926 && (src[*usedlen + 1] == 's'
15927 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
15928 {
15929 char_u *str;
15930 char_u *pat;
15931 char_u *sub;
15932 int sep;
15933 char_u *flags;
15934 int didit = FALSE;
15935
15936 flags = (char_u *)"";
15937 s = src + *usedlen + 2;
15938 if (src[*usedlen + 1] == 'g')
15939 {
15940 flags = (char_u *)"g";
15941 ++s;
15942 }
15943
15944 sep = *s++;
15945 if (sep)
15946 {
15947 /* find end of pattern */
15948 p = vim_strchr(s, sep);
15949 if (p != NULL)
15950 {
15951 pat = vim_strnsave(s, (int)(p - s));
15952 if (pat != NULL)
15953 {
15954 s = p + 1;
15955 /* find end of substitution */
15956 p = vim_strchr(s, sep);
15957 if (p != NULL)
15958 {
15959 sub = vim_strnsave(s, (int)(p - s));
15960 str = vim_strnsave(*fnamep, *fnamelen);
15961 if (sub != NULL && str != NULL)
15962 {
15963 *usedlen = (int)(p + 1 - src);
15964 s = do_string_sub(str, pat, sub, flags);
15965 if (s != NULL)
15966 {
15967 *fnamep = s;
15968 *fnamelen = (int)STRLEN(s);
15969 vim_free(*bufp);
15970 *bufp = s;
15971 didit = TRUE;
15972 }
15973 }
15974 vim_free(sub);
15975 vim_free(str);
15976 }
15977 vim_free(pat);
15978 }
15979 }
15980 /* after using ":s", repeat all the modifiers */
15981 if (didit)
15982 goto repeat;
15983 }
15984 }
15985
15986 return valid;
15987}
15988
15989/*
15990 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
15991 * "flags" can be "g" to do a global substitute.
15992 * Returns an allocated string, NULL for error.
15993 */
15994 char_u *
15995do_string_sub(str, pat, sub, flags)
15996 char_u *str;
15997 char_u *pat;
15998 char_u *sub;
15999 char_u *flags;
16000{
16001 int sublen;
16002 regmatch_T regmatch;
16003 int i;
16004 int do_all;
16005 char_u *tail;
16006 garray_T ga;
16007 char_u *ret;
16008 char_u *save_cpo;
16009
16010 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
16011 save_cpo = p_cpo;
16012 p_cpo = (char_u *)"";
16013
16014 ga_init2(&ga, 1, 200);
16015
16016 do_all = (flags[0] == 'g');
16017
16018 regmatch.rm_ic = p_ic;
16019 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16020 if (regmatch.regprog != NULL)
16021 {
16022 tail = str;
16023 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
16024 {
16025 /*
16026 * Get some space for a temporary buffer to do the substitution
16027 * into. It will contain:
16028 * - The text up to where the match is.
16029 * - The substituted text.
16030 * - The text after the match.
16031 */
16032 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
16033 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
16034 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
16035 {
16036 ga_clear(&ga);
16037 break;
16038 }
16039
16040 /* copy the text up to where the match is */
16041 i = (int)(regmatch.startp[0] - tail);
16042 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
16043 /* add the substituted text */
16044 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
16045 + ga.ga_len + i, TRUE, TRUE, FALSE);
16046 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 /* avoid getting stuck on a match with an empty string */
16048 if (tail == regmatch.endp[0])
16049 {
16050 if (*tail == NUL)
16051 break;
16052 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
16053 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 }
16055 else
16056 {
16057 tail = regmatch.endp[0];
16058 if (*tail == NUL)
16059 break;
16060 }
16061 if (!do_all)
16062 break;
16063 }
16064
16065 if (ga.ga_data != NULL)
16066 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
16067
16068 vim_free(regmatch.regprog);
16069 }
16070
16071 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
16072 ga_clear(&ga);
16073 p_cpo = save_cpo;
16074
16075 return ret;
16076}
16077
16078#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */