blob: a5a076f50cb4bb919e6503dcf95002c8572f9eb6 [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
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000109
110/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000111 * All user-defined global variables are stored in dictionary "globvardict".
112 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000114static dict_T globvardict;
115static dictitem_T globvars_var;
116#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
118/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000119 * Old Vim variables such as "v:version" are also available without the "v:".
120 * Also in functions. We need a special hashtable for them.
121 */
122hashtab_T compat_hashtab;
123
124/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000125 * Array to hold the hashtab with variables local to each sourced script.
126 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000128typedef struct
129{
130 dictitem_T sv_var;
131 dict_T sv_dict;
132} scriptvar_T;
133
134static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
135#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
136#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
138static int echo_attr = 0; /* attributes used for ":echo" */
139
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000140/* Values for trans_function_name() argument: */
141#define TFN_INT 1 /* internal function name OK */
142#define TFN_QUIET 2 /* no error messages */
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
145 * Structure to hold info for a user function.
146 */
147typedef struct ufunc ufunc_T;
148
149struct ufunc
150{
151 ufunc_T *next; /* next function in list */
152 char_u *name; /* name of function; can start with <SNR>123_
153 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
154 int varargs; /* variable nr of arguments */
155 int flags;
156 int calls; /* nr of active calls */
157 garray_T args; /* arguments */
158 garray_T lines; /* function lines */
159 scid_T script_ID; /* ID of script where function was defined,
160 used for s: variables */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161 int refcount; /* for numbered function: reference count */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162};
163
164/* function flags */
165#define FC_ABORT 1 /* abort function on error */
166#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000167#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169/*
170 * All user-defined functions are found in the forward-linked function list.
171 * The first function is pointed at by firstfunc.
172 */
173ufunc_T *firstfunc = NULL;
174
175#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
176#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
177
Bram Moolenaar33570922005-01-25 22:26:29 +0000178#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
179#define VAR_SHORT_LEN 20 /* short variable name length */
180#define FIXVAR_CNT 12 /* number of fixed variables */
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000183typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
185 ufunc_T *func; /* function being called */
186 int linenr; /* next line to be executed */
187 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000188 struct /* fixed variables for arguments */
189 {
190 dictitem_T var; /* variable (without room for name) */
191 char_u room[VAR_SHORT_LEN]; /* room for the name */
192 } fixvar[FIXVAR_CNT];
193 dict_T l_vars; /* l: local function variables */
194 dictitem_T l_vars_var; /* variable for l: scope */
195 dict_T l_avars; /* a: argument variables */
196 dictitem_T l_avars_var; /* variable for a: scope */
197 list_T l_varlist; /* list for a:000 */
198 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
199 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 linenr_T breakpoint; /* next line with breakpoint or zero */
201 int dbg_tick; /* debug_tick when breakpoint was set */
202 int level; /* top nesting level of executed function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000203} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000206 * Info used by a ":for" loop.
207 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000208typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000209{
210 int fi_semicolon; /* TRUE if ending in '; var]' */
211 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 listwatch_T fi_lw; /* keep an eye on the item used. */
213 list_T *fi_list; /* list being used */
214} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000215
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000216/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000217 * Struct used by trans_function_name()
218 */
219typedef struct
220{
Bram Moolenaar33570922005-01-25 22:26:29 +0000221 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000222 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000223 dictitem_T *fd_di; /* Dictionary item used */
224} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000225
Bram Moolenaara7043832005-01-21 11:56:39 +0000226
227/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000228 * Array to hold the value of v: variables.
229 * The value is in a dictitem, so that it can also be used in the v: scope.
230 * The reason to use this table anyway is for very quick access to the
231 * variables with the VV_ defines.
232 */
233#include "version.h"
234
235/* values for vv_flags: */
236#define VV_COMPAT 1 /* compatible, also used without "v:" */
237#define VV_RO 2 /* read-only */
238#define VV_RO_SBX 4 /* read-only in the sandbox*/
239
240#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
241
242static struct vimvar
243{
244 char *vv_name; /* name of variable, without v: */
245 int vv_len; /* length of name */
246 dictitem_T vv_di; /* value and name for key */
247 char vv_filler[16]; /* space for LONGEST name below!!! */
248 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
249} vimvars[VV_LEN] =
250{
251 /*
252 * The order here must match the VV_ defines in vim.h!
253 * Initializing a union does not work, leave tv.vval empty to get zero's.
254 */
255 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
256 {VV_NAME("count1", VAR_NUMBER), VV_RO},
257 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
258 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
259 {VV_NAME("warningmsg", VAR_STRING), 0},
260 {VV_NAME("statusmsg", VAR_STRING), 0},
261 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
262 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
263 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
264 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
265 {VV_NAME("termresponse", VAR_STRING), VV_RO},
266 {VV_NAME("fname", VAR_STRING), VV_RO},
267 {VV_NAME("lang", VAR_STRING), VV_RO},
268 {VV_NAME("lc_time", VAR_STRING), VV_RO},
269 {VV_NAME("ctype", VAR_STRING), VV_RO},
270 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
271 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
272 {VV_NAME("fname_in", VAR_STRING), VV_RO},
273 {VV_NAME("fname_out", VAR_STRING), VV_RO},
274 {VV_NAME("fname_new", VAR_STRING), VV_RO},
275 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
276 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
277 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
278 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
279 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
280 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
281 {VV_NAME("progname", VAR_STRING), VV_RO},
282 {VV_NAME("servername", VAR_STRING), VV_RO},
283 {VV_NAME("dying", VAR_NUMBER), VV_RO},
284 {VV_NAME("exception", VAR_STRING), VV_RO},
285 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
286 {VV_NAME("register", VAR_STRING), VV_RO},
287 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
288 {VV_NAME("insertmode", VAR_STRING), VV_RO},
289 {VV_NAME("val", VAR_STRING), VV_RO},
290 {VV_NAME("key", VAR_STRING), VV_RO},
291};
292
293/* shorthand */
294#define vv_type vv_di.di_tv.v_type
295#define vv_nr vv_di.di_tv.vval.v_number
296#define vv_str vv_di.di_tv.vval.v_string
297#define vv_tv vv_di.di_tv
298
299/*
300 * The v: variables are stored in dictionary "vimvardict".
301 * "vimvars_var" is the variable that is used for the "l:" scope.
302 */
303static dict_T vimvardict;
304static dictitem_T vimvars_var;
305#define vimvarht vimvardict.dv_hashtab
306
307static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
308static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
309static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
310static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
311static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
312static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
313static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
314static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
315static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate));
316static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
317static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
318static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
319static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
320static list_T *list_alloc __ARGS((void));
321static void list_unref __ARGS((list_T *l));
322static void list_free __ARGS((list_T *l));
323static listitem_T *listitem_alloc __ARGS((void));
324static void listitem_free __ARGS((listitem_T *item));
325static void listitem_remove __ARGS((list_T *l, listitem_T *item));
326static long list_len __ARGS((list_T *l));
327static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
328static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
329static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
330static int string_isa_number __ARGS((char_u *s));
331static listitem_T *list_find __ARGS((list_T *l, long n));
332static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
333static listitem_T *list_find_ext __ARGS((list_T *l, long *ip));
334static void list_append __ARGS((list_T *l, listitem_T *item));
335static int list_append_tv __ARGS((list_T *l, typval_T *tv));
336static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
337static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
338static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
339static list_T *list_copy __ARGS((list_T *orig, int deep));
340static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
341static char_u *list2string __ARGS((typval_T *tv));
342static void list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
343
344static dict_T *dict_alloc __ARGS((void));
345static void dict_unref __ARGS((dict_T *d));
346static void dict_free __ARGS((dict_T *d));
347static dictitem_T *dictitem_alloc __ARGS((char_u *key));
348static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
349static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
350static void dictitem_free __ARGS((dictitem_T *item));
351static int dict_add __ARGS((dict_T *d, dictitem_T *item));
352static long dict_len __ARGS((dict_T *d));
353static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
354static char_u *dict2string __ARGS((typval_T *tv));
355static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
356
357static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
358static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
359static char_u *string_quote __ARGS((char_u *str, int function));
360static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
361static int find_internal_func __ARGS((char_u *name));
362static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
363static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
364static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
365
366static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
367static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
368static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
369static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
370static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
371static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
372static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
373static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
374static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
375static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
376static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
377static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
378static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
379static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
380static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
381static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
382static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
383static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
384static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
385static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
386static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
387static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
388static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
389static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
390static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
391static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
392static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
393static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
394static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
395static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
396static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
397static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
398static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
399static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
400static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000456static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
503#ifdef HAVE_STRFTIME
504static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
506static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
533
534static win_T *find_win_by_nr __ARGS((typval_T *vp));
535static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
536static int get_env_len __ARGS((char_u **arg));
537static int get_id_len __ARGS((char_u **arg));
538static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate));
539static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
540static int eval_isnamec __ARGS((int c));
541static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv));
542static typval_T *alloc_tv __ARGS((void));
543static typval_T *alloc_string_tv __ARGS((char_u *string));
544static void free_tv __ARGS((typval_T *varp));
545static void clear_tv __ARGS((typval_T *varp));
546static void init_tv __ARGS((typval_T *varp));
547static long get_tv_number __ARGS((typval_T *varp));
548static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
549static char_u *get_tv_string __ARGS((typval_T *varp));
550static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
551static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
552static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname));
553static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
554static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
555static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
556static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
557static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
558static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
559static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000560static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void copy_tv __ARGS((typval_T *from, typval_T *to));
562static void item_copy __ARGS((typval_T *from, typval_T *to, int deep));
563static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
564static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
565static int eval_fname_script __ARGS((char_u *p));
566static int eval_fname_sid __ARGS((char_u *p));
567static void list_func_head __ARGS((ufunc_T *fp, int indent));
568static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
569static ufunc_T *find_func __ARGS((char_u *name));
570static int function_exists __ARGS((char_u *name));
571static void func_free __ARGS((ufunc_T *fp));
572static void func_unref __ARGS((char_u *name));
573static void func_ref __ARGS((char_u *name));
574static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
575static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
576
577static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
578
579static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
580static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
581static char_u *skip_var_one __ARGS((char_u *arg));
582static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
583static void list_glob_vars __ARGS((void));
584static void list_buf_vars __ARGS((void));
585static void list_win_vars __ARGS((void));
586static void list_vim_vars __ARGS((void));
587static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
588static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
589static int check_changedtick __ARGS((char_u *arg));
590static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
591static void clear_lval __ARGS((lval_T *lp));
592static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
593static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
594static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
595static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
596static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000597static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000599static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
600static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601
602/*
603 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000604 */
605 void
606eval_init()
607{
Bram Moolenaar33570922005-01-25 22:26:29 +0000608 int i;
609 struct vimvar *p;
610
611 init_var_dict(&globvardict, &globvars_var);
612 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000613 hash_init(&compat_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000614
615 for (i = 0; i < VV_LEN; ++i)
616 {
617 p = &vimvars[i];
618 STRCPY(p->vv_di.di_key, p->vv_name);
619 if (p->vv_flags & VV_RO)
620 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
621 else if (p->vv_flags & VV_RO_SBX)
622 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
623 else
624 p->vv_di.di_flags = DI_FLAGS_FIX;
625 /* add to v: scope dict */
626 hash_add(&vimvarht, p->vv_di.di_key);
627 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000628 /* add to compat scope dict */
629 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000630 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000631}
632
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 * Return the name of the executed function.
635 */
636 char_u *
637func_name(cookie)
638 void *cookie;
639{
Bram Moolenaar33570922005-01-25 22:26:29 +0000640 return ((funccall_T *)cookie)->func->name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641}
642
643/*
644 * Return the address holding the next breakpoint line for a funccall cookie.
645 */
646 linenr_T *
647func_breakpoint(cookie)
648 void *cookie;
649{
Bram Moolenaar33570922005-01-25 22:26:29 +0000650 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651}
652
653/*
654 * Return the address holding the debug tick for a funccall cookie.
655 */
656 int *
657func_dbg_tick(cookie)
658 void *cookie;
659{
Bram Moolenaar33570922005-01-25 22:26:29 +0000660 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661}
662
663/*
664 * Return the nesting level for a funccall cookie.
665 */
666 int
667func_level(cookie)
668 void *cookie;
669{
Bram Moolenaar33570922005-01-25 22:26:29 +0000670 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671}
672
673/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000674funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676/*
677 * Return TRUE when a function was ended by a ":return" command.
678 */
679 int
680current_func_returned()
681{
682 return current_funccal->returned;
683}
684
685
686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 * Set an internal variable to a string value. Creates the variable if it does
688 * not already exist.
689 */
690 void
691set_internal_string_var(name, value)
692 char_u *name;
693 char_u *value;
694{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000695 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 val = vim_strsave(value);
699 if (val != NULL)
700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000701 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000702 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000704 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 }
708}
709
710# if defined(FEAT_MBYTE) || defined(PROTO)
711 int
712eval_charconvert(enc_from, enc_to, fname_from, fname_to)
713 char_u *enc_from;
714 char_u *enc_to;
715 char_u *fname_from;
716 char_u *fname_to;
717{
718 int err = FALSE;
719
720 set_vim_var_string(VV_CC_FROM, enc_from, -1);
721 set_vim_var_string(VV_CC_TO, enc_to, -1);
722 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
723 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
724 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
725 err = TRUE;
726 set_vim_var_string(VV_CC_FROM, NULL, -1);
727 set_vim_var_string(VV_CC_TO, NULL, -1);
728 set_vim_var_string(VV_FNAME_IN, NULL, -1);
729 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
730
731 if (err)
732 return FAIL;
733 return OK;
734}
735# endif
736
737# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
738 int
739eval_printexpr(fname, args)
740 char_u *fname;
741 char_u *args;
742{
743 int err = FALSE;
744
745 set_vim_var_string(VV_FNAME_IN, fname, -1);
746 set_vim_var_string(VV_CMDARG, args, -1);
747 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
748 err = TRUE;
749 set_vim_var_string(VV_FNAME_IN, NULL, -1);
750 set_vim_var_string(VV_CMDARG, NULL, -1);
751
752 if (err)
753 {
754 mch_remove(fname);
755 return FAIL;
756 }
757 return OK;
758}
759# endif
760
761# if defined(FEAT_DIFF) || defined(PROTO)
762 void
763eval_diff(origfile, newfile, outfile)
764 char_u *origfile;
765 char_u *newfile;
766 char_u *outfile;
767{
768 int err = FALSE;
769
770 set_vim_var_string(VV_FNAME_IN, origfile, -1);
771 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
772 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
773 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
774 set_vim_var_string(VV_FNAME_IN, NULL, -1);
775 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
776 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
777}
778
779 void
780eval_patch(origfile, difffile, outfile)
781 char_u *origfile;
782 char_u *difffile;
783 char_u *outfile;
784{
785 int err;
786
787 set_vim_var_string(VV_FNAME_IN, origfile, -1);
788 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
789 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
790 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
791 set_vim_var_string(VV_FNAME_IN, NULL, -1);
792 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
793 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
794}
795# endif
796
797/*
798 * Top level evaluation function, returning a boolean.
799 * Sets "error" to TRUE if there was an error.
800 * Return TRUE or FALSE.
801 */
802 int
803eval_to_bool(arg, error, nextcmd, skip)
804 char_u *arg;
805 int *error;
806 char_u **nextcmd;
807 int skip; /* only parse, don't execute */
808{
Bram Moolenaar33570922005-01-25 22:26:29 +0000809 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 int retval = FALSE;
811
812 if (skip)
813 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 else
817 {
818 *error = FALSE;
819 if (!skip)
820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 retval = (get_tv_number(&tv) != 0);
822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824 }
825 if (skip)
826 --emsg_skip;
827
828 return retval;
829}
830
831/*
832 * Top level evaluation function, returning a string. If "skip" is TRUE,
833 * only parsing to "nextcmd" is done, without reporting errors. Return
834 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
835 */
836 char_u *
837eval_to_string_skip(arg, nextcmd, skip)
838 char_u *arg;
839 char_u **nextcmd;
840 int skip; /* only parse, don't execute */
841{
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 char_u *retval;
844
845 if (skip)
846 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 retval = NULL;
849 else
850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 retval = vim_strsave(get_tv_string(&tv));
852 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 if (skip)
855 --emsg_skip;
856
857 return retval;
858}
859
860/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000861 * Skip over an expression at "*pp".
862 * Return FAIL for an error, OK otherwise.
863 */
864 int
865skip_expr(pp)
866 char_u **pp;
867{
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000869
870 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000872}
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Top level evaluation function, returning a string.
876 * Return pointer to allocated memory, or NULL for failure.
877 */
878 char_u *
879eval_to_string(arg, nextcmd)
880 char_u *arg;
881 char_u **nextcmd;
882{
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 char_u *retval;
885
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000886 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 retval = NULL;
888 else
889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 retval = vim_strsave(get_tv_string(&tv));
891 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 }
893
894 return retval;
895}
896
897/*
898 * Call eval_to_string() with "sandbox" set and not using local variables.
899 */
900 char_u *
901eval_to_string_safe(arg, nextcmd)
902 char_u *arg;
903 char_u **nextcmd;
904{
905 char_u *retval;
906 void *save_funccalp;
907
908 save_funccalp = save_funccal();
909 ++sandbox;
910 retval = eval_to_string(arg, nextcmd);
911 --sandbox;
912 restore_funccal(save_funccalp);
913 return retval;
914}
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916/*
917 * Top level evaluation function, returning a number.
918 * Evaluates "expr" silently.
919 * Returns -1 for an error.
920 */
921 int
922eval_to_number(expr)
923 char_u *expr;
924{
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000927 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 ++emsg_off;
930
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000931 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 retval = -1;
933 else
934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000935 retval = get_tv_number(&rettv);
936 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
938 --emsg_off;
939
940 return retval;
941}
942
943#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
944/*
945 * Call some vimL function and return the result as a string
946 * Uses argv[argc] for the function arguments.
947 */
948 char_u *
949call_vim_function(func, argc, argv, safe)
950 char_u *func;
951 int argc;
952 char_u **argv;
953 int safe; /* use the sandbox */
954{
955 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 typval_T rettv;
957 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 long n;
959 int len;
960 int i;
961 int doesrange;
962 void *save_funccalp = NULL;
963
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (argvars == NULL)
966 return NULL;
967
968 for (i = 0; i < argc; i++)
969 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000970 /* Pass a NULL or empty argument as an empty string */
971 if (argv[i] == NULL || *argv[i] == NUL)
972 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000973 argvars[i].v_type = VAR_STRING;
974 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000975 continue;
976 }
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 /* Recognize a number argument, the others must be strings. */
979 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
980 if (len != 0 && len == (int)STRLEN(argv[i]))
981 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000982 argvars[i].v_type = VAR_NUMBER;
983 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985 else
986 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000987 argvars[i].v_type = VAR_STRING;
988 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 }
991
992 if (safe)
993 {
994 save_funccalp = save_funccal();
995 ++sandbox;
996 }
997
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000998 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
999 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001001 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001004 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 vim_free(argvars);
1006
1007 if (safe)
1008 {
1009 --sandbox;
1010 restore_funccal(save_funccalp);
1011 }
1012 return retval;
1013}
1014#endif
1015
1016/*
1017 * Save the current function call pointer, and set it to NULL.
1018 * Used when executing autocommands and for ":source".
1019 */
1020 void *
1021save_funccal()
1022{
Bram Moolenaar33570922005-01-25 22:26:29 +00001023 funccall_T *fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
1025 fc = current_funccal;
1026 current_funccal = NULL;
1027 return (void *)fc;
1028}
1029
1030 void
1031restore_funccal(fc)
1032 void *fc;
1033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 current_funccal = (funccall_T *)fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037#ifdef FEAT_FOLDING
1038/*
1039 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1040 * it in "*cp". Doesn't give error messages.
1041 */
1042 int
1043eval_foldexpr(arg, cp)
1044 char_u *arg;
1045 int *cp;
1046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 int retval;
1049 char_u *s;
1050
1051 ++emsg_off;
1052 ++sandbox;
1053 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 retval = 0;
1056 else
1057 {
1058 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 if (tv.v_type == VAR_NUMBER)
1060 retval = tv.vval.v_number;
1061 else if (tv.v_type == VAR_UNKNOWN
1062 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 retval = 0;
1064 else
1065 {
1066 /* If the result is a string, check if there is a non-digit before
1067 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001068 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (!VIM_ISDIGIT(*s) && *s != '-')
1070 *cp = *s++;
1071 retval = atol((char *)s);
1072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001073 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 }
1075 --emsg_off;
1076 --sandbox;
1077
1078 return retval;
1079}
1080#endif
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
1083 * Expands out the 'magic' {}'s in a variable/function name.
1084 * Note that this can call itself recursively, to deal with
1085 * constructs like foo{bar}{baz}{bam}
1086 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1087 * "in_start" ^
1088 * "expr_start" ^
1089 * "expr_end" ^
1090 * "in_end" ^
1091 *
1092 * Returns a new allocated string, which the caller must free.
1093 * Returns NULL for failure.
1094 */
1095 static char_u *
1096make_expanded_name(in_start, expr_start, expr_end, in_end)
1097 char_u *in_start;
1098 char_u *expr_start;
1099 char_u *expr_end;
1100 char_u *in_end;
1101{
1102 char_u c1;
1103 char_u *retval = NULL;
1104 char_u *temp_result;
1105 char_u *nextcmd = NULL;
1106
1107 if (expr_end == NULL || in_end == NULL)
1108 return NULL;
1109 *expr_start = NUL;
1110 *expr_end = NUL;
1111 c1 = *in_end;
1112 *in_end = NUL;
1113
1114 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1115 if (temp_result != NULL && nextcmd == NULL)
1116 {
1117 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1118 + (in_end - expr_end) + 1));
1119
1120 if (retval != NULL)
1121 {
1122 STRCPY(retval, in_start);
1123 STRCAT(retval, temp_result);
1124 STRCAT(retval, expr_end + 1);
1125 }
1126 }
1127 vim_free(temp_result);
1128
1129 *in_end = c1; /* put char back for error messages */
1130 *expr_start = '{';
1131 *expr_end = '}';
1132
1133 if (retval != NULL)
1134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (expr_start != NULL)
1137 {
1138 /* Further expansion! */
1139 temp_result = make_expanded_name(retval, expr_start,
1140 expr_end, temp_result);
1141 vim_free(retval);
1142 retval = temp_result;
1143 }
1144 }
1145
1146 return retval;
1147
1148}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 * ":let" list all variable values
1152 * ":let var1 var2" list variable values
1153 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001154 * ":let var += expr" assignment command.
1155 * ":let var -= expr" assignment command.
1156 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 */
1159 void
1160ex_let(eap)
1161 exarg_T *eap;
1162{
1163 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001165 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 int var_count = 0;
1168 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001169 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001171 expr = skip_var_list(arg, &var_count, &semicolon);
1172 if (expr == NULL)
1173 return;
1174 expr = vim_strchr(expr, '=');
1175 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001177 /*
1178 * ":let" without "=": list variables
1179 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001180 if (*arg == '[')
1181 EMSG(_(e_invarg));
1182 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 /* ":let var1 var2" */
1184 arg = list_arg_vars(eap, arg);
1185 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001188 list_glob_vars();
1189 list_buf_vars();
1190 list_win_vars();
1191 list_vim_vars();
1192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 eap->nextcmd = check_nextcmd(arg);
1194 }
1195 else
1196 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001197 op[0] = '=';
1198 op[1] = NUL;
1199 if (expr > arg)
1200 {
1201 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1202 op[0] = expr[-1]; /* +=, -= or .= */
1203 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001204 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (eap->skip)
1207 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001208 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (eap->skip)
1210 {
1211 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 --emsg_skip;
1214 }
1215 else if (i != FAIL)
1216 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001217 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 }
1222}
1223
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001224/*
1225 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1226 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 * When "nextchars" is not NULL it points to a string with characters that
1228 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1229 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001230 * Returns OK or FAIL;
1231 */
1232 static int
1233ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1234 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001235 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001236 int copy; /* copy values from "tv", don't move */
1237 int semicolon; /* from skip_var_list() */
1238 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001239 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001240{
1241 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001242 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001243 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001244 listitem_T *item;
1245 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001246
1247 if (*arg != '[')
1248 {
1249 /*
1250 * ":let var = expr" or ":for var in list"
1251 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001252 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001253 return FAIL;
1254 return OK;
1255 }
1256
1257 /*
1258 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1259 */
1260 l = tv->vval.v_list;
1261 if (tv->v_type != VAR_LIST || l == NULL)
1262 {
1263 EMSG(_(e_listreq));
1264 return FAIL;
1265 }
1266
1267 i = list_len(l);
1268 if (semicolon == 0 && var_count < i)
1269 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001270 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271 return FAIL;
1272 }
1273 if (var_count - semicolon > i)
1274 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001275 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001276 return FAIL;
1277 }
1278
1279 item = l->lv_first;
1280 while (*arg != ']')
1281 {
1282 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001284 item = item->li_next;
1285 if (arg == NULL)
1286 return FAIL;
1287
1288 arg = skipwhite(arg);
1289 if (*arg == ';')
1290 {
1291 /* Put the rest of the list (may be empty) in the var after ';'.
1292 * Create a new list for this. */
1293 l = list_alloc();
1294 if (l == NULL)
1295 return FAIL;
1296 while (item != NULL)
1297 {
1298 list_append_tv(l, &item->li_tv);
1299 item = item->li_next;
1300 }
1301
1302 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001303 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001304 ltv.vval.v_list = l;
1305 l->lv_refcount = 1;
1306
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001307 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1308 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001309 clear_tv(&ltv);
1310 if (arg == NULL)
1311 return FAIL;
1312 break;
1313 }
1314 else if (*arg != ',' && *arg != ']')
1315 {
1316 EMSG2(_(e_intern2), "ex_let_vars()");
1317 return FAIL;
1318 }
1319 }
1320
1321 return OK;
1322}
1323
1324/*
1325 * Skip over assignable variable "var" or list of variables "[var, var]".
1326 * Used for ":let varvar = expr" and ":for varvar in expr".
1327 * For "[var, var]" increment "*var_count" for each variable.
1328 * for "[var, var; var]" set "semicolon".
1329 * Return NULL for an error.
1330 */
1331 static char_u *
1332skip_var_list(arg, var_count, semicolon)
1333 char_u *arg;
1334 int *var_count;
1335 int *semicolon;
1336{
1337 char_u *p, *s;
1338
1339 if (*arg == '[')
1340 {
1341 /* "[var, var]": find the matching ']'. */
1342 p = arg;
1343 while (1)
1344 {
1345 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1346 s = skip_var_one(p);
1347 if (s == p)
1348 {
1349 EMSG2(_(e_invarg2), p);
1350 return NULL;
1351 }
1352 ++*var_count;
1353
1354 p = skipwhite(s);
1355 if (*p == ']')
1356 break;
1357 else if (*p == ';')
1358 {
1359 if (*semicolon == 1)
1360 {
1361 EMSG(_("Double ; in list of variables"));
1362 return NULL;
1363 }
1364 *semicolon = 1;
1365 }
1366 else if (*p != ',')
1367 {
1368 EMSG2(_(e_invarg2), p);
1369 return NULL;
1370 }
1371 }
1372 return p + 1;
1373 }
1374 else
1375 return skip_var_one(arg);
1376}
1377
1378 static char_u *
1379skip_var_one(arg)
1380 char_u *arg;
1381{
1382 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1383 ++arg;
1384 return find_name_end(arg, NULL, NULL, TRUE);
1385}
1386
Bram Moolenaara7043832005-01-21 11:56:39 +00001387/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001388 * List variables for hashtab "ht" with prefix "prefix".
1389 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001392list_hashtable_vars(ht, prefix, empty)
1393 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001394 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001395 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 hashitem_T *hi;
1398 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001399 int todo;
1400
1401 todo = ht->ht_used;
1402 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1403 {
1404 if (!HASHITEM_EMPTY(hi))
1405 {
1406 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 di = HI2DI(hi);
1408 if (empty || di->di_tv.v_type != VAR_STRING
1409 || di->di_tv.vval.v_string != NULL)
1410 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001411 }
1412 }
1413}
1414
1415/*
1416 * List global variables.
1417 */
1418 static void
1419list_glob_vars()
1420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001422}
1423
1424/*
1425 * List buffer variables.
1426 */
1427 static void
1428list_buf_vars()
1429{
Bram Moolenaar33570922005-01-25 22:26:29 +00001430 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001431}
1432
1433/*
1434 * List window variables.
1435 */
1436 static void
1437list_win_vars()
1438{
Bram Moolenaar33570922005-01-25 22:26:29 +00001439 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001440}
1441
1442/*
1443 * List Vim variables.
1444 */
1445 static void
1446list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447{
Bram Moolenaar33570922005-01-25 22:26:29 +00001448 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449}
1450
1451/*
1452 * List variables in "arg".
1453 */
1454 static char_u *
1455list_arg_vars(eap, arg)
1456 exarg_T *eap;
1457 char_u *arg;
1458{
1459 int error = FALSE;
1460 char_u *temp_string = NULL;
1461 int arg_len;
1462 char_u *expr_start;
1463 char_u *expr_end;
1464 char_u *name_end;
1465 int c1 = 0, c2;
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 dictitem_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467 char_u *name;
1468
1469 while (!ends_excmd(*arg) && !got_int)
1470 {
1471 /* Find the end of the name. */
1472 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1473
1474 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1475 {
1476 emsg_severe = TRUE;
1477 EMSG(_(e_trailing));
1478 break;
1479 }
1480 if (!error && !eap->skip)
1481 {
1482 if (expr_start != NULL)
1483 {
1484 temp_string = make_expanded_name(arg, expr_start,
1485 expr_end, name_end);
1486 if (temp_string == NULL)
1487 {
1488 /*
1489 * Report an invalid expression in braces, unless
1490 * the expression evaluation has been cancelled due
1491 * to an aborting error, an interrupt, or an
1492 * exception.
1493 */
1494 if (!aborting())
1495 {
1496 emsg_severe = TRUE;
1497 EMSG2(_(e_invarg2), arg);
1498 break;
1499 }
1500 error = TRUE;
1501 arg = skipwhite(name_end);
1502 continue;
1503 }
1504 arg = temp_string;
1505 arg_len = STRLEN(temp_string);
1506 }
1507 else
1508 {
1509 c1 = *name_end;
1510 *name_end = NUL;
1511 arg_len = (int)(name_end - arg);
1512 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001513 if (arg_len == 2 && arg[1] == ':')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001515 switch (*arg)
1516 {
1517 case 'g': list_glob_vars(); break;
1518 case 'b': list_buf_vars(); break;
1519 case 'w': list_win_vars(); break;
1520 case 'v': list_vim_vars(); break;
1521 default:
1522 EMSG2(_("E738: Can't list variables for %s"), arg);
1523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524 }
1525 else
1526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001527 if (STRCMP("b:changedtick", arg) == 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001529 char_u numbuf[NUMBUFLEN];
1530
1531 sprintf((char *)numbuf, "%ld",
1532 (long)curbuf->b_changedtick);
1533 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1534 VAR_NUMBER, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 }
1536 else
1537 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001538 varp = find_var(arg, NULL);
1539 if (varp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001541 /* Skip further arguments but do continue to
1542 * search for a trailing command. */
1543 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1544 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001545 }
1546 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001547 {
1548 name = vim_strchr(arg, ':');
1549 if (name != NULL)
1550 {
1551 /* "a:" vars have no name stored, use whole arg */
1552 if (arg[0] == 'a' && arg[1] == ':')
1553 c2 = NUL;
1554 else
1555 {
1556 c2 = *++name;
1557 *name = NUL;
1558 }
1559 list_one_var(varp, arg);
1560 if (c2 != NUL)
1561 *name = c2;
1562 }
1563 else
1564 list_one_var(varp, (char_u *)"");
1565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001566 }
1567 }
1568 if (expr_start != NULL)
1569 vim_free(temp_string);
1570 else
1571 *name_end = c1;
1572 }
1573 arg = skipwhite(name_end);
1574 }
1575
1576 return arg;
1577}
1578
1579/*
1580 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1581 * Returns a pointer to the char just after the var name.
1582 * Returns NULL if there is an error.
1583 */
1584 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001586 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001587 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001589 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591{
1592 int c1;
1593 char_u *name;
1594 char_u *p;
1595 char_u *arg_end = NULL;
1596 int len;
1597 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001598 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599
1600 /*
1601 * ":let $VAR = expr": Set environment variable.
1602 */
1603 if (*arg == '$')
1604 {
1605 /* Find the end of the name. */
1606 ++arg;
1607 name = arg;
1608 len = get_env_len(&arg);
1609 if (len == 0)
1610 EMSG2(_(e_invarg2), name - 1);
1611 else
1612 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 if (op != NULL && (*op == '+' || *op == '-'))
1614 EMSG2(_(e_letwrong), op);
1615 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001616 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 EMSG(_(e_letunexp));
1618 else
1619 {
1620 c1 = name[len];
1621 name[len] = NUL;
1622 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001623 if (op != NULL && *op == '.')
1624 {
1625 int mustfree = FALSE;
1626 char_u *s = vim_getenv(name, &mustfree);
1627
1628 if (s != NULL)
1629 {
1630 p = tofree = concat_str(s, p);
1631 if (mustfree)
1632 vim_free(s);
1633 }
1634 }
1635 if (p != NULL)
1636 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 if (STRICMP(name, "HOME") == 0)
1638 init_homedir();
1639 else if (didset_vim && STRICMP(name, "VIM") == 0)
1640 didset_vim = FALSE;
1641 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1642 didset_vimruntime = FALSE;
1643 name[len] = c1;
1644 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 }
1647 }
1648 }
1649
1650 /*
1651 * ":let &option = expr": Set option value.
1652 * ":let &l:option = expr": Set local option value.
1653 * ":let &g:option = expr": Set global option value.
1654 */
1655 else if (*arg == '&')
1656 {
1657 /* Find the end of the name. */
1658 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 if (p == NULL || (endchars != NULL
1660 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 EMSG(_(e_letunexp));
1662 else
1663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 long n;
1665 int opt_type;
1666 long numval;
1667 char_u *stringval = NULL;
1668 char_u *s;
1669
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 c1 = *p;
1671 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672
1673 n = get_tv_number(tv);
1674 s = get_tv_string(tv);
1675 if (op != NULL && *op != '=')
1676 {
1677 opt_type = get_option_value(arg, &numval,
1678 &stringval, opt_flags);
1679 if ((opt_type == 1 && *op == '.')
1680 || (opt_type == 0 && *op != '.'))
1681 EMSG2(_(e_letwrong), op);
1682 else
1683 {
1684 if (opt_type == 1) /* number */
1685 {
1686 if (*op == '+')
1687 n = numval + n;
1688 else
1689 n = numval - n;
1690 }
1691 else if (opt_type == 0 && stringval != NULL) /* string */
1692 {
1693 s = concat_str(stringval, s);
1694 vim_free(stringval);
1695 stringval = s;
1696 }
1697 }
1698 }
1699 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 *p = c1;
1701 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 }
1704 }
1705
1706 /*
1707 * ":let @r = expr": Set register contents.
1708 */
1709 else if (*arg == '@')
1710 {
1711 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 if (op != NULL && (*op == '+' || *op == '-'))
1713 EMSG2(_(e_letwrong), op);
1714 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 EMSG(_(e_letunexp));
1717 else
1718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 char_u *tofree = NULL;
1720 char_u *s;
1721
1722 p = get_tv_string(tv);
1723 if (op != NULL && *op == '.')
1724 {
1725 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1726 if (s != NULL)
1727 {
1728 p = tofree = concat_str(s, p);
1729 vim_free(s);
1730 }
1731 }
1732 if (p != NULL)
1733 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 }
1737 }
1738
1739 /*
1740 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001741 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001743 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001745 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001747 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1748 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001750 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1751 EMSG(_(e_letunexp));
1752 else
1753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001754 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001755 arg_end = p;
1756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001758 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 }
1760
1761 else
1762 EMSG2(_(e_invarg2), arg);
1763
1764 return arg_end;
1765}
1766
1767/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001768 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1769 */
1770 static int
1771check_changedtick(arg)
1772 char_u *arg;
1773{
1774 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1775 {
1776 EMSG2(_(e_readonlyvar), arg);
1777 return TRUE;
1778 }
1779 return FALSE;
1780}
1781
1782/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 * Get an lval: variable, Dict item or List item that can be assigned a value
1784 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1785 * "name.key", "name.key[expr]" etc.
1786 * Indexing only works if "name" is an existing List or Dictionary.
1787 * "name" points to the start of the name.
1788 * If "rettv" is not NULL it points to the value to be assigned.
1789 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1790 * wrong; must end in space or cmd separator.
1791 *
1792 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001793 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001794 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 */
1796 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001799 typval_T *rettv;
1800 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 int unlet;
1802 int skip;
1803 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 char_u *expr_start, *expr_end;
1807 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001808 dictitem_T *v;
1809 typval_T var1;
1810 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001811 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001812 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001813 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001814 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819
1820 if (skip)
1821 {
1822 /* When skipping just find the end of the name. */
1823 lp->ll_name = name;
1824 return find_name_end(name, NULL, NULL, TRUE);
1825 }
1826
1827 /* Find the end of the name. */
1828 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1829 if (expr_start != NULL)
1830 {
1831 /* Don't expand the name when we already know there is an error. */
1832 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1833 && *p != '[' && *p != '.')
1834 {
1835 EMSG(_(e_trailing));
1836 return NULL;
1837 }
1838
1839 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1840 if (lp->ll_exp_name == NULL)
1841 {
1842 /* Report an invalid expression in braces, unless the
1843 * expression evaluation has been cancelled due to an
1844 * aborting error, an interrupt, or an exception. */
1845 if (!aborting() && !quiet)
1846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001847 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 EMSG2(_(e_invarg2), name);
1849 return NULL;
1850 }
1851 }
1852 lp->ll_name = lp->ll_exp_name;
1853 }
1854 else
1855 lp->ll_name = name;
1856
1857 /* Without [idx] or .key we are done. */
1858 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1859 return p;
1860
1861 cc = *p;
1862 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 if (v == NULL && !quiet)
1865 EMSG2(_(e_undefvar), lp->ll_name);
1866 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 if (v == NULL)
1868 return NULL;
1869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001870 /*
1871 * Loop until no more [idx] or .key is following.
1872 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001876 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1877 && !(lp->ll_tv->v_type == VAR_DICT
1878 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001880 if (!quiet)
1881 EMSG(_("E689: Can only index a List or Dictionary"));
1882 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001884 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 if (!quiet)
1887 EMSG(_("E708: [:] must come last"));
1888 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001890
Bram Moolenaar8c711452005-01-14 21:53:12 +00001891 len = -1;
1892 if (*p == '.')
1893 {
1894 key = p + 1;
1895 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1896 ;
1897 if (len == 0)
1898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 if (!quiet)
1900 EMSG(_(e_emptykey));
1901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001902 }
1903 p = key + len;
1904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001905 else
1906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001907 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001908 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001909 if (*p == ':')
1910 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001911 else
1912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001913 empty1 = FALSE;
1914 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001916 }
1917
1918 /* Optionally get the second index [ :expr]. */
1919 if (*p == ':')
1920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001925 if (!empty1)
1926 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 if (rettv != NULL && (rettv->v_type != VAR_LIST
1930 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 if (!quiet)
1933 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001934 if (!empty1)
1935 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 }
1938 p = skipwhite(p + 1);
1939 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 else
1942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 if (!empty1)
1947 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001949 }
1950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001952 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001953 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955
Bram Moolenaar8c711452005-01-14 21:53:12 +00001956 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (!quiet)
1959 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001960 if (!empty1)
1961 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 }
1966
1967 /* Skip to past ']'. */
1968 ++p;
1969 }
1970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 {
1973 if (len == -1)
1974 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 key = get_tv_string(&var1);
1977 if (*key == NUL)
1978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!quiet)
1980 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001983 }
1984 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 lp->ll_list = NULL;
1986 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001987 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001990 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 if (len == -1)
1996 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 }
1999 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 if (len == -1)
2004 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 p = NULL;
2007 break;
2008 }
2009 if (len == -1)
2010 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 }
2013 else
2014 {
2015 /*
2016 * Get the number and item for the only or first index of the List.
2017 */
2018 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002019 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002020 else
2021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 clear_tv(&var1);
2024 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 lp->ll_list = lp->ll_tv->vval.v_list;
2027 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2028 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (!quiet)
2031 EMSGN(_(e_listidx), lp->ll_n1);
2032 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002035 }
2036
2037 /*
2038 * May need to find the item or absolute index for the second
2039 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 * When no index given: "lp->ll_empty2" is TRUE.
2041 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 if (ni == NULL)
2051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 if (!quiet)
2053 EMSGN(_(e_listidx), lp->ll_n2);
2054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002057 }
2058
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2060 if (lp->ll_n1 < 0)
2061 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2062 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 if (!quiet)
2065 EMSGN(_(e_listidx), lp->ll_n2);
2066 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002067 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002068 }
2069
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
2073
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 return p;
2075}
2076
2077/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002079 */
2080 static void
2081clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083{
2084 vim_free(lp->ll_exp_name);
2085 vim_free(lp->ll_newkey);
2086}
2087
2088/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002089 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002091 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 */
2093 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100{
2101 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 listitem_T *ni;
2103 listitem_T *ri;
2104 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105
2106 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 cc = *endp;
2111 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 if (op != NULL && *op != '=')
2113 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002116 /* handle +=, -= and .= */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK)
2118 {
2119 if (tv_op(&tv, rettv, op) == OK)
2120 set_var(lp->ll_name, &tv, FALSE);
2121 clear_tv(&tv);
2122 }
2123 }
2124 else
2125 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002129 else if (tv_check_lock(lp->ll_newkey == NULL
2130 ? lp->ll_tv->v_lock
2131 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2132 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 else if (lp->ll_range)
2134 {
2135 /*
2136 * Assign the List values to the list items.
2137 */
2138 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002139 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002140 if (op != NULL && *op != '=')
2141 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2142 else
2143 {
2144 clear_tv(&lp->ll_li->li_tv);
2145 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2146 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 ri = ri->li_next;
2148 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2149 break;
2150 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 /* Need to add an empty item. */
2153 ni = listitem_alloc();
2154 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 ri = NULL;
2157 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002158 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002160 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 ni->li_tv.vval.v_number = 0;
2162 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002163 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 lp->ll_li = lp->ll_li->li_next;
2165 ++lp->ll_n1;
2166 }
2167 if (ri != NULL)
2168 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002169 else if (lp->ll_empty2
2170 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 : lp->ll_n1 != lp->ll_n2)
2172 EMSG(_("E711: List value has not enough items"));
2173 }
2174 else
2175 {
2176 /*
2177 * Assign to a List or Dictionary item.
2178 */
2179 if (lp->ll_newkey != NULL)
2180 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002181 if (op != NULL && *op != '=')
2182 {
2183 EMSG2(_(e_letwrong), op);
2184 return;
2185 }
2186
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002188 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (di == NULL)
2190 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002191 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2192 {
2193 vim_free(di);
2194 return;
2195 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002197 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 else if (op != NULL && *op != '=')
2199 {
2200 tv_op(lp->ll_tv, rettv, op);
2201 return;
2202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 /*
2207 * Assign the value to the variable or list item.
2208 */
2209 if (copy)
2210 copy_tv(rettv, lp->ll_tv);
2211 else
2212 {
2213 *lp->ll_tv = *rettv;
2214 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
2216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217}
2218
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002219/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002220 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2221 * Returns OK or FAIL.
2222 */
2223 static int
2224tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002225 typval_T *tv1;
2226 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002227 char_u *op;
2228{
2229 long n;
2230 char_u numbuf[NUMBUFLEN];
2231 char_u *s;
2232
2233 /* Can't do anything with a Funcref or a Dict on the right. */
2234 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2235 {
2236 switch (tv1->v_type)
2237 {
2238 case VAR_DICT:
2239 case VAR_FUNC:
2240 break;
2241
2242 case VAR_LIST:
2243 if (*op != '+' || tv2->v_type != VAR_LIST)
2244 break;
2245 /* List += List */
2246 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2247 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2248 return OK;
2249
2250 case VAR_NUMBER:
2251 case VAR_STRING:
2252 if (tv2->v_type == VAR_LIST)
2253 break;
2254 if (*op == '+' || *op == '-')
2255 {
2256 /* nr += nr or nr -= nr*/
2257 n = get_tv_number(tv1);
2258 if (*op == '+')
2259 n += get_tv_number(tv2);
2260 else
2261 n -= get_tv_number(tv2);
2262 clear_tv(tv1);
2263 tv1->v_type = VAR_NUMBER;
2264 tv1->vval.v_number = n;
2265 }
2266 else
2267 {
2268 /* str .= str */
2269 s = get_tv_string(tv1);
2270 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2271 clear_tv(tv1);
2272 tv1->v_type = VAR_STRING;
2273 tv1->vval.v_string = s;
2274 }
2275 return OK;
2276 }
2277 }
2278
2279 EMSG2(_(e_letwrong), op);
2280 return FAIL;
2281}
2282
2283/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002284 * Add a watcher to a list.
2285 */
2286 static void
2287list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002288 list_T *l;
2289 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002290{
2291 lw->lw_next = l->lv_watch;
2292 l->lv_watch = lw;
2293}
2294
2295/*
2296 * Remove a watches from a list.
2297 * No warning when it isn't found...
2298 */
2299 static void
2300list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002301 list_T *l;
2302 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002303{
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002305
2306 lwp = &l->lv_watch;
2307 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2308 {
2309 if (lw == lwrem)
2310 {
2311 *lwp = lw->lw_next;
2312 break;
2313 }
2314 lwp = &lw->lw_next;
2315 }
2316}
2317
2318/*
2319 * Just before removing an item from a list: advance watchers to the next
2320 * item.
2321 */
2322 static void
2323list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002324 list_T *l;
2325 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002326{
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328
2329 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2330 if (lw->lw_item == item)
2331 lw->lw_item = item->li_next;
2332}
2333
2334/*
2335 * Evaluate the expression used in a ":for var in expr" command.
2336 * "arg" points to "var".
2337 * Set "*errp" to TRUE for an error, FALSE otherwise;
2338 * Return a pointer that holds the info. Null when there is an error.
2339 */
2340 void *
2341eval_for_line(arg, errp, nextcmdp, skip)
2342 char_u *arg;
2343 int *errp;
2344 char_u **nextcmdp;
2345 int skip;
2346{
Bram Moolenaar33570922005-01-25 22:26:29 +00002347 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002348 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002349 typval_T tv;
2350 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002351
2352 *errp = TRUE; /* default: there is an error */
2353
Bram Moolenaar33570922005-01-25 22:26:29 +00002354 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002355 if (fi == NULL)
2356 return NULL;
2357
2358 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2359 if (expr == NULL)
2360 return fi;
2361
2362 expr = skipwhite(expr);
2363 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2364 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002365 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002366 return fi;
2367 }
2368
2369 if (skip)
2370 ++emsg_skip;
2371 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2372 {
2373 *errp = FALSE;
2374 if (!skip)
2375 {
2376 l = tv.vval.v_list;
2377 if (tv.v_type != VAR_LIST || l == NULL)
2378 EMSG(_(e_listreq));
2379 else
2380 {
2381 fi->fi_list = l;
2382 list_add_watch(l, &fi->fi_lw);
2383 fi->fi_lw.lw_item = l->lv_first;
2384 }
2385 }
2386 }
2387 if (skip)
2388 --emsg_skip;
2389
2390 return fi;
2391}
2392
2393/*
2394 * Use the first item in a ":for" list. Advance to the next.
2395 * Assign the values to the variable (list). "arg" points to the first one.
2396 * Return TRUE when a valid item was found, FALSE when at end of list or
2397 * something wrong.
2398 */
2399 int
2400next_for_item(fi_void, arg)
2401 void *fi_void;
2402 char_u *arg;
2403{
Bram Moolenaar33570922005-01-25 22:26:29 +00002404 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002405 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002406 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002407
2408 item = fi->fi_lw.lw_item;
2409 if (item == NULL)
2410 result = FALSE;
2411 else
2412 {
2413 fi->fi_lw.lw_item = item->li_next;
2414 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2415 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2416 }
2417 return result;
2418}
2419
2420/*
2421 * Free the structure used to store info used by ":for".
2422 */
2423 void
2424free_for_info(fi_void)
2425 void *fi_void;
2426{
Bram Moolenaar33570922005-01-25 22:26:29 +00002427 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002429 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002430 list_rem_watch(fi->fi_list, &fi->fi_lw);
2431 vim_free(fi);
2432}
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2435
2436 void
2437set_context_for_expression(xp, arg, cmdidx)
2438 expand_T *xp;
2439 char_u *arg;
2440 cmdidx_T cmdidx;
2441{
2442 int got_eq = FALSE;
2443 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002446 if (cmdidx == CMD_let)
2447 {
2448 xp->xp_context = EXPAND_USER_VARS;
2449 if (vim_strchr(arg, '=') == NULL)
2450 {
2451 /* ":let var1 var2 ...": find last space. */
2452 for (p = arg + STRLEN(arg); p > arg; )
2453 {
2454 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002455 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002456 if (vim_iswhite(*p))
2457 break;
2458 }
2459 return;
2460 }
2461 }
2462 else
2463 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2464 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 while ((xp->xp_pattern = vim_strpbrk(arg,
2466 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2467 {
2468 c = *xp->xp_pattern;
2469 if (c == '&')
2470 {
2471 c = xp->xp_pattern[1];
2472 if (c == '&')
2473 {
2474 ++xp->xp_pattern;
2475 xp->xp_context = cmdidx != CMD_let || got_eq
2476 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2477 }
2478 else if (c != ' ')
2479 xp->xp_context = EXPAND_SETTINGS;
2480 }
2481 else if (c == '$')
2482 {
2483 /* environment variable */
2484 xp->xp_context = EXPAND_ENV_VARS;
2485 }
2486 else if (c == '=')
2487 {
2488 got_eq = TRUE;
2489 xp->xp_context = EXPAND_EXPRESSION;
2490 }
2491 else if (c == '<'
2492 && xp->xp_context == EXPAND_FUNCTIONS
2493 && vim_strchr(xp->xp_pattern, '(') == NULL)
2494 {
2495 /* Function name can start with "<SNR>" */
2496 break;
2497 }
2498 else if (cmdidx != CMD_let || got_eq)
2499 {
2500 if (c == '"') /* string */
2501 {
2502 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2503 if (c == '\\' && xp->xp_pattern[1] != NUL)
2504 ++xp->xp_pattern;
2505 xp->xp_context = EXPAND_NOTHING;
2506 }
2507 else if (c == '\'') /* literal string */
2508 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2511 /* skip */ ;
2512 xp->xp_context = EXPAND_NOTHING;
2513 }
2514 else if (c == '|')
2515 {
2516 if (xp->xp_pattern[1] == '|')
2517 {
2518 ++xp->xp_pattern;
2519 xp->xp_context = EXPAND_EXPRESSION;
2520 }
2521 else
2522 xp->xp_context = EXPAND_COMMANDS;
2523 }
2524 else
2525 xp->xp_context = EXPAND_EXPRESSION;
2526 }
2527 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528 /* Doesn't look like something valid, expand as an expression
2529 * anyway. */
2530 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 arg = xp->xp_pattern;
2532 if (*arg != NUL)
2533 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2534 /* skip */ ;
2535 }
2536 xp->xp_pattern = arg;
2537}
2538
2539#endif /* FEAT_CMDL_COMPL */
2540
2541/*
2542 * ":1,25call func(arg1, arg2)" function call.
2543 */
2544 void
2545ex_call(eap)
2546 exarg_T *eap;
2547{
2548 char_u *arg = eap->arg;
2549 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 linenr_T lnum;
2555 int doesrange;
2556 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002559 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2560 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002561 if (tofree == NULL)
2562 return;
2563
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002564 /* Increase refcount on dictionary, it could get deleted when evaluating
2565 * the arguments. */
2566 if (fudi.fd_dict != NULL)
2567 ++fudi.fd_dict->dv_refcount;
2568
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2570 len = STRLEN(tofree);
2571 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaar532c7802005-01-27 14:44:31 +00002573 /* Skip white space to allow ":call func ()". Not good, but required for
2574 * backward compatibility. */
2575 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578 if (*startarg != '(')
2579 {
2580 EMSG2(_("E107: Missing braces: %s"), name);
2581 goto end;
2582 }
2583
2584 /*
2585 * When skipping, evaluate the function once, to find the end of the
2586 * arguments.
2587 * When the function takes a range, this is discovered after the first
2588 * call, and the loop is broken.
2589 */
2590 if (eap->skip)
2591 {
2592 ++emsg_skip;
2593 lnum = eap->line2; /* do it once, also with an invalid range */
2594 }
2595 else
2596 lnum = eap->line1;
2597 for ( ; lnum <= eap->line2; ++lnum)
2598 {
2599 if (!eap->skip && eap->addr_count > 0)
2600 {
2601 curwin->w_cursor.lnum = lnum;
2602 curwin->w_cursor.col = 0;
2603 }
2604 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002605 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002606 eap->line1, eap->line2, &doesrange,
2607 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 failed = TRUE;
2610 break;
2611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (doesrange || eap->skip)
2614 break;
2615 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002616 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002618 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (aborting())
2620 break;
2621 }
2622 if (eap->skip)
2623 --emsg_skip;
2624
2625 if (!failed)
2626 {
2627 /* Check for trailing illegal characters and a following command. */
2628 if (!ends_excmd(*arg))
2629 {
2630 emsg_severe = TRUE;
2631 EMSG(_(e_trailing));
2632 }
2633 else
2634 eap->nextcmd = check_nextcmd(arg);
2635 }
2636
2637end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002638 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640}
2641
2642/*
2643 * ":unlet[!] var1 ... " command.
2644 */
2645 void
2646ex_unlet(eap)
2647 exarg_T *eap;
2648{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002649 ex_unletlock(eap, eap->arg, 0);
2650}
2651
2652/*
2653 * ":lockvar" and ":unlockvar" commands
2654 */
2655 void
2656ex_lockvar(eap)
2657 exarg_T *eap;
2658{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002660 int deep = 2;
2661
2662 if (eap->forceit)
2663 deep = -1;
2664 else if (vim_isdigit(*arg))
2665 {
2666 deep = getdigits(&arg);
2667 arg = skipwhite(arg);
2668 }
2669
2670 ex_unletlock(eap, arg, deep);
2671}
2672
2673/*
2674 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2675 */
2676 static void
2677ex_unletlock(eap, argstart, deep)
2678 exarg_T *eap;
2679 char_u *argstart;
2680 int deep;
2681{
2682 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002685 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 do
2688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 /* Parse the name and find the end. */
2690 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2691 if (lv.ll_name == NULL)
2692 error = TRUE; /* error but continue parsing */
2693 if (name_end == NULL || (!vim_iswhite(*name_end)
2694 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (name_end != NULL)
2697 {
2698 emsg_severe = TRUE;
2699 EMSG(_(e_trailing));
2700 }
2701 if (!(eap->skip || error))
2702 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 break;
2704 }
2705
2706 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002707 {
2708 if (eap->cmdidx == CMD_unlet)
2709 {
2710 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2711 error = TRUE;
2712 }
2713 else
2714 {
2715 if (do_lock_var(&lv, name_end, deep,
2716 eap->cmdidx == CMD_lockvar) == FAIL)
2717 error = TRUE;
2718 }
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (!eap->skip)
2722 clear_lval(&lv);
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 arg = skipwhite(name_end);
2725 } while (!ends_excmd(*arg));
2726
2727 eap->nextcmd = check_nextcmd(arg);
2728}
2729
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002732 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 int forceit;
2735{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 int ret = OK;
2737 int cc;
2738
2739 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 cc = *name_end;
2742 *name_end = NUL;
2743
2744 /* Normal name or expanded name. */
2745 if (check_changedtick(lp->ll_name))
2746 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002747 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002751 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2752 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 else if (lp->ll_range)
2754 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756
2757 /* Delete a range of List items. */
2758 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2759 {
2760 li = lp->ll_li->li_next;
2761 listitem_remove(lp->ll_list, lp->ll_li);
2762 lp->ll_li = li;
2763 ++lp->ll_n1;
2764 }
2765 }
2766 else
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 /* unlet a List item. */
2770 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002773 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 }
2775
2776 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777}
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779/*
2780 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002781 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
2783 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002784do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002786 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
Bram Moolenaar33570922005-01-25 22:26:29 +00002788 hashtab_T *ht;
2789 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002790 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 ht = find_var_ht(name, &varname);
2793 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002795 hi = hash_find(ht, varname);
2796 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002797 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002798 if (var_check_ro(HI2DI(hi)->di_flags, name))
2799 return FAIL;
2800 delete_var(ht, hi);
2801 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002804 if (forceit)
2805 return OK;
2806 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 return FAIL;
2808}
2809
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002810/*
2811 * Lock or unlock variable indicated by "lp".
2812 * "deep" is the levels to go (-1 for unlimited);
2813 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2814 */
2815 static int
2816do_lock_var(lp, name_end, deep, lock)
2817 lval_T *lp;
2818 char_u *name_end;
2819 int deep;
2820 int lock;
2821{
2822 int ret = OK;
2823 int cc;
2824 dictitem_T *di;
2825
2826 if (deep == 0) /* nothing to do */
2827 return OK;
2828
2829 if (lp->ll_tv == NULL)
2830 {
2831 cc = *name_end;
2832 *name_end = NUL;
2833
2834 /* Normal name or expanded name. */
2835 if (check_changedtick(lp->ll_name))
2836 ret = FAIL;
2837 else
2838 {
2839 di = find_var(lp->ll_name, NULL);
2840 if (di == NULL)
2841 ret = FAIL;
2842 else
2843 {
2844 if (lock)
2845 di->di_flags |= DI_FLAGS_LOCK;
2846 else
2847 di->di_flags &= ~DI_FLAGS_LOCK;
2848 item_lock(&di->di_tv, deep, lock);
2849 }
2850 }
2851 *name_end = cc;
2852 }
2853 else if (lp->ll_range)
2854 {
2855 listitem_T *li = lp->ll_li;
2856
2857 /* (un)lock a range of List items. */
2858 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2859 {
2860 item_lock(&li->li_tv, deep, lock);
2861 li = li->li_next;
2862 ++lp->ll_n1;
2863 }
2864 }
2865 else if (lp->ll_list != NULL)
2866 /* (un)lock a List item. */
2867 item_lock(&lp->ll_li->li_tv, deep, lock);
2868 else
2869 /* un(lock) a Dictionary item. */
2870 item_lock(&lp->ll_di->di_tv, deep, lock);
2871
2872 return ret;
2873}
2874
2875/*
2876 * Lock or unlock an item. "deep" is nr of levels to go.
2877 */
2878 static void
2879item_lock(tv, deep, lock)
2880 typval_T *tv;
2881 int deep;
2882 int lock;
2883{
2884 static int recurse = 0;
2885 list_T *l;
2886 listitem_T *li;
2887 dict_T *d;
2888 hashitem_T *hi;
2889 int todo;
2890
2891 if (recurse >= DICT_MAXNEST)
2892 {
2893 EMSG(_("E743: variable nested too deep for (un)lock"));
2894 return;
2895 }
2896 if (deep == 0)
2897 return;
2898 ++recurse;
2899
2900 /* lock/unlock the item itself */
2901 if (lock)
2902 tv->v_lock |= VAR_LOCKED;
2903 else
2904 tv->v_lock &= ~VAR_LOCKED;
2905
2906 switch (tv->v_type)
2907 {
2908 case VAR_LIST:
2909 if ((l = tv->vval.v_list) != NULL)
2910 {
2911 if (lock)
2912 l->lv_lock |= VAR_LOCKED;
2913 else
2914 l->lv_lock &= ~VAR_LOCKED;
2915 if (deep < 0 || deep > 1)
2916 /* recursive: lock/unlock the items the List contains */
2917 for (li = l->lv_first; li != NULL; li = li->li_next)
2918 item_lock(&li->li_tv, deep - 1, lock);
2919 }
2920 break;
2921 case VAR_DICT:
2922 if ((d = tv->vval.v_dict) != NULL)
2923 {
2924 if (lock)
2925 d->dv_lock |= VAR_LOCKED;
2926 else
2927 d->dv_lock &= ~VAR_LOCKED;
2928 if (deep < 0 || deep > 1)
2929 {
2930 /* recursive: lock/unlock the items the List contains */
2931 todo = d->dv_hashtab.ht_used;
2932 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2933 {
2934 if (!HASHITEM_EMPTY(hi))
2935 {
2936 --todo;
2937 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2938 }
2939 }
2940 }
2941 }
2942 }
2943 --recurse;
2944}
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2947/*
2948 * Delete all "menutrans_" variables.
2949 */
2950 void
2951del_menutrans_vars()
2952{
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002954 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaar33570922005-01-25 22:26:29 +00002956 hash_lock(&globvarht);
2957 todo = globvarht.ht_used;
2958 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00002959 {
2960 if (!HASHITEM_EMPTY(hi))
2961 {
2962 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002963 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2964 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00002965 }
2966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002967 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968}
2969#endif
2970
2971#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2972
2973/*
2974 * Local string buffer for the next two functions to store a variable name
2975 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2976 * get_user_var_name().
2977 */
2978
2979static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2980
2981static char_u *varnamebuf = NULL;
2982static int varnamebuflen = 0;
2983
2984/*
2985 * Function to concatenate a prefix and a variable name.
2986 */
2987 static char_u *
2988cat_prefix_varname(prefix, name)
2989 int prefix;
2990 char_u *name;
2991{
2992 int len;
2993
2994 len = (int)STRLEN(name) + 3;
2995 if (len > varnamebuflen)
2996 {
2997 vim_free(varnamebuf);
2998 len += 10; /* some additional space */
2999 varnamebuf = alloc(len);
3000 if (varnamebuf == NULL)
3001 {
3002 varnamebuflen = 0;
3003 return NULL;
3004 }
3005 varnamebuflen = len;
3006 }
3007 *varnamebuf = prefix;
3008 varnamebuf[1] = ':';
3009 STRCPY(varnamebuf + 2, name);
3010 return varnamebuf;
3011}
3012
3013/*
3014 * Function given to ExpandGeneric() to obtain the list of user defined
3015 * (global/buffer/window/built-in) variable names.
3016 */
3017/*ARGSUSED*/
3018 char_u *
3019get_user_var_name(xp, idx)
3020 expand_T *xp;
3021 int idx;
3022{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003023 static long_u gdone;
3024 static long_u bdone;
3025 static long_u wdone;
3026 static int vidx;
3027 static hashitem_T *hi;
3028 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003031 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003032
3033 /* Global variables */
3034 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003036 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003038 else
3039 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003040 while (HASHITEM_EMPTY(hi))
3041 ++hi;
3042 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3043 return cat_prefix_varname('g', hi->hi_key);
3044 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003046
3047 /* b: variables */
3048 ht = &curbuf->b_vars.dv_hashtab;
3049 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003051 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003052 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003053 else
3054 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003055 while (HASHITEM_EMPTY(hi))
3056 ++hi;
3057 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003061 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 return (char_u *)"b:changedtick";
3063 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003064
3065 /* w: variables */
3066 ht = &curwin->w_vars.dv_hashtab;
3067 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003069 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003071 else
3072 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003073 while (HASHITEM_EMPTY(hi))
3074 ++hi;
3075 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003077
3078 /* v: variables */
3079 if (vidx < VV_LEN)
3080 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
3082 vim_free(varnamebuf);
3083 varnamebuf = NULL;
3084 varnamebuflen = 0;
3085 return NULL;
3086}
3087
3088#endif /* FEAT_CMDL_COMPL */
3089
3090/*
3091 * types for expressions.
3092 */
3093typedef enum
3094{
3095 TYPE_UNKNOWN = 0
3096 , TYPE_EQUAL /* == */
3097 , TYPE_NEQUAL /* != */
3098 , TYPE_GREATER /* > */
3099 , TYPE_GEQUAL /* >= */
3100 , TYPE_SMALLER /* < */
3101 , TYPE_SEQUAL /* <= */
3102 , TYPE_MATCH /* =~ */
3103 , TYPE_NOMATCH /* !~ */
3104} exptype_T;
3105
3106/*
3107 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003108 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3110 */
3111
3112/*
3113 * Handle zero level expression.
3114 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003115 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * Return OK or FAIL.
3117 */
3118 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 char_u **nextcmd;
3123 int evaluate;
3124{
3125 int ret;
3126 char_u *p;
3127
3128 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003129 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (ret == FAIL || !ends_excmd(*p))
3131 {
3132 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 /*
3135 * Report the invalid expression unless the expression evaluation has
3136 * been cancelled due to an aborting error, an interrupt, or an
3137 * exception.
3138 */
3139 if (!aborting())
3140 EMSG2(_(e_invexpr2), arg);
3141 ret = FAIL;
3142 }
3143 if (nextcmd != NULL)
3144 *nextcmd = check_nextcmd(p);
3145
3146 return ret;
3147}
3148
3149/*
3150 * Handle top level expression:
3151 * expr1 ? expr0 : expr0
3152 *
3153 * "arg" must point to the first non-white of the expression.
3154 * "arg" is advanced to the next non-white after the recognized expression.
3155 *
3156 * Return OK or FAIL.
3157 */
3158 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003159eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 int evaluate;
3163{
3164 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 /*
3168 * Get the first variable.
3169 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003170 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return FAIL;
3172
3173 if ((*arg)[0] == '?')
3174 {
3175 result = FALSE;
3176 if (evaluate)
3177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003178 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
3182
3183 /*
3184 * Get the second variable.
3185 */
3186 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 return FAIL;
3189
3190 /*
3191 * Check for the ":".
3192 */
3193 if ((*arg)[0] != ':')
3194 {
3195 EMSG(_("E109: Missing ':' after '?'"));
3196 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003197 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 return FAIL;
3199 }
3200
3201 /*
3202 * Get the third variable.
3203 */
3204 *arg = skipwhite(*arg + 1);
3205 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3206 {
3207 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 return FAIL;
3210 }
3211 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003212 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
3214
3215 return OK;
3216}
3217
3218/*
3219 * Handle first level expression:
3220 * expr2 || expr2 || expr2 logical OR
3221 *
3222 * "arg" must point to the first non-white of the expression.
3223 * "arg" is advanced to the next non-white after the recognized expression.
3224 *
3225 * Return OK or FAIL.
3226 */
3227 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 int evaluate;
3232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 long result;
3235 int first;
3236
3237 /*
3238 * Get the first variable.
3239 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 return FAIL;
3242
3243 /*
3244 * Repeat until there is no following "||".
3245 */
3246 first = TRUE;
3247 result = FALSE;
3248 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3249 {
3250 if (evaluate && first)
3251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003252 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003254 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 first = FALSE;
3256 }
3257
3258 /*
3259 * Get the second variable.
3260 */
3261 *arg = skipwhite(*arg + 2);
3262 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3263 return FAIL;
3264
3265 /*
3266 * Compute the result.
3267 */
3268 if (evaluate && !result)
3269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003270 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 if (evaluate)
3275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 rettv->v_type = VAR_NUMBER;
3277 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 }
3280
3281 return OK;
3282}
3283
3284/*
3285 * Handle second level expression:
3286 * expr3 && expr3 && expr3 logical AND
3287 *
3288 * "arg" must point to the first non-white of the expression.
3289 * "arg" is advanced to the next non-white after the recognized expression.
3290 *
3291 * Return OK or FAIL.
3292 */
3293 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 int evaluate;
3298{
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 long result;
3301 int first;
3302
3303 /*
3304 * Get the first variable.
3305 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003306 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return FAIL;
3308
3309 /*
3310 * Repeat until there is no following "&&".
3311 */
3312 first = TRUE;
3313 result = TRUE;
3314 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3315 {
3316 if (evaluate && first)
3317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003318 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003320 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 first = FALSE;
3322 }
3323
3324 /*
3325 * Get the second variable.
3326 */
3327 *arg = skipwhite(*arg + 2);
3328 if (eval4(arg, &var2, evaluate && result) == FAIL)
3329 return FAIL;
3330
3331 /*
3332 * Compute the result.
3333 */
3334 if (evaluate && result)
3335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003338 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340 if (evaluate)
3341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 rettv->v_type = VAR_NUMBER;
3343 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345 }
3346
3347 return OK;
3348}
3349
3350/*
3351 * Handle third level expression:
3352 * var1 == var2
3353 * var1 =~ var2
3354 * var1 != var2
3355 * var1 !~ var2
3356 * var1 > var2
3357 * var1 >= var2
3358 * var1 < var2
3359 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003360 * var1 is var2
3361 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 *
3363 * "arg" must point to the first non-white of the expression.
3364 * "arg" is advanced to the next non-white after the recognized expression.
3365 *
3366 * Return OK or FAIL.
3367 */
3368 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int evaluate;
3373{
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 char_u *p;
3376 int i;
3377 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003378 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 int len = 2;
3380 long n1, n2;
3381 char_u *s1, *s2;
3382 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3383 regmatch_T regmatch;
3384 int ic;
3385 char_u *save_cpo;
3386
3387 /*
3388 * Get the first variable.
3389 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 return FAIL;
3392
3393 p = *arg;
3394 switch (p[0])
3395 {
3396 case '=': if (p[1] == '=')
3397 type = TYPE_EQUAL;
3398 else if (p[1] == '~')
3399 type = TYPE_MATCH;
3400 break;
3401 case '!': if (p[1] == '=')
3402 type = TYPE_NEQUAL;
3403 else if (p[1] == '~')
3404 type = TYPE_NOMATCH;
3405 break;
3406 case '>': if (p[1] != '=')
3407 {
3408 type = TYPE_GREATER;
3409 len = 1;
3410 }
3411 else
3412 type = TYPE_GEQUAL;
3413 break;
3414 case '<': if (p[1] != '=')
3415 {
3416 type = TYPE_SMALLER;
3417 len = 1;
3418 }
3419 else
3420 type = TYPE_SEQUAL;
3421 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003422 case 'i': if (p[1] == 's')
3423 {
3424 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3425 len = 5;
3426 if (!vim_isIDc(p[len]))
3427 {
3428 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3429 type_is = TRUE;
3430 }
3431 }
3432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434
3435 /*
3436 * If there is a comparitive operator, use it.
3437 */
3438 if (type != TYPE_UNKNOWN)
3439 {
3440 /* extra question mark appended: ignore case */
3441 if (p[len] == '?')
3442 {
3443 ic = TRUE;
3444 ++len;
3445 }
3446 /* extra '#' appended: match case */
3447 else if (p[len] == '#')
3448 {
3449 ic = FALSE;
3450 ++len;
3451 }
3452 /* nothing appened: use 'ignorecase' */
3453 else
3454 ic = p_ic;
3455
3456 /*
3457 * Get the second variable.
3458 */
3459 *arg = skipwhite(p + len);
3460 if (eval5(arg, &var2, evaluate) == FAIL)
3461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 return FAIL;
3464 }
3465
3466 if (evaluate)
3467 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003468 if (type_is && rettv->v_type != var2.v_type)
3469 {
3470 /* For "is" a different type always means FALSE, for "notis"
3471 * it means TRUE. */
3472 n1 = (type == TYPE_NEQUAL);
3473 }
3474 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3475 {
3476 if (type_is)
3477 {
3478 n1 = (rettv->v_type == var2.v_type
3479 && rettv->vval.v_list == var2.vval.v_list);
3480 if (type == TYPE_NEQUAL)
3481 n1 = !n1;
3482 }
3483 else if (rettv->v_type != var2.v_type
3484 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3485 {
3486 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003487 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003488 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003489 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003490 clear_tv(rettv);
3491 clear_tv(&var2);
3492 return FAIL;
3493 }
3494 else
3495 {
3496 /* Compare two Lists for being equal or unequal. */
3497 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3498 if (type == TYPE_NEQUAL)
3499 n1 = !n1;
3500 }
3501 }
3502
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3504 {
3505 if (type_is)
3506 {
3507 n1 = (rettv->v_type == var2.v_type
3508 && rettv->vval.v_dict == var2.vval.v_dict);
3509 if (type == TYPE_NEQUAL)
3510 n1 = !n1;
3511 }
3512 else if (rettv->v_type != var2.v_type
3513 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3514 {
3515 if (rettv->v_type != var2.v_type)
3516 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3517 else
3518 EMSG(_("E736: Invalid operation for Dictionary"));
3519 clear_tv(rettv);
3520 clear_tv(&var2);
3521 return FAIL;
3522 }
3523 else
3524 {
3525 /* Compare two Dictionaries for being equal or unequal. */
3526 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3527 if (type == TYPE_NEQUAL)
3528 n1 = !n1;
3529 }
3530 }
3531
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003532 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3533 {
3534 if (rettv->v_type != var2.v_type
3535 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3536 {
3537 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003538 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003539 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003540 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003541 clear_tv(rettv);
3542 clear_tv(&var2);
3543 return FAIL;
3544 }
3545 else
3546 {
3547 /* Compare two Funcrefs for being equal or unequal. */
3548 if (rettv->vval.v_string == NULL
3549 || var2.vval.v_string == NULL)
3550 n1 = FALSE;
3551 else
3552 n1 = STRCMP(rettv->vval.v_string,
3553 var2.vval.v_string) == 0;
3554 if (type == TYPE_NEQUAL)
3555 n1 = !n1;
3556 }
3557 }
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /*
3560 * If one of the two variables is a number, compare as a number.
3561 * When using "=~" or "!~", always compare as string.
3562 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003563 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 n1 = get_tv_number(rettv);
3567 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 switch (type)
3569 {
3570 case TYPE_EQUAL: n1 = (n1 == n2); break;
3571 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3572 case TYPE_GREATER: n1 = (n1 > n2); break;
3573 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3574 case TYPE_SMALLER: n1 = (n1 < n2); break;
3575 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3576 case TYPE_UNKNOWN:
3577 case TYPE_MATCH:
3578 case TYPE_NOMATCH: break; /* avoid gcc warning */
3579 }
3580 }
3581 else
3582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003583 s1 = get_tv_string_buf(rettv, buf1);
3584 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3586 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3587 else
3588 i = 0;
3589 n1 = FALSE;
3590 switch (type)
3591 {
3592 case TYPE_EQUAL: n1 = (i == 0); break;
3593 case TYPE_NEQUAL: n1 = (i != 0); break;
3594 case TYPE_GREATER: n1 = (i > 0); break;
3595 case TYPE_GEQUAL: n1 = (i >= 0); break;
3596 case TYPE_SMALLER: n1 = (i < 0); break;
3597 case TYPE_SEQUAL: n1 = (i <= 0); break;
3598
3599 case TYPE_MATCH:
3600 case TYPE_NOMATCH:
3601 /* avoid 'l' flag in 'cpoptions' */
3602 save_cpo = p_cpo;
3603 p_cpo = (char_u *)"";
3604 regmatch.regprog = vim_regcomp(s2,
3605 RE_MAGIC + RE_STRING);
3606 regmatch.rm_ic = ic;
3607 if (regmatch.regprog != NULL)
3608 {
3609 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3610 vim_free(regmatch.regprog);
3611 if (type == TYPE_NOMATCH)
3612 n1 = !n1;
3613 }
3614 p_cpo = save_cpo;
3615 break;
3616
3617 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3618 }
3619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 clear_tv(rettv);
3621 clear_tv(&var2);
3622 rettv->v_type = VAR_NUMBER;
3623 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625 }
3626
3627 return OK;
3628}
3629
3630/*
3631 * Handle fourth level expression:
3632 * + number addition
3633 * - number subtraction
3634 * . string concatenation
3635 *
3636 * "arg" must point to the first non-white of the expression.
3637 * "arg" is advanced to the next non-white after the recognized expression.
3638 *
3639 * Return OK or FAIL.
3640 */
3641 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 int evaluate;
3646{
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 typval_T var2;
3648 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 int op;
3650 long n1, n2;
3651 char_u *s1, *s2;
3652 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3653 char_u *p;
3654
3655 /*
3656 * Get the first variable.
3657 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return FAIL;
3660
3661 /*
3662 * Repeat computing, until no '+', '-' or '.' is following.
3663 */
3664 for (;;)
3665 {
3666 op = **arg;
3667 if (op != '+' && op != '-' && op != '.')
3668 break;
3669
3670 /*
3671 * Get the second variable.
3672 */
3673 *arg = skipwhite(*arg + 1);
3674 if (eval6(arg, &var2, evaluate) == FAIL)
3675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return FAIL;
3678 }
3679
3680 if (evaluate)
3681 {
3682 /*
3683 * Compute the result.
3684 */
3685 if (op == '.')
3686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 s1 = get_tv_string_buf(rettv, buf1);
3688 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003689 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 clear_tv(rettv);
3691 rettv->v_type = VAR_STRING;
3692 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003694 else if (op == '+' && rettv->v_type == VAR_LIST
3695 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003696 {
3697 /* concatenate Lists */
3698 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3699 &var3) == FAIL)
3700 {
3701 clear_tv(rettv);
3702 clear_tv(&var2);
3703 return FAIL;
3704 }
3705 clear_tv(rettv);
3706 *rettv = var3;
3707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 else
3709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 n1 = get_tv_number(rettv);
3711 n2 = get_tv_number(&var2);
3712 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (op == '+')
3714 n1 = n1 + n2;
3715 else
3716 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 rettv->v_type = VAR_NUMBER;
3718 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 }
3723 return OK;
3724}
3725
3726/*
3727 * Handle fifth level expression:
3728 * * number multiplication
3729 * / number division
3730 * % number modulo
3731 *
3732 * "arg" must point to the first non-white of the expression.
3733 * "arg" is advanced to the next non-white after the recognized expression.
3734 *
3735 * Return OK or FAIL.
3736 */
3737 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int evaluate;
3742{
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 int op;
3745 long n1, n2;
3746
3747 /*
3748 * Get the first variable.
3749 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003750 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 return FAIL;
3752
3753 /*
3754 * Repeat computing, until no '*', '/' or '%' is following.
3755 */
3756 for (;;)
3757 {
3758 op = **arg;
3759 if (op != '*' && op != '/' && op != '%')
3760 break;
3761
3762 if (evaluate)
3763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 n1 = get_tv_number(rettv);
3765 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 else
3768 n1 = 0;
3769
3770 /*
3771 * Get the second variable.
3772 */
3773 *arg = skipwhite(*arg + 1);
3774 if (eval7(arg, &var2, evaluate) == FAIL)
3775 return FAIL;
3776
3777 if (evaluate)
3778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 n2 = get_tv_number(&var2);
3780 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * Compute the result.
3784 */
3785 if (op == '*')
3786 n1 = n1 * n2;
3787 else if (op == '/')
3788 {
3789 if (n2 == 0) /* give an error message? */
3790 n1 = 0x7fffffffL;
3791 else
3792 n1 = n1 / n2;
3793 }
3794 else
3795 {
3796 if (n2 == 0) /* give an error message? */
3797 n1 = 0;
3798 else
3799 n1 = n1 % n2;
3800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 rettv->v_type = VAR_NUMBER;
3802 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 }
3805
3806 return OK;
3807}
3808
3809/*
3810 * Handle sixth level expression:
3811 * number number constant
3812 * "string" string contstant
3813 * 'string' literal string contstant
3814 * &option-name option value
3815 * @r register contents
3816 * identifier variable value
3817 * function() function call
3818 * $VAR environment variable
3819 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 * [expr, expr] List
3821 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 *
3823 * Also handle:
3824 * ! in front logical NOT
3825 * - in front unary minus
3826 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827 * trailing [] subscript in String or List
3828 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 *
3830 * "arg" must point to the first non-white of the expression.
3831 * "arg" is advanced to the next non-white after the recognized expression.
3832 *
3833 * Return OK or FAIL.
3834 */
3835 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 int evaluate;
3840{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 long n;
3842 int len;
3843 char_u *s;
3844 int val;
3845 char_u *start_leader, *end_leader;
3846 int ret = OK;
3847 char_u *alias;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 dict_T *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
3850 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003852 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 /*
3857 * Skip '!' and '-' characters. They are handled later.
3858 */
3859 start_leader = *arg;
3860 while (**arg == '!' || **arg == '-' || **arg == '+')
3861 *arg = skipwhite(*arg + 1);
3862 end_leader = *arg;
3863
3864 switch (**arg)
3865 {
3866 /*
3867 * Number constant.
3868 */
3869 case '0':
3870 case '1':
3871 case '2':
3872 case '3':
3873 case '4':
3874 case '5':
3875 case '6':
3876 case '7':
3877 case '8':
3878 case '9':
3879 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3880 *arg += len;
3881 if (evaluate)
3882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 rettv->v_type = VAR_NUMBER;
3884 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 break;
3887
3888 /*
3889 * String constant: "string".
3890 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 break;
3893
3894 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003895 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003898 break;
3899
3900 /*
3901 * List: [expr, expr]
3902 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 break;
3905
3906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003907 * Dictionary: {key: val, key: val}
3908 */
3909 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3910 break;
3911
3912 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003913 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003915 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917
3918 /*
3919 * Environment variable: $VAR.
3920 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003921 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924 /*
3925 * Register contents: @r.
3926 */
3927 case '@': ++*arg;
3928 if (evaluate)
3929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 rettv->v_type = VAR_STRING;
3931 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 if (**arg != NUL)
3934 ++*arg;
3935 break;
3936
3937 /*
3938 * nested expression: (expression).
3939 */
3940 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (**arg == ')')
3943 ++*arg;
3944 else if (ret == OK)
3945 {
3946 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 ret = FAIL;
3949 }
3950 break;
3951
Bram Moolenaar8c711452005-01-14 21:53:12 +00003952 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 break;
3954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003955
3956 if (ret == NOTDONE)
3957 {
3958 /*
3959 * Must be a variable or function name.
3960 * Can also be a curly-braces kind of name: {expr}.
3961 */
3962 s = *arg;
Bram Moolenaara7043832005-01-21 11:56:39 +00003963 len = get_name_len(arg, &alias, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 if (alias != NULL)
3965 s = alias;
3966
3967 if (len == 0)
3968 ret = FAIL;
3969 else
3970 {
3971 if (**arg == '(') /* recursive! */
3972 {
3973 /* If "s" is the name of a variable of type VAR_FUNC
3974 * use its contents. */
3975 s = deref_func_name(s, &len);
3976
3977 /* Invoke the function. */
3978 ret = get_func_tv(s, len, rettv, arg,
3979 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003980 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003981 /* Stop the expression evaluation when immediately
3982 * aborting on error, or when an interrupt occurred or
3983 * an exception was thrown but not caught. */
3984 if (aborting())
3985 {
3986 if (ret == OK)
3987 clear_tv(rettv);
3988 ret = FAIL;
3989 }
3990 }
3991 else if (evaluate)
3992 ret = get_var_tv(s, len, rettv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003993 else
3994 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003995 }
3996
3997 if (alias != NULL)
3998 vim_free(alias);
3999 }
4000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 *arg = skipwhite(*arg);
4002
4003 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004005 * Also handle function call with Funcref variable: func(expr)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004006 * Can all be combined: dict.func(expr)[idx]['func'](expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004008 selfdict = NULL;
4009 while (ret == OK
4010 && (**arg == '['
4011 || (**arg == '.' && rettv->v_type == VAR_DICT)
4012 || (**arg == '(' && rettv->v_type == VAR_FUNC))
4013 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00004015 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00004017 s = rettv->vval.v_string;
4018
4019 /* Invoke the function. Recursive! */
4020 ret = get_func_tv(s, STRLEN(s), rettv, arg,
4021 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4022 &len, evaluate, selfdict);
4023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004024 /* Stop the expression evaluation when immediately aborting on
4025 * error, or when an interrupt occurred or an exception was thrown
4026 * but not caught. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004027 if (aborting())
4028 {
4029 if (ret == OK)
4030 clear_tv(rettv);
4031 ret = FAIL;
4032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004033 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004034 selfdict = NULL;
4035 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004036 else /* **arg == '[' || **arg == '.' */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004038 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004039 if (rettv->v_type == VAR_DICT)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004040 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00004041 selfdict = rettv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004042 if (selfdict != NULL)
4043 ++selfdict->dv_refcount;
4044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004045 else
4046 selfdict = NULL;
4047 if (eval_index(arg, rettv, evaluate) == FAIL)
4048 {
4049 clear_tv(rettv);
4050 ret = FAIL;
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004054 dict_unref(selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056 /*
4057 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4058 */
4059 if (ret == OK && evaluate && end_leader > start_leader)
4060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 while (end_leader > start_leader)
4063 {
4064 --end_leader;
4065 if (*end_leader == '!')
4066 val = !val;
4067 else if (*end_leader == '-')
4068 val = -val;
4069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 clear_tv(rettv);
4071 rettv->v_type = VAR_NUMBER;
4072 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
4075 return ret;
4076}
4077
4078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004079 * Evaluate an "[expr]" or "[expr:expr]" index.
4080 * "*arg" points to the '['.
4081 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4082 */
4083 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004085 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004087 int evaluate;
4088{
4089 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004091 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004092 long len = -1;
4093 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004094 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004095 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004098 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004099 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 return FAIL;
4101 }
4102
Bram Moolenaar8c711452005-01-14 21:53:12 +00004103 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105 /*
4106 * dict.name
4107 */
4108 key = *arg + 1;
4109 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4110 ;
4111 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004112 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004113 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114 }
4115 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 /*
4118 * something[idx]
4119 *
4120 * Get the (first) variable from inside the [].
4121 */
4122 *arg = skipwhite(*arg + 1);
4123 if (**arg == ':')
4124 empty1 = TRUE;
4125 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4126 return FAIL;
4127
4128 /*
4129 * Get the second variable from inside the [:].
4130 */
4131 if (**arg == ':')
4132 {
4133 range = TRUE;
4134 *arg = skipwhite(*arg + 1);
4135 if (**arg == ']')
4136 empty2 = TRUE;
4137 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4138 {
4139 clear_tv(&var1);
4140 return FAIL;
4141 }
4142 }
4143
4144 /* Check for the ']'. */
4145 if (**arg != ']')
4146 {
4147 EMSG(_(e_missbrac));
4148 clear_tv(&var1);
4149 if (range)
4150 clear_tv(&var2);
4151 return FAIL;
4152 }
4153 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004154 }
4155
4156 if (evaluate)
4157 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004158 n1 = 0;
4159 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161 n1 = get_tv_number(&var1);
4162 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004163 }
4164 if (range)
4165 {
4166 if (empty2)
4167 n2 = -1;
4168 else
4169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 n2 = get_tv_number(&var2);
4171 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 }
4173 }
4174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004176 {
4177 case VAR_NUMBER:
4178 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004180 len = (long)STRLEN(s);
4181 if (range)
4182 {
4183 /* The resulting variable is a substring. If the indexes
4184 * are out of range the result is empty. */
4185 if (n1 < 0)
4186 {
4187 n1 = len + n1;
4188 if (n1 < 0)
4189 n1 = 0;
4190 }
4191 if (n2 < 0)
4192 n2 = len + n2;
4193 else if (n2 >= len)
4194 n2 = len;
4195 if (n1 >= len || n2 < 0 || n1 > n2)
4196 s = NULL;
4197 else
4198 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4199 }
4200 else
4201 {
4202 /* The resulting variable is a string of a single
4203 * character. If the index is too big or negative the
4204 * result is empty. */
4205 if (n1 >= len || n1 < 0)
4206 s = NULL;
4207 else
4208 s = vim_strnsave(s + n1, 1);
4209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
4211 rettv->v_type = VAR_STRING;
4212 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213 break;
4214
4215 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004217 if (n1 < 0)
4218 n1 = len + n1;
4219 if (!empty1 && (n1 < 0 || n1 >= len))
4220 {
4221 EMSGN(_(e_listidx), n1);
4222 return FAIL;
4223 }
4224 if (range)
4225 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 list_T *l;
4227 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004228
4229 if (n2 < 0)
4230 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232 {
4233 EMSGN(_(e_listidx), n2);
4234 return FAIL;
4235 }
4236 l = list_alloc();
4237 if (l == NULL)
4238 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004240 n1 <= n2; ++n1)
4241 {
4242 if (list_append_tv(l, &item->li_tv) == FAIL)
4243 {
4244 list_free(l);
4245 return FAIL;
4246 }
4247 item = item->li_next;
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
4250 rettv->v_type = VAR_LIST;
4251 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004252 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004253 }
4254 else
4255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004257 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
4259 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004260 }
4261 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262
4263 case VAR_DICT:
4264 if (range)
4265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004266 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 if (len == -1)
4268 clear_tv(&var1);
4269 return FAIL;
4270 }
4271 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004273
4274 if (len == -1)
4275 {
4276 key = get_tv_string(&var1);
4277 if (*key == NUL)
4278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004279 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004280 clear_tv(&var1);
4281 return FAIL;
4282 }
4283 }
4284
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004285 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004286
4287 if (item == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004288 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004289 if (len == -1)
4290 clear_tv(&var1);
4291 if (item == NULL)
4292 return FAIL;
4293
4294 copy_tv(&item->di_tv, &var1);
4295 clear_tv(rettv);
4296 *rettv = var1;
4297 }
4298 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004299 }
4300 }
4301
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004302 return OK;
4303}
4304
4305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 * Get an option value.
4307 * "arg" points to the '&' or '+' before the option name.
4308 * "arg" is advanced to character after the option name.
4309 * Return OK or FAIL.
4310 */
4311 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int evaluate;
4316{
4317 char_u *option_end;
4318 long numval;
4319 char_u *stringval;
4320 int opt_type;
4321 int c;
4322 int working = (**arg == '+'); /* has("+option") */
4323 int ret = OK;
4324 int opt_flags;
4325
4326 /*
4327 * Isolate the option name and find its value.
4328 */
4329 option_end = find_option_end(arg, &opt_flags);
4330 if (option_end == NULL)
4331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 EMSG2(_("E112: Option name missing: %s"), *arg);
4334 return FAIL;
4335 }
4336
4337 if (!evaluate)
4338 {
4339 *arg = option_end;
4340 return OK;
4341 }
4342
4343 c = *option_end;
4344 *option_end = NUL;
4345 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004346 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 if (opt_type == -3) /* invalid name */
4349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 EMSG2(_("E113: Unknown option: %s"), *arg);
4352 ret = FAIL;
4353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 if (opt_type == -2) /* hidden string option */
4357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 rettv->v_type = VAR_STRING;
4359 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 else if (opt_type == -1) /* hidden number option */
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else if (opt_type == 1) /* number option */
4367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 rettv->v_type = VAR_NUMBER;
4369 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 else /* string option */
4372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 rettv->v_type = VAR_STRING;
4374 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377 else if (working && (opt_type == -2 || opt_type == -1))
4378 ret = FAIL;
4379
4380 *option_end = c; /* put back for error messages */
4381 *arg = option_end;
4382
4383 return ret;
4384}
4385
4386/*
4387 * Allocate a variable for a string constant.
4388 * Return OK or FAIL.
4389 */
4390 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 int evaluate;
4395{
4396 char_u *p;
4397 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 int extra = 0;
4399
4400 /*
4401 * Find the end of the string, skipping backslashed characters.
4402 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004403 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
4405 if (*p == '\\' && p[1] != NUL)
4406 {
4407 ++p;
4408 /* A "\<x>" form occupies at least 4 characters, and produces up
4409 * to 6 characters: reserve space for 2 extra */
4410 if (*p == '<')
4411 extra += 2;
4412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414
4415 if (*p != '"')
4416 {
4417 EMSG2(_("E114: Missing quote: %s"), *arg);
4418 return FAIL;
4419 }
4420
4421 /* If only parsing, set *arg and return here */
4422 if (!evaluate)
4423 {
4424 *arg = p + 1;
4425 return OK;
4426 }
4427
4428 /*
4429 * Copy the string into allocated memory, handling backslashed
4430 * characters.
4431 */
4432 name = alloc((unsigned)(p - *arg + extra));
4433 if (name == NULL)
4434 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 rettv->v_type = VAR_STRING;
4436 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 if (*p == '\\')
4441 {
4442 switch (*++p)
4443 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004444 case 'b': *name++ = BS; ++p; break;
4445 case 'e': *name++ = ESC; ++p; break;
4446 case 'f': *name++ = FF; ++p; break;
4447 case 'n': *name++ = NL; ++p; break;
4448 case 'r': *name++ = CAR; ++p; break;
4449 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 case 'X': /* hex: "\x1", "\x12" */
4452 case 'x':
4453 case 'u': /* Unicode: "\u0023" */
4454 case 'U':
4455 if (vim_isxdigit(p[1]))
4456 {
4457 int n, nr;
4458 int c = toupper(*p);
4459
4460 if (c == 'X')
4461 n = 2;
4462 else
4463 n = 4;
4464 nr = 0;
4465 while (--n >= 0 && vim_isxdigit(p[1]))
4466 {
4467 ++p;
4468 nr = (nr << 4) + hex2nr(*p);
4469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471#ifdef FEAT_MBYTE
4472 /* For "\u" store the number according to
4473 * 'encoding'. */
4474 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004475 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 else
4477#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004478 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 break;
4481
4482 /* octal: "\1", "\12", "\123" */
4483 case '0':
4484 case '1':
4485 case '2':
4486 case '3':
4487 case '4':
4488 case '5':
4489 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 case '7': *name = *p++ - '0';
4491 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 *name = (*name << 3) + *p++ - '0';
4494 if (*p >= '0' && *p <= '7')
4495 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 break;
4499
4500 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (extra != 0)
4503 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506 }
4507 /* FALLTHROUGH */
4508
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 break;
4511 }
4512 }
4513 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 *arg = p + 1;
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 return OK;
4521}
4522
4523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 * Return OK or FAIL.
4526 */
4527 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004528get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 int evaluate;
4532{
4533 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004534 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004535 int reduce = 0;
4536
4537 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004539 */
4540 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004543 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004545 break;
4546 ++reduce;
4547 ++p;
4548 }
4549 }
4550
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004552 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004554 return FAIL;
4555 }
4556
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004558 if (!evaluate)
4559 {
4560 *arg = p + 1;
4561 return OK;
4562 }
4563
4564 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004566 */
4567 str = alloc((unsigned)((p - *arg) - reduce));
4568 if (str == NULL)
4569 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 rettv->v_type = VAR_STRING;
4571 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004572
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004574 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004576 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004578 break;
4579 ++p;
4580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004583 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004584 *arg = p + 1;
4585
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004586 return OK;
4587}
4588
4589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 * Allocate a variable for a List and fill it from "*arg".
4591 * Return OK or FAIL.
4592 */
4593 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004596 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 int evaluate;
4598{
Bram Moolenaar33570922005-01-25 22:26:29 +00004599 list_T *l = NULL;
4600 typval_T tv;
4601 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602
4603 if (evaluate)
4604 {
4605 l = list_alloc();
4606 if (l == NULL)
4607 return FAIL;
4608 }
4609
4610 *arg = skipwhite(*arg + 1);
4611 while (**arg != ']' && **arg != NUL)
4612 {
4613 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4614 goto failret;
4615 if (evaluate)
4616 {
4617 item = listitem_alloc();
4618 if (item != NULL)
4619 {
4620 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004621 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 list_append(l, item);
4623 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004624 else
4625 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 }
4627
4628 if (**arg == ']')
4629 break;
4630 if (**arg != ',')
4631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004632 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 goto failret;
4634 }
4635 *arg = skipwhite(*arg + 1);
4636 }
4637
4638 if (**arg != ']')
4639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641failret:
4642 if (evaluate)
4643 list_free(l);
4644 return FAIL;
4645 }
4646
4647 *arg = skipwhite(*arg + 1);
4648 if (evaluate)
4649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 rettv->v_type = VAR_LIST;
4651 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 ++l->lv_refcount;
4653 }
4654
4655 return OK;
4656}
4657
4658/*
4659 * Allocate an empty header for a list.
4660 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662list_alloc()
4663{
Bram Moolenaar33570922005-01-25 22:26:29 +00004664 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665}
4666
4667/*
4668 * Unreference a list: decrement the reference count and free it when it
4669 * becomes zero.
4670 */
4671 static void
4672list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004673 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674{
4675 if (l != NULL && --l->lv_refcount <= 0)
4676 list_free(l);
4677}
4678
4679/*
4680 * Free a list, including all items it points to.
4681 * Ignores the reference count.
4682 */
4683 static void
4684list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004685 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686{
Bram Moolenaar33570922005-01-25 22:26:29 +00004687 listitem_T *item;
4688 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689
4690 for (item = l->lv_first; item != NULL; item = next)
4691 {
4692 next = item->li_next;
4693 listitem_free(item);
4694 }
4695 vim_free(l);
4696}
4697
4698/*
4699 * Allocate a list item.
4700 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702listitem_alloc()
4703{
Bram Moolenaar33570922005-01-25 22:26:29 +00004704 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705}
4706
4707/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004708 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 */
4710 static void
4711listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 vim_free(item);
4716}
4717
4718/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004719 * Remove a list item from a List and free it. Also clears the value.
4720 */
4721 static void
4722listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 list_T *l;
4724 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004725{
4726 list_remove(l, item, item);
4727 listitem_free(item);
4728}
4729
4730/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 * Get the number of items in a list.
4732 */
4733 static long
4734list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004735 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736{
Bram Moolenaar33570922005-01-25 22:26:29 +00004737 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 long len = 0;
4739
4740 if (l == NULL)
4741 return 0L;
4742 for (item = l->lv_first; item != NULL; item = item->li_next)
4743 ++len;
4744 return len;
4745}
4746
4747/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004748 * Return TRUE when two lists have exactly the same values.
4749 */
4750 static int
4751list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004752 list_T *l1;
4753 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004754 int ic; /* ignore case for strings */
4755{
Bram Moolenaar33570922005-01-25 22:26:29 +00004756 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004757
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004758 if (list_len(l1) != list_len(l2))
4759 return FALSE;
4760
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004761 for (item1 = l1->lv_first, item2 = l2->lv_first;
4762 item1 != NULL && item2 != NULL;
4763 item1 = item1->li_next, item2 = item2->li_next)
4764 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4765 return FALSE;
4766 return item1 == NULL && item2 == NULL;
4767}
4768
4769/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004770 * Return TRUE when two dictionaries have exactly the same key/values.
4771 */
4772 static int
4773dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 dict_T *d1;
4775 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004776 int ic; /* ignore case for strings */
4777{
Bram Moolenaar33570922005-01-25 22:26:29 +00004778 hashitem_T *hi;
4779 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004780 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004781
4782 if (dict_len(d1) != dict_len(d2))
4783 return FALSE;
4784
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 todo = d1->dv_hashtab.ht_used;
4786 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004787 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004788 if (!HASHITEM_EMPTY(hi))
4789 {
4790 item2 = dict_find(d2, hi->hi_key, -1);
4791 if (item2 == NULL)
4792 return FALSE;
4793 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4794 return FALSE;
4795 --todo;
4796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004797 }
4798 return TRUE;
4799}
4800
4801/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004802 * Return TRUE if "tv1" and "tv2" have the same value.
4803 * Compares the items just like "==" would compare them.
4804 */
4805 static int
4806tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004807 typval_T *tv1;
4808 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004809 int ic; /* ignore case */
4810{
4811 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4812
4813 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4814 {
4815 /* recursive! */
4816 if (tv1->v_type != tv2->v_type
4817 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4818 return FALSE;
4819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004820 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4821 {
4822 /* recursive! */
4823 if (tv1->v_type != tv2->v_type
4824 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4825 return FALSE;
4826 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004827 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4828 {
4829 if (tv1->v_type != tv2->v_type
4830 || tv1->vval.v_string == NULL
4831 || tv2->vval.v_string == NULL
4832 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4833 return FALSE;
4834 }
4835 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4836 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004837 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4838 * Don't consider "4x" to be equal to 4. */
4839 if ((tv1->v_type == VAR_STRING
4840 && !string_isa_number(tv1->vval.v_string))
4841 || (tv2->v_type == VAR_STRING
4842 && !string_isa_number(tv2->vval.v_string)))
4843 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004844 if (get_tv_number(tv1) != get_tv_number(tv2))
4845 return FALSE;
4846 }
4847 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4848 get_tv_string_buf(tv2, buf2)) != 0)
4849 return FALSE;
4850 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4851 get_tv_string_buf(tv2, buf2)) != 0)
4852 return FALSE;
4853 return TRUE;
4854}
4855
4856/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004857 * Return TRUE if "tv" is a number without other non-white characters.
4858 */
4859 static int
4860string_isa_number(s)
4861 char_u *s;
4862{
4863 int len;
4864
4865 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4866 return len > 0 && *skipwhite(s + len) == NUL;
4867}
4868
4869/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 * Locate item with index "n" in list "l" and return it.
4871 * A negative index is counted from the end; -1 is the last item.
4872 * Returns NULL when "n" is out of range.
4873 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004874 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 long n;
4878{
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 long idx;
4881
4882 if (l == NULL)
4883 return NULL;
4884 if (n < 0)
4885 {
4886 idx = -1; /* search from the end */
4887 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4888 --idx;
4889 }
4890 else
4891 {
4892 idx = 0; /* search from the start */
4893 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4894 ++idx;
4895 }
4896 if (idx != n)
4897 return NULL;
4898 return item;
4899}
4900
4901/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004902 * Locate "item" list "l" and return its index.
4903 * Returns -1 when "item" is not in the list.
4904 */
4905 static long
4906list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004907 list_T *l;
4908 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004909{
4910 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00004911 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004912
4913 if (l == NULL)
4914 return -1;
4915 idx = 0;
4916 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4917 ++idx;
4918 if (li == NULL)
4919 return -1;
4920 return idx;;
4921}
4922
4923/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004924 * Like list_find(), but also find an item just past the end.
4925 * "*ip" is the item to find.
4926 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4927 * Returns NULL when item not found or item is just past the end.
4928 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 static listitem_T *
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004930list_find_ext(l, ip)
Bram Moolenaar33570922005-01-25 22:26:29 +00004931 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004932 long *ip;
4933{
4934 long n;
Bram Moolenaar33570922005-01-25 22:26:29 +00004935 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004936
4937 if (*ip < 0)
4938 {
4939 /* Count from the end: -1 is before last item. */
4940 item = l->lv_last;
4941 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4942 item = item->li_prev;
4943 if (item == NULL)
4944 n = 1; /* error! */
4945 }
4946 else
4947 {
4948 item = l->lv_first;
4949 for (n = *ip; n > 0 && item != NULL; --n)
4950 item = item->li_next;
4951 }
4952 *ip = n;
4953 return item;
4954}
4955
4956/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 * Append item "item" to the end of list "l".
4958 */
4959 static void
4960list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004961 list_T *l;
4962 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004963{
4964 if (l->lv_last == NULL)
4965 {
4966 /* empty list */
4967 l->lv_first = item;
4968 l->lv_last = item;
4969 item->li_prev = NULL;
4970 }
4971 else
4972 {
4973 l->lv_last->li_next = item;
4974 item->li_prev = l->lv_last;
4975 l->lv_last = item;
4976 }
4977 item->li_next = NULL;
4978}
4979
4980/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004982 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 */
4984 static int
4985list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004986 list_T *l;
4987 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988{
Bram Moolenaar33570922005-01-25 22:26:29 +00004989 listitem_T *ni = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990
4991 if (ni == NULL)
4992 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994 list_append(l, ni);
4995 return OK;
4996}
4997
4998/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004999 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005000 * If "item" is NULL append at the end.
5001 * Return FAIL when out of memory.
5002 */
5003 static int
5004list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005005 list_T *l;
5006 typval_T *tv;
5007 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005008{
Bram Moolenaar33570922005-01-25 22:26:29 +00005009 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005010
5011 if (ni == NULL)
5012 return FAIL;
5013 copy_tv(tv, &ni->li_tv);
5014 if (item == NULL)
5015 /* Append new item at end of list. */
5016 list_append(l, ni);
5017 else
5018 {
5019 /* Insert new item before existing item. */
5020 ni->li_prev = item->li_prev;
5021 ni->li_next = item;
5022 if (item->li_prev == NULL)
5023 l->lv_first = ni;
5024 else
5025 item->li_prev->li_next = ni;
5026 item->li_prev = ni;
5027 }
5028 return OK;
5029}
5030
5031/*
5032 * Extend "l1" with "l2".
5033 * If "bef" is NULL append at the end, otherwise insert before this item.
5034 * Returns FAIL when out of memory.
5035 */
5036 static int
5037list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005038 list_T *l1;
5039 list_T *l2;
5040 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005041{
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005043
5044 for (item = l2->lv_first; item != NULL; item = item->li_next)
5045 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5046 return FAIL;
5047 return OK;
5048}
5049
5050/*
5051 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5052 * Return FAIL when out of memory.
5053 */
5054 static int
5055list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005056 list_T *l1;
5057 list_T *l2;
5058 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005059{
Bram Moolenaar33570922005-01-25 22:26:29 +00005060 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005061
5062 /* make a copy of the first list. */
5063 l = list_copy(l1, FALSE);
5064 if (l == NULL)
5065 return FAIL;
5066 tv->v_type = VAR_LIST;
5067 tv->vval.v_list = l;
5068
5069 /* append all items from the second list */
5070 return list_extend(l, l2, NULL);
5071}
5072
5073/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005074 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 * The refcount of the new list is set to 1.
5076 * Returns NULL when out of memory.
5077 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005078 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079list_copy(orig, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 int deep;
5082{
Bram Moolenaar33570922005-01-25 22:26:29 +00005083 list_T *copy;
5084 listitem_T *item;
5085 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086
5087 if (orig == NULL)
5088 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005089
5090 copy = list_alloc();
5091 if (copy != NULL)
5092 {
5093 for (item = orig->lv_first; item != NULL; item = item->li_next)
5094 {
5095 ni = listitem_alloc();
5096 if (ni == NULL)
5097 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098 if (deep)
5099 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102 list_append(copy, ni);
5103 }
5104 ++copy->lv_refcount;
5105 }
5106
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 return copy;
5108}
5109
5110/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005111 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005112 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005114 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005115list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005116 list_T *l;
5117 listitem_T *item;
5118 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005119{
Bram Moolenaar33570922005-01-25 22:26:29 +00005120 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005122 /* notify watchers */
5123 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005125 list_fix_watch(l, ip);
5126 if (ip == item2)
5127 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005129
5130 if (item2->li_next == NULL)
5131 l->lv_last = item->li_prev;
5132 else
5133 item2->li_next->li_prev = item->li_prev;
5134 if (item->li_prev == NULL)
5135 l->lv_first = item2->li_next;
5136 else
5137 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138}
5139
5140/*
5141 * Return an allocated string with the string representation of a list.
5142 * May return NULL.
5143 */
5144 static char_u *
5145list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147{
5148 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149
5150 if (tv->vval.v_list == NULL)
5151 return NULL;
5152 ga_init2(&ga, (int)sizeof(char), 80);
5153 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005154 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 ga_append(&ga, ']');
5156 ga_append(&ga, NUL);
5157 return (char_u *)ga.ga_data;
5158}
5159
5160/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005161 * Join list "l" into a string in "*gap", using separator "sep".
5162 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
5163 */
5164 static void
5165list_join(gap, l, sep, echo)
5166 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005167 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005168 char_u *sep;
5169 int echo;
5170{
5171 int first = TRUE;
5172 char_u *tofree;
5173 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 char_u *s;
5176
5177 for (item = l->lv_first; item != NULL; item = item->li_next)
5178 {
5179 if (first)
5180 first = FALSE;
5181 else
5182 ga_concat(gap, sep);
5183
5184 if (echo)
5185 s = echo_string(&item->li_tv, &tofree, numbuf);
5186 else
5187 s = tv2string(&item->li_tv, &tofree, numbuf);
5188 if (s != NULL)
5189 ga_concat(gap, s);
5190 vim_free(tofree);
5191 }
5192}
5193
5194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 * Allocate an empty header for a dictionary.
5196 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005197 static dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198dict_alloc()
5199{
Bram Moolenaar33570922005-01-25 22:26:29 +00005200 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005201
Bram Moolenaar33570922005-01-25 22:26:29 +00005202 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005203 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005204 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005205 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005206 d->dv_lock = 0;
5207 d->dv_refcount = 0;
5208 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005209 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210}
5211
5212/*
5213 * Unreference a Dictionary: decrement the reference count and free it when it
5214 * becomes zero.
5215 */
5216 static void
5217dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005218 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219{
5220 if (d != NULL && --d->dv_refcount <= 0)
5221 dict_free(d);
5222}
5223
5224/*
5225 * Free a Dictionary, including all items it contains.
5226 * Ignores the reference count.
5227 */
5228 static void
5229dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005230 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005232 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005233 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005235 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 * hashtab. Must not try to resize the hashtab! */
5237 todo = d->dv_hashtab.ht_used;
5238 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005240 if (!HASHITEM_EMPTY(hi))
5241 {
5242 dictitem_free(HI2DI(hi));
5243 --todo;
5244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005246 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 vim_free(d);
5248}
5249
5250/*
5251 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005252 * The "key" is copied to the new item.
5253 * Note that the value of the item "di_tv" still needs to be initialized!
5254 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005256 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005257dictitem_alloc(key)
5258 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259{
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005261
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005263 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005264 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005265 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005266 di->di_flags = 0;
5267 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005268 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269}
5270
5271/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 * Make a copy of a Dictionary item.
5273 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005275dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005276 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005277{
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005279
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005281 if (di != NULL)
5282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005283 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005284 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005285 copy_tv(&org->di_tv, &di->di_tv);
5286 }
5287 return di;
5288}
5289
5290/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005291 * Remove item "item" from Dictionary "dict" and free it.
5292 */
5293 static void
5294dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 dict_T *dict;
5296 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005297{
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005299
Bram Moolenaar33570922005-01-25 22:26:29 +00005300 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005301 if (HASHITEM_EMPTY(hi))
5302 EMSG2(_(e_intern2), "dictitem_remove()");
5303 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005304 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005305 dictitem_free(item);
5306}
5307
5308/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 * Free a dict item. Also clears the value.
5310 */
5311 static void
5312dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 clear_tv(&item->di_tv);
5316 vim_free(item);
5317}
5318
5319/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005320 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5321 * The refcount of the new dict is set to 1.
5322 * Returns NULL when out of memory.
5323 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 static dict_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005325dict_copy(orig, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +00005326 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005327 int deep;
5328{
Bram Moolenaar33570922005-01-25 22:26:29 +00005329 dict_T *copy;
5330 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005331 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005332 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005333
5334 if (orig == NULL)
5335 return NULL;
5336
5337 copy = dict_alloc();
5338 if (copy != NULL)
5339 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 todo = orig->dv_hashtab.ht_used;
5341 for (hi = orig->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005343 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005345 --todo;
5346
5347 di = dictitem_alloc(hi->hi_key);
5348 if (di == NULL)
5349 break;
5350 if (deep)
5351 item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep);
5352 else
5353 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5354 if (dict_add(copy, di) == FAIL)
5355 {
5356 dictitem_free(di);
5357 break;
5358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005361
Bram Moolenaare9a41262005-01-15 22:18:47 +00005362 ++copy->dv_refcount;
5363 }
5364
5365 return copy;
5366}
5367
5368/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005370 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005372 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 dict_T *d;
5375 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376{
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378}
5379
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005381 * Get the number of items in a Dictionary.
5382 */
5383 static long
5384dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005386{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005387 if (d == NULL)
5388 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005390}
5391
5392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 * Find item "key[len]" in Dictionary "d".
5394 * If "len" is negative use strlen(key).
5395 * Returns NULL when not found.
5396 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005398dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 char_u *key;
5401 int len;
5402{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005403#define AKEYLEN 200
5404 char_u buf[AKEYLEN];
5405 char_u *akey;
5406 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005407 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005409 if (len < 0)
5410 akey = key;
5411 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005412 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005413 tofree = akey = vim_strnsave(key, len);
5414 if (akey == NULL)
5415 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005416 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005417 else
5418 {
5419 /* Avoid a malloc/free by using buf[]. */
5420 STRNCPY(buf, key, len);
5421 buf[len] = NUL;
5422 akey = buf;
5423 }
5424
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005426 vim_free(tofree);
5427 if (HASHITEM_EMPTY(hi))
5428 return NULL;
5429 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430}
5431
5432/*
5433 * Return an allocated string with the string representation of a Dictionary.
5434 * May return NULL.
5435 */
5436 static char_u *
5437dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439{
5440 garray_T ga;
5441 int first = TRUE;
5442 char_u *tofree;
5443 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005444 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005446 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005447 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005449 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 return NULL;
5451 ga_init2(&ga, (int)sizeof(char), 80);
5452 ga_append(&ga, '{');
5453
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 todo = d->dv_hashtab.ht_used;
5455 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005457 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005459 --todo;
5460
5461 if (first)
5462 first = FALSE;
5463 else
5464 ga_concat(&ga, (char_u *)", ");
5465
5466 tofree = string_quote(hi->hi_key, FALSE);
5467 if (tofree != NULL)
5468 {
5469 ga_concat(&ga, tofree);
5470 vim_free(tofree);
5471 }
5472 ga_concat(&ga, (char_u *)": ");
5473 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5474 if (s != NULL)
5475 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476 vim_free(tofree);
5477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 }
5479
5480 ga_append(&ga, '}');
5481 ga_append(&ga, NUL);
5482 return (char_u *)ga.ga_data;
5483}
5484
5485/*
5486 * Allocate a variable for a Dictionary and fill it from "*arg".
5487 * Return OK or FAIL. Returns NOTDONE for {expr}.
5488 */
5489 static int
5490get_dict_tv(arg, rettv, evaluate)
5491 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 int evaluate;
5494{
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 dict_T *d = NULL;
5496 typval_T tvkey;
5497 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005501 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502
5503 /*
5504 * First check if it's not a curly-braces thing: {expr}.
5505 * Must do this without evaluating, otherwise a function may be called
5506 * twice. Unfortunately this means we need to call eval1() twice for the
5507 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005508 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005509 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510 if (*start != '}')
5511 {
5512 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5513 return FAIL;
5514 if (*start == '}')
5515 return NOTDONE;
5516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517
5518 if (evaluate)
5519 {
5520 d = dict_alloc();
5521 if (d == NULL)
5522 return FAIL;
5523 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005524 tvkey.v_type = VAR_UNKNOWN;
5525 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526
5527 *arg = skipwhite(*arg + 1);
5528 while (**arg != '}' && **arg != NUL)
5529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005530 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 goto failret;
5532 if (**arg != ':')
5533 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005534 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005535 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 goto failret;
5537 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005538 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539 if (*key == NUL)
5540 {
5541 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005542 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 goto failret;
5544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005545
5546 *arg = skipwhite(*arg + 1);
5547 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 goto failret;
5551 }
5552 if (evaluate)
5553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005554 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (item != NULL)
5556 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005557 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005558 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 clear_tv(&tv);
5560 goto failret;
5561 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005562 item = dictitem_alloc(key);
5563 clear_tv(&tvkey);
5564 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005567 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005568 if (dict_add(d, item) == FAIL)
5569 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 }
5571 }
5572
5573 if (**arg == '}')
5574 break;
5575 if (**arg != ',')
5576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005577 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 goto failret;
5579 }
5580 *arg = skipwhite(*arg + 1);
5581 }
5582
5583 if (**arg != '}')
5584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005585 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586failret:
5587 if (evaluate)
5588 dict_free(d);
5589 return FAIL;
5590 }
5591
5592 *arg = skipwhite(*arg + 1);
5593 if (evaluate)
5594 {
5595 rettv->v_type = VAR_DICT;
5596 rettv->vval.v_dict = d;
5597 ++d->dv_refcount;
5598 }
5599
5600 return OK;
5601}
5602
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 * Return a string with the string representation of a variable.
5605 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005606 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005607 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 * May return NULL;
5609 */
5610 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005616 static int recurse = 0;
5617 char_u *r = NULL;
5618
Bram Moolenaar33570922005-01-25 22:26:29 +00005619 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005620 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005621 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005622 *tofree = NULL;
5623 return NULL;
5624 }
5625 ++recurse;
5626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 switch (tv->v_type)
5628 {
5629 case VAR_FUNC:
5630 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005631 r = tv->vval.v_string;
5632 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 case VAR_LIST:
5634 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005635 r = *tofree;
5636 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 case VAR_DICT:
5638 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 r = *tofree;
5640 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 case VAR_STRING:
5642 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005643 *tofree = NULL;
5644 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005647 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005648 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005650
5651 --recurse;
5652 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005653}
5654
5655/*
5656 * Return a string with the string representation of a variable.
5657 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5658 * "numbuf" is used for a number.
5659 * Puts quotes around strings, so that they can be parsed back by eval().
5660 * May return NULL;
5661 */
5662 static char_u *
5663tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005664 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 char_u **tofree;
5666 char_u *numbuf;
5667{
5668 switch (tv->v_type)
5669 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670 case VAR_FUNC:
5671 *tofree = string_quote(tv->vval.v_string, TRUE);
5672 return *tofree;
5673 case VAR_STRING:
5674 *tofree = string_quote(tv->vval.v_string, FALSE);
5675 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005676 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005677 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005679 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005683 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005684}
5685
5686/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005687 * Return string "str" in ' quotes, doubling ' characters.
5688 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005690 */
5691 static char_u *
5692string_quote(str, function)
5693 char_u *str;
5694 int function;
5695{
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 char_u *p, *r, *s;
5698
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 len = (function ? 13 : 3);
5700 if (str != NULL)
5701 {
5702 len += STRLEN(str);
5703 for (p = str; *p != NUL; mb_ptr_adv(p))
5704 if (*p == '\'')
5705 ++len;
5706 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707 s = r = alloc(len);
5708 if (r != NULL)
5709 {
5710 if (function)
5711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 r += 10;
5714 }
5715 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005717 if (str != NULL)
5718 for (p = str; *p != NUL; )
5719 {
5720 if (*p == '\'')
5721 *r++ = '\'';
5722 MB_COPY_CHAR(p, r);
5723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005725 if (function)
5726 *r++ = ')';
5727 *r++ = NUL;
5728 }
5729 return s;
5730}
5731
5732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 * Get the value of an environment variable.
5734 * "arg" is pointing to the '$'. It is advanced to after the name.
5735 * If the environment variable was not set, silently assume it is empty.
5736 * Always return OK.
5737 */
5738 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005739get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 int evaluate;
5743{
5744 char_u *string = NULL;
5745 int len;
5746 int cc;
5747 char_u *name;
5748
5749 ++*arg;
5750 name = *arg;
5751 len = get_env_len(arg);
5752 if (evaluate)
5753 {
5754 if (len != 0)
5755 {
5756 cc = name[len];
5757 name[len] = NUL;
5758 /* first try mch_getenv(), fast for normal environment vars */
5759 string = mch_getenv(name);
5760 if (string != NULL && *string != NUL)
5761 string = vim_strsave(string);
5762 else
5763 {
5764 /* next try expanding things like $VIM and ${HOME} */
5765 string = expand_env_save(name - 1);
5766 if (string != NULL && *string == '$')
5767 {
5768 vim_free(string);
5769 string = NULL;
5770 }
5771 }
5772 name[len] = cc;
5773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005774 rettv->v_type = VAR_STRING;
5775 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
5777
5778 return OK;
5779}
5780
5781/*
5782 * Array with names and number of arguments of all internal functions
5783 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5784 */
5785static struct fst
5786{
5787 char *f_name; /* function name */
5788 char f_min_argc; /* minimal number of arguments */
5789 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00005790 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792} functions[] =
5793{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005794 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {"append", 2, 2, f_append},
5796 {"argc", 0, 0, f_argc},
5797 {"argidx", 0, 0, f_argidx},
5798 {"argv", 1, 1, f_argv},
5799 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005800 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {"bufexists", 1, 1, f_bufexists},
5802 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5803 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5804 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5805 {"buflisted", 1, 1, f_buflisted},
5806 {"bufloaded", 1, 1, f_bufloaded},
5807 {"bufname", 1, 1, f_bufname},
5808 {"bufnr", 1, 1, f_bufnr},
5809 {"bufwinnr", 1, 1, f_bufwinnr},
5810 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005811 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005812 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {"char2nr", 1, 1, f_char2nr},
5814 {"cindent", 1, 1, f_cindent},
5815 {"col", 1, 1, f_col},
5816 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {"cscope_connection",0,3, f_cscope_connection},
5820 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {"delete", 1, 1, f_delete},
5823 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005824 {"diff_filler", 1, 1, f_diff_filler},
5825 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005826 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005828 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {"eventhandler", 0, 0, f_eventhandler},
5830 {"executable", 1, 1, f_executable},
5831 {"exists", 1, 1, f_exists},
5832 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5835 {"filereadable", 1, 1, f_filereadable},
5836 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005838 {"finddir", 1, 3, f_finddir},
5839 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {"fnamemodify", 2, 2, f_fnamemodify},
5841 {"foldclosed", 1, 1, f_foldclosed},
5842 {"foldclosedend", 1, 1, f_foldclosedend},
5843 {"foldlevel", 1, 1, f_foldlevel},
5844 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005845 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005848 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {"getbufvar", 2, 2, f_getbufvar},
5850 {"getchar", 0, 1, f_getchar},
5851 {"getcharmod", 0, 0, f_getcharmod},
5852 {"getcmdline", 0, 0, f_getcmdline},
5853 {"getcmdpos", 0, 0, f_getcmdpos},
5854 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005855 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005856 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {"getfsize", 1, 1, f_getfsize},
5858 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005859 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005860 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {"getreg", 0, 1, f_getreg},
5862 {"getregtype", 0, 1, f_getregtype},
5863 {"getwinposx", 0, 0, f_getwinposx},
5864 {"getwinposy", 0, 0, f_getwinposy},
5865 {"getwinvar", 2, 2, f_getwinvar},
5866 {"glob", 1, 1, f_glob},
5867 {"globpath", 2, 2, f_globpath},
5868 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005869 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {"hasmapto", 1, 2, f_hasmapto},
5871 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5872 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5873 {"histadd", 2, 2, f_histadd},
5874 {"histdel", 1, 2, f_histdel},
5875 {"histget", 1, 2, f_histget},
5876 {"histnr", 1, 1, f_histnr},
5877 {"hlID", 1, 1, f_hlID},
5878 {"hlexists", 1, 1, f_hlexists},
5879 {"hostname", 0, 0, f_hostname},
5880 {"iconv", 3, 3, f_iconv},
5881 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {"input", 1, 2, f_input},
5884 {"inputdialog", 1, 3, f_inputdialog},
5885 {"inputrestore", 0, 0, f_inputrestore},
5886 {"inputsave", 0, 0, f_inputsave},
5887 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005890 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {"libcall", 3, 3, f_libcall},
5897 {"libcallnr", 3, 3, f_libcallnr},
5898 {"line", 1, 1, f_line},
5899 {"line2byte", 1, 1, f_line2byte},
5900 {"lispindent", 1, 1, f_lispindent},
5901 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {"maparg", 1, 2, f_maparg},
5904 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005905 {"match", 2, 4, f_match},
5906 {"matchend", 2, 4, f_matchend},
5907 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005908 {"max", 1, 1, f_max},
5909 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {"mode", 0, 0, f_mode},
5911 {"nextnonblank", 1, 1, f_nextnonblank},
5912 {"nr2char", 1, 1, f_nr2char},
5913 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005914 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {"remote_expr", 2, 3, f_remote_expr},
5916 {"remote_foreground", 1, 1, f_remote_foreground},
5917 {"remote_peek", 1, 2, f_remote_peek},
5918 {"remote_read", 1, 1, f_remote_read},
5919 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005920 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005922 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005924 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {"search", 1, 2, f_search},
5926 {"searchpair", 3, 5, f_searchpair},
5927 {"server2client", 2, 2, f_server2client},
5928 {"serverlist", 0, 0, f_serverlist},
5929 {"setbufvar", 3, 3, f_setbufvar},
5930 {"setcmdpos", 1, 1, f_setcmdpos},
5931 {"setline", 2, 2, f_setline},
5932 {"setreg", 2, 3, f_setreg},
5933 {"setwinvar", 3, 3, f_setwinvar},
5934 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005935 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937#ifdef HAVE_STRFTIME
5938 {"strftime", 1, 2, f_strftime},
5939#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {"strlen", 1, 1, f_strlen},
5943 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00005944 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {"strtrans", 1, 1, f_strtrans},
5946 {"submatch", 1, 1, f_submatch},
5947 {"substitute", 4, 4, f_substitute},
5948 {"synID", 3, 3, f_synID},
5949 {"synIDattr", 2, 3, f_synIDattr},
5950 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005951 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {"tempname", 0, 0, f_tempname},
5953 {"tolower", 1, 1, f_tolower},
5954 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005955 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {"virtcol", 1, 1, f_virtcol},
5959 {"visualmode", 0, 1, f_visualmode},
5960 {"winbufnr", 1, 1, f_winbufnr},
5961 {"wincol", 0, 0, f_wincol},
5962 {"winheight", 1, 1, f_winheight},
5963 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005964 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {"winrestcmd", 0, 0, f_winrestcmd},
5966 {"winwidth", 1, 1, f_winwidth},
5967};
5968
5969#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5970
5971/*
5972 * Function given to ExpandGeneric() to obtain the list of internal
5973 * or user defined function names.
5974 */
5975 char_u *
5976get_function_name(xp, idx)
5977 expand_T *xp;
5978 int idx;
5979{
5980 static int intidx = -1;
5981 char_u *name;
5982
5983 if (idx == 0)
5984 intidx = -1;
5985 if (intidx < 0)
5986 {
5987 name = get_user_func_name(xp, idx);
5988 if (name != NULL)
5989 return name;
5990 }
5991 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5992 {
5993 STRCPY(IObuff, functions[intidx].f_name);
5994 STRCAT(IObuff, "(");
5995 if (functions[intidx].f_max_argc == 0)
5996 STRCAT(IObuff, ")");
5997 return IObuff;
5998 }
5999
6000 return NULL;
6001}
6002
6003/*
6004 * Function given to ExpandGeneric() to obtain the list of internal or
6005 * user defined variable or function names.
6006 */
6007/*ARGSUSED*/
6008 char_u *
6009get_expr_name(xp, idx)
6010 expand_T *xp;
6011 int idx;
6012{
6013 static int intidx = -1;
6014 char_u *name;
6015
6016 if (idx == 0)
6017 intidx = -1;
6018 if (intidx < 0)
6019 {
6020 name = get_function_name(xp, idx);
6021 if (name != NULL)
6022 return name;
6023 }
6024 return get_user_var_name(xp, ++intidx);
6025}
6026
6027#endif /* FEAT_CMDL_COMPL */
6028
6029/*
6030 * Find internal function in table above.
6031 * Return index, or -1 if not found
6032 */
6033 static int
6034find_internal_func(name)
6035 char_u *name; /* name of the function */
6036{
6037 int first = 0;
6038 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6039 int cmp;
6040 int x;
6041
6042 /*
6043 * Find the function name in the table. Binary search.
6044 */
6045 while (first <= last)
6046 {
6047 x = first + ((unsigned)(last - first) >> 1);
6048 cmp = STRCMP(name, functions[x].f_name);
6049 if (cmp < 0)
6050 last = x - 1;
6051 else if (cmp > 0)
6052 first = x + 1;
6053 else
6054 return x;
6055 }
6056 return -1;
6057}
6058
6059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6061 * name it contains, otherwise return "name".
6062 */
6063 static char_u *
6064deref_func_name(name, lenp)
6065 char_u *name;
6066 int *lenp;
6067{
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 int cc;
6070
6071 cc = name[*lenp];
6072 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006073 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 {
6079 *lenp = 0;
6080 return (char_u *)""; /* just in case */
6081 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 *lenp = STRLEN(v->di_tv.vval.v_string);
6083 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 }
6085
6086 return name;
6087}
6088
6089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 * Allocate a variable for the result of a function.
6091 * Return OK or FAIL.
6092 */
6093 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006094get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6095 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 char_u *name; /* name of the function */
6097 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 char_u **arg; /* argument, pointing to the '(' */
6100 linenr_T firstline; /* first line of range */
6101 linenr_T lastline; /* last line of range */
6102 int *doesrange; /* return: function handled range */
6103 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105{
6106 char_u *argp;
6107 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int argcount = 0; /* number of arguments found */
6110
6111 /*
6112 * Get the arguments.
6113 */
6114 argp = *arg;
6115 while (argcount < MAX_FUNC_ARGS)
6116 {
6117 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6118 if (*argp == ')' || *argp == ',' || *argp == NUL)
6119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6121 {
6122 ret = FAIL;
6123 break;
6124 }
6125 ++argcount;
6126 if (*argp != ',')
6127 break;
6128 }
6129 if (*argp == ')')
6130 ++argp;
6131 else
6132 ret = FAIL;
6133
6134 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006135 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006136 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 {
6139 if (argcount == MAX_FUNC_ARGS)
6140 EMSG2(_("E740: Too many arguments for function %s"), name);
6141 else
6142 EMSG2(_("E116: Invalid arguments for function %s"), name);
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006146 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 *arg = skipwhite(argp);
6149 return ret;
6150}
6151
6152
6153/*
6154 * Call a function with its resolved parameters
6155 * Return OK or FAIL.
6156 */
6157 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 char_u *name; /* name of the function */
6161 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 linenr_T firstline; /* first line of range */
6166 linenr_T lastline; /* last line of range */
6167 int *doesrange; /* return: function handled range */
6168 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#define ERROR_UNKNOWN 0
6173#define ERROR_TOOMANY 1
6174#define ERROR_TOOFEW 2
6175#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006176#define ERROR_DICT 4
6177#define ERROR_NONE 5
6178#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 int error = ERROR_NONE;
6180 int i;
6181 int llen;
6182 ufunc_T *fp;
6183 int cc;
6184#define FLEN_FIXED 40
6185 char_u fname_buf[FLEN_FIXED + 1];
6186 char_u *fname;
6187
6188 /*
6189 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6190 * Change <SNR>123_name() to K_SNR 123_name().
6191 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6192 */
6193 cc = name[len];
6194 name[len] = NUL;
6195 llen = eval_fname_script(name);
6196 if (llen > 0)
6197 {
6198 fname_buf[0] = K_SPECIAL;
6199 fname_buf[1] = KS_EXTRA;
6200 fname_buf[2] = (int)KE_SNR;
6201 i = 3;
6202 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6203 {
6204 if (current_SID <= 0)
6205 error = ERROR_SCRIPT;
6206 else
6207 {
6208 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6209 i = (int)STRLEN(fname_buf);
6210 }
6211 }
6212 if (i + STRLEN(name + llen) < FLEN_FIXED)
6213 {
6214 STRCPY(fname_buf + i, name + llen);
6215 fname = fname_buf;
6216 }
6217 else
6218 {
6219 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6220 if (fname == NULL)
6221 error = ERROR_OTHER;
6222 else
6223 {
6224 mch_memmove(fname, fname_buf, (size_t)i);
6225 STRCPY(fname + i, name + llen);
6226 }
6227 }
6228 }
6229 else
6230 fname = name;
6231
6232 *doesrange = FALSE;
6233
6234
6235 /* execute the function if no errors detected and executing */
6236 if (evaluate && error == ERROR_NONE)
6237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006238 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 error = ERROR_UNKNOWN;
6240
6241 if (!ASCII_ISLOWER(fname[0]))
6242 {
6243 /*
6244 * User defined function.
6245 */
6246 fp = find_func(fname);
6247#ifdef FEAT_AUTOCMD
6248 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
6249 fname, fname, TRUE, NULL)
6250#ifdef FEAT_EVAL
6251 && !aborting()
6252#endif
6253 )
6254 {
6255 /* executed an autocommand, search for function again */
6256 fp = find_func(fname);
6257 }
6258#endif
6259 if (fp != NULL)
6260 {
6261 if (fp->flags & FC_RANGE)
6262 *doesrange = TRUE;
6263 if (argcount < fp->args.ga_len)
6264 error = ERROR_TOOFEW;
6265 else if (!fp->varargs && argcount > fp->args.ga_len)
6266 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006267 else if ((fp->flags & FC_DICT) && selfdict == NULL)
6268 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 else
6270 {
6271 /*
6272 * Call the user function.
6273 * Save and restore search patterns, script variables and
6274 * redo buffer.
6275 */
6276 save_search_patterns();
6277 saveRedobuff();
6278 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006279 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006280 firstline, lastline,
6281 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006282 if (--fp->calls <= 0 && isdigit(*fp->name)
6283 && fp->refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006284 /* Function was unreferenced while being used, free it
6285 * now. */
6286 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 restoreRedobuff();
6288 restore_search_patterns();
6289 error = ERROR_NONE;
6290 }
6291 }
6292 }
6293 else
6294 {
6295 /*
6296 * Find the function name in the table, call its implementation.
6297 */
6298 i = find_internal_func(fname);
6299 if (i >= 0)
6300 {
6301 if (argcount < functions[i].f_min_argc)
6302 error = ERROR_TOOFEW;
6303 else if (argcount > functions[i].f_max_argc)
6304 error = ERROR_TOOMANY;
6305 else
6306 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006308 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 error = ERROR_NONE;
6310 }
6311 }
6312 }
6313 /*
6314 * The function call (or "FuncUndefined" autocommand sequence) might
6315 * have been aborted by an error, an interrupt, or an explicitly thrown
6316 * exception that has not been caught so far. This situation can be
6317 * tested for by calling aborting(). For an error in an internal
6318 * function or for the "E132" error in call_user_func(), however, the
6319 * throw point at which the "force_abort" flag (temporarily reset by
6320 * emsg()) is normally updated has not been reached yet. We need to
6321 * update that flag first to make aborting() reliable.
6322 */
6323 update_force_abort();
6324 }
6325 if (error == ERROR_NONE)
6326 ret = OK;
6327
6328 /*
6329 * Report an error unless the argument evaluation or function call has been
6330 * cancelled due to an aborting error, an interrupt, or an exception.
6331 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006332 if (!aborting())
6333 {
6334 switch (error)
6335 {
6336 case ERROR_UNKNOWN:
6337 EMSG2(_("E117: Unknown function: %s"), name);
6338 break;
6339 case ERROR_TOOMANY:
6340 EMSG2(_(e_toomanyarg), name);
6341 break;
6342 case ERROR_TOOFEW:
6343 EMSG2(_("E119: Not enough arguments for function: %s"),
6344 name);
6345 break;
6346 case ERROR_SCRIPT:
6347 EMSG2(_("E120: Using <SID> not in a script context: %s"),
6348 name);
6349 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 case ERROR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006351 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 name);
6353 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 }
6355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 name[len] = cc;
6358 if (fname != name && fname != fname_buf)
6359 vim_free(fname);
6360
6361 return ret;
6362}
6363
6364/*********************************************
6365 * Implementation of the built-in functions
6366 */
6367
6368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006369 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 */
6371 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006372f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 typval_T *argvars;
6374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006378 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006381 if ((l = argvars[0].vval.v_list) != NULL
6382 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6383 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385 }
6386 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006387 EMSG(_(e_listreq));
6388}
6389
6390/*
6391 * "append(lnum, string/list)" function
6392 */
6393 static void
6394f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 typval_T *argvars;
6396 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006397{
6398 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 list_T *l = NULL;
6400 listitem_T *li = NULL;
6401 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006402 long added = 0;
6403
6404 rettv->vval.v_number = 1; /* Default: Failed */
6405 lnum = get_tv_lnum(argvars);
6406 if (lnum >= 0
6407 && lnum <= curbuf->b_ml.ml_line_count
6408 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006410 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006412 l = argvars[1].vval.v_list;
6413 if (l == NULL)
6414 return;
6415 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006417 for (;;)
6418 {
6419 if (l == NULL)
6420 tv = &argvars[1]; /* append a string */
6421 else if (li == NULL)
6422 break; /* end of list */
6423 else
6424 tv = &li->li_tv; /* append item from list */
6425 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6426 ++added;
6427 if (l == NULL)
6428 break;
6429 li = li->li_next;
6430 }
6431
6432 appended_lines_mark(lnum, added);
6433 if (curwin->w_cursor.lnum > lnum)
6434 curwin->w_cursor.lnum += added;
6435 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437}
6438
6439/*
6440 * "argc()" function
6441 */
6442/* ARGSUSED */
6443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006444f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 typval_T *argvars;
6446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006448 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449}
6450
6451/*
6452 * "argidx()" function
6453 */
6454/* ARGSUSED */
6455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 typval_T *argvars;
6458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461}
6462
6463/*
6464 * "argv(nr)" function
6465 */
6466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 typval_T *argvars;
6469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 int idx;
6472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006473 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006475 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006477 rettv->vval.v_string = NULL;
6478 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479}
6480
6481/*
6482 * "browse(save, title, initdir, default)" function
6483 */
6484/* ARGSUSED */
6485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 typval_T *argvars;
6488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490#ifdef FEAT_BROWSE
6491 int save;
6492 char_u *title;
6493 char_u *initdir;
6494 char_u *defname;
6495 char_u buf[NUMBUFLEN];
6496 char_u buf2[NUMBUFLEN];
6497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006498 save = get_tv_number(&argvars[0]);
6499 title = get_tv_string(&argvars[1]);
6500 initdir = get_tv_string_buf(&argvars[2], buf);
6501 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006504 do_browse(save ? BROWSE_SAVE : 0,
6505 title, defname, NULL, initdir, NULL, curbuf);
6506#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006508#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006509 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006510}
6511
6512/*
6513 * "browsedir(title, initdir)" function
6514 */
6515/* ARGSUSED */
6516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006517f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 typval_T *argvars;
6519 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006520{
6521#ifdef FEAT_BROWSE
6522 char_u *title;
6523 char_u *initdir;
6524 char_u buf[NUMBUFLEN];
6525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006526 title = get_tv_string(&argvars[0]);
6527 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006528
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006529 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006530 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535}
6536
Bram Moolenaar33570922005-01-25 22:26:29 +00006537static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006538
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539/*
6540 * Find a buffer by number or exact name.
6541 */
6542 static buf_T *
6543find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 if (avar->v_type == VAR_NUMBER)
6549 buf = buflist_findnr((int)avar->vval.v_number);
6550 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006553 if (buf == NULL)
6554 {
6555 /* No full path name match, try a match with a URL or a "nofile"
6556 * buffer, these don't use the full path. */
6557 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6558 if (buf->b_fname != NULL
6559 && (path_with_url(buf->b_fname)
6560#ifdef FEAT_QUICKFIX
6561 || bt_nofile(buf)
6562#endif
6563 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006565 break;
6566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568 return buf;
6569}
6570
6571/*
6572 * "bufexists(expr)" function
6573 */
6574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 typval_T *argvars;
6577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006579 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580}
6581
6582/*
6583 * "buflisted(expr)" function
6584 */
6585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 typval_T *argvars;
6588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 buf_T *buf;
6591
6592 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006593 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
6596/*
6597 * "bufloaded(expr)" function
6598 */
6599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 typval_T *argvars;
6602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 buf_T *buf;
6605
6606 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006607 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
Bram Moolenaar33570922005-01-25 22:26:29 +00006610static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612/*
6613 * Get buffer by number or pattern.
6614 */
6615 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006616get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 int save_magic;
6621 char_u *save_cpo;
6622 buf_T *buf;
6623
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 if (tv->v_type == VAR_NUMBER)
6625 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (name == NULL || *name == NUL)
6627 return curbuf;
6628 if (name[0] == '$' && name[1] == NUL)
6629 return lastbuf;
6630
6631 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6632 save_magic = p_magic;
6633 p_magic = TRUE;
6634 save_cpo = p_cpo;
6635 p_cpo = (char_u *)"";
6636
6637 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6638 TRUE, FALSE));
6639
6640 p_magic = save_magic;
6641 p_cpo = save_cpo;
6642
6643 /* If not found, try expanding the name, like done for bufexists(). */
6644 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006645 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646
6647 return buf;
6648}
6649
6650/*
6651 * "bufname(expr)" function
6652 */
6653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006654f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 typval_T *argvars;
6656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 buf_T *buf;
6659
6660 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006661 buf = get_buf_tv(&argvars[0]);
6662 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006664 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 --emsg_off;
6668}
6669
6670/*
6671 * "bufnr(expr)" function
6672 */
6673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 typval_T *argvars;
6676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 buf_T *buf;
6679
6680 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006681 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006683 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 --emsg_off;
6687}
6688
6689/*
6690 * "bufwinnr(nr)" function
6691 */
6692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006693f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006694 typval_T *argvars;
6695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697#ifdef FEAT_WINDOWS
6698 win_T *wp;
6699 int winnr = 0;
6700#endif
6701 buf_T *buf;
6702
6703 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006704 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#ifdef FEAT_WINDOWS
6706 for (wp = firstwin; wp; wp = wp->w_next)
6707 {
6708 ++winnr;
6709 if (wp->w_buffer == buf)
6710 break;
6711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006712 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006714 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715#endif
6716 --emsg_off;
6717}
6718
6719/*
6720 * "byte2line(byte)" function
6721 */
6722/*ARGSUSED*/
6723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006724f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 typval_T *argvars;
6726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730#else
6731 long boff = 0;
6732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006733 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006735 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 (linenr_T)0, &boff);
6739#endif
6740}
6741
6742/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006743 * "byteidx()" function
6744 */
6745/*ARGSUSED*/
6746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006747f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006748 typval_T *argvars;
6749 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006750{
6751#ifdef FEAT_MBYTE
6752 char_u *t;
6753#endif
6754 char_u *str;
6755 long idx;
6756
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006757 str = get_tv_string(&argvars[0]);
6758 idx = get_tv_number(&argvars[1]);
6759 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006760 if (idx < 0)
6761 return;
6762
6763#ifdef FEAT_MBYTE
6764 t = str;
6765 for ( ; idx > 0; idx--)
6766 {
6767 if (*t == NUL) /* EOL reached */
6768 return;
6769 t += mb_ptr2len_check(t);
6770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006771 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006772#else
6773 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006775#endif
6776}
6777
6778/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006779 * "call(func, arglist)" function
6780 */
6781 static void
6782f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 typval_T *argvars;
6784 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006785{
6786 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00006787 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006788 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006789 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006790 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006792
6793 rettv->vval.v_number = 0;
6794 if (argvars[1].v_type != VAR_LIST)
6795 {
6796 EMSG(_(e_listreq));
6797 return;
6798 }
6799 if (argvars[1].vval.v_list == NULL)
6800 return;
6801
6802 if (argvars[0].v_type == VAR_FUNC)
6803 func = argvars[0].vval.v_string;
6804 else
6805 func = get_tv_string(&argvars[0]);
6806
Bram Moolenaare9a41262005-01-15 22:18:47 +00006807 if (argvars[2].v_type != VAR_UNKNOWN)
6808 {
6809 if (argvars[2].v_type != VAR_DICT)
6810 {
6811 EMSG(_(e_dictreq));
6812 return;
6813 }
6814 selfdict = argvars[2].vval.v_dict;
6815 }
6816
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006817 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6818 item = item->li_next)
6819 {
6820 if (argc == MAX_FUNC_ARGS)
6821 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006822 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006823 break;
6824 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006825 /* Make a copy of each argument. This is needed to be able to set
6826 * v_lock to VAR_FIXED in the copy without changing the original list.
6827 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006828 copy_tv(&item->li_tv, &argv[argc++]);
6829 }
6830
6831 if (item == NULL)
6832 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006833 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6834 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006835
6836 /* Free the arguments. */
6837 while (argc > 0)
6838 clear_tv(&argv[--argc]);
6839}
6840
6841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 * "char2nr(string)" function
6843 */
6844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006845f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006846 typval_T *argvars;
6847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849#ifdef FEAT_MBYTE
6850 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006851 rettv->vval.v_number =
6852 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 else
6854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006855 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
6858/*
6859 * "cindent(lnum)" function
6860 */
6861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 typval_T *argvars;
6864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866#ifdef FEAT_CINDENT
6867 pos_T pos;
6868 linenr_T lnum;
6869
6870 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006871 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6873 {
6874 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006875 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 curwin->w_cursor = pos;
6877 }
6878 else
6879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881}
6882
6883/*
6884 * "col(string)" function
6885 */
6886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006888 typval_T *argvars;
6889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 colnr_T col = 0;
6892 pos_T *fp;
6893
6894 fp = var2fpos(&argvars[0], FALSE);
6895 if (fp != NULL)
6896 {
6897 if (fp->col == MAXCOL)
6898 {
6899 /* '> can be MAXCOL, get the length of the line then */
6900 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6901 col = STRLEN(ml_get(fp->lnum)) + 1;
6902 else
6903 col = MAXCOL;
6904 }
6905 else
6906 {
6907 col = fp->col + 1;
6908#ifdef FEAT_VIRTUALEDIT
6909 /* col(".") when the cursor is on the NUL at the end of the line
6910 * because of "coladd" can be seen as an extra column. */
6911 if (virtual_active() && fp == &curwin->w_cursor)
6912 {
6913 char_u *p = ml_get_cursor();
6914
6915 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6916 curwin->w_virtcol - curwin->w_cursor.coladd))
6917 {
6918# ifdef FEAT_MBYTE
6919 int l;
6920
6921 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6922 col += l;
6923# else
6924 if (*p != NUL && p[1] == NUL)
6925 ++col;
6926# endif
6927 }
6928 }
6929#endif
6930 }
6931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006932 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
6935/*
6936 * "confirm(message, buttons[, default [, type]])" function
6937 */
6938/*ARGSUSED*/
6939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006940f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 typval_T *argvars;
6942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6945 char_u *message;
6946 char_u *buttons = NULL;
6947 char_u buf[NUMBUFLEN];
6948 char_u buf2[NUMBUFLEN];
6949 int def = 1;
6950 int type = VIM_GENERIC;
6951 int c;
6952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006953 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006954 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006956 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006959 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaara7043832005-01-21 11:56:39 +00006962 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006963 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 switch (TOUPPER_ASC(c))
6965 {
6966 case 'E': type = VIM_ERROR; break;
6967 case 'Q': type = VIM_QUESTION; break;
6968 case 'I': type = VIM_INFO; break;
6969 case 'W': type = VIM_WARNING; break;
6970 case 'G': type = VIM_GENERIC; break;
6971 }
6972 }
6973 }
6974 }
6975
6976 if (buttons == NULL || *buttons == NUL)
6977 buttons = (char_u *)_("&Ok");
6978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 def, NULL);
6981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006982 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983#endif
6984}
6985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006986/*
6987 * "copy()" function
6988 */
6989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 typval_T *argvars;
6992 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006993{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
6997/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006998 * "count()" function
6999 */
7000 static void
7001f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007002 typval_T *argvars;
7003 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007004{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007005 long n = 0;
7006 int ic = FALSE;
7007
Bram Moolenaare9a41262005-01-15 22:18:47 +00007008 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007009 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 listitem_T *li;
7011 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007013
Bram Moolenaare9a41262005-01-15 22:18:47 +00007014 if ((l = argvars[0].vval.v_list) != NULL)
7015 {
7016 li = l->lv_first;
7017 if (argvars[2].v_type != VAR_UNKNOWN)
7018 {
7019 ic = get_tv_number(&argvars[2]);
7020 if (argvars[3].v_type != VAR_UNKNOWN)
7021 {
7022 idx = get_tv_number(&argvars[3]);
7023 li = list_find(l, idx);
7024 if (li == NULL)
7025 EMSGN(_(e_listidx), idx);
7026 }
7027 }
7028
7029 for ( ; li != NULL; li = li->li_next)
7030 if (tv_equal(&li->li_tv, &argvars[1], ic))
7031 ++n;
7032 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034 else if (argvars[0].v_type == VAR_DICT)
7035 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 int todo;
7037 dict_T *d;
7038 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 if ((d = argvars[0].vval.v_dict) != NULL)
7041 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 if (argvars[2].v_type != VAR_UNKNOWN)
7043 {
7044 ic = get_tv_number(&argvars[2]);
7045 if (argvars[3].v_type != VAR_UNKNOWN)
7046 EMSG(_(e_invarg));
7047 }
7048
Bram Moolenaar33570922005-01-25 22:26:29 +00007049 todo = d->dv_hashtab.ht_used;
7050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 {
7052 if (!HASHITEM_EMPTY(hi))
7053 {
7054 --todo;
7055 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7056 ++n;
7057 }
7058 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007059 }
7060 }
7061 else
7062 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007063 rettv->vval.v_number = n;
7064}
7065
7066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7068 *
7069 * Checks the existence of a cscope connection.
7070 */
7071/*ARGSUSED*/
7072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 typval_T *argvars;
7075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077#ifdef FEAT_CSCOPE
7078 int num = 0;
7079 char_u *dbpath = NULL;
7080 char_u *prepend = NULL;
7081 char_u buf[NUMBUFLEN];
7082
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083 if (argvars[0].v_type != VAR_UNKNOWN
7084 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007086 num = (int)get_tv_number(&argvars[0]);
7087 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007088 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007089 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 }
7091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007092 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007094 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095#endif
7096}
7097
7098/*
7099 * "cursor(lnum, col)" function
7100 *
7101 * Moves the cursor to the specified line and column
7102 */
7103/*ARGSUSED*/
7104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007105f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 typval_T *argvars;
7107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 long line, col;
7110
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007111 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 if (line > 0)
7113 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007114 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 if (col > 0)
7116 curwin->w_cursor.col = col - 1;
7117#ifdef FEAT_VIRTUALEDIT
7118 curwin->w_cursor.coladd = 0;
7119#endif
7120
7121 /* Make sure the cursor is in a valid position. */
7122 check_cursor();
7123#ifdef FEAT_MBYTE
7124 /* Correct cursor for multi-byte character. */
7125 if (has_mbyte)
7126 mb_adjust_cursor();
7127#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007128
7129 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130}
7131
7132/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007133 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 */
7135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007136f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 typval_T *argvars;
7138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141}
7142
7143/*
7144 * "delete()" function
7145 */
7146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007147f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 typval_T *argvars;
7149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155}
7156
7157/*
7158 * "did_filetype()" function
7159 */
7160/*ARGSUSED*/
7161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007162f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 typval_T *argvars;
7164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007167 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007169 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170#endif
7171}
7172
7173/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007174 * "diff_filler()" function
7175 */
7176/*ARGSUSED*/
7177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007178f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 typval_T *argvars;
7180 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007181{
7182#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007183 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007184#endif
7185}
7186
7187/*
7188 * "diff_hlID()" function
7189 */
7190/*ARGSUSED*/
7191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007192f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 typval_T *argvars;
7194 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007195{
7196#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007197 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007198 static linenr_T prev_lnum = 0;
7199 static int changedtick = 0;
7200 static int fnum = 0;
7201 static int change_start = 0;
7202 static int change_end = 0;
7203 static enum hlf_value hlID = 0;
7204 int filler_lines;
7205 int col;
7206
7207 if (lnum != prev_lnum
7208 || changedtick != curbuf->b_changedtick
7209 || fnum != curbuf->b_fnum)
7210 {
7211 /* New line, buffer, change: need to get the values. */
7212 filler_lines = diff_check(curwin, lnum);
7213 if (filler_lines < 0)
7214 {
7215 if (filler_lines == -1)
7216 {
7217 change_start = MAXCOL;
7218 change_end = -1;
7219 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7220 hlID = HLF_ADD; /* added line */
7221 else
7222 hlID = HLF_CHD; /* changed line */
7223 }
7224 else
7225 hlID = HLF_ADD; /* added line */
7226 }
7227 else
7228 hlID = (enum hlf_value)0;
7229 prev_lnum = lnum;
7230 changedtick = curbuf->b_changedtick;
7231 fnum = curbuf->b_fnum;
7232 }
7233
7234 if (hlID == HLF_CHD || hlID == HLF_TXD)
7235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007237 if (col >= change_start && col <= change_end)
7238 hlID = HLF_TXD; /* changed text */
7239 else
7240 hlID = HLF_CHD; /* changed line */
7241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007243#endif
7244}
7245
7246/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007247 * "empty({expr})" function
7248 */
7249 static void
7250f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 typval_T *argvars;
7252 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007253{
7254 int n;
7255
7256 switch (argvars[0].v_type)
7257 {
7258 case VAR_STRING:
7259 case VAR_FUNC:
7260 n = argvars[0].vval.v_string == NULL
7261 || *argvars[0].vval.v_string == NUL;
7262 break;
7263 case VAR_NUMBER:
7264 n = argvars[0].vval.v_number == 0;
7265 break;
7266 case VAR_LIST:
7267 n = argvars[0].vval.v_list == NULL
7268 || argvars[0].vval.v_list->lv_first == NULL;
7269 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007270 case VAR_DICT:
7271 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007274 default:
7275 EMSG2(_(e_intern2), "f_empty()");
7276 n = 0;
7277 }
7278
7279 rettv->vval.v_number = n;
7280}
7281
7282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 * "escape({string}, {chars})" function
7284 */
7285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007286f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 typval_T *argvars;
7288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 char_u buf[NUMBUFLEN];
7291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007292 rettv->vval.v_string =
7293 vim_strsave_escaped(get_tv_string(&argvars[0]),
7294 get_tv_string_buf(&argvars[1], buf));
7295 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296}
7297
7298/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007299 * "eval()" function
7300 */
7301/*ARGSUSED*/
7302 static void
7303f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 typval_T *argvars;
7305 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007306{
7307 char_u *s;
7308
7309 s = get_tv_string(&argvars[0]);
7310 s = skipwhite(s);
7311
7312 if (eval1(&s, rettv, TRUE) == FAIL)
7313 rettv->vval.v_number = 0;
7314 else if (*s != NUL)
7315 EMSG(_(e_trailing));
7316}
7317
7318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 * "eventhandler()" function
7320 */
7321/*ARGSUSED*/
7322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007323f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 typval_T *argvars;
7325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007327 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328}
7329
7330/*
7331 * "executable()" function
7332 */
7333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007334f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 typval_T *argvars;
7336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007338 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339}
7340
7341/*
7342 * "exists()" function
7343 */
7344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 typval_T *argvars;
7347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349 char_u *p;
7350 char_u *name;
7351 int n = FALSE;
7352 int len = 0;
7353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007354 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (*p == '$') /* environment variable */
7356 {
7357 /* first try "normal" environment variables (fast) */
7358 if (mch_getenv(p + 1) != NULL)
7359 n = TRUE;
7360 else
7361 {
7362 /* try expanding things like $VIM and ${HOME} */
7363 p = expand_env_save(p);
7364 if (p != NULL && *p != '$')
7365 n = TRUE;
7366 vim_free(p);
7367 }
7368 }
7369 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007370 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 else if (*p == '*') /* internal or user defined function */
7372 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 else if (*p == ':')
7376 {
7377 n = cmd_exists(p + 1);
7378 }
7379 else if (*p == '#')
7380 {
7381#ifdef FEAT_AUTOCMD
7382 name = p + 1;
7383 p = vim_strchr(name, '#');
7384 if (p != NULL)
7385 n = au_exists(name, p, p + 1);
7386 else
7387 n = au_exists(name, name + STRLEN(name), NULL);
7388#endif
7389 }
7390 else /* internal variable */
7391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 char_u *expr_start;
7393 char_u *expr_end;
7394 char_u *temp_string = NULL;
7395 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 name = p;
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (expr_start != NULL)
7401 {
7402 temp_string = make_expanded_name(name, expr_start, expr_end, s);
7403 if (temp_string != NULL)
7404 {
7405 len = STRLEN(temp_string);
7406 name = temp_string;
7407 }
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (len == 0)
7410 len = get_id_len(&p);
7411 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
7420/*
7421 * "expand()" function
7422 */
7423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 typval_T *argvars;
7426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 char_u *s;
7429 int len;
7430 char_u *errormsg;
7431 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7432 expand_T xpc;
7433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007434 rettv->v_type = VAR_STRING;
7435 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (*s == '%' || *s == '#' || *s == '<')
7437 {
7438 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 --emsg_off;
7441 }
7442 else
7443 {
7444 /* When the optional second argument is non-zero, don't remove matches
7445 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 flags |= WILD_KEEP_ALL;
7448 ExpandInit(&xpc);
7449 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 ExpandCleanup(&xpc);
7452 }
7453}
7454
7455/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007456 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007458 */
7459 static void
7460f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 typval_T *argvars;
7462 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007463{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007464 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007465 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007466 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 list_T *l1, *l2;
7468 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 long before;
7470 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007471
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 l1 = argvars[0].vval.v_list;
7473 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007474 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7475 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 {
7477 if (argvars[2].v_type != VAR_UNKNOWN)
7478 {
7479 n = before = get_tv_number(&argvars[2]);
7480 item = list_find_ext(l1, &n);
7481 if (n != 0)
7482 {
7483 EMSGN(_(e_listidx), before);
7484 return;
7485 }
7486 }
7487 else
7488 item = NULL;
7489 list_extend(l1, l2, item);
7490
7491 ++l1->lv_refcount;
7492 copy_tv(&argvars[0], rettv);
7493 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7496 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 dict_T *d1, *d2;
7498 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 char_u *action;
7500 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503
7504 d1 = argvars[0].vval.v_dict;
7505 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007506 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7507 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 {
7509 /* Check the third argument. */
7510 if (argvars[2].v_type != VAR_UNKNOWN)
7511 {
7512 static char *(av[]) = {"keep", "force", "error"};
7513
7514 action = get_tv_string(&argvars[2]);
7515 for (i = 0; i < 3; ++i)
7516 if (STRCMP(action, av[i]) == 0)
7517 break;
7518 if (i == 3)
7519 {
7520 EMSGN(_(e_invarg2), action);
7521 return;
7522 }
7523 }
7524 else
7525 action = (char_u *)"force";
7526
7527 /* Go over all entries in the second dict and add them to the
7528 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 todo = d2->dv_hashtab.ht_used;
7530 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 --todo;
7535 di1 = dict_find(d1, hi2->hi_key, -1);
7536 if (di1 == NULL)
7537 {
7538 di1 = dictitem_copy(HI2DI(hi2));
7539 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7540 dictitem_free(di1);
7541 }
7542 else if (*action == 'e')
7543 {
7544 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7545 break;
7546 }
7547 else if (*action == 'f')
7548 {
7549 clear_tv(&di1->di_tv);
7550 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 }
7553 }
7554
7555 ++d1->dv_refcount;
7556 copy_tv(&argvars[0], rettv);
7557 }
7558 }
7559 else
7560 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007561}
7562
7563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 * "filereadable()" function
7565 */
7566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *argvars;
7569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 FILE *fd;
7572 char_u *p;
7573 int n;
7574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7577 {
7578 n = TRUE;
7579 fclose(fd);
7580 }
7581 else
7582 n = FALSE;
7583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585}
7586
7587/*
7588 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7589 * rights to write into.
7590 */
7591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 typval_T *argvars;
7594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 char_u *p;
7597 int retval = 0;
7598#if defined(UNIX) || defined(VMS)
7599 int perm = 0;
7600#endif
7601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603#if defined(UNIX) || defined(VMS)
7604 perm = mch_getperm(p);
7605#endif
7606#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7607 if (
7608# ifdef WIN3264
7609 mch_writable(p) &&
7610# else
7611# if defined(UNIX) || defined(VMS)
7612 (perm & 0222) &&
7613# endif
7614# endif
7615 mch_access((char *)p, W_OK) == 0
7616 )
7617#endif
7618 {
7619 ++retval;
7620 if (mch_isdir(p))
7621 ++retval;
7622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624}
7625
Bram Moolenaar33570922005-01-25 22:26:29 +00007626static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007627
7628 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007629findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 typval_T *argvars;
7631 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007632 int dir;
7633{
7634#ifdef FEAT_SEARCHPATH
7635 char_u *fname;
7636 char_u *fresult = NULL;
7637 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7638 char_u *p;
7639 char_u pathbuf[NUMBUFLEN];
7640 int count = 1;
7641 int first = TRUE;
7642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007644
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007648 if (*p != NUL)
7649 path = p;
7650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007653 }
7654
7655 do
7656 {
7657 vim_free(fresult);
7658 fresult = find_file_in_path_option(first ? fname : NULL,
7659 first ? (int)STRLEN(fname) : 0,
7660 0, first, path, dir, NULL);
7661 first = FALSE;
7662 } while (--count > 0 && fresult != NULL);
7663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007669}
7670
Bram Moolenaar33570922005-01-25 22:26:29 +00007671static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7672static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673
7674/*
7675 * Implementation of map() and filter().
7676 */
7677 static void
7678filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 int map;
7682{
7683 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 listitem_T *li, *nli;
7686 list_T *l = NULL;
7687 dictitem_T *di;
7688 hashtab_T *ht;
7689 hashitem_T *hi;
7690 dict_T *d = NULL;
7691 typval_T save_val;
7692 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007695 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
7696
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007697
7698 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007701 if ((l = argvars[0].vval.v_list) == NULL
7702 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007703 return;
7704 }
7705 else if (argvars[0].v_type == VAR_DICT)
7706 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007707 if ((d = argvars[0].vval.v_dict) == NULL
7708 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007709 return;
7710 }
7711 else
7712 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 return;
7715 }
7716
7717 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 save_val = vimvars[VV_VAL].vv_tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719
7720 if (argvars[0].v_type == VAR_DICT)
7721 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 save_key = vimvars[VV_KEY].vv_tv;
7723 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00007726 hash_lock(ht);
7727 todo = ht->ht_used;
7728 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007732 --todo;
7733 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007734 if (tv_check_lock(di->di_tv.v_lock, msg))
7735 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
7738 break;
7739 if (!map && rem)
7740 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007742 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007744 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007745
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 clear_tv(&vimvars[VV_KEY].vv_tv);
7747 vimvars[VV_KEY].vv_tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007749 else
7750 {
7751 for (li = l->lv_first; li != NULL; li = nli)
7752 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007753 if (tv_check_lock(li->li_tv.v_lock, msg))
7754 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007755 nli = li->li_next;
7756 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7757 break;
7758 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007759 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007760 }
7761 }
7762
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 clear_tv(&vimvars[VV_VAL].vv_tv);
7764 vimvars[VV_VAL].vv_tv = save_val;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765
7766 copy_tv(&argvars[0], rettv);
7767}
7768
7769 static int
7770filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007772 char_u *expr;
7773 int map;
7774 int *remp;
7775{
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007777 char_u *s;
7778
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007780 s = expr;
7781 if (eval1(&s, &rettv, TRUE) == FAIL)
7782 return FAIL;
7783 if (*s != NUL) /* check for trailing chars after expr */
7784 {
7785 EMSG2(_(e_invexpr2), s);
7786 return FAIL;
7787 }
7788 if (map)
7789 {
7790 /* map(): replace the list item value */
7791 clear_tv(tv);
7792 *tv = rettv;
7793 }
7794 else
7795 {
7796 /* filter(): when expr is zero remove the item */
7797 *remp = (get_tv_number(&rettv) == 0);
7798 clear_tv(&rettv);
7799 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007801 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007802}
7803
7804/*
7805 * "filter()" function
7806 */
7807 static void
7808f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *argvars;
7810 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007811{
7812 filter_map(argvars, rettv, FALSE);
7813}
7814
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007815/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007816 * "finddir({fname}[, {path}[, {count}]])" function
7817 */
7818 static void
7819f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 typval_T *argvars;
7821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007822{
7823 findfilendir(argvars, rettv, TRUE);
7824}
7825
7826/*
7827 * "findfile({fname}[, {path}[, {count}]])" function
7828 */
7829 static void
7830f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 typval_T *argvars;
7832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007833{
7834 findfilendir(argvars, rettv, FALSE);
7835}
7836
7837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 * "fnamemodify({fname}, {mods})" function
7839 */
7840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 typval_T *argvars;
7843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 char_u *fname;
7846 char_u *mods;
7847 int usedlen = 0;
7848 int len;
7849 char_u *fbuf = NULL;
7850 char_u buf[NUMBUFLEN];
7851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 fname = get_tv_string(&argvars[0]);
7853 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 len = (int)STRLEN(fname);
7855
7856 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 vim_free(fbuf);
7864}
7865
Bram Moolenaar33570922005-01-25 22:26:29 +00007866static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867
7868/*
7869 * "foldclosed()" function
7870 */
7871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 typval_T *argvars;
7874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 int end;
7876{
7877#ifdef FEAT_FOLDING
7878 linenr_T lnum;
7879 linenr_T first, last;
7880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7883 {
7884 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7885 {
7886 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 return;
7891 }
7892 }
7893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895}
7896
7897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007898 * "foldclosed()" function
7899 */
7900 static void
7901f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 typval_T *argvars;
7903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007904{
7905 foldclosed_both(argvars, rettv, FALSE);
7906}
7907
7908/*
7909 * "foldclosedend()" function
7910 */
7911 static void
7912f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 typval_T *argvars;
7914 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007915{
7916 foldclosed_both(argvars, rettv, TRUE);
7917}
7918
7919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 * "foldlevel()" function
7921 */
7922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 typval_T *argvars;
7925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927#ifdef FEAT_FOLDING
7928 linenr_T lnum;
7929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 else
7934#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
7938/*
7939 * "foldtext()" function
7940 */
7941/*ARGSUSED*/
7942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 typval_T *argvars;
7945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947#ifdef FEAT_FOLDING
7948 linenr_T lnum;
7949 char_u *s;
7950 char_u *r;
7951 int len;
7952 char *txt;
7953#endif
7954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 rettv->v_type = VAR_STRING;
7956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7959 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7960 <= curbuf->b_ml.ml_line_count
7961 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
7963 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007964 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7965 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {
7967 if (!linewhite(lnum))
7968 break;
7969 ++lnum;
7970 }
7971
7972 /* Find interesting text in this line. */
7973 s = skipwhite(ml_get(lnum));
7974 /* skip C comment-start */
7975 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007978 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007979 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007980 {
7981 s = skipwhite(ml_get(lnum + 1));
7982 if (*s == '*')
7983 s = skipwhite(s + 1);
7984 }
7985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 txt = _("+-%s%3ld lines: ");
7987 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 + 20 /* for %3ld */
7990 + STRLEN(s))); /* concatenated */
7991 if (r != NULL)
7992 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007993 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7994 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7995 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 len = (int)STRLEN(r);
7997 STRCAT(r, s);
7998 /* remove 'foldmarker' and 'commentstring' */
7999 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008000 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 }
8002 }
8003#endif
8004}
8005
8006/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008007 * "foldtextresult(lnum)" function
8008 */
8009/*ARGSUSED*/
8010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 typval_T *argvars;
8013 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008014{
8015#ifdef FEAT_FOLDING
8016 linenr_T lnum;
8017 char_u *text;
8018 char_u buf[51];
8019 foldinfo_T foldinfo;
8020 int fold_count;
8021#endif
8022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 rettv->v_type = VAR_STRING;
8024 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008025#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008026 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008027 fold_count = foldedCount(curwin, lnum, &foldinfo);
8028 if (fold_count > 0)
8029 {
8030 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8031 &foldinfo, buf);
8032 if (text == buf)
8033 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008034 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008035 }
8036#endif
8037}
8038
8039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 * "foreground()" function
8041 */
8042/*ARGSUSED*/
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049#ifdef FEAT_GUI
8050 if (gui.in_use)
8051 gui_mch_set_foreground();
8052#else
8053# ifdef WIN32
8054 win32_set_foreground();
8055# endif
8056#endif
8057}
8058
8059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008060 * "function()" function
8061 */
8062/*ARGSUSED*/
8063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 typval_T *argvars;
8066 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008067{
8068 char_u *s;
8069
Bram Moolenaara7043832005-01-21 11:56:39 +00008070 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008072 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008073 EMSG2(_(e_invarg2), s);
8074 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008075 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008076 else
8077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008078 rettv->vval.v_string = vim_strsave(s);
8079 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008080 }
8081}
8082
8083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008084 * "get()" function
8085 */
8086 static void
8087f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008088 typval_T *argvars;
8089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008090{
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 listitem_T *li;
8092 list_T *l;
8093 dictitem_T *di;
8094 dict_T *d;
8095 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008096
Bram Moolenaare9a41262005-01-15 22:18:47 +00008097 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008098 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008099 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008100 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008101 li = list_find(l, get_tv_number(&argvars[1]));
8102 if (li != NULL)
8103 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008104 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008105 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106 else if (argvars[0].v_type == VAR_DICT)
8107 {
8108 if ((d = argvars[0].vval.v_dict) != NULL)
8109 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008110 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111 if (di != NULL)
8112 tv = &di->di_tv;
8113 }
8114 }
8115 else
8116 EMSG2(_(e_listdictarg), "get()");
8117
8118 if (tv == NULL)
8119 {
8120 if (argvars[2].v_type == VAR_UNKNOWN)
8121 rettv->vval.v_number = 0;
8122 else
8123 copy_tv(&argvars[2], rettv);
8124 }
8125 else
8126 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008127}
8128
8129/*
8130 * "getbufvar()" function
8131 */
8132 static void
8133f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008134 typval_T *argvars;
8135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008136{
8137 buf_T *buf;
8138 buf_T *save_curbuf;
8139 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008141
8142 ++emsg_off;
8143 buf = get_buf_tv(&argvars[0]);
8144 varname = get_tv_string(&argvars[1]);
8145
8146 rettv->v_type = VAR_STRING;
8147 rettv->vval.v_string = NULL;
8148
8149 if (buf != NULL && varname != NULL)
8150 {
8151 if (*varname == '&') /* buffer-local-option */
8152 {
8153 /* set curbuf to be our buf, temporarily */
8154 save_curbuf = curbuf;
8155 curbuf = buf;
8156
8157 get_option_tv(&varname, rettv, TRUE);
8158
8159 /* restore previous notion of curbuf */
8160 curbuf = save_curbuf;
8161 }
8162 else
8163 {
8164 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008165 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008168 }
8169 }
8170
8171 --emsg_off;
8172}
8173
8174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 * "getchar()" function
8176 */
8177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008178f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008179 typval_T *argvars;
8180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
8182 varnumber_T n;
8183
8184 ++no_mapping;
8185 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 /* getchar(): blocking wait. */
8188 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 /* getchar(1): only check if char avail */
8191 n = vpeekc();
8192 else if (vpeekc() == NUL)
8193 /* getchar(0) and no char avail: return zero */
8194 n = 0;
8195 else
8196 /* getchar(0) and char avail: return char */
8197 n = safe_vgetc();
8198 --no_mapping;
8199 --allow_keys;
8200
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (IS_SPECIAL(n) || mod_mask != 0)
8203 {
8204 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8205 int i = 0;
8206
8207 /* Turn a special key into three bytes, plus modifier. */
8208 if (mod_mask != 0)
8209 {
8210 temp[i++] = K_SPECIAL;
8211 temp[i++] = KS_MODIFIER;
8212 temp[i++] = mod_mask;
8213 }
8214 if (IS_SPECIAL(n))
8215 {
8216 temp[i++] = K_SPECIAL;
8217 temp[i++] = K_SECOND(n);
8218 temp[i++] = K_THIRD(n);
8219 }
8220#ifdef FEAT_MBYTE
8221 else if (has_mbyte)
8222 i += (*mb_char2bytes)(n, temp + i);
8223#endif
8224 else
8225 temp[i++] = n;
8226 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->v_type = VAR_STRING;
8228 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 }
8230}
8231
8232/*
8233 * "getcharmod()" function
8234 */
8235/*ARGSUSED*/
8236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 typval_T *argvars;
8239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243
8244/*
8245 * "getcmdline()" function
8246 */
8247/*ARGSUSED*/
8248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 typval_T *argvars;
8251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 rettv->v_type = VAR_STRING;
8254 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255}
8256
8257/*
8258 * "getcmdpos()" function
8259 */
8260/*ARGSUSED*/
8261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 typval_T *argvars;
8264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267}
8268
8269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 * "getcwd()" function
8271 */
8272/*ARGSUSED*/
8273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008275 typval_T *argvars;
8276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 char_u cwd[MAXPATHL];
8279
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008282 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 else
8284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288#endif
8289 }
8290}
8291
8292/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008293 * "getfontname()" function
8294 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008295/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T *argvars;
8299 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008300{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301 rettv->v_type = VAR_STRING;
8302 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008303#ifdef FEAT_GUI
8304 if (gui.in_use)
8305 {
8306 GuiFont font;
8307 char_u *name = NULL;
8308
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008310 {
8311 /* Get the "Normal" font. Either the name saved by
8312 * hl_set_font_name() or from the font ID. */
8313 font = gui.norm_font;
8314 name = hl_get_font_name();
8315 }
8316 else
8317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008319 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8320 return;
8321 font = gui_mch_get_font(name, FALSE);
8322 if (font == NOFONT)
8323 return; /* Invalid font name, return empty string. */
8324 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008325 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008326 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008327 gui_mch_free_font(font);
8328 }
8329#endif
8330}
8331
8332/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008333 * "getfperm({fname})" function
8334 */
8335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T *argvars;
8338 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008339{
8340 char_u *fname;
8341 struct stat st;
8342 char_u *perm = NULL;
8343 char_u flags[] = "rwx";
8344 int i;
8345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008346 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008349 if (mch_stat((char *)fname, &st) >= 0)
8350 {
8351 perm = vim_strsave((char_u *)"---------");
8352 if (perm != NULL)
8353 {
8354 for (i = 0; i < 9; i++)
8355 {
8356 if (st.st_mode & (1 << (8 - i)))
8357 perm[i] = flags[i % 3];
8358 }
8359 }
8360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008362}
8363
8364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 * "getfsize({fname})" function
8366 */
8367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 char_u *fname;
8373 struct stat st;
8374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008375 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 if (mch_stat((char *)fname, &st) >= 0)
8380 {
8381 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 }
8386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388}
8389
8390/*
8391 * "getftime({fname})" function
8392 */
8393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008394f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008395 typval_T *argvars;
8396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
8398 char_u *fname;
8399 struct stat st;
8400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
8403 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407}
8408
8409/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008410 * "getftype({fname})" function
8411 */
8412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008414 typval_T *argvars;
8415 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008416{
8417 char_u *fname;
8418 struct stat st;
8419 char_u *type = NULL;
8420 char *t;
8421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008424 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008425 if (mch_lstat((char *)fname, &st) >= 0)
8426 {
8427#ifdef S_ISREG
8428 if (S_ISREG(st.st_mode))
8429 t = "file";
8430 else if (S_ISDIR(st.st_mode))
8431 t = "dir";
8432# ifdef S_ISLNK
8433 else if (S_ISLNK(st.st_mode))
8434 t = "link";
8435# endif
8436# ifdef S_ISBLK
8437 else if (S_ISBLK(st.st_mode))
8438 t = "bdev";
8439# endif
8440# ifdef S_ISCHR
8441 else if (S_ISCHR(st.st_mode))
8442 t = "cdev";
8443# endif
8444# ifdef S_ISFIFO
8445 else if (S_ISFIFO(st.st_mode))
8446 t = "fifo";
8447# endif
8448# ifdef S_ISSOCK
8449 else if (S_ISSOCK(st.st_mode))
8450 t = "fifo";
8451# endif
8452 else
8453 t = "other";
8454#else
8455# ifdef S_IFMT
8456 switch (st.st_mode & S_IFMT)
8457 {
8458 case S_IFREG: t = "file"; break;
8459 case S_IFDIR: t = "dir"; break;
8460# ifdef S_IFLNK
8461 case S_IFLNK: t = "link"; break;
8462# endif
8463# ifdef S_IFBLK
8464 case S_IFBLK: t = "bdev"; break;
8465# endif
8466# ifdef S_IFCHR
8467 case S_IFCHR: t = "cdev"; break;
8468# endif
8469# ifdef S_IFIFO
8470 case S_IFIFO: t = "fifo"; break;
8471# endif
8472# ifdef S_IFSOCK
8473 case S_IFSOCK: t = "socket"; break;
8474# endif
8475 default: t = "other";
8476 }
8477# else
8478 if (mch_isdir(fname))
8479 t = "dir";
8480 else
8481 t = "file";
8482# endif
8483#endif
8484 type = vim_strsave((char_u *)t);
8485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008486 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008487}
8488
8489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008490 * "getline(lnum)" function
8491 */
8492 static void
8493f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 typval_T *argvars;
8495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008496{
8497 linenr_T lnum;
8498 linenr_T end;
8499 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 list_T *l;
8501 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008502
8503 lnum = get_tv_lnum(argvars);
8504
8505 if (argvars[1].v_type == VAR_UNKNOWN)
8506 {
8507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8508 p = ml_get(lnum);
8509 else
8510 p = (char_u *)"";
8511
8512 rettv->v_type = VAR_STRING;
8513 rettv->vval.v_string = vim_strsave(p);
8514 }
8515 else
8516 {
8517 end = get_tv_lnum(&argvars[1]);
8518 if (end < lnum)
8519 {
8520 EMSG(_(e_invrange));
8521 rettv->vval.v_number = 0;
8522 }
8523 else
8524 {
8525 l = list_alloc();
8526 if (l != NULL)
8527 {
8528 if (lnum < 1)
8529 lnum = 1;
8530 if (end > curbuf->b_ml.ml_line_count)
8531 end = curbuf->b_ml.ml_line_count;
8532 while (lnum <= end)
8533 {
8534 li = listitem_alloc();
8535 if (li == NULL)
8536 break;
8537 list_append(l, li);
8538 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008539 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008540 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8541 }
8542 rettv->vval.v_list = l;
8543 rettv->v_type = VAR_LIST;
8544 ++l->lv_refcount;
8545 }
8546 }
8547 }
8548}
8549
8550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * "getreg()" function
8552 */
8553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 char_u *strregname;
8559 int regname;
8560
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008564 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 regname = (strregname == NULL ? '"' : *strregname);
8566 if (regname == 0)
8567 regname = '"';
8568
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 rettv->v_type = VAR_STRING;
8570 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571}
8572
8573/*
8574 * "getregtype()" function
8575 */
8576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 typval_T *argvars;
8579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 char_u *strregname;
8582 int regname;
8583 char_u buf[NUMBUFLEN + 2];
8584 long reglen = 0;
8585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 else
8589 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
8592 regname = (strregname == NULL ? '"' : *strregname);
8593 if (regname == 0)
8594 regname = '"';
8595
8596 buf[0] = NUL;
8597 buf[1] = NUL;
8598 switch (get_reg_type(regname, &reglen))
8599 {
8600 case MLINE: buf[0] = 'V'; break;
8601 case MCHAR: buf[0] = 'v'; break;
8602#ifdef FEAT_VISUAL
8603 case MBLOCK:
8604 buf[0] = Ctrl_V;
8605 sprintf((char *)buf + 1, "%ld", reglen + 1);
8606 break;
8607#endif
8608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 rettv->v_type = VAR_STRING;
8610 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611}
8612
8613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 * "getwinposx()" function
8615 */
8616/*ARGSUSED*/
8617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008618f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 typval_T *argvars;
8620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623#ifdef FEAT_GUI
8624 if (gui.in_use)
8625 {
8626 int x, y;
8627
8628 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 }
8631#endif
8632}
8633
8634/*
8635 * "getwinposy()" function
8636 */
8637/*ARGSUSED*/
8638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 typval_T *argvars;
8641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644#ifdef FEAT_GUI
8645 if (gui.in_use)
8646 {
8647 int x, y;
8648
8649 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652#endif
8653}
8654
8655/*
8656 * "getwinvar()" function
8657 */
8658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 typval_T *argvars;
8661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
8663 win_T *win, *oldcurwin;
8664 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
8667 ++emsg_off;
8668 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671 rettv->v_type = VAR_STRING;
8672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673
8674 if (win != NULL && varname != NULL)
8675 {
8676 if (*varname == '&') /* window-local-option */
8677 {
8678 /* set curwin to be our win, temporarily */
8679 oldcurwin = curwin;
8680 curwin = win;
8681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
8684 /* restore previous notion of curwin */
8685 curwin = oldcurwin;
8686 }
8687 else
8688 {
8689 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 }
8694 }
8695
8696 --emsg_off;
8697}
8698
8699/*
8700 * "glob()" function
8701 */
8702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 typval_T *argvars;
8705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 expand_T xpc;
8708
8709 ExpandInit(&xpc);
8710 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->v_type = VAR_STRING;
8712 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8714 ExpandCleanup(&xpc);
8715}
8716
8717/*
8718 * "globpath()" function
8719 */
8720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008722 typval_T *argvars;
8723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 char_u buf1[NUMBUFLEN];
8726
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727 rettv->v_type = VAR_STRING;
8728 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8729 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730}
8731
8732/*
8733 * "has()" function
8734 */
8735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *argvars;
8738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
8740 int i;
8741 char_u *name;
8742 int n = FALSE;
8743 static char *(has_list[]) =
8744 {
8745#ifdef AMIGA
8746 "amiga",
8747# ifdef FEAT_ARP
8748 "arp",
8749# endif
8750#endif
8751#ifdef __BEOS__
8752 "beos",
8753#endif
8754#ifdef MSDOS
8755# ifdef DJGPP
8756 "dos32",
8757# else
8758 "dos16",
8759# endif
8760#endif
8761#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8762 "mac",
8763#endif
8764#if defined(MACOS_X_UNIX)
8765 "macunix",
8766#endif
8767#ifdef OS2
8768 "os2",
8769#endif
8770#ifdef __QNX__
8771 "qnx",
8772#endif
8773#ifdef RISCOS
8774 "riscos",
8775#endif
8776#ifdef UNIX
8777 "unix",
8778#endif
8779#ifdef VMS
8780 "vms",
8781#endif
8782#ifdef WIN16
8783 "win16",
8784#endif
8785#ifdef WIN32
8786 "win32",
8787#endif
8788#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8789 "win32unix",
8790#endif
8791#ifdef WIN64
8792 "win64",
8793#endif
8794#ifdef EBCDIC
8795 "ebcdic",
8796#endif
8797#ifndef CASE_INSENSITIVE_FILENAME
8798 "fname_case",
8799#endif
8800#ifdef FEAT_ARABIC
8801 "arabic",
8802#endif
8803#ifdef FEAT_AUTOCMD
8804 "autocmd",
8805#endif
8806#ifdef FEAT_BEVAL
8807 "balloon_eval",
8808#endif
8809#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8810 "builtin_terms",
8811# ifdef ALL_BUILTIN_TCAPS
8812 "all_builtin_terms",
8813# endif
8814#endif
8815#ifdef FEAT_BYTEOFF
8816 "byte_offset",
8817#endif
8818#ifdef FEAT_CINDENT
8819 "cindent",
8820#endif
8821#ifdef FEAT_CLIENTSERVER
8822 "clientserver",
8823#endif
8824#ifdef FEAT_CLIPBOARD
8825 "clipboard",
8826#endif
8827#ifdef FEAT_CMDL_COMPL
8828 "cmdline_compl",
8829#endif
8830#ifdef FEAT_CMDHIST
8831 "cmdline_hist",
8832#endif
8833#ifdef FEAT_COMMENTS
8834 "comments",
8835#endif
8836#ifdef FEAT_CRYPT
8837 "cryptv",
8838#endif
8839#ifdef FEAT_CSCOPE
8840 "cscope",
8841#endif
8842#ifdef DEBUG
8843 "debug",
8844#endif
8845#ifdef FEAT_CON_DIALOG
8846 "dialog_con",
8847#endif
8848#ifdef FEAT_GUI_DIALOG
8849 "dialog_gui",
8850#endif
8851#ifdef FEAT_DIFF
8852 "diff",
8853#endif
8854#ifdef FEAT_DIGRAPHS
8855 "digraphs",
8856#endif
8857#ifdef FEAT_DND
8858 "dnd",
8859#endif
8860#ifdef FEAT_EMACS_TAGS
8861 "emacs_tags",
8862#endif
8863 "eval", /* always present, of course! */
8864#ifdef FEAT_EX_EXTRA
8865 "ex_extra",
8866#endif
8867#ifdef FEAT_SEARCH_EXTRA
8868 "extra_search",
8869#endif
8870#ifdef FEAT_FKMAP
8871 "farsi",
8872#endif
8873#ifdef FEAT_SEARCHPATH
8874 "file_in_path",
8875#endif
8876#ifdef FEAT_FIND_ID
8877 "find_in_path",
8878#endif
8879#ifdef FEAT_FOLDING
8880 "folding",
8881#endif
8882#ifdef FEAT_FOOTER
8883 "footer",
8884#endif
8885#if !defined(USE_SYSTEM) && defined(UNIX)
8886 "fork",
8887#endif
8888#ifdef FEAT_GETTEXT
8889 "gettext",
8890#endif
8891#ifdef FEAT_GUI
8892 "gui",
8893#endif
8894#ifdef FEAT_GUI_ATHENA
8895# ifdef FEAT_GUI_NEXTAW
8896 "gui_neXtaw",
8897# else
8898 "gui_athena",
8899# endif
8900#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008901#ifdef FEAT_GUI_KDE
8902 "gui_kde",
8903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904#ifdef FEAT_GUI_GTK
8905 "gui_gtk",
8906# ifdef HAVE_GTK2
8907 "gui_gtk2",
8908# endif
8909#endif
8910#ifdef FEAT_GUI_MAC
8911 "gui_mac",
8912#endif
8913#ifdef FEAT_GUI_MOTIF
8914 "gui_motif",
8915#endif
8916#ifdef FEAT_GUI_PHOTON
8917 "gui_photon",
8918#endif
8919#ifdef FEAT_GUI_W16
8920 "gui_win16",
8921#endif
8922#ifdef FEAT_GUI_W32
8923 "gui_win32",
8924#endif
8925#ifdef FEAT_HANGULIN
8926 "hangul_input",
8927#endif
8928#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8929 "iconv",
8930#endif
8931#ifdef FEAT_INS_EXPAND
8932 "insert_expand",
8933#endif
8934#ifdef FEAT_JUMPLIST
8935 "jumplist",
8936#endif
8937#ifdef FEAT_KEYMAP
8938 "keymap",
8939#endif
8940#ifdef FEAT_LANGMAP
8941 "langmap",
8942#endif
8943#ifdef FEAT_LIBCALL
8944 "libcall",
8945#endif
8946#ifdef FEAT_LINEBREAK
8947 "linebreak",
8948#endif
8949#ifdef FEAT_LISP
8950 "lispindent",
8951#endif
8952#ifdef FEAT_LISTCMDS
8953 "listcmds",
8954#endif
8955#ifdef FEAT_LOCALMAP
8956 "localmap",
8957#endif
8958#ifdef FEAT_MENU
8959 "menu",
8960#endif
8961#ifdef FEAT_SESSION
8962 "mksession",
8963#endif
8964#ifdef FEAT_MODIFY_FNAME
8965 "modify_fname",
8966#endif
8967#ifdef FEAT_MOUSE
8968 "mouse",
8969#endif
8970#ifdef FEAT_MOUSESHAPE
8971 "mouseshape",
8972#endif
8973#if defined(UNIX) || defined(VMS)
8974# ifdef FEAT_MOUSE_DEC
8975 "mouse_dec",
8976# endif
8977# ifdef FEAT_MOUSE_GPM
8978 "mouse_gpm",
8979# endif
8980# ifdef FEAT_MOUSE_JSB
8981 "mouse_jsbterm",
8982# endif
8983# ifdef FEAT_MOUSE_NET
8984 "mouse_netterm",
8985# endif
8986# ifdef FEAT_MOUSE_PTERM
8987 "mouse_pterm",
8988# endif
8989# ifdef FEAT_MOUSE_XTERM
8990 "mouse_xterm",
8991# endif
8992#endif
8993#ifdef FEAT_MBYTE
8994 "multi_byte",
8995#endif
8996#ifdef FEAT_MBYTE_IME
8997 "multi_byte_ime",
8998#endif
8999#ifdef FEAT_MULTI_LANG
9000 "multi_lang",
9001#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009002#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009003#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009004 "mzscheme",
9005#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007#ifdef FEAT_OLE
9008 "ole",
9009#endif
9010#ifdef FEAT_OSFILETYPE
9011 "osfiletype",
9012#endif
9013#ifdef FEAT_PATH_EXTRA
9014 "path_extra",
9015#endif
9016#ifdef FEAT_PERL
9017#ifndef DYNAMIC_PERL
9018 "perl",
9019#endif
9020#endif
9021#ifdef FEAT_PYTHON
9022#ifndef DYNAMIC_PYTHON
9023 "python",
9024#endif
9025#endif
9026#ifdef FEAT_POSTSCRIPT
9027 "postscript",
9028#endif
9029#ifdef FEAT_PRINTER
9030 "printer",
9031#endif
9032#ifdef FEAT_QUICKFIX
9033 "quickfix",
9034#endif
9035#ifdef FEAT_RIGHTLEFT
9036 "rightleft",
9037#endif
9038#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9039 "ruby",
9040#endif
9041#ifdef FEAT_SCROLLBIND
9042 "scrollbind",
9043#endif
9044#ifdef FEAT_CMDL_INFO
9045 "showcmd",
9046 "cmdline_info",
9047#endif
9048#ifdef FEAT_SIGNS
9049 "signs",
9050#endif
9051#ifdef FEAT_SMARTINDENT
9052 "smartindent",
9053#endif
9054#ifdef FEAT_SNIFF
9055 "sniff",
9056#endif
9057#ifdef FEAT_STL_OPT
9058 "statusline",
9059#endif
9060#ifdef FEAT_SUN_WORKSHOP
9061 "sun_workshop",
9062#endif
9063#ifdef FEAT_NETBEANS_INTG
9064 "netbeans_intg",
9065#endif
9066#ifdef FEAT_SYN_HL
9067 "syntax",
9068#endif
9069#if defined(USE_SYSTEM) || !defined(UNIX)
9070 "system",
9071#endif
9072#ifdef FEAT_TAG_BINS
9073 "tag_binary",
9074#endif
9075#ifdef FEAT_TAG_OLDSTATIC
9076 "tag_old_static",
9077#endif
9078#ifdef FEAT_TAG_ANYWHITE
9079 "tag_any_white",
9080#endif
9081#ifdef FEAT_TCL
9082# ifndef DYNAMIC_TCL
9083 "tcl",
9084# endif
9085#endif
9086#ifdef TERMINFO
9087 "terminfo",
9088#endif
9089#ifdef FEAT_TERMRESPONSE
9090 "termresponse",
9091#endif
9092#ifdef FEAT_TEXTOBJ
9093 "textobjects",
9094#endif
9095#ifdef HAVE_TGETENT
9096 "tgetent",
9097#endif
9098#ifdef FEAT_TITLE
9099 "title",
9100#endif
9101#ifdef FEAT_TOOLBAR
9102 "toolbar",
9103#endif
9104#ifdef FEAT_USR_CMDS
9105 "user-commands", /* was accidentally included in 5.4 */
9106 "user_commands",
9107#endif
9108#ifdef FEAT_VIMINFO
9109 "viminfo",
9110#endif
9111#ifdef FEAT_VERTSPLIT
9112 "vertsplit",
9113#endif
9114#ifdef FEAT_VIRTUALEDIT
9115 "virtualedit",
9116#endif
9117#ifdef FEAT_VISUAL
9118 "visual",
9119#endif
9120#ifdef FEAT_VISUALEXTRA
9121 "visualextra",
9122#endif
9123#ifdef FEAT_VREPLACE
9124 "vreplace",
9125#endif
9126#ifdef FEAT_WILDIGN
9127 "wildignore",
9128#endif
9129#ifdef FEAT_WILDMENU
9130 "wildmenu",
9131#endif
9132#ifdef FEAT_WINDOWS
9133 "windows",
9134#endif
9135#ifdef FEAT_WAK
9136 "winaltkeys",
9137#endif
9138#ifdef FEAT_WRITEBACKUP
9139 "writebackup",
9140#endif
9141#ifdef FEAT_XIM
9142 "xim",
9143#endif
9144#ifdef FEAT_XFONTSET
9145 "xfontset",
9146#endif
9147#ifdef USE_XSMP
9148 "xsmp",
9149#endif
9150#ifdef USE_XSMP_INTERACT
9151 "xsmp_interact",
9152#endif
9153#ifdef FEAT_XCLIPBOARD
9154 "xterm_clipboard",
9155#endif
9156#ifdef FEAT_XTERM_SAVE
9157 "xterm_save",
9158#endif
9159#if defined(UNIX) && defined(FEAT_X11)
9160 "X11",
9161#endif
9162 NULL
9163 };
9164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 for (i = 0; has_list[i] != NULL; ++i)
9167 if (STRICMP(name, has_list[i]) == 0)
9168 {
9169 n = TRUE;
9170 break;
9171 }
9172
9173 if (n == FALSE)
9174 {
9175 if (STRNICMP(name, "patch", 5) == 0)
9176 n = has_patch(atoi((char *)name + 5));
9177 else if (STRICMP(name, "vim_starting") == 0)
9178 n = (starting != 0);
9179#ifdef DYNAMIC_TCL
9180 else if (STRICMP(name, "tcl") == 0)
9181 n = tcl_enabled(FALSE);
9182#endif
9183#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9184 else if (STRICMP(name, "iconv") == 0)
9185 n = iconv_enabled(FALSE);
9186#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009187#ifdef DYNAMIC_MZSCHEME
9188 else if (STRICMP(name, "mzscheme") == 0)
9189 n = mzscheme_enabled(FALSE);
9190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191#ifdef DYNAMIC_RUBY
9192 else if (STRICMP(name, "ruby") == 0)
9193 n = ruby_enabled(FALSE);
9194#endif
9195#ifdef DYNAMIC_PYTHON
9196 else if (STRICMP(name, "python") == 0)
9197 n = python_enabled(FALSE);
9198#endif
9199#ifdef DYNAMIC_PERL
9200 else if (STRICMP(name, "perl") == 0)
9201 n = perl_enabled(FALSE);
9202#endif
9203#ifdef FEAT_GUI
9204 else if (STRICMP(name, "gui_running") == 0)
9205 n = (gui.in_use || gui.starting);
9206# ifdef FEAT_GUI_W32
9207 else if (STRICMP(name, "gui_win32s") == 0)
9208 n = gui_is_win32s();
9209# endif
9210# ifdef FEAT_BROWSE
9211 else if (STRICMP(name, "browse") == 0)
9212 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9213# endif
9214#endif
9215#ifdef FEAT_SYN_HL
9216 else if (STRICMP(name, "syntax_items") == 0)
9217 n = syntax_present(curbuf);
9218#endif
9219#if defined(WIN3264)
9220 else if (STRICMP(name, "win95") == 0)
9221 n = mch_windows95();
9222#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009223#ifdef FEAT_NETBEANS_INTG
9224 else if (STRICMP(name, "netbeans_enabled") == 0)
9225 n = usingNetbeans;
9226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
9232/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009233 * "has_key()" function
9234 */
9235 static void
9236f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009237 typval_T *argvars;
9238 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009239{
9240 rettv->vval.v_number = 0;
9241 if (argvars[0].v_type != VAR_DICT)
9242 {
9243 EMSG(_(e_dictreq));
9244 return;
9245 }
9246 if (argvars[0].vval.v_dict == NULL)
9247 return;
9248
9249 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009250 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009251}
9252
9253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 * "hasmapto()" function
9255 */
9256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 typval_T *argvars;
9259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 char_u *name;
9262 char_u *mode;
9263 char_u buf[NUMBUFLEN];
9264
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009266 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 mode = (char_u *)"nvo";
9268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009269 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270
9271 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009272 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275}
9276
9277/*
9278 * "histadd()" function
9279 */
9280/*ARGSUSED*/
9281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *argvars;
9284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286#ifdef FEAT_CMDHIST
9287 int histype;
9288 char_u *str;
9289 char_u buf[NUMBUFLEN];
9290#endif
9291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (check_restricted() || check_secure())
9294 return;
9295#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 if (histype >= 0)
9298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (*str != NUL)
9301 {
9302 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 return;
9305 }
9306 }
9307#endif
9308}
9309
9310/*
9311 * "histdel()" function
9312 */
9313/*ARGSUSED*/
9314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318{
9319#ifdef FEAT_CMDHIST
9320 int n;
9321 char_u buf[NUMBUFLEN];
9322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009323 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009326 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9329 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 else
9331 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9333 get_tv_string_buf(&argvars[1], buf));
9334 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337#endif
9338}
9339
9340/*
9341 * "histget()" function
9342 */
9343/*ARGSUSED*/
9344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 typval_T *argvars;
9347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349#ifdef FEAT_CMDHIST
9350 int type;
9351 int idx;
9352
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009354 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 idx = get_history_idx(type);
9356 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 idx = (int)get_tv_number(&argvars[1]);
9358 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
9364
9365/*
9366 * "histnr()" function
9367 */
9368/*ARGSUSED*/
9369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 typval_T *argvars;
9372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373{
9374 int i;
9375
9376#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 if (i >= HIST_CMD && i < HIST_COUNT)
9379 i = get_history_idx(i);
9380 else
9381#endif
9382 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384}
9385
9386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 * "highlightID(name)" function
9388 */
9389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
9397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009398 * "highlight_exists()" function
9399 */
9400 static void
9401f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009402 typval_T *argvars;
9403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404{
9405 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9406}
9407
9408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 * "hostname()" function
9410 */
9411/*ARGSUSED*/
9412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 typval_T *argvars;
9415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417 char_u hostname[256];
9418
9419 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->v_type = VAR_STRING;
9421 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
9424/*
9425 * iconv() function
9426 */
9427/*ARGSUSED*/
9428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 typval_T *argvars;
9431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433#ifdef FEAT_MBYTE
9434 char_u buf1[NUMBUFLEN];
9435 char_u buf2[NUMBUFLEN];
9436 char_u *from, *to, *str;
9437 vimconv_T vimconv;
9438#endif
9439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->v_type = VAR_STRING;
9441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442
9443#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 str = get_tv_string(&argvars[0]);
9445 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9446 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 vimconv.vc_type = CONV_NONE;
9448 convert_setup(&vimconv, from, to);
9449
9450 /* If the encodings are equal, no conversion needed. */
9451 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
9456 convert_setup(&vimconv, NULL, NULL);
9457 vim_free(from);
9458 vim_free(to);
9459#endif
9460}
9461
9462/*
9463 * "indent()" function
9464 */
9465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 linenr_T lnum;
9471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477}
9478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009479/*
9480 * "index()" function
9481 */
9482 static void
9483f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 typval_T *argvars;
9485 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009486{
Bram Moolenaar33570922005-01-25 22:26:29 +00009487 list_T *l;
9488 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009489 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009490 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009491 int ic = FALSE;
9492
9493 rettv->vval.v_number = -1;
9494 if (argvars[0].v_type != VAR_LIST)
9495 {
9496 EMSG(_(e_listreq));
9497 return;
9498 }
9499 l = argvars[0].vval.v_list;
9500 if (l != NULL)
9501 {
9502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009503 {
9504 min_idx = get_tv_number(&argvars[2]);
9505 if (argvars[3].v_type != VAR_UNKNOWN)
9506 ic = get_tv_number(&argvars[3]);
9507 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009508
9509 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009510 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009511 {
9512 rettv->vval.v_number = idx;
9513 break;
9514 }
9515 }
9516}
9517
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518static int inputsecret_flag = 0;
9519
9520/*
9521 * "input()" function
9522 * Also handles inputsecret() when inputsecret is set.
9523 */
9524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 char_u *p = NULL;
9531 int c;
9532 char_u buf[NUMBUFLEN];
9533 int cmd_silent_save = cmd_silent;
9534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
9537#ifdef NO_CONSOLE_INPUT
9538 /* While starting up, there is no place to enter text. */
9539 if (no_console_input())
9540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 return;
9543 }
9544#endif
9545
9546 cmd_silent = FALSE; /* Want to see the prompt. */
9547 if (prompt != NULL)
9548 {
9549 /* Only the part of the message after the last NL is considered as
9550 * prompt for the command line */
9551 p = vim_strrchr(prompt, '\n');
9552 if (p == NULL)
9553 p = prompt;
9554 else
9555 {
9556 ++p;
9557 c = *p;
9558 *p = NUL;
9559 msg_start();
9560 msg_clr_eos();
9561 msg_puts_attr(prompt, echo_attr);
9562 msg_didout = FALSE;
9563 msg_starthere();
9564 *p = c;
9565 }
9566 cmdline_row = msg_row;
9567 }
9568
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9574
9575 /* since the user typed this, no need to wait for return */
9576 need_wait_return = FALSE;
9577 msg_didout = FALSE;
9578 cmd_silent = cmd_silent_save;
9579}
9580
9581/*
9582 * "inputdialog()" function
9583 */
9584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589#if defined(FEAT_GUI_TEXTDIALOG)
9590 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9591 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9592 {
9593 char_u *message;
9594 char_u buf[NUMBUFLEN];
9595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 IObuff[IOSIZE - 1] = NUL;
9601 }
9602 else
9603 IObuff[0] = NUL;
9604 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9605 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 else
9608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009609 if (argvars[1].v_type != VAR_UNKNOWN
9610 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_string = vim_strsave(
9612 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 }
9618 else
9619#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621}
9622
9623static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9624
9625/*
9626 * "inputrestore()" function
9627 */
9628/*ARGSUSED*/
9629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009631 typval_T *argvars;
9632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 if (ga_userinput.ga_len > 0)
9635 {
9636 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9638 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 }
9641 else if (p_verbose > 1)
9642 {
9643 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 }
9646}
9647
9648/*
9649 * "inputsave()" function
9650 */
9651/*ARGSUSED*/
9652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 typval_T *argvars;
9655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 /* Add an entry to the stack of typehead storage. */
9658 if (ga_grow(&ga_userinput, 1) == OK)
9659 {
9660 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9661 + ga_userinput.ga_len);
9662 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 }
9665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667}
9668
9669/*
9670 * "inputsecret()" function
9671 */
9672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677 ++cmdline_star;
9678 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 --cmdline_star;
9681 --inputsecret_flag;
9682}
9683
9684/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009685 * "insert()" function
9686 */
9687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 typval_T *argvars;
9690 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691{
9692 long before = 0;
9693 long n;
Bram Moolenaar33570922005-01-25 22:26:29 +00009694 listitem_T *item;
9695 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009696
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009697 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009699 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009700 else if ((l = argvars[0].vval.v_list) != NULL
9701 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 {
9703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009705
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009706 n = before;
9707 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009708 if (n > 0)
9709 EMSGN(_(e_listidx), before);
9710 else
9711 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009712 list_insert_tv(l, &argvars[1], item);
9713 ++l->lv_refcount;
9714 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009715 }
9716 }
9717}
9718
9719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 * "isdirectory()" function
9721 */
9722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 typval_T *argvars;
9725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728}
9729
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009730static int tv_islocked __ARGS((typval_T *tv));
9731
9732/*
9733 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
9734 * it refers to a List or Dictionary that is locked.
9735 */
9736 static int
9737tv_islocked(tv)
9738 typval_T *tv;
9739{
9740 return (tv->v_lock & VAR_LOCKED)
9741 || (tv->v_type == VAR_LIST
9742 && tv->vval.v_list != NULL
9743 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
9744 || (tv->v_type == VAR_DICT
9745 && tv->vval.v_dict != NULL
9746 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
9747}
9748
9749/*
9750 * "islocked()" function
9751 */
9752 static void
9753f_islocked(argvars, rettv)
9754 typval_T *argvars;
9755 typval_T *rettv;
9756{
9757 lval_T lv;
9758 char_u *end;
9759 dictitem_T *di;
9760
9761 rettv->vval.v_number = -1;
9762 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
9763 if (end != NULL && lv.ll_name != NULL)
9764 {
9765 if (*end != NUL)
9766 EMSG(_(e_trailing));
9767 else
9768 {
9769 if (lv.ll_tv == NULL)
9770 {
9771 if (check_changedtick(lv.ll_name))
9772 rettv->vval.v_number = 1; /* always locked */
9773 else
9774 {
9775 di = find_var(lv.ll_name, NULL);
9776 if (di != NULL)
9777 {
9778 /* Consider a variable locked when:
9779 * 1. the variable itself is locked
9780 * 2. the value of the variable is locked.
9781 * 3. the List or Dict value is locked.
9782 */
9783 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
9784 || tv_islocked(&di->di_tv));
9785 }
9786 }
9787 }
9788 else if (lv.ll_range)
9789 EMSG(_("E745: Range not allowed"));
9790 else if (lv.ll_newkey != NULL)
9791 EMSG2(_(e_dictkey), lv.ll_newkey);
9792 else if (lv.ll_list != NULL)
9793 /* List item. */
9794 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
9795 else
9796 /* Dictionary item. */
9797 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
9798 }
9799 }
9800
9801 clear_lval(&lv);
9802}
9803
Bram Moolenaar33570922005-01-25 22:26:29 +00009804static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +00009805
9806/*
9807 * Turn a dict into a list:
9808 * "what" == 0: list of keys
9809 * "what" == 1: list of values
9810 * "what" == 2: list of items
9811 */
9812 static void
9813dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009814 typval_T *argvars;
9815 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009816 int what;
9817{
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 list_T *l;
9819 list_T *l2;
9820 dictitem_T *di;
9821 hashitem_T *hi;
9822 listitem_T *li;
9823 listitem_T *li2;
9824 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009825 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009826
9827 rettv->vval.v_number = 0;
9828 if (argvars[0].v_type != VAR_DICT)
9829 {
9830 EMSG(_(e_dictreq));
9831 return;
9832 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009833 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009834 return;
9835
9836 l = list_alloc();
9837 if (l == NULL)
9838 return;
9839 rettv->v_type = VAR_LIST;
9840 rettv->vval.v_list = l;
9841 ++l->lv_refcount;
9842
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 todo = d->dv_hashtab.ht_used;
9844 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009846 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00009847 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009848 --todo;
9849 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009850
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009851 li = listitem_alloc();
9852 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009853 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009854 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009855
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009856 if (what == 0)
9857 {
9858 /* keys() */
9859 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009860 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009861 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9862 }
9863 else if (what == 1)
9864 {
9865 /* values() */
9866 copy_tv(&di->di_tv, &li->li_tv);
9867 }
9868 else
9869 {
9870 /* items() */
9871 l2 = list_alloc();
9872 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009873 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009874 li->li_tv.vval.v_list = l2;
9875 if (l2 == NULL)
9876 break;
9877 ++l2->lv_refcount;
9878
9879 li2 = listitem_alloc();
9880 if (li2 == NULL)
9881 break;
9882 list_append(l2, li2);
9883 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009884 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009885 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9886
9887 li2 = listitem_alloc();
9888 if (li2 == NULL)
9889 break;
9890 list_append(l2, li2);
9891 copy_tv(&di->di_tv, &li2->li_tv);
9892 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00009893 }
9894 }
9895}
9896
9897/*
9898 * "items(dict)" function
9899 */
9900 static void
9901f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009904{
9905 dict_list(argvars, rettv, 2);
9906}
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009909 * "join()" function
9910 */
9911 static void
9912f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009915{
9916 garray_T ga;
9917 char_u *sep;
9918
9919 rettv->vval.v_number = 0;
9920 if (argvars[0].v_type != VAR_LIST)
9921 {
9922 EMSG(_(e_listreq));
9923 return;
9924 }
9925 if (argvars[0].vval.v_list == NULL)
9926 return;
9927 if (argvars[1].v_type == VAR_UNKNOWN)
9928 sep = (char_u *)" ";
9929 else
9930 sep = get_tv_string(&argvars[1]);
9931
9932 ga_init2(&ga, (int)sizeof(char), 80);
9933 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9934 ga_append(&ga, NUL);
9935
9936 rettv->v_type = VAR_STRING;
9937 rettv->vval.v_string = (char_u *)ga.ga_data;
9938}
9939
9940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009941 * "keys()" function
9942 */
9943 static void
9944f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009947{
9948 dict_list(argvars, rettv, 0);
9949}
9950
9951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 * "last_buffer_nr()" function.
9953 */
9954/*ARGSUSED*/
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960 int n = 0;
9961 buf_T *buf;
9962
9963 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9964 if (n < buf->b_fnum)
9965 n = buf->b_fnum;
9966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009968}
9969
9970/*
9971 * "len()" function
9972 */
9973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 typval_T *argvars;
9976 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009977{
9978 switch (argvars[0].v_type)
9979 {
9980 case VAR_STRING:
9981 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = (varnumber_T)STRLEN(
9983 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009984 break;
9985 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009987 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009988 case VAR_DICT:
9989 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9990 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009991 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009992 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009993 break;
9994 }
9995}
9996
Bram Moolenaar33570922005-01-25 22:26:29 +00009997static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009998
9999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 typval_T *argvars;
10002 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010003 int type;
10004{
10005#ifdef FEAT_LIBCALL
10006 char_u *string_in;
10007 char_u **string_result;
10008 int nr_result;
10009#endif
10010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010014 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010016
10017 if (check_restricted() || check_secure())
10018 return;
10019
10020#ifdef FEAT_LIBCALL
10021 /* The first two args must be strings, otherwise its meaningless */
10022 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10023 {
10024 if (argvars[2].v_type == VAR_NUMBER)
10025 string_in = NULL;
10026 else
10027 string_in = argvars[2].vval.v_string;
10028 if (type == VAR_NUMBER)
10029 string_result = NULL;
10030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010032 if (mch_libcall(argvars[0].vval.v_string,
10033 argvars[1].vval.v_string,
10034 string_in,
10035 argvars[2].vval.v_number,
10036 string_result,
10037 &nr_result) == OK
10038 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010040 }
10041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042}
10043
10044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010045 * "libcall()" function
10046 */
10047 static void
10048f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *argvars;
10050 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010051{
10052 libcall_common(argvars, rettv, VAR_STRING);
10053}
10054
10055/*
10056 * "libcallnr()" function
10057 */
10058 static void
10059f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010062{
10063 libcall_common(argvars, rettv, VAR_NUMBER);
10064}
10065
10066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 * "line(string)" function
10068 */
10069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 typval_T *argvars;
10072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 linenr_T lnum = 0;
10075 pos_T *fp;
10076
10077 fp = var2fpos(&argvars[0], TRUE);
10078 if (fp != NULL)
10079 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081}
10082
10083/*
10084 * "line2byte(lnum)" function
10085 */
10086/*ARGSUSED*/
10087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091{
10092#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#else
10095 linenr_T lnum;
10096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10102 if (rettv->vval.v_number >= 0)
10103 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#endif
10105}
10106
10107/*
10108 * "lispindent(lnum)" function
10109 */
10110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010112 typval_T *argvars;
10113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
10115#ifdef FEAT_LISP
10116 pos_T pos;
10117 linenr_T lnum;
10118
10119 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10122 {
10123 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 curwin->w_cursor = pos;
10126 }
10127 else
10128#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130}
10131
10132/*
10133 * "localtime()" function
10134 */
10135/*ARGSUSED*/
10136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142}
10143
Bram Moolenaar33570922005-01-25 22:26:29 +000010144static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
10146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010148 typval_T *argvars;
10149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 int exact;
10151{
10152 char_u *keys;
10153 char_u *which;
10154 char_u buf[NUMBUFLEN];
10155 char_u *keys_buf = NULL;
10156 char_u *rhs;
10157 int mode;
10158 garray_T ga;
10159
10160 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->v_type = VAR_STRING;
10162 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 if (*keys == NUL)
10166 return;
10167
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010168 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 else
10171 which = (char_u *)"";
10172 mode = get_map_mode(&which, 0);
10173
10174 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10175 rhs = check_map(keys, mode, exact);
10176 vim_free(keys_buf);
10177 if (rhs != NULL)
10178 {
10179 ga_init(&ga);
10180 ga.ga_itemsize = 1;
10181 ga.ga_growsize = 40;
10182
10183 while (*rhs != NUL)
10184 ga_concat(&ga, str2special(&rhs, FALSE));
10185
10186 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 }
10189}
10190
10191/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010192 * "map()" function
10193 */
10194 static void
10195f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010196 typval_T *argvars;
10197 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010198{
10199 filter_map(argvars, rettv, TRUE);
10200}
10201
10202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010203 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 */
10205 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010206f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010207 typval_T *argvars;
10208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010210 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211}
10212
10213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010214 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 */
10216 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010217f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 typval_T *argvars;
10219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010221 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222}
10223
Bram Moolenaar33570922005-01-25 22:26:29 +000010224static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
10226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010227find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010228 typval_T *argvars;
10229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 int type;
10231{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010232 char_u *str = NULL;
10233 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 char_u *pat;
10235 regmatch_T regmatch;
10236 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010237 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 char_u *save_cpo;
10239 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010240 long nth = 1;
10241 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 list_T *l = NULL;
10243 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010244 long idx = 0;
10245 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246
10247 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10248 save_cpo = p_cpo;
10249 p_cpo = (char_u *)"";
10250
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 if (type == 2)
10252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253 rettv->v_type = VAR_STRING;
10254 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 }
10256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259 if (argvars[0].v_type == VAR_LIST)
10260 {
10261 if ((l = argvars[0].vval.v_list) == NULL)
10262 goto theend;
10263 li = l->lv_first;
10264 }
10265 else
10266 expr = str = get_tv_string(&argvars[0]);
10267
10268 pat = get_tv_string_buf(&argvars[1], patbuf);
10269
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010272 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010273 if (l != NULL)
10274 {
10275 li = list_find(l, start);
10276 if (li == NULL)
10277 goto theend;
10278 if (start < 0)
10279 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010280 listitem_T *ni;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010281
10282 /* Need to compute the index. */
10283 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
10284 ++idx;
10285 }
10286 else
10287 idx = start;
10288 }
10289 else
10290 {
10291 if (start < 0)
10292 start = 0;
10293 if (start > (long)STRLEN(str))
10294 goto theend;
10295 str += start;
10296 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010297
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010298 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 }
10301
10302 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10303 if (regmatch.regprog != NULL)
10304 {
10305 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010306
10307 while (1)
10308 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010309 if (l != NULL)
10310 {
10311 if (li == NULL)
10312 {
10313 match = FALSE;
10314 break;
10315 }
10316 str = echo_string(&li->li_tv, &tofree, strbuf);
10317 }
10318
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010319 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010320
10321 if (l != NULL)
10322 vim_free(tofree);
10323 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010324 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010325 if (l == NULL && !match)
10326 break;
10327
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010328 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010329 if (l != NULL)
10330 {
10331 li = li->li_next;
10332 ++idx;
10333 }
10334 else
10335 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010336#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010337 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010338#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010339 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010340#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010341 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010342 }
10343
10344 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 {
10346 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010347 {
10348 if (l != NULL)
10349 copy_tv(&li->li_tv, rettv);
10350 else
10351 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010353 }
10354 else if (l != NULL)
10355 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 else
10357 {
10358 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 (varnumber_T)(regmatch.startp[0] - str);
10361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366 }
10367 vim_free(regmatch.regprog);
10368 }
10369
10370theend:
10371 p_cpo = save_cpo;
10372}
10373
10374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010375 * "match()" function
10376 */
10377 static void
10378f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010381{
10382 find_some_match(argvars, rettv, 1);
10383}
10384
10385/*
10386 * "matchend()" function
10387 */
10388 static void
10389f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010390 typval_T *argvars;
10391 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010392{
10393 find_some_match(argvars, rettv, 0);
10394}
10395
10396/*
10397 * "matchstr()" function
10398 */
10399 static void
10400f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010401 typval_T *argvars;
10402 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010403{
10404 find_some_match(argvars, rettv, 2);
10405}
10406
Bram Moolenaar33570922005-01-25 22:26:29 +000010407static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010408
10409 static void
10410max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 typval_T *argvars;
10412 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010413 int domax;
10414{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010415 long n = 0;
10416 long i;
10417
10418 if (argvars[0].v_type == VAR_LIST)
10419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010420 list_T *l;
10421 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010423 l = argvars[0].vval.v_list;
10424 if (l != NULL)
10425 {
10426 li = l->lv_first;
10427 if (li != NULL)
10428 {
10429 n = get_tv_number(&li->li_tv);
10430 while (1)
10431 {
10432 li = li->li_next;
10433 if (li == NULL)
10434 break;
10435 i = get_tv_number(&li->li_tv);
10436 if (domax ? i > n : i < n)
10437 n = i;
10438 }
10439 }
10440 }
10441 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010442 else if (argvars[0].v_type == VAR_DICT)
10443 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010444 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010445 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010447 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448
10449 d = argvars[0].vval.v_dict;
10450 if (d != NULL)
10451 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010452 todo = d->dv_hashtab.ht_used;
10453 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010455 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010457 --todo;
10458 i = get_tv_number(&HI2DI(hi)->di_tv);
10459 if (first)
10460 {
10461 n = i;
10462 first = FALSE;
10463 }
10464 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 n = i;
10466 }
10467 }
10468 }
10469 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010470 else
10471 EMSG(_(e_listreq));
10472 rettv->vval.v_number = n;
10473}
10474
10475/*
10476 * "max()" function
10477 */
10478 static void
10479f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 typval_T *argvars;
10481 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010482{
10483 max_min(argvars, rettv, TRUE);
10484}
10485
10486/*
10487 * "min()" function
10488 */
10489 static void
10490f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 typval_T *argvars;
10492 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010493{
10494 max_min(argvars, rettv, FALSE);
10495}
10496
Bram Moolenaar0d660222005-01-07 21:51:51 +000010497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 * "mode()" function
10499 */
10500/*ARGSUSED*/
10501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010503 typval_T *argvars;
10504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506 char_u buf[2];
10507
10508#ifdef FEAT_VISUAL
10509 if (VIsual_active)
10510 {
10511 if (VIsual_select)
10512 buf[0] = VIsual_mode + 's' - 'v';
10513 else
10514 buf[0] = VIsual_mode;
10515 }
10516 else
10517#endif
10518 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10519 buf[0] = 'r';
10520 else if (State & INSERT)
10521 {
10522 if (State & REPLACE_FLAG)
10523 buf[0] = 'R';
10524 else
10525 buf[0] = 'i';
10526 }
10527 else if (State & CMDLINE)
10528 buf[0] = 'c';
10529 else
10530 buf[0] = 'n';
10531
10532 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 rettv->vval.v_string = vim_strsave(buf);
10534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536
10537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010538 * "nextnonblank()" function
10539 */
10540 static void
10541f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010542 typval_T *argvars;
10543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010544{
10545 linenr_T lnum;
10546
10547 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10548 {
10549 if (lnum > curbuf->b_ml.ml_line_count)
10550 {
10551 lnum = 0;
10552 break;
10553 }
10554 if (*skipwhite(ml_get(lnum)) != NUL)
10555 break;
10556 }
10557 rettv->vval.v_number = lnum;
10558}
10559
10560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 * "nr2char()" function
10562 */
10563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568 char_u buf[NUMBUFLEN];
10569
10570#ifdef FEAT_MBYTE
10571 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 else
10574#endif
10575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 buf[1] = NUL;
10578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 rettv->v_type = VAR_STRING;
10580 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010581}
10582
10583/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010584 * "prevnonblank()" function
10585 */
10586 static void
10587f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010588 typval_T *argvars;
10589 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010590{
10591 linenr_T lnum;
10592
10593 lnum = get_tv_lnum(argvars);
10594 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10595 lnum = 0;
10596 else
10597 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10598 --lnum;
10599 rettv->vval.v_number = lnum;
10600}
10601
Bram Moolenaar8c711452005-01-14 21:53:12 +000010602/*
10603 * "range()" function
10604 */
10605 static void
10606f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 typval_T *argvars;
10608 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010609{
10610 long start;
10611 long end;
10612 long stride = 1;
10613 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010614 list_T *l;
10615 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010616
10617 start = get_tv_number(&argvars[0]);
10618 if (argvars[1].v_type == VAR_UNKNOWN)
10619 {
10620 end = start - 1;
10621 start = 0;
10622 }
10623 else
10624 {
10625 end = get_tv_number(&argvars[1]);
10626 if (argvars[2].v_type != VAR_UNKNOWN)
10627 stride = get_tv_number(&argvars[2]);
10628 }
10629
10630 rettv->vval.v_number = 0;
10631 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010632 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010633 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010634 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010635 else
10636 {
10637 l = list_alloc();
10638 if (l != NULL)
10639 {
10640 rettv->v_type = VAR_LIST;
10641 rettv->vval.v_list = l;
10642 ++l->lv_refcount;
10643
10644 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10645 {
10646 li = listitem_alloc();
10647 if (li == NULL)
10648 break;
10649 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010650 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010651 li->li_tv.vval.v_number = i;
10652 list_append(l, li);
10653 }
10654 }
10655 }
10656}
10657
Bram Moolenaar0d660222005-01-07 21:51:51 +000010658#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10659static void make_connection __ARGS((void));
10660static int check_connection __ARGS((void));
10661
10662 static void
10663make_connection()
10664{
10665 if (X_DISPLAY == NULL
10666# ifdef FEAT_GUI
10667 && !gui.in_use
10668# endif
10669 )
10670 {
10671 x_force_connect = TRUE;
10672 setup_term_clip();
10673 x_force_connect = FALSE;
10674 }
10675}
10676
10677 static int
10678check_connection()
10679{
10680 make_connection();
10681 if (X_DISPLAY == NULL)
10682 {
10683 EMSG(_("E240: No connection to Vim server"));
10684 return FAIL;
10685 }
10686 return OK;
10687}
10688#endif
10689
10690#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010691static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000010692
10693 static void
10694remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000010695 typval_T *argvars;
10696 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010697 int expr;
10698{
10699 char_u *server_name;
10700 char_u *keys;
10701 char_u *r = NULL;
10702 char_u buf[NUMBUFLEN];
10703# ifdef WIN32
10704 HWND w;
10705# else
10706 Window w;
10707# endif
10708
10709 if (check_restricted() || check_secure())
10710 return;
10711
10712# ifdef FEAT_X11
10713 if (check_connection() == FAIL)
10714 return;
10715# endif
10716
10717 server_name = get_tv_string(&argvars[0]);
10718 keys = get_tv_string_buf(&argvars[1], buf);
10719# ifdef WIN32
10720 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10721# else
10722 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10723 < 0)
10724# endif
10725 {
10726 if (r != NULL)
10727 EMSG(r); /* sending worked but evaluation failed */
10728 else
10729 EMSG2(_("E241: Unable to send to %s"), server_name);
10730 return;
10731 }
10732
10733 rettv->vval.v_string = r;
10734
10735 if (argvars[2].v_type != VAR_UNKNOWN)
10736 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010738 char_u str[30];
10739
10740 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 v.di_tv.v_type = VAR_STRING;
10742 v.di_tv.vval.v_string = vim_strsave(str);
10743 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
10744 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010745 }
10746}
10747#endif
10748
10749/*
10750 * "remote_expr()" function
10751 */
10752/*ARGSUSED*/
10753 static void
10754f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010755 typval_T *argvars;
10756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010757{
10758 rettv->v_type = VAR_STRING;
10759 rettv->vval.v_string = NULL;
10760#ifdef FEAT_CLIENTSERVER
10761 remote_common(argvars, rettv, TRUE);
10762#endif
10763}
10764
10765/*
10766 * "remote_foreground()" function
10767 */
10768/*ARGSUSED*/
10769 static void
10770f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010771 typval_T *argvars;
10772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010773{
10774 rettv->vval.v_number = 0;
10775#ifdef FEAT_CLIENTSERVER
10776# ifdef WIN32
10777 /* On Win32 it's done in this application. */
10778 serverForeground(get_tv_string(&argvars[0]));
10779# else
10780 /* Send a foreground() expression to the server. */
10781 argvars[1].v_type = VAR_STRING;
10782 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10783 argvars[2].v_type = VAR_UNKNOWN;
10784 remote_common(argvars, rettv, TRUE);
10785 vim_free(argvars[1].vval.v_string);
10786# endif
10787#endif
10788}
10789
10790/*ARGSUSED*/
10791 static void
10792f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010793 typval_T *argvars;
10794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010795{
10796#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010797 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798 char_u *s = NULL;
10799# ifdef WIN32
10800 int n = 0;
10801# endif
10802
10803 if (check_restricted() || check_secure())
10804 {
10805 rettv->vval.v_number = -1;
10806 return;
10807 }
10808# ifdef WIN32
10809 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10810 if (n == 0)
10811 rettv->vval.v_number = -1;
10812 else
10813 {
10814 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10815 rettv->vval.v_number = (s != NULL);
10816 }
10817# else
10818 rettv->vval.v_number = 0;
10819 if (check_connection() == FAIL)
10820 return;
10821
10822 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10823 serverStrToWin(get_tv_string(&argvars[0])), &s);
10824# endif
10825
10826 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10827 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 v.di_tv.v_type = VAR_STRING;
10829 v.di_tv.vval.v_string = vim_strsave(s);
10830 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
10831 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832 }
10833#else
10834 rettv->vval.v_number = -1;
10835#endif
10836}
10837
10838/*ARGSUSED*/
10839 static void
10840f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010841 typval_T *argvars;
10842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010843{
10844 char_u *r = NULL;
10845
10846#ifdef FEAT_CLIENTSERVER
10847 if (!check_restricted() && !check_secure())
10848 {
10849# ifdef WIN32
10850 /* The server's HWND is encoded in the 'id' parameter */
10851 int n = 0;
10852
10853 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10854 if (n != 0)
10855 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10856 if (r == NULL)
10857# else
10858 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10859 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10860# endif
10861 EMSG(_("E277: Unable to read a server reply"));
10862 }
10863#endif
10864 rettv->v_type = VAR_STRING;
10865 rettv->vval.v_string = r;
10866}
10867
10868/*
10869 * "remote_send()" function
10870 */
10871/*ARGSUSED*/
10872 static void
10873f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010874 typval_T *argvars;
10875 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876{
10877 rettv->v_type = VAR_STRING;
10878 rettv->vval.v_string = NULL;
10879#ifdef FEAT_CLIENTSERVER
10880 remote_common(argvars, rettv, FALSE);
10881#endif
10882}
10883
10884/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010885 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010886 */
10887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 typval_T *argvars;
10890 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010891{
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 list_T *l;
10893 listitem_T *item, *item2;
10894 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010895 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010896 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010897 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 dict_T *d;
10899 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010900
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010901 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010902 if (argvars[0].v_type == VAR_DICT)
10903 {
10904 if (argvars[2].v_type != VAR_UNKNOWN)
10905 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010906 else if ((d = argvars[0].vval.v_dict) != NULL
10907 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010908 {
10909 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010910 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010911 if (di == NULL)
10912 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010913 else
10914 {
10915 *rettv = di->di_tv;
10916 init_tv(&di->di_tv);
10917 dictitem_remove(d, di);
10918 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010919 }
10920 }
10921 else if (argvars[0].v_type != VAR_LIST)
10922 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010923 else if ((l = argvars[0].vval.v_list) != NULL
10924 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010927 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010928 if (item == NULL)
10929 EMSGN(_(e_listidx), idx);
10930 else
10931 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010932 if (argvars[2].v_type == VAR_UNKNOWN)
10933 {
10934 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010935 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010936 *rettv = item->li_tv;
10937 vim_free(item);
10938 }
10939 else
10940 {
10941 /* Remove range of items, return list with values. */
10942 end = get_tv_number(&argvars[2]);
10943 item2 = list_find(l, end);
10944 if (item2 == NULL)
10945 EMSGN(_(e_listidx), end);
10946 else
10947 {
10948 for (li = item; li != item2 && li != NULL; li = li->li_next)
10949 ;
10950 if (li == NULL) /* didn't find "item2" after "item" */
10951 EMSG(_(e_invrange));
10952 else
10953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010954 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010955 l = list_alloc();
10956 if (l != NULL)
10957 {
10958 rettv->v_type = VAR_LIST;
10959 rettv->vval.v_list = l;
10960 l->lv_first = item;
10961 l->lv_last = item2;
10962 l->lv_refcount = 1;
10963 item->li_prev = NULL;
10964 item2->li_next = NULL;
10965 }
10966 }
10967 }
10968 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010969 }
10970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971}
10972
10973/*
10974 * "rename({from}, {to})" function
10975 */
10976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 typval_T *argvars;
10979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980{
10981 char_u buf[NUMBUFLEN];
10982
10983 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10987 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010991 * "repeat()" function
10992 */
10993/*ARGSUSED*/
10994 static void
10995f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 typval_T *argvars;
10997 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010998{
10999 char_u *p;
11000 int n;
11001 int slen;
11002 int len;
11003 char_u *r;
11004 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011006
11007 n = get_tv_number(&argvars[1]);
11008 if (argvars[0].v_type == VAR_LIST)
11009 {
11010 l = list_alloc();
11011 if (l != NULL && argvars[0].vval.v_list != NULL)
11012 {
11013 l->lv_refcount = 1;
11014 while (n-- > 0)
11015 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11016 break;
11017 }
11018 rettv->v_type = VAR_LIST;
11019 rettv->vval.v_list = l;
11020 }
11021 else
11022 {
11023 p = get_tv_string(&argvars[0]);
11024 rettv->v_type = VAR_STRING;
11025 rettv->vval.v_string = NULL;
11026
11027 slen = (int)STRLEN(p);
11028 len = slen * n;
11029 if (len <= 0)
11030 return;
11031
11032 r = alloc(len + 1);
11033 if (r != NULL)
11034 {
11035 for (i = 0; i < n; i++)
11036 mch_memmove(r + i * slen, p, (size_t)slen);
11037 r[len] = NUL;
11038 }
11039
11040 rettv->vval.v_string = r;
11041 }
11042}
11043
11044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 * "resolve()" function
11046 */
11047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 typval_T *argvars;
11050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
11052 char_u *p;
11053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055#ifdef FEAT_SHORTCUT
11056 {
11057 char_u *v = NULL;
11058
11059 v = mch_resolve_shortcut(p);
11060 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 }
11065#else
11066# ifdef HAVE_READLINK
11067 {
11068 char_u buf[MAXPATHL + 1];
11069 char_u *cpy;
11070 int len;
11071 char_u *remain = NULL;
11072 char_u *q;
11073 int is_relative_to_current = FALSE;
11074 int has_trailing_pathsep = FALSE;
11075 int limit = 100;
11076
11077 p = vim_strsave(p);
11078
11079 if (p[0] == '.' && (vim_ispathsep(p[1])
11080 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11081 is_relative_to_current = TRUE;
11082
11083 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011084 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 has_trailing_pathsep = TRUE;
11086
11087 q = getnextcomp(p);
11088 if (*q != NUL)
11089 {
11090 /* Separate the first path component in "p", and keep the
11091 * remainder (beginning with the path separator). */
11092 remain = vim_strsave(q - 1);
11093 q[-1] = NUL;
11094 }
11095
11096 for (;;)
11097 {
11098 for (;;)
11099 {
11100 len = readlink((char *)p, (char *)buf, MAXPATHL);
11101 if (len <= 0)
11102 break;
11103 buf[len] = NUL;
11104
11105 if (limit-- == 0)
11106 {
11107 vim_free(p);
11108 vim_free(remain);
11109 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 goto fail;
11112 }
11113
11114 /* Ensure that the result will have a trailing path separator
11115 * if the argument has one. */
11116 if (remain == NULL && has_trailing_pathsep)
11117 add_pathsep(buf);
11118
11119 /* Separate the first path component in the link value and
11120 * concatenate the remainders. */
11121 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11122 if (*q != NUL)
11123 {
11124 if (remain == NULL)
11125 remain = vim_strsave(q - 1);
11126 else
11127 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011128 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 if (cpy != NULL)
11130 {
11131 STRCAT(cpy, remain);
11132 vim_free(remain);
11133 remain = cpy;
11134 }
11135 }
11136 q[-1] = NUL;
11137 }
11138
11139 q = gettail(p);
11140 if (q > p && *q == NUL)
11141 {
11142 /* Ignore trailing path separator. */
11143 q[-1] = NUL;
11144 q = gettail(p);
11145 }
11146 if (q > p && !mch_isFullName(buf))
11147 {
11148 /* symlink is relative to directory of argument */
11149 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11150 if (cpy != NULL)
11151 {
11152 STRCPY(cpy, p);
11153 STRCPY(gettail(cpy), buf);
11154 vim_free(p);
11155 p = cpy;
11156 }
11157 }
11158 else
11159 {
11160 vim_free(p);
11161 p = vim_strsave(buf);
11162 }
11163 }
11164
11165 if (remain == NULL)
11166 break;
11167
11168 /* Append the first path component of "remain" to "p". */
11169 q = getnextcomp(remain + 1);
11170 len = q - remain - (*q != NUL);
11171 cpy = vim_strnsave(p, STRLEN(p) + len);
11172 if (cpy != NULL)
11173 {
11174 STRNCAT(cpy, remain, len);
11175 vim_free(p);
11176 p = cpy;
11177 }
11178 /* Shorten "remain". */
11179 if (*q != NUL)
11180 STRCPY(remain, q - 1);
11181 else
11182 {
11183 vim_free(remain);
11184 remain = NULL;
11185 }
11186 }
11187
11188 /* If the result is a relative path name, make it explicitly relative to
11189 * the current directory if and only if the argument had this form. */
11190 if (!vim_ispathsep(*p))
11191 {
11192 if (is_relative_to_current
11193 && *p != NUL
11194 && !(p[0] == '.'
11195 && (p[1] == NUL
11196 || vim_ispathsep(p[1])
11197 || (p[1] == '.'
11198 && (p[2] == NUL
11199 || vim_ispathsep(p[2]))))))
11200 {
11201 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011202 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 if (cpy != NULL)
11204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 vim_free(p);
11206 p = cpy;
11207 }
11208 }
11209 else if (!is_relative_to_current)
11210 {
11211 /* Strip leading "./". */
11212 q = p;
11213 while (q[0] == '.' && vim_ispathsep(q[1]))
11214 q += 2;
11215 if (q > p)
11216 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11217 }
11218 }
11219
11220 /* Ensure that the result will have no trailing path separator
11221 * if the argument had none. But keep "/" or "//". */
11222 if (!has_trailing_pathsep)
11223 {
11224 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011225 if (after_pathsep(p, q))
11226 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 }
11228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 }
11231# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233# endif
11234#endif
11235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237
11238#ifdef HAVE_READLINK
11239fail:
11240#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242}
11243
11244/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011245 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 */
11247 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011249 typval_T *argvars;
11250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
Bram Moolenaar33570922005-01-25 22:26:29 +000011252 list_T *l;
11253 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 rettv->vval.v_number = 0;
11256 if (argvars[0].v_type != VAR_LIST)
11257 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011258 else if ((l = argvars[0].vval.v_list) != NULL
11259 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011260 {
11261 li = l->lv_last;
11262 l->lv_first = l->lv_last = li;
11263 while (li != NULL)
11264 {
11265 ni = li->li_prev;
11266 list_append(l, li);
11267 li = ni;
11268 }
11269 rettv->vval.v_list = l;
11270 rettv->v_type = VAR_LIST;
11271 ++l->lv_refcount;
11272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273}
11274
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011275#define SP_NOMOVE 1 /* don't move cursor */
11276#define SP_REPEAT 2 /* repeat to find outer pair */
11277#define SP_RETCOUNT 4 /* return matchcount */
11278
Bram Moolenaar33570922005-01-25 22:26:29 +000011279static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280
11281/*
11282 * Get flags for a search function.
11283 * Possibly sets "p_ws".
11284 * Returns BACKWARD, FORWARD or zero (for an error).
11285 */
11286 static int
11287get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011289 int *flagsp;
11290{
11291 int dir = FORWARD;
11292 char_u *flags;
11293 char_u nbuf[NUMBUFLEN];
11294 int mask;
11295
11296 if (varp->v_type != VAR_UNKNOWN)
11297 {
11298 flags = get_tv_string_buf(varp, nbuf);
11299 while (*flags != NUL)
11300 {
11301 switch (*flags)
11302 {
11303 case 'b': dir = BACKWARD; break;
11304 case 'w': p_ws = TRUE; break;
11305 case 'W': p_ws = FALSE; break;
11306 default: mask = 0;
11307 if (flagsp != NULL)
11308 switch (*flags)
11309 {
11310 case 'n': mask = SP_NOMOVE; break;
11311 case 'r': mask = SP_REPEAT; break;
11312 case 'm': mask = SP_RETCOUNT; break;
11313 }
11314 if (mask == 0)
11315 {
11316 EMSG2(_(e_invarg2), flags);
11317 dir = 0;
11318 }
11319 else
11320 *flagsp |= mask;
11321 }
11322 if (dir == 0)
11323 break;
11324 ++flags;
11325 }
11326 }
11327 return dir;
11328}
11329
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330/*
11331 * "search()" function
11332 */
11333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T *argvars;
11336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
11338 char_u *pat;
11339 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011340 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 int save_p_ws = p_ws;
11342 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011343 int flags = 0;
11344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011348 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11349 if (dir == 0)
11350 goto theend;
11351 if ((flags & ~SP_NOMOVE) != 0)
11352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011354 goto theend;
11355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011357 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11359 SEARCH_KEEP, RE_SEARCH) != FAIL)
11360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 curwin->w_cursor = pos;
11363 /* "/$" will put the cursor after the end of the line, may need to
11364 * correct that here */
11365 check_cursor();
11366 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011367
11368 /* If 'n' flag is used: restore cursor position. */
11369 if (flags & SP_NOMOVE)
11370 curwin->w_cursor = save_cursor;
11371theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 p_ws = save_p_ws;
11373}
11374
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375/*
11376 * "searchpair()" function
11377 */
11378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382{
11383 char_u *spat, *mpat, *epat;
11384 char_u *skip;
11385 char_u *pat, *pat2, *pat3;
11386 pos_T pos;
11387 pos_T firstpos;
11388 pos_T save_cursor;
11389 pos_T save_pos;
11390 int save_p_ws = p_ws;
11391 char_u *save_cpo;
11392 int dir;
11393 int flags = 0;
11394 char_u nbuf1[NUMBUFLEN];
11395 char_u nbuf2[NUMBUFLEN];
11396 char_u nbuf3[NUMBUFLEN];
11397 int n;
11398 int r;
11399 int nest = 1;
11400 int err;
11401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403
11404 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11405 save_cpo = p_cpo;
11406 p_cpo = (char_u *)"";
11407
11408 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 spat = get_tv_string(&argvars[0]);
11410 mpat = get_tv_string_buf(&argvars[1], nbuf1);
11411 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
11413 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11414 * start/middle/end (pat3, for the top pair). */
11415 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
11416 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
11417 if (pat2 == NULL || pat3 == NULL)
11418 goto theend;
11419 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
11420 if (*mpat == NUL)
11421 STRCPY(pat3, pat2);
11422 else
11423 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11424 spat, epat, mpat);
11425
11426 /* Handle the optional fourth argument: flags */
11427 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011428 if (dir == 0)
11429 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
11431 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011432 if (argvars[3].v_type == VAR_UNKNOWN
11433 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 skip = (char_u *)"";
11435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
11438 save_cursor = curwin->w_cursor;
11439 pos = curwin->w_cursor;
11440 firstpos.lnum = 0;
11441 pat = pat3;
11442 for (;;)
11443 {
11444 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11445 SEARCH_KEEP, RE_SEARCH);
11446 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11447 /* didn't find it or found the first match again: FAIL */
11448 break;
11449
11450 if (firstpos.lnum == 0)
11451 firstpos = pos;
11452
11453 /* If the skip pattern matches, ignore this match. */
11454 if (*skip != NUL)
11455 {
11456 save_pos = curwin->w_cursor;
11457 curwin->w_cursor = pos;
11458 r = eval_to_bool(skip, &err, NULL, FALSE);
11459 curwin->w_cursor = save_pos;
11460 if (err)
11461 {
11462 /* Evaluating {skip} caused an error, break here. */
11463 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 break;
11466 }
11467 if (r)
11468 continue;
11469 }
11470
11471 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11472 {
11473 /* Found end when searching backwards or start when searching
11474 * forward: nested pair. */
11475 ++nest;
11476 pat = pat2; /* nested, don't search for middle */
11477 }
11478 else
11479 {
11480 /* Found end when searching forward or start when searching
11481 * backward: end of (nested) pair; or found middle in outer pair. */
11482 if (--nest == 1)
11483 pat = pat3; /* outer level, search for middle */
11484 }
11485
11486 if (nest == 0)
11487 {
11488 /* Found the match: return matchcount or line number. */
11489 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 curwin->w_cursor = pos;
11494 if (!(flags & SP_REPEAT))
11495 break;
11496 nest = 1; /* search for next unmatched */
11497 }
11498 }
11499
11500 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 curwin->w_cursor = save_cursor;
11503
11504theend:
11505 vim_free(pat2);
11506 vim_free(pat3);
11507 p_ws = save_p_ws;
11508 p_cpo = save_cpo;
11509}
11510
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511/*ARGSUSED*/
11512 static void
11513f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 typval_T *argvars;
11515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011517#ifdef FEAT_CLIENTSERVER
11518 char_u buf[NUMBUFLEN];
11519 char_u *server = get_tv_string(&argvars[0]);
11520 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522 rettv->vval.v_number = -1;
11523 if (check_restricted() || check_secure())
11524 return;
11525# ifdef FEAT_X11
11526 if (check_connection() == FAIL)
11527 return;
11528# endif
11529
11530 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532 EMSG(_("E258: Unable to send to client"));
11533 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 rettv->vval.v_number = 0;
11536#else
11537 rettv->vval.v_number = -1;
11538#endif
11539}
11540
11541/*ARGSUSED*/
11542 static void
11543f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 typval_T *argvars;
11545 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011546{
11547 char_u *r = NULL;
11548
11549#ifdef FEAT_CLIENTSERVER
11550# ifdef WIN32
11551 r = serverGetVimNames();
11552# else
11553 make_connection();
11554 if (X_DISPLAY != NULL)
11555 r = serverGetVimNames(X_DISPLAY);
11556# endif
11557#endif
11558 rettv->v_type = VAR_STRING;
11559 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560}
11561
11562/*
11563 * "setbufvar()" function
11564 */
11565/*ARGSUSED*/
11566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *argvars;
11569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570{
11571 buf_T *buf;
11572#ifdef FEAT_AUTOCMD
11573 aco_save_T aco;
11574#else
11575 buf_T *save_curbuf;
11576#endif
11577 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011578 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 char_u nbuf[NUMBUFLEN];
11580
11581 if (check_restricted() || check_secure())
11582 return;
11583 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 buf = get_buf_tv(&argvars[0]);
11585 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 varp = &argvars[2];
11587
11588 if (buf != NULL && varname != NULL && varp != NULL)
11589 {
11590 /* set curbuf to be our buf, temporarily */
11591#ifdef FEAT_AUTOCMD
11592 aucmd_prepbuf(&aco, buf);
11593#else
11594 save_curbuf = curbuf;
11595 curbuf = buf;
11596#endif
11597
11598 if (*varname == '&')
11599 {
11600 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601 set_option_value(varname, get_tv_number(varp),
11602 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 }
11604 else
11605 {
11606 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11607 if (bufvarname != NULL)
11608 {
11609 STRCPY(bufvarname, "b:");
11610 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011611 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 vim_free(bufvarname);
11613 }
11614 }
11615
11616 /* reset notion of buffer */
11617#ifdef FEAT_AUTOCMD
11618 aucmd_restbuf(&aco);
11619#else
11620 curbuf = save_curbuf;
11621#endif
11622 }
11623 --emsg_off;
11624}
11625
11626/*
11627 * "setcmdpos()" function
11628 */
11629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011631 typval_T *argvars;
11632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_number = set_cmdline_pos(
11635 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636}
11637
11638/*
11639 * "setline()" function
11640 */
11641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011643 typval_T *argvars;
11644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
11646 linenr_T lnum;
11647 char_u *line;
11648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 lnum = get_tv_lnum(argvars);
11650 line = get_tv_string(&argvars[1]);
11651 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652
11653 if (lnum >= 1
11654 && lnum <= curbuf->b_ml.ml_line_count
11655 && u_savesub(lnum) == OK
11656 && ml_replace(lnum, line, TRUE) == OK)
11657 {
11658 changed_bytes(lnum, 0);
11659 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 }
11662}
11663
11664/*
11665 * "setreg()" function
11666 */
11667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 typval_T *argvars;
11670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671{
11672 int regname;
11673 char_u *strregname;
11674 char_u *stropt;
11675 int append;
11676 char_u yank_type;
11677 long block_len;
11678
11679 block_len = -1;
11680 yank_type = MAUTO;
11681 append = FALSE;
11682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 strregname = get_tv_string(argvars);
11684 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685
11686 regname = (strregname == NULL ? '"' : *strregname);
11687 if (regname == 0 || regname == '@')
11688 regname = '"';
11689 else if (regname == '=')
11690 return;
11691
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011692 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 switch (*stropt)
11696 {
11697 case 'a': case 'A': /* append */
11698 append = TRUE;
11699 break;
11700 case 'v': case 'c': /* character-wise selection */
11701 yank_type = MCHAR;
11702 break;
11703 case 'V': case 'l': /* line-wise selection */
11704 yank_type = MLINE;
11705 break;
11706#ifdef FEAT_VISUAL
11707 case 'b': case Ctrl_V: /* block-wise selection */
11708 yank_type = MBLOCK;
11709 if (VIM_ISDIGIT(stropt[1]))
11710 {
11711 ++stropt;
11712 block_len = getdigits(&stropt) - 1;
11713 --stropt;
11714 }
11715 break;
11716#endif
11717 }
11718 }
11719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723}
11724
11725
11726/*
11727 * "setwinvar(expr)" function
11728 */
11729/*ARGSUSED*/
11730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *argvars;
11733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734{
11735 win_T *win;
11736#ifdef FEAT_WINDOWS
11737 win_T *save_curwin;
11738#endif
11739 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 char_u nbuf[NUMBUFLEN];
11742
11743 if (check_restricted() || check_secure())
11744 return;
11745 ++emsg_off;
11746 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 varp = &argvars[2];
11749
11750 if (win != NULL && varname != NULL && varp != NULL)
11751 {
11752#ifdef FEAT_WINDOWS
11753 /* set curwin to be our win, temporarily */
11754 save_curwin = curwin;
11755 curwin = win;
11756 curbuf = curwin->w_buffer;
11757#endif
11758
11759 if (*varname == '&')
11760 {
11761 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 set_option_value(varname, get_tv_number(varp),
11763 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 }
11765 else
11766 {
11767 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11768 if (winvarname != NULL)
11769 {
11770 STRCPY(winvarname, "w:");
11771 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011772 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 vim_free(winvarname);
11774 }
11775 }
11776
11777#ifdef FEAT_WINDOWS
11778 /* Restore current window, if it's still valid (autocomands can make
11779 * it invalid). */
11780 if (win_valid(save_curwin))
11781 {
11782 curwin = save_curwin;
11783 curbuf = curwin->w_buffer;
11784 }
11785#endif
11786 }
11787 --emsg_off;
11788}
11789
11790/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011791 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 */
11793 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011794f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011798 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799
Bram Moolenaar0d660222005-01-07 21:51:51 +000011800 p = get_tv_string(&argvars[0]);
11801 rettv->vval.v_string = vim_strsave(p);
11802 simplify_filename(rettv->vval.v_string); /* simplify in place */
11803 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
Bram Moolenaar0d660222005-01-07 21:51:51 +000011806static int
11807#ifdef __BORLANDC__
11808 _RTLENTRYF
11809#endif
11810 item_compare __ARGS((const void *s1, const void *s2));
11811static int
11812#ifdef __BORLANDC__
11813 _RTLENTRYF
11814#endif
11815 item_compare2 __ARGS((const void *s1, const void *s2));
11816
11817static int item_compare_ic;
11818static char_u *item_compare_func;
11819#define ITEM_COMPARE_FAIL 999
11820
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011822 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011824 static int
11825#ifdef __BORLANDC__
11826_RTLENTRYF
11827#endif
11828item_compare(s1, s2)
11829 const void *s1;
11830 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011832 char_u *p1, *p2;
11833 char_u *tofree1, *tofree2;
11834 int res;
11835 char_u numbuf1[NUMBUFLEN];
11836 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
11839 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011840 if (item_compare_ic)
11841 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011843 res = STRCMP(p1, p2);
11844 vim_free(tofree1);
11845 vim_free(tofree2);
11846 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847}
11848
11849 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850#ifdef __BORLANDC__
11851_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011853item_compare2(s1, s2)
11854 const void *s1;
11855 const void *s2;
11856{
11857 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T rettv;
11859 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011862 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
11863 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
11865 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011866
11867 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11868 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011870 clear_tv(&argv[0]);
11871 clear_tv(&argv[1]);
11872
11873 if (res == FAIL)
11874 res = ITEM_COMPARE_FAIL;
11875 else
11876 res = get_tv_number(&rettv);
11877 clear_tv(&rettv);
11878 return res;
11879}
11880
11881/*
11882 * "sort({list})" function
11883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 typval_T *argvars;
11887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888{
Bram Moolenaar33570922005-01-25 22:26:29 +000011889 list_T *l;
11890 listitem_T *li;
11891 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 long len;
11893 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895 rettv->vval.v_number = 0;
11896 if (argvars[0].v_type != VAR_LIST)
11897 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 else
11899 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011901 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902 return;
11903 rettv->vval.v_list = l;
11904 rettv->v_type = VAR_LIST;
11905 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906
Bram Moolenaar0d660222005-01-07 21:51:51 +000011907 len = list_len(l);
11908 if (len <= 1)
11909 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910
Bram Moolenaar0d660222005-01-07 21:51:51 +000011911 item_compare_ic = FALSE;
11912 item_compare_func = NULL;
11913 if (argvars[1].v_type != VAR_UNKNOWN)
11914 {
11915 if (argvars[1].v_type == VAR_FUNC)
11916 item_compare_func = argvars[0].vval.v_string;
11917 else
11918 {
11919 i = get_tv_number(&argvars[1]);
11920 if (i == 1)
11921 item_compare_ic = TRUE;
11922 else
11923 item_compare_func = get_tv_string(&argvars[1]);
11924 }
11925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926
Bram Moolenaar0d660222005-01-07 21:51:51 +000011927 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011929 if (ptrs == NULL)
11930 return;
11931 i = 0;
11932 for (li = l->lv_first; li != NULL; li = li->li_next)
11933 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934
Bram Moolenaar0d660222005-01-07 21:51:51 +000011935 /* test the compare function */
11936 if (item_compare_func != NULL
11937 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11938 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011939 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011941 {
11942 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000011943 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000011944 item_compare_func == NULL ? item_compare : item_compare2);
11945
11946 /* Clear the List and append the items in the sorted order. */
11947 l->lv_first = l->lv_last = NULL;
11948 for (i = 0; i < len; ++i)
11949 list_append(l, ptrs[i]);
11950 }
11951
11952 vim_free(ptrs);
11953 }
11954}
11955
11956 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011957f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011958 typval_T *argvars;
11959 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011960{
11961 char_u *str;
11962 char_u *end;
11963 char_u *pat;
11964 regmatch_T regmatch;
11965 char_u patbuf[NUMBUFLEN];
11966 char_u *save_cpo;
11967 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 listitem_T *ni;
11969 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011970 colnr_T col = 0;
11971
11972 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11973 save_cpo = p_cpo;
11974 p_cpo = (char_u *)"";
11975
11976 str = get_tv_string(&argvars[0]);
11977 if (argvars[1].v_type == VAR_UNKNOWN)
11978 pat = (char_u *)"[\\x01- ]\\+";
11979 else
11980 pat = get_tv_string_buf(&argvars[1], patbuf);
11981
11982 l = list_alloc();
11983 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985 rettv->v_type = VAR_LIST;
11986 rettv->vval.v_list = l;
11987 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11990 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011992 regmatch.rm_ic = FALSE;
11993 while (*str != NUL)
11994 {
11995 match = vim_regexec_nl(&regmatch, str, col);
11996 if (match)
11997 end = regmatch.startp[0];
11998 else
11999 end = str + STRLEN(str);
12000 if (end > str)
12001 {
12002 ni = listitem_alloc();
12003 if (ni == NULL)
12004 break;
12005 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012006 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012007 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12008 list_append(l, ni);
12009 }
12010 if (!match)
12011 break;
12012 /* Advance to just after the match. */
12013 if (regmatch.endp[0] > str)
12014 col = 0;
12015 else
12016 {
12017 /* Don't get stuck at the same match. */
12018#ifdef FEAT_MBYTE
12019 col = mb_ptr2len_check(regmatch.endp[0]);
12020#else
12021 col = 1;
12022#endif
12023 }
12024 str = regmatch.endp[0];
12025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026
Bram Moolenaar0d660222005-01-07 21:51:51 +000012027 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
Bram Moolenaar0d660222005-01-07 21:51:51 +000012030 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031}
12032
12033#ifdef HAVE_STRFTIME
12034/*
12035 * "strftime({format}[, {time}])" function
12036 */
12037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012039 typval_T *argvars;
12040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041{
12042 char_u result_buf[256];
12043 struct tm *curtime;
12044 time_t seconds;
12045 char_u *p;
12046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012050 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 seconds = time(NULL);
12052 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 curtime = localtime(&seconds);
12055 /* MSVC returns NULL for an invalid value of seconds. */
12056 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 else
12059 {
12060# ifdef FEAT_MBYTE
12061 vimconv_T conv;
12062 char_u *enc;
12063
12064 conv.vc_type = CONV_NONE;
12065 enc = enc_locale();
12066 convert_setup(&conv, p_enc, enc);
12067 if (conv.vc_type != CONV_NONE)
12068 p = string_convert(&conv, p, NULL);
12069# endif
12070 if (p != NULL)
12071 (void)strftime((char *)result_buf, sizeof(result_buf),
12072 (char *)p, curtime);
12073 else
12074 result_buf[0] = NUL;
12075
12076# ifdef FEAT_MBYTE
12077 if (conv.vc_type != CONV_NONE)
12078 vim_free(p);
12079 convert_setup(&conv, enc, p_enc);
12080 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 else
12083# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085
12086# ifdef FEAT_MBYTE
12087 /* Release conversion descriptors */
12088 convert_setup(&conv, NULL, NULL);
12089 vim_free(enc);
12090# endif
12091 }
12092}
12093#endif
12094
12095/*
12096 * "stridx()" function
12097 */
12098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012100 typval_T *argvars;
12101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102{
12103 char_u buf[NUMBUFLEN];
12104 char_u *needle;
12105 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012110 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113
Bram Moolenaar33570922005-01-25 22:26:29 +000012114 if (argvars[2].v_type != VAR_UNKNOWN)
12115 {
12116 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012117 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012118 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012119 if (start_idx >= 0)
12120 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012121 }
12122
12123 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12124 if (pos != NULL)
12125 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126}
12127
12128/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012129 * "string()" function
12130 */
12131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012133 typval_T *argvars;
12134 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012135{
12136 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012137 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012140 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012141 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143}
12144
12145/*
12146 * "strlen()" function
12147 */
12148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 typval_T *argvars;
12151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_number = (varnumber_T)(STRLEN(
12154 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155}
12156
12157/*
12158 * "strpart()" function
12159 */
12160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012162 typval_T *argvars;
12163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164{
12165 char_u *p;
12166 int n;
12167 int len;
12168 int slen;
12169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 slen = (int)STRLEN(p);
12172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012174 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 else
12177 len = slen - n; /* default len: all bytes that are available. */
12178
12179 /*
12180 * Only return the overlap between the specified part and the actual
12181 * string.
12182 */
12183 if (n < 0)
12184 {
12185 len += n;
12186 n = 0;
12187 }
12188 else if (n > slen)
12189 n = slen;
12190 if (len < 0)
12191 len = 0;
12192 else if (n + len > slen)
12193 len = slen - n;
12194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195 rettv->v_type = VAR_STRING;
12196 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197}
12198
12199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200 * "strridx()" function
12201 */
12202 static void
12203f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 typval_T *argvars;
12205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206{
12207 char_u buf[NUMBUFLEN];
12208 char_u *needle;
12209 char_u *haystack;
12210 char_u *rest;
12211 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012212 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213
12214 needle = get_tv_string(&argvars[1]);
12215 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012216 haystack_len = STRLEN(haystack);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217 if (*needle == NUL)
12218 /* Empty string matches past the end. */
Bram Moolenaar532c7802005-01-27 14:44:31 +000012219 lastmatch = haystack + haystack_len;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012220 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012221 {
12222 if (argvars[2].v_type != VAR_UNKNOWN)
12223 {
12224 /* Third argument: upper limit for index */
12225 end_idx = get_tv_number(&argvars[2]);
12226 if (end_idx < 0)
12227 {
12228 /* can never find a match */
12229 rettv->vval.v_number = -1;
12230 return;
12231 }
12232 }
12233 else
12234 end_idx = haystack_len;
12235
Bram Moolenaar0d660222005-01-07 21:51:51 +000012236 for (rest = haystack; *rest != '\0'; ++rest)
12237 {
12238 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012239 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012240 break;
12241 lastmatch = rest;
12242 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012243 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244
12245 if (lastmatch == NULL)
12246 rettv->vval.v_number = -1;
12247 else
12248 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12249}
12250
12251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 * "strtrans()" function
12253 */
12254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012256 typval_T *argvars;
12257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 rettv->v_type = VAR_STRING;
12260 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261}
12262
12263/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264 * "submatch()" function
12265 */
12266 static void
12267f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 typval_T *argvars;
12269 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012270{
12271 rettv->v_type = VAR_STRING;
12272 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12273}
12274
12275/*
12276 * "substitute()" function
12277 */
12278 static void
12279f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012280 typval_T *argvars;
12281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282{
12283 char_u patbuf[NUMBUFLEN];
12284 char_u subbuf[NUMBUFLEN];
12285 char_u flagsbuf[NUMBUFLEN];
12286
12287 rettv->v_type = VAR_STRING;
12288 rettv->vval.v_string = do_string_sub(
12289 get_tv_string(&argvars[0]),
12290 get_tv_string_buf(&argvars[1], patbuf),
12291 get_tv_string_buf(&argvars[2], subbuf),
12292 get_tv_string_buf(&argvars[3], flagsbuf));
12293}
12294
12295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 * "synID(line, col, trans)" function
12297 */
12298/*ARGSUSED*/
12299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012301 typval_T *argvars;
12302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303{
12304 int id = 0;
12305#ifdef FEAT_SYN_HL
12306 long line;
12307 long col;
12308 int trans;
12309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 line = get_tv_lnum(argvars);
12311 col = get_tv_number(&argvars[1]) - 1;
12312 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
12314 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12315 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12316 id = syn_get_id(line, col, trans);
12317#endif
12318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320}
12321
12322/*
12323 * "synIDattr(id, what [, mode])" function
12324 */
12325/*ARGSUSED*/
12326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012328 typval_T *argvars;
12329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 char_u *p = NULL;
12332#ifdef FEAT_SYN_HL
12333 int id;
12334 char_u *what;
12335 char_u *mode;
12336 char_u modebuf[NUMBUFLEN];
12337 int modec;
12338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 id = get_tv_number(&argvars[0]);
12340 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012341 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 modec = TOLOWER_ASC(mode[0]);
12345 if (modec != 't' && modec != 'c'
12346#ifdef FEAT_GUI
12347 && modec != 'g'
12348#endif
12349 )
12350 modec = 0; /* replace invalid with current */
12351 }
12352 else
12353 {
12354#ifdef FEAT_GUI
12355 if (gui.in_use)
12356 modec = 'g';
12357 else
12358#endif
12359 if (t_colors > 1)
12360 modec = 'c';
12361 else
12362 modec = 't';
12363 }
12364
12365
12366 switch (TOLOWER_ASC(what[0]))
12367 {
12368 case 'b':
12369 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12370 p = highlight_color(id, what, modec);
12371 else /* bold */
12372 p = highlight_has_attr(id, HL_BOLD, modec);
12373 break;
12374
12375 case 'f': /* fg[#] */
12376 p = highlight_color(id, what, modec);
12377 break;
12378
12379 case 'i':
12380 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12381 p = highlight_has_attr(id, HL_INVERSE, modec);
12382 else /* italic */
12383 p = highlight_has_attr(id, HL_ITALIC, modec);
12384 break;
12385
12386 case 'n': /* name */
12387 p = get_highlight_name(NULL, id - 1);
12388 break;
12389
12390 case 'r': /* reverse */
12391 p = highlight_has_attr(id, HL_INVERSE, modec);
12392 break;
12393
12394 case 's': /* standout */
12395 p = highlight_has_attr(id, HL_STANDOUT, modec);
12396 break;
12397
12398 case 'u': /* underline */
12399 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12400 break;
12401 }
12402
12403 if (p != NULL)
12404 p = vim_strsave(p);
12405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 rettv->v_type = VAR_STRING;
12407 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408}
12409
12410/*
12411 * "synIDtrans(id)" function
12412 */
12413/*ARGSUSED*/
12414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012416 typval_T *argvars;
12417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418{
12419 int id;
12420
12421#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423
12424 if (id > 0)
12425 id = syn_get_final_id(id);
12426 else
12427#endif
12428 id = 0;
12429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431}
12432
12433/*
12434 * "system()" function
12435 */
12436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012438 typval_T *argvars;
12439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012441 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012443 char_u *infile = NULL;
12444 char_u buf[NUMBUFLEN];
12445 int err = FALSE;
12446 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012448 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012449 {
12450 /*
12451 * Write the string to a temp file, to be used for input of the shell
12452 * command.
12453 */
12454 if ((infile = vim_tempname('i')) == NULL)
12455 {
12456 EMSG(_(e_notmp));
12457 return;
12458 }
12459
12460 fd = mch_fopen((char *)infile, WRITEBIN);
12461 if (fd == NULL)
12462 {
12463 EMSG2(_(e_notopen), infile);
12464 goto done;
12465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012467 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12468 err = TRUE;
12469 if (fclose(fd) != 0)
12470 err = TRUE;
12471 if (err)
12472 {
12473 EMSG(_("E677: Error writing temp file"));
12474 goto done;
12475 }
12476 }
12477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012479
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480#ifdef USE_CR
12481 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012482 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 {
12484 char_u *s;
12485
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012486 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 {
12488 if (*s == CAR)
12489 *s = NL;
12490 }
12491 }
12492#else
12493# ifdef USE_CRNL
12494 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012495 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 {
12497 char_u *s, *d;
12498
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012499 d = res;
12500 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 {
12502 if (s[0] == CAR && s[1] == NL)
12503 ++s;
12504 *d++ = *s;
12505 }
12506 *d = NUL;
12507 }
12508# endif
12509#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012510
12511done:
12512 if (infile != NULL)
12513 {
12514 mch_remove(infile);
12515 vim_free(infile);
12516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->v_type = VAR_STRING;
12518 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519}
12520
12521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 * "tempname()" function
12523 */
12524/*ARGSUSED*/
12525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 typval_T *argvars;
12528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529{
12530 static int x = 'A';
12531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 rettv->v_type = VAR_STRING;
12533 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534
12535 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12536 * names. Skip 'I' and 'O', they are used for shell redirection. */
12537 do
12538 {
12539 if (x == 'Z')
12540 x = '0';
12541 else if (x == '9')
12542 x = 'A';
12543 else
12544 {
12545#ifdef EBCDIC
12546 if (x == 'I')
12547 x = 'J';
12548 else if (x == 'R')
12549 x = 'S';
12550 else
12551#endif
12552 ++x;
12553 }
12554 } while (x == 'I' || x == 'O');
12555}
12556
12557/*
12558 * "tolower(string)" function
12559 */
12560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012562 typval_T *argvars;
12563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564{
12565 char_u *p;
12566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 p = vim_strsave(get_tv_string(&argvars[0]));
12568 rettv->v_type = VAR_STRING;
12569 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570
12571 if (p != NULL)
12572 while (*p != NUL)
12573 {
12574#ifdef FEAT_MBYTE
12575 int l;
12576
12577 if (enc_utf8)
12578 {
12579 int c, lc;
12580
12581 c = utf_ptr2char(p);
12582 lc = utf_tolower(c);
12583 l = utf_ptr2len_check(p);
12584 /* TODO: reallocate string when byte count changes. */
12585 if (utf_char2len(lc) == l)
12586 utf_char2bytes(lc, p);
12587 p += l;
12588 }
12589 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12590 p += l; /* skip multi-byte character */
12591 else
12592#endif
12593 {
12594 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12595 ++p;
12596 }
12597 }
12598}
12599
12600/*
12601 * "toupper(string)" function
12602 */
12603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 typval_T *argvars;
12606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607{
12608 char_u *p;
12609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 p = vim_strsave(get_tv_string(&argvars[0]));
12611 rettv->v_type = VAR_STRING;
12612 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613
12614 if (p != NULL)
12615 while (*p != NUL)
12616 {
12617#ifdef FEAT_MBYTE
12618 int l;
12619
12620 if (enc_utf8)
12621 {
12622 int c, uc;
12623
12624 c = utf_ptr2char(p);
12625 uc = utf_toupper(c);
12626 l = utf_ptr2len_check(p);
12627 /* TODO: reallocate string when byte count changes. */
12628 if (utf_char2len(uc) == l)
12629 utf_char2bytes(uc, p);
12630 p += l;
12631 }
12632 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12633 p += l; /* skip multi-byte character */
12634 else
12635#endif
12636 {
12637 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12638 p++;
12639 }
12640 }
12641}
12642
12643/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012644 * "tr(string, fromstr, tostr)" function
12645 */
12646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 typval_T *argvars;
12649 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012650{
12651 char_u *instr;
12652 char_u *fromstr;
12653 char_u *tostr;
12654 char_u *p;
12655#ifdef FEAT_MBYTE
12656 int inlen;
12657 int fromlen;
12658 int tolen;
12659 int idx;
12660 char_u *cpstr;
12661 int cplen;
12662 int first = TRUE;
12663#endif
12664 char_u buf[NUMBUFLEN];
12665 char_u buf2[NUMBUFLEN];
12666 garray_T ga;
12667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 instr = get_tv_string(&argvars[0]);
12669 fromstr = get_tv_string_buf(&argvars[1], buf);
12670 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012671
12672 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->v_type = VAR_STRING;
12674 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012675 ga_init2(&ga, (int)sizeof(char), 80);
12676
12677#ifdef FEAT_MBYTE
12678 if (!has_mbyte)
12679#endif
12680 /* not multi-byte: fromstr and tostr must be the same length */
12681 if (STRLEN(fromstr) != STRLEN(tostr))
12682 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012683#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012684error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012685#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012686 EMSG2(_(e_invarg2), fromstr);
12687 ga_clear(&ga);
12688 return;
12689 }
12690
12691 /* fromstr and tostr have to contain the same number of chars */
12692 while (*instr != NUL)
12693 {
12694#ifdef FEAT_MBYTE
12695 if (has_mbyte)
12696 {
12697 inlen = mb_ptr2len_check(instr);
12698 cpstr = instr;
12699 cplen = inlen;
12700 idx = 0;
12701 for (p = fromstr; *p != NUL; p += fromlen)
12702 {
12703 fromlen = mb_ptr2len_check(p);
12704 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12705 {
12706 for (p = tostr; *p != NUL; p += tolen)
12707 {
12708 tolen = mb_ptr2len_check(p);
12709 if (idx-- == 0)
12710 {
12711 cplen = tolen;
12712 cpstr = p;
12713 break;
12714 }
12715 }
12716 if (*p == NUL) /* tostr is shorter than fromstr */
12717 goto error;
12718 break;
12719 }
12720 ++idx;
12721 }
12722
12723 if (first && cpstr == instr)
12724 {
12725 /* Check that fromstr and tostr have the same number of
12726 * (multi-byte) characters. Done only once when a character
12727 * of instr doesn't appear in fromstr. */
12728 first = FALSE;
12729 for (p = tostr; *p != NUL; p += tolen)
12730 {
12731 tolen = mb_ptr2len_check(p);
12732 --idx;
12733 }
12734 if (idx != 0)
12735 goto error;
12736 }
12737
12738 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012739 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012740 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012741
12742 instr += inlen;
12743 }
12744 else
12745#endif
12746 {
12747 /* When not using multi-byte chars we can do it faster. */
12748 p = vim_strchr(fromstr, *instr);
12749 if (p != NULL)
12750 ga_append(&ga, tostr[p - fromstr]);
12751 else
12752 ga_append(&ga, *instr);
12753 ++instr;
12754 }
12755 }
12756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012758}
12759
12760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 * "type(expr)" function
12762 */
12763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012765 typval_T *argvars;
12766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012768 int n;
12769
12770 switch (argvars[0].v_type)
12771 {
12772 case VAR_NUMBER: n = 0; break;
12773 case VAR_STRING: n = 1; break;
12774 case VAR_FUNC: n = 2; break;
12775 case VAR_LIST: n = 3; break;
12776 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12777 }
12778 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779}
12780
12781/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012782 * "values(dict)" function
12783 */
12784 static void
12785f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012788{
12789 dict_list(argvars, rettv, 1);
12790}
12791
12792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 * "virtcol(string)" function
12794 */
12795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 typval_T *argvars;
12798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799{
12800 colnr_T vcol = 0;
12801 pos_T *fp;
12802
12803 fp = var2fpos(&argvars[0], FALSE);
12804 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12805 {
12806 getvvcol(curwin, fp, NULL, NULL, &vcol);
12807 ++vcol;
12808 }
12809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811}
12812
12813/*
12814 * "visualmode()" function
12815 */
12816/*ARGSUSED*/
12817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *argvars;
12820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821{
12822#ifdef FEAT_VISUAL
12823 char_u str[2];
12824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 str[0] = curbuf->b_visual_mode_eval;
12827 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829
12830 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012831 if ((argvars[0].v_type == VAR_NUMBER
12832 && argvars[0].vval.v_number != 0)
12833 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 curbuf->b_visual_mode_eval = NUL;
12836#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838#endif
12839}
12840
12841/*
12842 * "winbufnr(nr)" function
12843 */
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 win_T *wp;
12850
12851 wp = find_win_by_nr(&argvars[0]);
12852 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856}
12857
12858/*
12859 * "wincol()" function
12860 */
12861/*ARGSUSED*/
12862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *argvars;
12865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
12867 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869}
12870
12871/*
12872 * "winheight(nr)" function
12873 */
12874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878{
12879 win_T *wp;
12880
12881 wp = find_win_by_nr(&argvars[0]);
12882 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
12888/*
12889 * "winline()" function
12890 */
12891/*ARGSUSED*/
12892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 typval_T *argvars;
12895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896{
12897 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899}
12900
12901/*
12902 * "winnr()" function
12903 */
12904/* ARGSUSED */
12905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012907 typval_T *argvars;
12908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909{
12910 int nr = 1;
12911#ifdef FEAT_WINDOWS
12912 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012913 win_T *twin = curwin;
12914 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012916 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012919 if (STRCMP(arg, "$") == 0)
12920 twin = lastwin;
12921 else if (STRCMP(arg, "#") == 0)
12922 {
12923 twin = prevwin;
12924 if (prevwin == NULL)
12925 nr = 0;
12926 }
12927 else
12928 {
12929 EMSG2(_(e_invexpr2), arg);
12930 nr = 0;
12931 }
12932 }
12933
12934 if (nr > 0)
12935 for (wp = firstwin; wp != twin; wp = wp->w_next)
12936 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939}
12940
12941/*
12942 * "winrestcmd()" function
12943 */
12944/* ARGSUSED */
12945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012947 typval_T *argvars;
12948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949{
12950#ifdef FEAT_WINDOWS
12951 win_T *wp;
12952 int winnr = 1;
12953 garray_T ga;
12954 char_u buf[50];
12955
12956 ga_init2(&ga, (int)sizeof(char), 70);
12957 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12958 {
12959 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12960 ga_concat(&ga, buf);
12961# ifdef FEAT_VERTSPLIT
12962 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12963 ga_concat(&ga, buf);
12964# endif
12965 ++winnr;
12966 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012967 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012973 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974}
12975
12976/*
12977 * "winwidth(nr)" function
12978 */
12979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012981 typval_T *argvars;
12982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983{
12984 win_T *wp;
12985
12986 wp = find_win_by_nr(&argvars[0]);
12987 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 else
12990#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994#endif
12995}
12996
12997 static win_T *
12998find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012999 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000{
13001#ifdef FEAT_WINDOWS
13002 win_T *wp;
13003#endif
13004 int nr;
13005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013006 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007
13008#ifdef FEAT_WINDOWS
13009 if (nr == 0)
13010 return curwin;
13011
13012 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13013 if (--nr <= 0)
13014 break;
13015 return wp;
13016#else
13017 if (nr == 0 || nr == 1)
13018 return curwin;
13019 return NULL;
13020#endif
13021}
13022
13023/*
13024 * Translate a String variable into a position.
13025 */
13026 static pos_T *
13027var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013028 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 int lnum; /* TRUE when $ is last line */
13030{
13031 char_u *name;
13032 static pos_T pos;
13033 pos_T *pp;
13034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 if (name[0] == '.') /* cursor */
13037 return &curwin->w_cursor;
13038 if (name[0] == '\'') /* mark */
13039 {
13040 pp = getmark(name[1], FALSE);
13041 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13042 return NULL;
13043 return pp;
13044 }
13045 if (name[0] == '$') /* last column or line */
13046 {
13047 if (lnum)
13048 {
13049 pos.lnum = curbuf->b_ml.ml_line_count;
13050 pos.col = 0;
13051 }
13052 else
13053 {
13054 pos.lnum = curwin->w_cursor.lnum;
13055 pos.col = (colnr_T)STRLEN(ml_get_curline());
13056 }
13057 return &pos;
13058 }
13059 return NULL;
13060}
13061
13062/*
13063 * Get the length of an environment variable name.
13064 * Advance "arg" to the first character after the name.
13065 * Return 0 for error.
13066 */
13067 static int
13068get_env_len(arg)
13069 char_u **arg;
13070{
13071 char_u *p;
13072 int len;
13073
13074 for (p = *arg; vim_isIDc(*p); ++p)
13075 ;
13076 if (p == *arg) /* no name found */
13077 return 0;
13078
13079 len = (int)(p - *arg);
13080 *arg = p;
13081 return len;
13082}
13083
13084/*
13085 * Get the length of the name of a function or internal variable.
13086 * "arg" is advanced to the first non-white character after the name.
13087 * Return 0 if something is wrong.
13088 */
13089 static int
13090get_id_len(arg)
13091 char_u **arg;
13092{
13093 char_u *p;
13094 int len;
13095
13096 /* Find the end of the name. */
13097 for (p = *arg; eval_isnamec(*p); ++p)
13098 ;
13099 if (p == *arg) /* no name found */
13100 return 0;
13101
13102 len = (int)(p - *arg);
13103 *arg = skipwhite(p);
13104
13105 return len;
13106}
13107
13108/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013109 * Get the length of the name of a variable or function.
13110 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 * "arg" is advanced to the first non-white character after the name.
13112 * Return 0 if something is wrong.
13113 * If the name contains 'magic' {}'s, expand them and return the
13114 * expanded name in an allocated string via 'alias' - caller must free.
13115 */
13116 static int
Bram Moolenaara7043832005-01-21 11:56:39 +000013117get_name_len(arg, alias, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 char_u **arg;
13119 char_u **alias;
13120 int evaluate;
13121{
13122 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 char_u *p;
13124 char_u *expr_start;
13125 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126
13127 *alias = NULL; /* default to no alias */
13128
13129 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13130 && (*arg)[2] == (int)KE_SNR)
13131 {
13132 /* hard coded <SNR>, already translated */
13133 *arg += 3;
13134 return get_id_len(arg) + 3;
13135 }
13136 len = eval_fname_script(*arg);
13137 if (len > 0)
13138 {
13139 /* literal "<SID>", "s:" or "<SNR>" */
13140 *arg += len;
13141 }
13142
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 if (expr_start != NULL)
13148 {
13149 char_u *temp_string;
13150
13151 if (!evaluate)
13152 {
13153 len += (int)(p - *arg);
13154 *arg = skipwhite(p);
13155 return len;
13156 }
13157
13158 /*
13159 * Include any <SID> etc in the expanded string:
13160 * Thus the -len here.
13161 */
13162 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13163 if (temp_string == NULL)
13164 return 0;
13165 *alias = temp_string;
13166 *arg = skipwhite(p);
13167 return (int)STRLEN(temp_string);
13168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169
13170 len += get_id_len(arg);
13171 if (len == 0)
13172 EMSG2(_(e_invexpr2), *arg);
13173
13174 return len;
13175}
13176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177/*
13178 * Find the end of a variable or function name, taking care of magic braces.
13179 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13180 * start and end of the first magic braces item.
13181 * Return a pointer to just after the name. Equal to "arg" if there is no
13182 * valid name.
13183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 char_u *arg;
13187 char_u **expr_start;
13188 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013189 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 int mb_nest = 0;
13192 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 char_u *p;
13194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 *expr_start = NULL;
13198 *expr_end = NULL;
13199 }
13200
13201 for (p = arg; *p != NUL
13202 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013203 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013204 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 || mb_nest != 0
13206 || br_nest != 0); ++p)
13207 {
13208 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 if (*p == '[')
13211 ++br_nest;
13212 else if (*p == ']')
13213 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217 if (*p == '{')
13218 {
13219 mb_nest++;
13220 if (expr_start != NULL && *expr_start == NULL)
13221 *expr_start = p;
13222 }
13223 else if (*p == '}')
13224 {
13225 mb_nest--;
13226 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13227 *expr_end = p;
13228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 }
13231
13232 return p;
13233}
13234
13235/*
13236 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013237 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 */
13239 static int
13240eval_isnamec(c)
13241 int c;
13242{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013243 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244}
13245
13246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 * Set number v: variable to "val".
13248 */
13249 void
13250set_vim_var_nr(idx, val)
13251 int idx;
13252 long val;
13253{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013254 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255}
13256
13257/*
13258 * Get number v: variable value;
13259 */
13260 long
13261get_vim_var_nr(idx)
13262 int idx;
13263{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013264 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265}
13266
13267/*
13268 * Set v:count, v:count1 and v:prevcount.
13269 */
13270 void
13271set_vcount(count, count1)
13272 long count;
13273 long count1;
13274{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013275 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
13276 vimvars[VV_COUNT].vv_nr = count;
13277 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278}
13279
13280/*
13281 * Set string v: variable to a copy of "val".
13282 */
13283 void
13284set_vim_var_string(idx, val, len)
13285 int idx;
13286 char_u *val;
13287 int len; /* length of "val" to use or -1 (whole string) */
13288{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013289 /* Need to do this (at least) once, since we can't initialize a union.
13290 * Will always be invoked when "v:progname" is set. */
13291 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
13292
Bram Moolenaare9a41262005-01-15 22:18:47 +000013293 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013295 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013297 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013299 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300}
13301
13302/*
13303 * Set v:register if needed.
13304 */
13305 void
13306set_reg_var(c)
13307 int c;
13308{
13309 char_u regname;
13310
13311 if (c == 0 || c == ' ')
13312 regname = '"';
13313 else
13314 regname = c;
13315 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013316 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 set_vim_var_string(VV_REG, &regname, 1);
13318}
13319
13320/*
13321 * Get or set v:exception. If "oldval" == NULL, return the current value.
13322 * Otherwise, restore the value to "oldval" and return NULL.
13323 * Must always be called in pairs to save and restore v:exception! Does not
13324 * take care of memory allocations.
13325 */
13326 char_u *
13327v_exception(oldval)
13328 char_u *oldval;
13329{
13330 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013331 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332
Bram Moolenaare9a41262005-01-15 22:18:47 +000013333 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 return NULL;
13335}
13336
13337/*
13338 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
13339 * Otherwise, restore the value to "oldval" and return NULL.
13340 * Must always be called in pairs to save and restore v:throwpoint! Does not
13341 * take care of memory allocations.
13342 */
13343 char_u *
13344v_throwpoint(oldval)
13345 char_u *oldval;
13346{
13347 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013348 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349
Bram Moolenaare9a41262005-01-15 22:18:47 +000013350 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 return NULL;
13352}
13353
13354#if defined(FEAT_AUTOCMD) || defined(PROTO)
13355/*
13356 * Set v:cmdarg.
13357 * If "eap" != NULL, use "eap" to generate the value and return the old value.
13358 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
13359 * Must always be called in pairs!
13360 */
13361 char_u *
13362set_cmdarg(eap, oldarg)
13363 exarg_T *eap;
13364 char_u *oldarg;
13365{
13366 char_u *oldval;
13367 char_u *newval;
13368 unsigned len;
13369
Bram Moolenaare9a41262005-01-15 22:18:47 +000013370 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013371 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013373 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000013374 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013375 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 }
13377
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013378 if (eap->force_bin == FORCE_BIN)
13379 len = 6;
13380 else if (eap->force_bin == FORCE_NOBIN)
13381 len = 8;
13382 else
13383 len = 0;
13384 if (eap->force_ff != 0)
13385 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
13386# ifdef FEAT_MBYTE
13387 if (eap->force_enc != 0)
13388 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
13389# endif
13390
13391 newval = alloc(len + 1);
13392 if (newval == NULL)
13393 return NULL;
13394
13395 if (eap->force_bin == FORCE_BIN)
13396 sprintf((char *)newval, " ++bin");
13397 else if (eap->force_bin == FORCE_NOBIN)
13398 sprintf((char *)newval, " ++nobin");
13399 else
13400 *newval = NUL;
13401 if (eap->force_ff != 0)
13402 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
13403 eap->cmd + eap->force_ff);
13404# ifdef FEAT_MBYTE
13405 if (eap->force_enc != 0)
13406 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
13407 eap->cmd + eap->force_enc);
13408# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000013409 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013410 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411}
13412#endif
13413
13414/*
13415 * Get the value of internal variable "name".
13416 * Return OK or FAIL.
13417 */
13418 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 char_u *name;
13421 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000013422 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423{
13424 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000013425 typval_T *tv = NULL;
13426 typval_T atv;
13427 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429
13430 /* truncate the name, so that we can use strcmp() */
13431 cc = name[len];
13432 name[len] = NUL;
13433
13434 /*
13435 * Check for "b:changedtick".
13436 */
13437 if (STRCMP(name, "b:changedtick") == 0)
13438 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013439 atv.v_type = VAR_NUMBER;
13440 atv.vval.v_number = curbuf->b_changedtick;
13441 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 }
13443
13444 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 * Check for user-defined variables.
13446 */
13447 else
13448 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013449 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 }
13453
Bram Moolenaare9a41262005-01-15 22:18:47 +000013454 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456 if (rettv != NULL)
13457 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 ret = FAIL;
13459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013460 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013461 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
13463 name[len] = cc;
13464
13465 return ret;
13466}
13467
13468/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013469 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13470 * value).
13471 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013472 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013474{
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013476}
13477
13478/*
13479 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 * The string "s" must have been allocated, it is consumed.
13481 * Return NULL for out of memory, the variable otherwise.
13482 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013483 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013484alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 char_u *s;
13486{
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv = alloc_tv();
13490 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->v_type = VAR_STRING;
13493 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 }
13495 else
13496 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498}
13499
13500/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013501 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 */
13503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506{
13507 if (varp != NULL)
13508 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013509 switch (varp->v_type)
13510 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013511 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013512 func_unref(varp->vval.v_string);
13513 /*FALLTHROUGH*/
13514 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515 vim_free(varp->vval.v_string);
13516 break;
13517 case VAR_LIST:
13518 list_unref(varp->vval.v_list);
13519 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013520 case VAR_DICT:
13521 dict_unref(varp->vval.v_dict);
13522 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523 default:
13524 break;
13525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 vim_free(varp);
13527 }
13528}
13529
13530/*
13531 * Free the memory for a variable value and set the value to NULL or 0.
13532 */
13533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
13537 if (varp != NULL)
13538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013539 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013541 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013542 func_unref(varp->vval.v_string);
13543 /*FALLTHROUGH*/
13544 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013545 vim_free(varp->vval.v_string);
13546 varp->vval.v_string = NULL;
13547 break;
13548 case VAR_LIST:
13549 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013550 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013551 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013552 case VAR_DICT:
13553 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013554 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013555 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013557 varp->vval.v_number = 0;
13558 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 case VAR_UNKNOWN:
13560 break;
13561 default:
13562 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013564 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 }
13566}
13567
13568/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 * Set the value of a variable to NULL without freeing items.
13570 */
13571 static void
13572init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574{
13575 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013576 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577}
13578
13579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 * Get the number value of a variable.
13581 * If it is a String variable, uses vim_str2nr().
13582 */
13583 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013587 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013591 case VAR_NUMBER:
13592 n = (long)(varp->vval.v_number);
13593 break;
13594 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013595 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013596 break;
13597 case VAR_STRING:
13598 if (varp->vval.v_string != NULL)
13599 vim_str2nr(varp->vval.v_string, NULL, NULL,
13600 TRUE, TRUE, &n, NULL);
13601 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013602 case VAR_LIST:
13603 EMSG(_("E703: Using a List as a number"));
13604 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013605 case VAR_DICT:
13606 EMSG(_("E728: Using a Dictionary as a number"));
13607 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013608 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013609 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013610 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013612 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613}
13614
13615/*
13616 * Get the lnum from the first argument. Also accepts ".", "$", etc.
13617 */
13618 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000013620 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621{
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 linenr_T lnum;
13624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 if (lnum == 0) /* no valid number, try using line() */
13627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv.v_type = VAR_NUMBER;
13629 f_line(argvars, &rettv);
13630 lnum = rettv.vval.v_number;
13631 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 }
13633 return lnum;
13634}
13635
13636/*
13637 * Get the string value of a variable.
13638 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000013639 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
13640 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 * If the String variable has never been set, return an empty string.
13642 * Never returns NULL;
13643 */
13644 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
13648 static char_u mybuf[NUMBUFLEN];
13649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
13653 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013656 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013658 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013660 case VAR_NUMBER:
13661 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
13662 return buf;
13663 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013664 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013665 break;
13666 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013667 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013668 break;
13669 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013670 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013671 break;
13672 case VAR_STRING:
13673 if (varp->vval.v_string != NULL)
13674 return varp->vval.v_string;
13675 break;
13676 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013680 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681}
13682
13683/*
13684 * Find variable "name" in the list of variables.
13685 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013686 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000013687 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000013691find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000013693 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694{
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013696 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697
Bram Moolenaara7043832005-01-21 11:56:39 +000013698 ht = find_var_ht(name, &varname);
13699 if (htp != NULL)
13700 *htp = ht;
13701 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 return NULL;
Bram Moolenaara7043832005-01-21 11:56:39 +000013703 return find_var_in_ht(ht, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704}
13705
13706/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013707 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000013708 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000013711find_var_in_ht(ht, varname)
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000013713 char_u *varname;
13714{
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 hashitem_T *hi;
13716
13717 if (*varname == NUL)
13718 {
13719 /* Must be something like "s:", otherwise "ht" would be NULL. */
13720 switch (varname[-2])
13721 {
13722 case 's': return &SCRIPT_SV(current_SID).sv_var;
13723 case 'g': return &globvars_var;
13724 case 'v': return &vimvars_var;
13725 case 'b': return &curbuf->b_bufvar;
13726 case 'w': return &curwin->w_winvar;
13727 case 'l': return &current_funccal->l_vars_var;
13728 case 'a': return &current_funccal->l_avars_var;
13729 }
13730 return NULL;
13731 }
Bram Moolenaara7043832005-01-21 11:56:39 +000013732
13733 hi = hash_find(ht, varname);
13734 if (HASHITEM_EMPTY(hi))
13735 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000013737}
13738
13739/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000013741 * Set "varname" to the start of name without ':'.
13742 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013743 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000013744find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 char_u *name;
13746 char_u **varname;
13747{
13748 if (name[1] != ':')
13749 {
13750 /* If not "x:name" there must not be any ":" in the name. */
13751 if (vim_strchr(name, ':') != NULL)
13752 return NULL;
13753 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013754
13755 /* "version" is "v:version" in all scopes */
13756 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
13757 return &compat_hashtab;
13758
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 return &globvarht; /* global variable */
13761 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 }
13763 *varname = name + 2;
13764 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 if (*name == 'g') /* global variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 return &globvarht;
13770 if (*name == 'v') /* v: variable */
13771 return &vimvarht;
13772 if (*name == 'a' && current_funccal != NULL) /* function argument */
13773 return &current_funccal->l_avars.dv_hashtab;
13774 if (*name == 'l' && current_funccal != NULL) /* local function variable */
13775 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 if (*name == 's' /* script variable */
13777 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
13778 return &SCRIPT_VARS(current_SID);
13779 return NULL;
13780}
13781
13782/*
13783 * Get the string value of a (global/local) variable.
13784 * Returns NULL when it doesn't exist.
13785 */
13786 char_u *
13787get_var_value(name)
13788 char_u *name;
13789{
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaara7043832005-01-21 11:56:39 +000013792 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 if (v == NULL)
13794 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000013795 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796}
13797
13798/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 * sourcing this script and when executing functions defined in the script.
13801 */
13802 void
13803new_script_vars(id)
13804 scid_T id;
13805{
Bram Moolenaara7043832005-01-21 11:56:39 +000013806 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 hashtab_T *ht;
13808 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000013809
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
13811 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013812 /* Re-allocating ga_data means that an ht_array pointing to
13813 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000013815 for (i = 1; i <= ga_scripts.ga_len; ++i)
13816 {
13817 ht = &SCRIPT_VARS(i);
13818 if (ht->ht_mask == HT_INIT_SIZE - 1)
13819 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 sv = &SCRIPT_SV(i);
13821 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000013822 }
13823
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 while (ga_scripts.ga_len < id)
13825 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
13827 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 }
13830 }
13831}
13832
13833/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013834 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
13835 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 */
13837 void
Bram Moolenaar33570922005-01-25 22:26:29 +000013838init_var_dict(dict, dict_var)
13839 dict_T *dict;
13840 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841{
Bram Moolenaar33570922005-01-25 22:26:29 +000013842 hash_init(&dict->dv_hashtab);
13843 dict->dv_refcount = 99999;
13844 dict_var->di_tv.vval.v_dict = dict;
13845 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013846 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013847 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
13848 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849}
13850
13851/*
13852 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000013853 * Frees all allocated variables and the value they contain.
13854 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 */
13856 void
Bram Moolenaara7043832005-01-21 11:56:39 +000013857vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 hashtab_T *ht;
13859{
13860 vars_clear_ext(ht, TRUE);
13861}
13862
13863/*
13864 * Like vars_clear(), but only free the value if "free_val" is TRUE.
13865 */
13866 static void
13867vars_clear_ext(ht, free_val)
13868 hashtab_T *ht;
13869 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870{
Bram Moolenaara7043832005-01-21 11:56:39 +000013871 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 hashitem_T *hi;
13873 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874
Bram Moolenaar33570922005-01-25 22:26:29 +000013875 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000013876 todo = ht->ht_used;
13877 for (hi = ht->ht_array; todo > 0; ++hi)
13878 {
13879 if (!HASHITEM_EMPTY(hi))
13880 {
13881 --todo;
13882
Bram Moolenaar33570922005-01-25 22:26:29 +000013883 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000013884 * ht_array might change then. hash_clear() takes care of it
13885 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013886 v = HI2DI(hi);
13887 if (free_val)
13888 clear_tv(&v->di_tv);
13889 if ((v->di_flags & DI_FLAGS_FIX) == 0)
13890 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000013891 }
13892 }
13893 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894}
13895
Bram Moolenaara7043832005-01-21 11:56:39 +000013896/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013897 * Delete a variable from hashtab "ht" at item "hi".
13898 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000013899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000013901delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000013902 hashtab_T *ht;
13903 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904{
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000013906
13907 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000013908 clear_tv(&di->di_tv);
13909 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910}
13911
13912/*
13913 * List the value of one internal variable.
13914 */
13915 static void
13916list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 char_u *prefix;
13919{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013920 char_u *tofree;
13921 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013922 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013923
Bram Moolenaar33570922005-01-25 22:26:29 +000013924 s = echo_string(&v->di_tv, &tofree, numbuf);
13925 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013926 s == NULL ? (char_u *)"" : s);
13927 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928}
13929
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 static void
13931list_one_var_a(prefix, name, type, string)
13932 char_u *prefix;
13933 char_u *name;
13934 int type;
13935 char_u *string;
13936{
13937 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13938 if (name != NULL) /* "a:" vars don't have a name stored */
13939 msg_puts(name);
13940 msg_putchar(' ');
13941 msg_advance(22);
13942 if (type == VAR_NUMBER)
13943 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013944 else if (type == VAR_FUNC)
13945 msg_putchar('*');
13946 else if (type == VAR_LIST)
13947 {
13948 msg_putchar('[');
13949 if (*string == '[')
13950 ++string;
13951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013952 else if (type == VAR_DICT)
13953 {
13954 msg_putchar('{');
13955 if (*string == '{')
13956 ++string;
13957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 else
13959 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013960
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013962
13963 if (type == VAR_FUNC)
13964 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965}
13966
13967/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 * If the variable already exists, the value is updated.
13970 * Otherwise the variable is created.
13971 */
13972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977{
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013980 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013983 {
13984 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13985 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13986 ? name[2] : name[0]))
13987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013988 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013989 return;
13990 }
13991 if (function_exists(name))
13992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013993 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013994 return;
13995 }
13996 }
13997
Bram Moolenaara7043832005-01-21 11:56:39 +000013998 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014000 {
14001 EMSG2(_("E461: Illegal variable name: %s"), name);
14002 return;
14003 }
14004
14005 v = find_var_in_ht(ht, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014006 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014009 if (var_check_ro(v->di_flags, name)
14010 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014011 return;
14012 if (v->di_tv.v_type != tv->v_type
14013 && !((v->di_tv.v_type == VAR_STRING
14014 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015 && (tv->v_type == VAR_STRING
14016 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014018 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014019 return;
14020 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014021
14022 /*
14023 * Handle setting internal v: variables separately: we keep the type.
14024 */
14025 if (ht == &vimvarht)
14026 {
14027 if (v->di_tv.v_type == VAR_STRING)
14028 {
14029 vim_free(v->di_tv.vval.v_string);
14030 if (copy || tv->v_type != VAR_STRING)
14031 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14032 else
14033 {
14034 /* Take over the string to avoid an extra alloc/free. */
14035 v->di_tv.vval.v_string = tv->vval.v_string;
14036 tv->vval.v_string = NULL;
14037 }
14038 }
14039 else if (v->di_tv.v_type != VAR_NUMBER)
14040 EMSG2(_(e_intern2), "set_var()");
14041 else
14042 v->di_tv.vval.v_number = get_tv_number(tv);
14043 return;
14044 }
14045
14046 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 }
14048 else /* add a new variable */
14049 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014050 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14051 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014052 if (v == NULL)
14053 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014057 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 return;
14059 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014060 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014064 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014065 else
14066 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014067 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014068 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071}
14072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014073/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14075 * Also give an error message.
14076 */
14077 static int
14078var_check_ro(flags, name)
14079 int flags;
14080 char_u *name;
14081{
14082 if (flags & DI_FLAGS_RO)
14083 {
14084 EMSG2(_(e_readonlyvar), name);
14085 return TRUE;
14086 }
14087 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14088 {
14089 EMSG2(_(e_readonlysbx), name);
14090 return TRUE;
14091 }
14092 return FALSE;
14093}
14094
14095/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014096 * Return TRUE if typeval "tv" is set to be locked (immutable).
14097 * Also give an error message, using "name".
14098 */
14099 static int
14100tv_check_lock(lock, name)
14101 int lock;
14102 char_u *name;
14103{
14104 if (lock & VAR_LOCKED)
14105 {
14106 EMSG2(_("E741: Value is locked: %s"),
14107 name == NULL ? (char_u *)_("Unknown") : name);
14108 return TRUE;
14109 }
14110 if (lock & VAR_FIXED)
14111 {
14112 EMSG2(_("E742: Cannot change value of %s"),
14113 name == NULL ? (char_u *)_("Unknown") : name);
14114 return TRUE;
14115 }
14116 return FALSE;
14117}
14118
14119/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014121 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014122 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *from;
14127 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014129 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014130 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014131 switch (from->v_type)
14132 {
14133 case VAR_NUMBER:
14134 to->vval.v_number = from->vval.v_number;
14135 break;
14136 case VAR_STRING:
14137 case VAR_FUNC:
14138 if (from->vval.v_string == NULL)
14139 to->vval.v_string = NULL;
14140 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014142 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014143 if (from->v_type == VAR_FUNC)
14144 func_ref(to->vval.v_string);
14145 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014146 break;
14147 case VAR_LIST:
14148 if (from->vval.v_list == NULL)
14149 to->vval.v_list = NULL;
14150 else
14151 {
14152 to->vval.v_list = from->vval.v_list;
14153 ++to->vval.v_list->lv_refcount;
14154 }
14155 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014156 case VAR_DICT:
14157 if (from->vval.v_dict == NULL)
14158 to->vval.v_dict = NULL;
14159 else
14160 {
14161 to->vval.v_dict = from->vval.v_dict;
14162 ++to->vval.v_dict->dv_refcount;
14163 }
14164 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014165 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014166 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014167 break;
14168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169}
14170
14171/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014172 * Make a copy of an item.
14173 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
14174 */
14175 static void
14176item_copy(from, to, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 typval_T *from;
14178 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014179 int deep;
14180{
14181 static int recurse = 0;
14182
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014184 {
14185 EMSG(_("E698: variable nested too deep for making a copy"));
14186 return;
14187 }
14188 ++recurse;
14189
14190 switch (from->v_type)
14191 {
14192 case VAR_NUMBER:
14193 case VAR_STRING:
14194 case VAR_FUNC:
14195 copy_tv(from, to);
14196 break;
14197 case VAR_LIST:
14198 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014199 to->v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014200 to->vval.v_list = list_copy(from->vval.v_list, deep);
14201 break;
14202 case VAR_DICT:
14203 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014204 to->v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014205 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
14206 break;
14207 default:
14208 EMSG2(_(e_intern2), "item_copy()");
14209 }
14210 --recurse;
14211}
14212
14213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 * ":echo expr1 ..." print each argument separated with a space, add a
14215 * newline at the end.
14216 * ":echon expr1 ..." print each argument plain.
14217 */
14218 void
14219ex_echo(eap)
14220 exarg_T *eap;
14221{
14222 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000014223 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014224 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 char_u *p;
14226 int needclr = TRUE;
14227 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014228 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229
14230 if (eap->skip)
14231 ++emsg_skip;
14232 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
14233 {
14234 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 {
14237 /*
14238 * Report the invalid expression unless the expression evaluation
14239 * has been cancelled due to an aborting error, an interrupt, or an
14240 * exception.
14241 */
14242 if (!aborting())
14243 EMSG2(_(e_invexpr2), p);
14244 break;
14245 }
14246 if (!eap->skip)
14247 {
14248 if (atstart)
14249 {
14250 atstart = FALSE;
14251 /* Call msg_start() after eval1(), evaluating the expression
14252 * may cause a message to appear. */
14253 if (eap->cmdidx == CMD_echo)
14254 msg_start();
14255 }
14256 else if (eap->cmdidx == CMD_echo)
14257 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014258 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014259 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 if (*p == '\n' || *p == '\r' || *p == TAB)
14261 {
14262 if (*p != TAB && needclr)
14263 {
14264 /* remove any text still there from the command */
14265 msg_clr_eos();
14266 needclr = FALSE;
14267 }
14268 msg_putchar_attr(*p, echo_attr);
14269 }
14270 else
14271 {
14272#ifdef FEAT_MBYTE
14273 if (has_mbyte)
14274 {
14275 int i = (*mb_ptr2len_check)(p);
14276
14277 (void)msg_outtrans_len_attr(p, i, echo_attr);
14278 p += i - 1;
14279 }
14280 else
14281#endif
14282 (void)msg_outtrans_len_attr(p, 1, echo_attr);
14283 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014284 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 arg = skipwhite(arg);
14288 }
14289 eap->nextcmd = check_nextcmd(arg);
14290
14291 if (eap->skip)
14292 --emsg_skip;
14293 else
14294 {
14295 /* remove text that may still be there from the command */
14296 if (needclr)
14297 msg_clr_eos();
14298 if (eap->cmdidx == CMD_echo)
14299 msg_end();
14300 }
14301}
14302
14303/*
14304 * ":echohl {name}".
14305 */
14306 void
14307ex_echohl(eap)
14308 exarg_T *eap;
14309{
14310 int id;
14311
14312 id = syn_name2id(eap->arg);
14313 if (id == 0)
14314 echo_attr = 0;
14315 else
14316 echo_attr = syn_id2attr(id);
14317}
14318
14319/*
14320 * ":execute expr1 ..." execute the result of an expression.
14321 * ":echomsg expr1 ..." Print a message
14322 * ":echoerr expr1 ..." Print an error
14323 * Each gets spaces around each argument and a newline at the end for
14324 * echo commands
14325 */
14326 void
14327ex_execute(eap)
14328 exarg_T *eap;
14329{
14330 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000014331 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 int ret = OK;
14333 char_u *p;
14334 garray_T ga;
14335 int len;
14336 int save_did_emsg;
14337
14338 ga_init2(&ga, 1, 80);
14339
14340 if (eap->skip)
14341 ++emsg_skip;
14342 while (*arg != NUL && *arg != '|' && *arg != '\n')
14343 {
14344 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 {
14347 /*
14348 * Report the invalid expression unless the expression evaluation
14349 * has been cancelled due to an aborting error, an interrupt, or an
14350 * exception.
14351 */
14352 if (!aborting())
14353 EMSG2(_(e_invexpr2), p);
14354 ret = FAIL;
14355 break;
14356 }
14357
14358 if (!eap->skip)
14359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 len = (int)STRLEN(p);
14362 if (ga_grow(&ga, len + 2) == FAIL)
14363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 ret = FAIL;
14366 break;
14367 }
14368 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 ga.ga_len += len;
14372 }
14373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 arg = skipwhite(arg);
14376 }
14377
14378 if (ret != FAIL && ga.ga_data != NULL)
14379 {
14380 if (eap->cmdidx == CMD_echomsg)
14381 MSG_ATTR(ga.ga_data, echo_attr);
14382 else if (eap->cmdidx == CMD_echoerr)
14383 {
14384 /* We don't want to abort following commands, restore did_emsg. */
14385 save_did_emsg = did_emsg;
14386 EMSG((char_u *)ga.ga_data);
14387 if (!force_abort)
14388 did_emsg = save_did_emsg;
14389 }
14390 else if (eap->cmdidx == CMD_execute)
14391 do_cmdline((char_u *)ga.ga_data,
14392 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
14393 }
14394
14395 ga_clear(&ga);
14396
14397 if (eap->skip)
14398 --emsg_skip;
14399
14400 eap->nextcmd = check_nextcmd(arg);
14401}
14402
14403/*
14404 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
14405 * "arg" points to the "&" or '+' when called, to "option" when returning.
14406 * Returns NULL when no option name found. Otherwise pointer to the char
14407 * after the option name.
14408 */
14409 static char_u *
14410find_option_end(arg, opt_flags)
14411 char_u **arg;
14412 int *opt_flags;
14413{
14414 char_u *p = *arg;
14415
14416 ++p;
14417 if (*p == 'g' && p[1] == ':')
14418 {
14419 *opt_flags = OPT_GLOBAL;
14420 p += 2;
14421 }
14422 else if (*p == 'l' && p[1] == ':')
14423 {
14424 *opt_flags = OPT_LOCAL;
14425 p += 2;
14426 }
14427 else
14428 *opt_flags = 0;
14429
14430 if (!ASCII_ISALPHA(*p))
14431 return NULL;
14432 *arg = p;
14433
14434 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
14435 p += 4; /* termcap option */
14436 else
14437 while (ASCII_ISALPHA(*p))
14438 ++p;
14439 return p;
14440}
14441
14442/*
14443 * ":function"
14444 */
14445 void
14446ex_function(eap)
14447 exarg_T *eap;
14448{
14449 char_u *theline;
14450 int j;
14451 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 char_u *name = NULL;
14454 char_u *p;
14455 char_u *arg;
14456 garray_T newargs;
14457 garray_T newlines;
14458 int varargs = FALSE;
14459 int mustend = FALSE;
14460 int flags = 0;
14461 ufunc_T *fp;
14462 int indent;
14463 int nesting;
14464 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014465 dictitem_T *v;
14466 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014467 static int func_nr = 0; /* number for nameless function */
14468 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
14470 /*
14471 * ":function" without argument: list functions.
14472 */
14473 if (ends_excmd(*eap->arg))
14474 {
14475 if (!eap->skip)
14476 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014477 if (!isdigit(*fp->name))
14478 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 eap->nextcmd = check_nextcmd(eap->arg);
14480 return;
14481 }
14482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014483 /*
14484 * Get the function name. There are these situations:
14485 * func normal function name
14486 * "name" == func, "fudi.fd_dict" == NULL
14487 * dict.func new dictionary entry
14488 * "name" == NULL, "fudi.fd_dict" set,
14489 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
14490 * dict.func existing dict entry with a Funcref
14491 * "name" == fname, "fudi.fd_dict" set,
14492 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14493 * dict.func existing dict entry that's not a Funcref
14494 * "name" == NULL, "fudi.fd_dict" set,
14495 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014498 name = trans_function_name(&p, eap->skip, 0, &fudi);
14499 paren = (vim_strchr(p, '(') != NULL);
14500 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 {
14502 /*
14503 * Return on an invalid expression in braces, unless the expression
14504 * evaluation has been cancelled due to an aborting error, an
14505 * interrupt, or an exception.
14506 */
14507 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014509 if (!eap->skip && fudi.fd_newkey != NULL)
14510 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014511 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 else
14515 eap->skip = TRUE;
14516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 /* An error in a function call during evaluation of an expression in magic
14518 * braces should not cause the function not to be defined. */
14519 saved_did_emsg = did_emsg;
14520 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521
14522 /*
14523 * ":function func" with only function name: list function.
14524 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014525 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 {
14527 if (!ends_excmd(*skipwhite(p)))
14528 {
14529 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014530 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 }
14532 eap->nextcmd = check_nextcmd(p);
14533 if (eap->nextcmd != NULL)
14534 *p = NUL;
14535 if (!eap->skip && !got_int)
14536 {
14537 fp = find_func(name);
14538 if (fp != NULL)
14539 {
14540 list_func_head(fp, TRUE);
14541 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
14542 {
14543 msg_putchar('\n');
14544 msg_outnum((long)(j + 1));
14545 if (j < 9)
14546 msg_putchar(' ');
14547 if (j < 99)
14548 msg_putchar(' ');
14549 msg_prt_line(FUNCLINE(fp, j));
14550 out_flush(); /* show a line at a time */
14551 ui_breakcheck();
14552 }
14553 if (!got_int)
14554 {
14555 msg_putchar('\n');
14556 msg_puts((char_u *)" endfunction");
14557 }
14558 }
14559 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014560 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014562 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 }
14564
14565 /*
14566 * ":function name(arg1, arg2)" Define function.
14567 */
14568 p = skipwhite(p);
14569 if (*p != '(')
14570 {
14571 if (!eap->skip)
14572 {
14573 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014574 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 }
14576 /* attempt to continue by skipping some text */
14577 if (vim_strchr(p, '(') != NULL)
14578 p = vim_strchr(p, '(');
14579 }
14580 p = skipwhite(p + 1);
14581
14582 ga_init2(&newargs, (int)sizeof(char_u *), 3);
14583 ga_init2(&newlines, (int)sizeof(char_u *), 3);
14584
14585 /*
14586 * Isolate the arguments: "arg1, arg2, ...)"
14587 */
14588 while (*p != ')')
14589 {
14590 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
14591 {
14592 varargs = TRUE;
14593 p += 3;
14594 mustend = TRUE;
14595 }
14596 else
14597 {
14598 arg = p;
14599 while (ASCII_ISALNUM(*p) || *p == '_')
14600 ++p;
14601 if (arg == p || isdigit(*arg)
14602 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
14603 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
14604 {
14605 if (!eap->skip)
14606 EMSG2(_("E125: Illegal argument: %s"), arg);
14607 break;
14608 }
14609 if (ga_grow(&newargs, 1) == FAIL)
14610 goto erret;
14611 c = *p;
14612 *p = NUL;
14613 arg = vim_strsave(arg);
14614 if (arg == NULL)
14615 goto erret;
14616 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
14617 *p = c;
14618 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 if (*p == ',')
14620 ++p;
14621 else
14622 mustend = TRUE;
14623 }
14624 p = skipwhite(p);
14625 if (mustend && *p != ')')
14626 {
14627 if (!eap->skip)
14628 EMSG2(_(e_invarg2), eap->arg);
14629 break;
14630 }
14631 }
14632 ++p; /* skip the ')' */
14633
Bram Moolenaare9a41262005-01-15 22:18:47 +000014634 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 for (;;)
14636 {
14637 p = skipwhite(p);
14638 if (STRNCMP(p, "range", 5) == 0)
14639 {
14640 flags |= FC_RANGE;
14641 p += 5;
14642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014643 else if (STRNCMP(p, "dict", 4) == 0)
14644 {
14645 flags |= FC_DICT;
14646 p += 4;
14647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 else if (STRNCMP(p, "abort", 5) == 0)
14649 {
14650 flags |= FC_ABORT;
14651 p += 5;
14652 }
14653 else
14654 break;
14655 }
14656
14657 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
14658 EMSG(_(e_trailing));
14659
14660 /*
14661 * Read the body of the function, until ":endfunction" is found.
14662 */
14663 if (KeyTyped)
14664 {
14665 /* Check if the function already exists, don't let the user type the
14666 * whole function before telling him it doesn't work! For a script we
14667 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014668 if (!eap->skip && !eap->forceit)
14669 {
14670 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
14671 EMSG(_(e_funcdict));
14672 else if (name != NULL && find_func(name) != NULL)
14673 EMSG2(_(e_funcexts), name);
14674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675
14676 msg_putchar('\n'); /* don't overwrite the function name */
14677 cmdline_row = msg_row;
14678 }
14679
14680 indent = 2;
14681 nesting = 0;
14682 for (;;)
14683 {
14684 msg_scroll = TRUE;
14685 need_wait_return = FALSE;
14686 if (eap->getline == NULL)
14687 theline = getcmdline(':', 0L, indent);
14688 else
14689 theline = eap->getline(':', eap->cookie, indent);
14690 if (KeyTyped)
14691 lines_left = Rows - 1;
14692 if (theline == NULL)
14693 {
14694 EMSG(_("E126: Missing :endfunction"));
14695 goto erret;
14696 }
14697
14698 if (skip_until != NULL)
14699 {
14700 /* between ":append" and "." and between ":python <<EOF" and "EOF"
14701 * don't check for ":endfunc". */
14702 if (STRCMP(theline, skip_until) == 0)
14703 {
14704 vim_free(skip_until);
14705 skip_until = NULL;
14706 }
14707 }
14708 else
14709 {
14710 /* skip ':' and blanks*/
14711 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
14712 ;
14713
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014714 /* Check for "endfunction". */
14715 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 {
14717 vim_free(theline);
14718 break;
14719 }
14720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014721 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 * at "end". */
14723 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
14724 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014725 else if (STRNCMP(p, "if", 2) == 0
14726 || STRNCMP(p, "wh", 2) == 0
14727 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 || STRNCMP(p, "try", 3) == 0)
14729 indent += 2;
14730
14731 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014732 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014734 if (*p == '!')
14735 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 p += eval_fname_script(p);
14737 if (ASCII_ISALPHA(*p))
14738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014739 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 if (*skipwhite(p) == '(')
14741 {
14742 ++nesting;
14743 indent += 2;
14744 }
14745 }
14746 }
14747
14748 /* Check for ":append" or ":insert". */
14749 p = skip_range(p, NULL);
14750 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
14751 || (p[0] == 'i'
14752 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
14753 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
14754 skip_until = vim_strsave((char_u *)".");
14755
14756 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
14757 arg = skipwhite(skiptowhite(p));
14758 if (arg[0] == '<' && arg[1] =='<'
14759 && ((p[0] == 'p' && p[1] == 'y'
14760 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
14761 || (p[0] == 'p' && p[1] == 'e'
14762 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
14763 || (p[0] == 't' && p[1] == 'c'
14764 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
14765 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
14766 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014767 || (p[0] == 'm' && p[1] == 'z'
14768 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 ))
14770 {
14771 /* ":python <<" continues until a dot, like ":append" */
14772 p = skipwhite(arg + 2);
14773 if (*p == NUL)
14774 skip_until = vim_strsave((char_u *)".");
14775 else
14776 skip_until = vim_strsave(p);
14777 }
14778 }
14779
14780 /* Add the line to the function. */
14781 if (ga_grow(&newlines, 1) == FAIL)
14782 goto erret;
14783 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
14784 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 }
14786
14787 /* Don't define the function when skipping commands or when an error was
14788 * detected. */
14789 if (eap->skip || did_emsg)
14790 goto erret;
14791
14792 /*
14793 * If there are no errors, add the function
14794 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014795 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014796 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014797 v = find_var(name, NULL);
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014799 {
14800 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
14801 goto erret;
14802 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014803
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014804 fp = find_func(name);
14805 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014807 if (!eap->forceit)
14808 {
14809 EMSG2(_(e_funcexts), name);
14810 goto erret;
14811 }
14812 if (fp->calls > 0)
14813 {
14814 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
14815 name);
14816 goto erret;
14817 }
14818 /* redefine existing function */
14819 ga_clear_strings(&(fp->args));
14820 ga_clear_strings(&(fp->lines));
14821 vim_free(name);
14822 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 }
14825 else
14826 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014827 char numbuf[20];
14828
14829 fp = NULL;
14830 if (fudi.fd_newkey == NULL && !eap->forceit)
14831 {
14832 EMSG(_(e_funcdict));
14833 goto erret;
14834 }
14835
14836 /* Give the function a sequential number. Can only be used with a
14837 * Funcref! */
14838 vim_free(name);
14839 sprintf(numbuf, "%d", ++func_nr);
14840 name = vim_strsave((char_u *)numbuf);
14841 if (name == NULL)
14842 goto erret;
14843 }
14844
14845 if (fp == NULL)
14846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14848 if (fp == NULL)
14849 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014850
14851 if (fudi.fd_dict != NULL)
14852 {
14853 if (fudi.fd_di == NULL)
14854 {
14855 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014856 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014857 if (fudi.fd_di == NULL)
14858 {
14859 vim_free(fp);
14860 goto erret;
14861 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014862 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
14863 {
14864 vim_free(fudi.fd_di);
14865 goto erret;
14866 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014867 }
14868 else
14869 /* overwrite existing dict entry */
14870 clear_tv(&fudi.fd_di->di_tv);
14871 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014872 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014873 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
14874 fp->refcount = 1;
14875 }
14876
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014878 fp->name = name;
14879 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 fp->next = firstfunc;
14881 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 }
14883 fp->args = newargs;
14884 fp->lines = newlines;
14885 fp->varargs = varargs;
14886 fp->flags = flags;
14887 fp->calls = 0;
14888 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014889 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890
14891erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 ga_clear_strings(&newargs);
14893 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014894ret_free:
14895 vim_free(skip_until);
14896 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899}
14900
14901/*
14902 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000014903 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014905 * flags:
14906 * TFN_INT: internal function name OK
14907 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 * Advances "pp" to just after the function name (if no error).
14909 */
14910 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014911trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 char_u **pp;
14913 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014914 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000014915 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014917 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 char_u *start;
14919 char_u *end;
14920 int lead;
14921 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014924
14925 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014926 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000014928
14929 /* Check for hard coded <SNR>: already translated function ID (from a user
14930 * command). */
14931 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
14932 && (*pp)[2] == (int)KE_SNR)
14933 {
14934 *pp += 3;
14935 len = get_id_len(pp) + 3;
14936 return vim_strnsave(start, len);
14937 }
14938
14939 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
14940 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000014942 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014945 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014946 if (end == start)
14947 {
14948 if (!skip)
14949 EMSG(_("E129: Function name required"));
14950 goto theend;
14951 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014952 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014953 {
14954 /*
14955 * Report an invalid expression in braces, unless the expression
14956 * evaluation has been cancelled due to an aborting error, an
14957 * interrupt, or an exception.
14958 */
14959 if (!aborting())
14960 {
14961 if (end != NULL)
14962 EMSG2(_(e_invarg2), start);
14963 }
14964 else
14965 *pp = find_name_end(start, NULL, NULL, TRUE);
14966 goto theend;
14967 }
14968
14969 if (lv.ll_tv != NULL)
14970 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014971 if (fdp != NULL)
14972 {
14973 fdp->fd_dict = lv.ll_dict;
14974 fdp->fd_newkey = lv.ll_newkey;
14975 lv.ll_newkey = NULL;
14976 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014977 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014978 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14979 {
14980 name = vim_strsave(lv.ll_tv->vval.v_string);
14981 *pp = end;
14982 }
14983 else
14984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014985 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
14986 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014987 EMSG(_(e_funcref));
14988 else
14989 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014990 name = NULL;
14991 }
14992 goto theend;
14993 }
14994
14995 if (lv.ll_name == NULL)
14996 {
14997 /* Error found, but continue after the function name. */
14998 *pp = end;
14999 goto theend;
15000 }
15001
15002 if (lv.ll_exp_name != NULL)
15003 len = STRLEN(lv.ll_exp_name);
15004 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015005 {
15006 if (lead == 2) /* skip over "s:" */
15007 lv.ll_name += 2;
15008 len = (int)(end - lv.ll_name);
15009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015010
15011 /*
15012 * Copy the function name to allocated memory.
15013 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15014 * Accept <SNR>123_name() outside a script.
15015 */
15016 if (skip)
15017 lead = 0; /* do nothing */
15018 else if (lead > 0)
15019 {
15020 lead = 3;
15021 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
15022 {
15023 if (current_SID <= 0)
15024 {
15025 EMSG(_(e_usingsid));
15026 goto theend;
15027 }
15028 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
15029 lead += (int)STRLEN(sid_buf);
15030 }
15031 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015032 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015033 {
15034 EMSG2(_("E128: Function name must start with a capital: %s"),
15035 lv.ll_name);
15036 goto theend;
15037 }
15038 name = alloc((unsigned)(len + lead + 1));
15039 if (name != NULL)
15040 {
15041 if (lead > 0)
15042 {
15043 name[0] = K_SPECIAL;
15044 name[1] = KS_EXTRA;
15045 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000015046 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015047 STRCPY(name + 3, sid_buf);
15048 }
15049 mch_memmove(name + lead, lv.ll_name, (size_t)len);
15050 name[len + lead] = NUL;
15051 }
15052 *pp = end;
15053
15054theend:
15055 clear_lval(&lv);
15056 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057}
15058
15059/*
15060 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
15061 * Return 2 if "p" starts with "s:".
15062 * Return 0 otherwise.
15063 */
15064 static int
15065eval_fname_script(p)
15066 char_u *p;
15067{
15068 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
15069 || STRNICMP(p + 1, "SNR>", 4) == 0))
15070 return 5;
15071 if (p[0] == 's' && p[1] == ':')
15072 return 2;
15073 return 0;
15074}
15075
15076/*
15077 * Return TRUE if "p" starts with "<SID>" or "s:".
15078 * Only works if eval_fname_script() returned non-zero for "p"!
15079 */
15080 static int
15081eval_fname_sid(p)
15082 char_u *p;
15083{
15084 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
15085}
15086
15087/*
15088 * List the head of the function: "name(arg1, arg2)".
15089 */
15090 static void
15091list_func_head(fp, indent)
15092 ufunc_T *fp;
15093 int indent;
15094{
15095 int j;
15096
15097 msg_start();
15098 if (indent)
15099 MSG_PUTS(" ");
15100 MSG_PUTS("function ");
15101 if (fp->name[0] == K_SPECIAL)
15102 {
15103 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
15104 msg_puts(fp->name + 3);
15105 }
15106 else
15107 msg_puts(fp->name);
15108 msg_putchar('(');
15109 for (j = 0; j < fp->args.ga_len; ++j)
15110 {
15111 if (j)
15112 MSG_PUTS(", ");
15113 msg_puts(FUNCARG(fp, j));
15114 }
15115 if (fp->varargs)
15116 {
15117 if (j)
15118 MSG_PUTS(", ");
15119 MSG_PUTS("...");
15120 }
15121 msg_putchar(')');
15122}
15123
15124/*
15125 * Find a function by name, return pointer to it in ufuncs.
15126 * Return NULL for unknown function.
15127 */
15128 static ufunc_T *
15129find_func(name)
15130 char_u *name;
15131{
15132 ufunc_T *fp;
15133
15134 for (fp = firstfunc; fp != NULL; fp = fp->next)
15135 if (STRCMP(name, fp->name) == 0)
15136 break;
15137 return fp;
15138}
15139
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015140/*
15141 * Return TRUE if a function "name" exists.
15142 */
15143 static int
15144function_exists(name)
15145 char_u *name;
15146{
15147 char_u *p = name;
15148 int n = FALSE;
15149
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015150 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015151 if (p != NULL)
15152 {
15153 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
15154 n = (find_func(p) != NULL);
15155 else if (ASCII_ISLOWER(*p))
15156 n = (find_internal_func(p) >= 0);
15157 vim_free(p);
15158 }
15159 return n;
15160}
15161
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
15163
15164/*
15165 * Function given to ExpandGeneric() to obtain the list of user defined
15166 * function names.
15167 */
15168 char_u *
15169get_user_func_name(xp, idx)
15170 expand_T *xp;
15171 int idx;
15172{
15173 static ufunc_T *fp = NULL;
15174
15175 if (idx == 0)
15176 fp = firstfunc;
15177 if (fp != NULL)
15178 {
15179 if (STRLEN(fp->name) + 4 >= IOSIZE)
15180 return fp->name; /* prevents overflow */
15181
15182 cat_func_name(IObuff, fp);
15183 if (xp->xp_context != EXPAND_USER_FUNC)
15184 {
15185 STRCAT(IObuff, "(");
15186 if (!fp->varargs && fp->args.ga_len == 0)
15187 STRCAT(IObuff, ")");
15188 }
15189
15190 fp = fp->next;
15191 return IObuff;
15192 }
15193 return NULL;
15194}
15195
15196#endif /* FEAT_CMDL_COMPL */
15197
15198/*
15199 * Copy the function name of "fp" to buffer "buf".
15200 * "buf" must be able to hold the function name plus three bytes.
15201 * Takes care of script-local function names.
15202 */
15203 static void
15204cat_func_name(buf, fp)
15205 char_u *buf;
15206 ufunc_T *fp;
15207{
15208 if (fp->name[0] == K_SPECIAL)
15209 {
15210 STRCPY(buf, "<SNR>");
15211 STRCAT(buf, fp->name + 3);
15212 }
15213 else
15214 STRCPY(buf, fp->name);
15215}
15216
15217/*
15218 * ":delfunction {name}"
15219 */
15220 void
15221ex_delfunction(eap)
15222 exarg_T *eap;
15223{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015224 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 char_u *p;
15226 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228
15229 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015230 name = trans_function_name(&p, eap->skip, 0, &fudi);
15231 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015233 {
15234 if (fudi.fd_dict != NULL && !eap->skip)
15235 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 if (!ends_excmd(*skipwhite(p)))
15239 {
15240 vim_free(name);
15241 EMSG(_(e_trailing));
15242 return;
15243 }
15244 eap->nextcmd = check_nextcmd(p);
15245 if (eap->nextcmd != NULL)
15246 *p = NUL;
15247
15248 if (!eap->skip)
15249 fp = find_func(name);
15250 vim_free(name);
15251
15252 if (!eap->skip)
15253 {
15254 if (fp == NULL)
15255 {
15256 EMSG2(_("E130: Undefined function: %s"), eap->arg);
15257 return;
15258 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015259 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 {
15261 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
15262 return;
15263 }
15264
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015265 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015267 /* Delete the dict item that refers to the function, it will
15268 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015269 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015271 else
15272 func_free(fp);
15273 }
15274}
15275
15276/*
15277 * Free a function and remove it from the list of functions.
15278 */
15279 static void
15280func_free(fp)
15281 ufunc_T *fp;
15282{
15283 ufunc_T *pfp;
15284
15285 /* clear this function */
15286 vim_free(fp->name);
15287 ga_clear_strings(&(fp->args));
15288 ga_clear_strings(&(fp->lines));
15289
15290 /* remove the function from the function list */
15291 if (firstfunc == fp)
15292 firstfunc = fp->next;
15293 else
15294 {
15295 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
15296 if (pfp->next == fp)
15297 {
15298 pfp->next = fp->next;
15299 break;
15300 }
15301 }
15302 vim_free(fp);
15303}
15304
15305/*
15306 * Unreference a Function: decrement the reference count and free it when it
15307 * becomes zero. Only for numbered functions.
15308 */
15309 static void
15310func_unref(name)
15311 char_u *name;
15312{
15313 ufunc_T *fp;
15314
15315 if (name != NULL && isdigit(*name))
15316 {
15317 fp = find_func(name);
15318 if (fp == NULL)
15319 EMSG2(_(e_intern2), "func_unref()");
15320 else if (--fp->refcount <= 0)
15321 {
15322 /* Only delete it when it's not being used. Otherwise it's done
15323 * when "calls" becomes zero. */
15324 if (fp->calls == 0)
15325 func_free(fp);
15326 }
15327 }
15328}
15329
15330/*
15331 * Count a reference to a Function.
15332 */
15333 static void
15334func_ref(name)
15335 char_u *name;
15336{
15337 ufunc_T *fp;
15338
15339 if (name != NULL && isdigit(*name))
15340 {
15341 fp = find_func(name);
15342 if (fp == NULL)
15343 EMSG2(_(e_intern2), "func_ref()");
15344 else
15345 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 }
15347}
15348
15349/*
15350 * Call a user function.
15351 */
15352 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000015353call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 ufunc_T *fp; /* pointer to function */
15355 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000015356 typval_T *argvars; /* arguments */
15357 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 linenr_T firstline; /* first line of range */
15359 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361{
Bram Moolenaar33570922005-01-25 22:26:29 +000015362 char_u *save_sourcing_name;
15363 linenr_T save_sourcing_lnum;
15364 scid_T save_current_SID;
15365 funccall_T fc;
15366 funccall_T *save_fcp = current_funccal;
15367 int save_did_emsg;
15368 static int depth = 0;
15369 dictitem_T *v;
15370 int fixvar_idx = 0; /* index in fixvar[] */
15371 int i;
15372 int ai;
15373 char_u numbuf[NUMBUFLEN];
15374 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375
15376 /* If depth of calling is getting too high, don't execute the function */
15377 if (depth >= p_mfd)
15378 {
15379 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015380 rettv->v_type = VAR_NUMBER;
15381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 return;
15383 }
15384 ++depth;
15385
15386 line_breakcheck(); /* check for CTRL-C hit */
15387
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 fc.rettv = rettv;
15391 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 fc.linenr = 0;
15393 fc.returned = FALSE;
15394 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 /* Check if this function has a breakpoint. */
15396 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
15397 fc.dbg_tick = debug_tick;
15398
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 /*
15400 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
15401 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
15402 * each argument variable and saves a lot of time.
15403 */
15404 /*
15405 * Init l: variables.
15406 */
15407 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000015408 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 /* Set l:self to "selfdict". */
15411 v = &fc.fixvar[fixvar_idx++].var;
15412 STRCPY(v->di_key, "self");
15413 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
15414 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
15415 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015416 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015417 v->di_tv.vval.v_dict = selfdict;
15418 ++selfdict->dv_refcount;
15419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015420
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 /*
15422 * Init a: variables.
15423 * Set a:0 to "argcount".
15424 * Set a:000 to a list with room for the "..." arguments.
15425 */
15426 init_var_dict(&fc.l_avars, &fc.l_avars_var);
15427 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
15428 (varnumber_T)(argcount - fp->args.ga_len));
15429 v = &fc.fixvar[fixvar_idx++].var;
15430 STRCPY(v->di_key, "000");
15431 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15432 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15433 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015434 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015435 v->di_tv.vval.v_list = &fc.l_varlist;
15436 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
15437 fc.l_varlist.lv_refcount = 99999;
15438
15439 /*
15440 * Set a:firstline to "firstline" and a:lastline to "lastline".
15441 * Set a:name to named arguments.
15442 * Set a:N to the "..." arguments.
15443 */
15444 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
15445 (varnumber_T)firstline);
15446 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
15447 (varnumber_T)lastline);
15448 for (i = 0; i < argcount; ++i)
15449 {
15450 ai = i - fp->args.ga_len;
15451 if (ai < 0)
15452 /* named argument a:name */
15453 name = FUNCARG(fp, i);
15454 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015455 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 /* "..." argument a:1, a:2, etc. */
15457 sprintf((char *)numbuf, "%d", ai + 1);
15458 name = numbuf;
15459 }
15460 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
15461 {
15462 v = &fc.fixvar[fixvar_idx++].var;
15463 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15464 }
15465 else
15466 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015467 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15468 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000015469 if (v == NULL)
15470 break;
15471 v->di_flags = DI_FLAGS_RO;
15472 }
15473 STRCPY(v->di_key, name);
15474 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15475
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015476 /* Note: the values are copied directly to avoid alloc/free.
15477 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015479 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015480
15481 if (ai >= 0 && ai < MAX_FUNC_ARGS)
15482 {
15483 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
15484 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015485 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015486 }
15487 }
15488
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 /* Don't redraw while executing the function. */
15490 ++RedrawingDisabled;
15491 save_sourcing_name = sourcing_name;
15492 save_sourcing_lnum = sourcing_lnum;
15493 sourcing_lnum = 1;
15494 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
15495 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
15496 if (sourcing_name != NULL)
15497 {
15498 if (save_sourcing_name != NULL
15499 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
15500 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
15501 else
15502 STRCPY(sourcing_name, "function ");
15503 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
15504
15505 if (p_verbose >= 12)
15506 {
15507 ++no_wait_return;
15508 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15509 msg_str((char_u *)_("calling %s"), sourcing_name);
15510 if (p_verbose >= 14)
15511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 char_u buf[MSG_BUF_LEN];
15513
15514 msg_puts((char_u *)"(");
15515 for (i = 0; i < argcount; ++i)
15516 {
15517 if (i > 0)
15518 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015519 if (argvars[i].v_type == VAR_NUMBER)
15520 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 else
15522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015523 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 buf, MSG_BUF_LEN);
15525 msg_puts((char_u *)"\"");
15526 msg_puts(buf);
15527 msg_puts((char_u *)"\"");
15528 }
15529 }
15530 msg_puts((char_u *)")");
15531 }
15532 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15533 cmdline_row = msg_row;
15534 --no_wait_return;
15535 }
15536 }
15537 save_current_SID = current_SID;
15538 current_SID = fp->script_ID;
15539 save_did_emsg = did_emsg;
15540 did_emsg = FALSE;
15541
15542 /* call do_cmdline() to execute the lines */
15543 do_cmdline(NULL, get_func_line, (void *)&fc,
15544 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
15545
15546 --RedrawingDisabled;
15547
15548 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 clear_tv(rettv);
15552 rettv->v_type = VAR_NUMBER;
15553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 }
15555
15556 /* when being verbose, mention the return value */
15557 if (p_verbose >= 12)
15558 {
15559 char_u *sn, *val;
15560
15561 ++no_wait_return;
15562 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15563
15564 /* Make sure the output fits in IObuff. */
15565 sn = sourcing_name;
15566 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
15567 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
15568
15569 if (aborting())
15570 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 (long)fc.rettv->vval.v_number);
15574 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 if (STRLEN(val) > IOSIZE / 2 - 50)
15578 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
15579 smsg((char_u *)_("%s returning \"%s\""), sn, val);
15580 }
15581 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15582 cmdline_row = msg_row;
15583 --no_wait_return;
15584 }
15585
15586 vim_free(sourcing_name);
15587 sourcing_name = save_sourcing_name;
15588 sourcing_lnum = save_sourcing_lnum;
15589 current_SID = save_current_SID;
15590
15591 if (p_verbose >= 12 && sourcing_name != NULL)
15592 {
15593 ++no_wait_return;
15594 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15595 msg_str((char_u *)_("continuing in %s"), sourcing_name);
15596 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15597 cmdline_row = msg_row;
15598 --no_wait_return;
15599 }
15600
15601 did_emsg |= save_did_emsg;
15602 current_funccal = save_fcp;
15603
Bram Moolenaar33570922005-01-25 22:26:29 +000015604 /* The a: variables typevals were not alloced, only free the allocated
15605 * variables. */
15606 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
15607
15608 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 --depth;
15610}
15611
15612/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 * Add a number variable "name" to dict "dp" with value "nr".
15614 */
15615 static void
15616add_nr_var(dp, v, name, nr)
15617 dict_T *dp;
15618 dictitem_T *v;
15619 char *name;
15620 varnumber_T nr;
15621{
15622 STRCPY(v->di_key, name);
15623 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15624 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
15625 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015626 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 v->di_tv.vval.v_number = nr;
15628}
15629
15630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 * ":return [expr]"
15632 */
15633 void
15634ex_return(eap)
15635 exarg_T *eap;
15636{
15637 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 int returning = FALSE;
15640
15641 if (current_funccal == NULL)
15642 {
15643 EMSG(_("E133: :return not inside a function"));
15644 return;
15645 }
15646
15647 if (eap->skip)
15648 ++emsg_skip;
15649
15650 eap->nextcmd = NULL;
15651 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 {
15654 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015655 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015657 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 }
15659 /* It's safer to return also on error. */
15660 else if (!eap->skip)
15661 {
15662 /*
15663 * Return unless the expression evaluation has been cancelled due to an
15664 * aborting error, an interrupt, or an exception.
15665 */
15666 if (!aborting())
15667 returning = do_return(eap, FALSE, TRUE, NULL);
15668 }
15669
15670 /* When skipping or the return gets pending, advance to the next command
15671 * in this line (!returning). Otherwise, ignore the rest of the line.
15672 * Following lines will be ignored by get_func_line(). */
15673 if (returning)
15674 eap->nextcmd = NULL;
15675 else if (eap->nextcmd == NULL) /* no argument */
15676 eap->nextcmd = check_nextcmd(arg);
15677
15678 if (eap->skip)
15679 --emsg_skip;
15680}
15681
15682/*
15683 * Return from a function. Possibly makes the return pending. Also called
15684 * for a pending return at the ":endtry" or after returning from an extra
15685 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000015686 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015687 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 * FALSE when the return gets pending.
15689 */
15690 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 exarg_T *eap;
15693 int reanimate;
15694 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696{
15697 int idx;
15698 struct condstack *cstack = eap->cstack;
15699
15700 if (reanimate)
15701 /* Undo the return. */
15702 current_funccal->returned = FALSE;
15703
15704 /*
15705 * Cleanup (and inactivate) conditionals, but stop when a try conditional
15706 * not in its finally clause (which then is to be executed next) is found.
15707 * In this case, make the ":return" pending for execution at the ":endtry".
15708 * Otherwise, return normally.
15709 */
15710 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
15711 if (idx >= 0)
15712 {
15713 cstack->cs_pending[idx] = CSTP_RETURN;
15714
15715 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716 /* A pending return again gets pending. "rettv" points to an
15717 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 else
15721 {
15722 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015723 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015725 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 {
15729 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015731 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 else
15733 EMSG(_(e_outofmem));
15734 }
15735 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737
15738 if (reanimate)
15739 {
15740 /* The pending return value could be overwritten by a ":return"
15741 * without argument in a finally clause; reset the default
15742 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015743 current_funccal->rettv->v_type = VAR_NUMBER;
15744 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 }
15746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015747 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 }
15749 else
15750 {
15751 current_funccal->returned = TRUE;
15752
15753 /* If the return is carried out now, store the return value. For
15754 * a return immediately after reanimation, the value is already
15755 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000015759 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 }
15763 }
15764
15765 return idx < 0;
15766}
15767
15768/*
15769 * Free the variable with a pending return value.
15770 */
15771 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772discard_pending_return(rettv)
15773 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774{
Bram Moolenaar33570922005-01-25 22:26:29 +000015775 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776}
15777
15778/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015779 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 * is an allocated string. Used by report_pending() for verbose messages.
15781 */
15782 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015783get_return_cmd(rettv)
15784 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 char_u *s;
15787 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015788 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015790 if (rettv == NULL)
15791 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 else
Bram Moolenaar33570922005-01-25 22:26:29 +000015793 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794
15795 STRCPY(IObuff, ":return ");
15796 STRNCPY(IObuff + 8, s, IOSIZE - 8);
15797 if (STRLEN(s) + 8 >= IOSIZE)
15798 STRCPY(IObuff + IOSIZE - 4, "...");
15799 vim_free(tofree);
15800 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801}
15802
15803/*
15804 * Get next function line.
15805 * Called by do_cmdline() to get the next line.
15806 * Returns allocated string, or NULL for end of function.
15807 */
15808/* ARGSUSED */
15809 char_u *
15810get_func_line(c, cookie, indent)
15811 int c; /* not used */
15812 void *cookie;
15813 int indent; /* not used */
15814{
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 char_u *retval;
15817 garray_T *gap; /* growarray with function lines */
15818
15819 /* If breakpoints have been added/deleted need to check for it. */
15820 if (fcp->dbg_tick != debug_tick)
15821 {
15822 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15823 sourcing_lnum);
15824 fcp->dbg_tick = debug_tick;
15825 }
15826
15827 gap = &fcp->func->lines;
15828 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15829 retval = NULL;
15830 else if (fcp->returned || fcp->linenr >= gap->ga_len)
15831 retval = NULL;
15832 else
15833 {
15834 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
15835 sourcing_lnum = fcp->linenr;
15836 }
15837
15838 /* Did we encounter a breakpoint? */
15839 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
15840 {
15841 dbg_breakpoint(fcp->func->name, sourcing_lnum);
15842 /* Find next breakpoint. */
15843 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15844 sourcing_lnum);
15845 fcp->dbg_tick = debug_tick;
15846 }
15847
15848 return retval;
15849}
15850
15851/*
15852 * Return TRUE if the currently active function should be ended, because a
15853 * return was encountered or an error occured. Used inside a ":while".
15854 */
15855 int
15856func_has_ended(cookie)
15857 void *cookie;
15858{
Bram Moolenaar33570922005-01-25 22:26:29 +000015859 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860
15861 /* Ignore the "abort" flag if the abortion behavior has been changed due to
15862 * an error inside a try conditional. */
15863 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15864 || fcp->returned);
15865}
15866
15867/*
15868 * return TRUE if cookie indicates a function which "abort"s on errors.
15869 */
15870 int
15871func_has_abort(cookie)
15872 void *cookie;
15873{
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 return ((funccall_T *)cookie)->func->flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875}
15876
15877#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
15878typedef enum
15879{
15880 VAR_FLAVOUR_DEFAULT,
15881 VAR_FLAVOUR_SESSION,
15882 VAR_FLAVOUR_VIMINFO
15883} var_flavour_T;
15884
15885static var_flavour_T var_flavour __ARGS((char_u *varname));
15886
15887 static var_flavour_T
15888var_flavour(varname)
15889 char_u *varname;
15890{
15891 char_u *p = varname;
15892
15893 if (ASCII_ISUPPER(*p))
15894 {
15895 while (*(++p))
15896 if (ASCII_ISLOWER(*p))
15897 return VAR_FLAVOUR_SESSION;
15898 return VAR_FLAVOUR_VIMINFO;
15899 }
15900 else
15901 return VAR_FLAVOUR_DEFAULT;
15902}
15903#endif
15904
15905#if defined(FEAT_VIMINFO) || defined(PROTO)
15906/*
15907 * Restore global vars that start with a capital from the viminfo file
15908 */
15909 int
15910read_viminfo_varlist(virp, writing)
15911 vir_T *virp;
15912 int writing;
15913{
15914 char_u *tab;
15915 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917
15918 if (!writing && (find_viminfo_parameter('!') != NULL))
15919 {
15920 tab = vim_strchr(virp->vir_line + 1, '\t');
15921 if (tab != NULL)
15922 {
15923 *tab++ = '\0'; /* isolate the variable name */
15924 if (*tab == 'S') /* string var */
15925 is_string = TRUE;
15926
15927 tab = vim_strchr(tab, '\t');
15928 if (tab != NULL)
15929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 if (is_string)
15931 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015932 tv.v_type = VAR_STRING;
15933 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 }
15936 else
15937 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015938 tv.v_type = VAR_NUMBER;
15939 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015941 set_var(virp->vir_line + 1, &tv, FALSE);
15942 if (is_string)
15943 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 }
15945 }
15946 }
15947
15948 return viminfo_readline(virp);
15949}
15950
15951/*
15952 * Write global vars that start with a capital to the viminfo file
15953 */
15954 void
15955write_viminfo_varlist(fp)
15956 FILE *fp;
15957{
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 hashitem_T *hi;
15959 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000015960 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015961 char *s;
15962 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015963 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964
15965 if (find_viminfo_parameter('!') == NULL)
15966 return;
15967
15968 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000015969
Bram Moolenaar33570922005-01-25 22:26:29 +000015970 todo = globvarht.ht_used;
15971 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015973 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015975 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 this_var = HI2DI(hi);
15977 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015978 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000015980 {
15981 case VAR_STRING: s = "STR"; break;
15982 case VAR_NUMBER: s = "NUM"; break;
15983 default: continue;
15984 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
15986 viminfo_writestring(fp, echo_string(&this_var->di_tv,
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015987 &tofree, numbuf));
Bram Moolenaara7043832005-01-21 11:56:39 +000015988 vim_free(tofree);
15989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 }
15991 }
15992}
15993#endif
15994
15995#if defined(FEAT_SESSION) || defined(PROTO)
15996 int
15997store_session_globals(fd)
15998 FILE *fd;
15999{
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 hashitem_T *hi;
16001 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000016002 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 char_u *p, *t;
16004
Bram Moolenaar33570922005-01-25 22:26:29 +000016005 todo = globvarht.ht_used;
16006 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016008 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016010 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 this_var = HI2DI(hi);
16012 if ((this_var->di_tv.v_type == VAR_NUMBER
16013 || this_var->di_tv.v_type == VAR_STRING)
16014 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016015 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016016 /* Escape special characters with a backslash. Turn a LF and
16017 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000016019 (char_u *)"\\\"\n\r");
16020 if (p == NULL) /* out of memory */
16021 break;
16022 for (t = p; *t != NUL; ++t)
16023 if (*t == '\n')
16024 *t = 'n';
16025 else if (*t == '\r')
16026 *t = 'r';
16027 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000016028 this_var->di_key,
16029 (this_var->di_tv.v_type == VAR_STRING) ? '"'
16030 : ' ',
16031 p,
16032 (this_var->di_tv.v_type == VAR_STRING) ? '"'
16033 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000016034 || put_eol(fd) == FAIL)
16035 {
16036 vim_free(p);
16037 return FAIL;
16038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 vim_free(p);
16040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 }
16042 }
16043 return OK;
16044}
16045#endif
16046
16047#endif /* FEAT_EVAL */
16048
16049#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
16050
16051
16052#ifdef WIN3264
16053/*
16054 * Functions for ":8" filename modifier: get 8.3 version of a filename.
16055 */
16056static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
16057static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
16058static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
16059
16060/*
16061 * Get the short pathname of a file.
16062 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
16063 */
16064 static int
16065get_short_pathname(fnamep, bufp, fnamelen)
16066 char_u **fnamep;
16067 char_u **bufp;
16068 int *fnamelen;
16069{
16070 int l,len;
16071 char_u *newbuf;
16072
16073 len = *fnamelen;
16074
16075 l = GetShortPathName(*fnamep, *fnamep, len);
16076 if (l > len - 1)
16077 {
16078 /* If that doesn't work (not enough space), then save the string
16079 * and try again with a new buffer big enough
16080 */
16081 newbuf = vim_strnsave(*fnamep, l);
16082 if (newbuf == NULL)
16083 return 0;
16084
16085 vim_free(*bufp);
16086 *fnamep = *bufp = newbuf;
16087
16088 l = GetShortPathName(*fnamep,*fnamep,l+1);
16089
16090 /* Really should always succeed, as the buffer is big enough */
16091 }
16092
16093 *fnamelen = l;
16094 return 1;
16095}
16096
16097/*
16098 * Create a short path name. Returns the length of the buffer it needs.
16099 * Doesn't copy over the end of the buffer passed in.
16100 */
16101 static int
16102shortpath_for_invalid_fname(fname, bufp, fnamelen)
16103 char_u **fname;
16104 char_u **bufp;
16105 int *fnamelen;
16106{
16107 char_u *s, *p, *pbuf2, *pbuf3;
16108 char_u ch;
16109 int l,len,len2,plen,slen;
16110
16111 /* Make a copy */
16112 len2 = *fnamelen;
16113 pbuf2 = vim_strnsave(*fname, len2);
16114 pbuf3 = NULL;
16115
16116 s = pbuf2 + len2 - 1; /* Find the end */
16117 slen = 1;
16118 plen = len2;
16119
16120 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016121 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 {
16123 --s;
16124 ++slen;
16125 --plen;
16126 }
16127
16128 do
16129 {
16130 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016131 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 {
16133 --s;
16134 ++slen;
16135 --plen;
16136 }
16137 if (s <= pbuf2)
16138 break;
16139
16140 /* Remeber the character that is about to be blatted */
16141 ch = *s;
16142 *s = 0; /* get_short_pathname requires a null-terminated string */
16143
16144 /* Try it in situ */
16145 p = pbuf2;
16146 if (!get_short_pathname(&p, &pbuf3, &plen))
16147 {
16148 vim_free(pbuf2);
16149 return -1;
16150 }
16151 *s = ch; /* Preserve the string */
16152 } while (plen == 0);
16153
16154 if (plen > 0)
16155 {
16156 /* Remeber the length of the new string. */
16157 *fnamelen = len = plen + slen;
16158 vim_free(*bufp);
16159 if (len > len2)
16160 {
16161 /* If there's not enough space in the currently allocated string,
16162 * then copy it to a buffer big enough.
16163 */
16164 *fname= *bufp = vim_strnsave(p, len);
16165 if (*fname == NULL)
16166 return -1;
16167 }
16168 else
16169 {
16170 /* Transfer pbuf2 to being the main buffer (it's big enough) */
16171 *fname = *bufp = pbuf2;
16172 if (p != pbuf2)
16173 strncpy(*fname, p, plen);
16174 pbuf2 = NULL;
16175 }
16176 /* Concat the next bit */
16177 strncpy(*fname + plen, s, slen);
16178 (*fname)[len] = '\0';
16179 }
16180 vim_free(pbuf3);
16181 vim_free(pbuf2);
16182 return 0;
16183}
16184
16185/*
16186 * Get a pathname for a partial path.
16187 */
16188 static int
16189shortpath_for_partial(fnamep, bufp, fnamelen)
16190 char_u **fnamep;
16191 char_u **bufp;
16192 int *fnamelen;
16193{
16194 int sepcount, len, tflen;
16195 char_u *p;
16196 char_u *pbuf, *tfname;
16197 int hasTilde;
16198
16199 /* Count up the path seperators from the RHS.. so we know which part
16200 * of the path to return.
16201 */
16202 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016203 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 if (vim_ispathsep(*p))
16205 ++sepcount;
16206
16207 /* Need full path first (use expand_env() to remove a "~/") */
16208 hasTilde = (**fnamep == '~');
16209 if (hasTilde)
16210 pbuf = tfname = expand_env_save(*fnamep);
16211 else
16212 pbuf = tfname = FullName_save(*fnamep, FALSE);
16213
16214 len = tflen = STRLEN(tfname);
16215
16216 if (!get_short_pathname(&tfname, &pbuf, &len))
16217 return -1;
16218
16219 if (len == 0)
16220 {
16221 /* Don't have a valid filename, so shorten the rest of the
16222 * path if we can. This CAN give us invalid 8.3 filenames, but
16223 * there's not a lot of point in guessing what it might be.
16224 */
16225 len = tflen;
16226 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
16227 return -1;
16228 }
16229
16230 /* Count the paths backward to find the beginning of the desired string. */
16231 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016232 {
16233#ifdef FEAT_MBYTE
16234 if (has_mbyte)
16235 p -= mb_head_off(tfname, p);
16236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 if (vim_ispathsep(*p))
16238 {
16239 if (sepcount == 0 || (hasTilde && sepcount == 1))
16240 break;
16241 else
16242 sepcount --;
16243 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 if (hasTilde)
16246 {
16247 --p;
16248 if (p >= tfname)
16249 *p = '~';
16250 else
16251 return -1;
16252 }
16253 else
16254 ++p;
16255
16256 /* Copy in the string - p indexes into tfname - allocated at pbuf */
16257 vim_free(*bufp);
16258 *fnamelen = (int)STRLEN(p);
16259 *bufp = pbuf;
16260 *fnamep = p;
16261
16262 return 0;
16263}
16264#endif /* WIN3264 */
16265
16266/*
16267 * Adjust a filename, according to a string of modifiers.
16268 * *fnamep must be NUL terminated when called. When returning, the length is
16269 * determined by *fnamelen.
16270 * Returns valid flags.
16271 * When there is an error, *fnamep is set to NULL.
16272 */
16273 int
16274modify_fname(src, usedlen, fnamep, bufp, fnamelen)
16275 char_u *src; /* string with modifiers */
16276 int *usedlen; /* characters after src that are used */
16277 char_u **fnamep; /* file name so far */
16278 char_u **bufp; /* buffer for allocated file name or NULL */
16279 int *fnamelen; /* length of fnamep */
16280{
16281 int valid = 0;
16282 char_u *tail;
16283 char_u *s, *p, *pbuf;
16284 char_u dirname[MAXPATHL];
16285 int c;
16286 int has_fullname = 0;
16287#ifdef WIN3264
16288 int has_shortname = 0;
16289#endif
16290
16291repeat:
16292 /* ":p" - full path/file_name */
16293 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
16294 {
16295 has_fullname = 1;
16296
16297 valid |= VALID_PATH;
16298 *usedlen += 2;
16299
16300 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
16301 if ((*fnamep)[0] == '~'
16302#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
16303 && ((*fnamep)[1] == '/'
16304# ifdef BACKSLASH_IN_FILENAME
16305 || (*fnamep)[1] == '\\'
16306# endif
16307 || (*fnamep)[1] == NUL)
16308
16309#endif
16310 )
16311 {
16312 *fnamep = expand_env_save(*fnamep);
16313 vim_free(*bufp); /* free any allocated file name */
16314 *bufp = *fnamep;
16315 if (*fnamep == NULL)
16316 return -1;
16317 }
16318
16319 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016320 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 {
16322 if (vim_ispathsep(*p)
16323 && p[1] == '.'
16324 && (p[2] == NUL
16325 || vim_ispathsep(p[2])
16326 || (p[2] == '.'
16327 && (p[3] == NUL || vim_ispathsep(p[3])))))
16328 break;
16329 }
16330
16331 /* FullName_save() is slow, don't use it when not needed. */
16332 if (*p != NUL || !vim_isAbsName(*fnamep))
16333 {
16334 *fnamep = FullName_save(*fnamep, *p != NUL);
16335 vim_free(*bufp); /* free any allocated file name */
16336 *bufp = *fnamep;
16337 if (*fnamep == NULL)
16338 return -1;
16339 }
16340
16341 /* Append a path separator to a directory. */
16342 if (mch_isdir(*fnamep))
16343 {
16344 /* Make room for one or two extra characters. */
16345 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
16346 vim_free(*bufp); /* free any allocated file name */
16347 *bufp = *fnamep;
16348 if (*fnamep == NULL)
16349 return -1;
16350 add_pathsep(*fnamep);
16351 }
16352 }
16353
16354 /* ":." - path relative to the current directory */
16355 /* ":~" - path relative to the home directory */
16356 /* ":8" - shortname path - postponed till after */
16357 while (src[*usedlen] == ':'
16358 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
16359 {
16360 *usedlen += 2;
16361 if (c == '8')
16362 {
16363#ifdef WIN3264
16364 has_shortname = 1; /* Postpone this. */
16365#endif
16366 continue;
16367 }
16368 pbuf = NULL;
16369 /* Need full path first (use expand_env() to remove a "~/") */
16370 if (!has_fullname)
16371 {
16372 if (c == '.' && **fnamep == '~')
16373 p = pbuf = expand_env_save(*fnamep);
16374 else
16375 p = pbuf = FullName_save(*fnamep, FALSE);
16376 }
16377 else
16378 p = *fnamep;
16379
16380 has_fullname = 0;
16381
16382 if (p != NULL)
16383 {
16384 if (c == '.')
16385 {
16386 mch_dirname(dirname, MAXPATHL);
16387 s = shorten_fname(p, dirname);
16388 if (s != NULL)
16389 {
16390 *fnamep = s;
16391 if (pbuf != NULL)
16392 {
16393 vim_free(*bufp); /* free any allocated file name */
16394 *bufp = pbuf;
16395 pbuf = NULL;
16396 }
16397 }
16398 }
16399 else
16400 {
16401 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
16402 /* Only replace it when it starts with '~' */
16403 if (*dirname == '~')
16404 {
16405 s = vim_strsave(dirname);
16406 if (s != NULL)
16407 {
16408 *fnamep = s;
16409 vim_free(*bufp);
16410 *bufp = s;
16411 }
16412 }
16413 }
16414 vim_free(pbuf);
16415 }
16416 }
16417
16418 tail = gettail(*fnamep);
16419 *fnamelen = (int)STRLEN(*fnamep);
16420
16421 /* ":h" - head, remove "/file_name", can be repeated */
16422 /* Don't remove the first "/" or "c:\" */
16423 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
16424 {
16425 valid |= VALID_HEAD;
16426 *usedlen += 2;
16427 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016428 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 --tail;
16430 *fnamelen = (int)(tail - *fnamep);
16431#ifdef VMS
16432 if (*fnamelen > 0)
16433 *fnamelen += 1; /* the path separator is part of the path */
16434#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016435 while (tail > s && !after_pathsep(s, tail))
16436 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 }
16438
16439 /* ":8" - shortname */
16440 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
16441 {
16442 *usedlen += 2;
16443#ifdef WIN3264
16444 has_shortname = 1;
16445#endif
16446 }
16447
16448#ifdef WIN3264
16449 /* Check shortname after we have done 'heads' and before we do 'tails'
16450 */
16451 if (has_shortname)
16452 {
16453 pbuf = NULL;
16454 /* Copy the string if it is shortened by :h */
16455 if (*fnamelen < (int)STRLEN(*fnamep))
16456 {
16457 p = vim_strnsave(*fnamep, *fnamelen);
16458 if (p == 0)
16459 return -1;
16460 vim_free(*bufp);
16461 *bufp = *fnamep = p;
16462 }
16463
16464 /* Split into two implementations - makes it easier. First is where
16465 * there isn't a full name already, second is where there is.
16466 */
16467 if (!has_fullname && !vim_isAbsName(*fnamep))
16468 {
16469 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
16470 return -1;
16471 }
16472 else
16473 {
16474 int l;
16475
16476 /* Simple case, already have the full-name
16477 * Nearly always shorter, so try first time. */
16478 l = *fnamelen;
16479 if (!get_short_pathname(fnamep, bufp, &l))
16480 return -1;
16481
16482 if (l == 0)
16483 {
16484 /* Couldn't find the filename.. search the paths.
16485 */
16486 l = *fnamelen;
16487 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
16488 return -1;
16489 }
16490 *fnamelen = l;
16491 }
16492 }
16493#endif /* WIN3264 */
16494
16495 /* ":t" - tail, just the basename */
16496 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
16497 {
16498 *usedlen += 2;
16499 *fnamelen -= (int)(tail - *fnamep);
16500 *fnamep = tail;
16501 }
16502
16503 /* ":e" - extension, can be repeated */
16504 /* ":r" - root, without extension, can be repeated */
16505 while (src[*usedlen] == ':'
16506 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
16507 {
16508 /* find a '.' in the tail:
16509 * - for second :e: before the current fname
16510 * - otherwise: The last '.'
16511 */
16512 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
16513 s = *fnamep - 2;
16514 else
16515 s = *fnamep + *fnamelen - 1;
16516 for ( ; s > tail; --s)
16517 if (s[0] == '.')
16518 break;
16519 if (src[*usedlen + 1] == 'e') /* :e */
16520 {
16521 if (s > tail)
16522 {
16523 *fnamelen += (int)(*fnamep - (s + 1));
16524 *fnamep = s + 1;
16525#ifdef VMS
16526 /* cut version from the extension */
16527 s = *fnamep + *fnamelen - 1;
16528 for ( ; s > *fnamep; --s)
16529 if (s[0] == ';')
16530 break;
16531 if (s > *fnamep)
16532 *fnamelen = s - *fnamep;
16533#endif
16534 }
16535 else if (*fnamep <= tail)
16536 *fnamelen = 0;
16537 }
16538 else /* :r */
16539 {
16540 if (s > tail) /* remove one extension */
16541 *fnamelen = (int)(s - *fnamep);
16542 }
16543 *usedlen += 2;
16544 }
16545
16546 /* ":s?pat?foo?" - substitute */
16547 /* ":gs?pat?foo?" - global substitute */
16548 if (src[*usedlen] == ':'
16549 && (src[*usedlen + 1] == 's'
16550 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
16551 {
16552 char_u *str;
16553 char_u *pat;
16554 char_u *sub;
16555 int sep;
16556 char_u *flags;
16557 int didit = FALSE;
16558
16559 flags = (char_u *)"";
16560 s = src + *usedlen + 2;
16561 if (src[*usedlen + 1] == 'g')
16562 {
16563 flags = (char_u *)"g";
16564 ++s;
16565 }
16566
16567 sep = *s++;
16568 if (sep)
16569 {
16570 /* find end of pattern */
16571 p = vim_strchr(s, sep);
16572 if (p != NULL)
16573 {
16574 pat = vim_strnsave(s, (int)(p - s));
16575 if (pat != NULL)
16576 {
16577 s = p + 1;
16578 /* find end of substitution */
16579 p = vim_strchr(s, sep);
16580 if (p != NULL)
16581 {
16582 sub = vim_strnsave(s, (int)(p - s));
16583 str = vim_strnsave(*fnamep, *fnamelen);
16584 if (sub != NULL && str != NULL)
16585 {
16586 *usedlen = (int)(p + 1 - src);
16587 s = do_string_sub(str, pat, sub, flags);
16588 if (s != NULL)
16589 {
16590 *fnamep = s;
16591 *fnamelen = (int)STRLEN(s);
16592 vim_free(*bufp);
16593 *bufp = s;
16594 didit = TRUE;
16595 }
16596 }
16597 vim_free(sub);
16598 vim_free(str);
16599 }
16600 vim_free(pat);
16601 }
16602 }
16603 /* after using ":s", repeat all the modifiers */
16604 if (didit)
16605 goto repeat;
16606 }
16607 }
16608
16609 return valid;
16610}
16611
16612/*
16613 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
16614 * "flags" can be "g" to do a global substitute.
16615 * Returns an allocated string, NULL for error.
16616 */
16617 char_u *
16618do_string_sub(str, pat, sub, flags)
16619 char_u *str;
16620 char_u *pat;
16621 char_u *sub;
16622 char_u *flags;
16623{
16624 int sublen;
16625 regmatch_T regmatch;
16626 int i;
16627 int do_all;
16628 char_u *tail;
16629 garray_T ga;
16630 char_u *ret;
16631 char_u *save_cpo;
16632
16633 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
16634 save_cpo = p_cpo;
16635 p_cpo = (char_u *)"";
16636
16637 ga_init2(&ga, 1, 200);
16638
16639 do_all = (flags[0] == 'g');
16640
16641 regmatch.rm_ic = p_ic;
16642 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16643 if (regmatch.regprog != NULL)
16644 {
16645 tail = str;
16646 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
16647 {
16648 /*
16649 * Get some space for a temporary buffer to do the substitution
16650 * into. It will contain:
16651 * - The text up to where the match is.
16652 * - The substituted text.
16653 * - The text after the match.
16654 */
16655 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
16656 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
16657 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
16658 {
16659 ga_clear(&ga);
16660 break;
16661 }
16662
16663 /* copy the text up to where the match is */
16664 i = (int)(regmatch.startp[0] - tail);
16665 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
16666 /* add the substituted text */
16667 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
16668 + ga.ga_len + i, TRUE, TRUE, FALSE);
16669 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 /* avoid getting stuck on a match with an empty string */
16671 if (tail == regmatch.endp[0])
16672 {
16673 if (*tail == NUL)
16674 break;
16675 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
16676 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 }
16678 else
16679 {
16680 tail = regmatch.endp[0];
16681 if (*tail == NUL)
16682 break;
16683 }
16684 if (!do_all)
16685 break;
16686 }
16687
16688 if (ga.ga_data != NULL)
16689 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
16690
16691 vim_free(regmatch.regprog);
16692 }
16693
16694 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
16695 ga_clear(&ga);
16696 p_cpo = save_cpo;
16697
16698 return ret;
16699}
16700
16701#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */