blob: 30d9b967a1668b71d1e5e11808bfa8dd127fe0cf [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 */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000238#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239
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},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
290 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000291};
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));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000315static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000316static 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));
Bram Moolenaar33570922005-01-25 22:26:29 +0000333static void list_append __ARGS((list_T *l, listitem_T *item));
334static int list_append_tv __ARGS((list_T *l, typval_T *tv));
335static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
336static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
337static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
338static list_T *list_copy __ARGS((list_T *orig, int deep));
339static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
340static char_u *list2string __ARGS((typval_T *tv));
341static void list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
342
343static dict_T *dict_alloc __ARGS((void));
344static void dict_unref __ARGS((dict_T *d));
345static void dict_free __ARGS((dict_T *d));
346static dictitem_T *dictitem_alloc __ARGS((char_u *key));
347static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
348static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
349static void dictitem_free __ARGS((dictitem_T *item));
350static int dict_add __ARGS((dict_T *d, dictitem_T *item));
351static long dict_len __ARGS((dict_T *d));
352static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
353static char_u *dict2string __ARGS((typval_T *tv));
354static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355
356static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
357static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
358static char_u *string_quote __ARGS((char_u *str, int function));
359static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
360static int find_internal_func __ARGS((char_u *name));
361static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
362static 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));
363static 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));
364
365static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
366static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
367static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
368static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
369static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
370static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
371static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
372static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
373static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
374static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
375static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
376static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
377static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
378static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
379static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
380static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
381static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
382static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
383static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
384static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
385static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
386static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
387static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
388static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
389static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
390static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
391static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
392static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
393static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
394static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
395static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
396static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
397static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
398static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
399static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
400static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000455static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000472static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static 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));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000481static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
504#ifdef HAVE_STRFTIME
505static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
506#endif
507static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000534static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535
536static win_T *find_win_by_nr __ARGS((typval_T *vp));
537static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
538static int get_env_len __ARGS((char_u **arg));
539static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000540static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
542static int eval_isnamec __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000543static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
544static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static typval_T *alloc_tv __ARGS((void));
546static typval_T *alloc_string_tv __ARGS((char_u *string));
547static void free_tv __ARGS((typval_T *varp));
548static void clear_tv __ARGS((typval_T *varp));
549static void init_tv __ARGS((typval_T *varp));
550static long get_tv_number __ARGS((typval_T *varp));
551static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
552static char_u *get_tv_string __ARGS((typval_T *varp));
553static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
554static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
555static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname));
556static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
557static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
558static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
559static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
560static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
561static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
562static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void copy_tv __ARGS((typval_T *from, typval_T *to));
565static void item_copy __ARGS((typval_T *from, typval_T *to, int deep));
566static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
567static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
568static int eval_fname_script __ARGS((char_u *p));
569static int eval_fname_sid __ARGS((char_u *p));
570static void list_func_head __ARGS((ufunc_T *fp, int indent));
571static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
572static ufunc_T *find_func __ARGS((char_u *name));
573static int function_exists __ARGS((char_u *name));
574static void func_free __ARGS((ufunc_T *fp));
575static void func_unref __ARGS((char_u *name));
576static void func_ref __ARGS((char_u *name));
577static 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));
578static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
579
580static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
581
582static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
583static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
584static char_u *skip_var_one __ARGS((char_u *arg));
585static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
586static void list_glob_vars __ARGS((void));
587static void list_buf_vars __ARGS((void));
588static void list_win_vars __ARGS((void));
589static void list_vim_vars __ARGS((void));
590static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
591static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
592static int check_changedtick __ARGS((char_u *arg));
593static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
594static void clear_lval __ARGS((lval_T *lp));
595static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
596static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
597static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
598static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
599static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
603static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604
605/*
606 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000607 */
608 void
609eval_init()
610{
Bram Moolenaar33570922005-01-25 22:26:29 +0000611 int i;
612 struct vimvar *p;
613
614 init_var_dict(&globvardict, &globvars_var);
615 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000616 hash_init(&compat_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000617
618 for (i = 0; i < VV_LEN; ++i)
619 {
620 p = &vimvars[i];
621 STRCPY(p->vv_di.di_key, p->vv_name);
622 if (p->vv_flags & VV_RO)
623 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
624 else if (p->vv_flags & VV_RO_SBX)
625 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
626 else
627 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000628
629 /* add to v: scope dict, unless the value is not always available */
630 if (p->vv_type != VAR_UNKNOWN)
631 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000632 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000633 /* add to compat scope dict */
634 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000635 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000636}
637
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 * Return the name of the executed function.
640 */
641 char_u *
642func_name(cookie)
643 void *cookie;
644{
Bram Moolenaar33570922005-01-25 22:26:29 +0000645 return ((funccall_T *)cookie)->func->name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646}
647
648/*
649 * Return the address holding the next breakpoint line for a funccall cookie.
650 */
651 linenr_T *
652func_breakpoint(cookie)
653 void *cookie;
654{
Bram Moolenaar33570922005-01-25 22:26:29 +0000655 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656}
657
658/*
659 * Return the address holding the debug tick for a funccall cookie.
660 */
661 int *
662func_dbg_tick(cookie)
663 void *cookie;
664{
Bram Moolenaar33570922005-01-25 22:26:29 +0000665 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
668/*
669 * Return the nesting level for a funccall cookie.
670 */
671 int
672func_level(cookie)
673 void *cookie;
674{
Bram Moolenaar33570922005-01-25 22:26:29 +0000675 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676}
677
678/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000679funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681/*
682 * Return TRUE when a function was ended by a ":return" command.
683 */
684 int
685current_func_returned()
686{
687 return current_funccal->returned;
688}
689
690
691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * Set an internal variable to a string value. Creates the variable if it does
693 * not already exist.
694 */
695 void
696set_internal_string_var(name, value)
697 char_u *name;
698 char_u *value;
699{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000700 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000701 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703 val = vim_strsave(value);
704 if (val != NULL)
705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000706 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000707 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000709 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000710 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 }
712 }
713}
714
715# if defined(FEAT_MBYTE) || defined(PROTO)
716 int
717eval_charconvert(enc_from, enc_to, fname_from, fname_to)
718 char_u *enc_from;
719 char_u *enc_to;
720 char_u *fname_from;
721 char_u *fname_to;
722{
723 int err = FALSE;
724
725 set_vim_var_string(VV_CC_FROM, enc_from, -1);
726 set_vim_var_string(VV_CC_TO, enc_to, -1);
727 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
728 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
729 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
730 err = TRUE;
731 set_vim_var_string(VV_CC_FROM, NULL, -1);
732 set_vim_var_string(VV_CC_TO, NULL, -1);
733 set_vim_var_string(VV_FNAME_IN, NULL, -1);
734 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
735
736 if (err)
737 return FAIL;
738 return OK;
739}
740# endif
741
742# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
743 int
744eval_printexpr(fname, args)
745 char_u *fname;
746 char_u *args;
747{
748 int err = FALSE;
749
750 set_vim_var_string(VV_FNAME_IN, fname, -1);
751 set_vim_var_string(VV_CMDARG, args, -1);
752 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
753 err = TRUE;
754 set_vim_var_string(VV_FNAME_IN, NULL, -1);
755 set_vim_var_string(VV_CMDARG, NULL, -1);
756
757 if (err)
758 {
759 mch_remove(fname);
760 return FAIL;
761 }
762 return OK;
763}
764# endif
765
766# if defined(FEAT_DIFF) || defined(PROTO)
767 void
768eval_diff(origfile, newfile, outfile)
769 char_u *origfile;
770 char_u *newfile;
771 char_u *outfile;
772{
773 int err = FALSE;
774
775 set_vim_var_string(VV_FNAME_IN, origfile, -1);
776 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
777 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
778 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
779 set_vim_var_string(VV_FNAME_IN, NULL, -1);
780 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
781 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
782}
783
784 void
785eval_patch(origfile, difffile, outfile)
786 char_u *origfile;
787 char_u *difffile;
788 char_u *outfile;
789{
790 int err;
791
792 set_vim_var_string(VV_FNAME_IN, origfile, -1);
793 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
794 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
795 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
796 set_vim_var_string(VV_FNAME_IN, NULL, -1);
797 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
798 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
799}
800# endif
801
802/*
803 * Top level evaluation function, returning a boolean.
804 * Sets "error" to TRUE if there was an error.
805 * Return TRUE or FALSE.
806 */
807 int
808eval_to_bool(arg, error, nextcmd, skip)
809 char_u *arg;
810 int *error;
811 char_u **nextcmd;
812 int skip; /* only parse, don't execute */
813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 int retval = FALSE;
816
817 if (skip)
818 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 else
822 {
823 *error = FALSE;
824 if (!skip)
825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000826 retval = (get_tv_number(&tv) != 0);
827 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 }
829 }
830 if (skip)
831 --emsg_skip;
832
833 return retval;
834}
835
836/*
837 * Top level evaluation function, returning a string. If "skip" is TRUE,
838 * only parsing to "nextcmd" is done, without reporting errors. Return
839 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
840 */
841 char_u *
842eval_to_string_skip(arg, nextcmd, skip)
843 char_u *arg;
844 char_u **nextcmd;
845 int skip; /* only parse, don't execute */
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 char_u *retval;
849
850 if (skip)
851 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 retval = NULL;
854 else
855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 retval = vim_strsave(get_tv_string(&tv));
857 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 if (skip)
860 --emsg_skip;
861
862 return retval;
863}
864
865/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000866 * Skip over an expression at "*pp".
867 * Return FAIL for an error, OK otherwise.
868 */
869 int
870skip_expr(pp)
871 char_u **pp;
872{
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000874
875 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000877}
878
879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * Top level evaluation function, returning a string.
881 * Return pointer to allocated memory, or NULL for failure.
882 */
883 char_u *
884eval_to_string(arg, nextcmd)
885 char_u *arg;
886 char_u **nextcmd;
887{
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 char_u *retval;
890
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = NULL;
893 else
894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 retval = vim_strsave(get_tv_string(&tv));
896 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898
899 return retval;
900}
901
902/*
903 * Call eval_to_string() with "sandbox" set and not using local variables.
904 */
905 char_u *
906eval_to_string_safe(arg, nextcmd)
907 char_u *arg;
908 char_u **nextcmd;
909{
910 char_u *retval;
911 void *save_funccalp;
912
913 save_funccalp = save_funccal();
914 ++sandbox;
915 retval = eval_to_string(arg, nextcmd);
916 --sandbox;
917 restore_funccal(save_funccalp);
918 return retval;
919}
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921/*
922 * Top level evaluation function, returning a number.
923 * Evaluates "expr" silently.
924 * Returns -1 for an error.
925 */
926 int
927eval_to_number(expr)
928 char_u *expr;
929{
Bram Moolenaar33570922005-01-25 22:26:29 +0000930 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000932 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934 ++emsg_off;
935
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000936 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 retval = -1;
938 else
939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 retval = get_tv_number(&rettv);
941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943 --emsg_off;
944
945 return retval;
946}
947
948#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
949/*
950 * Call some vimL function and return the result as a string
951 * Uses argv[argc] for the function arguments.
952 */
953 char_u *
954call_vim_function(func, argc, argv, safe)
955 char_u *func;
956 int argc;
957 char_u **argv;
958 int safe; /* use the sandbox */
959{
960 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +0000961 typval_T rettv;
962 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 long n;
964 int len;
965 int i;
966 int doesrange;
967 void *save_funccalp = NULL;
968
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (argvars == NULL)
971 return NULL;
972
973 for (i = 0; i < argc; i++)
974 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000975 /* Pass a NULL or empty argument as an empty string */
976 if (argv[i] == NULL || *argv[i] == NUL)
977 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000978 argvars[i].v_type = VAR_STRING;
979 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000980 continue;
981 }
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 /* Recognize a number argument, the others must be strings. */
984 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
985 if (len != 0 && len == (int)STRLEN(argv[i]))
986 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000987 argvars[i].v_type = VAR_NUMBER;
988 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else
991 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000992 argvars[i].v_type = VAR_STRING;
993 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 }
996
997 if (safe)
998 {
999 save_funccalp = save_funccal();
1000 ++sandbox;
1001 }
1002
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001003 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1004 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001006 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 vim_free(argvars);
1011
1012 if (safe)
1013 {
1014 --sandbox;
1015 restore_funccal(save_funccalp);
1016 }
1017 return retval;
1018}
1019#endif
1020
1021/*
1022 * Save the current function call pointer, and set it to NULL.
1023 * Used when executing autocommands and for ":source".
1024 */
1025 void *
1026save_funccal()
1027{
Bram Moolenaar33570922005-01-25 22:26:29 +00001028 funccall_T *fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 fc = current_funccal;
1031 current_funccal = NULL;
1032 return (void *)fc;
1033}
1034
1035 void
1036restore_funccal(fc)
1037 void *fc;
1038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 current_funccal = (funccall_T *)fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042#ifdef FEAT_FOLDING
1043/*
1044 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1045 * it in "*cp". Doesn't give error messages.
1046 */
1047 int
1048eval_foldexpr(arg, cp)
1049 char_u *arg;
1050 int *cp;
1051{
Bram Moolenaar33570922005-01-25 22:26:29 +00001052 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int retval;
1054 char_u *s;
1055
1056 ++emsg_off;
1057 ++sandbox;
1058 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 retval = 0;
1061 else
1062 {
1063 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001064 if (tv.v_type == VAR_NUMBER)
1065 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001066 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 retval = 0;
1068 else
1069 {
1070 /* If the result is a string, check if there is a non-digit before
1071 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001072 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (!VIM_ISDIGIT(*s) && *s != '-')
1074 *cp = *s++;
1075 retval = atol((char *)s);
1076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001077 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 }
1079 --emsg_off;
1080 --sandbox;
1081
1082 return retval;
1083}
1084#endif
1085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 * ":let" list all variable values
1088 * ":let var1 var2" list variable values
1089 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001090 * ":let var += expr" assignment command.
1091 * ":let var -= expr" assignment command.
1092 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
1095 void
1096ex_let(eap)
1097 exarg_T *eap;
1098{
1099 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001103 int var_count = 0;
1104 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001105 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001107 expr = skip_var_list(arg, &var_count, &semicolon);
1108 if (expr == NULL)
1109 return;
1110 expr = vim_strchr(expr, '=');
1111 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001113 /*
1114 * ":let" without "=": list variables
1115 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001116 if (*arg == '[')
1117 EMSG(_(e_invarg));
1118 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 /* ":let var1 var2" */
1120 arg = list_arg_vars(eap, arg);
1121 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001124 list_glob_vars();
1125 list_buf_vars();
1126 list_win_vars();
1127 list_vim_vars();
1128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 eap->nextcmd = check_nextcmd(arg);
1130 }
1131 else
1132 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001133 op[0] = '=';
1134 op[1] = NUL;
1135 if (expr > arg)
1136 {
1137 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1138 op[0] = expr[-1]; /* +=, -= or .= */
1139 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001140 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (eap->skip)
1143 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001144 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (eap->skip)
1146 {
1147 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 --emsg_skip;
1150 }
1151 else if (i != FAIL)
1152 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001153 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001154 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157 }
1158}
1159
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001160/*
1161 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1162 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001163 * When "nextchars" is not NULL it points to a string with characters that
1164 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1165 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001166 * Returns OK or FAIL;
1167 */
1168 static int
1169ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1170 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001171 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001172 int copy; /* copy values from "tv", don't move */
1173 int semicolon; /* from skip_var_list() */
1174 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001175 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001176{
1177 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001179 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 listitem_T *item;
1181 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001182
1183 if (*arg != '[')
1184 {
1185 /*
1186 * ":let var = expr" or ":for var in list"
1187 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001189 return FAIL;
1190 return OK;
1191 }
1192
1193 /*
1194 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1195 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001196 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001197 {
1198 EMSG(_(e_listreq));
1199 return FAIL;
1200 }
1201
1202 i = list_len(l);
1203 if (semicolon == 0 && var_count < i)
1204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001205 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001206 return FAIL;
1207 }
1208 if (var_count - semicolon > i)
1209 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001210 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001211 return FAIL;
1212 }
1213
1214 item = l->lv_first;
1215 while (*arg != ']')
1216 {
1217 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001219 item = item->li_next;
1220 if (arg == NULL)
1221 return FAIL;
1222
1223 arg = skipwhite(arg);
1224 if (*arg == ';')
1225 {
1226 /* Put the rest of the list (may be empty) in the var after ';'.
1227 * Create a new list for this. */
1228 l = list_alloc();
1229 if (l == NULL)
1230 return FAIL;
1231 while (item != NULL)
1232 {
1233 list_append_tv(l, &item->li_tv);
1234 item = item->li_next;
1235 }
1236
1237 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001238 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001239 ltv.vval.v_list = l;
1240 l->lv_refcount = 1;
1241
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001242 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1243 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244 clear_tv(&ltv);
1245 if (arg == NULL)
1246 return FAIL;
1247 break;
1248 }
1249 else if (*arg != ',' && *arg != ']')
1250 {
1251 EMSG2(_(e_intern2), "ex_let_vars()");
1252 return FAIL;
1253 }
1254 }
1255
1256 return OK;
1257}
1258
1259/*
1260 * Skip over assignable variable "var" or list of variables "[var, var]".
1261 * Used for ":let varvar = expr" and ":for varvar in expr".
1262 * For "[var, var]" increment "*var_count" for each variable.
1263 * for "[var, var; var]" set "semicolon".
1264 * Return NULL for an error.
1265 */
1266 static char_u *
1267skip_var_list(arg, var_count, semicolon)
1268 char_u *arg;
1269 int *var_count;
1270 int *semicolon;
1271{
1272 char_u *p, *s;
1273
1274 if (*arg == '[')
1275 {
1276 /* "[var, var]": find the matching ']'. */
1277 p = arg;
1278 while (1)
1279 {
1280 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1281 s = skip_var_one(p);
1282 if (s == p)
1283 {
1284 EMSG2(_(e_invarg2), p);
1285 return NULL;
1286 }
1287 ++*var_count;
1288
1289 p = skipwhite(s);
1290 if (*p == ']')
1291 break;
1292 else if (*p == ';')
1293 {
1294 if (*semicolon == 1)
1295 {
1296 EMSG(_("Double ; in list of variables"));
1297 return NULL;
1298 }
1299 *semicolon = 1;
1300 }
1301 else if (*p != ',')
1302 {
1303 EMSG2(_(e_invarg2), p);
1304 return NULL;
1305 }
1306 }
1307 return p + 1;
1308 }
1309 else
1310 return skip_var_one(arg);
1311}
1312
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001313/*
1314 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1315 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001316 static char_u *
1317skip_var_one(arg)
1318 char_u *arg;
1319{
1320 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1321 ++arg;
1322 return find_name_end(arg, NULL, NULL, TRUE);
1323}
1324
Bram Moolenaara7043832005-01-21 11:56:39 +00001325/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 * List variables for hashtab "ht" with prefix "prefix".
1327 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001328 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001330list_hashtable_vars(ht, prefix, empty)
1331 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001332 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001334{
Bram Moolenaar33570922005-01-25 22:26:29 +00001335 hashitem_T *hi;
1336 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001337 int todo;
1338
1339 todo = ht->ht_used;
1340 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1341 {
1342 if (!HASHITEM_EMPTY(hi))
1343 {
1344 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 di = HI2DI(hi);
1346 if (empty || di->di_tv.v_type != VAR_STRING
1347 || di->di_tv.vval.v_string != NULL)
1348 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001349 }
1350 }
1351}
1352
1353/*
1354 * List global variables.
1355 */
1356 static void
1357list_glob_vars()
1358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001360}
1361
1362/*
1363 * List buffer variables.
1364 */
1365 static void
1366list_buf_vars()
1367{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001368 char_u numbuf[NUMBUFLEN];
1369
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001371
1372 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1373 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001374}
1375
1376/*
1377 * List window variables.
1378 */
1379 static void
1380list_win_vars()
1381{
Bram Moolenaar33570922005-01-25 22:26:29 +00001382 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001383}
1384
1385/*
1386 * List Vim variables.
1387 */
1388 static void
1389list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390{
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392}
1393
1394/*
1395 * List variables in "arg".
1396 */
1397 static char_u *
1398list_arg_vars(eap, arg)
1399 exarg_T *eap;
1400 char_u *arg;
1401{
1402 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001403 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001405 char_u *name_start;
1406 char_u *arg_subsc;
1407 char_u *tofree;
1408 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409
1410 while (!ends_excmd(*arg) && !got_int)
1411 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001412 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001414 arg = find_name_end(arg, NULL, NULL, TRUE);
1415 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1416 {
1417 emsg_severe = TRUE;
1418 EMSG(_(e_trailing));
1419 break;
1420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001424 /* get_name_len() takes care of expanding curly braces */
1425 name_start = name = arg;
1426 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1427 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001429 /* This is mainly to keep test 49 working: when expanding
1430 * curly braces fails overrule the exception error message. */
1431 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001433 emsg_severe = TRUE;
1434 EMSG2(_(e_invarg2), arg);
1435 break;
1436 }
1437 error = TRUE;
1438 }
1439 else
1440 {
1441 if (tofree != NULL)
1442 name = tofree;
1443 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 else
1446 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001447 /* handle d.key, l[idx], f(expr) */
1448 arg_subsc = arg;
1449 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001450 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001452 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001453 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001454 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001455 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001456 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001457 case 'g': list_glob_vars(); break;
1458 case 'b': list_buf_vars(); break;
1459 case 'w': list_win_vars(); break;
1460 case 'v': list_vim_vars(); break;
1461 default:
1462 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001463 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001464 }
1465 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001466 {
1467 char_u numbuf[NUMBUFLEN];
1468 char_u *tf;
1469 int c;
1470 char_u *s;
1471
1472 s = echo_string(&tv, &tf, numbuf);
1473 c = *arg;
1474 *arg = NUL;
1475 list_one_var_a((char_u *)"",
1476 arg == arg_subsc ? name : name_start,
1477 tv.v_type, s == NULL ? (char_u *)"" : s);
1478 *arg = c;
1479 vim_free(tf);
1480 }
1481 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 }
1484 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001485
1486 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001488
1489 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490 }
1491
1492 return arg;
1493}
1494
1495/*
1496 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1497 * Returns a pointer to the char just after the var name.
1498 * Returns NULL if there is an error.
1499 */
1500 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001501ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001503 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507{
1508 int c1;
1509 char_u *name;
1510 char_u *p;
1511 char_u *arg_end = NULL;
1512 int len;
1513 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001515
1516 /*
1517 * ":let $VAR = expr": Set environment variable.
1518 */
1519 if (*arg == '$')
1520 {
1521 /* Find the end of the name. */
1522 ++arg;
1523 name = arg;
1524 len = get_env_len(&arg);
1525 if (len == 0)
1526 EMSG2(_(e_invarg2), name - 1);
1527 else
1528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 if (op != NULL && (*op == '+' || *op == '-'))
1530 EMSG2(_(e_letwrong), op);
1531 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 EMSG(_(e_letunexp));
1534 else
1535 {
1536 c1 = name[len];
1537 name[len] = NUL;
1538 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001539 if (op != NULL && *op == '.')
1540 {
1541 int mustfree = FALSE;
1542 char_u *s = vim_getenv(name, &mustfree);
1543
1544 if (s != NULL)
1545 {
1546 p = tofree = concat_str(s, p);
1547 if (mustfree)
1548 vim_free(s);
1549 }
1550 }
1551 if (p != NULL)
1552 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 if (STRICMP(name, "HOME") == 0)
1554 init_homedir();
1555 else if (didset_vim && STRICMP(name, "VIM") == 0)
1556 didset_vim = FALSE;
1557 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1558 didset_vimruntime = FALSE;
1559 name[len] = c1;
1560 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001562 }
1563 }
1564 }
1565
1566 /*
1567 * ":let &option = expr": Set option value.
1568 * ":let &l:option = expr": Set local option value.
1569 * ":let &g:option = expr": Set global option value.
1570 */
1571 else if (*arg == '&')
1572 {
1573 /* Find the end of the name. */
1574 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 if (p == NULL || (endchars != NULL
1576 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 EMSG(_(e_letunexp));
1578 else
1579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580 long n;
1581 int opt_type;
1582 long numval;
1583 char_u *stringval = NULL;
1584 char_u *s;
1585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001586 c1 = *p;
1587 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588
1589 n = get_tv_number(tv);
1590 s = get_tv_string(tv);
1591 if (op != NULL && *op != '=')
1592 {
1593 opt_type = get_option_value(arg, &numval,
1594 &stringval, opt_flags);
1595 if ((opt_type == 1 && *op == '.')
1596 || (opt_type == 0 && *op != '.'))
1597 EMSG2(_(e_letwrong), op);
1598 else
1599 {
1600 if (opt_type == 1) /* number */
1601 {
1602 if (*op == '+')
1603 n = numval + n;
1604 else
1605 n = numval - n;
1606 }
1607 else if (opt_type == 0 && stringval != NULL) /* string */
1608 {
1609 s = concat_str(stringval, s);
1610 vim_free(stringval);
1611 stringval = s;
1612 }
1613 }
1614 }
1615 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 *p = c1;
1617 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 }
1620 }
1621
1622 /*
1623 * ":let @r = expr": Set register contents.
1624 */
1625 else if (*arg == '@')
1626 {
1627 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 if (op != NULL && (*op == '+' || *op == '-'))
1629 EMSG2(_(e_letwrong), op);
1630 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 EMSG(_(e_letunexp));
1633 else
1634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 char_u *tofree = NULL;
1636 char_u *s;
1637
1638 p = get_tv_string(tv);
1639 if (op != NULL && *op == '.')
1640 {
1641 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1642 if (s != NULL)
1643 {
1644 p = tofree = concat_str(s, p);
1645 vim_free(s);
1646 }
1647 }
1648 if (p != NULL)
1649 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001651 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 }
1653 }
1654
1655 /*
1656 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001657 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001659 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001661 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001663 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1664 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001666 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1667 EMSG(_(e_letunexp));
1668 else
1669 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001671 arg_end = p;
1672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001674 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 }
1676
1677 else
1678 EMSG2(_(e_invarg2), arg);
1679
1680 return arg_end;
1681}
1682
1683/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001684 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1685 */
1686 static int
1687check_changedtick(arg)
1688 char_u *arg;
1689{
1690 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1691 {
1692 EMSG2(_(e_readonlyvar), arg);
1693 return TRUE;
1694 }
1695 return FALSE;
1696}
1697
1698/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001699 * Get an lval: variable, Dict item or List item that can be assigned a value
1700 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1701 * "name.key", "name.key[expr]" etc.
1702 * Indexing only works if "name" is an existing List or Dictionary.
1703 * "name" points to the start of the name.
1704 * If "rettv" is not NULL it points to the value to be assigned.
1705 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1706 * wrong; must end in space or cmd separator.
1707 *
1708 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001709 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001710 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711 */
1712 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001713get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001715 typval_T *rettv;
1716 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001717 int unlet;
1718 int skip;
1719 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001722 char_u *expr_start, *expr_end;
1723 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001724 dictitem_T *v;
1725 typval_T var1;
1726 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001727 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001728 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001729 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001730 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001731 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001733 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001734 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001735
1736 if (skip)
1737 {
1738 /* When skipping just find the end of the name. */
1739 lp->ll_name = name;
1740 return find_name_end(name, NULL, NULL, TRUE);
1741 }
1742
1743 /* Find the end of the name. */
1744 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1745 if (expr_start != NULL)
1746 {
1747 /* Don't expand the name when we already know there is an error. */
1748 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1749 && *p != '[' && *p != '.')
1750 {
1751 EMSG(_(e_trailing));
1752 return NULL;
1753 }
1754
1755 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1756 if (lp->ll_exp_name == NULL)
1757 {
1758 /* Report an invalid expression in braces, unless the
1759 * expression evaluation has been cancelled due to an
1760 * aborting error, an interrupt, or an exception. */
1761 if (!aborting() && !quiet)
1762 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001763 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001764 EMSG2(_(e_invarg2), name);
1765 return NULL;
1766 }
1767 }
1768 lp->ll_name = lp->ll_exp_name;
1769 }
1770 else
1771 lp->ll_name = name;
1772
1773 /* Without [idx] or .key we are done. */
1774 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1775 return p;
1776
1777 cc = *p;
1778 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00001779 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001780 if (v == NULL && !quiet)
1781 EMSG2(_(e_undefvar), lp->ll_name);
1782 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 if (v == NULL)
1784 return NULL;
1785
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001786 /*
1787 * Loop until no more [idx] or .key is following.
1788 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001789 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1793 && !(lp->ll_tv->v_type == VAR_DICT
1794 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 if (!quiet)
1797 EMSG(_("E689: Can only index a List or Dictionary"));
1798 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001800 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001802 if (!quiet)
1803 EMSG(_("E708: [:] must come last"));
1804 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001806
Bram Moolenaar8c711452005-01-14 21:53:12 +00001807 len = -1;
1808 if (*p == '.')
1809 {
1810 key = p + 1;
1811 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1812 ;
1813 if (len == 0)
1814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001815 if (!quiet)
1816 EMSG(_(e_emptykey));
1817 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001818 }
1819 p = key + len;
1820 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001821 else
1822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001823 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001824 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001825 if (*p == ':')
1826 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001827 else
1828 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001829 empty1 = FALSE;
1830 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001832 }
1833
1834 /* Optionally get the second index [ :expr]. */
1835 if (*p == ':')
1836 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001839 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001841 if (!empty1)
1842 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001844 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845 if (rettv != NULL && (rettv->v_type != VAR_LIST
1846 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001847 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 if (!quiet)
1849 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001850 if (!empty1)
1851 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001853 }
1854 p = skipwhite(p + 1);
1855 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001857 else
1858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001859 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001860 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001862 if (!empty1)
1863 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001865 }
1866 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001869 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001870 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001871
Bram Moolenaar8c711452005-01-14 21:53:12 +00001872 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 if (!quiet)
1875 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001876 if (!empty1)
1877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001879 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001880 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001881 }
1882
1883 /* Skip to past ']'. */
1884 ++p;
1885 }
1886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001887 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001888 {
1889 if (len == -1)
1890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001892 key = get_tv_string(&var1);
1893 if (*key == NUL)
1894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 if (!quiet)
1896 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001897 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001899 }
1900 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 lp->ll_list = NULL;
1902 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001903 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001906 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001911 if (len == -1)
1912 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001914 }
1915 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001917 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001919 if (len == -1)
1920 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001922 p = NULL;
1923 break;
1924 }
1925 if (len == -1)
1926 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001928 }
1929 else
1930 {
1931 /*
1932 * Get the number and item for the only or first index of the List.
1933 */
1934 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001936 else
1937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001939 clear_tv(&var1);
1940 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 lp->ll_list = lp->ll_tv->vval.v_list;
1943 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1944 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 if (!quiet)
1947 EMSGN(_(e_listidx), lp->ll_n1);
1948 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001949 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001951 }
1952
1953 /*
1954 * May need to find the item or absolute index for the second
1955 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 * When no index given: "lp->ll_empty2" is TRUE.
1957 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001958 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 if (ni == NULL)
1967 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 if (!quiet)
1969 EMSGN(_(e_listidx), lp->ll_n2);
1970 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 }
1974
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
1976 if (lp->ll_n1 < 0)
1977 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1978 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 if (!quiet)
1981 EMSGN(_(e_listidx), lp->ll_n2);
1982 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001984 }
1985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 }
1989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 return p;
1991}
1992
1993/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001994 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 */
1996 static void
1997clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00001998 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999{
2000 vim_free(lp->ll_exp_name);
2001 vim_free(lp->ll_newkey);
2002}
2003
2004/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002005 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002007 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 */
2009 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002011 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002013 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016{
2017 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002018 listitem_T *ni;
2019 listitem_T *ri;
2020 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021
2022 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 cc = *endp;
2027 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 if (op != NULL && *op != '=')
2029 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002032 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2034 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 {
2036 if (tv_op(&tv, rettv, op) == OK)
2037 set_var(lp->ll_name, &tv, FALSE);
2038 clear_tv(&tv);
2039 }
2040 }
2041 else
2042 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002046 else if (tv_check_lock(lp->ll_newkey == NULL
2047 ? lp->ll_tv->v_lock
2048 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2049 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 else if (lp->ll_range)
2051 {
2052 /*
2053 * Assign the List values to the list items.
2054 */
2055 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002056 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002057 if (op != NULL && *op != '=')
2058 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2059 else
2060 {
2061 clear_tv(&lp->ll_li->li_tv);
2062 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2063 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 ri = ri->li_next;
2065 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2066 break;
2067 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002068 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 /* Need to add an empty item. */
2070 ni = listitem_alloc();
2071 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 ri = NULL;
2074 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002075 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002077 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 ni->li_tv.vval.v_number = 0;
2079 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002080 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 lp->ll_li = lp->ll_li->li_next;
2082 ++lp->ll_n1;
2083 }
2084 if (ri != NULL)
2085 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 else if (lp->ll_empty2
2087 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 : lp->ll_n1 != lp->ll_n2)
2089 EMSG(_("E711: List value has not enough items"));
2090 }
2091 else
2092 {
2093 /*
2094 * Assign to a List or Dictionary item.
2095 */
2096 if (lp->ll_newkey != NULL)
2097 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 if (op != NULL && *op != '=')
2099 {
2100 EMSG2(_(e_letwrong), op);
2101 return;
2102 }
2103
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002104 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002105 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 if (di == NULL)
2107 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002108 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2109 {
2110 vim_free(di);
2111 return;
2112 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002114 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115 else if (op != NULL && *op != '=')
2116 {
2117 tv_op(lp->ll_tv, rettv, op);
2118 return;
2119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 /*
2124 * Assign the value to the variable or list item.
2125 */
2126 if (copy)
2127 copy_tv(rettv, lp->ll_tv);
2128 else
2129 {
2130 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002131 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 }
2134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135}
2136
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2139 * Returns OK or FAIL.
2140 */
2141 static int
2142tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002143 typval_T *tv1;
2144 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002145 char_u *op;
2146{
2147 long n;
2148 char_u numbuf[NUMBUFLEN];
2149 char_u *s;
2150
2151 /* Can't do anything with a Funcref or a Dict on the right. */
2152 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2153 {
2154 switch (tv1->v_type)
2155 {
2156 case VAR_DICT:
2157 case VAR_FUNC:
2158 break;
2159
2160 case VAR_LIST:
2161 if (*op != '+' || tv2->v_type != VAR_LIST)
2162 break;
2163 /* List += List */
2164 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2165 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2166 return OK;
2167
2168 case VAR_NUMBER:
2169 case VAR_STRING:
2170 if (tv2->v_type == VAR_LIST)
2171 break;
2172 if (*op == '+' || *op == '-')
2173 {
2174 /* nr += nr or nr -= nr*/
2175 n = get_tv_number(tv1);
2176 if (*op == '+')
2177 n += get_tv_number(tv2);
2178 else
2179 n -= get_tv_number(tv2);
2180 clear_tv(tv1);
2181 tv1->v_type = VAR_NUMBER;
2182 tv1->vval.v_number = n;
2183 }
2184 else
2185 {
2186 /* str .= str */
2187 s = get_tv_string(tv1);
2188 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2189 clear_tv(tv1);
2190 tv1->v_type = VAR_STRING;
2191 tv1->vval.v_string = s;
2192 }
2193 return OK;
2194 }
2195 }
2196
2197 EMSG2(_(e_letwrong), op);
2198 return FAIL;
2199}
2200
2201/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002202 * Add a watcher to a list.
2203 */
2204 static void
2205list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 list_T *l;
2207 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002208{
2209 lw->lw_next = l->lv_watch;
2210 l->lv_watch = lw;
2211}
2212
2213/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002214 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002215 * No warning when it isn't found...
2216 */
2217 static void
2218list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002219 list_T *l;
2220 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002221{
Bram Moolenaar33570922005-01-25 22:26:29 +00002222 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002223
2224 lwp = &l->lv_watch;
2225 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2226 {
2227 if (lw == lwrem)
2228 {
2229 *lwp = lw->lw_next;
2230 break;
2231 }
2232 lwp = &lw->lw_next;
2233 }
2234}
2235
2236/*
2237 * Just before removing an item from a list: advance watchers to the next
2238 * item.
2239 */
2240 static void
2241list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002242 list_T *l;
2243 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002244{
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002246
2247 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2248 if (lw->lw_item == item)
2249 lw->lw_item = item->li_next;
2250}
2251
2252/*
2253 * Evaluate the expression used in a ":for var in expr" command.
2254 * "arg" points to "var".
2255 * Set "*errp" to TRUE for an error, FALSE otherwise;
2256 * Return a pointer that holds the info. Null when there is an error.
2257 */
2258 void *
2259eval_for_line(arg, errp, nextcmdp, skip)
2260 char_u *arg;
2261 int *errp;
2262 char_u **nextcmdp;
2263 int skip;
2264{
Bram Moolenaar33570922005-01-25 22:26:29 +00002265 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002266 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002267 typval_T tv;
2268 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002269
2270 *errp = TRUE; /* default: there is an error */
2271
Bram Moolenaar33570922005-01-25 22:26:29 +00002272 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002273 if (fi == NULL)
2274 return NULL;
2275
2276 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2277 if (expr == NULL)
2278 return fi;
2279
2280 expr = skipwhite(expr);
2281 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2282 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002283 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002284 return fi;
2285 }
2286
2287 if (skip)
2288 ++emsg_skip;
2289 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2290 {
2291 *errp = FALSE;
2292 if (!skip)
2293 {
2294 l = tv.vval.v_list;
2295 if (tv.v_type != VAR_LIST || l == NULL)
2296 EMSG(_(e_listreq));
2297 else
2298 {
2299 fi->fi_list = l;
2300 list_add_watch(l, &fi->fi_lw);
2301 fi->fi_lw.lw_item = l->lv_first;
2302 }
2303 }
2304 }
2305 if (skip)
2306 --emsg_skip;
2307
2308 return fi;
2309}
2310
2311/*
2312 * Use the first item in a ":for" list. Advance to the next.
2313 * Assign the values to the variable (list). "arg" points to the first one.
2314 * Return TRUE when a valid item was found, FALSE when at end of list or
2315 * something wrong.
2316 */
2317 int
2318next_for_item(fi_void, arg)
2319 void *fi_void;
2320 char_u *arg;
2321{
Bram Moolenaar33570922005-01-25 22:26:29 +00002322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002323 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002324 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002325
2326 item = fi->fi_lw.lw_item;
2327 if (item == NULL)
2328 result = FALSE;
2329 else
2330 {
2331 fi->fi_lw.lw_item = item->li_next;
2332 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2333 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2334 }
2335 return result;
2336}
2337
2338/*
2339 * Free the structure used to store info used by ":for".
2340 */
2341 void
2342free_for_info(fi_void)
2343 void *fi_void;
2344{
Bram Moolenaar33570922005-01-25 22:26:29 +00002345 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002347 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002348 list_rem_watch(fi->fi_list, &fi->fi_lw);
2349 vim_free(fi);
2350}
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2353
2354 void
2355set_context_for_expression(xp, arg, cmdidx)
2356 expand_T *xp;
2357 char_u *arg;
2358 cmdidx_T cmdidx;
2359{
2360 int got_eq = FALSE;
2361 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364 if (cmdidx == CMD_let)
2365 {
2366 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002367 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 {
2369 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002370 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002371 {
2372 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002373 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (vim_iswhite(*p))
2375 break;
2376 }
2377 return;
2378 }
2379 }
2380 else
2381 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2382 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 while ((xp->xp_pattern = vim_strpbrk(arg,
2384 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2385 {
2386 c = *xp->xp_pattern;
2387 if (c == '&')
2388 {
2389 c = xp->xp_pattern[1];
2390 if (c == '&')
2391 {
2392 ++xp->xp_pattern;
2393 xp->xp_context = cmdidx != CMD_let || got_eq
2394 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2395 }
2396 else if (c != ' ')
2397 xp->xp_context = EXPAND_SETTINGS;
2398 }
2399 else if (c == '$')
2400 {
2401 /* environment variable */
2402 xp->xp_context = EXPAND_ENV_VARS;
2403 }
2404 else if (c == '=')
2405 {
2406 got_eq = TRUE;
2407 xp->xp_context = EXPAND_EXPRESSION;
2408 }
2409 else if (c == '<'
2410 && xp->xp_context == EXPAND_FUNCTIONS
2411 && vim_strchr(xp->xp_pattern, '(') == NULL)
2412 {
2413 /* Function name can start with "<SNR>" */
2414 break;
2415 }
2416 else if (cmdidx != CMD_let || got_eq)
2417 {
2418 if (c == '"') /* string */
2419 {
2420 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2421 if (c == '\\' && xp->xp_pattern[1] != NUL)
2422 ++xp->xp_pattern;
2423 xp->xp_context = EXPAND_NOTHING;
2424 }
2425 else if (c == '\'') /* literal string */
2426 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2429 /* skip */ ;
2430 xp->xp_context = EXPAND_NOTHING;
2431 }
2432 else if (c == '|')
2433 {
2434 if (xp->xp_pattern[1] == '|')
2435 {
2436 ++xp->xp_pattern;
2437 xp->xp_context = EXPAND_EXPRESSION;
2438 }
2439 else
2440 xp->xp_context = EXPAND_COMMANDS;
2441 }
2442 else
2443 xp->xp_context = EXPAND_EXPRESSION;
2444 }
2445 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002446 /* Doesn't look like something valid, expand as an expression
2447 * anyway. */
2448 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 arg = xp->xp_pattern;
2450 if (*arg != NUL)
2451 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2452 /* skip */ ;
2453 }
2454 xp->xp_pattern = arg;
2455}
2456
2457#endif /* FEAT_CMDL_COMPL */
2458
2459/*
2460 * ":1,25call func(arg1, arg2)" function call.
2461 */
2462 void
2463ex_call(eap)
2464 exarg_T *eap;
2465{
2466 char_u *arg = eap->arg;
2467 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 linenr_T lnum;
2473 int doesrange;
2474 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002475 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002477 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2478 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 if (tofree == NULL)
2480 return;
2481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002482 /* Increase refcount on dictionary, it could get deleted when evaluating
2483 * the arguments. */
2484 if (fudi.fd_dict != NULL)
2485 ++fudi.fd_dict->dv_refcount;
2486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2488 len = STRLEN(tofree);
2489 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar532c7802005-01-27 14:44:31 +00002491 /* Skip white space to allow ":call func ()". Not good, but required for
2492 * backward compatibility. */
2493 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
2496 if (*startarg != '(')
2497 {
2498 EMSG2(_("E107: Missing braces: %s"), name);
2499 goto end;
2500 }
2501
2502 /*
2503 * When skipping, evaluate the function once, to find the end of the
2504 * arguments.
2505 * When the function takes a range, this is discovered after the first
2506 * call, and the loop is broken.
2507 */
2508 if (eap->skip)
2509 {
2510 ++emsg_skip;
2511 lnum = eap->line2; /* do it once, also with an invalid range */
2512 }
2513 else
2514 lnum = eap->line1;
2515 for ( ; lnum <= eap->line2; ++lnum)
2516 {
2517 if (!eap->skip && eap->addr_count > 0)
2518 {
2519 curwin->w_cursor.lnum = lnum;
2520 curwin->w_cursor.col = 0;
2521 }
2522 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002524 eap->line1, eap->line2, &doesrange,
2525 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 failed = TRUE;
2528 break;
2529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (doesrange || eap->skip)
2532 break;
2533 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002534 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002536 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (aborting())
2538 break;
2539 }
2540 if (eap->skip)
2541 --emsg_skip;
2542
2543 if (!failed)
2544 {
2545 /* Check for trailing illegal characters and a following command. */
2546 if (!ends_excmd(*arg))
2547 {
2548 emsg_severe = TRUE;
2549 EMSG(_(e_trailing));
2550 }
2551 else
2552 eap->nextcmd = check_nextcmd(arg);
2553 }
2554
2555end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002556 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558}
2559
2560/*
2561 * ":unlet[!] var1 ... " command.
2562 */
2563 void
2564ex_unlet(eap)
2565 exarg_T *eap;
2566{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002567 ex_unletlock(eap, eap->arg, 0);
2568}
2569
2570/*
2571 * ":lockvar" and ":unlockvar" commands
2572 */
2573 void
2574ex_lockvar(eap)
2575 exarg_T *eap;
2576{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002578 int deep = 2;
2579
2580 if (eap->forceit)
2581 deep = -1;
2582 else if (vim_isdigit(*arg))
2583 {
2584 deep = getdigits(&arg);
2585 arg = skipwhite(arg);
2586 }
2587
2588 ex_unletlock(eap, arg, deep);
2589}
2590
2591/*
2592 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2593 */
2594 static void
2595ex_unletlock(eap, argstart, deep)
2596 exarg_T *eap;
2597 char_u *argstart;
2598 int deep;
2599{
2600 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002603 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 do
2606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 /* Parse the name and find the end. */
2608 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2609 if (lv.ll_name == NULL)
2610 error = TRUE; /* error but continue parsing */
2611 if (name_end == NULL || (!vim_iswhite(*name_end)
2612 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (name_end != NULL)
2615 {
2616 emsg_severe = TRUE;
2617 EMSG(_(e_trailing));
2618 }
2619 if (!(eap->skip || error))
2620 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 break;
2622 }
2623
2624 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002625 {
2626 if (eap->cmdidx == CMD_unlet)
2627 {
2628 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2629 error = TRUE;
2630 }
2631 else
2632 {
2633 if (do_lock_var(&lv, name_end, deep,
2634 eap->cmdidx == CMD_lockvar) == FAIL)
2635 error = TRUE;
2636 }
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!eap->skip)
2640 clear_lval(&lv);
2641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 arg = skipwhite(name_end);
2643 } while (!ends_excmd(*arg));
2644
2645 eap->nextcmd = check_nextcmd(arg);
2646}
2647
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 int forceit;
2653{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 int ret = OK;
2655 int cc;
2656
2657 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 cc = *name_end;
2660 *name_end = NUL;
2661
2662 /* Normal name or expanded name. */
2663 if (check_changedtick(lp->ll_name))
2664 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002665 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002669 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2670 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 else if (lp->ll_range)
2672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674
2675 /* Delete a range of List items. */
2676 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2677 {
2678 li = lp->ll_li->li_next;
2679 listitem_remove(lp->ll_list, lp->ll_li);
2680 lp->ll_li = li;
2681 ++lp->ll_n1;
2682 }
2683 }
2684 else
2685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 /* unlet a List item. */
2688 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002691 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 }
2693
2694 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695}
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002699 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 */
2701 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002702do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002704 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
Bram Moolenaar33570922005-01-25 22:26:29 +00002706 hashtab_T *ht;
2707 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002708 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
Bram Moolenaar33570922005-01-25 22:26:29 +00002710 ht = find_var_ht(name, &varname);
2711 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002713 hi = hash_find(ht, varname);
2714 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002715 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002716 if (var_check_ro(HI2DI(hi)->di_flags, name))
2717 return FAIL;
2718 delete_var(ht, hi);
2719 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002722 if (forceit)
2723 return OK;
2724 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 return FAIL;
2726}
2727
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002728/*
2729 * Lock or unlock variable indicated by "lp".
2730 * "deep" is the levels to go (-1 for unlimited);
2731 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2732 */
2733 static int
2734do_lock_var(lp, name_end, deep, lock)
2735 lval_T *lp;
2736 char_u *name_end;
2737 int deep;
2738 int lock;
2739{
2740 int ret = OK;
2741 int cc;
2742 dictitem_T *di;
2743
2744 if (deep == 0) /* nothing to do */
2745 return OK;
2746
2747 if (lp->ll_tv == NULL)
2748 {
2749 cc = *name_end;
2750 *name_end = NUL;
2751
2752 /* Normal name or expanded name. */
2753 if (check_changedtick(lp->ll_name))
2754 ret = FAIL;
2755 else
2756 {
2757 di = find_var(lp->ll_name, NULL);
2758 if (di == NULL)
2759 ret = FAIL;
2760 else
2761 {
2762 if (lock)
2763 di->di_flags |= DI_FLAGS_LOCK;
2764 else
2765 di->di_flags &= ~DI_FLAGS_LOCK;
2766 item_lock(&di->di_tv, deep, lock);
2767 }
2768 }
2769 *name_end = cc;
2770 }
2771 else if (lp->ll_range)
2772 {
2773 listitem_T *li = lp->ll_li;
2774
2775 /* (un)lock a range of List items. */
2776 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2777 {
2778 item_lock(&li->li_tv, deep, lock);
2779 li = li->li_next;
2780 ++lp->ll_n1;
2781 }
2782 }
2783 else if (lp->ll_list != NULL)
2784 /* (un)lock a List item. */
2785 item_lock(&lp->ll_li->li_tv, deep, lock);
2786 else
2787 /* un(lock) a Dictionary item. */
2788 item_lock(&lp->ll_di->di_tv, deep, lock);
2789
2790 return ret;
2791}
2792
2793/*
2794 * Lock or unlock an item. "deep" is nr of levels to go.
2795 */
2796 static void
2797item_lock(tv, deep, lock)
2798 typval_T *tv;
2799 int deep;
2800 int lock;
2801{
2802 static int recurse = 0;
2803 list_T *l;
2804 listitem_T *li;
2805 dict_T *d;
2806 hashitem_T *hi;
2807 int todo;
2808
2809 if (recurse >= DICT_MAXNEST)
2810 {
2811 EMSG(_("E743: variable nested too deep for (un)lock"));
2812 return;
2813 }
2814 if (deep == 0)
2815 return;
2816 ++recurse;
2817
2818 /* lock/unlock the item itself */
2819 if (lock)
2820 tv->v_lock |= VAR_LOCKED;
2821 else
2822 tv->v_lock &= ~VAR_LOCKED;
2823
2824 switch (tv->v_type)
2825 {
2826 case VAR_LIST:
2827 if ((l = tv->vval.v_list) != NULL)
2828 {
2829 if (lock)
2830 l->lv_lock |= VAR_LOCKED;
2831 else
2832 l->lv_lock &= ~VAR_LOCKED;
2833 if (deep < 0 || deep > 1)
2834 /* recursive: lock/unlock the items the List contains */
2835 for (li = l->lv_first; li != NULL; li = li->li_next)
2836 item_lock(&li->li_tv, deep - 1, lock);
2837 }
2838 break;
2839 case VAR_DICT:
2840 if ((d = tv->vval.v_dict) != NULL)
2841 {
2842 if (lock)
2843 d->dv_lock |= VAR_LOCKED;
2844 else
2845 d->dv_lock &= ~VAR_LOCKED;
2846 if (deep < 0 || deep > 1)
2847 {
2848 /* recursive: lock/unlock the items the List contains */
2849 todo = d->dv_hashtab.ht_used;
2850 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2851 {
2852 if (!HASHITEM_EMPTY(hi))
2853 {
2854 --todo;
2855 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2856 }
2857 }
2858 }
2859 }
2860 }
2861 --recurse;
2862}
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2865/*
2866 * Delete all "menutrans_" variables.
2867 */
2868 void
2869del_menutrans_vars()
2870{
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002872 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 hash_lock(&globvarht);
2875 todo = globvarht.ht_used;
2876 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00002877 {
2878 if (!HASHITEM_EMPTY(hi))
2879 {
2880 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2882 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00002883 }
2884 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886}
2887#endif
2888
2889#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2890
2891/*
2892 * Local string buffer for the next two functions to store a variable name
2893 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2894 * get_user_var_name().
2895 */
2896
2897static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2898
2899static char_u *varnamebuf = NULL;
2900static int varnamebuflen = 0;
2901
2902/*
2903 * Function to concatenate a prefix and a variable name.
2904 */
2905 static char_u *
2906cat_prefix_varname(prefix, name)
2907 int prefix;
2908 char_u *name;
2909{
2910 int len;
2911
2912 len = (int)STRLEN(name) + 3;
2913 if (len > varnamebuflen)
2914 {
2915 vim_free(varnamebuf);
2916 len += 10; /* some additional space */
2917 varnamebuf = alloc(len);
2918 if (varnamebuf == NULL)
2919 {
2920 varnamebuflen = 0;
2921 return NULL;
2922 }
2923 varnamebuflen = len;
2924 }
2925 *varnamebuf = prefix;
2926 varnamebuf[1] = ':';
2927 STRCPY(varnamebuf + 2, name);
2928 return varnamebuf;
2929}
2930
2931/*
2932 * Function given to ExpandGeneric() to obtain the list of user defined
2933 * (global/buffer/window/built-in) variable names.
2934 */
2935/*ARGSUSED*/
2936 char_u *
2937get_user_var_name(xp, idx)
2938 expand_T *xp;
2939 int idx;
2940{
Bram Moolenaar532c7802005-01-27 14:44:31 +00002941 static long_u gdone;
2942 static long_u bdone;
2943 static long_u wdone;
2944 static int vidx;
2945 static hashitem_T *hi;
2946 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00002949 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00002950
2951 /* Global variables */
2952 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002954 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00002956 else
2957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002958 while (HASHITEM_EMPTY(hi))
2959 ++hi;
2960 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2961 return cat_prefix_varname('g', hi->hi_key);
2962 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002964
2965 /* b: variables */
2966 ht = &curbuf->b_vars.dv_hashtab;
2967 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002969 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002970 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00002971 else
2972 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002973 while (HASHITEM_EMPTY(hi))
2974 ++hi;
2975 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002979 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 return (char_u *)"b:changedtick";
2981 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002982
2983 /* w: variables */
2984 ht = &curwin->w_vars.dv_hashtab;
2985 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002987 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00002989 else
2990 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002991 while (HASHITEM_EMPTY(hi))
2992 ++hi;
2993 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002995
2996 /* v: variables */
2997 if (vidx < VV_LEN)
2998 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 vim_free(varnamebuf);
3001 varnamebuf = NULL;
3002 varnamebuflen = 0;
3003 return NULL;
3004}
3005
3006#endif /* FEAT_CMDL_COMPL */
3007
3008/*
3009 * types for expressions.
3010 */
3011typedef enum
3012{
3013 TYPE_UNKNOWN = 0
3014 , TYPE_EQUAL /* == */
3015 , TYPE_NEQUAL /* != */
3016 , TYPE_GREATER /* > */
3017 , TYPE_GEQUAL /* >= */
3018 , TYPE_SMALLER /* < */
3019 , TYPE_SEQUAL /* <= */
3020 , TYPE_MATCH /* =~ */
3021 , TYPE_NOMATCH /* !~ */
3022} exptype_T;
3023
3024/*
3025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3028 */
3029
3030/*
3031 * Handle zero level expression.
3032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 * Return OK or FAIL.
3035 */
3036 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003037eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 char_u **nextcmd;
3041 int evaluate;
3042{
3043 int ret;
3044 char_u *p;
3045
3046 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003047 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (ret == FAIL || !ends_excmd(*p))
3049 {
3050 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 /*
3053 * Report the invalid expression unless the expression evaluation has
3054 * been cancelled due to an aborting error, an interrupt, or an
3055 * exception.
3056 */
3057 if (!aborting())
3058 EMSG2(_(e_invexpr2), arg);
3059 ret = FAIL;
3060 }
3061 if (nextcmd != NULL)
3062 *nextcmd = check_nextcmd(p);
3063
3064 return ret;
3065}
3066
3067/*
3068 * Handle top level expression:
3069 * expr1 ? expr0 : expr0
3070 *
3071 * "arg" must point to the first non-white of the expression.
3072 * "arg" is advanced to the next non-white after the recognized expression.
3073 *
3074 * Return OK or FAIL.
3075 */
3076 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 int evaluate;
3081{
3082 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085 /*
3086 * Get the first variable.
3087 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003088 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 return FAIL;
3090
3091 if ((*arg)[0] == '?')
3092 {
3093 result = FALSE;
3094 if (evaluate)
3095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003096 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100
3101 /*
3102 * Get the second variable.
3103 */
3104 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return FAIL;
3107
3108 /*
3109 * Check for the ":".
3110 */
3111 if ((*arg)[0] != ':')
3112 {
3113 EMSG(_("E109: Missing ':' after '?'"));
3114 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003115 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 return FAIL;
3117 }
3118
3119 /*
3120 * Get the third variable.
3121 */
3122 *arg = skipwhite(*arg + 1);
3123 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3124 {
3125 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003126 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return FAIL;
3128 }
3129 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003130 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
3132
3133 return OK;
3134}
3135
3136/*
3137 * Handle first level expression:
3138 * expr2 || expr2 || expr2 logical OR
3139 *
3140 * "arg" must point to the first non-white of the expression.
3141 * "arg" is advanced to the next non-white after the recognized expression.
3142 *
3143 * Return OK or FAIL.
3144 */
3145 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 int evaluate;
3150{
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 long result;
3153 int first;
3154
3155 /*
3156 * Get the first variable.
3157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003158 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return FAIL;
3160
3161 /*
3162 * Repeat until there is no following "||".
3163 */
3164 first = TRUE;
3165 result = FALSE;
3166 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3167 {
3168 if (evaluate && first)
3169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003170 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003172 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 first = FALSE;
3174 }
3175
3176 /*
3177 * Get the second variable.
3178 */
3179 *arg = skipwhite(*arg + 2);
3180 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3181 return FAIL;
3182
3183 /*
3184 * Compute the result.
3185 */
3186 if (evaluate && !result)
3187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003188 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003190 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 if (evaluate)
3193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 rettv->v_type = VAR_NUMBER;
3195 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197 }
3198
3199 return OK;
3200}
3201
3202/*
3203 * Handle second level expression:
3204 * expr3 && expr3 && expr3 logical AND
3205 *
3206 * "arg" must point to the first non-white of the expression.
3207 * "arg" is advanced to the next non-white after the recognized expression.
3208 *
3209 * Return OK or FAIL.
3210 */
3211 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003212eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int evaluate;
3216{
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 long result;
3219 int first;
3220
3221 /*
3222 * Get the first variable.
3223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003224 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 return FAIL;
3226
3227 /*
3228 * Repeat until there is no following "&&".
3229 */
3230 first = TRUE;
3231 result = TRUE;
3232 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3233 {
3234 if (evaluate && first)
3235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003238 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 first = FALSE;
3240 }
3241
3242 /*
3243 * Get the second variable.
3244 */
3245 *arg = skipwhite(*arg + 2);
3246 if (eval4(arg, &var2, evaluate && result) == FAIL)
3247 return FAIL;
3248
3249 /*
3250 * Compute the result.
3251 */
3252 if (evaluate && result)
3253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003254 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003256 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 if (evaluate)
3259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003260 rettv->v_type = VAR_NUMBER;
3261 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 }
3264
3265 return OK;
3266}
3267
3268/*
3269 * Handle third level expression:
3270 * var1 == var2
3271 * var1 =~ var2
3272 * var1 != var2
3273 * var1 !~ var2
3274 * var1 > var2
3275 * var1 >= var2
3276 * var1 < var2
3277 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003278 * var1 is var2
3279 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 *
3281 * "arg" must point to the first non-white of the expression.
3282 * "arg" is advanced to the next non-white after the recognized expression.
3283 *
3284 * Return OK or FAIL.
3285 */
3286 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 int evaluate;
3291{
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 char_u *p;
3294 int i;
3295 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003296 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 int len = 2;
3298 long n1, n2;
3299 char_u *s1, *s2;
3300 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3301 regmatch_T regmatch;
3302 int ic;
3303 char_u *save_cpo;
3304
3305 /*
3306 * Get the first variable.
3307 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003308 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 return FAIL;
3310
3311 p = *arg;
3312 switch (p[0])
3313 {
3314 case '=': if (p[1] == '=')
3315 type = TYPE_EQUAL;
3316 else if (p[1] == '~')
3317 type = TYPE_MATCH;
3318 break;
3319 case '!': if (p[1] == '=')
3320 type = TYPE_NEQUAL;
3321 else if (p[1] == '~')
3322 type = TYPE_NOMATCH;
3323 break;
3324 case '>': if (p[1] != '=')
3325 {
3326 type = TYPE_GREATER;
3327 len = 1;
3328 }
3329 else
3330 type = TYPE_GEQUAL;
3331 break;
3332 case '<': if (p[1] != '=')
3333 {
3334 type = TYPE_SMALLER;
3335 len = 1;
3336 }
3337 else
3338 type = TYPE_SEQUAL;
3339 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003340 case 'i': if (p[1] == 's')
3341 {
3342 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3343 len = 5;
3344 if (!vim_isIDc(p[len]))
3345 {
3346 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3347 type_is = TRUE;
3348 }
3349 }
3350 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
3352
3353 /*
3354 * If there is a comparitive operator, use it.
3355 */
3356 if (type != TYPE_UNKNOWN)
3357 {
3358 /* extra question mark appended: ignore case */
3359 if (p[len] == '?')
3360 {
3361 ic = TRUE;
3362 ++len;
3363 }
3364 /* extra '#' appended: match case */
3365 else if (p[len] == '#')
3366 {
3367 ic = FALSE;
3368 ++len;
3369 }
3370 /* nothing appened: use 'ignorecase' */
3371 else
3372 ic = p_ic;
3373
3374 /*
3375 * Get the second variable.
3376 */
3377 *arg = skipwhite(p + len);
3378 if (eval5(arg, &var2, evaluate) == FAIL)
3379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 return FAIL;
3382 }
3383
3384 if (evaluate)
3385 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003386 if (type_is && rettv->v_type != var2.v_type)
3387 {
3388 /* For "is" a different type always means FALSE, for "notis"
3389 * it means TRUE. */
3390 n1 = (type == TYPE_NEQUAL);
3391 }
3392 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3393 {
3394 if (type_is)
3395 {
3396 n1 = (rettv->v_type == var2.v_type
3397 && rettv->vval.v_list == var2.vval.v_list);
3398 if (type == TYPE_NEQUAL)
3399 n1 = !n1;
3400 }
3401 else if (rettv->v_type != var2.v_type
3402 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3403 {
3404 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003405 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003406 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003407 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003408 clear_tv(rettv);
3409 clear_tv(&var2);
3410 return FAIL;
3411 }
3412 else
3413 {
3414 /* Compare two Lists for being equal or unequal. */
3415 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3416 if (type == TYPE_NEQUAL)
3417 n1 = !n1;
3418 }
3419 }
3420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3422 {
3423 if (type_is)
3424 {
3425 n1 = (rettv->v_type == var2.v_type
3426 && rettv->vval.v_dict == var2.vval.v_dict);
3427 if (type == TYPE_NEQUAL)
3428 n1 = !n1;
3429 }
3430 else if (rettv->v_type != var2.v_type
3431 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3432 {
3433 if (rettv->v_type != var2.v_type)
3434 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3435 else
3436 EMSG(_("E736: Invalid operation for Dictionary"));
3437 clear_tv(rettv);
3438 clear_tv(&var2);
3439 return FAIL;
3440 }
3441 else
3442 {
3443 /* Compare two Dictionaries for being equal or unequal. */
3444 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3445 if (type == TYPE_NEQUAL)
3446 n1 = !n1;
3447 }
3448 }
3449
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003450 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3451 {
3452 if (rettv->v_type != var2.v_type
3453 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3454 {
3455 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003456 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003457 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003458 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003459 clear_tv(rettv);
3460 clear_tv(&var2);
3461 return FAIL;
3462 }
3463 else
3464 {
3465 /* Compare two Funcrefs for being equal or unequal. */
3466 if (rettv->vval.v_string == NULL
3467 || var2.vval.v_string == NULL)
3468 n1 = FALSE;
3469 else
3470 n1 = STRCMP(rettv->vval.v_string,
3471 var2.vval.v_string) == 0;
3472 if (type == TYPE_NEQUAL)
3473 n1 = !n1;
3474 }
3475 }
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /*
3478 * If one of the two variables is a number, compare as a number.
3479 * When using "=~" or "!~", always compare as string.
3480 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003481 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 n1 = get_tv_number(rettv);
3485 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 switch (type)
3487 {
3488 case TYPE_EQUAL: n1 = (n1 == n2); break;
3489 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3490 case TYPE_GREATER: n1 = (n1 > n2); break;
3491 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3492 case TYPE_SMALLER: n1 = (n1 < n2); break;
3493 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3494 case TYPE_UNKNOWN:
3495 case TYPE_MATCH:
3496 case TYPE_NOMATCH: break; /* avoid gcc warning */
3497 }
3498 }
3499 else
3500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 s1 = get_tv_string_buf(rettv, buf1);
3502 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3504 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3505 else
3506 i = 0;
3507 n1 = FALSE;
3508 switch (type)
3509 {
3510 case TYPE_EQUAL: n1 = (i == 0); break;
3511 case TYPE_NEQUAL: n1 = (i != 0); break;
3512 case TYPE_GREATER: n1 = (i > 0); break;
3513 case TYPE_GEQUAL: n1 = (i >= 0); break;
3514 case TYPE_SMALLER: n1 = (i < 0); break;
3515 case TYPE_SEQUAL: n1 = (i <= 0); break;
3516
3517 case TYPE_MATCH:
3518 case TYPE_NOMATCH:
3519 /* avoid 'l' flag in 'cpoptions' */
3520 save_cpo = p_cpo;
3521 p_cpo = (char_u *)"";
3522 regmatch.regprog = vim_regcomp(s2,
3523 RE_MAGIC + RE_STRING);
3524 regmatch.rm_ic = ic;
3525 if (regmatch.regprog != NULL)
3526 {
3527 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3528 vim_free(regmatch.regprog);
3529 if (type == TYPE_NOMATCH)
3530 n1 = !n1;
3531 }
3532 p_cpo = save_cpo;
3533 break;
3534
3535 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3536 }
3537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003538 clear_tv(rettv);
3539 clear_tv(&var2);
3540 rettv->v_type = VAR_NUMBER;
3541 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543 }
3544
3545 return OK;
3546}
3547
3548/*
3549 * Handle fourth level expression:
3550 * + number addition
3551 * - number subtraction
3552 * . string concatenation
3553 *
3554 * "arg" must point to the first non-white of the expression.
3555 * "arg" is advanced to the next non-white after the recognized expression.
3556 *
3557 * Return OK or FAIL.
3558 */
3559 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 int evaluate;
3564{
Bram Moolenaar33570922005-01-25 22:26:29 +00003565 typval_T var2;
3566 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 int op;
3568 long n1, n2;
3569 char_u *s1, *s2;
3570 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3571 char_u *p;
3572
3573 /*
3574 * Get the first variable.
3575 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003576 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 return FAIL;
3578
3579 /*
3580 * Repeat computing, until no '+', '-' or '.' is following.
3581 */
3582 for (;;)
3583 {
3584 op = **arg;
3585 if (op != '+' && op != '-' && op != '.')
3586 break;
3587
3588 /*
3589 * Get the second variable.
3590 */
3591 *arg = skipwhite(*arg + 1);
3592 if (eval6(arg, &var2, evaluate) == FAIL)
3593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 return FAIL;
3596 }
3597
3598 if (evaluate)
3599 {
3600 /*
3601 * Compute the result.
3602 */
3603 if (op == '.')
3604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003605 s1 = get_tv_string_buf(rettv, buf1);
3606 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003607 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 clear_tv(rettv);
3609 rettv->v_type = VAR_STRING;
3610 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003612 else if (op == '+' && rettv->v_type == VAR_LIST
3613 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003614 {
3615 /* concatenate Lists */
3616 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3617 &var3) == FAIL)
3618 {
3619 clear_tv(rettv);
3620 clear_tv(&var2);
3621 return FAIL;
3622 }
3623 clear_tv(rettv);
3624 *rettv = var3;
3625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 else
3627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003628 n1 = get_tv_number(rettv);
3629 n2 = get_tv_number(&var2);
3630 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (op == '+')
3632 n1 = n1 + n2;
3633 else
3634 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003635 rettv->v_type = VAR_NUMBER;
3636 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 }
3641 return OK;
3642}
3643
3644/*
3645 * Handle fifth level expression:
3646 * * number multiplication
3647 * / number division
3648 * % number modulo
3649 *
3650 * "arg" must point to the first non-white of the expression.
3651 * "arg" is advanced to the next non-white after the recognized expression.
3652 *
3653 * Return OK or FAIL.
3654 */
3655 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003656eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 int evaluate;
3660{
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int op;
3663 long n1, n2;
3664
3665 /*
3666 * Get the first variable.
3667 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003668 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 return FAIL;
3670
3671 /*
3672 * Repeat computing, until no '*', '/' or '%' is following.
3673 */
3674 for (;;)
3675 {
3676 op = **arg;
3677 if (op != '*' && op != '/' && op != '%')
3678 break;
3679
3680 if (evaluate)
3681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 n1 = get_tv_number(rettv);
3683 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 else
3686 n1 = 0;
3687
3688 /*
3689 * Get the second variable.
3690 */
3691 *arg = skipwhite(*arg + 1);
3692 if (eval7(arg, &var2, evaluate) == FAIL)
3693 return FAIL;
3694
3695 if (evaluate)
3696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 n2 = get_tv_number(&var2);
3698 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 /*
3701 * Compute the result.
3702 */
3703 if (op == '*')
3704 n1 = n1 * n2;
3705 else if (op == '/')
3706 {
3707 if (n2 == 0) /* give an error message? */
3708 n1 = 0x7fffffffL;
3709 else
3710 n1 = n1 / n2;
3711 }
3712 else
3713 {
3714 if (n2 == 0) /* give an error message? */
3715 n1 = 0;
3716 else
3717 n1 = n1 % n2;
3718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719 rettv->v_type = VAR_NUMBER;
3720 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 }
3723
3724 return OK;
3725}
3726
3727/*
3728 * Handle sixth level expression:
3729 * number number constant
3730 * "string" string contstant
3731 * 'string' literal string contstant
3732 * &option-name option value
3733 * @r register contents
3734 * identifier variable value
3735 * function() function call
3736 * $VAR environment variable
3737 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 * [expr, expr] List
3739 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 *
3741 * Also handle:
3742 * ! in front logical NOT
3743 * - in front unary minus
3744 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003745 * trailing [] subscript in String or List
3746 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 *
3748 * "arg" must point to the first non-white of the expression.
3749 * "arg" is advanced to the next non-white after the recognized expression.
3750 *
3751 * Return OK or FAIL.
3752 */
3753 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003754eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 int evaluate;
3758{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 long n;
3760 int len;
3761 char_u *s;
3762 int val;
3763 char_u *start_leader, *end_leader;
3764 int ret = OK;
3765 char_u *alias;
3766
3767 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003769 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * Skip '!' and '-' characters. They are handled later.
3775 */
3776 start_leader = *arg;
3777 while (**arg == '!' || **arg == '-' || **arg == '+')
3778 *arg = skipwhite(*arg + 1);
3779 end_leader = *arg;
3780
3781 switch (**arg)
3782 {
3783 /*
3784 * Number constant.
3785 */
3786 case '0':
3787 case '1':
3788 case '2':
3789 case '3':
3790 case '4':
3791 case '5':
3792 case '6':
3793 case '7':
3794 case '8':
3795 case '9':
3796 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3797 *arg += len;
3798 if (evaluate)
3799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 rettv->v_type = VAR_NUMBER;
3801 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 break;
3804
3805 /*
3806 * String constant: "string".
3807 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 break;
3810
3811 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003812 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003815 break;
3816
3817 /*
3818 * List: [expr, expr]
3819 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 break;
3822
3823 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824 * Dictionary: {key: val, key: val}
3825 */
3826 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3827 break;
3828
3829 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003830 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003832 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 break;
3834
3835 /*
3836 * Environment variable: $VAR.
3837 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 break;
3840
3841 /*
3842 * Register contents: @r.
3843 */
3844 case '@': ++*arg;
3845 if (evaluate)
3846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 rettv->v_type = VAR_STRING;
3848 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850 if (**arg != NUL)
3851 ++*arg;
3852 break;
3853
3854 /*
3855 * nested expression: (expression).
3856 */
3857 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (**arg == ')')
3860 ++*arg;
3861 else if (ret == OK)
3862 {
3863 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 ret = FAIL;
3866 }
3867 break;
3868
Bram Moolenaar8c711452005-01-14 21:53:12 +00003869 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003872
3873 if (ret == NOTDONE)
3874 {
3875 /*
3876 * Must be a variable or function name.
3877 * Can also be a curly-braces kind of name: {expr}.
3878 */
3879 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003880 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003881 if (alias != NULL)
3882 s = alias;
3883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003884 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 ret = FAIL;
3886 else
3887 {
3888 if (**arg == '(') /* recursive! */
3889 {
3890 /* If "s" is the name of a variable of type VAR_FUNC
3891 * use its contents. */
3892 s = deref_func_name(s, &len);
3893
3894 /* Invoke the function. */
3895 ret = get_func_tv(s, len, rettv, arg,
3896 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003897 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003898 /* Stop the expression evaluation when immediately
3899 * aborting on error, or when an interrupt occurred or
3900 * an exception was thrown but not caught. */
3901 if (aborting())
3902 {
3903 if (ret == OK)
3904 clear_tv(rettv);
3905 ret = FAIL;
3906 }
3907 }
3908 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003909 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003910 else
3911 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003912 }
3913
3914 if (alias != NULL)
3915 vim_free(alias);
3916 }
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 *arg = skipwhite(*arg);
3919
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003920 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
3921 * expr(expr). */
3922 if (ret == OK)
3923 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 /*
3926 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3927 */
3928 if (ret == OK && evaluate && end_leader > start_leader)
3929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 while (end_leader > start_leader)
3932 {
3933 --end_leader;
3934 if (*end_leader == '!')
3935 val = !val;
3936 else if (*end_leader == '-')
3937 val = -val;
3938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003939 clear_tv(rettv);
3940 rettv->v_type = VAR_NUMBER;
3941 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943
3944 return ret;
3945}
3946
3947/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003948 * Evaluate an "[expr]" or "[expr:expr]" index.
3949 * "*arg" points to the '['.
3950 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3951 */
3952 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003953eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003954 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003956 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003957 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958{
3959 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003962 long len = -1;
3963 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003965 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003969 if (verbose)
3970 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003971 return FAIL;
3972 }
3973
Bram Moolenaar8c711452005-01-14 21:53:12 +00003974 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003976 /*
3977 * dict.name
3978 */
3979 key = *arg + 1;
3980 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3981 ;
3982 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003983 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003984 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003985 }
3986 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003988 /*
3989 * something[idx]
3990 *
3991 * Get the (first) variable from inside the [].
3992 */
3993 *arg = skipwhite(*arg + 1);
3994 if (**arg == ':')
3995 empty1 = TRUE;
3996 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3997 return FAIL;
3998
3999 /*
4000 * Get the second variable from inside the [:].
4001 */
4002 if (**arg == ':')
4003 {
4004 range = TRUE;
4005 *arg = skipwhite(*arg + 1);
4006 if (**arg == ']')
4007 empty2 = TRUE;
4008 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4009 {
4010 clear_tv(&var1);
4011 return FAIL;
4012 }
4013 }
4014
4015 /* Check for the ']'. */
4016 if (**arg != ']')
4017 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004018 if (verbose)
4019 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 clear_tv(&var1);
4021 if (range)
4022 clear_tv(&var2);
4023 return FAIL;
4024 }
4025 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004026 }
4027
4028 if (evaluate)
4029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004030 n1 = 0;
4031 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 n1 = get_tv_number(&var1);
4034 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004035 }
4036 if (range)
4037 {
4038 if (empty2)
4039 n2 = -1;
4040 else
4041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 n2 = get_tv_number(&var2);
4043 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004044 }
4045 }
4046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004048 {
4049 case VAR_NUMBER:
4050 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004052 len = (long)STRLEN(s);
4053 if (range)
4054 {
4055 /* The resulting variable is a substring. If the indexes
4056 * are out of range the result is empty. */
4057 if (n1 < 0)
4058 {
4059 n1 = len + n1;
4060 if (n1 < 0)
4061 n1 = 0;
4062 }
4063 if (n2 < 0)
4064 n2 = len + n2;
4065 else if (n2 >= len)
4066 n2 = len;
4067 if (n1 >= len || n2 < 0 || n1 > n2)
4068 s = NULL;
4069 else
4070 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4071 }
4072 else
4073 {
4074 /* The resulting variable is a string of a single
4075 * character. If the index is too big or negative the
4076 * result is empty. */
4077 if (n1 >= len || n1 < 0)
4078 s = NULL;
4079 else
4080 s = vim_strnsave(s + n1, 1);
4081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 clear_tv(rettv);
4083 rettv->v_type = VAR_STRING;
4084 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004085 break;
4086
4087 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004089 if (n1 < 0)
4090 n1 = len + n1;
4091 if (!empty1 && (n1 < 0 || n1 >= len))
4092 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004093 if (verbose)
4094 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004095 return FAIL;
4096 }
4097 if (range)
4098 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 list_T *l;
4100 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004101
4102 if (n2 < 0)
4103 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004104 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004106 if (verbose)
4107 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004108 return FAIL;
4109 }
4110 l = list_alloc();
4111 if (l == NULL)
4112 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114 n1 <= n2; ++n1)
4115 {
4116 if (list_append_tv(l, &item->li_tv) == FAIL)
4117 {
4118 list_free(l);
4119 return FAIL;
4120 }
4121 item = item->li_next;
4122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
4124 rettv->v_type = VAR_LIST;
4125 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004126 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004127 }
4128 else
4129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
4133 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004134 }
4135 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004136
4137 case VAR_DICT:
4138 if (range)
4139 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004140 if (verbose)
4141 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004142 if (len == -1)
4143 clear_tv(&var1);
4144 return FAIL;
4145 }
4146 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148
4149 if (len == -1)
4150 {
4151 key = get_tv_string(&var1);
4152 if (*key == NUL)
4153 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004154 if (verbose)
4155 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 clear_tv(&var1);
4157 return FAIL;
4158 }
4159 }
4160
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004161 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004162
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004163 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004164 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004165 if (len == -1)
4166 clear_tv(&var1);
4167 if (item == NULL)
4168 return FAIL;
4169
4170 copy_tv(&item->di_tv, &var1);
4171 clear_tv(rettv);
4172 *rettv = var1;
4173 }
4174 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004175 }
4176 }
4177
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004178 return OK;
4179}
4180
4181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 * Get an option value.
4183 * "arg" points to the '&' or '+' before the option name.
4184 * "arg" is advanced to character after the option name.
4185 * Return OK or FAIL.
4186 */
4187 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004190 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 int evaluate;
4192{
4193 char_u *option_end;
4194 long numval;
4195 char_u *stringval;
4196 int opt_type;
4197 int c;
4198 int working = (**arg == '+'); /* has("+option") */
4199 int ret = OK;
4200 int opt_flags;
4201
4202 /*
4203 * Isolate the option name and find its value.
4204 */
4205 option_end = find_option_end(arg, &opt_flags);
4206 if (option_end == NULL)
4207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 EMSG2(_("E112: Option name missing: %s"), *arg);
4210 return FAIL;
4211 }
4212
4213 if (!evaluate)
4214 {
4215 *arg = option_end;
4216 return OK;
4217 }
4218
4219 c = *option_end;
4220 *option_end = NUL;
4221 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 if (opt_type == -3) /* invalid name */
4225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 EMSG2(_("E113: Unknown option: %s"), *arg);
4228 ret = FAIL;
4229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
4232 if (opt_type == -2) /* hidden string option */
4233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 rettv->v_type = VAR_STRING;
4235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 else if (opt_type == -1) /* hidden number option */
4238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 rettv->v_type = VAR_NUMBER;
4240 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 else if (opt_type == 1) /* number option */
4243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 rettv->v_type = VAR_NUMBER;
4245 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else /* string option */
4248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 rettv->v_type = VAR_STRING;
4250 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 }
4253 else if (working && (opt_type == -2 || opt_type == -1))
4254 ret = FAIL;
4255
4256 *option_end = c; /* put back for error messages */
4257 *arg = option_end;
4258
4259 return ret;
4260}
4261
4262/*
4263 * Allocate a variable for a string constant.
4264 * Return OK or FAIL.
4265 */
4266 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 int evaluate;
4271{
4272 char_u *p;
4273 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int extra = 0;
4275
4276 /*
4277 * Find the end of the string, skipping backslashed characters.
4278 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004279 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 if (*p == '\\' && p[1] != NUL)
4282 {
4283 ++p;
4284 /* A "\<x>" form occupies at least 4 characters, and produces up
4285 * to 6 characters: reserve space for 2 extra */
4286 if (*p == '<')
4287 extra += 2;
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 if (*p != '"')
4292 {
4293 EMSG2(_("E114: Missing quote: %s"), *arg);
4294 return FAIL;
4295 }
4296
4297 /* If only parsing, set *arg and return here */
4298 if (!evaluate)
4299 {
4300 *arg = p + 1;
4301 return OK;
4302 }
4303
4304 /*
4305 * Copy the string into allocated memory, handling backslashed
4306 * characters.
4307 */
4308 name = alloc((unsigned)(p - *arg + extra));
4309 if (name == NULL)
4310 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 rettv->v_type = VAR_STRING;
4312 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 if (*p == '\\')
4317 {
4318 switch (*++p)
4319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004320 case 'b': *name++ = BS; ++p; break;
4321 case 'e': *name++ = ESC; ++p; break;
4322 case 'f': *name++ = FF; ++p; break;
4323 case 'n': *name++ = NL; ++p; break;
4324 case 'r': *name++ = CAR; ++p; break;
4325 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
4327 case 'X': /* hex: "\x1", "\x12" */
4328 case 'x':
4329 case 'u': /* Unicode: "\u0023" */
4330 case 'U':
4331 if (vim_isxdigit(p[1]))
4332 {
4333 int n, nr;
4334 int c = toupper(*p);
4335
4336 if (c == 'X')
4337 n = 2;
4338 else
4339 n = 4;
4340 nr = 0;
4341 while (--n >= 0 && vim_isxdigit(p[1]))
4342 {
4343 ++p;
4344 nr = (nr << 4) + hex2nr(*p);
4345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004346 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#ifdef FEAT_MBYTE
4348 /* For "\u" store the number according to
4349 * 'encoding'. */
4350 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004351 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 else
4353#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004354 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 break;
4357
4358 /* octal: "\1", "\12", "\123" */
4359 case '0':
4360 case '1':
4361 case '2':
4362 case '3':
4363 case '4':
4364 case '5':
4365 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004366 case '7': *name = *p++ - '0';
4367 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 *name = (*name << 3) + *p++ - '0';
4370 if (*p >= '0' && *p <= '7')
4371 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 break;
4375
4376 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (extra != 0)
4379 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 break;
4382 }
4383 /* FALLTHROUGH */
4384
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 break;
4387 }
4388 }
4389 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 *arg = p + 1;
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 return OK;
4397}
4398
4399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 * Return OK or FAIL.
4402 */
4403 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 int evaluate;
4408{
4409 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004410 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004411 int reduce = 0;
4412
4413 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004415 */
4416 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004420 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004421 break;
4422 ++reduce;
4423 ++p;
4424 }
4425 }
4426
Bram Moolenaar8c711452005-01-14 21:53:12 +00004427 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004428 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004430 return FAIL;
4431 }
4432
Bram Moolenaar8c711452005-01-14 21:53:12 +00004433 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004434 if (!evaluate)
4435 {
4436 *arg = p + 1;
4437 return OK;
4438 }
4439
4440 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004442 */
4443 str = alloc((unsigned)((p - *arg) - reduce));
4444 if (str == NULL)
4445 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 rettv->v_type = VAR_STRING;
4447 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004448
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004450 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004452 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004454 break;
4455 ++p;
4456 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004458 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004460 *arg = p + 1;
4461
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004462 return OK;
4463}
4464
4465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 * Allocate a variable for a List and fill it from "*arg".
4467 * Return OK or FAIL.
4468 */
4469 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004471 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004472 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 int evaluate;
4474{
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 list_T *l = NULL;
4476 typval_T tv;
4477 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004478
4479 if (evaluate)
4480 {
4481 l = list_alloc();
4482 if (l == NULL)
4483 return FAIL;
4484 }
4485
4486 *arg = skipwhite(*arg + 1);
4487 while (**arg != ']' && **arg != NUL)
4488 {
4489 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4490 goto failret;
4491 if (evaluate)
4492 {
4493 item = listitem_alloc();
4494 if (item != NULL)
4495 {
4496 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004497 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004498 list_append(l, item);
4499 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004500 else
4501 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 }
4503
4504 if (**arg == ']')
4505 break;
4506 if (**arg != ',')
4507 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509 goto failret;
4510 }
4511 *arg = skipwhite(*arg + 1);
4512 }
4513
4514 if (**arg != ']')
4515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517failret:
4518 if (evaluate)
4519 list_free(l);
4520 return FAIL;
4521 }
4522
4523 *arg = skipwhite(*arg + 1);
4524 if (evaluate)
4525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 rettv->v_type = VAR_LIST;
4527 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 ++l->lv_refcount;
4529 }
4530
4531 return OK;
4532}
4533
4534/*
4535 * Allocate an empty header for a list.
4536 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004537 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538list_alloc()
4539{
Bram Moolenaar33570922005-01-25 22:26:29 +00004540 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541}
4542
4543/*
4544 * Unreference a list: decrement the reference count and free it when it
4545 * becomes zero.
4546 */
4547 static void
4548list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004549 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550{
4551 if (l != NULL && --l->lv_refcount <= 0)
4552 list_free(l);
4553}
4554
4555/*
4556 * Free a list, including all items it points to.
4557 * Ignores the reference count.
4558 */
4559 static void
4560list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004561 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562{
Bram Moolenaar33570922005-01-25 22:26:29 +00004563 listitem_T *item;
4564 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565
4566 for (item = l->lv_first; item != NULL; item = next)
4567 {
4568 next = item->li_next;
4569 listitem_free(item);
4570 }
4571 vim_free(l);
4572}
4573
4574/*
4575 * Allocate a list item.
4576 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004577 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578listitem_alloc()
4579{
Bram Moolenaar33570922005-01-25 22:26:29 +00004580 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581}
4582
4583/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004584 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 */
4586 static void
4587listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004588 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004590 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591 vim_free(item);
4592}
4593
4594/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004595 * Remove a list item from a List and free it. Also clears the value.
4596 */
4597 static void
4598listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004599 list_T *l;
4600 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004601{
4602 list_remove(l, item, item);
4603 listitem_free(item);
4604}
4605
4606/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 * Get the number of items in a list.
4608 */
4609 static long
4610list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004611 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613 if (l == NULL)
4614 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004615 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616}
4617
4618/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004619 * Return TRUE when two lists have exactly the same values.
4620 */
4621 static int
4622list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004623 list_T *l1;
4624 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 int ic; /* ignore case for strings */
4626{
Bram Moolenaar33570922005-01-25 22:26:29 +00004627 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004628
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004629 if (list_len(l1) != list_len(l2))
4630 return FALSE;
4631
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 for (item1 = l1->lv_first, item2 = l2->lv_first;
4633 item1 != NULL && item2 != NULL;
4634 item1 = item1->li_next, item2 = item2->li_next)
4635 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4636 return FALSE;
4637 return item1 == NULL && item2 == NULL;
4638}
4639
4640/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004641 * Return TRUE when two dictionaries have exactly the same key/values.
4642 */
4643 static int
4644dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004645 dict_T *d1;
4646 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004647 int ic; /* ignore case for strings */
4648{
Bram Moolenaar33570922005-01-25 22:26:29 +00004649 hashitem_T *hi;
4650 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004651 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004652
4653 if (dict_len(d1) != dict_len(d2))
4654 return FALSE;
4655
Bram Moolenaar33570922005-01-25 22:26:29 +00004656 todo = d1->dv_hashtab.ht_used;
4657 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004659 if (!HASHITEM_EMPTY(hi))
4660 {
4661 item2 = dict_find(d2, hi->hi_key, -1);
4662 if (item2 == NULL)
4663 return FALSE;
4664 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4665 return FALSE;
4666 --todo;
4667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004668 }
4669 return TRUE;
4670}
4671
4672/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004673 * Return TRUE if "tv1" and "tv2" have the same value.
4674 * Compares the items just like "==" would compare them.
4675 */
4676 static int
4677tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004678 typval_T *tv1;
4679 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004680 int ic; /* ignore case */
4681{
4682 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4683
4684 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4685 {
4686 /* recursive! */
4687 if (tv1->v_type != tv2->v_type
4688 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4689 return FALSE;
4690 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004691 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4692 {
4693 /* recursive! */
4694 if (tv1->v_type != tv2->v_type
4695 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4696 return FALSE;
4697 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004698 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4699 {
4700 if (tv1->v_type != tv2->v_type
4701 || tv1->vval.v_string == NULL
4702 || tv2->vval.v_string == NULL
4703 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4704 return FALSE;
4705 }
4706 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4707 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004708 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4709 * Don't consider "4x" to be equal to 4. */
4710 if ((tv1->v_type == VAR_STRING
4711 && !string_isa_number(tv1->vval.v_string))
4712 || (tv2->v_type == VAR_STRING
4713 && !string_isa_number(tv2->vval.v_string)))
4714 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004715 if (get_tv_number(tv1) != get_tv_number(tv2))
4716 return FALSE;
4717 }
4718 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4719 get_tv_string_buf(tv2, buf2)) != 0)
4720 return FALSE;
4721 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4722 get_tv_string_buf(tv2, buf2)) != 0)
4723 return FALSE;
4724 return TRUE;
4725}
4726
4727/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004728 * Return TRUE if "tv" is a number without other non-white characters.
4729 */
4730 static int
4731string_isa_number(s)
4732 char_u *s;
4733{
4734 int len;
4735
4736 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4737 return len > 0 && *skipwhite(s + len) == NUL;
4738}
4739
4740/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 * Locate item with index "n" in list "l" and return it.
4742 * A negative index is counted from the end; -1 is the last item.
4743 * Returns NULL when "n" is out of range.
4744 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004745 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004747 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 long n;
4749{
Bram Moolenaar33570922005-01-25 22:26:29 +00004750 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 long idx;
4752
4753 if (l == NULL)
4754 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004755
4756 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004758 n = l->lv_len + n;
4759
4760 /* Check for index out of range. */
4761 if (n < 0 || n >= l->lv_len)
4762 return NULL;
4763
4764 /* When there is a cached index may start search from there. */
4765 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004767 if (n < l->lv_idx / 2)
4768 {
4769 /* closest to the start of the list */
4770 item = l->lv_first;
4771 idx = 0;
4772 }
4773 else if (n > (l->lv_idx + l->lv_len) / 2)
4774 {
4775 /* closest to the end of the list */
4776 item = l->lv_last;
4777 idx = l->lv_len - 1;
4778 }
4779 else
4780 {
4781 /* closest to the cached index */
4782 item = l->lv_idx_item;
4783 idx = l->lv_idx;
4784 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 }
4786 else
4787 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004788 if (n < l->lv_len / 2)
4789 {
4790 /* closest to the start of the list */
4791 item = l->lv_first;
4792 idx = 0;
4793 }
4794 else
4795 {
4796 /* closest to the end of the list */
4797 item = l->lv_last;
4798 idx = l->lv_len - 1;
4799 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00004801
4802 while (n > idx)
4803 {
4804 /* search forward */
4805 item = item->li_next;
4806 ++idx;
4807 }
4808 while (n < idx)
4809 {
4810 /* search backward */
4811 item = item->li_prev;
4812 --idx;
4813 }
4814
4815 /* cache the used index */
4816 l->lv_idx = idx;
4817 l->lv_idx_item = item;
4818
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819 return item;
4820}
4821
4822/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004823 * Locate "item" list "l" and return its index.
4824 * Returns -1 when "item" is not in the list.
4825 */
4826 static long
4827list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 list_T *l;
4829 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004830{
4831 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00004832 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004833
4834 if (l == NULL)
4835 return -1;
4836 idx = 0;
4837 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4838 ++idx;
4839 if (li == NULL)
4840 return -1;
4841 return idx;;
4842}
4843
4844/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004845 * Append item "item" to the end of list "l".
4846 */
4847 static void
4848list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004849 list_T *l;
4850 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851{
4852 if (l->lv_last == NULL)
4853 {
4854 /* empty list */
4855 l->lv_first = item;
4856 l->lv_last = item;
4857 item->li_prev = NULL;
4858 }
4859 else
4860 {
4861 l->lv_last->li_next = item;
4862 item->li_prev = l->lv_last;
4863 l->lv_last = item;
4864 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00004865 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004866 item->li_next = NULL;
4867}
4868
4869/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004870 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004871 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872 */
4873 static int
4874list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 list_T *l;
4876 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 listitem_T *ni = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879
4880 if (ni == NULL)
4881 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883 list_append(l, ni);
4884 return OK;
4885}
4886
4887/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004888 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004889 * If "item" is NULL append at the end.
4890 * Return FAIL when out of memory.
4891 */
4892 static int
4893list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 list_T *l;
4895 typval_T *tv;
4896 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004897{
Bram Moolenaar33570922005-01-25 22:26:29 +00004898 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004899
4900 if (ni == NULL)
4901 return FAIL;
4902 copy_tv(tv, &ni->li_tv);
4903 if (item == NULL)
4904 /* Append new item at end of list. */
4905 list_append(l, ni);
4906 else
4907 {
4908 /* Insert new item before existing item. */
4909 ni->li_prev = item->li_prev;
4910 ni->li_next = item;
4911 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004912 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004913 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004914 ++l->lv_idx;
4915 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004916 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00004917 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004918 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004919 l->lv_idx_item = NULL;
4920 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004921 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004922 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004923 }
4924 return OK;
4925}
4926
4927/*
4928 * Extend "l1" with "l2".
4929 * If "bef" is NULL append at the end, otherwise insert before this item.
4930 * Returns FAIL when out of memory.
4931 */
4932 static int
4933list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 list_T *l1;
4935 list_T *l2;
4936 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004937{
Bram Moolenaar33570922005-01-25 22:26:29 +00004938 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004939
4940 for (item = l2->lv_first; item != NULL; item = item->li_next)
4941 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4942 return FAIL;
4943 return OK;
4944}
4945
4946/*
4947 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4948 * Return FAIL when out of memory.
4949 */
4950 static int
4951list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004952 list_T *l1;
4953 list_T *l2;
4954 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004955{
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004957
4958 /* make a copy of the first list. */
4959 l = list_copy(l1, FALSE);
4960 if (l == NULL)
4961 return FAIL;
4962 tv->v_type = VAR_LIST;
4963 tv->vval.v_list = l;
4964
4965 /* append all items from the second list */
4966 return list_extend(l, l2, NULL);
4967}
4968
4969/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004970 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004971 * The refcount of the new list is set to 1.
4972 * Returns NULL when out of memory.
4973 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004974 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975list_copy(orig, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 int deep;
4978{
Bram Moolenaar33570922005-01-25 22:26:29 +00004979 list_T *copy;
4980 listitem_T *item;
4981 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982
4983 if (orig == NULL)
4984 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985
4986 copy = list_alloc();
4987 if (copy != NULL)
4988 {
4989 for (item = orig->lv_first; item != NULL; item = item->li_next)
4990 {
4991 ni = listitem_alloc();
4992 if (ni == NULL)
4993 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004994 if (deep)
4995 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004998 list_append(copy, ni);
4999 }
5000 ++copy->lv_refcount;
5001 }
5002
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003 return copy;
5004}
5005
5006/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005007 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005008 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005009 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005010 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005011list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005012 list_T *l;
5013 listitem_T *item;
5014 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005015{
Bram Moolenaar33570922005-01-25 22:26:29 +00005016 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005017
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005018 /* notify watchers */
5019 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005021 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005022 list_fix_watch(l, ip);
5023 if (ip == item2)
5024 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005026
5027 if (item2->li_next == NULL)
5028 l->lv_last = item->li_prev;
5029 else
5030 item2->li_next->li_prev = item->li_prev;
5031 if (item->li_prev == NULL)
5032 l->lv_first = item2->li_next;
5033 else
5034 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005035 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036}
5037
5038/*
5039 * Return an allocated string with the string representation of a list.
5040 * May return NULL.
5041 */
5042 static char_u *
5043list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005044 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005045{
5046 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047
5048 if (tv->vval.v_list == NULL)
5049 return NULL;
5050 ga_init2(&ga, (int)sizeof(char), 80);
5051 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005053 ga_append(&ga, ']');
5054 ga_append(&ga, NUL);
5055 return (char_u *)ga.ga_data;
5056}
5057
5058/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 * Join list "l" into a string in "*gap", using separator "sep".
5060 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
5061 */
5062 static void
5063list_join(gap, l, sep, echo)
5064 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005065 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 char_u *sep;
5067 int echo;
5068{
5069 int first = TRUE;
5070 char_u *tofree;
5071 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 char_u *s;
5074
5075 for (item = l->lv_first; item != NULL; item = item->li_next)
5076 {
5077 if (first)
5078 first = FALSE;
5079 else
5080 ga_concat(gap, sep);
5081
5082 if (echo)
5083 s = echo_string(&item->li_tv, &tofree, numbuf);
5084 else
5085 s = tv2string(&item->li_tv, &tofree, numbuf);
5086 if (s != NULL)
5087 ga_concat(gap, s);
5088 vim_free(tofree);
5089 }
5090}
5091
5092/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Allocate an empty header for a dictionary.
5094 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005095 static dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096dict_alloc()
5097{
Bram Moolenaar33570922005-01-25 22:26:29 +00005098 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005099
Bram Moolenaar33570922005-01-25 22:26:29 +00005100 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005101 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005102 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005103 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005104 d->dv_lock = 0;
5105 d->dv_refcount = 0;
5106 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005107 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108}
5109
5110/*
5111 * Unreference a Dictionary: decrement the reference count and free it when it
5112 * becomes zero.
5113 */
5114 static void
5115dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005116 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117{
5118 if (d != NULL && --d->dv_refcount <= 0)
5119 dict_free(d);
5120}
5121
5122/*
5123 * Free a Dictionary, including all items it contains.
5124 * Ignores the reference count.
5125 */
5126 static void
5127dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005128 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005130 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005131 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005133 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005134 * hashtab. Must not try to resize the hashtab! */
5135 todo = d->dv_hashtab.ht_used;
5136 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005138 if (!HASHITEM_EMPTY(hi))
5139 {
5140 dictitem_free(HI2DI(hi));
5141 --todo;
5142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005144 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 vim_free(d);
5146}
5147
5148/*
5149 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005150 * The "key" is copied to the new item.
5151 * Note that the value of the item "di_tv" still needs to be initialized!
5152 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005154 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005155dictitem_alloc(key)
5156 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157{
Bram Moolenaar33570922005-01-25 22:26:29 +00005158 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005159
Bram Moolenaar33570922005-01-25 22:26:29 +00005160 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005161 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005162 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005163 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005164 di->di_flags = 0;
5165 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005166 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167}
5168
5169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Make a copy of a Dictionary item.
5171 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005172 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175{
Bram Moolenaar33570922005-01-25 22:26:29 +00005176 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177
Bram Moolenaar33570922005-01-25 22:26:29 +00005178 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 if (di != NULL)
5180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005181 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005182 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183 copy_tv(&org->di_tv, &di->di_tv);
5184 }
5185 return di;
5186}
5187
5188/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005189 * Remove item "item" from Dictionary "dict" and free it.
5190 */
5191 static void
5192dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005193 dict_T *dict;
5194 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005195{
Bram Moolenaar33570922005-01-25 22:26:29 +00005196 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005197
Bram Moolenaar33570922005-01-25 22:26:29 +00005198 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005199 if (HASHITEM_EMPTY(hi))
5200 EMSG2(_(e_intern2), "dictitem_remove()");
5201 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005202 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005203 dictitem_free(item);
5204}
5205
5206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 * Free a dict item. Also clears the value.
5208 */
5209 static void
5210dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005211 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 clear_tv(&item->di_tv);
5214 vim_free(item);
5215}
5216
5217/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005218 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5219 * The refcount of the new dict is set to 1.
5220 * Returns NULL when out of memory.
5221 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 static dict_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005223dict_copy(orig, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 int deep;
5226{
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 dict_T *copy;
5228 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005229 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005230 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005231
5232 if (orig == NULL)
5233 return NULL;
5234
5235 copy = dict_alloc();
5236 if (copy != NULL)
5237 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 todo = orig->dv_hashtab.ht_used;
5239 for (hi = orig->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005241 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005243 --todo;
5244
5245 di = dictitem_alloc(hi->hi_key);
5246 if (di == NULL)
5247 break;
5248 if (deep)
5249 item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep);
5250 else
5251 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5252 if (dict_add(copy, di) == FAIL)
5253 {
5254 dictitem_free(di);
5255 break;
5256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005258 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005259
Bram Moolenaare9a41262005-01-15 22:18:47 +00005260 ++copy->dv_refcount;
5261 }
5262
5263 return copy;
5264}
5265
5266/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005268 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005270 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 dict_T *d;
5273 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274{
Bram Moolenaar33570922005-01-25 22:26:29 +00005275 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276}
5277
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005279 * Get the number of items in a Dictionary.
5280 */
5281 static long
5282dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005284{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005285 if (d == NULL)
5286 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005287 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005288}
5289
5290/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 * Find item "key[len]" in Dictionary "d".
5292 * If "len" is negative use strlen(key).
5293 * Returns NULL when not found.
5294 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005296dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 char_u *key;
5299 int len;
5300{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005301#define AKEYLEN 200
5302 char_u buf[AKEYLEN];
5303 char_u *akey;
5304 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005307 if (len < 0)
5308 akey = key;
5309 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005311 tofree = akey = vim_strnsave(key, len);
5312 if (akey == NULL)
5313 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005314 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005315 else
5316 {
5317 /* Avoid a malloc/free by using buf[]. */
5318 STRNCPY(buf, key, len);
5319 buf[len] = NUL;
5320 akey = buf;
5321 }
5322
Bram Moolenaar33570922005-01-25 22:26:29 +00005323 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005324 vim_free(tofree);
5325 if (HASHITEM_EMPTY(hi))
5326 return NULL;
5327 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328}
5329
5330/*
5331 * Return an allocated string with the string representation of a Dictionary.
5332 * May return NULL.
5333 */
5334 static char_u *
5335dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337{
5338 garray_T ga;
5339 int first = TRUE;
5340 char_u *tofree;
5341 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005342 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005345 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005347 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 return NULL;
5349 ga_init2(&ga, (int)sizeof(char), 80);
5350 ga_append(&ga, '{');
5351
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 todo = d->dv_hashtab.ht_used;
5353 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005355 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005357 --todo;
5358
5359 if (first)
5360 first = FALSE;
5361 else
5362 ga_concat(&ga, (char_u *)", ");
5363
5364 tofree = string_quote(hi->hi_key, FALSE);
5365 if (tofree != NULL)
5366 {
5367 ga_concat(&ga, tofree);
5368 vim_free(tofree);
5369 }
5370 ga_concat(&ga, (char_u *)": ");
5371 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5372 if (s != NULL)
5373 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 vim_free(tofree);
5375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 }
5377
5378 ga_append(&ga, '}');
5379 ga_append(&ga, NUL);
5380 return (char_u *)ga.ga_data;
5381}
5382
5383/*
5384 * Allocate a variable for a Dictionary and fill it from "*arg".
5385 * Return OK or FAIL. Returns NOTDONE for {expr}.
5386 */
5387 static int
5388get_dict_tv(arg, rettv, evaluate)
5389 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005390 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 int evaluate;
5392{
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 dict_T *d = NULL;
5394 typval_T tvkey;
5395 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005399 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400
5401 /*
5402 * First check if it's not a curly-braces thing: {expr}.
5403 * Must do this without evaluating, otherwise a function may be called
5404 * twice. Unfortunately this means we need to call eval1() twice for the
5405 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005406 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005408 if (*start != '}')
5409 {
5410 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5411 return FAIL;
5412 if (*start == '}')
5413 return NOTDONE;
5414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415
5416 if (evaluate)
5417 {
5418 d = dict_alloc();
5419 if (d == NULL)
5420 return FAIL;
5421 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005422 tvkey.v_type = VAR_UNKNOWN;
5423 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424
5425 *arg = skipwhite(*arg + 1);
5426 while (**arg != '}' && **arg != NUL)
5427 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005428 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 goto failret;
5430 if (**arg != ':')
5431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005432 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005433 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 goto failret;
5435 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005436 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 if (*key == NUL)
5438 {
5439 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005440 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 goto failret;
5442 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443
5444 *arg = skipwhite(*arg + 1);
5445 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5446 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005447 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 goto failret;
5449 }
5450 if (evaluate)
5451 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005452 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 if (item != NULL)
5454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005455 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005456 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 clear_tv(&tv);
5458 goto failret;
5459 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005460 item = dictitem_alloc(key);
5461 clear_tv(&tvkey);
5462 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005465 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005466 if (dict_add(d, item) == FAIL)
5467 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 }
5469 }
5470
5471 if (**arg == '}')
5472 break;
5473 if (**arg != ',')
5474 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005475 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476 goto failret;
5477 }
5478 *arg = skipwhite(*arg + 1);
5479 }
5480
5481 if (**arg != '}')
5482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005483 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484failret:
5485 if (evaluate)
5486 dict_free(d);
5487 return FAIL;
5488 }
5489
5490 *arg = skipwhite(*arg + 1);
5491 if (evaluate)
5492 {
5493 rettv->v_type = VAR_DICT;
5494 rettv->vval.v_dict = d;
5495 ++d->dv_refcount;
5496 }
5497
5498 return OK;
5499}
5500
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 * Return a string with the string representation of a variable.
5503 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005505 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 * May return NULL;
5507 */
5508 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005509echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005512 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005514 static int recurse = 0;
5515 char_u *r = NULL;
5516
Bram Moolenaar33570922005-01-25 22:26:29 +00005517 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520 *tofree = NULL;
5521 return NULL;
5522 }
5523 ++recurse;
5524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 switch (tv->v_type)
5526 {
5527 case VAR_FUNC:
5528 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005529 r = tv->vval.v_string;
5530 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 case VAR_LIST:
5532 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005533 r = *tofree;
5534 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 case VAR_DICT:
5536 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005537 r = *tofree;
5538 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 case VAR_STRING:
5540 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005541 *tofree = NULL;
5542 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005545 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005546 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005548
5549 --recurse;
5550 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005551}
5552
5553/*
5554 * Return a string with the string representation of a variable.
5555 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5556 * "numbuf" is used for a number.
5557 * Puts quotes around strings, so that they can be parsed back by eval().
5558 * May return NULL;
5559 */
5560 static char_u *
5561tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005563 char_u **tofree;
5564 char_u *numbuf;
5565{
5566 switch (tv->v_type)
5567 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005568 case VAR_FUNC:
5569 *tofree = string_quote(tv->vval.v_string, TRUE);
5570 return *tofree;
5571 case VAR_STRING:
5572 *tofree = string_quote(tv->vval.v_string, FALSE);
5573 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005574 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005575 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005577 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005578 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005581 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582}
5583
5584/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 * Return string "str" in ' quotes, doubling ' characters.
5586 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005588 */
5589 static char_u *
5590string_quote(str, function)
5591 char_u *str;
5592 int function;
5593{
Bram Moolenaar33570922005-01-25 22:26:29 +00005594 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005595 char_u *p, *r, *s;
5596
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 len = (function ? 13 : 3);
5598 if (str != NULL)
5599 {
5600 len += STRLEN(str);
5601 for (p = str; *p != NUL; mb_ptr_adv(p))
5602 if (*p == '\'')
5603 ++len;
5604 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 s = r = alloc(len);
5606 if (r != NULL)
5607 {
5608 if (function)
5609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611 r += 10;
5612 }
5613 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 if (str != NULL)
5616 for (p = str; *p != NUL; )
5617 {
5618 if (*p == '\'')
5619 *r++ = '\'';
5620 MB_COPY_CHAR(p, r);
5621 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005623 if (function)
5624 *r++ = ')';
5625 *r++ = NUL;
5626 }
5627 return s;
5628}
5629
5630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 * Get the value of an environment variable.
5632 * "arg" is pointing to the '$'. It is advanced to after the name.
5633 * If the environment variable was not set, silently assume it is empty.
5634 * Always return OK.
5635 */
5636 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 int evaluate;
5641{
5642 char_u *string = NULL;
5643 int len;
5644 int cc;
5645 char_u *name;
5646
5647 ++*arg;
5648 name = *arg;
5649 len = get_env_len(arg);
5650 if (evaluate)
5651 {
5652 if (len != 0)
5653 {
5654 cc = name[len];
5655 name[len] = NUL;
5656 /* first try mch_getenv(), fast for normal environment vars */
5657 string = mch_getenv(name);
5658 if (string != NULL && *string != NUL)
5659 string = vim_strsave(string);
5660 else
5661 {
5662 /* next try expanding things like $VIM and ${HOME} */
5663 string = expand_env_save(name - 1);
5664 if (string != NULL && *string == '$')
5665 {
5666 vim_free(string);
5667 string = NULL;
5668 }
5669 }
5670 name[len] = cc;
5671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675
5676 return OK;
5677}
5678
5679/*
5680 * Array with names and number of arguments of all internal functions
5681 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5682 */
5683static struct fst
5684{
5685 char *f_name; /* function name */
5686 char f_min_argc; /* minimal number of arguments */
5687 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00005688 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690} functions[] =
5691{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005692 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {"append", 2, 2, f_append},
5694 {"argc", 0, 0, f_argc},
5695 {"argidx", 0, 0, f_argidx},
5696 {"argv", 1, 1, f_argv},
5697 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005698 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {"bufexists", 1, 1, f_bufexists},
5700 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5701 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5702 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5703 {"buflisted", 1, 1, f_buflisted},
5704 {"bufloaded", 1, 1, f_bufloaded},
5705 {"bufname", 1, 1, f_bufname},
5706 {"bufnr", 1, 1, f_bufnr},
5707 {"bufwinnr", 1, 1, f_bufwinnr},
5708 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005709 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005710 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {"char2nr", 1, 1, f_char2nr},
5712 {"cindent", 1, 1, f_cindent},
5713 {"col", 1, 1, f_col},
5714 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {"cscope_connection",0,3, f_cscope_connection},
5718 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {"delete", 1, 1, f_delete},
5721 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005722 {"diff_filler", 1, 1, f_diff_filler},
5723 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005724 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {"eventhandler", 0, 0, f_eventhandler},
5728 {"executable", 1, 1, f_executable},
5729 {"exists", 1, 1, f_exists},
5730 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005731 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5733 {"filereadable", 1, 1, f_filereadable},
5734 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005736 {"finddir", 1, 3, f_finddir},
5737 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {"fnamemodify", 2, 2, f_fnamemodify},
5739 {"foldclosed", 1, 1, f_foldclosed},
5740 {"foldclosedend", 1, 1, f_foldclosedend},
5741 {"foldlevel", 1, 1, f_foldlevel},
5742 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005743 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005746 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {"getbufvar", 2, 2, f_getbufvar},
5748 {"getchar", 0, 1, f_getchar},
5749 {"getcharmod", 0, 0, f_getcharmod},
5750 {"getcmdline", 0, 0, f_getcmdline},
5751 {"getcmdpos", 0, 0, f_getcmdpos},
5752 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005753 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005754 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {"getfsize", 1, 1, f_getfsize},
5756 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005757 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005758 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {"getreg", 0, 1, f_getreg},
5760 {"getregtype", 0, 1, f_getregtype},
5761 {"getwinposx", 0, 0, f_getwinposx},
5762 {"getwinposy", 0, 0, f_getwinposy},
5763 {"getwinvar", 2, 2, f_getwinvar},
5764 {"glob", 1, 1, f_glob},
5765 {"globpath", 2, 2, f_globpath},
5766 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005767 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {"hasmapto", 1, 2, f_hasmapto},
5769 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5770 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5771 {"histadd", 2, 2, f_histadd},
5772 {"histdel", 1, 2, f_histdel},
5773 {"histget", 1, 2, f_histget},
5774 {"histnr", 1, 1, f_histnr},
5775 {"hlID", 1, 1, f_hlID},
5776 {"hlexists", 1, 1, f_hlexists},
5777 {"hostname", 0, 0, f_hostname},
5778 {"iconv", 3, 3, f_iconv},
5779 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 {"input", 1, 2, f_input},
5782 {"inputdialog", 1, 3, f_inputdialog},
5783 {"inputrestore", 0, 0, f_inputrestore},
5784 {"inputsave", 0, 0, f_inputsave},
5785 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005788 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {"libcall", 3, 3, f_libcall},
5795 {"libcallnr", 3, 3, f_libcallnr},
5796 {"line", 1, 1, f_line},
5797 {"line2byte", 1, 1, f_line2byte},
5798 {"lispindent", 1, 1, f_lispindent},
5799 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {"maparg", 1, 2, f_maparg},
5802 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005803 {"match", 2, 4, f_match},
5804 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005805 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005806 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005807 {"max", 1, 1, f_max},
5808 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {"mode", 0, 0, f_mode},
5810 {"nextnonblank", 1, 1, f_nextnonblank},
5811 {"nr2char", 1, 1, f_nr2char},
5812 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 {"range", 1, 3, f_range},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005814 {"readfile", 1, 2, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {"remote_expr", 2, 3, f_remote_expr},
5816 {"remote_foreground", 1, 1, f_remote_foreground},
5817 {"remote_peek", 1, 2, f_remote_peek},
5818 {"remote_read", 1, 1, f_remote_read},
5819 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005820 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005822 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005824 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {"search", 1, 2, f_search},
5826 {"searchpair", 3, 5, f_searchpair},
5827 {"server2client", 2, 2, f_server2client},
5828 {"serverlist", 0, 0, f_serverlist},
5829 {"setbufvar", 3, 3, f_setbufvar},
5830 {"setcmdpos", 1, 1, f_setcmdpos},
5831 {"setline", 2, 2, f_setline},
5832 {"setreg", 2, 3, f_setreg},
5833 {"setwinvar", 3, 3, f_setwinvar},
5834 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005835 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837#ifdef HAVE_STRFTIME
5838 {"strftime", 1, 2, f_strftime},
5839#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {"strlen", 1, 1, f_strlen},
5843 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00005844 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {"strtrans", 1, 1, f_strtrans},
5846 {"submatch", 1, 1, f_submatch},
5847 {"substitute", 4, 4, f_substitute},
5848 {"synID", 3, 3, f_synID},
5849 {"synIDattr", 2, 3, f_synIDattr},
5850 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005851 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {"tempname", 0, 0, f_tempname},
5853 {"tolower", 1, 1, f_tolower},
5854 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005855 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {"virtcol", 1, 1, f_virtcol},
5859 {"visualmode", 0, 1, f_visualmode},
5860 {"winbufnr", 1, 1, f_winbufnr},
5861 {"wincol", 0, 0, f_wincol},
5862 {"winheight", 1, 1, f_winheight},
5863 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005864 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {"winrestcmd", 0, 0, f_winrestcmd},
5866 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005867 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868};
5869
5870#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5871
5872/*
5873 * Function given to ExpandGeneric() to obtain the list of internal
5874 * or user defined function names.
5875 */
5876 char_u *
5877get_function_name(xp, idx)
5878 expand_T *xp;
5879 int idx;
5880{
5881 static int intidx = -1;
5882 char_u *name;
5883
5884 if (idx == 0)
5885 intidx = -1;
5886 if (intidx < 0)
5887 {
5888 name = get_user_func_name(xp, idx);
5889 if (name != NULL)
5890 return name;
5891 }
5892 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5893 {
5894 STRCPY(IObuff, functions[intidx].f_name);
5895 STRCAT(IObuff, "(");
5896 if (functions[intidx].f_max_argc == 0)
5897 STRCAT(IObuff, ")");
5898 return IObuff;
5899 }
5900
5901 return NULL;
5902}
5903
5904/*
5905 * Function given to ExpandGeneric() to obtain the list of internal or
5906 * user defined variable or function names.
5907 */
5908/*ARGSUSED*/
5909 char_u *
5910get_expr_name(xp, idx)
5911 expand_T *xp;
5912 int idx;
5913{
5914 static int intidx = -1;
5915 char_u *name;
5916
5917 if (idx == 0)
5918 intidx = -1;
5919 if (intidx < 0)
5920 {
5921 name = get_function_name(xp, idx);
5922 if (name != NULL)
5923 return name;
5924 }
5925 return get_user_var_name(xp, ++intidx);
5926}
5927
5928#endif /* FEAT_CMDL_COMPL */
5929
5930/*
5931 * Find internal function in table above.
5932 * Return index, or -1 if not found
5933 */
5934 static int
5935find_internal_func(name)
5936 char_u *name; /* name of the function */
5937{
5938 int first = 0;
5939 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5940 int cmp;
5941 int x;
5942
5943 /*
5944 * Find the function name in the table. Binary search.
5945 */
5946 while (first <= last)
5947 {
5948 x = first + ((unsigned)(last - first) >> 1);
5949 cmp = STRCMP(name, functions[x].f_name);
5950 if (cmp < 0)
5951 last = x - 1;
5952 else if (cmp > 0)
5953 first = x + 1;
5954 else
5955 return x;
5956 }
5957 return -1;
5958}
5959
5960/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5962 * name it contains, otherwise return "name".
5963 */
5964 static char_u *
5965deref_func_name(name, lenp)
5966 char_u *name;
5967 int *lenp;
5968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 int cc;
5971
5972 cc = name[*lenp];
5973 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00005974 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005978 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 {
5980 *lenp = 0;
5981 return (char_u *)""; /* just in case */
5982 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 *lenp = STRLEN(v->di_tv.vval.v_string);
5984 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 }
5986
5987 return name;
5988}
5989
5990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 * Allocate a variable for the result of a function.
5992 * Return OK or FAIL.
5993 */
5994 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5996 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 char_u *name; /* name of the function */
5998 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 char_u **arg; /* argument, pointing to the '(' */
6001 linenr_T firstline; /* first line of range */
6002 linenr_T lastline; /* last line of range */
6003 int *doesrange; /* return: function handled range */
6004 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
6007 char_u *argp;
6008 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 int argcount = 0; /* number of arguments found */
6011
6012 /*
6013 * Get the arguments.
6014 */
6015 argp = *arg;
6016 while (argcount < MAX_FUNC_ARGS)
6017 {
6018 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6019 if (*argp == ')' || *argp == ',' || *argp == NUL)
6020 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6022 {
6023 ret = FAIL;
6024 break;
6025 }
6026 ++argcount;
6027 if (*argp != ',')
6028 break;
6029 }
6030 if (*argp == ')')
6031 ++argp;
6032 else
6033 ret = FAIL;
6034
6035 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006036 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006037 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 {
6040 if (argcount == MAX_FUNC_ARGS)
6041 EMSG2(_("E740: Too many arguments for function %s"), name);
6042 else
6043 EMSG2(_("E116: Invalid arguments for function %s"), name);
6044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006047 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049 *arg = skipwhite(argp);
6050 return ret;
6051}
6052
6053
6054/*
6055 * Call a function with its resolved parameters
6056 * Return OK or FAIL.
6057 */
6058 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006059call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 char_u *name; /* name of the function */
6062 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 linenr_T firstline; /* first line of range */
6067 linenr_T lastline; /* last line of range */
6068 int *doesrange; /* return: function handled range */
6069 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#define ERROR_UNKNOWN 0
6074#define ERROR_TOOMANY 1
6075#define ERROR_TOOFEW 2
6076#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077#define ERROR_DICT 4
6078#define ERROR_NONE 5
6079#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 int error = ERROR_NONE;
6081 int i;
6082 int llen;
6083 ufunc_T *fp;
6084 int cc;
6085#define FLEN_FIXED 40
6086 char_u fname_buf[FLEN_FIXED + 1];
6087 char_u *fname;
6088
6089 /*
6090 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6091 * Change <SNR>123_name() to K_SNR 123_name().
6092 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6093 */
6094 cc = name[len];
6095 name[len] = NUL;
6096 llen = eval_fname_script(name);
6097 if (llen > 0)
6098 {
6099 fname_buf[0] = K_SPECIAL;
6100 fname_buf[1] = KS_EXTRA;
6101 fname_buf[2] = (int)KE_SNR;
6102 i = 3;
6103 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6104 {
6105 if (current_SID <= 0)
6106 error = ERROR_SCRIPT;
6107 else
6108 {
6109 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6110 i = (int)STRLEN(fname_buf);
6111 }
6112 }
6113 if (i + STRLEN(name + llen) < FLEN_FIXED)
6114 {
6115 STRCPY(fname_buf + i, name + llen);
6116 fname = fname_buf;
6117 }
6118 else
6119 {
6120 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6121 if (fname == NULL)
6122 error = ERROR_OTHER;
6123 else
6124 {
6125 mch_memmove(fname, fname_buf, (size_t)i);
6126 STRCPY(fname + i, name + llen);
6127 }
6128 }
6129 }
6130 else
6131 fname = name;
6132
6133 *doesrange = FALSE;
6134
6135
6136 /* execute the function if no errors detected and executing */
6137 if (evaluate && error == ERROR_NONE)
6138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006139 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 error = ERROR_UNKNOWN;
6141
6142 if (!ASCII_ISLOWER(fname[0]))
6143 {
6144 /*
6145 * User defined function.
6146 */
6147 fp = find_func(fname);
6148#ifdef FEAT_AUTOCMD
6149 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
6150 fname, fname, TRUE, NULL)
6151#ifdef FEAT_EVAL
6152 && !aborting()
6153#endif
6154 )
6155 {
6156 /* executed an autocommand, search for function again */
6157 fp = find_func(fname);
6158 }
6159#endif
6160 if (fp != NULL)
6161 {
6162 if (fp->flags & FC_RANGE)
6163 *doesrange = TRUE;
6164 if (argcount < fp->args.ga_len)
6165 error = ERROR_TOOFEW;
6166 else if (!fp->varargs && argcount > fp->args.ga_len)
6167 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168 else if ((fp->flags & FC_DICT) && selfdict == NULL)
6169 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 else
6171 {
6172 /*
6173 * Call the user function.
6174 * Save and restore search patterns, script variables and
6175 * redo buffer.
6176 */
6177 save_search_patterns();
6178 saveRedobuff();
6179 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006180 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 firstline, lastline,
6182 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183 if (--fp->calls <= 0 && isdigit(*fp->name)
6184 && fp->refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006185 /* Function was unreferenced while being used, free it
6186 * now. */
6187 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 restoreRedobuff();
6189 restore_search_patterns();
6190 error = ERROR_NONE;
6191 }
6192 }
6193 }
6194 else
6195 {
6196 /*
6197 * Find the function name in the table, call its implementation.
6198 */
6199 i = find_internal_func(fname);
6200 if (i >= 0)
6201 {
6202 if (argcount < functions[i].f_min_argc)
6203 error = ERROR_TOOFEW;
6204 else if (argcount > functions[i].f_max_argc)
6205 error = ERROR_TOOMANY;
6206 else
6207 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 error = ERROR_NONE;
6211 }
6212 }
6213 }
6214 /*
6215 * The function call (or "FuncUndefined" autocommand sequence) might
6216 * have been aborted by an error, an interrupt, or an explicitly thrown
6217 * exception that has not been caught so far. This situation can be
6218 * tested for by calling aborting(). For an error in an internal
6219 * function or for the "E132" error in call_user_func(), however, the
6220 * throw point at which the "force_abort" flag (temporarily reset by
6221 * emsg()) is normally updated has not been reached yet. We need to
6222 * update that flag first to make aborting() reliable.
6223 */
6224 update_force_abort();
6225 }
6226 if (error == ERROR_NONE)
6227 ret = OK;
6228
6229 /*
6230 * Report an error unless the argument evaluation or function call has been
6231 * cancelled due to an aborting error, an interrupt, or an exception.
6232 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006233 if (!aborting())
6234 {
6235 switch (error)
6236 {
6237 case ERROR_UNKNOWN:
6238 EMSG2(_("E117: Unknown function: %s"), name);
6239 break;
6240 case ERROR_TOOMANY:
6241 EMSG2(_(e_toomanyarg), name);
6242 break;
6243 case ERROR_TOOFEW:
6244 EMSG2(_("E119: Not enough arguments for function: %s"),
6245 name);
6246 break;
6247 case ERROR_SCRIPT:
6248 EMSG2(_("E120: Using <SID> not in a script context: %s"),
6249 name);
6250 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006251 case ERROR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253 name);
6254 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255 }
6256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 name[len] = cc;
6259 if (fname != name && fname != fname_buf)
6260 vim_free(fname);
6261
6262 return ret;
6263}
6264
6265/*********************************************
6266 * Implementation of the built-in functions
6267 */
6268
6269/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006270 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 */
6272 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006273f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 typval_T *argvars;
6275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006279 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006282 if ((l = argvars[0].vval.v_list) != NULL
6283 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6284 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006285 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 }
6287 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006288 EMSG(_(e_listreq));
6289}
6290
6291/*
6292 * "append(lnum, string/list)" function
6293 */
6294 static void
6295f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 typval_T *argvars;
6297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006298{
6299 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 list_T *l = NULL;
6301 listitem_T *li = NULL;
6302 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006303 long added = 0;
6304
6305 rettv->vval.v_number = 1; /* Default: Failed */
6306 lnum = get_tv_lnum(argvars);
6307 if (lnum >= 0
6308 && lnum <= curbuf->b_ml.ml_line_count
6309 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006311 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006313 l = argvars[1].vval.v_list;
6314 if (l == NULL)
6315 return;
6316 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006318 for (;;)
6319 {
6320 if (l == NULL)
6321 tv = &argvars[1]; /* append a string */
6322 else if (li == NULL)
6323 break; /* end of list */
6324 else
6325 tv = &li->li_tv; /* append item from list */
6326 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6327 ++added;
6328 if (l == NULL)
6329 break;
6330 li = li->li_next;
6331 }
6332
6333 appended_lines_mark(lnum, added);
6334 if (curwin->w_cursor.lnum > lnum)
6335 curwin->w_cursor.lnum += added;
6336 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338}
6339
6340/*
6341 * "argc()" function
6342 */
6343/* ARGSUSED */
6344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006345f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 typval_T *argvars;
6347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006349 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351
6352/*
6353 * "argidx()" function
6354 */
6355/* ARGSUSED */
6356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006357f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 typval_T *argvars;
6359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006361 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362}
6363
6364/*
6365 * "argv(nr)" function
6366 */
6367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006368f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 typval_T *argvars;
6370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 int idx;
6373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006378 rettv->vval.v_string = NULL;
6379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380}
6381
6382/*
6383 * "browse(save, title, initdir, default)" function
6384 */
6385/* ARGSUSED */
6386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006387f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 typval_T *argvars;
6389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391#ifdef FEAT_BROWSE
6392 int save;
6393 char_u *title;
6394 char_u *initdir;
6395 char_u *defname;
6396 char_u buf[NUMBUFLEN];
6397 char_u buf2[NUMBUFLEN];
6398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006399 save = get_tv_number(&argvars[0]);
6400 title = get_tv_string(&argvars[1]);
6401 initdir = get_tv_string_buf(&argvars[2], buf);
6402 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006404 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006405 do_browse(save ? BROWSE_SAVE : 0,
6406 title, defname, NULL, initdir, NULL, curbuf);
6407#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006409#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006411}
6412
6413/*
6414 * "browsedir(title, initdir)" function
6415 */
6416/* ARGSUSED */
6417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 typval_T *argvars;
6420 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006421{
6422#ifdef FEAT_BROWSE
6423 char_u *title;
6424 char_u *initdir;
6425 char_u buf[NUMBUFLEN];
6426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427 title = get_tv_string(&argvars[0]);
6428 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006430 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006431 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006433 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436}
6437
Bram Moolenaar33570922005-01-25 22:26:29 +00006438static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440/*
6441 * Find a buffer by number or exact name.
6442 */
6443 static buf_T *
6444find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449 if (avar->v_type == VAR_NUMBER)
6450 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006451 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006454 if (buf == NULL)
6455 {
6456 /* No full path name match, try a match with a URL or a "nofile"
6457 * buffer, these don't use the full path. */
6458 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6459 if (buf->b_fname != NULL
6460 && (path_with_url(buf->b_fname)
6461#ifdef FEAT_QUICKFIX
6462 || bt_nofile(buf)
6463#endif
6464 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006466 break;
6467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 }
6469 return buf;
6470}
6471
6472/*
6473 * "bufexists(expr)" function
6474 */
6475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 typval_T *argvars;
6478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006480 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481}
6482
6483/*
6484 * "buflisted(expr)" function
6485 */
6486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 typval_T *argvars;
6489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 buf_T *buf;
6492
6493 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006494 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495}
6496
6497/*
6498 * "bufloaded(expr)" function
6499 */
6500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006501f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 typval_T *argvars;
6503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 buf_T *buf;
6506
6507 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509}
6510
Bram Moolenaar33570922005-01-25 22:26:29 +00006511static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006512
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513/*
6514 * Get buffer by number or pattern.
6515 */
6516 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006517get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006520 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 int save_magic;
6522 char_u *save_cpo;
6523 buf_T *buf;
6524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 if (tv->v_type == VAR_NUMBER)
6526 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 if (tv->v_type != VAR_STRING)
6528 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (name == NULL || *name == NUL)
6530 return curbuf;
6531 if (name[0] == '$' && name[1] == NUL)
6532 return lastbuf;
6533
6534 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6535 save_magic = p_magic;
6536 p_magic = TRUE;
6537 save_cpo = p_cpo;
6538 p_cpo = (char_u *)"";
6539
6540 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6541 TRUE, FALSE));
6542
6543 p_magic = save_magic;
6544 p_cpo = save_cpo;
6545
6546 /* If not found, try expanding the name, like done for bufexists(). */
6547 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
6550 return buf;
6551}
6552
6553/*
6554 * "bufname(expr)" function
6555 */
6556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006557f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 typval_T *argvars;
6559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 buf_T *buf;
6562
6563 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006564 buf = get_buf_tv(&argvars[0]);
6565 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006567 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 --emsg_off;
6571}
6572
6573/*
6574 * "bufnr(expr)" function
6575 */
6576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 typval_T *argvars;
6579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 buf_T *buf;
6582
6583 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006584 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 --emsg_off;
6590}
6591
6592/*
6593 * "bufwinnr(nr)" function
6594 */
6595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 typval_T *argvars;
6598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
6600#ifdef FEAT_WINDOWS
6601 win_T *wp;
6602 int winnr = 0;
6603#endif
6604 buf_T *buf;
6605
6606 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006607 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608#ifdef FEAT_WINDOWS
6609 for (wp = firstwin; wp; wp = wp->w_next)
6610 {
6611 ++winnr;
6612 if (wp->w_buffer == buf)
6613 break;
6614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006615 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006617 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618#endif
6619 --emsg_off;
6620}
6621
6622/*
6623 * "byte2line(byte)" function
6624 */
6625/*ARGSUSED*/
6626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006627f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 typval_T *argvars;
6629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#else
6634 long boff = 0;
6635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006640 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 (linenr_T)0, &boff);
6642#endif
6643}
6644
6645/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006646 * "byteidx()" function
6647 */
6648/*ARGSUSED*/
6649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006650f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 typval_T *argvars;
6652 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006653{
6654#ifdef FEAT_MBYTE
6655 char_u *t;
6656#endif
6657 char_u *str;
6658 long idx;
6659
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006660 str = get_tv_string(&argvars[0]);
6661 idx = get_tv_number(&argvars[1]);
6662 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006663 if (idx < 0)
6664 return;
6665
6666#ifdef FEAT_MBYTE
6667 t = str;
6668 for ( ; idx > 0; idx--)
6669 {
6670 if (*t == NUL) /* EOL reached */
6671 return;
6672 t += mb_ptr2len_check(t);
6673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006675#else
6676 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006677 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006678#endif
6679}
6680
6681/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006682 * "call(func, arglist)" function
6683 */
6684 static void
6685f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006686 typval_T *argvars;
6687 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006688{
6689 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00006690 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006691 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006693 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00006694 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006695
6696 rettv->vval.v_number = 0;
6697 if (argvars[1].v_type != VAR_LIST)
6698 {
6699 EMSG(_(e_listreq));
6700 return;
6701 }
6702 if (argvars[1].vval.v_list == NULL)
6703 return;
6704
6705 if (argvars[0].v_type == VAR_FUNC)
6706 func = argvars[0].vval.v_string;
6707 else
6708 func = get_tv_string(&argvars[0]);
6709
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 if (argvars[2].v_type != VAR_UNKNOWN)
6711 {
6712 if (argvars[2].v_type != VAR_DICT)
6713 {
6714 EMSG(_(e_dictreq));
6715 return;
6716 }
6717 selfdict = argvars[2].vval.v_dict;
6718 }
6719
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006720 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6721 item = item->li_next)
6722 {
6723 if (argc == MAX_FUNC_ARGS)
6724 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006725 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 break;
6727 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006728 /* Make a copy of each argument. This is needed to be able to set
6729 * v_lock to VAR_FIXED in the copy without changing the original list.
6730 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006731 copy_tv(&item->li_tv, &argv[argc++]);
6732 }
6733
6734 if (item == NULL)
6735 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006736 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6737 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006738
6739 /* Free the arguments. */
6740 while (argc > 0)
6741 clear_tv(&argv[--argc]);
6742}
6743
6744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 * "char2nr(string)" function
6746 */
6747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006748f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006749 typval_T *argvars;
6750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752#ifdef FEAT_MBYTE
6753 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006754 rettv->vval.v_number =
6755 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 else
6757#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006758 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759}
6760
6761/*
6762 * "cindent(lnum)" function
6763 */
6764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006765f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006766 typval_T *argvars;
6767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769#ifdef FEAT_CINDENT
6770 pos_T pos;
6771 linenr_T lnum;
6772
6773 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6776 {
6777 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006778 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 curwin->w_cursor = pos;
6780 }
6781 else
6782#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
6785
6786/*
6787 * "col(string)" function
6788 */
6789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 typval_T *argvars;
6792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 colnr_T col = 0;
6795 pos_T *fp;
6796
6797 fp = var2fpos(&argvars[0], FALSE);
6798 if (fp != NULL)
6799 {
6800 if (fp->col == MAXCOL)
6801 {
6802 /* '> can be MAXCOL, get the length of the line then */
6803 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6804 col = STRLEN(ml_get(fp->lnum)) + 1;
6805 else
6806 col = MAXCOL;
6807 }
6808 else
6809 {
6810 col = fp->col + 1;
6811#ifdef FEAT_VIRTUALEDIT
6812 /* col(".") when the cursor is on the NUL at the end of the line
6813 * because of "coladd" can be seen as an extra column. */
6814 if (virtual_active() && fp == &curwin->w_cursor)
6815 {
6816 char_u *p = ml_get_cursor();
6817
6818 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6819 curwin->w_virtcol - curwin->w_cursor.coladd))
6820 {
6821# ifdef FEAT_MBYTE
6822 int l;
6823
6824 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6825 col += l;
6826# else
6827 if (*p != NUL && p[1] == NUL)
6828 ++col;
6829# endif
6830 }
6831 }
6832#endif
6833 }
6834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836}
6837
6838/*
6839 * "confirm(message, buttons[, default [, type]])" function
6840 */
6841/*ARGSUSED*/
6842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006844 typval_T *argvars;
6845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6848 char_u *message;
6849 char_u *buttons = NULL;
6850 char_u buf[NUMBUFLEN];
6851 char_u buf2[NUMBUFLEN];
6852 int def = 1;
6853 int type = VIM_GENERIC;
6854 int c;
6855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006856 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006859 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006860 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006863 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
Bram Moolenaara7043832005-01-21 11:56:39 +00006865 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006866 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 switch (TOUPPER_ASC(c))
6868 {
6869 case 'E': type = VIM_ERROR; break;
6870 case 'Q': type = VIM_QUESTION; break;
6871 case 'I': type = VIM_INFO; break;
6872 case 'W': type = VIM_WARNING; break;
6873 case 'G': type = VIM_GENERIC; break;
6874 }
6875 }
6876 }
6877 }
6878
6879 if (buttons == NULL || *buttons == NUL)
6880 buttons = (char_u *)_("&Ok");
6881
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006882 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 def, NULL);
6884#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006885 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886#endif
6887}
6888
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006889/*
6890 * "copy()" function
6891 */
6892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 typval_T *argvars;
6895 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
6900/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006901 * "count()" function
6902 */
6903 static void
6904f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 typval_T *argvars;
6906 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006907{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006908 long n = 0;
6909 int ic = FALSE;
6910
Bram Moolenaare9a41262005-01-15 22:18:47 +00006911 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006912 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 listitem_T *li;
6914 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006915 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006916
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 if ((l = argvars[0].vval.v_list) != NULL)
6918 {
6919 li = l->lv_first;
6920 if (argvars[2].v_type != VAR_UNKNOWN)
6921 {
6922 ic = get_tv_number(&argvars[2]);
6923 if (argvars[3].v_type != VAR_UNKNOWN)
6924 {
6925 idx = get_tv_number(&argvars[3]);
6926 li = list_find(l, idx);
6927 if (li == NULL)
6928 EMSGN(_(e_listidx), idx);
6929 }
6930 }
6931
6932 for ( ; li != NULL; li = li->li_next)
6933 if (tv_equal(&li->li_tv, &argvars[1], ic))
6934 ++n;
6935 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 else if (argvars[0].v_type == VAR_DICT)
6938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 int todo;
6940 dict_T *d;
6941 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 if ((d = argvars[0].vval.v_dict) != NULL)
6944 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 if (argvars[2].v_type != VAR_UNKNOWN)
6946 {
6947 ic = get_tv_number(&argvars[2]);
6948 if (argvars[3].v_type != VAR_UNKNOWN)
6949 EMSG(_(e_invarg));
6950 }
6951
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 todo = d->dv_hashtab.ht_used;
6953 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006954 {
6955 if (!HASHITEM_EMPTY(hi))
6956 {
6957 --todo;
6958 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
6959 ++n;
6960 }
6961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 }
6963 }
6964 else
6965 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006966 rettv->vval.v_number = n;
6967}
6968
6969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6971 *
6972 * Checks the existence of a cscope connection.
6973 */
6974/*ARGSUSED*/
6975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006976f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 typval_T *argvars;
6978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980#ifdef FEAT_CSCOPE
6981 int num = 0;
6982 char_u *dbpath = NULL;
6983 char_u *prepend = NULL;
6984 char_u buf[NUMBUFLEN];
6985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006986 if (argvars[0].v_type != VAR_UNKNOWN
6987 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989 num = (int)get_tv_number(&argvars[0]);
6990 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006995 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
6999}
7000
7001/*
7002 * "cursor(lnum, col)" function
7003 *
7004 * Moves the cursor to the specified line and column
7005 */
7006/*ARGSUSED*/
7007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007008f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 typval_T *argvars;
7010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 long line, col;
7013
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (line > 0)
7016 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007017 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (col > 0)
7019 curwin->w_cursor.col = col - 1;
7020#ifdef FEAT_VIRTUALEDIT
7021 curwin->w_cursor.coladd = 0;
7022#endif
7023
7024 /* Make sure the cursor is in a valid position. */
7025 check_cursor();
7026#ifdef FEAT_MBYTE
7027 /* Correct cursor for multi-byte character. */
7028 if (has_mbyte)
7029 mb_adjust_cursor();
7030#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007031
7032 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033}
7034
7035/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007036 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 */
7038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007039f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 typval_T *argvars;
7041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007043 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044}
7045
7046/*
7047 * "delete()" function
7048 */
7049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007050f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 typval_T *argvars;
7052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007057 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058}
7059
7060/*
7061 * "did_filetype()" function
7062 */
7063/*ARGSUSED*/
7064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007065f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 typval_T *argvars;
7067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007070 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#endif
7074}
7075
7076/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007077 * "diff_filler()" function
7078 */
7079/*ARGSUSED*/
7080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007081f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007082 typval_T *argvars;
7083 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007084{
7085#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007086 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007087#endif
7088}
7089
7090/*
7091 * "diff_hlID()" function
7092 */
7093/*ARGSUSED*/
7094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007095f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 typval_T *argvars;
7097 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007098{
7099#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007101 static linenr_T prev_lnum = 0;
7102 static int changedtick = 0;
7103 static int fnum = 0;
7104 static int change_start = 0;
7105 static int change_end = 0;
7106 static enum hlf_value hlID = 0;
7107 int filler_lines;
7108 int col;
7109
7110 if (lnum != prev_lnum
7111 || changedtick != curbuf->b_changedtick
7112 || fnum != curbuf->b_fnum)
7113 {
7114 /* New line, buffer, change: need to get the values. */
7115 filler_lines = diff_check(curwin, lnum);
7116 if (filler_lines < 0)
7117 {
7118 if (filler_lines == -1)
7119 {
7120 change_start = MAXCOL;
7121 change_end = -1;
7122 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7123 hlID = HLF_ADD; /* added line */
7124 else
7125 hlID = HLF_CHD; /* changed line */
7126 }
7127 else
7128 hlID = HLF_ADD; /* added line */
7129 }
7130 else
7131 hlID = (enum hlf_value)0;
7132 prev_lnum = lnum;
7133 changedtick = curbuf->b_changedtick;
7134 fnum = curbuf->b_fnum;
7135 }
7136
7137 if (hlID == HLF_CHD || hlID == HLF_TXD)
7138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007139 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007140 if (col >= change_start && col <= change_end)
7141 hlID = HLF_TXD; /* changed text */
7142 else
7143 hlID = HLF_CHD; /* changed line */
7144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007145 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007146#endif
7147}
7148
7149/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007150 * "empty({expr})" function
7151 */
7152 static void
7153f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 typval_T *argvars;
7155 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007156{
7157 int n;
7158
7159 switch (argvars[0].v_type)
7160 {
7161 case VAR_STRING:
7162 case VAR_FUNC:
7163 n = argvars[0].vval.v_string == NULL
7164 || *argvars[0].vval.v_string == NUL;
7165 break;
7166 case VAR_NUMBER:
7167 n = argvars[0].vval.v_number == 0;
7168 break;
7169 case VAR_LIST:
7170 n = argvars[0].vval.v_list == NULL
7171 || argvars[0].vval.v_list->lv_first == NULL;
7172 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 case VAR_DICT:
7174 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007177 default:
7178 EMSG2(_(e_intern2), "f_empty()");
7179 n = 0;
7180 }
7181
7182 rettv->vval.v_number = n;
7183}
7184
7185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 * "escape({string}, {chars})" function
7187 */
7188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007189f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 typval_T *argvars;
7191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 char_u buf[NUMBUFLEN];
7194
Bram Moolenaar758711c2005-02-02 23:11:38 +00007195 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7196 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007197 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198}
7199
7200/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007201 * "eval()" function
7202 */
7203/*ARGSUSED*/
7204 static void
7205f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 typval_T *argvars;
7207 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007208{
7209 char_u *s;
7210
7211 s = get_tv_string(&argvars[0]);
7212 s = skipwhite(s);
7213
7214 if (eval1(&s, rettv, TRUE) == FAIL)
7215 rettv->vval.v_number = 0;
7216 else if (*s != NUL)
7217 EMSG(_(e_trailing));
7218}
7219
7220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 * "eventhandler()" function
7222 */
7223/*ARGSUSED*/
7224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 typval_T *argvars;
7227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007229 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230}
7231
7232/*
7233 * "executable()" function
7234 */
7235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 typval_T *argvars;
7238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007240 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
7243/*
7244 * "exists()" function
7245 */
7246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007247f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 typval_T *argvars;
7249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
7251 char_u *p;
7252 char_u *name;
7253 int n = FALSE;
7254 int len = 0;
7255
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007256 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if (*p == '$') /* environment variable */
7258 {
7259 /* first try "normal" environment variables (fast) */
7260 if (mch_getenv(p + 1) != NULL)
7261 n = TRUE;
7262 else
7263 {
7264 /* try expanding things like $VIM and ${HOME} */
7265 p = expand_env_save(p);
7266 if (p != NULL && *p != '$')
7267 n = TRUE;
7268 vim_free(p);
7269 }
7270 }
7271 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007272 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 else if (*p == '*') /* internal or user defined function */
7274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 else if (*p == ':')
7278 {
7279 n = cmd_exists(p + 1);
7280 }
7281 else if (*p == '#')
7282 {
7283#ifdef FEAT_AUTOCMD
7284 name = p + 1;
7285 p = vim_strchr(name, '#');
7286 if (p != NULL)
7287 n = au_exists(name, p, p + 1);
7288 else
7289 n = au_exists(name, name + STRLEN(name), NULL);
7290#endif
7291 }
7292 else /* internal variable */
7293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007294 char_u *tofree;
7295 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007297 /* get_name_len() takes care of expanding curly braces */
7298 name = p;
7299 len = get_name_len(&p, &tofree, TRUE, FALSE);
7300 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007302 if (tofree != NULL)
7303 name = tofree;
7304 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7305 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007307 /* handle d.key, l[idx], f(expr) */
7308 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7309 if (n)
7310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007314 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
7316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007317 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
7320/*
7321 * "expand()" function
7322 */
7323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007324f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 typval_T *argvars;
7326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328 char_u *s;
7329 int len;
7330 char_u *errormsg;
7331 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7332 expand_T xpc;
7333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007334 rettv->v_type = VAR_STRING;
7335 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 if (*s == '%' || *s == '#' || *s == '<')
7337 {
7338 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007339 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 --emsg_off;
7341 }
7342 else
7343 {
7344 /* When the optional second argument is non-zero, don't remove matches
7345 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 flags |= WILD_KEEP_ALL;
7348 ExpandInit(&xpc);
7349 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007350 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 ExpandCleanup(&xpc);
7352 }
7353}
7354
7355/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007356 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007358 */
7359 static void
7360f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 typval_T *argvars;
7362 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007363{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007364 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007366 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 list_T *l1, *l2;
7368 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007370
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 l1 = argvars[0].vval.v_list;
7372 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007373 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7374 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 {
7376 if (argvars[2].v_type != VAR_UNKNOWN)
7377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007378 before = get_tv_number(&argvars[2]);
7379 if (before == l1->lv_len)
7380 item = NULL;
7381 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007383 item = list_find(l1, before);
7384 if (item == NULL)
7385 {
7386 EMSGN(_(e_listidx), before);
7387 return;
7388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 }
7390 }
7391 else
7392 item = NULL;
7393 list_extend(l1, l2, item);
7394
7395 ++l1->lv_refcount;
7396 copy_tv(&argvars[0], rettv);
7397 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7400 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 dict_T *d1, *d2;
7402 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 char_u *action;
7404 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407
7408 d1 = argvars[0].vval.v_dict;
7409 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007410 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7411 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 {
7413 /* Check the third argument. */
7414 if (argvars[2].v_type != VAR_UNKNOWN)
7415 {
7416 static char *(av[]) = {"keep", "force", "error"};
7417
7418 action = get_tv_string(&argvars[2]);
7419 for (i = 0; i < 3; ++i)
7420 if (STRCMP(action, av[i]) == 0)
7421 break;
7422 if (i == 3)
7423 {
7424 EMSGN(_(e_invarg2), action);
7425 return;
7426 }
7427 }
7428 else
7429 action = (char_u *)"force";
7430
7431 /* Go over all entries in the second dict and add them to the
7432 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 todo = d2->dv_hashtab.ht_used;
7434 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 --todo;
7439 di1 = dict_find(d1, hi2->hi_key, -1);
7440 if (di1 == NULL)
7441 {
7442 di1 = dictitem_copy(HI2DI(hi2));
7443 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7444 dictitem_free(di1);
7445 }
7446 else if (*action == 'e')
7447 {
7448 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7449 break;
7450 }
7451 else if (*action == 'f')
7452 {
7453 clear_tv(&di1->di_tv);
7454 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456 }
7457 }
7458
7459 ++d1->dv_refcount;
7460 copy_tv(&argvars[0], rettv);
7461 }
7462 }
7463 else
7464 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007465}
7466
7467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 * "filereadable()" function
7469 */
7470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 typval_T *argvars;
7473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 FILE *fd;
7476 char_u *p;
7477 int n;
7478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007479 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7481 {
7482 n = TRUE;
7483 fclose(fd);
7484 }
7485 else
7486 n = FALSE;
7487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491/*
7492 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7493 * rights to write into.
7494 */
7495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 typval_T *argvars;
7498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499{
7500 char_u *p;
7501 int retval = 0;
7502#if defined(UNIX) || defined(VMS)
7503 int perm = 0;
7504#endif
7505
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#if defined(UNIX) || defined(VMS)
7508 perm = mch_getperm(p);
7509#endif
7510#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7511 if (
7512# ifdef WIN3264
7513 mch_writable(p) &&
7514# else
7515# if defined(UNIX) || defined(VMS)
7516 (perm & 0222) &&
7517# endif
7518# endif
7519 mch_access((char *)p, W_OK) == 0
7520 )
7521#endif
7522 {
7523 ++retval;
7524 if (mch_isdir(p))
7525 ++retval;
7526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528}
7529
Bram Moolenaar33570922005-01-25 22:26:29 +00007530static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007531
7532 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007533findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 typval_T *argvars;
7535 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007536 int dir;
7537{
7538#ifdef FEAT_SEARCHPATH
7539 char_u *fname;
7540 char_u *fresult = NULL;
7541 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7542 char_u *p;
7543 char_u pathbuf[NUMBUFLEN];
7544 int count = 1;
7545 int first = TRUE;
7546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007548
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007552 if (*p != NUL)
7553 path = p;
7554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007557 }
7558
7559 do
7560 {
7561 vim_free(fresult);
7562 fresult = find_file_in_path_option(first ? fname : NULL,
7563 first ? (int)STRLEN(fname) : 0,
7564 0, first, path, dir, NULL);
7565 first = FALSE;
7566 } while (--count > 0 && fresult != NULL);
7567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007568 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007569#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007570 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007573}
7574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007575static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7576static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007577static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7578static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007579
7580/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007581 * Prepare v: variable "idx" to be used.
7582 * Save the current typeval in "save_tv".
7583 * When not used yet add the variable to the v: hashtable.
7584 */
7585 static void
7586prepare_vimvar(idx, save_tv)
7587 int idx;
7588 typval_T *save_tv;
7589{
7590 *save_tv = vimvars[idx].vv_tv;
7591 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7592 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
7593}
7594
7595/*
7596 * Restore v: variable "idx" to typeval "save_tv".
7597 * When no longer defined, remove the variable from the v: hashtable.
7598 */
7599 static void
7600restore_vimvar(idx, save_tv)
7601 int idx;
7602 typval_T *save_tv;
7603{
7604 hashitem_T *hi;
7605
7606 clear_tv(&vimvars[idx].vv_tv);
7607 vimvars[idx].vv_tv = *save_tv;
7608 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7609 {
7610 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
7611 if (HASHITEM_EMPTY(hi))
7612 EMSG2(_(e_intern2), "restore_vimvar()");
7613 else
7614 hash_remove(&vimvarht, hi);
7615 }
7616}
7617
7618/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007619 * Implementation of map() and filter().
7620 */
7621 static void
7622filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 typval_T *argvars;
7624 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007625 int map;
7626{
7627 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 listitem_T *li, *nli;
7630 list_T *l = NULL;
7631 dictitem_T *di;
7632 hashtab_T *ht;
7633 hashitem_T *hi;
7634 dict_T *d = NULL;
7635 typval_T save_val;
7636 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007639 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
7640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007641
7642 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007645 if ((l = argvars[0].vval.v_list) == NULL
7646 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 return;
7648 }
7649 else if (argvars[0].v_type == VAR_DICT)
7650 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007651 if ((d = argvars[0].vval.v_dict) == NULL
7652 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007653 return;
7654 }
7655 else
7656 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007657 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 return;
7659 }
7660
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007661 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007662 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663
7664 if (argvars[0].v_type == VAR_DICT)
7665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007666 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00007670 hash_lock(ht);
7671 todo = ht->ht_used;
7672 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007676 --todo;
7677 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007678 if (tv_check_lock(di->di_tv.v_lock, msg))
7679 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007681 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
7682 break;
7683 if (!map && rem)
7684 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007687 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007688 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007690 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007691 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 else
7693 {
7694 for (li = l->lv_first; li != NULL; li = nli)
7695 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007696 if (tv_check_lock(li->li_tv.v_lock, msg))
7697 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698 nli = li->li_next;
7699 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7700 break;
7701 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007702 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007703 }
7704 }
7705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007706 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007707
7708 copy_tv(&argvars[0], rettv);
7709}
7710
7711 static int
7712filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 char_u *expr;
7715 int map;
7716 int *remp;
7717{
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719 char_u *s;
7720
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 s = expr;
7723 if (eval1(&s, &rettv, TRUE) == FAIL)
7724 return FAIL;
7725 if (*s != NUL) /* check for trailing chars after expr */
7726 {
7727 EMSG2(_(e_invexpr2), s);
7728 return FAIL;
7729 }
7730 if (map)
7731 {
7732 /* map(): replace the list item value */
7733 clear_tv(tv);
7734 *tv = rettv;
7735 }
7736 else
7737 {
7738 /* filter(): when expr is zero remove the item */
7739 *remp = (get_tv_number(&rettv) == 0);
7740 clear_tv(&rettv);
7741 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007743 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007744}
7745
7746/*
7747 * "filter()" function
7748 */
7749 static void
7750f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *argvars;
7752 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753{
7754 filter_map(argvars, rettv, FALSE);
7755}
7756
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007758 * "finddir({fname}[, {path}[, {count}]])" function
7759 */
7760 static void
7761f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *argvars;
7763 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007764{
7765 findfilendir(argvars, rettv, TRUE);
7766}
7767
7768/*
7769 * "findfile({fname}[, {path}[, {count}]])" function
7770 */
7771 static void
7772f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 typval_T *argvars;
7774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007775{
7776 findfilendir(argvars, rettv, FALSE);
7777}
7778
7779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 * "fnamemodify({fname}, {mods})" function
7781 */
7782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 typval_T *argvars;
7785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 char_u *fname;
7788 char_u *mods;
7789 int usedlen = 0;
7790 int len;
7791 char_u *fbuf = NULL;
7792 char_u buf[NUMBUFLEN];
7793
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 fname = get_tv_string(&argvars[0]);
7795 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 len = (int)STRLEN(fname);
7797
7798 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7799
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 vim_free(fbuf);
7806}
7807
Bram Moolenaar33570922005-01-25 22:26:29 +00007808static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810/*
7811 * "foldclosed()" function
7812 */
7813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 typval_T *argvars;
7816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 int end;
7818{
7819#ifdef FEAT_FOLDING
7820 linenr_T lnum;
7821 linenr_T first, last;
7822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7825 {
7826 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7827 {
7828 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007829 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007831 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 return;
7833 }
7834 }
7835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
7839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007840 * "foldclosed()" function
7841 */
7842 static void
7843f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 typval_T *argvars;
7845 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007846{
7847 foldclosed_both(argvars, rettv, FALSE);
7848}
7849
7850/*
7851 * "foldclosedend()" function
7852 */
7853 static void
7854f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 typval_T *argvars;
7856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007857{
7858 foldclosed_both(argvars, rettv, TRUE);
7859}
7860
7861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 * "foldlevel()" function
7863 */
7864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 typval_T *argvars;
7867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869#ifdef FEAT_FOLDING
7870 linenr_T lnum;
7871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 else
7876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
7880/*
7881 * "foldtext()" function
7882 */
7883/*ARGSUSED*/
7884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 typval_T *argvars;
7887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889#ifdef FEAT_FOLDING
7890 linenr_T lnum;
7891 char_u *s;
7892 char_u *r;
7893 int len;
7894 char *txt;
7895#endif
7896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 rettv->v_type = VAR_STRING;
7898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7901 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7902 <= curbuf->b_ml.ml_line_count
7903 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
7905 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7907 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {
7909 if (!linewhite(lnum))
7910 break;
7911 ++lnum;
7912 }
7913
7914 /* Find interesting text in this line. */
7915 s = skipwhite(ml_get(lnum));
7916 /* skip C comment-start */
7917 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007920 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007921 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007922 {
7923 s = skipwhite(ml_get(lnum + 1));
7924 if (*s == '*')
7925 s = skipwhite(s + 1);
7926 }
7927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 txt = _("+-%s%3ld lines: ");
7929 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007930 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 + 20 /* for %3ld */
7932 + STRLEN(s))); /* concatenated */
7933 if (r != NULL)
7934 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7936 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7937 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 len = (int)STRLEN(r);
7939 STRCAT(r, s);
7940 /* remove 'foldmarker' and 'commentstring' */
7941 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
7944 }
7945#endif
7946}
7947
7948/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007949 * "foldtextresult(lnum)" function
7950 */
7951/*ARGSUSED*/
7952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 typval_T *argvars;
7955 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007956{
7957#ifdef FEAT_FOLDING
7958 linenr_T lnum;
7959 char_u *text;
7960 char_u buf[51];
7961 foldinfo_T foldinfo;
7962 int fold_count;
7963#endif
7964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->v_type = VAR_STRING;
7966 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007967#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007969 fold_count = foldedCount(curwin, lnum, &foldinfo);
7970 if (fold_count > 0)
7971 {
7972 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7973 &foldinfo, buf);
7974 if (text == buf)
7975 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007977 }
7978#endif
7979}
7980
7981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 * "foreground()" function
7983 */
7984/*ARGSUSED*/
7985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 typval_T *argvars;
7988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991#ifdef FEAT_GUI
7992 if (gui.in_use)
7993 gui_mch_set_foreground();
7994#else
7995# ifdef WIN32
7996 win32_set_foreground();
7997# endif
7998#endif
7999}
8000
8001/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 * "function()" function
8003 */
8004/*ARGSUSED*/
8005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 typval_T *argvars;
8008 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009{
8010 char_u *s;
8011
Bram Moolenaara7043832005-01-21 11:56:39 +00008012 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008014 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015 EMSG2(_(e_invarg2), s);
8016 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008017 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 else
8019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->vval.v_string = vim_strsave(s);
8021 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022 }
8023}
8024
8025/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008026 * "get()" function
8027 */
8028 static void
8029f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 typval_T *argvars;
8031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008032{
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 listitem_T *li;
8034 list_T *l;
8035 dictitem_T *di;
8036 dict_T *d;
8037 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008038
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008040 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008042 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008043 li = list_find(l, get_tv_number(&argvars[1]));
8044 if (li != NULL)
8045 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008048 else if (argvars[0].v_type == VAR_DICT)
8049 {
8050 if ((d = argvars[0].vval.v_dict) != NULL)
8051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008052 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 if (di != NULL)
8054 tv = &di->di_tv;
8055 }
8056 }
8057 else
8058 EMSG2(_(e_listdictarg), "get()");
8059
8060 if (tv == NULL)
8061 {
8062 if (argvars[2].v_type == VAR_UNKNOWN)
8063 rettv->vval.v_number = 0;
8064 else
8065 copy_tv(&argvars[2], rettv);
8066 }
8067 else
8068 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008069}
8070
8071/*
8072 * "getbufvar()" function
8073 */
8074 static void
8075f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 typval_T *argvars;
8077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008078{
8079 buf_T *buf;
8080 buf_T *save_curbuf;
8081 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008083
8084 ++emsg_off;
8085 buf = get_buf_tv(&argvars[0]);
8086 varname = get_tv_string(&argvars[1]);
8087
8088 rettv->v_type = VAR_STRING;
8089 rettv->vval.v_string = NULL;
8090
8091 if (buf != NULL && varname != NULL)
8092 {
8093 if (*varname == '&') /* buffer-local-option */
8094 {
8095 /* set curbuf to be our buf, temporarily */
8096 save_curbuf = curbuf;
8097 curbuf = buf;
8098
8099 get_option_tv(&varname, rettv, TRUE);
8100
8101 /* restore previous notion of curbuf */
8102 curbuf = save_curbuf;
8103 }
8104 else
8105 {
8106 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008108 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008109 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008110 }
8111 }
8112
8113 --emsg_off;
8114}
8115
8116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 * "getchar()" function
8118 */
8119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 typval_T *argvars;
8122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 varnumber_T n;
8125
8126 ++no_mapping;
8127 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 /* getchar(): blocking wait. */
8130 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008131 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 /* getchar(1): only check if char avail */
8133 n = vpeekc();
8134 else if (vpeekc() == NUL)
8135 /* getchar(0) and no char avail: return zero */
8136 n = 0;
8137 else
8138 /* getchar(0) and char avail: return char */
8139 n = safe_vgetc();
8140 --no_mapping;
8141 --allow_keys;
8142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008143 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 if (IS_SPECIAL(n) || mod_mask != 0)
8145 {
8146 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8147 int i = 0;
8148
8149 /* Turn a special key into three bytes, plus modifier. */
8150 if (mod_mask != 0)
8151 {
8152 temp[i++] = K_SPECIAL;
8153 temp[i++] = KS_MODIFIER;
8154 temp[i++] = mod_mask;
8155 }
8156 if (IS_SPECIAL(n))
8157 {
8158 temp[i++] = K_SPECIAL;
8159 temp[i++] = K_SECOND(n);
8160 temp[i++] = K_THIRD(n);
8161 }
8162#ifdef FEAT_MBYTE
8163 else if (has_mbyte)
8164 i += (*mb_char2bytes)(n, temp + i);
8165#endif
8166 else
8167 temp[i++] = n;
8168 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 rettv->v_type = VAR_STRING;
8170 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 }
8172}
8173
8174/*
8175 * "getcharmod()" function
8176 */
8177/*ARGSUSED*/
8178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008179f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008180 typval_T *argvars;
8181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008183 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184}
8185
8186/*
8187 * "getcmdline()" function
8188 */
8189/*ARGSUSED*/
8190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008191f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008192 typval_T *argvars;
8193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008195 rettv->v_type = VAR_STRING;
8196 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197}
8198
8199/*
8200 * "getcmdpos()" function
8201 */
8202/*ARGSUSED*/
8203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008204f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 typval_T *argvars;
8206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
8210
8211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 * "getcwd()" function
8213 */
8214/*ARGSUSED*/
8215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 typval_T *argvars;
8218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 char_u cwd[MAXPATHL];
8221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 else
8226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230#endif
8231 }
8232}
8233
8234/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008235 * "getfontname()" function
8236 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008237/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 typval_T *argvars;
8241 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 rettv->v_type = VAR_STRING;
8244 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008245#ifdef FEAT_GUI
8246 if (gui.in_use)
8247 {
8248 GuiFont font;
8249 char_u *name = NULL;
8250
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008251 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008252 {
8253 /* Get the "Normal" font. Either the name saved by
8254 * hl_set_font_name() or from the font ID. */
8255 font = gui.norm_font;
8256 name = hl_get_font_name();
8257 }
8258 else
8259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008261 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8262 return;
8263 font = gui_mch_get_font(name, FALSE);
8264 if (font == NOFONT)
8265 return; /* Invalid font name, return empty string. */
8266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008269 gui_mch_free_font(font);
8270 }
8271#endif
8272}
8273
8274/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008275 * "getfperm({fname})" function
8276 */
8277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008279 typval_T *argvars;
8280 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008281{
8282 char_u *fname;
8283 struct stat st;
8284 char_u *perm = NULL;
8285 char_u flags[] = "rwx";
8286 int i;
8287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008288 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008291 if (mch_stat((char *)fname, &st) >= 0)
8292 {
8293 perm = vim_strsave((char_u *)"---------");
8294 if (perm != NULL)
8295 {
8296 for (i = 0; i < 9; i++)
8297 {
8298 if (st.st_mode & (1 << (8 - i)))
8299 perm[i] = flags[i % 3];
8300 }
8301 }
8302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008303 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008304}
8305
8306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 * "getfsize({fname})" function
8308 */
8309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 typval_T *argvars;
8312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 char_u *fname;
8315 struct stat st;
8316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
8321 if (mch_stat((char *)fname, &st) >= 0)
8322 {
8323 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008324 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330}
8331
8332/*
8333 * "getftime({fname})" function
8334 */
8335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T *argvars;
8338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 char_u *fname;
8341 struct stat st;
8342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008346 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349}
8350
8351/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008352 * "getftype({fname})" function
8353 */
8354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008356 typval_T *argvars;
8357 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008358{
8359 char_u *fname;
8360 struct stat st;
8361 char_u *type = NULL;
8362 char *t;
8363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008365
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008367 if (mch_lstat((char *)fname, &st) >= 0)
8368 {
8369#ifdef S_ISREG
8370 if (S_ISREG(st.st_mode))
8371 t = "file";
8372 else if (S_ISDIR(st.st_mode))
8373 t = "dir";
8374# ifdef S_ISLNK
8375 else if (S_ISLNK(st.st_mode))
8376 t = "link";
8377# endif
8378# ifdef S_ISBLK
8379 else if (S_ISBLK(st.st_mode))
8380 t = "bdev";
8381# endif
8382# ifdef S_ISCHR
8383 else if (S_ISCHR(st.st_mode))
8384 t = "cdev";
8385# endif
8386# ifdef S_ISFIFO
8387 else if (S_ISFIFO(st.st_mode))
8388 t = "fifo";
8389# endif
8390# ifdef S_ISSOCK
8391 else if (S_ISSOCK(st.st_mode))
8392 t = "fifo";
8393# endif
8394 else
8395 t = "other";
8396#else
8397# ifdef S_IFMT
8398 switch (st.st_mode & S_IFMT)
8399 {
8400 case S_IFREG: t = "file"; break;
8401 case S_IFDIR: t = "dir"; break;
8402# ifdef S_IFLNK
8403 case S_IFLNK: t = "link"; break;
8404# endif
8405# ifdef S_IFBLK
8406 case S_IFBLK: t = "bdev"; break;
8407# endif
8408# ifdef S_IFCHR
8409 case S_IFCHR: t = "cdev"; break;
8410# endif
8411# ifdef S_IFIFO
8412 case S_IFIFO: t = "fifo"; break;
8413# endif
8414# ifdef S_IFSOCK
8415 case S_IFSOCK: t = "socket"; break;
8416# endif
8417 default: t = "other";
8418 }
8419# else
8420 if (mch_isdir(fname))
8421 t = "dir";
8422 else
8423 t = "file";
8424# endif
8425#endif
8426 type = vim_strsave((char_u *)t);
8427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008429}
8430
8431/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008432 * "getline(lnum)" function
8433 */
8434 static void
8435f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 typval_T *argvars;
8437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008438{
8439 linenr_T lnum;
8440 linenr_T end;
8441 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 list_T *l;
8443 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008444
8445 lnum = get_tv_lnum(argvars);
8446
8447 if (argvars[1].v_type == VAR_UNKNOWN)
8448 {
8449 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8450 p = ml_get(lnum);
8451 else
8452 p = (char_u *)"";
8453
8454 rettv->v_type = VAR_STRING;
8455 rettv->vval.v_string = vim_strsave(p);
8456 }
8457 else
8458 {
8459 end = get_tv_lnum(&argvars[1]);
8460 if (end < lnum)
8461 {
8462 EMSG(_(e_invrange));
8463 rettv->vval.v_number = 0;
8464 }
8465 else
8466 {
8467 l = list_alloc();
8468 if (l != NULL)
8469 {
8470 if (lnum < 1)
8471 lnum = 1;
8472 if (end > curbuf->b_ml.ml_line_count)
8473 end = curbuf->b_ml.ml_line_count;
8474 while (lnum <= end)
8475 {
8476 li = listitem_alloc();
8477 if (li == NULL)
8478 break;
8479 list_append(l, li);
8480 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008481 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8483 }
8484 rettv->vval.v_list = l;
8485 rettv->v_type = VAR_LIST;
8486 ++l->lv_refcount;
8487 }
8488 }
8489 }
8490}
8491
8492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 * "getreg()" function
8494 */
8495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 typval_T *argvars;
8498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 char_u *strregname;
8501 int regname;
8502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 regname = (strregname == NULL ? '"' : *strregname);
8508 if (regname == 0)
8509 regname = '"';
8510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511 rettv->v_type = VAR_STRING;
8512 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513}
8514
8515/*
8516 * "getregtype()" function
8517 */
8518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 typval_T *argvars;
8521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 char_u *strregname;
8524 int regname;
8525 char_u buf[NUMBUFLEN + 2];
8526 long reglen = 0;
8527
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 else
8531 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
8534 regname = (strregname == NULL ? '"' : *strregname);
8535 if (regname == 0)
8536 regname = '"';
8537
8538 buf[0] = NUL;
8539 buf[1] = NUL;
8540 switch (get_reg_type(regname, &reglen))
8541 {
8542 case MLINE: buf[0] = 'V'; break;
8543 case MCHAR: buf[0] = 'v'; break;
8544#ifdef FEAT_VISUAL
8545 case MBLOCK:
8546 buf[0] = Ctrl_V;
8547 sprintf((char *)buf + 1, "%ld", reglen + 1);
8548 break;
8549#endif
8550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551 rettv->v_type = VAR_STRING;
8552 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 * "getwinposx()" function
8557 */
8558/*ARGSUSED*/
8559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 typval_T *argvars;
8562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565#ifdef FEAT_GUI
8566 if (gui.in_use)
8567 {
8568 int x, y;
8569
8570 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573#endif
8574}
8575
8576/*
8577 * "getwinposy()" function
8578 */
8579/*ARGSUSED*/
8580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *argvars;
8583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586#ifdef FEAT_GUI
8587 if (gui.in_use)
8588 {
8589 int x, y;
8590
8591 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 }
8594#endif
8595}
8596
8597/*
8598 * "getwinvar()" function
8599 */
8600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008602 typval_T *argvars;
8603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 win_T *win, *oldcurwin;
8606 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609 ++emsg_off;
8610 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613 rettv->v_type = VAR_STRING;
8614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
8616 if (win != NULL && varname != NULL)
8617 {
8618 if (*varname == '&') /* window-local-option */
8619 {
8620 /* set curwin to be our win, temporarily */
8621 oldcurwin = curwin;
8622 curwin = win;
8623
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626 /* restore previous notion of curwin */
8627 curwin = oldcurwin;
8628 }
8629 else
8630 {
8631 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636 }
8637
8638 --emsg_off;
8639}
8640
8641/*
8642 * "glob()" function
8643 */
8644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 typval_T *argvars;
8647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 expand_T xpc;
8650
8651 ExpandInit(&xpc);
8652 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 rettv->v_type = VAR_STRING;
8654 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8656 ExpandCleanup(&xpc);
8657}
8658
8659/*
8660 * "globpath()" function
8661 */
8662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 typval_T *argvars;
8665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
8667 char_u buf1[NUMBUFLEN];
8668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 rettv->v_type = VAR_STRING;
8670 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8671 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672}
8673
8674/*
8675 * "has()" function
8676 */
8677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008678f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 typval_T *argvars;
8680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 int i;
8683 char_u *name;
8684 int n = FALSE;
8685 static char *(has_list[]) =
8686 {
8687#ifdef AMIGA
8688 "amiga",
8689# ifdef FEAT_ARP
8690 "arp",
8691# endif
8692#endif
8693#ifdef __BEOS__
8694 "beos",
8695#endif
8696#ifdef MSDOS
8697# ifdef DJGPP
8698 "dos32",
8699# else
8700 "dos16",
8701# endif
8702#endif
8703#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8704 "mac",
8705#endif
8706#if defined(MACOS_X_UNIX)
8707 "macunix",
8708#endif
8709#ifdef OS2
8710 "os2",
8711#endif
8712#ifdef __QNX__
8713 "qnx",
8714#endif
8715#ifdef RISCOS
8716 "riscos",
8717#endif
8718#ifdef UNIX
8719 "unix",
8720#endif
8721#ifdef VMS
8722 "vms",
8723#endif
8724#ifdef WIN16
8725 "win16",
8726#endif
8727#ifdef WIN32
8728 "win32",
8729#endif
8730#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8731 "win32unix",
8732#endif
8733#ifdef WIN64
8734 "win64",
8735#endif
8736#ifdef EBCDIC
8737 "ebcdic",
8738#endif
8739#ifndef CASE_INSENSITIVE_FILENAME
8740 "fname_case",
8741#endif
8742#ifdef FEAT_ARABIC
8743 "arabic",
8744#endif
8745#ifdef FEAT_AUTOCMD
8746 "autocmd",
8747#endif
8748#ifdef FEAT_BEVAL
8749 "balloon_eval",
8750#endif
8751#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8752 "builtin_terms",
8753# ifdef ALL_BUILTIN_TCAPS
8754 "all_builtin_terms",
8755# endif
8756#endif
8757#ifdef FEAT_BYTEOFF
8758 "byte_offset",
8759#endif
8760#ifdef FEAT_CINDENT
8761 "cindent",
8762#endif
8763#ifdef FEAT_CLIENTSERVER
8764 "clientserver",
8765#endif
8766#ifdef FEAT_CLIPBOARD
8767 "clipboard",
8768#endif
8769#ifdef FEAT_CMDL_COMPL
8770 "cmdline_compl",
8771#endif
8772#ifdef FEAT_CMDHIST
8773 "cmdline_hist",
8774#endif
8775#ifdef FEAT_COMMENTS
8776 "comments",
8777#endif
8778#ifdef FEAT_CRYPT
8779 "cryptv",
8780#endif
8781#ifdef FEAT_CSCOPE
8782 "cscope",
8783#endif
8784#ifdef DEBUG
8785 "debug",
8786#endif
8787#ifdef FEAT_CON_DIALOG
8788 "dialog_con",
8789#endif
8790#ifdef FEAT_GUI_DIALOG
8791 "dialog_gui",
8792#endif
8793#ifdef FEAT_DIFF
8794 "diff",
8795#endif
8796#ifdef FEAT_DIGRAPHS
8797 "digraphs",
8798#endif
8799#ifdef FEAT_DND
8800 "dnd",
8801#endif
8802#ifdef FEAT_EMACS_TAGS
8803 "emacs_tags",
8804#endif
8805 "eval", /* always present, of course! */
8806#ifdef FEAT_EX_EXTRA
8807 "ex_extra",
8808#endif
8809#ifdef FEAT_SEARCH_EXTRA
8810 "extra_search",
8811#endif
8812#ifdef FEAT_FKMAP
8813 "farsi",
8814#endif
8815#ifdef FEAT_SEARCHPATH
8816 "file_in_path",
8817#endif
8818#ifdef FEAT_FIND_ID
8819 "find_in_path",
8820#endif
8821#ifdef FEAT_FOLDING
8822 "folding",
8823#endif
8824#ifdef FEAT_FOOTER
8825 "footer",
8826#endif
8827#if !defined(USE_SYSTEM) && defined(UNIX)
8828 "fork",
8829#endif
8830#ifdef FEAT_GETTEXT
8831 "gettext",
8832#endif
8833#ifdef FEAT_GUI
8834 "gui",
8835#endif
8836#ifdef FEAT_GUI_ATHENA
8837# ifdef FEAT_GUI_NEXTAW
8838 "gui_neXtaw",
8839# else
8840 "gui_athena",
8841# endif
8842#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008843#ifdef FEAT_GUI_KDE
8844 "gui_kde",
8845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846#ifdef FEAT_GUI_GTK
8847 "gui_gtk",
8848# ifdef HAVE_GTK2
8849 "gui_gtk2",
8850# endif
8851#endif
8852#ifdef FEAT_GUI_MAC
8853 "gui_mac",
8854#endif
8855#ifdef FEAT_GUI_MOTIF
8856 "gui_motif",
8857#endif
8858#ifdef FEAT_GUI_PHOTON
8859 "gui_photon",
8860#endif
8861#ifdef FEAT_GUI_W16
8862 "gui_win16",
8863#endif
8864#ifdef FEAT_GUI_W32
8865 "gui_win32",
8866#endif
8867#ifdef FEAT_HANGULIN
8868 "hangul_input",
8869#endif
8870#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8871 "iconv",
8872#endif
8873#ifdef FEAT_INS_EXPAND
8874 "insert_expand",
8875#endif
8876#ifdef FEAT_JUMPLIST
8877 "jumplist",
8878#endif
8879#ifdef FEAT_KEYMAP
8880 "keymap",
8881#endif
8882#ifdef FEAT_LANGMAP
8883 "langmap",
8884#endif
8885#ifdef FEAT_LIBCALL
8886 "libcall",
8887#endif
8888#ifdef FEAT_LINEBREAK
8889 "linebreak",
8890#endif
8891#ifdef FEAT_LISP
8892 "lispindent",
8893#endif
8894#ifdef FEAT_LISTCMDS
8895 "listcmds",
8896#endif
8897#ifdef FEAT_LOCALMAP
8898 "localmap",
8899#endif
8900#ifdef FEAT_MENU
8901 "menu",
8902#endif
8903#ifdef FEAT_SESSION
8904 "mksession",
8905#endif
8906#ifdef FEAT_MODIFY_FNAME
8907 "modify_fname",
8908#endif
8909#ifdef FEAT_MOUSE
8910 "mouse",
8911#endif
8912#ifdef FEAT_MOUSESHAPE
8913 "mouseshape",
8914#endif
8915#if defined(UNIX) || defined(VMS)
8916# ifdef FEAT_MOUSE_DEC
8917 "mouse_dec",
8918# endif
8919# ifdef FEAT_MOUSE_GPM
8920 "mouse_gpm",
8921# endif
8922# ifdef FEAT_MOUSE_JSB
8923 "mouse_jsbterm",
8924# endif
8925# ifdef FEAT_MOUSE_NET
8926 "mouse_netterm",
8927# endif
8928# ifdef FEAT_MOUSE_PTERM
8929 "mouse_pterm",
8930# endif
8931# ifdef FEAT_MOUSE_XTERM
8932 "mouse_xterm",
8933# endif
8934#endif
8935#ifdef FEAT_MBYTE
8936 "multi_byte",
8937#endif
8938#ifdef FEAT_MBYTE_IME
8939 "multi_byte_ime",
8940#endif
8941#ifdef FEAT_MULTI_LANG
8942 "multi_lang",
8943#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008944#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00008945#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008946 "mzscheme",
8947#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949#ifdef FEAT_OLE
8950 "ole",
8951#endif
8952#ifdef FEAT_OSFILETYPE
8953 "osfiletype",
8954#endif
8955#ifdef FEAT_PATH_EXTRA
8956 "path_extra",
8957#endif
8958#ifdef FEAT_PERL
8959#ifndef DYNAMIC_PERL
8960 "perl",
8961#endif
8962#endif
8963#ifdef FEAT_PYTHON
8964#ifndef DYNAMIC_PYTHON
8965 "python",
8966#endif
8967#endif
8968#ifdef FEAT_POSTSCRIPT
8969 "postscript",
8970#endif
8971#ifdef FEAT_PRINTER
8972 "printer",
8973#endif
8974#ifdef FEAT_QUICKFIX
8975 "quickfix",
8976#endif
8977#ifdef FEAT_RIGHTLEFT
8978 "rightleft",
8979#endif
8980#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8981 "ruby",
8982#endif
8983#ifdef FEAT_SCROLLBIND
8984 "scrollbind",
8985#endif
8986#ifdef FEAT_CMDL_INFO
8987 "showcmd",
8988 "cmdline_info",
8989#endif
8990#ifdef FEAT_SIGNS
8991 "signs",
8992#endif
8993#ifdef FEAT_SMARTINDENT
8994 "smartindent",
8995#endif
8996#ifdef FEAT_SNIFF
8997 "sniff",
8998#endif
8999#ifdef FEAT_STL_OPT
9000 "statusline",
9001#endif
9002#ifdef FEAT_SUN_WORKSHOP
9003 "sun_workshop",
9004#endif
9005#ifdef FEAT_NETBEANS_INTG
9006 "netbeans_intg",
9007#endif
9008#ifdef FEAT_SYN_HL
9009 "syntax",
9010#endif
9011#if defined(USE_SYSTEM) || !defined(UNIX)
9012 "system",
9013#endif
9014#ifdef FEAT_TAG_BINS
9015 "tag_binary",
9016#endif
9017#ifdef FEAT_TAG_OLDSTATIC
9018 "tag_old_static",
9019#endif
9020#ifdef FEAT_TAG_ANYWHITE
9021 "tag_any_white",
9022#endif
9023#ifdef FEAT_TCL
9024# ifndef DYNAMIC_TCL
9025 "tcl",
9026# endif
9027#endif
9028#ifdef TERMINFO
9029 "terminfo",
9030#endif
9031#ifdef FEAT_TERMRESPONSE
9032 "termresponse",
9033#endif
9034#ifdef FEAT_TEXTOBJ
9035 "textobjects",
9036#endif
9037#ifdef HAVE_TGETENT
9038 "tgetent",
9039#endif
9040#ifdef FEAT_TITLE
9041 "title",
9042#endif
9043#ifdef FEAT_TOOLBAR
9044 "toolbar",
9045#endif
9046#ifdef FEAT_USR_CMDS
9047 "user-commands", /* was accidentally included in 5.4 */
9048 "user_commands",
9049#endif
9050#ifdef FEAT_VIMINFO
9051 "viminfo",
9052#endif
9053#ifdef FEAT_VERTSPLIT
9054 "vertsplit",
9055#endif
9056#ifdef FEAT_VIRTUALEDIT
9057 "virtualedit",
9058#endif
9059#ifdef FEAT_VISUAL
9060 "visual",
9061#endif
9062#ifdef FEAT_VISUALEXTRA
9063 "visualextra",
9064#endif
9065#ifdef FEAT_VREPLACE
9066 "vreplace",
9067#endif
9068#ifdef FEAT_WILDIGN
9069 "wildignore",
9070#endif
9071#ifdef FEAT_WILDMENU
9072 "wildmenu",
9073#endif
9074#ifdef FEAT_WINDOWS
9075 "windows",
9076#endif
9077#ifdef FEAT_WAK
9078 "winaltkeys",
9079#endif
9080#ifdef FEAT_WRITEBACKUP
9081 "writebackup",
9082#endif
9083#ifdef FEAT_XIM
9084 "xim",
9085#endif
9086#ifdef FEAT_XFONTSET
9087 "xfontset",
9088#endif
9089#ifdef USE_XSMP
9090 "xsmp",
9091#endif
9092#ifdef USE_XSMP_INTERACT
9093 "xsmp_interact",
9094#endif
9095#ifdef FEAT_XCLIPBOARD
9096 "xterm_clipboard",
9097#endif
9098#ifdef FEAT_XTERM_SAVE
9099 "xterm_save",
9100#endif
9101#if defined(UNIX) && defined(FEAT_X11)
9102 "X11",
9103#endif
9104 NULL
9105 };
9106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 for (i = 0; has_list[i] != NULL; ++i)
9109 if (STRICMP(name, has_list[i]) == 0)
9110 {
9111 n = TRUE;
9112 break;
9113 }
9114
9115 if (n == FALSE)
9116 {
9117 if (STRNICMP(name, "patch", 5) == 0)
9118 n = has_patch(atoi((char *)name + 5));
9119 else if (STRICMP(name, "vim_starting") == 0)
9120 n = (starting != 0);
9121#ifdef DYNAMIC_TCL
9122 else if (STRICMP(name, "tcl") == 0)
9123 n = tcl_enabled(FALSE);
9124#endif
9125#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9126 else if (STRICMP(name, "iconv") == 0)
9127 n = iconv_enabled(FALSE);
9128#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009129#ifdef DYNAMIC_MZSCHEME
9130 else if (STRICMP(name, "mzscheme") == 0)
9131 n = mzscheme_enabled(FALSE);
9132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#ifdef DYNAMIC_RUBY
9134 else if (STRICMP(name, "ruby") == 0)
9135 n = ruby_enabled(FALSE);
9136#endif
9137#ifdef DYNAMIC_PYTHON
9138 else if (STRICMP(name, "python") == 0)
9139 n = python_enabled(FALSE);
9140#endif
9141#ifdef DYNAMIC_PERL
9142 else if (STRICMP(name, "perl") == 0)
9143 n = perl_enabled(FALSE);
9144#endif
9145#ifdef FEAT_GUI
9146 else if (STRICMP(name, "gui_running") == 0)
9147 n = (gui.in_use || gui.starting);
9148# ifdef FEAT_GUI_W32
9149 else if (STRICMP(name, "gui_win32s") == 0)
9150 n = gui_is_win32s();
9151# endif
9152# ifdef FEAT_BROWSE
9153 else if (STRICMP(name, "browse") == 0)
9154 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9155# endif
9156#endif
9157#ifdef FEAT_SYN_HL
9158 else if (STRICMP(name, "syntax_items") == 0)
9159 n = syntax_present(curbuf);
9160#endif
9161#if defined(WIN3264)
9162 else if (STRICMP(name, "win95") == 0)
9163 n = mch_windows95();
9164#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009165#ifdef FEAT_NETBEANS_INTG
9166 else if (STRICMP(name, "netbeans_enabled") == 0)
9167 n = usingNetbeans;
9168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
9170
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009175 * "has_key()" function
9176 */
9177 static void
9178f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009179 typval_T *argvars;
9180 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009181{
9182 rettv->vval.v_number = 0;
9183 if (argvars[0].v_type != VAR_DICT)
9184 {
9185 EMSG(_(e_dictreq));
9186 return;
9187 }
9188 if (argvars[0].vval.v_dict == NULL)
9189 return;
9190
9191 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009192 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009193}
9194
9195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 * "hasmapto()" function
9197 */
9198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009200 typval_T *argvars;
9201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 char_u *name;
9204 char_u *mode;
9205 char_u buf[NUMBUFLEN];
9206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009208 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 mode = (char_u *)"nvo";
9210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212
9213 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217}
9218
9219/*
9220 * "histadd()" function
9221 */
9222/*ARGSUSED*/
9223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009225 typval_T *argvars;
9226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227{
9228#ifdef FEAT_CMDHIST
9229 int histype;
9230 char_u *str;
9231 char_u buf[NUMBUFLEN];
9232#endif
9233
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (check_restricted() || check_secure())
9236 return;
9237#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 if (histype >= 0)
9240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009241 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 if (*str != NUL)
9243 {
9244 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009245 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 return;
9247 }
9248 }
9249#endif
9250}
9251
9252/*
9253 * "histdel()" function
9254 */
9255/*ARGSUSED*/
9256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009257f_histdel(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#ifdef FEAT_CMDHIST
9262 int n;
9263 char_u buf[NUMBUFLEN];
9264
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009265 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009268 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9271 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 else
9273 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9275 get_tv_string_buf(&argvars[1], buf));
9276 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279#endif
9280}
9281
9282/*
9283 * "histget()" function
9284 */
9285/*ARGSUSED*/
9286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009288 typval_T *argvars;
9289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291#ifdef FEAT_CMDHIST
9292 int type;
9293 int idx;
9294
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009296 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 idx = get_history_idx(type);
9298 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 idx = (int)get_tv_number(&argvars[1]);
9300 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305}
9306
9307/*
9308 * "histnr()" function
9309 */
9310/*ARGSUSED*/
9311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 typval_T *argvars;
9314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 int i;
9317
9318#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 if (i >= HIST_CMD && i < HIST_COUNT)
9321 i = get_history_idx(i);
9322 else
9323#endif
9324 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326}
9327
9328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 * "highlightID(name)" function
9330 */
9331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009333 typval_T *argvars;
9334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337}
9338
9339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009340 * "highlight_exists()" function
9341 */
9342 static void
9343f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T *argvars;
9345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009346{
9347 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9348}
9349
9350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 * "hostname()" function
9352 */
9353/*ARGSUSED*/
9354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
9359 char_u hostname[256];
9360
9361 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->v_type = VAR_STRING;
9363 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364}
9365
9366/*
9367 * iconv() function
9368 */
9369/*ARGSUSED*/
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375#ifdef FEAT_MBYTE
9376 char_u buf1[NUMBUFLEN];
9377 char_u buf2[NUMBUFLEN];
9378 char_u *from, *to, *str;
9379 vimconv_T vimconv;
9380#endif
9381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 rettv->v_type = VAR_STRING;
9383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384
9385#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386 str = get_tv_string(&argvars[0]);
9387 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9388 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 vimconv.vc_type = CONV_NONE;
9390 convert_setup(&vimconv, from, to);
9391
9392 /* If the encodings are equal, no conversion needed. */
9393 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
9398 convert_setup(&vimconv, NULL, NULL);
9399 vim_free(from);
9400 vim_free(to);
9401#endif
9402}
9403
9404/*
9405 * "indent()" function
9406 */
9407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 typval_T *argvars;
9410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
9412 linenr_T lnum;
9413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
9420
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009421/*
9422 * "index()" function
9423 */
9424 static void
9425f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009426 typval_T *argvars;
9427 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009428{
Bram Moolenaar33570922005-01-25 22:26:29 +00009429 list_T *l;
9430 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009431 long idx = 0;
9432 int ic = FALSE;
9433
9434 rettv->vval.v_number = -1;
9435 if (argvars[0].v_type != VAR_LIST)
9436 {
9437 EMSG(_(e_listreq));
9438 return;
9439 }
9440 l = argvars[0].vval.v_list;
9441 if (l != NULL)
9442 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009443 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009444 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009445 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009446 /* Start at specified item. Use the cached index that list_find()
9447 * sets, so that a negative number also works. */
9448 item = list_find(l, get_tv_number(&argvars[2]));
9449 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009450 if (argvars[3].v_type != VAR_UNKNOWN)
9451 ic = get_tv_number(&argvars[3]);
9452 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009453
Bram Moolenaar758711c2005-02-02 23:11:38 +00009454 for ( ; item != NULL; item = item->li_next, ++idx)
9455 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009456 {
9457 rettv->vval.v_number = idx;
9458 break;
9459 }
9460 }
9461}
9462
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463static int inputsecret_flag = 0;
9464
9465/*
9466 * "input()" function
9467 * Also handles inputsecret() when inputsecret is set.
9468 */
9469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *argvars;
9472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 char_u *p = NULL;
9476 int c;
9477 char_u buf[NUMBUFLEN];
9478 int cmd_silent_save = cmd_silent;
9479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482#ifdef NO_CONSOLE_INPUT
9483 /* While starting up, there is no place to enter text. */
9484 if (no_console_input())
9485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 return;
9488 }
9489#endif
9490
9491 cmd_silent = FALSE; /* Want to see the prompt. */
9492 if (prompt != NULL)
9493 {
9494 /* Only the part of the message after the last NL is considered as
9495 * prompt for the command line */
9496 p = vim_strrchr(prompt, '\n');
9497 if (p == NULL)
9498 p = prompt;
9499 else
9500 {
9501 ++p;
9502 c = *p;
9503 *p = NUL;
9504 msg_start();
9505 msg_clr_eos();
9506 msg_puts_attr(prompt, echo_attr);
9507 msg_didout = FALSE;
9508 msg_starthere();
9509 *p = c;
9510 }
9511 cmdline_row = msg_row;
9512 }
9513
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9519
9520 /* since the user typed this, no need to wait for return */
9521 need_wait_return = FALSE;
9522 msg_didout = FALSE;
9523 cmd_silent = cmd_silent_save;
9524}
9525
9526/*
9527 * "inputdialog()" function
9528 */
9529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009531 typval_T *argvars;
9532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533{
9534#if defined(FEAT_GUI_TEXTDIALOG)
9535 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9536 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9537 {
9538 char_u *message;
9539 char_u buf[NUMBUFLEN];
9540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009542 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 IObuff[IOSIZE - 1] = NUL;
9546 }
9547 else
9548 IObuff[0] = NUL;
9549 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9550 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 else
9553 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009554 if (argvars[1].v_type != VAR_UNKNOWN
9555 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_string = vim_strsave(
9557 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 }
9563 else
9564#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566}
9567
9568static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9569
9570/*
9571 * "inputrestore()" function
9572 */
9573/*ARGSUSED*/
9574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 if (ga_userinput.ga_len > 0)
9580 {
9581 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9583 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 }
9586 else if (p_verbose > 1)
9587 {
9588 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 }
9591}
9592
9593/*
9594 * "inputsave()" function
9595 */
9596/*ARGSUSED*/
9597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009599 typval_T *argvars;
9600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 /* Add an entry to the stack of typehead storage. */
9603 if (ga_grow(&ga_userinput, 1) == OK)
9604 {
9605 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9606 + ga_userinput.ga_len);
9607 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 }
9610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612}
9613
9614/*
9615 * "inputsecret()" function
9616 */
9617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009619 typval_T *argvars;
9620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621{
9622 ++cmdline_star;
9623 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 --cmdline_star;
9626 --inputsecret_flag;
9627}
9628
9629/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009630 * "insert()" function
9631 */
9632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 typval_T *argvars;
9635 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009636{
9637 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009638 listitem_T *item;
9639 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009640
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009641 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009642 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009643 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009644 else if ((l = argvars[0].vval.v_list) != NULL
9645 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009646 {
9647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009649
Bram Moolenaar758711c2005-02-02 23:11:38 +00009650 if (before == l->lv_len)
9651 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 else
9653 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009654 item = list_find(l, before);
9655 if (item == NULL)
9656 {
9657 EMSGN(_(e_listidx), before);
9658 l = NULL;
9659 }
9660 }
9661 if (l != NULL)
9662 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009663 list_insert_tv(l, &argvars[1], item);
9664 ++l->lv_refcount;
9665 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009666 }
9667 }
9668}
9669
9670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 * "isdirectory()" function
9672 */
9673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009675 typval_T *argvars;
9676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679}
9680
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009681static int tv_islocked __ARGS((typval_T *tv));
9682
9683/*
9684 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
9685 * it refers to a List or Dictionary that is locked.
9686 */
9687 static int
9688tv_islocked(tv)
9689 typval_T *tv;
9690{
9691 return (tv->v_lock & VAR_LOCKED)
9692 || (tv->v_type == VAR_LIST
9693 && tv->vval.v_list != NULL
9694 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
9695 || (tv->v_type == VAR_DICT
9696 && tv->vval.v_dict != NULL
9697 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
9698}
9699
9700/*
9701 * "islocked()" function
9702 */
9703 static void
9704f_islocked(argvars, rettv)
9705 typval_T *argvars;
9706 typval_T *rettv;
9707{
9708 lval_T lv;
9709 char_u *end;
9710 dictitem_T *di;
9711
9712 rettv->vval.v_number = -1;
9713 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
9714 if (end != NULL && lv.ll_name != NULL)
9715 {
9716 if (*end != NUL)
9717 EMSG(_(e_trailing));
9718 else
9719 {
9720 if (lv.ll_tv == NULL)
9721 {
9722 if (check_changedtick(lv.ll_name))
9723 rettv->vval.v_number = 1; /* always locked */
9724 else
9725 {
9726 di = find_var(lv.ll_name, NULL);
9727 if (di != NULL)
9728 {
9729 /* Consider a variable locked when:
9730 * 1. the variable itself is locked
9731 * 2. the value of the variable is locked.
9732 * 3. the List or Dict value is locked.
9733 */
9734 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
9735 || tv_islocked(&di->di_tv));
9736 }
9737 }
9738 }
9739 else if (lv.ll_range)
9740 EMSG(_("E745: Range not allowed"));
9741 else if (lv.ll_newkey != NULL)
9742 EMSG2(_(e_dictkey), lv.ll_newkey);
9743 else if (lv.ll_list != NULL)
9744 /* List item. */
9745 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
9746 else
9747 /* Dictionary item. */
9748 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
9749 }
9750 }
9751
9752 clear_lval(&lv);
9753}
9754
Bram Moolenaar33570922005-01-25 22:26:29 +00009755static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +00009756
9757/*
9758 * Turn a dict into a list:
9759 * "what" == 0: list of keys
9760 * "what" == 1: list of values
9761 * "what" == 2: list of items
9762 */
9763 static void
9764dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009767 int what;
9768{
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 list_T *l;
9770 list_T *l2;
9771 dictitem_T *di;
9772 hashitem_T *hi;
9773 listitem_T *li;
9774 listitem_T *li2;
9775 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009776 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009777
9778 rettv->vval.v_number = 0;
9779 if (argvars[0].v_type != VAR_DICT)
9780 {
9781 EMSG(_(e_dictreq));
9782 return;
9783 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009784 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009785 return;
9786
9787 l = list_alloc();
9788 if (l == NULL)
9789 return;
9790 rettv->v_type = VAR_LIST;
9791 rettv->vval.v_list = l;
9792 ++l->lv_refcount;
9793
Bram Moolenaar33570922005-01-25 22:26:29 +00009794 todo = d->dv_hashtab.ht_used;
9795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009796 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009797 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00009798 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009799 --todo;
9800 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009801
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009802 li = listitem_alloc();
9803 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009804 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009805 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009807 if (what == 0)
9808 {
9809 /* keys() */
9810 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009811 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009812 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9813 }
9814 else if (what == 1)
9815 {
9816 /* values() */
9817 copy_tv(&di->di_tv, &li->li_tv);
9818 }
9819 else
9820 {
9821 /* items() */
9822 l2 = list_alloc();
9823 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009824 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009825 li->li_tv.vval.v_list = l2;
9826 if (l2 == NULL)
9827 break;
9828 ++l2->lv_refcount;
9829
9830 li2 = listitem_alloc();
9831 if (li2 == NULL)
9832 break;
9833 list_append(l2, li2);
9834 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009835 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009836 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9837
9838 li2 = listitem_alloc();
9839 if (li2 == NULL)
9840 break;
9841 list_append(l2, li2);
9842 copy_tv(&di->di_tv, &li2->li_tv);
9843 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00009844 }
9845 }
9846}
9847
9848/*
9849 * "items(dict)" function
9850 */
9851 static void
9852f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009853 typval_T *argvars;
9854 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009855{
9856 dict_list(argvars, rettv, 2);
9857}
9858
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009860 * "join()" function
9861 */
9862 static void
9863f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009866{
9867 garray_T ga;
9868 char_u *sep;
9869
9870 rettv->vval.v_number = 0;
9871 if (argvars[0].v_type != VAR_LIST)
9872 {
9873 EMSG(_(e_listreq));
9874 return;
9875 }
9876 if (argvars[0].vval.v_list == NULL)
9877 return;
9878 if (argvars[1].v_type == VAR_UNKNOWN)
9879 sep = (char_u *)" ";
9880 else
9881 sep = get_tv_string(&argvars[1]);
9882
9883 ga_init2(&ga, (int)sizeof(char), 80);
9884 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9885 ga_append(&ga, NUL);
9886
9887 rettv->v_type = VAR_STRING;
9888 rettv->vval.v_string = (char_u *)ga.ga_data;
9889}
9890
9891/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009892 * "keys()" function
9893 */
9894 static void
9895f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009898{
9899 dict_list(argvars, rettv, 0);
9900}
9901
9902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 * "last_buffer_nr()" function.
9904 */
9905/*ARGSUSED*/
9906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 int n = 0;
9912 buf_T *buf;
9913
9914 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9915 if (n < buf->b_fnum)
9916 n = buf->b_fnum;
9917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009919}
9920
9921/*
9922 * "len()" function
9923 */
9924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 typval_T *argvars;
9927 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009928{
9929 switch (argvars[0].v_type)
9930 {
9931 case VAR_STRING:
9932 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 rettv->vval.v_number = (varnumber_T)STRLEN(
9934 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009935 break;
9936 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009938 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 case VAR_DICT:
9940 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9941 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009942 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009943 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009944 break;
9945 }
9946}
9947
Bram Moolenaar33570922005-01-25 22:26:29 +00009948static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009949
9950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 typval_T *argvars;
9953 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009954 int type;
9955{
9956#ifdef FEAT_LIBCALL
9957 char_u *string_in;
9958 char_u **string_result;
9959 int nr_result;
9960#endif
9961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009962 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009963 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009967
9968 if (check_restricted() || check_secure())
9969 return;
9970
9971#ifdef FEAT_LIBCALL
9972 /* The first two args must be strings, otherwise its meaningless */
9973 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9974 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009975 string_in = NULL;
9976 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009977 string_in = argvars[2].vval.v_string;
9978 if (type == VAR_NUMBER)
9979 string_result = NULL;
9980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009982 if (mch_libcall(argvars[0].vval.v_string,
9983 argvars[1].vval.v_string,
9984 string_in,
9985 argvars[2].vval.v_number,
9986 string_result,
9987 &nr_result) == OK
9988 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009990 }
9991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992}
9993
9994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009995 * "libcall()" function
9996 */
9997 static void
9998f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009999 typval_T *argvars;
10000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010001{
10002 libcall_common(argvars, rettv, VAR_STRING);
10003}
10004
10005/*
10006 * "libcallnr()" function
10007 */
10008 static void
10009f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *argvars;
10011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010012{
10013 libcall_common(argvars, rettv, VAR_NUMBER);
10014}
10015
10016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 * "line(string)" function
10018 */
10019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024 linenr_T lnum = 0;
10025 pos_T *fp;
10026
10027 fp = var2fpos(&argvars[0], TRUE);
10028 if (fp != NULL)
10029 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031}
10032
10033/*
10034 * "line2byte(lnum)" function
10035 */
10036/*ARGSUSED*/
10037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *argvars;
10040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
10042#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#else
10045 linenr_T lnum;
10046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10052 if (rettv->vval.v_number >= 0)
10053 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054#endif
10055}
10056
10057/*
10058 * "lispindent(lnum)" function
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
10065#ifdef FEAT_LISP
10066 pos_T pos;
10067 linenr_T lnum;
10068
10069 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10072 {
10073 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 curwin->w_cursor = pos;
10076 }
10077 else
10078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080}
10081
10082/*
10083 * "localtime()" function
10084 */
10085/*ARGSUSED*/
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092}
10093
Bram Moolenaar33570922005-01-25 22:26:29 +000010094static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
10096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010098 typval_T *argvars;
10099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 int exact;
10101{
10102 char_u *keys;
10103 char_u *which;
10104 char_u buf[NUMBUFLEN];
10105 char_u *keys_buf = NULL;
10106 char_u *rhs;
10107 int mode;
10108 garray_T ga;
10109
10110 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->v_type = VAR_STRING;
10112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 if (*keys == NUL)
10116 return;
10117
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010118 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 else
10121 which = (char_u *)"";
10122 mode = get_map_mode(&which, 0);
10123
10124 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10125 rhs = check_map(keys, mode, exact);
10126 vim_free(keys_buf);
10127 if (rhs != NULL)
10128 {
10129 ga_init(&ga);
10130 ga.ga_itemsize = 1;
10131 ga.ga_growsize = 40;
10132
10133 while (*rhs != NUL)
10134 ga_concat(&ga, str2special(&rhs, FALSE));
10135
10136 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139}
10140
10141/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010142 * "map()" function
10143 */
10144 static void
10145f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010146 typval_T *argvars;
10147 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010148{
10149 filter_map(argvars, rettv, TRUE);
10150}
10151
10152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010153 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 */
10155 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010156f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010160 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161}
10162
10163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010164 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 */
10166 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010167f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010168 typval_T *argvars;
10169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010171 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172}
10173
Bram Moolenaar33570922005-01-25 22:26:29 +000010174static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175
10176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010178 typval_T *argvars;
10179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 int type;
10181{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010182 char_u *str = NULL;
10183 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 char_u *pat;
10185 regmatch_T regmatch;
10186 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010187 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 char_u *save_cpo;
10189 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010190 long nth = 1;
10191 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 list_T *l = NULL;
10193 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010195 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
10197 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10198 save_cpo = p_cpo;
10199 p_cpo = (char_u *)"";
10200
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010201 rettv->vval.v_number = -1;
10202 if (type == 3)
10203 {
10204 /* return empty list when there are no matches */
10205 if ((rettv->vval.v_list = list_alloc()) == NULL)
10206 goto theend;
10207 rettv->v_type = VAR_LIST;
10208 ++rettv->vval.v_list->lv_refcount;
10209 }
10210 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212 rettv->v_type = VAR_STRING;
10213 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010216 if (argvars[0].v_type == VAR_LIST)
10217 {
10218 if ((l = argvars[0].vval.v_list) == NULL)
10219 goto theend;
10220 li = l->lv_first;
10221 }
10222 else
10223 expr = str = get_tv_string(&argvars[0]);
10224
10225 pat = get_tv_string_buf(&argvars[1], patbuf);
10226
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010227 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010230 if (l != NULL)
10231 {
10232 li = list_find(l, start);
10233 if (li == NULL)
10234 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010235 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010236 }
10237 else
10238 {
10239 if (start < 0)
10240 start = 0;
10241 if (start > (long)STRLEN(str))
10242 goto theend;
10243 str += start;
10244 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010246 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 }
10249
10250 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10251 if (regmatch.regprog != NULL)
10252 {
10253 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010254
10255 while (1)
10256 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010257 if (l != NULL)
10258 {
10259 if (li == NULL)
10260 {
10261 match = FALSE;
10262 break;
10263 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010264 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 str = echo_string(&li->li_tv, &tofree, strbuf);
10266 }
10267
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010268 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010271 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010272 if (l == NULL && !match)
10273 break;
10274
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010275 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010276 if (l != NULL)
10277 {
10278 li = li->li_next;
10279 ++idx;
10280 }
10281 else
10282 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010283#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010284 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010285#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010286 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010287#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010289 }
10290
10291 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010293 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010295 int i;
10296
10297 /* return list with matched string and submatches */
10298 for (i = 0; i < NSUBEXP; ++i)
10299 {
10300 if (regmatch.endp[i] == NULL)
10301 break;
10302 li = listitem_alloc();
10303 if (li == NULL)
10304 break;
10305 li->li_tv.v_type = VAR_STRING;
10306 li->li_tv.v_lock = 0;
10307 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10308 (int)(regmatch.endp[i] - regmatch.startp[i]));
10309 list_append(rettv->vval.v_list, li);
10310 }
10311 }
10312 else if (type == 2)
10313 {
10314 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010315 if (l != NULL)
10316 copy_tv(&li->li_tv, rettv);
10317 else
10318 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010320 }
10321 else if (l != NULL)
10322 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 else
10324 {
10325 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 (varnumber_T)(regmatch.startp[0] - str);
10328 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 }
10333 }
10334 vim_free(regmatch.regprog);
10335 }
10336
10337theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010338 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 p_cpo = save_cpo;
10340}
10341
10342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343 * "match()" function
10344 */
10345 static void
10346f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010347 typval_T *argvars;
10348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010349{
10350 find_some_match(argvars, rettv, 1);
10351}
10352
10353/*
10354 * "matchend()" function
10355 */
10356 static void
10357f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010360{
10361 find_some_match(argvars, rettv, 0);
10362}
10363
10364/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010365 * "matchlist()" function
10366 */
10367 static void
10368f_matchlist(argvars, rettv)
10369 typval_T *argvars;
10370 typval_T *rettv;
10371{
10372 find_some_match(argvars, rettv, 3);
10373}
10374
10375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010376 * "matchstr()" function
10377 */
10378 static void
10379f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010380 typval_T *argvars;
10381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010382{
10383 find_some_match(argvars, rettv, 2);
10384}
10385
Bram Moolenaar33570922005-01-25 22:26:29 +000010386static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010387
10388 static void
10389max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010390 typval_T *argvars;
10391 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010392 int domax;
10393{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010394 long n = 0;
10395 long i;
10396
10397 if (argvars[0].v_type == VAR_LIST)
10398 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010399 list_T *l;
10400 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010401
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010402 l = argvars[0].vval.v_list;
10403 if (l != NULL)
10404 {
10405 li = l->lv_first;
10406 if (li != NULL)
10407 {
10408 n = get_tv_number(&li->li_tv);
10409 while (1)
10410 {
10411 li = li->li_next;
10412 if (li == NULL)
10413 break;
10414 i = get_tv_number(&li->li_tv);
10415 if (domax ? i > n : i < n)
10416 n = i;
10417 }
10418 }
10419 }
10420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 else if (argvars[0].v_type == VAR_DICT)
10422 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010423 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010424 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010426 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010427
10428 d = argvars[0].vval.v_dict;
10429 if (d != NULL)
10430 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010431 todo = d->dv_hashtab.ht_used;
10432 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010434 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010436 --todo;
10437 i = get_tv_number(&HI2DI(hi)->di_tv);
10438 if (first)
10439 {
10440 n = i;
10441 first = FALSE;
10442 }
10443 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010444 n = i;
10445 }
10446 }
10447 }
10448 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010449 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010450 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010451 rettv->vval.v_number = n;
10452}
10453
10454/*
10455 * "max()" function
10456 */
10457 static void
10458f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 typval_T *argvars;
10460 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010461{
10462 max_min(argvars, rettv, TRUE);
10463}
10464
10465/*
10466 * "min()" function
10467 */
10468 static void
10469f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010470 typval_T *argvars;
10471 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010472{
10473 max_min(argvars, rettv, FALSE);
10474}
10475
Bram Moolenaar0d660222005-01-07 21:51:51 +000010476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 * "mode()" function
10478 */
10479/*ARGSUSED*/
10480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010482 typval_T *argvars;
10483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 char_u buf[2];
10486
10487#ifdef FEAT_VISUAL
10488 if (VIsual_active)
10489 {
10490 if (VIsual_select)
10491 buf[0] = VIsual_mode + 's' - 'v';
10492 else
10493 buf[0] = VIsual_mode;
10494 }
10495 else
10496#endif
10497 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10498 buf[0] = 'r';
10499 else if (State & INSERT)
10500 {
10501 if (State & REPLACE_FLAG)
10502 buf[0] = 'R';
10503 else
10504 buf[0] = 'i';
10505 }
10506 else if (State & CMDLINE)
10507 buf[0] = 'c';
10508 else
10509 buf[0] = 'n';
10510
10511 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_string = vim_strsave(buf);
10513 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514}
10515
10516/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010517 * "nextnonblank()" function
10518 */
10519 static void
10520f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010523{
10524 linenr_T lnum;
10525
10526 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10527 {
10528 if (lnum > curbuf->b_ml.ml_line_count)
10529 {
10530 lnum = 0;
10531 break;
10532 }
10533 if (*skipwhite(ml_get(lnum)) != NUL)
10534 break;
10535 }
10536 rettv->vval.v_number = lnum;
10537}
10538
10539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 * "nr2char()" function
10541 */
10542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010544 typval_T *argvars;
10545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547 char_u buf[NUMBUFLEN];
10548
10549#ifdef FEAT_MBYTE
10550 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 else
10553#endif
10554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 buf[1] = NUL;
10557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->v_type = VAR_STRING;
10559 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010560}
10561
10562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010563 * "prevnonblank()" function
10564 */
10565 static void
10566f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010567 typval_T *argvars;
10568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010569{
10570 linenr_T lnum;
10571
10572 lnum = get_tv_lnum(argvars);
10573 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10574 lnum = 0;
10575 else
10576 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10577 --lnum;
10578 rettv->vval.v_number = lnum;
10579}
10580
Bram Moolenaar8c711452005-01-14 21:53:12 +000010581/*
10582 * "range()" function
10583 */
10584 static void
10585f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010588{
10589 long start;
10590 long end;
10591 long stride = 1;
10592 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 list_T *l;
10594 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010595
10596 start = get_tv_number(&argvars[0]);
10597 if (argvars[1].v_type == VAR_UNKNOWN)
10598 {
10599 end = start - 1;
10600 start = 0;
10601 }
10602 else
10603 {
10604 end = get_tv_number(&argvars[1]);
10605 if (argvars[2].v_type != VAR_UNKNOWN)
10606 stride = get_tv_number(&argvars[2]);
10607 }
10608
10609 rettv->vval.v_number = 0;
10610 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010611 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010612 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010613 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010614 else
10615 {
10616 l = list_alloc();
10617 if (l != NULL)
10618 {
10619 rettv->v_type = VAR_LIST;
10620 rettv->vval.v_list = l;
10621 ++l->lv_refcount;
10622
10623 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10624 {
10625 li = listitem_alloc();
10626 if (li == NULL)
10627 break;
10628 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010629 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010630 li->li_tv.vval.v_number = i;
10631 list_append(l, li);
10632 }
10633 }
10634 }
10635}
10636
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010637/*
10638 * "readfile()" function
10639 */
10640 static void
10641f_readfile(argvars, rettv)
10642 typval_T *argvars;
10643 typval_T *rettv;
10644{
10645 int binary = FALSE;
10646 char_u *fname;
10647 FILE *fd;
10648 list_T *l;
10649 listitem_T *li;
10650#define FREAD_SIZE 200 /* optimized for text lines */
10651 char_u buf[FREAD_SIZE];
10652 int readlen; /* size of last fread() */
10653 int buflen; /* nr of valid chars in buf[] */
10654 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
10655 int tolist; /* first byte in buf[] still to be put in list */
10656 int chop; /* how many CR to chop off */
10657 char_u *prev = NULL; /* previously read bytes, if any */
10658 int prevlen = 0; /* length of "prev" if not NULL */
10659 char_u *s;
10660 int len;
10661
10662 if (argvars[1].v_type != VAR_UNKNOWN
10663 && STRCMP(get_tv_string(&argvars[1]), "b") == 0)
10664 binary = TRUE;
10665
10666 l = list_alloc();
10667 if (l == NULL)
10668 return;
10669 rettv->v_type = VAR_LIST;
10670 rettv->vval.v_list = l;
10671 l->lv_refcount = 1;
10672
10673 /* Always open the file in binary mode, library functions have a mind of
10674 * their own about CR-LF conversion. */
10675 fname = get_tv_string(&argvars[0]);
10676 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
10677 {
10678 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
10679 return;
10680 }
10681
10682 filtd = 0;
10683 for (;;)
10684 {
10685 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
10686 buflen = filtd + readlen;
10687 tolist = 0;
10688 for ( ; filtd < buflen || readlen <= 0; ++filtd)
10689 {
10690 if (buf[filtd] == '\n' || readlen <= 0)
10691 {
10692 /* Only when in binary mode add an empty list item when the
10693 * last line ends in a '\n'. */
10694 if (!binary && readlen == 0 && filtd == 0)
10695 break;
10696
10697 /* Found end-of-line or end-of-file: add a text line to the
10698 * list. */
10699 chop = 0;
10700 if (!binary)
10701 while (filtd - chop - 1 >= tolist
10702 && buf[filtd - chop - 1] == '\r')
10703 ++chop;
10704 len = filtd - tolist - chop;
10705 if (prev == NULL)
10706 s = vim_strnsave(buf + tolist, len);
10707 else
10708 {
10709 s = alloc((unsigned)(prevlen + len + 1));
10710 if (s != NULL)
10711 {
10712 mch_memmove(s, prev, prevlen);
10713 vim_free(prev);
10714 prev = NULL;
10715 mch_memmove(s + prevlen, buf + tolist, len);
10716 s[prevlen + len] = NUL;
10717 }
10718 }
10719 tolist = filtd + 1;
10720
10721 li = listitem_alloc();
10722 if (li == NULL)
10723 {
10724 vim_free(s);
10725 break;
10726 }
10727 li->li_tv.v_type = VAR_STRING;
10728 li->li_tv.v_lock = 0;
10729 li->li_tv.vval.v_string = s;
10730 list_append(l, li);
10731
10732 if (readlen <= 0)
10733 break;
10734 }
10735 else if (buf[filtd] == NUL)
10736 buf[filtd] = '\n';
10737 }
10738 if (readlen <= 0)
10739 break;
10740
10741 if (tolist == 0)
10742 {
10743 /* "buf" is full, need to move text to an allocated buffer */
10744 if (prev == NULL)
10745 {
10746 prev = vim_strnsave(buf, buflen);
10747 prevlen = buflen;
10748 }
10749 else
10750 {
10751 s = alloc((unsigned)(prevlen + buflen));
10752 if (s != NULL)
10753 {
10754 mch_memmove(s, prev, prevlen);
10755 mch_memmove(s + prevlen, buf, buflen);
10756 vim_free(prev);
10757 prev = s;
10758 prevlen += buflen;
10759 }
10760 }
10761 filtd = 0;
10762 }
10763 else
10764 {
10765 mch_memmove(buf, buf + tolist, buflen - tolist);
10766 filtd -= tolist;
10767 }
10768 }
10769
10770 fclose(fd);
10771}
10772
10773
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10775static void make_connection __ARGS((void));
10776static int check_connection __ARGS((void));
10777
10778 static void
10779make_connection()
10780{
10781 if (X_DISPLAY == NULL
10782# ifdef FEAT_GUI
10783 && !gui.in_use
10784# endif
10785 )
10786 {
10787 x_force_connect = TRUE;
10788 setup_term_clip();
10789 x_force_connect = FALSE;
10790 }
10791}
10792
10793 static int
10794check_connection()
10795{
10796 make_connection();
10797 if (X_DISPLAY == NULL)
10798 {
10799 EMSG(_("E240: No connection to Vim server"));
10800 return FAIL;
10801 }
10802 return OK;
10803}
10804#endif
10805
10806#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010807static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808
10809 static void
10810remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813 int expr;
10814{
10815 char_u *server_name;
10816 char_u *keys;
10817 char_u *r = NULL;
10818 char_u buf[NUMBUFLEN];
10819# ifdef WIN32
10820 HWND w;
10821# else
10822 Window w;
10823# endif
10824
10825 if (check_restricted() || check_secure())
10826 return;
10827
10828# ifdef FEAT_X11
10829 if (check_connection() == FAIL)
10830 return;
10831# endif
10832
10833 server_name = get_tv_string(&argvars[0]);
10834 keys = get_tv_string_buf(&argvars[1], buf);
10835# ifdef WIN32
10836 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10837# else
10838 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10839 < 0)
10840# endif
10841 {
10842 if (r != NULL)
10843 EMSG(r); /* sending worked but evaluation failed */
10844 else
10845 EMSG2(_("E241: Unable to send to %s"), server_name);
10846 return;
10847 }
10848
10849 rettv->vval.v_string = r;
10850
10851 if (argvars[2].v_type != VAR_UNKNOWN)
10852 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010853 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010854 char_u str[30];
10855
10856 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 v.di_tv.v_type = VAR_STRING;
10858 v.di_tv.vval.v_string = vim_strsave(str);
10859 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
10860 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010861 }
10862}
10863#endif
10864
10865/*
10866 * "remote_expr()" function
10867 */
10868/*ARGSUSED*/
10869 static void
10870f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 typval_T *argvars;
10872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010873{
10874 rettv->v_type = VAR_STRING;
10875 rettv->vval.v_string = NULL;
10876#ifdef FEAT_CLIENTSERVER
10877 remote_common(argvars, rettv, TRUE);
10878#endif
10879}
10880
10881/*
10882 * "remote_foreground()" function
10883 */
10884/*ARGSUSED*/
10885 static void
10886f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *argvars;
10888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010889{
10890 rettv->vval.v_number = 0;
10891#ifdef FEAT_CLIENTSERVER
10892# ifdef WIN32
10893 /* On Win32 it's done in this application. */
10894 serverForeground(get_tv_string(&argvars[0]));
10895# else
10896 /* Send a foreground() expression to the server. */
10897 argvars[1].v_type = VAR_STRING;
10898 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10899 argvars[2].v_type = VAR_UNKNOWN;
10900 remote_common(argvars, rettv, TRUE);
10901 vim_free(argvars[1].vval.v_string);
10902# endif
10903#endif
10904}
10905
10906/*ARGSUSED*/
10907 static void
10908f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010911{
10912#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010913 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010914 char_u *s = NULL;
10915# ifdef WIN32
10916 int n = 0;
10917# endif
10918
10919 if (check_restricted() || check_secure())
10920 {
10921 rettv->vval.v_number = -1;
10922 return;
10923 }
10924# ifdef WIN32
10925 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10926 if (n == 0)
10927 rettv->vval.v_number = -1;
10928 else
10929 {
10930 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10931 rettv->vval.v_number = (s != NULL);
10932 }
10933# else
10934 rettv->vval.v_number = 0;
10935 if (check_connection() == FAIL)
10936 return;
10937
10938 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10939 serverStrToWin(get_tv_string(&argvars[0])), &s);
10940# endif
10941
10942 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10943 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 v.di_tv.v_type = VAR_STRING;
10945 v.di_tv.vval.v_string = vim_strsave(s);
10946 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
10947 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010948 }
10949#else
10950 rettv->vval.v_number = -1;
10951#endif
10952}
10953
10954/*ARGSUSED*/
10955 static void
10956f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959{
10960 char_u *r = NULL;
10961
10962#ifdef FEAT_CLIENTSERVER
10963 if (!check_restricted() && !check_secure())
10964 {
10965# ifdef WIN32
10966 /* The server's HWND is encoded in the 'id' parameter */
10967 int n = 0;
10968
10969 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10970 if (n != 0)
10971 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10972 if (r == NULL)
10973# else
10974 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10975 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10976# endif
10977 EMSG(_("E277: Unable to read a server reply"));
10978 }
10979#endif
10980 rettv->v_type = VAR_STRING;
10981 rettv->vval.v_string = r;
10982}
10983
10984/*
10985 * "remote_send()" function
10986 */
10987/*ARGSUSED*/
10988 static void
10989f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 typval_T *argvars;
10991 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992{
10993 rettv->v_type = VAR_STRING;
10994 rettv->vval.v_string = NULL;
10995#ifdef FEAT_CLIENTSERVER
10996 remote_common(argvars, rettv, FALSE);
10997#endif
10998}
10999
11000/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011001 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011002 */
11003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011007{
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 list_T *l;
11009 listitem_T *item, *item2;
11010 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011011 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011012 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011013 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 dict_T *d;
11015 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011017 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011018 if (argvars[0].v_type == VAR_DICT)
11019 {
11020 if (argvars[2].v_type != VAR_UNKNOWN)
11021 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011022 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011023 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011024 {
11025 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011026 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011027 if (di == NULL)
11028 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011029 else
11030 {
11031 *rettv = di->di_tv;
11032 init_tv(&di->di_tv);
11033 dictitem_remove(d, di);
11034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011035 }
11036 }
11037 else if (argvars[0].v_type != VAR_LIST)
11038 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011039 else if ((l = argvars[0].vval.v_list) != NULL
11040 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011043 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044 if (item == NULL)
11045 EMSGN(_(e_listidx), idx);
11046 else
11047 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011048 if (argvars[2].v_type == VAR_UNKNOWN)
11049 {
11050 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011051 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011052 *rettv = item->li_tv;
11053 vim_free(item);
11054 }
11055 else
11056 {
11057 /* Remove range of items, return list with values. */
11058 end = get_tv_number(&argvars[2]);
11059 item2 = list_find(l, end);
11060 if (item2 == NULL)
11061 EMSGN(_(e_listidx), end);
11062 else
11063 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011064 int cnt = 0;
11065
11066 for (li = item; li != NULL; li = li->li_next)
11067 {
11068 ++cnt;
11069 if (li == item2)
11070 break;
11071 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011072 if (li == NULL) /* didn't find "item2" after "item" */
11073 EMSG(_(e_invrange));
11074 else
11075 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011076 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011077 l = list_alloc();
11078 if (l != NULL)
11079 {
11080 rettv->v_type = VAR_LIST;
11081 rettv->vval.v_list = l;
11082 l->lv_first = item;
11083 l->lv_last = item2;
11084 l->lv_refcount = 1;
11085 item->li_prev = NULL;
11086 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011087 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011088 }
11089 }
11090 }
11091 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011092 }
11093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094}
11095
11096/*
11097 * "rename({from}, {to})" function
11098 */
11099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103{
11104 char_u buf[NUMBUFLEN];
11105
11106 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11110 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111}
11112
11113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011114 * "repeat()" function
11115 */
11116/*ARGSUSED*/
11117 static void
11118f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 typval_T *argvars;
11120 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011121{
11122 char_u *p;
11123 int n;
11124 int slen;
11125 int len;
11126 char_u *r;
11127 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011129
11130 n = get_tv_number(&argvars[1]);
11131 if (argvars[0].v_type == VAR_LIST)
11132 {
11133 l = list_alloc();
11134 if (l != NULL && argvars[0].vval.v_list != NULL)
11135 {
11136 l->lv_refcount = 1;
11137 while (n-- > 0)
11138 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11139 break;
11140 }
11141 rettv->v_type = VAR_LIST;
11142 rettv->vval.v_list = l;
11143 }
11144 else
11145 {
11146 p = get_tv_string(&argvars[0]);
11147 rettv->v_type = VAR_STRING;
11148 rettv->vval.v_string = NULL;
11149
11150 slen = (int)STRLEN(p);
11151 len = slen * n;
11152 if (len <= 0)
11153 return;
11154
11155 r = alloc(len + 1);
11156 if (r != NULL)
11157 {
11158 for (i = 0; i < n; i++)
11159 mch_memmove(r + i * slen, p, (size_t)slen);
11160 r[len] = NUL;
11161 }
11162
11163 rettv->vval.v_string = r;
11164 }
11165}
11166
11167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 * "resolve()" function
11169 */
11170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011172 typval_T *argvars;
11173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174{
11175 char_u *p;
11176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178#ifdef FEAT_SHORTCUT
11179 {
11180 char_u *v = NULL;
11181
11182 v = mch_resolve_shortcut(p);
11183 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 }
11188#else
11189# ifdef HAVE_READLINK
11190 {
11191 char_u buf[MAXPATHL + 1];
11192 char_u *cpy;
11193 int len;
11194 char_u *remain = NULL;
11195 char_u *q;
11196 int is_relative_to_current = FALSE;
11197 int has_trailing_pathsep = FALSE;
11198 int limit = 100;
11199
11200 p = vim_strsave(p);
11201
11202 if (p[0] == '.' && (vim_ispathsep(p[1])
11203 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11204 is_relative_to_current = TRUE;
11205
11206 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011207 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 has_trailing_pathsep = TRUE;
11209
11210 q = getnextcomp(p);
11211 if (*q != NUL)
11212 {
11213 /* Separate the first path component in "p", and keep the
11214 * remainder (beginning with the path separator). */
11215 remain = vim_strsave(q - 1);
11216 q[-1] = NUL;
11217 }
11218
11219 for (;;)
11220 {
11221 for (;;)
11222 {
11223 len = readlink((char *)p, (char *)buf, MAXPATHL);
11224 if (len <= 0)
11225 break;
11226 buf[len] = NUL;
11227
11228 if (limit-- == 0)
11229 {
11230 vim_free(p);
11231 vim_free(remain);
11232 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 goto fail;
11235 }
11236
11237 /* Ensure that the result will have a trailing path separator
11238 * if the argument has one. */
11239 if (remain == NULL && has_trailing_pathsep)
11240 add_pathsep(buf);
11241
11242 /* Separate the first path component in the link value and
11243 * concatenate the remainders. */
11244 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11245 if (*q != NUL)
11246 {
11247 if (remain == NULL)
11248 remain = vim_strsave(q - 1);
11249 else
11250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011251 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 if (cpy != NULL)
11253 {
11254 STRCAT(cpy, remain);
11255 vim_free(remain);
11256 remain = cpy;
11257 }
11258 }
11259 q[-1] = NUL;
11260 }
11261
11262 q = gettail(p);
11263 if (q > p && *q == NUL)
11264 {
11265 /* Ignore trailing path separator. */
11266 q[-1] = NUL;
11267 q = gettail(p);
11268 }
11269 if (q > p && !mch_isFullName(buf))
11270 {
11271 /* symlink is relative to directory of argument */
11272 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11273 if (cpy != NULL)
11274 {
11275 STRCPY(cpy, p);
11276 STRCPY(gettail(cpy), buf);
11277 vim_free(p);
11278 p = cpy;
11279 }
11280 }
11281 else
11282 {
11283 vim_free(p);
11284 p = vim_strsave(buf);
11285 }
11286 }
11287
11288 if (remain == NULL)
11289 break;
11290
11291 /* Append the first path component of "remain" to "p". */
11292 q = getnextcomp(remain + 1);
11293 len = q - remain - (*q != NUL);
11294 cpy = vim_strnsave(p, STRLEN(p) + len);
11295 if (cpy != NULL)
11296 {
11297 STRNCAT(cpy, remain, len);
11298 vim_free(p);
11299 p = cpy;
11300 }
11301 /* Shorten "remain". */
11302 if (*q != NUL)
11303 STRCPY(remain, q - 1);
11304 else
11305 {
11306 vim_free(remain);
11307 remain = NULL;
11308 }
11309 }
11310
11311 /* If the result is a relative path name, make it explicitly relative to
11312 * the current directory if and only if the argument had this form. */
11313 if (!vim_ispathsep(*p))
11314 {
11315 if (is_relative_to_current
11316 && *p != NUL
11317 && !(p[0] == '.'
11318 && (p[1] == NUL
11319 || vim_ispathsep(p[1])
11320 || (p[1] == '.'
11321 && (p[2] == NUL
11322 || vim_ispathsep(p[2]))))))
11323 {
11324 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011325 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 if (cpy != NULL)
11327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 vim_free(p);
11329 p = cpy;
11330 }
11331 }
11332 else if (!is_relative_to_current)
11333 {
11334 /* Strip leading "./". */
11335 q = p;
11336 while (q[0] == '.' && vim_ispathsep(q[1]))
11337 q += 2;
11338 if (q > p)
11339 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11340 }
11341 }
11342
11343 /* Ensure that the result will have no trailing path separator
11344 * if the argument had none. But keep "/" or "//". */
11345 if (!has_trailing_pathsep)
11346 {
11347 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011348 if (after_pathsep(p, q))
11349 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 }
11351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 }
11354# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356# endif
11357#endif
11358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
11361#ifdef HAVE_READLINK
11362fail:
11363#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365}
11366
11367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011368 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 */
11370 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011371f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011372 typval_T *argvars;
11373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaar33570922005-01-25 22:26:29 +000011375 list_T *l;
11376 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377
Bram Moolenaar0d660222005-01-07 21:51:51 +000011378 rettv->vval.v_number = 0;
11379 if (argvars[0].v_type != VAR_LIST)
11380 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011381 else if ((l = argvars[0].vval.v_list) != NULL
11382 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383 {
11384 li = l->lv_last;
11385 l->lv_first = l->lv_last = li;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011386 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011387 while (li != NULL)
11388 {
11389 ni = li->li_prev;
11390 list_append(l, li);
11391 li = ni;
11392 }
11393 rettv->vval.v_list = l;
11394 rettv->v_type = VAR_LIST;
11395 ++l->lv_refcount;
11396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397}
11398
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399#define SP_NOMOVE 1 /* don't move cursor */
11400#define SP_REPEAT 2 /* repeat to find outer pair */
11401#define SP_RETCOUNT 4 /* return matchcount */
11402
Bram Moolenaar33570922005-01-25 22:26:29 +000011403static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011404
11405/*
11406 * Get flags for a search function.
11407 * Possibly sets "p_ws".
11408 * Returns BACKWARD, FORWARD or zero (for an error).
11409 */
11410 static int
11411get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011412 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011413 int *flagsp;
11414{
11415 int dir = FORWARD;
11416 char_u *flags;
11417 char_u nbuf[NUMBUFLEN];
11418 int mask;
11419
11420 if (varp->v_type != VAR_UNKNOWN)
11421 {
11422 flags = get_tv_string_buf(varp, nbuf);
11423 while (*flags != NUL)
11424 {
11425 switch (*flags)
11426 {
11427 case 'b': dir = BACKWARD; break;
11428 case 'w': p_ws = TRUE; break;
11429 case 'W': p_ws = FALSE; break;
11430 default: mask = 0;
11431 if (flagsp != NULL)
11432 switch (*flags)
11433 {
11434 case 'n': mask = SP_NOMOVE; break;
11435 case 'r': mask = SP_REPEAT; break;
11436 case 'm': mask = SP_RETCOUNT; break;
11437 }
11438 if (mask == 0)
11439 {
11440 EMSG2(_(e_invarg2), flags);
11441 dir = 0;
11442 }
11443 else
11444 *flagsp |= mask;
11445 }
11446 if (dir == 0)
11447 break;
11448 ++flags;
11449 }
11450 }
11451 return dir;
11452}
11453
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454/*
11455 * "search()" function
11456 */
11457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 typval_T *argvars;
11460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
11462 char_u *pat;
11463 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011464 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 int save_p_ws = p_ws;
11466 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011467 int flags = 0;
11468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011472 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11473 if (dir == 0)
11474 goto theend;
11475 if ((flags & ~SP_NOMOVE) != 0)
11476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011478 goto theend;
11479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11483 SEARCH_KEEP, RE_SEARCH) != FAIL)
11484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 curwin->w_cursor = pos;
11487 /* "/$" will put the cursor after the end of the line, may need to
11488 * correct that here */
11489 check_cursor();
11490 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491
11492 /* If 'n' flag is used: restore cursor position. */
11493 if (flags & SP_NOMOVE)
11494 curwin->w_cursor = save_cursor;
11495theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 p_ws = save_p_ws;
11497}
11498
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499/*
11500 * "searchpair()" function
11501 */
11502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507 char_u *spat, *mpat, *epat;
11508 char_u *skip;
11509 char_u *pat, *pat2, *pat3;
11510 pos_T pos;
11511 pos_T firstpos;
11512 pos_T save_cursor;
11513 pos_T save_pos;
11514 int save_p_ws = p_ws;
11515 char_u *save_cpo;
11516 int dir;
11517 int flags = 0;
11518 char_u nbuf1[NUMBUFLEN];
11519 char_u nbuf2[NUMBUFLEN];
11520 char_u nbuf3[NUMBUFLEN];
11521 int n;
11522 int r;
11523 int nest = 1;
11524 int err;
11525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527
11528 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11529 save_cpo = p_cpo;
11530 p_cpo = (char_u *)"";
11531
11532 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 spat = get_tv_string(&argvars[0]);
11534 mpat = get_tv_string_buf(&argvars[1], nbuf1);
11535 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536
11537 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11538 * start/middle/end (pat3, for the top pair). */
11539 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
11540 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
11541 if (pat2 == NULL || pat3 == NULL)
11542 goto theend;
11543 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
11544 if (*mpat == NUL)
11545 STRCPY(pat3, pat2);
11546 else
11547 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11548 spat, epat, mpat);
11549
11550 /* Handle the optional fourth argument: flags */
11551 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011552 if (dir == 0)
11553 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554
11555 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011556 if (argvars[3].v_type == VAR_UNKNOWN
11557 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 skip = (char_u *)"";
11559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561
11562 save_cursor = curwin->w_cursor;
11563 pos = curwin->w_cursor;
11564 firstpos.lnum = 0;
11565 pat = pat3;
11566 for (;;)
11567 {
11568 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11569 SEARCH_KEEP, RE_SEARCH);
11570 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11571 /* didn't find it or found the first match again: FAIL */
11572 break;
11573
11574 if (firstpos.lnum == 0)
11575 firstpos = pos;
11576
11577 /* If the skip pattern matches, ignore this match. */
11578 if (*skip != NUL)
11579 {
11580 save_pos = curwin->w_cursor;
11581 curwin->w_cursor = pos;
11582 r = eval_to_bool(skip, &err, NULL, FALSE);
11583 curwin->w_cursor = save_pos;
11584 if (err)
11585 {
11586 /* Evaluating {skip} caused an error, break here. */
11587 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 break;
11590 }
11591 if (r)
11592 continue;
11593 }
11594
11595 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11596 {
11597 /* Found end when searching backwards or start when searching
11598 * forward: nested pair. */
11599 ++nest;
11600 pat = pat2; /* nested, don't search for middle */
11601 }
11602 else
11603 {
11604 /* Found end when searching forward or start when searching
11605 * backward: end of (nested) pair; or found middle in outer pair. */
11606 if (--nest == 1)
11607 pat = pat3; /* outer level, search for middle */
11608 }
11609
11610 if (nest == 0)
11611 {
11612 /* Found the match: return matchcount or line number. */
11613 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 curwin->w_cursor = pos;
11618 if (!(flags & SP_REPEAT))
11619 break;
11620 nest = 1; /* search for next unmatched */
11621 }
11622 }
11623
11624 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 curwin->w_cursor = save_cursor;
11627
11628theend:
11629 vim_free(pat2);
11630 vim_free(pat3);
11631 p_ws = save_p_ws;
11632 p_cpo = save_cpo;
11633}
11634
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635/*ARGSUSED*/
11636 static void
11637f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011641#ifdef FEAT_CLIENTSERVER
11642 char_u buf[NUMBUFLEN];
11643 char_u *server = get_tv_string(&argvars[0]);
11644 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645
Bram Moolenaar0d660222005-01-07 21:51:51 +000011646 rettv->vval.v_number = -1;
11647 if (check_restricted() || check_secure())
11648 return;
11649# ifdef FEAT_X11
11650 if (check_connection() == FAIL)
11651 return;
11652# endif
11653
11654 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 EMSG(_("E258: Unable to send to client"));
11657 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011659 rettv->vval.v_number = 0;
11660#else
11661 rettv->vval.v_number = -1;
11662#endif
11663}
11664
11665/*ARGSUSED*/
11666 static void
11667f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *argvars;
11669 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670{
11671 char_u *r = NULL;
11672
11673#ifdef FEAT_CLIENTSERVER
11674# ifdef WIN32
11675 r = serverGetVimNames();
11676# else
11677 make_connection();
11678 if (X_DISPLAY != NULL)
11679 r = serverGetVimNames(X_DISPLAY);
11680# endif
11681#endif
11682 rettv->v_type = VAR_STRING;
11683 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684}
11685
11686/*
11687 * "setbufvar()" function
11688 */
11689/*ARGSUSED*/
11690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694{
11695 buf_T *buf;
11696#ifdef FEAT_AUTOCMD
11697 aco_save_T aco;
11698#else
11699 buf_T *save_curbuf;
11700#endif
11701 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 char_u nbuf[NUMBUFLEN];
11704
11705 if (check_restricted() || check_secure())
11706 return;
11707 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 buf = get_buf_tv(&argvars[0]);
11709 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 varp = &argvars[2];
11711
11712 if (buf != NULL && varname != NULL && varp != NULL)
11713 {
11714 /* set curbuf to be our buf, temporarily */
11715#ifdef FEAT_AUTOCMD
11716 aucmd_prepbuf(&aco, buf);
11717#else
11718 save_curbuf = curbuf;
11719 curbuf = buf;
11720#endif
11721
11722 if (*varname == '&')
11723 {
11724 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 set_option_value(varname, get_tv_number(varp),
11726 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 }
11728 else
11729 {
11730 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11731 if (bufvarname != NULL)
11732 {
11733 STRCPY(bufvarname, "b:");
11734 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011735 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 vim_free(bufvarname);
11737 }
11738 }
11739
11740 /* reset notion of buffer */
11741#ifdef FEAT_AUTOCMD
11742 aucmd_restbuf(&aco);
11743#else
11744 curbuf = save_curbuf;
11745#endif
11746 }
11747 --emsg_off;
11748}
11749
11750/*
11751 * "setcmdpos()" function
11752 */
11753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->vval.v_number = set_cmdline_pos(
11759 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760}
11761
11762/*
11763 * "setline()" function
11764 */
11765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011767 typval_T *argvars;
11768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769{
11770 linenr_T lnum;
11771 char_u *line;
11772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 lnum = get_tv_lnum(argvars);
11774 line = get_tv_string(&argvars[1]);
11775 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776
11777 if (lnum >= 1
11778 && lnum <= curbuf->b_ml.ml_line_count
11779 && u_savesub(lnum) == OK
11780 && ml_replace(lnum, line, TRUE) == OK)
11781 {
11782 changed_bytes(lnum, 0);
11783 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 }
11786}
11787
11788/*
11789 * "setreg()" function
11790 */
11791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
11796 int regname;
11797 char_u *strregname;
11798 char_u *stropt;
11799 int append;
11800 char_u yank_type;
11801 long block_len;
11802
11803 block_len = -1;
11804 yank_type = MAUTO;
11805 append = FALSE;
11806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 strregname = get_tv_string(argvars);
11808 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809
11810 regname = (strregname == NULL ? '"' : *strregname);
11811 if (regname == 0 || regname == '@')
11812 regname = '"';
11813 else if (regname == '=')
11814 return;
11815
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011816 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 switch (*stropt)
11820 {
11821 case 'a': case 'A': /* append */
11822 append = TRUE;
11823 break;
11824 case 'v': case 'c': /* character-wise selection */
11825 yank_type = MCHAR;
11826 break;
11827 case 'V': case 'l': /* line-wise selection */
11828 yank_type = MLINE;
11829 break;
11830#ifdef FEAT_VISUAL
11831 case 'b': case Ctrl_V: /* block-wise selection */
11832 yank_type = MBLOCK;
11833 if (VIM_ISDIGIT(stropt[1]))
11834 {
11835 ++stropt;
11836 block_len = getdigits(&stropt) - 1;
11837 --stropt;
11838 }
11839 break;
11840#endif
11841 }
11842 }
11843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847}
11848
11849
11850/*
11851 * "setwinvar(expr)" function
11852 */
11853/*ARGSUSED*/
11854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *argvars;
11857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
11859 win_T *win;
11860#ifdef FEAT_WINDOWS
11861 win_T *save_curwin;
11862#endif
11863 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 char_u nbuf[NUMBUFLEN];
11866
11867 if (check_restricted() || check_secure())
11868 return;
11869 ++emsg_off;
11870 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 varp = &argvars[2];
11873
11874 if (win != NULL && varname != NULL && varp != NULL)
11875 {
11876#ifdef FEAT_WINDOWS
11877 /* set curwin to be our win, temporarily */
11878 save_curwin = curwin;
11879 curwin = win;
11880 curbuf = curwin->w_buffer;
11881#endif
11882
11883 if (*varname == '&')
11884 {
11885 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886 set_option_value(varname, get_tv_number(varp),
11887 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 }
11889 else
11890 {
11891 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11892 if (winvarname != NULL)
11893 {
11894 STRCPY(winvarname, "w:");
11895 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011896 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 vim_free(winvarname);
11898 }
11899 }
11900
11901#ifdef FEAT_WINDOWS
11902 /* Restore current window, if it's still valid (autocomands can make
11903 * it invalid). */
11904 if (win_valid(save_curwin))
11905 {
11906 curwin = save_curwin;
11907 curbuf = curwin->w_buffer;
11908 }
11909#endif
11910 }
11911 --emsg_off;
11912}
11913
11914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011915 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 */
11917 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011918f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011922 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924 p = get_tv_string(&argvars[0]);
11925 rettv->vval.v_string = vim_strsave(p);
11926 simplify_filename(rettv->vval.v_string); /* simplify in place */
11927 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930static int
11931#ifdef __BORLANDC__
11932 _RTLENTRYF
11933#endif
11934 item_compare __ARGS((const void *s1, const void *s2));
11935static int
11936#ifdef __BORLANDC__
11937 _RTLENTRYF
11938#endif
11939 item_compare2 __ARGS((const void *s1, const void *s2));
11940
11941static int item_compare_ic;
11942static char_u *item_compare_func;
11943#define ITEM_COMPARE_FAIL 999
11944
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011946 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948 static int
11949#ifdef __BORLANDC__
11950_RTLENTRYF
11951#endif
11952item_compare(s1, s2)
11953 const void *s1;
11954 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956 char_u *p1, *p2;
11957 char_u *tofree1, *tofree2;
11958 int res;
11959 char_u numbuf1[NUMBUFLEN];
11960 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
Bram Moolenaar33570922005-01-25 22:26:29 +000011962 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
11963 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011964 if (item_compare_ic)
11965 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967 res = STRCMP(p1, p2);
11968 vim_free(tofree1);
11969 vim_free(tofree2);
11970 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971}
11972
11973 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011974#ifdef __BORLANDC__
11975_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011977item_compare2(s1, s2)
11978 const void *s1;
11979 const void *s2;
11980{
11981 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 typval_T rettv;
11983 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011986 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
11987 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
11989 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011990
11991 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11992 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994 clear_tv(&argv[0]);
11995 clear_tv(&argv[1]);
11996
11997 if (res == FAIL)
11998 res = ITEM_COMPARE_FAIL;
11999 else
12000 res = get_tv_number(&rettv);
12001 clear_tv(&rettv);
12002 return res;
12003}
12004
12005/*
12006 * "sort({list})" function
12007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012009f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 typval_T *argvars;
12011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 list_T *l;
12014 listitem_T *li;
12015 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016 long len;
12017 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019 rettv->vval.v_number = 0;
12020 if (argvars[0].v_type != VAR_LIST)
12021 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 else
12023 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012025 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026 return;
12027 rettv->vval.v_list = l;
12028 rettv->v_type = VAR_LIST;
12029 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031 len = list_len(l);
12032 if (len <= 1)
12033 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035 item_compare_ic = FALSE;
12036 item_compare_func = NULL;
12037 if (argvars[1].v_type != VAR_UNKNOWN)
12038 {
12039 if (argvars[1].v_type == VAR_FUNC)
12040 item_compare_func = argvars[0].vval.v_string;
12041 else
12042 {
12043 i = get_tv_number(&argvars[1]);
12044 if (i == 1)
12045 item_compare_ic = TRUE;
12046 else
12047 item_compare_func = get_tv_string(&argvars[1]);
12048 }
12049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050
Bram Moolenaar0d660222005-01-07 21:51:51 +000012051 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012053 if (ptrs == NULL)
12054 return;
12055 i = 0;
12056 for (li = l->lv_first; li != NULL; li = li->li_next)
12057 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059 /* test the compare function */
12060 if (item_compare_func != NULL
12061 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12062 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012063 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065 {
12066 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012067 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068 item_compare_func == NULL ? item_compare : item_compare2);
12069
12070 /* Clear the List and append the items in the sorted order. */
12071 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012072 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073 for (i = 0; i < len; ++i)
12074 list_append(l, ptrs[i]);
12075 }
12076
12077 vim_free(ptrs);
12078 }
12079}
12080
12081 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012082f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012083 typval_T *argvars;
12084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085{
12086 char_u *str;
12087 char_u *end;
12088 char_u *pat;
12089 regmatch_T regmatch;
12090 char_u patbuf[NUMBUFLEN];
12091 char_u *save_cpo;
12092 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 listitem_T *ni;
12094 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012095 colnr_T col = 0;
12096
12097 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12098 save_cpo = p_cpo;
12099 p_cpo = (char_u *)"";
12100
12101 str = get_tv_string(&argvars[0]);
12102 if (argvars[1].v_type == VAR_UNKNOWN)
12103 pat = (char_u *)"[\\x01- ]\\+";
12104 else
12105 pat = get_tv_string_buf(&argvars[1], patbuf);
12106
12107 l = list_alloc();
12108 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012110 rettv->v_type = VAR_LIST;
12111 rettv->vval.v_list = l;
12112 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113
Bram Moolenaar0d660222005-01-07 21:51:51 +000012114 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12115 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012117 regmatch.rm_ic = FALSE;
12118 while (*str != NUL)
12119 {
12120 match = vim_regexec_nl(&regmatch, str, col);
12121 if (match)
12122 end = regmatch.startp[0];
12123 else
12124 end = str + STRLEN(str);
12125 if (end > str)
12126 {
12127 ni = listitem_alloc();
12128 if (ni == NULL)
12129 break;
12130 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012131 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12133 list_append(l, ni);
12134 }
12135 if (!match)
12136 break;
12137 /* Advance to just after the match. */
12138 if (regmatch.endp[0] > str)
12139 col = 0;
12140 else
12141 {
12142 /* Don't get stuck at the same match. */
12143#ifdef FEAT_MBYTE
12144 col = mb_ptr2len_check(regmatch.endp[0]);
12145#else
12146 col = 1;
12147#endif
12148 }
12149 str = regmatch.endp[0];
12150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156}
12157
12158#ifdef HAVE_STRFTIME
12159/*
12160 * "strftime({format}[, {time}])" function
12161 */
12162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012164 typval_T *argvars;
12165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166{
12167 char_u result_buf[256];
12168 struct tm *curtime;
12169 time_t seconds;
12170 char_u *p;
12171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012175 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 seconds = time(NULL);
12177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 curtime = localtime(&seconds);
12180 /* MSVC returns NULL for an invalid value of seconds. */
12181 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012182 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 else
12184 {
12185# ifdef FEAT_MBYTE
12186 vimconv_T conv;
12187 char_u *enc;
12188
12189 conv.vc_type = CONV_NONE;
12190 enc = enc_locale();
12191 convert_setup(&conv, p_enc, enc);
12192 if (conv.vc_type != CONV_NONE)
12193 p = string_convert(&conv, p, NULL);
12194# endif
12195 if (p != NULL)
12196 (void)strftime((char *)result_buf, sizeof(result_buf),
12197 (char *)p, curtime);
12198 else
12199 result_buf[0] = NUL;
12200
12201# ifdef FEAT_MBYTE
12202 if (conv.vc_type != CONV_NONE)
12203 vim_free(p);
12204 convert_setup(&conv, enc, p_enc);
12205 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 else
12208# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210
12211# ifdef FEAT_MBYTE
12212 /* Release conversion descriptors */
12213 convert_setup(&conv, NULL, NULL);
12214 vim_free(enc);
12215# endif
12216 }
12217}
12218#endif
12219
12220/*
12221 * "stridx()" function
12222 */
12223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012224f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012225 typval_T *argvars;
12226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227{
12228 char_u buf[NUMBUFLEN];
12229 char_u *needle;
12230 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012231 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012236 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238
Bram Moolenaar33570922005-01-25 22:26:29 +000012239 if (argvars[2].v_type != VAR_UNKNOWN)
12240 {
12241 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012242 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012244 if (start_idx >= 0)
12245 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 }
12247
12248 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12249 if (pos != NULL)
12250 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251}
12252
12253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012254 * "string()" function
12255 */
12256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012258 typval_T *argvars;
12259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012260{
12261 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012262 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012265 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012266 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268}
12269
12270/*
12271 * "strlen()" function
12272 */
12273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012275 typval_T *argvars;
12276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_number = (varnumber_T)(STRLEN(
12279 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280}
12281
12282/*
12283 * "strpart()" function
12284 */
12285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012287 typval_T *argvars;
12288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289{
12290 char_u *p;
12291 int n;
12292 int len;
12293 int slen;
12294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 slen = (int)STRLEN(p);
12297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 else
12302 len = slen - n; /* default len: all bytes that are available. */
12303
12304 /*
12305 * Only return the overlap between the specified part and the actual
12306 * string.
12307 */
12308 if (n < 0)
12309 {
12310 len += n;
12311 n = 0;
12312 }
12313 else if (n > slen)
12314 n = slen;
12315 if (len < 0)
12316 len = 0;
12317 else if (n + len > slen)
12318 len = slen - n;
12319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 rettv->v_type = VAR_STRING;
12321 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322}
12323
12324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325 * "strridx()" function
12326 */
12327 static void
12328f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012329 typval_T *argvars;
12330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331{
12332 char_u buf[NUMBUFLEN];
12333 char_u *needle;
12334 char_u *haystack;
12335 char_u *rest;
12336 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012337 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012338
12339 needle = get_tv_string(&argvars[1]);
12340 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012341 haystack_len = STRLEN(haystack);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342 if (*needle == NUL)
12343 /* Empty string matches past the end. */
Bram Moolenaar532c7802005-01-27 14:44:31 +000012344 lastmatch = haystack + haystack_len;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012346 {
12347 if (argvars[2].v_type != VAR_UNKNOWN)
12348 {
12349 /* Third argument: upper limit for index */
12350 end_idx = get_tv_number(&argvars[2]);
12351 if (end_idx < 0)
12352 {
12353 /* can never find a match */
12354 rettv->vval.v_number = -1;
12355 return;
12356 }
12357 }
12358 else
12359 end_idx = haystack_len;
12360
Bram Moolenaar0d660222005-01-07 21:51:51 +000012361 for (rest = haystack; *rest != '\0'; ++rest)
12362 {
12363 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012364 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365 break;
12366 lastmatch = rest;
12367 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012368 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012369
12370 if (lastmatch == NULL)
12371 rettv->vval.v_number = -1;
12372 else
12373 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12374}
12375
12376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 * "strtrans()" function
12378 */
12379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->v_type = VAR_STRING;
12385 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386}
12387
12388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389 * "submatch()" function
12390 */
12391 static void
12392f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *argvars;
12394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012395{
12396 rettv->v_type = VAR_STRING;
12397 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12398}
12399
12400/*
12401 * "substitute()" function
12402 */
12403 static void
12404f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012405 typval_T *argvars;
12406 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407{
12408 char_u patbuf[NUMBUFLEN];
12409 char_u subbuf[NUMBUFLEN];
12410 char_u flagsbuf[NUMBUFLEN];
12411
12412 rettv->v_type = VAR_STRING;
12413 rettv->vval.v_string = do_string_sub(
12414 get_tv_string(&argvars[0]),
12415 get_tv_string_buf(&argvars[1], patbuf),
12416 get_tv_string_buf(&argvars[2], subbuf),
12417 get_tv_string_buf(&argvars[3], flagsbuf));
12418}
12419
12420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 * "synID(line, col, trans)" function
12422 */
12423/*ARGSUSED*/
12424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012425f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012426 typval_T *argvars;
12427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428{
12429 int id = 0;
12430#ifdef FEAT_SYN_HL
12431 long line;
12432 long col;
12433 int trans;
12434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 line = get_tv_lnum(argvars);
12436 col = get_tv_number(&argvars[1]) - 1;
12437 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438
12439 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12440 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12441 id = syn_get_id(line, col, trans);
12442#endif
12443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445}
12446
12447/*
12448 * "synIDattr(id, what [, mode])" function
12449 */
12450/*ARGSUSED*/
12451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012453 typval_T *argvars;
12454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455{
12456 char_u *p = NULL;
12457#ifdef FEAT_SYN_HL
12458 int id;
12459 char_u *what;
12460 char_u *mode;
12461 char_u modebuf[NUMBUFLEN];
12462 int modec;
12463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 id = get_tv_number(&argvars[0]);
12465 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 modec = TOLOWER_ASC(mode[0]);
12470 if (modec != 't' && modec != 'c'
12471#ifdef FEAT_GUI
12472 && modec != 'g'
12473#endif
12474 )
12475 modec = 0; /* replace invalid with current */
12476 }
12477 else
12478 {
12479#ifdef FEAT_GUI
12480 if (gui.in_use)
12481 modec = 'g';
12482 else
12483#endif
12484 if (t_colors > 1)
12485 modec = 'c';
12486 else
12487 modec = 't';
12488 }
12489
12490
12491 switch (TOLOWER_ASC(what[0]))
12492 {
12493 case 'b':
12494 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12495 p = highlight_color(id, what, modec);
12496 else /* bold */
12497 p = highlight_has_attr(id, HL_BOLD, modec);
12498 break;
12499
12500 case 'f': /* fg[#] */
12501 p = highlight_color(id, what, modec);
12502 break;
12503
12504 case 'i':
12505 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12506 p = highlight_has_attr(id, HL_INVERSE, modec);
12507 else /* italic */
12508 p = highlight_has_attr(id, HL_ITALIC, modec);
12509 break;
12510
12511 case 'n': /* name */
12512 p = get_highlight_name(NULL, id - 1);
12513 break;
12514
12515 case 'r': /* reverse */
12516 p = highlight_has_attr(id, HL_INVERSE, modec);
12517 break;
12518
12519 case 's': /* standout */
12520 p = highlight_has_attr(id, HL_STANDOUT, modec);
12521 break;
12522
12523 case 'u': /* underline */
12524 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12525 break;
12526 }
12527
12528 if (p != NULL)
12529 p = vim_strsave(p);
12530#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->v_type = VAR_STRING;
12532 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533}
12534
12535/*
12536 * "synIDtrans(id)" function
12537 */
12538/*ARGSUSED*/
12539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012541 typval_T *argvars;
12542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543{
12544 int id;
12545
12546#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
12549 if (id > 0)
12550 id = syn_get_final_id(id);
12551 else
12552#endif
12553 id = 0;
12554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556}
12557
12558/*
12559 * "system()" function
12560 */
12561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012563 typval_T *argvars;
12564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012566 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012568 char_u *infile = NULL;
12569 char_u buf[NUMBUFLEN];
12570 int err = FALSE;
12571 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012573 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012574 {
12575 /*
12576 * Write the string to a temp file, to be used for input of the shell
12577 * command.
12578 */
12579 if ((infile = vim_tempname('i')) == NULL)
12580 {
12581 EMSG(_(e_notmp));
12582 return;
12583 }
12584
12585 fd = mch_fopen((char *)infile, WRITEBIN);
12586 if (fd == NULL)
12587 {
12588 EMSG2(_(e_notopen), infile);
12589 goto done;
12590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012592 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12593 err = TRUE;
12594 if (fclose(fd) != 0)
12595 err = TRUE;
12596 if (err)
12597 {
12598 EMSG(_("E677: Error writing temp file"));
12599 goto done;
12600 }
12601 }
12602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012604
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605#ifdef USE_CR
12606 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012607 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 {
12609 char_u *s;
12610
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012611 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 {
12613 if (*s == CAR)
12614 *s = NL;
12615 }
12616 }
12617#else
12618# ifdef USE_CRNL
12619 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012620 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 {
12622 char_u *s, *d;
12623
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012624 d = res;
12625 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 {
12627 if (s[0] == CAR && s[1] == NL)
12628 ++s;
12629 *d++ = *s;
12630 }
12631 *d = NUL;
12632 }
12633# endif
12634#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012635
12636done:
12637 if (infile != NULL)
12638 {
12639 mch_remove(infile);
12640 vim_free(infile);
12641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642 rettv->v_type = VAR_STRING;
12643 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644}
12645
12646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 * "tempname()" function
12648 */
12649/*ARGSUSED*/
12650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 typval_T *argvars;
12653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654{
12655 static int x = 'A';
12656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->v_type = VAR_STRING;
12658 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659
12660 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12661 * names. Skip 'I' and 'O', they are used for shell redirection. */
12662 do
12663 {
12664 if (x == 'Z')
12665 x = '0';
12666 else if (x == '9')
12667 x = 'A';
12668 else
12669 {
12670#ifdef EBCDIC
12671 if (x == 'I')
12672 x = 'J';
12673 else if (x == 'R')
12674 x = 'S';
12675 else
12676#endif
12677 ++x;
12678 }
12679 } while (x == 'I' || x == 'O');
12680}
12681
12682/*
12683 * "tolower(string)" function
12684 */
12685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
12690 char_u *p;
12691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 p = vim_strsave(get_tv_string(&argvars[0]));
12693 rettv->v_type = VAR_STRING;
12694 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695
12696 if (p != NULL)
12697 while (*p != NUL)
12698 {
12699#ifdef FEAT_MBYTE
12700 int l;
12701
12702 if (enc_utf8)
12703 {
12704 int c, lc;
12705
12706 c = utf_ptr2char(p);
12707 lc = utf_tolower(c);
12708 l = utf_ptr2len_check(p);
12709 /* TODO: reallocate string when byte count changes. */
12710 if (utf_char2len(lc) == l)
12711 utf_char2bytes(lc, p);
12712 p += l;
12713 }
12714 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12715 p += l; /* skip multi-byte character */
12716 else
12717#endif
12718 {
12719 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12720 ++p;
12721 }
12722 }
12723}
12724
12725/*
12726 * "toupper(string)" function
12727 */
12728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012730 typval_T *argvars;
12731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732{
12733 char_u *p;
12734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 p = vim_strsave(get_tv_string(&argvars[0]));
12736 rettv->v_type = VAR_STRING;
12737 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
12739 if (p != NULL)
12740 while (*p != NUL)
12741 {
12742#ifdef FEAT_MBYTE
12743 int l;
12744
12745 if (enc_utf8)
12746 {
12747 int c, uc;
12748
12749 c = utf_ptr2char(p);
12750 uc = utf_toupper(c);
12751 l = utf_ptr2len_check(p);
12752 /* TODO: reallocate string when byte count changes. */
12753 if (utf_char2len(uc) == l)
12754 utf_char2bytes(uc, p);
12755 p += l;
12756 }
12757 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12758 p += l; /* skip multi-byte character */
12759 else
12760#endif
12761 {
12762 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12763 p++;
12764 }
12765 }
12766}
12767
12768/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012769 * "tr(string, fromstr, tostr)" function
12770 */
12771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012773 typval_T *argvars;
12774 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012775{
12776 char_u *instr;
12777 char_u *fromstr;
12778 char_u *tostr;
12779 char_u *p;
12780#ifdef FEAT_MBYTE
12781 int inlen;
12782 int fromlen;
12783 int tolen;
12784 int idx;
12785 char_u *cpstr;
12786 int cplen;
12787 int first = TRUE;
12788#endif
12789 char_u buf[NUMBUFLEN];
12790 char_u buf2[NUMBUFLEN];
12791 garray_T ga;
12792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 instr = get_tv_string(&argvars[0]);
12794 fromstr = get_tv_string_buf(&argvars[1], buf);
12795 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012796
12797 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->v_type = VAR_STRING;
12799 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012800 ga_init2(&ga, (int)sizeof(char), 80);
12801
12802#ifdef FEAT_MBYTE
12803 if (!has_mbyte)
12804#endif
12805 /* not multi-byte: fromstr and tostr must be the same length */
12806 if (STRLEN(fromstr) != STRLEN(tostr))
12807 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012808#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012809error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012810#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012811 EMSG2(_(e_invarg2), fromstr);
12812 ga_clear(&ga);
12813 return;
12814 }
12815
12816 /* fromstr and tostr have to contain the same number of chars */
12817 while (*instr != NUL)
12818 {
12819#ifdef FEAT_MBYTE
12820 if (has_mbyte)
12821 {
12822 inlen = mb_ptr2len_check(instr);
12823 cpstr = instr;
12824 cplen = inlen;
12825 idx = 0;
12826 for (p = fromstr; *p != NUL; p += fromlen)
12827 {
12828 fromlen = mb_ptr2len_check(p);
12829 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12830 {
12831 for (p = tostr; *p != NUL; p += tolen)
12832 {
12833 tolen = mb_ptr2len_check(p);
12834 if (idx-- == 0)
12835 {
12836 cplen = tolen;
12837 cpstr = p;
12838 break;
12839 }
12840 }
12841 if (*p == NUL) /* tostr is shorter than fromstr */
12842 goto error;
12843 break;
12844 }
12845 ++idx;
12846 }
12847
12848 if (first && cpstr == instr)
12849 {
12850 /* Check that fromstr and tostr have the same number of
12851 * (multi-byte) characters. Done only once when a character
12852 * of instr doesn't appear in fromstr. */
12853 first = FALSE;
12854 for (p = tostr; *p != NUL; p += tolen)
12855 {
12856 tolen = mb_ptr2len_check(p);
12857 --idx;
12858 }
12859 if (idx != 0)
12860 goto error;
12861 }
12862
12863 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012864 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012865 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012866
12867 instr += inlen;
12868 }
12869 else
12870#endif
12871 {
12872 /* When not using multi-byte chars we can do it faster. */
12873 p = vim_strchr(fromstr, *instr);
12874 if (p != NULL)
12875 ga_append(&ga, tostr[p - fromstr]);
12876 else
12877 ga_append(&ga, *instr);
12878 ++instr;
12879 }
12880 }
12881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012883}
12884
12885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 * "type(expr)" function
12887 */
12888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *argvars;
12891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012893 int n;
12894
12895 switch (argvars[0].v_type)
12896 {
12897 case VAR_NUMBER: n = 0; break;
12898 case VAR_STRING: n = 1; break;
12899 case VAR_FUNC: n = 2; break;
12900 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012901 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012902 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12903 }
12904 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905}
12906
12907/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012908 * "values(dict)" function
12909 */
12910 static void
12911f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 typval_T *argvars;
12913 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012914{
12915 dict_list(argvars, rettv, 1);
12916}
12917
12918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 * "virtcol(string)" function
12920 */
12921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012923 typval_T *argvars;
12924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925{
12926 colnr_T vcol = 0;
12927 pos_T *fp;
12928
12929 fp = var2fpos(&argvars[0], FALSE);
12930 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12931 {
12932 getvvcol(curwin, fp, NULL, NULL, &vcol);
12933 ++vcol;
12934 }
12935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937}
12938
12939/*
12940 * "visualmode()" function
12941 */
12942/*ARGSUSED*/
12943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012945 typval_T *argvars;
12946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
12948#ifdef FEAT_VISUAL
12949 char_u str[2];
12950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 str[0] = curbuf->b_visual_mode_eval;
12953 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955
12956 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012957 if ((argvars[0].v_type == VAR_NUMBER
12958 && argvars[0].vval.v_number != 0)
12959 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 curbuf->b_visual_mode_eval = NUL;
12962#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964#endif
12965}
12966
12967/*
12968 * "winbufnr(nr)" function
12969 */
12970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 typval_T *argvars;
12973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974{
12975 win_T *wp;
12976
12977 wp = find_win_by_nr(&argvars[0]);
12978 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982}
12983
12984/*
12985 * "wincol()" function
12986 */
12987/*ARGSUSED*/
12988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012990 typval_T *argvars;
12991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992{
12993 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012994 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995}
12996
12997/*
12998 * "winheight(nr)" function
12999 */
13000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 typval_T *argvars;
13003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004{
13005 win_T *wp;
13006
13007 wp = find_win_by_nr(&argvars[0]);
13008 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013011 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012}
13013
13014/*
13015 * "winline()" function
13016 */
13017/*ARGSUSED*/
13018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013020 typval_T *argvars;
13021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022{
13023 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025}
13026
13027/*
13028 * "winnr()" function
13029 */
13030/* ARGSUSED */
13031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013033 typval_T *argvars;
13034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035{
13036 int nr = 1;
13037#ifdef FEAT_WINDOWS
13038 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013039 win_T *twin = curwin;
13040 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013042 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013045 if (STRCMP(arg, "$") == 0)
13046 twin = lastwin;
13047 else if (STRCMP(arg, "#") == 0)
13048 {
13049 twin = prevwin;
13050 if (prevwin == NULL)
13051 nr = 0;
13052 }
13053 else
13054 {
13055 EMSG2(_(e_invexpr2), arg);
13056 nr = 0;
13057 }
13058 }
13059
13060 if (nr > 0)
13061 for (wp = firstwin; wp != twin; wp = wp->w_next)
13062 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065}
13066
13067/*
13068 * "winrestcmd()" function
13069 */
13070/* ARGSUSED */
13071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013073 typval_T *argvars;
13074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075{
13076#ifdef FEAT_WINDOWS
13077 win_T *wp;
13078 int winnr = 1;
13079 garray_T ga;
13080 char_u buf[50];
13081
13082 ga_init2(&ga, (int)sizeof(char), 70);
13083 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13084 {
13085 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13086 ga_concat(&ga, buf);
13087# ifdef FEAT_VERTSPLIT
13088 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13089 ga_concat(&ga, buf);
13090# endif
13091 ++winnr;
13092 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013093 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013099 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100}
13101
13102/*
13103 * "winwidth(nr)" function
13104 */
13105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013107 typval_T *argvars;
13108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109{
13110 win_T *wp;
13111
13112 wp = find_win_by_nr(&argvars[0]);
13113 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 else
13116#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120#endif
13121}
13122
13123 static win_T *
13124find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013125 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126{
13127#ifdef FEAT_WINDOWS
13128 win_T *wp;
13129#endif
13130 int nr;
13131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133
13134#ifdef FEAT_WINDOWS
13135 if (nr == 0)
13136 return curwin;
13137
13138 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13139 if (--nr <= 0)
13140 break;
13141 return wp;
13142#else
13143 if (nr == 0 || nr == 1)
13144 return curwin;
13145 return NULL;
13146#endif
13147}
13148
13149/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013150 * "writefile()" function
13151 */
13152 static void
13153f_writefile(argvars, rettv)
13154 typval_T *argvars;
13155 typval_T *rettv;
13156{
13157 int binary = FALSE;
13158 char_u *fname;
13159 FILE *fd;
13160 listitem_T *li;
13161 char_u *s;
13162 int ret = 0;
13163 int c;
13164
13165 if (argvars[0].v_type != VAR_LIST)
13166 {
13167 EMSG2(_(e_listarg), "writefile()");
13168 return;
13169 }
13170 if (argvars[0].vval.v_list == NULL)
13171 return;
13172
13173 if (argvars[2].v_type != VAR_UNKNOWN
13174 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13175 binary = TRUE;
13176
13177 /* Always open the file in binary mode, library functions have a mind of
13178 * their own about CR-LF conversion. */
13179 fname = get_tv_string(&argvars[1]);
13180 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13181 {
13182 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13183 ret = -1;
13184 }
13185 else
13186 {
13187 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13188 li = li->li_next)
13189 {
13190 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13191 {
13192 if (*s == '\n')
13193 c = putc(NUL, fd);
13194 else
13195 c = putc(*s, fd);
13196 if (c == EOF)
13197 {
13198 ret = -1;
13199 break;
13200 }
13201 }
13202 if (!binary || li->li_next != NULL)
13203 if (putc('\n', fd) == EOF)
13204 {
13205 ret = -1;
13206 break;
13207 }
13208 if (ret < 0)
13209 {
13210 EMSG(_(e_write));
13211 break;
13212 }
13213 }
13214 fclose(fd);
13215 }
13216
13217 rettv->vval.v_number = ret;
13218}
13219
13220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 * Translate a String variable into a position.
13222 */
13223 static pos_T *
13224var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 int lnum; /* TRUE when $ is last line */
13227{
13228 char_u *name;
13229 static pos_T pos;
13230 pos_T *pp;
13231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 if (name[0] == '.') /* cursor */
13234 return &curwin->w_cursor;
13235 if (name[0] == '\'') /* mark */
13236 {
13237 pp = getmark(name[1], FALSE);
13238 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13239 return NULL;
13240 return pp;
13241 }
13242 if (name[0] == '$') /* last column or line */
13243 {
13244 if (lnum)
13245 {
13246 pos.lnum = curbuf->b_ml.ml_line_count;
13247 pos.col = 0;
13248 }
13249 else
13250 {
13251 pos.lnum = curwin->w_cursor.lnum;
13252 pos.col = (colnr_T)STRLEN(ml_get_curline());
13253 }
13254 return &pos;
13255 }
13256 return NULL;
13257}
13258
13259/*
13260 * Get the length of an environment variable name.
13261 * Advance "arg" to the first character after the name.
13262 * Return 0 for error.
13263 */
13264 static int
13265get_env_len(arg)
13266 char_u **arg;
13267{
13268 char_u *p;
13269 int len;
13270
13271 for (p = *arg; vim_isIDc(*p); ++p)
13272 ;
13273 if (p == *arg) /* no name found */
13274 return 0;
13275
13276 len = (int)(p - *arg);
13277 *arg = p;
13278 return len;
13279}
13280
13281/*
13282 * Get the length of the name of a function or internal variable.
13283 * "arg" is advanced to the first non-white character after the name.
13284 * Return 0 if something is wrong.
13285 */
13286 static int
13287get_id_len(arg)
13288 char_u **arg;
13289{
13290 char_u *p;
13291 int len;
13292
13293 /* Find the end of the name. */
13294 for (p = *arg; eval_isnamec(*p); ++p)
13295 ;
13296 if (p == *arg) /* no name found */
13297 return 0;
13298
13299 len = (int)(p - *arg);
13300 *arg = skipwhite(p);
13301
13302 return len;
13303}
13304
13305/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013306 * Get the length of the name of a variable or function.
13307 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013309 * Return -1 if curly braces expansion failed.
13310 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 * If the name contains 'magic' {}'s, expand them and return the
13312 * expanded name in an allocated string via 'alias' - caller must free.
13313 */
13314 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013315get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 char_u **arg;
13317 char_u **alias;
13318 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013319 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320{
13321 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 char_u *p;
13323 char_u *expr_start;
13324 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325
13326 *alias = NULL; /* default to no alias */
13327
13328 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13329 && (*arg)[2] == (int)KE_SNR)
13330 {
13331 /* hard coded <SNR>, already translated */
13332 *arg += 3;
13333 return get_id_len(arg) + 3;
13334 }
13335 len = eval_fname_script(*arg);
13336 if (len > 0)
13337 {
13338 /* literal "<SID>", "s:" or "<SNR>" */
13339 *arg += len;
13340 }
13341
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 if (expr_start != NULL)
13347 {
13348 char_u *temp_string;
13349
13350 if (!evaluate)
13351 {
13352 len += (int)(p - *arg);
13353 *arg = skipwhite(p);
13354 return len;
13355 }
13356
13357 /*
13358 * Include any <SID> etc in the expanded string:
13359 * Thus the -len here.
13360 */
13361 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13362 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013363 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 *alias = temp_string;
13365 *arg = skipwhite(p);
13366 return (int)STRLEN(temp_string);
13367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368
13369 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013370 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 EMSG2(_(e_invexpr2), *arg);
13372
13373 return len;
13374}
13375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376/*
13377 * Find the end of a variable or function name, taking care of magic braces.
13378 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13379 * start and end of the first magic braces item.
13380 * Return a pointer to just after the name. Equal to "arg" if there is no
13381 * valid name.
13382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 char_u *arg;
13386 char_u **expr_start;
13387 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013388 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 int mb_nest = 0;
13391 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 char_u *p;
13393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013394 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013396 *expr_start = NULL;
13397 *expr_end = NULL;
13398 }
13399
13400 for (p = arg; *p != NUL
13401 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013402 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013403 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404 || mb_nest != 0
13405 || br_nest != 0); ++p)
13406 {
13407 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409 if (*p == '[')
13410 ++br_nest;
13411 else if (*p == ']')
13412 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416 if (*p == '{')
13417 {
13418 mb_nest++;
13419 if (expr_start != NULL && *expr_start == NULL)
13420 *expr_start = p;
13421 }
13422 else if (*p == '}')
13423 {
13424 mb_nest--;
13425 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13426 *expr_end = p;
13427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 }
13430
13431 return p;
13432}
13433
13434/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013435 * Expands out the 'magic' {}'s in a variable/function name.
13436 * Note that this can call itself recursively, to deal with
13437 * constructs like foo{bar}{baz}{bam}
13438 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13439 * "in_start" ^
13440 * "expr_start" ^
13441 * "expr_end" ^
13442 * "in_end" ^
13443 *
13444 * Returns a new allocated string, which the caller must free.
13445 * Returns NULL for failure.
13446 */
13447 static char_u *
13448make_expanded_name(in_start, expr_start, expr_end, in_end)
13449 char_u *in_start;
13450 char_u *expr_start;
13451 char_u *expr_end;
13452 char_u *in_end;
13453{
13454 char_u c1;
13455 char_u *retval = NULL;
13456 char_u *temp_result;
13457 char_u *nextcmd = NULL;
13458
13459 if (expr_end == NULL || in_end == NULL)
13460 return NULL;
13461 *expr_start = NUL;
13462 *expr_end = NUL;
13463 c1 = *in_end;
13464 *in_end = NUL;
13465
13466 temp_result = eval_to_string(expr_start + 1, &nextcmd);
13467 if (temp_result != NULL && nextcmd == NULL)
13468 {
13469 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
13470 + (in_end - expr_end) + 1));
13471 if (retval != NULL)
13472 {
13473 STRCPY(retval, in_start);
13474 STRCAT(retval, temp_result);
13475 STRCAT(retval, expr_end + 1);
13476 }
13477 }
13478 vim_free(temp_result);
13479
13480 *in_end = c1; /* put char back for error messages */
13481 *expr_start = '{';
13482 *expr_end = '}';
13483
13484 if (retval != NULL)
13485 {
13486 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
13487 if (expr_start != NULL)
13488 {
13489 /* Further expansion! */
13490 temp_result = make_expanded_name(retval, expr_start,
13491 expr_end, temp_result);
13492 vim_free(retval);
13493 retval = temp_result;
13494 }
13495 }
13496
13497 return retval;
13498}
13499
13500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013502 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 */
13504 static int
13505eval_isnamec(c)
13506 int c;
13507{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013508 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509}
13510
13511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 * Set number v: variable to "val".
13513 */
13514 void
13515set_vim_var_nr(idx, val)
13516 int idx;
13517 long val;
13518{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013519 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520}
13521
13522/*
13523 * Get number v: variable value;
13524 */
13525 long
13526get_vim_var_nr(idx)
13527 int idx;
13528{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013529 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530}
13531
13532/*
13533 * Set v:count, v:count1 and v:prevcount.
13534 */
13535 void
13536set_vcount(count, count1)
13537 long count;
13538 long count1;
13539{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013540 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
13541 vimvars[VV_COUNT].vv_nr = count;
13542 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543}
13544
13545/*
13546 * Set string v: variable to a copy of "val".
13547 */
13548 void
13549set_vim_var_string(idx, val, len)
13550 int idx;
13551 char_u *val;
13552 int len; /* length of "val" to use or -1 (whole string) */
13553{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013554 /* Need to do this (at least) once, since we can't initialize a union.
13555 * Will always be invoked when "v:progname" is set. */
13556 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
13557
Bram Moolenaare9a41262005-01-15 22:18:47 +000013558 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013560 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013562 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013564 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565}
13566
13567/*
13568 * Set v:register if needed.
13569 */
13570 void
13571set_reg_var(c)
13572 int c;
13573{
13574 char_u regname;
13575
13576 if (c == 0 || c == ' ')
13577 regname = '"';
13578 else
13579 regname = c;
13580 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013581 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 set_vim_var_string(VV_REG, &regname, 1);
13583}
13584
13585/*
13586 * Get or set v:exception. If "oldval" == NULL, return the current value.
13587 * Otherwise, restore the value to "oldval" and return NULL.
13588 * Must always be called in pairs to save and restore v:exception! Does not
13589 * take care of memory allocations.
13590 */
13591 char_u *
13592v_exception(oldval)
13593 char_u *oldval;
13594{
13595 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013596 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597
Bram Moolenaare9a41262005-01-15 22:18:47 +000013598 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 return NULL;
13600}
13601
13602/*
13603 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
13604 * Otherwise, restore the value to "oldval" and return NULL.
13605 * Must always be called in pairs to save and restore v:throwpoint! Does not
13606 * take care of memory allocations.
13607 */
13608 char_u *
13609v_throwpoint(oldval)
13610 char_u *oldval;
13611{
13612 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013613 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
Bram Moolenaare9a41262005-01-15 22:18:47 +000013615 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 return NULL;
13617}
13618
13619#if defined(FEAT_AUTOCMD) || defined(PROTO)
13620/*
13621 * Set v:cmdarg.
13622 * If "eap" != NULL, use "eap" to generate the value and return the old value.
13623 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
13624 * Must always be called in pairs!
13625 */
13626 char_u *
13627set_cmdarg(eap, oldarg)
13628 exarg_T *eap;
13629 char_u *oldarg;
13630{
13631 char_u *oldval;
13632 char_u *newval;
13633 unsigned len;
13634
Bram Moolenaare9a41262005-01-15 22:18:47 +000013635 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013636 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013638 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000013639 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 }
13642
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013643 if (eap->force_bin == FORCE_BIN)
13644 len = 6;
13645 else if (eap->force_bin == FORCE_NOBIN)
13646 len = 8;
13647 else
13648 len = 0;
13649 if (eap->force_ff != 0)
13650 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
13651# ifdef FEAT_MBYTE
13652 if (eap->force_enc != 0)
13653 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
13654# endif
13655
13656 newval = alloc(len + 1);
13657 if (newval == NULL)
13658 return NULL;
13659
13660 if (eap->force_bin == FORCE_BIN)
13661 sprintf((char *)newval, " ++bin");
13662 else if (eap->force_bin == FORCE_NOBIN)
13663 sprintf((char *)newval, " ++nobin");
13664 else
13665 *newval = NUL;
13666 if (eap->force_ff != 0)
13667 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
13668 eap->cmd + eap->force_ff);
13669# ifdef FEAT_MBYTE
13670 if (eap->force_enc != 0)
13671 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
13672 eap->cmd + eap->force_enc);
13673# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000013674 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013675 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676}
13677#endif
13678
13679/*
13680 * Get the value of internal variable "name".
13681 * Return OK or FAIL.
13682 */
13683 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013684get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 char_u *name;
13686 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000013687 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013688 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689{
13690 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 typval_T *tv = NULL;
13692 typval_T atv;
13693 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695
13696 /* truncate the name, so that we can use strcmp() */
13697 cc = name[len];
13698 name[len] = NUL;
13699
13700 /*
13701 * Check for "b:changedtick".
13702 */
13703 if (STRCMP(name, "b:changedtick") == 0)
13704 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013705 atv.v_type = VAR_NUMBER;
13706 atv.vval.v_number = curbuf->b_changedtick;
13707 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 }
13709
13710 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 * Check for user-defined variables.
13712 */
13713 else
13714 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013715 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 }
13719
Bram Moolenaare9a41262005-01-15 22:18:47 +000013720 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013722 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013723 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 ret = FAIL;
13725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013727 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728
13729 name[len] = cc;
13730
13731 return ret;
13732}
13733
13734/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013735 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
13736 * Also handle function call with Funcref variable: func(expr)
13737 * Can all be combined: dict.func(expr)[idx]['func'](expr)
13738 */
13739 static int
13740handle_subscript(arg, rettv, evaluate, verbose)
13741 char_u **arg;
13742 typval_T *rettv;
13743 int evaluate; /* do more than finding the end */
13744 int verbose; /* give error messages */
13745{
13746 int ret = OK;
13747 dict_T *selfdict = NULL;
13748 char_u *s;
13749 int len;
13750
13751 while (ret == OK
13752 && (**arg == '['
13753 || (**arg == '.' && rettv->v_type == VAR_DICT)
13754 || (**arg == '(' && rettv->v_type == VAR_FUNC))
13755 && !vim_iswhite(*(*arg - 1)))
13756 {
13757 if (**arg == '(')
13758 {
13759 s = rettv->vval.v_string;
13760
13761 /* Invoke the function. Recursive! */
13762 ret = get_func_tv(s, STRLEN(s), rettv, arg,
13763 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
13764 &len, evaluate, selfdict);
13765
13766 /* Stop the expression evaluation when immediately aborting on
13767 * error, or when an interrupt occurred or an exception was thrown
13768 * but not caught. */
13769 if (aborting())
13770 {
13771 if (ret == OK)
13772 clear_tv(rettv);
13773 ret = FAIL;
13774 }
13775 dict_unref(selfdict);
13776 selfdict = NULL;
13777 }
13778 else /* **arg == '[' || **arg == '.' */
13779 {
13780 dict_unref(selfdict);
13781 if (rettv->v_type == VAR_DICT)
13782 {
13783 selfdict = rettv->vval.v_dict;
13784 if (selfdict != NULL)
13785 ++selfdict->dv_refcount;
13786 }
13787 else
13788 selfdict = NULL;
13789 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
13790 {
13791 clear_tv(rettv);
13792 ret = FAIL;
13793 }
13794 }
13795 }
13796 dict_unref(selfdict);
13797 return ret;
13798}
13799
13800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013801 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13802 * value).
13803 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013806{
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013808}
13809
13810/*
13811 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 * The string "s" must have been allocated, it is consumed.
13813 * Return NULL for out of memory, the variable otherwise.
13814 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013815 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 char_u *s;
13818{
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013821 rettv = alloc_tv();
13822 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->v_type = VAR_STRING;
13825 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 }
13827 else
13828 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830}
13831
13832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013833 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 */
13835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013837 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838{
13839 if (varp != NULL)
13840 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013841 switch (varp->v_type)
13842 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013843 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013844 func_unref(varp->vval.v_string);
13845 /*FALLTHROUGH*/
13846 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013847 vim_free(varp->vval.v_string);
13848 break;
13849 case VAR_LIST:
13850 list_unref(varp->vval.v_list);
13851 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013852 case VAR_DICT:
13853 dict_unref(varp->vval.v_dict);
13854 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013855 case VAR_NUMBER:
13856 case VAR_UNKNOWN:
13857 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013858 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000013859 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860 break;
13861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 vim_free(varp);
13863 }
13864}
13865
13866/*
13867 * Free the memory for a variable value and set the value to NULL or 0.
13868 */
13869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872{
13873 if (varp != NULL)
13874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013877 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013878 func_unref(varp->vval.v_string);
13879 /*FALLTHROUGH*/
13880 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013881 vim_free(varp->vval.v_string);
13882 varp->vval.v_string = NULL;
13883 break;
13884 case VAR_LIST:
13885 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013886 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013887 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013888 case VAR_DICT:
13889 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013890 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013891 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013893 varp->vval.v_number = 0;
13894 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013895 case VAR_UNKNOWN:
13896 break;
13897 default:
13898 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013900 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 }
13902}
13903
13904/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905 * Set the value of a variable to NULL without freeing items.
13906 */
13907 static void
13908init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013909 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910{
13911 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013912 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913}
13914
13915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 * Get the number value of a variable.
13917 * If it is a String variable, uses vim_str2nr().
13918 */
13919 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013921 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013923 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013925 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013927 case VAR_NUMBER:
13928 n = (long)(varp->vval.v_number);
13929 break;
13930 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013931 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013932 break;
13933 case VAR_STRING:
13934 if (varp->vval.v_string != NULL)
13935 vim_str2nr(varp->vval.v_string, NULL, NULL,
13936 TRUE, TRUE, &n, NULL);
13937 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013938 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000013939 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013940 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013941 case VAR_DICT:
13942 EMSG(_("E728: Using a Dictionary as a number"));
13943 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013944 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013945 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013948 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949}
13950
13951/*
13952 * Get the lnum from the first argument. Also accepts ".", "$", etc.
13953 */
13954 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000013956 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957{
Bram Moolenaar33570922005-01-25 22:26:29 +000013958 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 linenr_T lnum;
13960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 if (lnum == 0) /* no valid number, try using line() */
13963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 rettv.v_type = VAR_NUMBER;
13965 f_line(argvars, &rettv);
13966 lnum = rettv.vval.v_number;
13967 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 }
13969 return lnum;
13970}
13971
13972/*
13973 * Get the string value of a variable.
13974 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000013975 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
13976 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 * If the String variable has never been set, return an empty string.
13978 * Never returns NULL;
13979 */
13980 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013982 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983{
13984 static char_u mybuf[NUMBUFLEN];
13985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987}
13988
13989 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000013991 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013992 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013994 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013996 case VAR_NUMBER:
13997 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
13998 return buf;
13999 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014000 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014001 break;
14002 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014003 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014004 break;
14005 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014006 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014007 break;
14008 case VAR_STRING:
14009 if (varp->vval.v_string != NULL)
14010 return varp->vval.v_string;
14011 break;
14012 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014014 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014016 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017}
14018
14019/*
14020 * Find variable "name" in the list of variables.
14021 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014022 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014023 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014024 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014026 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014027find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014029 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014032 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033
Bram Moolenaara7043832005-01-21 11:56:39 +000014034 ht = find_var_ht(name, &varname);
14035 if (htp != NULL)
14036 *htp = ht;
14037 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 return NULL;
Bram Moolenaara7043832005-01-21 11:56:39 +000014039 return find_var_in_ht(ht, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040}
14041
14042/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014043 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014044 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014046 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014047find_var_in_ht(ht, varname)
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014049 char_u *varname;
14050{
Bram Moolenaar33570922005-01-25 22:26:29 +000014051 hashitem_T *hi;
14052
14053 if (*varname == NUL)
14054 {
14055 /* Must be something like "s:", otherwise "ht" would be NULL. */
14056 switch (varname[-2])
14057 {
14058 case 's': return &SCRIPT_SV(current_SID).sv_var;
14059 case 'g': return &globvars_var;
14060 case 'v': return &vimvars_var;
14061 case 'b': return &curbuf->b_bufvar;
14062 case 'w': return &curwin->w_winvar;
14063 case 'l': return &current_funccal->l_vars_var;
14064 case 'a': return &current_funccal->l_avars_var;
14065 }
14066 return NULL;
14067 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014068
14069 hi = hash_find(ht, varname);
14070 if (HASHITEM_EMPTY(hi))
14071 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014072 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014073}
14074
14075/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014077 * Set "varname" to the start of name without ':'.
14078 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014079 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014080find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 char_u *name;
14082 char_u **varname;
14083{
14084 if (name[1] != ':')
14085 {
14086 /* If not "x:name" there must not be any ":" in the name. */
14087 if (vim_strchr(name, ':') != NULL)
14088 return NULL;
14089 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014090
14091 /* "version" is "v:version" in all scopes */
14092 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14093 return &compat_hashtab;
14094
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 return &globvarht; /* global variable */
14097 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 }
14099 *varname = name + 2;
14100 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 if (*name == 'g') /* global variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 return &globvarht;
14106 if (*name == 'v') /* v: variable */
14107 return &vimvarht;
14108 if (*name == 'a' && current_funccal != NULL) /* function argument */
14109 return &current_funccal->l_avars.dv_hashtab;
14110 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14111 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 if (*name == 's' /* script variable */
14113 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14114 return &SCRIPT_VARS(current_SID);
14115 return NULL;
14116}
14117
14118/*
14119 * Get the string value of a (global/local) variable.
14120 * Returns NULL when it doesn't exist.
14121 */
14122 char_u *
14123get_var_value(name)
14124 char_u *name;
14125{
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127
Bram Moolenaara7043832005-01-21 11:56:39 +000014128 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 if (v == NULL)
14130 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014131 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132}
14133
14134/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014135 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 * sourcing this script and when executing functions defined in the script.
14137 */
14138 void
14139new_script_vars(id)
14140 scid_T id;
14141{
Bram Moolenaara7043832005-01-21 11:56:39 +000014142 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 hashtab_T *ht;
14144 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014145
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14147 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014148 /* Re-allocating ga_data means that an ht_array pointing to
14149 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014150 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014151 for (i = 1; i <= ga_scripts.ga_len; ++i)
14152 {
14153 ht = &SCRIPT_VARS(i);
14154 if (ht->ht_mask == HT_INIT_SIZE - 1)
14155 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 sv = &SCRIPT_SV(i);
14157 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014158 }
14159
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 while (ga_scripts.ga_len < id)
14161 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14163 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165 }
14166 }
14167}
14168
14169/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14171 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 */
14173 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014174init_var_dict(dict, dict_var)
14175 dict_T *dict;
14176 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177{
Bram Moolenaar33570922005-01-25 22:26:29 +000014178 hash_init(&dict->dv_hashtab);
14179 dict->dv_refcount = 99999;
14180 dict_var->di_tv.vval.v_dict = dict;
14181 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014182 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14184 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185}
14186
14187/*
14188 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 * Frees all allocated variables and the value they contain.
14190 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 */
14192 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014193vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 hashtab_T *ht;
14195{
14196 vars_clear_ext(ht, TRUE);
14197}
14198
14199/*
14200 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14201 */
14202 static void
14203vars_clear_ext(ht, free_val)
14204 hashtab_T *ht;
14205 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206{
Bram Moolenaara7043832005-01-21 11:56:39 +000014207 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 hashitem_T *hi;
14209 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014212 todo = ht->ht_used;
14213 for (hi = ht->ht_array; todo > 0; ++hi)
14214 {
14215 if (!HASHITEM_EMPTY(hi))
14216 {
14217 --todo;
14218
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014220 * ht_array might change then. hash_clear() takes care of it
14221 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014222 v = HI2DI(hi);
14223 if (free_val)
14224 clear_tv(&v->di_tv);
14225 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14226 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014227 }
14228 }
14229 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230}
14231
Bram Moolenaara7043832005-01-21 11:56:39 +000014232/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014233 * Delete a variable from hashtab "ht" at item "hi".
14234 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014237delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 hashtab_T *ht;
14239 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240{
Bram Moolenaar33570922005-01-25 22:26:29 +000014241 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014242
14243 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014244 clear_tv(&di->di_tv);
14245 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246}
14247
14248/*
14249 * List the value of one internal variable.
14250 */
14251 static void
14252list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014253 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 char_u *prefix;
14255{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014256 char_u *tofree;
14257 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014258 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014259
Bram Moolenaar33570922005-01-25 22:26:29 +000014260 s = echo_string(&v->di_tv, &tofree, numbuf);
14261 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014262 s == NULL ? (char_u *)"" : s);
14263 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264}
14265
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 static void
14267list_one_var_a(prefix, name, type, string)
14268 char_u *prefix;
14269 char_u *name;
14270 int type;
14271 char_u *string;
14272{
14273 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14274 if (name != NULL) /* "a:" vars don't have a name stored */
14275 msg_puts(name);
14276 msg_putchar(' ');
14277 msg_advance(22);
14278 if (type == VAR_NUMBER)
14279 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014280 else if (type == VAR_FUNC)
14281 msg_putchar('*');
14282 else if (type == VAR_LIST)
14283 {
14284 msg_putchar('[');
14285 if (*string == '[')
14286 ++string;
14287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014288 else if (type == VAR_DICT)
14289 {
14290 msg_putchar('{');
14291 if (*string == '{')
14292 ++string;
14293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 else
14295 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014296
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014298
14299 if (type == VAR_FUNC)
14300 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301}
14302
14303/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014304 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 * If the variable already exists, the value is updated.
14306 * Otherwise the variable is created.
14307 */
14308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014311 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313{
Bram Moolenaar33570922005-01-25 22:26:29 +000014314 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014316 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014319 {
14320 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14321 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14322 ? name[2] : name[0]))
14323 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014324 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014325 return;
14326 }
14327 if (function_exists(name))
14328 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014329 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330 return;
14331 }
14332 }
14333
Bram Moolenaara7043832005-01-21 11:56:39 +000014334 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014336 {
14337 EMSG2(_("E461: Illegal variable name: %s"), name);
14338 return;
14339 }
14340
14341 v = find_var_in_ht(ht, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014344 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014345 if (var_check_ro(v->di_flags, name)
14346 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 return;
14348 if (v->di_tv.v_type != tv->v_type
14349 && !((v->di_tv.v_type == VAR_STRING
14350 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 && (tv->v_type == VAR_STRING
14352 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014353 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014354 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014355 return;
14356 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014357
14358 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014359 * Handle setting internal v: variables separately: we don't change
14360 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 */
14362 if (ht == &vimvarht)
14363 {
14364 if (v->di_tv.v_type == VAR_STRING)
14365 {
14366 vim_free(v->di_tv.vval.v_string);
14367 if (copy || tv->v_type != VAR_STRING)
14368 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14369 else
14370 {
14371 /* Take over the string to avoid an extra alloc/free. */
14372 v->di_tv.vval.v_string = tv->vval.v_string;
14373 tv->vval.v_string = NULL;
14374 }
14375 }
14376 else if (v->di_tv.v_type != VAR_NUMBER)
14377 EMSG2(_(e_intern2), "set_var()");
14378 else
14379 v->di_tv.vval.v_number = get_tv_number(tv);
14380 return;
14381 }
14382
14383 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 }
14385 else /* add a new variable */
14386 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014387 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14388 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014389 if (v == NULL)
14390 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014391 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014394 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 return;
14396 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014397 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014401 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014402 else
14403 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014405 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408}
14409
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014411 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14412 * Also give an error message.
14413 */
14414 static int
14415var_check_ro(flags, name)
14416 int flags;
14417 char_u *name;
14418{
14419 if (flags & DI_FLAGS_RO)
14420 {
14421 EMSG2(_(e_readonlyvar), name);
14422 return TRUE;
14423 }
14424 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14425 {
14426 EMSG2(_(e_readonlysbx), name);
14427 return TRUE;
14428 }
14429 return FALSE;
14430}
14431
14432/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014433 * Return TRUE if typeval "tv" is set to be locked (immutable).
14434 * Also give an error message, using "name".
14435 */
14436 static int
14437tv_check_lock(lock, name)
14438 int lock;
14439 char_u *name;
14440{
14441 if (lock & VAR_LOCKED)
14442 {
14443 EMSG2(_("E741: Value is locked: %s"),
14444 name == NULL ? (char_u *)_("Unknown") : name);
14445 return TRUE;
14446 }
14447 if (lock & VAR_FIXED)
14448 {
14449 EMSG2(_("E742: Cannot change value of %s"),
14450 name == NULL ? (char_u *)_("Unknown") : name);
14451 return TRUE;
14452 }
14453 return FALSE;
14454}
14455
14456/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014459 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000014463 typval_T *from;
14464 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014466 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014467 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014468 switch (from->v_type)
14469 {
14470 case VAR_NUMBER:
14471 to->vval.v_number = from->vval.v_number;
14472 break;
14473 case VAR_STRING:
14474 case VAR_FUNC:
14475 if (from->vval.v_string == NULL)
14476 to->vval.v_string = NULL;
14477 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014479 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014480 if (from->v_type == VAR_FUNC)
14481 func_ref(to->vval.v_string);
14482 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014483 break;
14484 case VAR_LIST:
14485 if (from->vval.v_list == NULL)
14486 to->vval.v_list = NULL;
14487 else
14488 {
14489 to->vval.v_list = from->vval.v_list;
14490 ++to->vval.v_list->lv_refcount;
14491 }
14492 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014493 case VAR_DICT:
14494 if (from->vval.v_dict == NULL)
14495 to->vval.v_dict = NULL;
14496 else
14497 {
14498 to->vval.v_dict = from->vval.v_dict;
14499 ++to->vval.v_dict->dv_refcount;
14500 }
14501 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014502 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014504 break;
14505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506}
14507
14508/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014509 * Make a copy of an item.
14510 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
14511 */
14512 static void
14513item_copy(from, to, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +000014514 typval_T *from;
14515 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014516 int deep;
14517{
14518 static int recurse = 0;
14519
Bram Moolenaar33570922005-01-25 22:26:29 +000014520 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014521 {
14522 EMSG(_("E698: variable nested too deep for making a copy"));
14523 return;
14524 }
14525 ++recurse;
14526
14527 switch (from->v_type)
14528 {
14529 case VAR_NUMBER:
14530 case VAR_STRING:
14531 case VAR_FUNC:
14532 copy_tv(from, to);
14533 break;
14534 case VAR_LIST:
14535 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014536 to->v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014537 to->vval.v_list = list_copy(from->vval.v_list, deep);
14538 break;
14539 case VAR_DICT:
14540 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014541 to->v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014542 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
14543 break;
14544 default:
14545 EMSG2(_(e_intern2), "item_copy()");
14546 }
14547 --recurse;
14548}
14549
14550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 * ":echo expr1 ..." print each argument separated with a space, add a
14552 * newline at the end.
14553 * ":echon expr1 ..." print each argument plain.
14554 */
14555 void
14556ex_echo(eap)
14557 exarg_T *eap;
14558{
14559 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014561 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 char_u *p;
14563 int needclr = TRUE;
14564 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
14567 if (eap->skip)
14568 ++emsg_skip;
14569 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
14570 {
14571 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 {
14574 /*
14575 * Report the invalid expression unless the expression evaluation
14576 * has been cancelled due to an aborting error, an interrupt, or an
14577 * exception.
14578 */
14579 if (!aborting())
14580 EMSG2(_(e_invexpr2), p);
14581 break;
14582 }
14583 if (!eap->skip)
14584 {
14585 if (atstart)
14586 {
14587 atstart = FALSE;
14588 /* Call msg_start() after eval1(), evaluating the expression
14589 * may cause a message to appear. */
14590 if (eap->cmdidx == CMD_echo)
14591 msg_start();
14592 }
14593 else if (eap->cmdidx == CMD_echo)
14594 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014595 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014596 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 if (*p == '\n' || *p == '\r' || *p == TAB)
14598 {
14599 if (*p != TAB && needclr)
14600 {
14601 /* remove any text still there from the command */
14602 msg_clr_eos();
14603 needclr = FALSE;
14604 }
14605 msg_putchar_attr(*p, echo_attr);
14606 }
14607 else
14608 {
14609#ifdef FEAT_MBYTE
14610 if (has_mbyte)
14611 {
14612 int i = (*mb_ptr2len_check)(p);
14613
14614 (void)msg_outtrans_len_attr(p, i, echo_attr);
14615 p += i - 1;
14616 }
14617 else
14618#endif
14619 (void)msg_outtrans_len_attr(p, 1, echo_attr);
14620 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014621 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 arg = skipwhite(arg);
14625 }
14626 eap->nextcmd = check_nextcmd(arg);
14627
14628 if (eap->skip)
14629 --emsg_skip;
14630 else
14631 {
14632 /* remove text that may still be there from the command */
14633 if (needclr)
14634 msg_clr_eos();
14635 if (eap->cmdidx == CMD_echo)
14636 msg_end();
14637 }
14638}
14639
14640/*
14641 * ":echohl {name}".
14642 */
14643 void
14644ex_echohl(eap)
14645 exarg_T *eap;
14646{
14647 int id;
14648
14649 id = syn_name2id(eap->arg);
14650 if (id == 0)
14651 echo_attr = 0;
14652 else
14653 echo_attr = syn_id2attr(id);
14654}
14655
14656/*
14657 * ":execute expr1 ..." execute the result of an expression.
14658 * ":echomsg expr1 ..." Print a message
14659 * ":echoerr expr1 ..." Print an error
14660 * Each gets spaces around each argument and a newline at the end for
14661 * echo commands
14662 */
14663 void
14664ex_execute(eap)
14665 exarg_T *eap;
14666{
14667 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 int ret = OK;
14670 char_u *p;
14671 garray_T ga;
14672 int len;
14673 int save_did_emsg;
14674
14675 ga_init2(&ga, 1, 80);
14676
14677 if (eap->skip)
14678 ++emsg_skip;
14679 while (*arg != NUL && *arg != '|' && *arg != '\n')
14680 {
14681 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 {
14684 /*
14685 * Report the invalid expression unless the expression evaluation
14686 * has been cancelled due to an aborting error, an interrupt, or an
14687 * exception.
14688 */
14689 if (!aborting())
14690 EMSG2(_(e_invexpr2), p);
14691 ret = FAIL;
14692 break;
14693 }
14694
14695 if (!eap->skip)
14696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014697 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 len = (int)STRLEN(p);
14699 if (ga_grow(&ga, len + 2) == FAIL)
14700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014701 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 ret = FAIL;
14703 break;
14704 }
14705 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 ga.ga_len += len;
14709 }
14710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 arg = skipwhite(arg);
14713 }
14714
14715 if (ret != FAIL && ga.ga_data != NULL)
14716 {
14717 if (eap->cmdidx == CMD_echomsg)
14718 MSG_ATTR(ga.ga_data, echo_attr);
14719 else if (eap->cmdidx == CMD_echoerr)
14720 {
14721 /* We don't want to abort following commands, restore did_emsg. */
14722 save_did_emsg = did_emsg;
14723 EMSG((char_u *)ga.ga_data);
14724 if (!force_abort)
14725 did_emsg = save_did_emsg;
14726 }
14727 else if (eap->cmdidx == CMD_execute)
14728 do_cmdline((char_u *)ga.ga_data,
14729 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
14730 }
14731
14732 ga_clear(&ga);
14733
14734 if (eap->skip)
14735 --emsg_skip;
14736
14737 eap->nextcmd = check_nextcmd(arg);
14738}
14739
14740/*
14741 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
14742 * "arg" points to the "&" or '+' when called, to "option" when returning.
14743 * Returns NULL when no option name found. Otherwise pointer to the char
14744 * after the option name.
14745 */
14746 static char_u *
14747find_option_end(arg, opt_flags)
14748 char_u **arg;
14749 int *opt_flags;
14750{
14751 char_u *p = *arg;
14752
14753 ++p;
14754 if (*p == 'g' && p[1] == ':')
14755 {
14756 *opt_flags = OPT_GLOBAL;
14757 p += 2;
14758 }
14759 else if (*p == 'l' && p[1] == ':')
14760 {
14761 *opt_flags = OPT_LOCAL;
14762 p += 2;
14763 }
14764 else
14765 *opt_flags = 0;
14766
14767 if (!ASCII_ISALPHA(*p))
14768 return NULL;
14769 *arg = p;
14770
14771 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
14772 p += 4; /* termcap option */
14773 else
14774 while (ASCII_ISALPHA(*p))
14775 ++p;
14776 return p;
14777}
14778
14779/*
14780 * ":function"
14781 */
14782 void
14783ex_function(eap)
14784 exarg_T *eap;
14785{
14786 char_u *theline;
14787 int j;
14788 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 char_u *name = NULL;
14791 char_u *p;
14792 char_u *arg;
14793 garray_T newargs;
14794 garray_T newlines;
14795 int varargs = FALSE;
14796 int mustend = FALSE;
14797 int flags = 0;
14798 ufunc_T *fp;
14799 int indent;
14800 int nesting;
14801 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 dictitem_T *v;
14803 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014804 static int func_nr = 0; /* number for nameless function */
14805 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806
14807 /*
14808 * ":function" without argument: list functions.
14809 */
14810 if (ends_excmd(*eap->arg))
14811 {
14812 if (!eap->skip)
14813 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014814 if (!isdigit(*fp->name))
14815 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 eap->nextcmd = check_nextcmd(eap->arg);
14817 return;
14818 }
14819
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014820 /*
14821 * Get the function name. There are these situations:
14822 * func normal function name
14823 * "name" == func, "fudi.fd_dict" == NULL
14824 * dict.func new dictionary entry
14825 * "name" == NULL, "fudi.fd_dict" set,
14826 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
14827 * dict.func existing dict entry with a Funcref
14828 * "name" == fname, "fudi.fd_dict" set,
14829 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14830 * dict.func existing dict entry that's not a Funcref
14831 * "name" == NULL, "fudi.fd_dict" set,
14832 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014835 name = trans_function_name(&p, eap->skip, 0, &fudi);
14836 paren = (vim_strchr(p, '(') != NULL);
14837 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 {
14839 /*
14840 * Return on an invalid expression in braces, unless the expression
14841 * evaluation has been cancelled due to an aborting error, an
14842 * interrupt, or an exception.
14843 */
14844 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014846 if (!eap->skip && fudi.fd_newkey != NULL)
14847 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014848 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 else
14852 eap->skip = TRUE;
14853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 /* An error in a function call during evaluation of an expression in magic
14855 * braces should not cause the function not to be defined. */
14856 saved_did_emsg = did_emsg;
14857 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858
14859 /*
14860 * ":function func" with only function name: list function.
14861 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014862 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 {
14864 if (!ends_excmd(*skipwhite(p)))
14865 {
14866 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014867 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 }
14869 eap->nextcmd = check_nextcmd(p);
14870 if (eap->nextcmd != NULL)
14871 *p = NUL;
14872 if (!eap->skip && !got_int)
14873 {
14874 fp = find_func(name);
14875 if (fp != NULL)
14876 {
14877 list_func_head(fp, TRUE);
14878 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
14879 {
14880 msg_putchar('\n');
14881 msg_outnum((long)(j + 1));
14882 if (j < 9)
14883 msg_putchar(' ');
14884 if (j < 99)
14885 msg_putchar(' ');
14886 msg_prt_line(FUNCLINE(fp, j));
14887 out_flush(); /* show a line at a time */
14888 ui_breakcheck();
14889 }
14890 if (!got_int)
14891 {
14892 msg_putchar('\n');
14893 msg_puts((char_u *)" endfunction");
14894 }
14895 }
14896 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014897 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014899 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 }
14901
14902 /*
14903 * ":function name(arg1, arg2)" Define function.
14904 */
14905 p = skipwhite(p);
14906 if (*p != '(')
14907 {
14908 if (!eap->skip)
14909 {
14910 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014911 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 }
14913 /* attempt to continue by skipping some text */
14914 if (vim_strchr(p, '(') != NULL)
14915 p = vim_strchr(p, '(');
14916 }
14917 p = skipwhite(p + 1);
14918
14919 ga_init2(&newargs, (int)sizeof(char_u *), 3);
14920 ga_init2(&newlines, (int)sizeof(char_u *), 3);
14921
14922 /*
14923 * Isolate the arguments: "arg1, arg2, ...)"
14924 */
14925 while (*p != ')')
14926 {
14927 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
14928 {
14929 varargs = TRUE;
14930 p += 3;
14931 mustend = TRUE;
14932 }
14933 else
14934 {
14935 arg = p;
14936 while (ASCII_ISALNUM(*p) || *p == '_')
14937 ++p;
14938 if (arg == p || isdigit(*arg)
14939 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
14940 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
14941 {
14942 if (!eap->skip)
14943 EMSG2(_("E125: Illegal argument: %s"), arg);
14944 break;
14945 }
14946 if (ga_grow(&newargs, 1) == FAIL)
14947 goto erret;
14948 c = *p;
14949 *p = NUL;
14950 arg = vim_strsave(arg);
14951 if (arg == NULL)
14952 goto erret;
14953 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
14954 *p = c;
14955 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 if (*p == ',')
14957 ++p;
14958 else
14959 mustend = TRUE;
14960 }
14961 p = skipwhite(p);
14962 if (mustend && *p != ')')
14963 {
14964 if (!eap->skip)
14965 EMSG2(_(e_invarg2), eap->arg);
14966 break;
14967 }
14968 }
14969 ++p; /* skip the ')' */
14970
Bram Moolenaare9a41262005-01-15 22:18:47 +000014971 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 for (;;)
14973 {
14974 p = skipwhite(p);
14975 if (STRNCMP(p, "range", 5) == 0)
14976 {
14977 flags |= FC_RANGE;
14978 p += 5;
14979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014980 else if (STRNCMP(p, "dict", 4) == 0)
14981 {
14982 flags |= FC_DICT;
14983 p += 4;
14984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 else if (STRNCMP(p, "abort", 5) == 0)
14986 {
14987 flags |= FC_ABORT;
14988 p += 5;
14989 }
14990 else
14991 break;
14992 }
14993
14994 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
14995 EMSG(_(e_trailing));
14996
14997 /*
14998 * Read the body of the function, until ":endfunction" is found.
14999 */
15000 if (KeyTyped)
15001 {
15002 /* Check if the function already exists, don't let the user type the
15003 * whole function before telling him it doesn't work! For a script we
15004 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015005 if (!eap->skip && !eap->forceit)
15006 {
15007 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15008 EMSG(_(e_funcdict));
15009 else if (name != NULL && find_func(name) != NULL)
15010 EMSG2(_(e_funcexts), name);
15011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012
15013 msg_putchar('\n'); /* don't overwrite the function name */
15014 cmdline_row = msg_row;
15015 }
15016
15017 indent = 2;
15018 nesting = 0;
15019 for (;;)
15020 {
15021 msg_scroll = TRUE;
15022 need_wait_return = FALSE;
15023 if (eap->getline == NULL)
15024 theline = getcmdline(':', 0L, indent);
15025 else
15026 theline = eap->getline(':', eap->cookie, indent);
15027 if (KeyTyped)
15028 lines_left = Rows - 1;
15029 if (theline == NULL)
15030 {
15031 EMSG(_("E126: Missing :endfunction"));
15032 goto erret;
15033 }
15034
15035 if (skip_until != NULL)
15036 {
15037 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15038 * don't check for ":endfunc". */
15039 if (STRCMP(theline, skip_until) == 0)
15040 {
15041 vim_free(skip_until);
15042 skip_until = NULL;
15043 }
15044 }
15045 else
15046 {
15047 /* skip ':' and blanks*/
15048 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15049 ;
15050
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015051 /* Check for "endfunction". */
15052 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 {
15054 vim_free(theline);
15055 break;
15056 }
15057
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015058 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 * at "end". */
15060 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15061 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015062 else if (STRNCMP(p, "if", 2) == 0
15063 || STRNCMP(p, "wh", 2) == 0
15064 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 || STRNCMP(p, "try", 3) == 0)
15066 indent += 2;
15067
15068 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015069 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015071 if (*p == '!')
15072 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 p += eval_fname_script(p);
15074 if (ASCII_ISALPHA(*p))
15075 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015076 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 if (*skipwhite(p) == '(')
15078 {
15079 ++nesting;
15080 indent += 2;
15081 }
15082 }
15083 }
15084
15085 /* Check for ":append" or ":insert". */
15086 p = skip_range(p, NULL);
15087 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15088 || (p[0] == 'i'
15089 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15090 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15091 skip_until = vim_strsave((char_u *)".");
15092
15093 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15094 arg = skipwhite(skiptowhite(p));
15095 if (arg[0] == '<' && arg[1] =='<'
15096 && ((p[0] == 'p' && p[1] == 'y'
15097 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15098 || (p[0] == 'p' && p[1] == 'e'
15099 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15100 || (p[0] == 't' && p[1] == 'c'
15101 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15102 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15103 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015104 || (p[0] == 'm' && p[1] == 'z'
15105 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 ))
15107 {
15108 /* ":python <<" continues until a dot, like ":append" */
15109 p = skipwhite(arg + 2);
15110 if (*p == NUL)
15111 skip_until = vim_strsave((char_u *)".");
15112 else
15113 skip_until = vim_strsave(p);
15114 }
15115 }
15116
15117 /* Add the line to the function. */
15118 if (ga_grow(&newlines, 1) == FAIL)
15119 goto erret;
15120 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15121 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 }
15123
15124 /* Don't define the function when skipping commands or when an error was
15125 * detected. */
15126 if (eap->skip || did_emsg)
15127 goto erret;
15128
15129 /*
15130 * If there are no errors, add the function
15131 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015132 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015133 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015134 v = find_var(name, NULL);
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015136 {
15137 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
15138 goto erret;
15139 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015141 fp = find_func(name);
15142 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015144 if (!eap->forceit)
15145 {
15146 EMSG2(_(e_funcexts), name);
15147 goto erret;
15148 }
15149 if (fp->calls > 0)
15150 {
15151 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
15152 name);
15153 goto erret;
15154 }
15155 /* redefine existing function */
15156 ga_clear_strings(&(fp->args));
15157 ga_clear_strings(&(fp->lines));
15158 vim_free(name);
15159 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 }
15162 else
15163 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015164 char numbuf[20];
15165
15166 fp = NULL;
15167 if (fudi.fd_newkey == NULL && !eap->forceit)
15168 {
15169 EMSG(_(e_funcdict));
15170 goto erret;
15171 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015172 if (fudi.fd_di == NULL)
15173 {
15174 /* Can't add a function to a locked dictionary */
15175 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15176 goto erret;
15177 }
15178 /* Can't change an existing function if it is locked */
15179 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15180 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015181
15182 /* Give the function a sequential number. Can only be used with a
15183 * Funcref! */
15184 vim_free(name);
15185 sprintf(numbuf, "%d", ++func_nr);
15186 name = vim_strsave((char_u *)numbuf);
15187 if (name == NULL)
15188 goto erret;
15189 }
15190
15191 if (fp == NULL)
15192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
15194 if (fp == NULL)
15195 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015196
15197 if (fudi.fd_dict != NULL)
15198 {
15199 if (fudi.fd_di == NULL)
15200 {
15201 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015202 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015203 if (fudi.fd_di == NULL)
15204 {
15205 vim_free(fp);
15206 goto erret;
15207 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015208 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15209 {
15210 vim_free(fudi.fd_di);
15211 goto erret;
15212 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015213 }
15214 else
15215 /* overwrite existing dict entry */
15216 clear_tv(&fudi.fd_di->di_tv);
15217 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015218 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015219 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
15220 fp->refcount = 1;
15221 }
15222
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015224 fp->name = name;
15225 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 fp->next = firstfunc;
15227 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 }
15229 fp->args = newargs;
15230 fp->lines = newlines;
15231 fp->varargs = varargs;
15232 fp->flags = flags;
15233 fp->calls = 0;
15234 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015235 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236
15237erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 ga_clear_strings(&newargs);
15239 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015240ret_free:
15241 vim_free(skip_until);
15242 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245}
15246
15247/*
15248 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015249 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015251 * flags:
15252 * TFN_INT: internal function name OK
15253 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 * Advances "pp" to just after the function name (if no error).
15255 */
15256 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015257trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 char_u **pp;
15259 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015260 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015261 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015263 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 char_u *start;
15265 char_u *end;
15266 int lead;
15267 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015269 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015270
15271 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015274
15275 /* Check for hard coded <SNR>: already translated function ID (from a user
15276 * command). */
15277 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15278 && (*pp)[2] == (int)KE_SNR)
15279 {
15280 *pp += 3;
15281 len = get_id_len(pp) + 3;
15282 return vim_strnsave(start, len);
15283 }
15284
15285 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15286 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015288 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015290
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015291 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015292 if (end == start)
15293 {
15294 if (!skip)
15295 EMSG(_("E129: Function name required"));
15296 goto theend;
15297 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015298 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015299 {
15300 /*
15301 * Report an invalid expression in braces, unless the expression
15302 * evaluation has been cancelled due to an aborting error, an
15303 * interrupt, or an exception.
15304 */
15305 if (!aborting())
15306 {
15307 if (end != NULL)
15308 EMSG2(_(e_invarg2), start);
15309 }
15310 else
15311 *pp = find_name_end(start, NULL, NULL, TRUE);
15312 goto theend;
15313 }
15314
15315 if (lv.ll_tv != NULL)
15316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015317 if (fdp != NULL)
15318 {
15319 fdp->fd_dict = lv.ll_dict;
15320 fdp->fd_newkey = lv.ll_newkey;
15321 lv.ll_newkey = NULL;
15322 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015324 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15325 {
15326 name = vim_strsave(lv.ll_tv->vval.v_string);
15327 *pp = end;
15328 }
15329 else
15330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015331 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15332 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015333 EMSG(_(e_funcref));
15334 else
15335 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015336 name = NULL;
15337 }
15338 goto theend;
15339 }
15340
15341 if (lv.ll_name == NULL)
15342 {
15343 /* Error found, but continue after the function name. */
15344 *pp = end;
15345 goto theend;
15346 }
15347
15348 if (lv.ll_exp_name != NULL)
15349 len = STRLEN(lv.ll_exp_name);
15350 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015351 {
15352 if (lead == 2) /* skip over "s:" */
15353 lv.ll_name += 2;
15354 len = (int)(end - lv.ll_name);
15355 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015356
15357 /*
15358 * Copy the function name to allocated memory.
15359 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15360 * Accept <SNR>123_name() outside a script.
15361 */
15362 if (skip)
15363 lead = 0; /* do nothing */
15364 else if (lead > 0)
15365 {
15366 lead = 3;
15367 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
15368 {
15369 if (current_SID <= 0)
15370 {
15371 EMSG(_(e_usingsid));
15372 goto theend;
15373 }
15374 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
15375 lead += (int)STRLEN(sid_buf);
15376 }
15377 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015378 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015379 {
15380 EMSG2(_("E128: Function name must start with a capital: %s"),
15381 lv.ll_name);
15382 goto theend;
15383 }
15384 name = alloc((unsigned)(len + lead + 1));
15385 if (name != NULL)
15386 {
15387 if (lead > 0)
15388 {
15389 name[0] = K_SPECIAL;
15390 name[1] = KS_EXTRA;
15391 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000015392 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015393 STRCPY(name + 3, sid_buf);
15394 }
15395 mch_memmove(name + lead, lv.ll_name, (size_t)len);
15396 name[len + lead] = NUL;
15397 }
15398 *pp = end;
15399
15400theend:
15401 clear_lval(&lv);
15402 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403}
15404
15405/*
15406 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
15407 * Return 2 if "p" starts with "s:".
15408 * Return 0 otherwise.
15409 */
15410 static int
15411eval_fname_script(p)
15412 char_u *p;
15413{
15414 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
15415 || STRNICMP(p + 1, "SNR>", 4) == 0))
15416 return 5;
15417 if (p[0] == 's' && p[1] == ':')
15418 return 2;
15419 return 0;
15420}
15421
15422/*
15423 * Return TRUE if "p" starts with "<SID>" or "s:".
15424 * Only works if eval_fname_script() returned non-zero for "p"!
15425 */
15426 static int
15427eval_fname_sid(p)
15428 char_u *p;
15429{
15430 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
15431}
15432
15433/*
15434 * List the head of the function: "name(arg1, arg2)".
15435 */
15436 static void
15437list_func_head(fp, indent)
15438 ufunc_T *fp;
15439 int indent;
15440{
15441 int j;
15442
15443 msg_start();
15444 if (indent)
15445 MSG_PUTS(" ");
15446 MSG_PUTS("function ");
15447 if (fp->name[0] == K_SPECIAL)
15448 {
15449 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
15450 msg_puts(fp->name + 3);
15451 }
15452 else
15453 msg_puts(fp->name);
15454 msg_putchar('(');
15455 for (j = 0; j < fp->args.ga_len; ++j)
15456 {
15457 if (j)
15458 MSG_PUTS(", ");
15459 msg_puts(FUNCARG(fp, j));
15460 }
15461 if (fp->varargs)
15462 {
15463 if (j)
15464 MSG_PUTS(", ");
15465 MSG_PUTS("...");
15466 }
15467 msg_putchar(')');
15468}
15469
15470/*
15471 * Find a function by name, return pointer to it in ufuncs.
15472 * Return NULL for unknown function.
15473 */
15474 static ufunc_T *
15475find_func(name)
15476 char_u *name;
15477{
15478 ufunc_T *fp;
15479
15480 for (fp = firstfunc; fp != NULL; fp = fp->next)
15481 if (STRCMP(name, fp->name) == 0)
15482 break;
15483 return fp;
15484}
15485
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015486/*
15487 * Return TRUE if a function "name" exists.
15488 */
15489 static int
15490function_exists(name)
15491 char_u *name;
15492{
15493 char_u *p = name;
15494 int n = FALSE;
15495
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015496 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015497 if (p != NULL)
15498 {
15499 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
15500 n = (find_func(p) != NULL);
15501 else if (ASCII_ISLOWER(*p))
15502 n = (find_internal_func(p) >= 0);
15503 vim_free(p);
15504 }
15505 return n;
15506}
15507
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
15509
15510/*
15511 * Function given to ExpandGeneric() to obtain the list of user defined
15512 * function names.
15513 */
15514 char_u *
15515get_user_func_name(xp, idx)
15516 expand_T *xp;
15517 int idx;
15518{
15519 static ufunc_T *fp = NULL;
15520
15521 if (idx == 0)
15522 fp = firstfunc;
15523 if (fp != NULL)
15524 {
15525 if (STRLEN(fp->name) + 4 >= IOSIZE)
15526 return fp->name; /* prevents overflow */
15527
15528 cat_func_name(IObuff, fp);
15529 if (xp->xp_context != EXPAND_USER_FUNC)
15530 {
15531 STRCAT(IObuff, "(");
15532 if (!fp->varargs && fp->args.ga_len == 0)
15533 STRCAT(IObuff, ")");
15534 }
15535
15536 fp = fp->next;
15537 return IObuff;
15538 }
15539 return NULL;
15540}
15541
15542#endif /* FEAT_CMDL_COMPL */
15543
15544/*
15545 * Copy the function name of "fp" to buffer "buf".
15546 * "buf" must be able to hold the function name plus three bytes.
15547 * Takes care of script-local function names.
15548 */
15549 static void
15550cat_func_name(buf, fp)
15551 char_u *buf;
15552 ufunc_T *fp;
15553{
15554 if (fp->name[0] == K_SPECIAL)
15555 {
15556 STRCPY(buf, "<SNR>");
15557 STRCAT(buf, fp->name + 3);
15558 }
15559 else
15560 STRCPY(buf, fp->name);
15561}
15562
15563/*
15564 * ":delfunction {name}"
15565 */
15566 void
15567ex_delfunction(eap)
15568 exarg_T *eap;
15569{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015570 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 char_u *p;
15572 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015573 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574
15575 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015576 name = trans_function_name(&p, eap->skip, 0, &fudi);
15577 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015579 {
15580 if (fudi.fd_dict != NULL && !eap->skip)
15581 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 if (!ends_excmd(*skipwhite(p)))
15585 {
15586 vim_free(name);
15587 EMSG(_(e_trailing));
15588 return;
15589 }
15590 eap->nextcmd = check_nextcmd(p);
15591 if (eap->nextcmd != NULL)
15592 *p = NUL;
15593
15594 if (!eap->skip)
15595 fp = find_func(name);
15596 vim_free(name);
15597
15598 if (!eap->skip)
15599 {
15600 if (fp == NULL)
15601 {
15602 EMSG2(_("E130: Undefined function: %s"), eap->arg);
15603 return;
15604 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015605 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 {
15607 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
15608 return;
15609 }
15610
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015611 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015613 /* Delete the dict item that refers to the function, it will
15614 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015615 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015617 else
15618 func_free(fp);
15619 }
15620}
15621
15622/*
15623 * Free a function and remove it from the list of functions.
15624 */
15625 static void
15626func_free(fp)
15627 ufunc_T *fp;
15628{
15629 ufunc_T *pfp;
15630
15631 /* clear this function */
15632 vim_free(fp->name);
15633 ga_clear_strings(&(fp->args));
15634 ga_clear_strings(&(fp->lines));
15635
15636 /* remove the function from the function list */
15637 if (firstfunc == fp)
15638 firstfunc = fp->next;
15639 else
15640 {
15641 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
15642 if (pfp->next == fp)
15643 {
15644 pfp->next = fp->next;
15645 break;
15646 }
15647 }
15648 vim_free(fp);
15649}
15650
15651/*
15652 * Unreference a Function: decrement the reference count and free it when it
15653 * becomes zero. Only for numbered functions.
15654 */
15655 static void
15656func_unref(name)
15657 char_u *name;
15658{
15659 ufunc_T *fp;
15660
15661 if (name != NULL && isdigit(*name))
15662 {
15663 fp = find_func(name);
15664 if (fp == NULL)
15665 EMSG2(_(e_intern2), "func_unref()");
15666 else if (--fp->refcount <= 0)
15667 {
15668 /* Only delete it when it's not being used. Otherwise it's done
15669 * when "calls" becomes zero. */
15670 if (fp->calls == 0)
15671 func_free(fp);
15672 }
15673 }
15674}
15675
15676/*
15677 * Count a reference to a Function.
15678 */
15679 static void
15680func_ref(name)
15681 char_u *name;
15682{
15683 ufunc_T *fp;
15684
15685 if (name != NULL && isdigit(*name))
15686 {
15687 fp = find_func(name);
15688 if (fp == NULL)
15689 EMSG2(_(e_intern2), "func_ref()");
15690 else
15691 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 }
15693}
15694
15695/*
15696 * Call a user function.
15697 */
15698 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000015699call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 ufunc_T *fp; /* pointer to function */
15701 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000015702 typval_T *argvars; /* arguments */
15703 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 linenr_T firstline; /* first line of range */
15705 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000015706 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707{
Bram Moolenaar33570922005-01-25 22:26:29 +000015708 char_u *save_sourcing_name;
15709 linenr_T save_sourcing_lnum;
15710 scid_T save_current_SID;
15711 funccall_T fc;
15712 funccall_T *save_fcp = current_funccal;
15713 int save_did_emsg;
15714 static int depth = 0;
15715 dictitem_T *v;
15716 int fixvar_idx = 0; /* index in fixvar[] */
15717 int i;
15718 int ai;
15719 char_u numbuf[NUMBUFLEN];
15720 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721
15722 /* If depth of calling is getting too high, don't execute the function */
15723 if (depth >= p_mfd)
15724 {
15725 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015726 rettv->v_type = VAR_NUMBER;
15727 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 return;
15729 }
15730 ++depth;
15731
15732 line_breakcheck(); /* check for CTRL-C hit */
15733
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736 fc.rettv = rettv;
15737 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 fc.linenr = 0;
15739 fc.returned = FALSE;
15740 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 /* Check if this function has a breakpoint. */
15742 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
15743 fc.dbg_tick = debug_tick;
15744
Bram Moolenaar33570922005-01-25 22:26:29 +000015745 /*
15746 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
15747 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
15748 * each argument variable and saves a lot of time.
15749 */
15750 /*
15751 * Init l: variables.
15752 */
15753 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000015754 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015755 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 /* Set l:self to "selfdict". */
15757 v = &fc.fixvar[fixvar_idx++].var;
15758 STRCPY(v->di_key, "self");
15759 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
15760 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
15761 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015762 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015763 v->di_tv.vval.v_dict = selfdict;
15764 ++selfdict->dv_refcount;
15765 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015766
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 /*
15768 * Init a: variables.
15769 * Set a:0 to "argcount".
15770 * Set a:000 to a list with room for the "..." arguments.
15771 */
15772 init_var_dict(&fc.l_avars, &fc.l_avars_var);
15773 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
15774 (varnumber_T)(argcount - fp->args.ga_len));
15775 v = &fc.fixvar[fixvar_idx++].var;
15776 STRCPY(v->di_key, "000");
15777 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15778 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15779 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015780 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 v->di_tv.vval.v_list = &fc.l_varlist;
15782 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
15783 fc.l_varlist.lv_refcount = 99999;
15784
15785 /*
15786 * Set a:firstline to "firstline" and a:lastline to "lastline".
15787 * Set a:name to named arguments.
15788 * Set a:N to the "..." arguments.
15789 */
15790 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
15791 (varnumber_T)firstline);
15792 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
15793 (varnumber_T)lastline);
15794 for (i = 0; i < argcount; ++i)
15795 {
15796 ai = i - fp->args.ga_len;
15797 if (ai < 0)
15798 /* named argument a:name */
15799 name = FUNCARG(fp, i);
15800 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015801 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015802 /* "..." argument a:1, a:2, etc. */
15803 sprintf((char *)numbuf, "%d", ai + 1);
15804 name = numbuf;
15805 }
15806 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
15807 {
15808 v = &fc.fixvar[fixvar_idx++].var;
15809 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15810 }
15811 else
15812 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015813 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15814 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 if (v == NULL)
15816 break;
15817 v->di_flags = DI_FLAGS_RO;
15818 }
15819 STRCPY(v->di_key, name);
15820 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15821
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015822 /* Note: the values are copied directly to avoid alloc/free.
15823 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015824 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015825 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015826
15827 if (ai >= 0 && ai < MAX_FUNC_ARGS)
15828 {
15829 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
15830 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015831 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015832 }
15833 }
15834
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 /* Don't redraw while executing the function. */
15836 ++RedrawingDisabled;
15837 save_sourcing_name = sourcing_name;
15838 save_sourcing_lnum = sourcing_lnum;
15839 sourcing_lnum = 1;
15840 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
15841 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
15842 if (sourcing_name != NULL)
15843 {
15844 if (save_sourcing_name != NULL
15845 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
15846 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
15847 else
15848 STRCPY(sourcing_name, "function ");
15849 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
15850
15851 if (p_verbose >= 12)
15852 {
15853 ++no_wait_return;
15854 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15855 msg_str((char_u *)_("calling %s"), sourcing_name);
15856 if (p_verbose >= 14)
15857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000015859 char_u numbuf[NUMBUFLEN];
15860 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861
15862 msg_puts((char_u *)"(");
15863 for (i = 0; i < argcount; ++i)
15864 {
15865 if (i > 0)
15866 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015867 if (argvars[i].v_type == VAR_NUMBER)
15868 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 else
15870 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015871 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000015874 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 }
15876 }
15877 msg_puts((char_u *)")");
15878 }
15879 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15880 cmdline_row = msg_row;
15881 --no_wait_return;
15882 }
15883 }
15884 save_current_SID = current_SID;
15885 current_SID = fp->script_ID;
15886 save_did_emsg = did_emsg;
15887 did_emsg = FALSE;
15888
15889 /* call do_cmdline() to execute the lines */
15890 do_cmdline(NULL, get_func_line, (void *)&fc,
15891 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
15892
15893 --RedrawingDisabled;
15894
15895 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015898 clear_tv(rettv);
15899 rettv->v_type = VAR_NUMBER;
15900 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 }
15902
15903 /* when being verbose, mention the return value */
15904 if (p_verbose >= 12)
15905 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015906 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907
15908 ++no_wait_return;
15909 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15910
15911 /* Make sure the output fits in IObuff. */
15912 sn = sourcing_name;
15913 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
15914 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
15915
15916 if (aborting())
15917 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015918 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015920 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000015921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015923 char_u buf[MSG_BUF_LEN];
15924 char_u numbuf[NUMBUFLEN];
15925 char_u *tofree;
15926
15927 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
15928 buf, MSG_BUF_LEN);
15929 smsg((char_u *)_("%s returning %s"), sn, buf);
15930 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 }
15932 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15933 cmdline_row = msg_row;
15934 --no_wait_return;
15935 }
15936
15937 vim_free(sourcing_name);
15938 sourcing_name = save_sourcing_name;
15939 sourcing_lnum = save_sourcing_lnum;
15940 current_SID = save_current_SID;
15941
15942 if (p_verbose >= 12 && sourcing_name != NULL)
15943 {
15944 ++no_wait_return;
15945 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15946 msg_str((char_u *)_("continuing in %s"), sourcing_name);
15947 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15948 cmdline_row = msg_row;
15949 --no_wait_return;
15950 }
15951
15952 did_emsg |= save_did_emsg;
15953 current_funccal = save_fcp;
15954
Bram Moolenaar33570922005-01-25 22:26:29 +000015955 /* The a: variables typevals were not alloced, only free the allocated
15956 * variables. */
15957 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
15958
15959 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 --depth;
15961}
15962
15963/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 * Add a number variable "name" to dict "dp" with value "nr".
15965 */
15966 static void
15967add_nr_var(dp, v, name, nr)
15968 dict_T *dp;
15969 dictitem_T *v;
15970 char *name;
15971 varnumber_T nr;
15972{
15973 STRCPY(v->di_key, name);
15974 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15975 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
15976 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015977 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 v->di_tv.vval.v_number = nr;
15979}
15980
15981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 * ":return [expr]"
15983 */
15984 void
15985ex_return(eap)
15986 exarg_T *eap;
15987{
15988 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 int returning = FALSE;
15991
15992 if (current_funccal == NULL)
15993 {
15994 EMSG(_("E133: :return not inside a function"));
15995 return;
15996 }
15997
15998 if (eap->skip)
15999 ++emsg_skip;
16000
16001 eap->nextcmd = NULL;
16002 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 {
16005 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016006 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 }
16010 /* It's safer to return also on error. */
16011 else if (!eap->skip)
16012 {
16013 /*
16014 * Return unless the expression evaluation has been cancelled due to an
16015 * aborting error, an interrupt, or an exception.
16016 */
16017 if (!aborting())
16018 returning = do_return(eap, FALSE, TRUE, NULL);
16019 }
16020
16021 /* When skipping or the return gets pending, advance to the next command
16022 * in this line (!returning). Otherwise, ignore the rest of the line.
16023 * Following lines will be ignored by get_func_line(). */
16024 if (returning)
16025 eap->nextcmd = NULL;
16026 else if (eap->nextcmd == NULL) /* no argument */
16027 eap->nextcmd = check_nextcmd(arg);
16028
16029 if (eap->skip)
16030 --emsg_skip;
16031}
16032
16033/*
16034 * Return from a function. Possibly makes the return pending. Also called
16035 * for a pending return at the ":endtry" or after returning from an extra
16036 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016037 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016038 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 * FALSE when the return gets pending.
16040 */
16041 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016042do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 exarg_T *eap;
16044 int reanimate;
16045 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047{
16048 int idx;
16049 struct condstack *cstack = eap->cstack;
16050
16051 if (reanimate)
16052 /* Undo the return. */
16053 current_funccal->returned = FALSE;
16054
16055 /*
16056 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16057 * not in its finally clause (which then is to be executed next) is found.
16058 * In this case, make the ":return" pending for execution at the ":endtry".
16059 * Otherwise, return normally.
16060 */
16061 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16062 if (idx >= 0)
16063 {
16064 cstack->cs_pending[idx] = CSTP_RETURN;
16065
16066 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016067 /* A pending return again gets pending. "rettv" points to an
16068 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 else
16072 {
16073 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016076 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016078 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 {
16080 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 else
16084 EMSG(_(e_outofmem));
16085 }
16086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016087 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088
16089 if (reanimate)
16090 {
16091 /* The pending return value could be overwritten by a ":return"
16092 * without argument in a finally clause; reset the default
16093 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016094 current_funccal->rettv->v_type = VAR_NUMBER;
16095 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 }
16097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016098 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 }
16100 else
16101 {
16102 current_funccal->returned = TRUE;
16103
16104 /* If the return is carried out now, store the return value. For
16105 * a return immediately after reanimation, the value is already
16106 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016107 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 }
16114 }
16115
16116 return idx < 0;
16117}
16118
16119/*
16120 * Free the variable with a pending return value.
16121 */
16122 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016123discard_pending_return(rettv)
16124 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125{
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127}
16128
16129/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016130 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 * is an allocated string. Used by report_pending() for verbose messages.
16132 */
16133 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016134get_return_cmd(rettv)
16135 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016137 char_u *s;
16138 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016139 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 if (rettv == NULL)
16142 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 else
Bram Moolenaar33570922005-01-25 22:26:29 +000016144 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145
16146 STRCPY(IObuff, ":return ");
16147 STRNCPY(IObuff + 8, s, IOSIZE - 8);
16148 if (STRLEN(s) + 8 >= IOSIZE)
16149 STRCPY(IObuff + IOSIZE - 4, "...");
16150 vim_free(tofree);
16151 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152}
16153
16154/*
16155 * Get next function line.
16156 * Called by do_cmdline() to get the next line.
16157 * Returns allocated string, or NULL for end of function.
16158 */
16159/* ARGSUSED */
16160 char_u *
16161get_func_line(c, cookie, indent)
16162 int c; /* not used */
16163 void *cookie;
16164 int indent; /* not used */
16165{
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 char_u *retval;
16168 garray_T *gap; /* growarray with function lines */
16169
16170 /* If breakpoints have been added/deleted need to check for it. */
16171 if (fcp->dbg_tick != debug_tick)
16172 {
16173 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
16174 sourcing_lnum);
16175 fcp->dbg_tick = debug_tick;
16176 }
16177
16178 gap = &fcp->func->lines;
16179 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
16180 retval = NULL;
16181 else if (fcp->returned || fcp->linenr >= gap->ga_len)
16182 retval = NULL;
16183 else
16184 {
16185 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
16186 sourcing_lnum = fcp->linenr;
16187 }
16188
16189 /* Did we encounter a breakpoint? */
16190 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
16191 {
16192 dbg_breakpoint(fcp->func->name, sourcing_lnum);
16193 /* Find next breakpoint. */
16194 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
16195 sourcing_lnum);
16196 fcp->dbg_tick = debug_tick;
16197 }
16198
16199 return retval;
16200}
16201
16202/*
16203 * Return TRUE if the currently active function should be ended, because a
16204 * return was encountered or an error occured. Used inside a ":while".
16205 */
16206 int
16207func_has_ended(cookie)
16208 void *cookie;
16209{
Bram Moolenaar33570922005-01-25 22:26:29 +000016210 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211
16212 /* Ignore the "abort" flag if the abortion behavior has been changed due to
16213 * an error inside a try conditional. */
16214 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
16215 || fcp->returned);
16216}
16217
16218/*
16219 * return TRUE if cookie indicates a function which "abort"s on errors.
16220 */
16221 int
16222func_has_abort(cookie)
16223 void *cookie;
16224{
Bram Moolenaar33570922005-01-25 22:26:29 +000016225 return ((funccall_T *)cookie)->func->flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226}
16227
16228#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
16229typedef enum
16230{
16231 VAR_FLAVOUR_DEFAULT,
16232 VAR_FLAVOUR_SESSION,
16233 VAR_FLAVOUR_VIMINFO
16234} var_flavour_T;
16235
16236static var_flavour_T var_flavour __ARGS((char_u *varname));
16237
16238 static var_flavour_T
16239var_flavour(varname)
16240 char_u *varname;
16241{
16242 char_u *p = varname;
16243
16244 if (ASCII_ISUPPER(*p))
16245 {
16246 while (*(++p))
16247 if (ASCII_ISLOWER(*p))
16248 return VAR_FLAVOUR_SESSION;
16249 return VAR_FLAVOUR_VIMINFO;
16250 }
16251 else
16252 return VAR_FLAVOUR_DEFAULT;
16253}
16254#endif
16255
16256#if defined(FEAT_VIMINFO) || defined(PROTO)
16257/*
16258 * Restore global vars that start with a capital from the viminfo file
16259 */
16260 int
16261read_viminfo_varlist(virp, writing)
16262 vir_T *virp;
16263 int writing;
16264{
16265 char_u *tab;
16266 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268
16269 if (!writing && (find_viminfo_parameter('!') != NULL))
16270 {
16271 tab = vim_strchr(virp->vir_line + 1, '\t');
16272 if (tab != NULL)
16273 {
16274 *tab++ = '\0'; /* isolate the variable name */
16275 if (*tab == 'S') /* string var */
16276 is_string = TRUE;
16277
16278 tab = vim_strchr(tab, '\t');
16279 if (tab != NULL)
16280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 if (is_string)
16282 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016283 tv.v_type = VAR_STRING;
16284 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 }
16287 else
16288 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016289 tv.v_type = VAR_NUMBER;
16290 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016292 set_var(virp->vir_line + 1, &tv, FALSE);
16293 if (is_string)
16294 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 }
16296 }
16297 }
16298
16299 return viminfo_readline(virp);
16300}
16301
16302/*
16303 * Write global vars that start with a capital to the viminfo file
16304 */
16305 void
16306write_viminfo_varlist(fp)
16307 FILE *fp;
16308{
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 hashitem_T *hi;
16310 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000016311 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016312 char *s;
16313 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016314 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315
16316 if (find_viminfo_parameter('!') == NULL)
16317 return;
16318
16319 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000016320
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 todo = globvarht.ht_used;
16322 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016324 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016326 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 this_var = HI2DI(hi);
16328 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016329 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000016331 {
16332 case VAR_STRING: s = "STR"; break;
16333 case VAR_NUMBER: s = "NUM"; break;
16334 default: continue;
16335 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
16337 viminfo_writestring(fp, echo_string(&this_var->di_tv,
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016338 &tofree, numbuf));
Bram Moolenaara7043832005-01-21 11:56:39 +000016339 vim_free(tofree);
16340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 }
16342 }
16343}
16344#endif
16345
16346#if defined(FEAT_SESSION) || defined(PROTO)
16347 int
16348store_session_globals(fd)
16349 FILE *fd;
16350{
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 hashitem_T *hi;
16352 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000016353 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 char_u *p, *t;
16355
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 todo = globvarht.ht_used;
16357 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016359 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016361 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 this_var = HI2DI(hi);
16363 if ((this_var->di_tv.v_type == VAR_NUMBER
16364 || this_var->di_tv.v_type == VAR_STRING)
16365 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016366 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016367 /* Escape special characters with a backslash. Turn a LF and
16368 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000016370 (char_u *)"\\\"\n\r");
16371 if (p == NULL) /* out of memory */
16372 break;
16373 for (t = p; *t != NUL; ++t)
16374 if (*t == '\n')
16375 *t = 'n';
16376 else if (*t == '\r')
16377 *t = 'r';
16378 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000016379 this_var->di_key,
16380 (this_var->di_tv.v_type == VAR_STRING) ? '"'
16381 : ' ',
16382 p,
16383 (this_var->di_tv.v_type == VAR_STRING) ? '"'
16384 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000016385 || put_eol(fd) == FAIL)
16386 {
16387 vim_free(p);
16388 return FAIL;
16389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 vim_free(p);
16391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 }
16393 }
16394 return OK;
16395}
16396#endif
16397
16398#endif /* FEAT_EVAL */
16399
16400#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
16401
16402
16403#ifdef WIN3264
16404/*
16405 * Functions for ":8" filename modifier: get 8.3 version of a filename.
16406 */
16407static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
16408static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
16409static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
16410
16411/*
16412 * Get the short pathname of a file.
16413 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
16414 */
16415 static int
16416get_short_pathname(fnamep, bufp, fnamelen)
16417 char_u **fnamep;
16418 char_u **bufp;
16419 int *fnamelen;
16420{
16421 int l,len;
16422 char_u *newbuf;
16423
16424 len = *fnamelen;
16425
16426 l = GetShortPathName(*fnamep, *fnamep, len);
16427 if (l > len - 1)
16428 {
16429 /* If that doesn't work (not enough space), then save the string
16430 * and try again with a new buffer big enough
16431 */
16432 newbuf = vim_strnsave(*fnamep, l);
16433 if (newbuf == NULL)
16434 return 0;
16435
16436 vim_free(*bufp);
16437 *fnamep = *bufp = newbuf;
16438
16439 l = GetShortPathName(*fnamep,*fnamep,l+1);
16440
16441 /* Really should always succeed, as the buffer is big enough */
16442 }
16443
16444 *fnamelen = l;
16445 return 1;
16446}
16447
16448/*
16449 * Create a short path name. Returns the length of the buffer it needs.
16450 * Doesn't copy over the end of the buffer passed in.
16451 */
16452 static int
16453shortpath_for_invalid_fname(fname, bufp, fnamelen)
16454 char_u **fname;
16455 char_u **bufp;
16456 int *fnamelen;
16457{
16458 char_u *s, *p, *pbuf2, *pbuf3;
16459 char_u ch;
16460 int l,len,len2,plen,slen;
16461
16462 /* Make a copy */
16463 len2 = *fnamelen;
16464 pbuf2 = vim_strnsave(*fname, len2);
16465 pbuf3 = NULL;
16466
16467 s = pbuf2 + len2 - 1; /* Find the end */
16468 slen = 1;
16469 plen = len2;
16470
16471 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016472 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 {
16474 --s;
16475 ++slen;
16476 --plen;
16477 }
16478
16479 do
16480 {
16481 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016482 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 {
16484 --s;
16485 ++slen;
16486 --plen;
16487 }
16488 if (s <= pbuf2)
16489 break;
16490
16491 /* Remeber the character that is about to be blatted */
16492 ch = *s;
16493 *s = 0; /* get_short_pathname requires a null-terminated string */
16494
16495 /* Try it in situ */
16496 p = pbuf2;
16497 if (!get_short_pathname(&p, &pbuf3, &plen))
16498 {
16499 vim_free(pbuf2);
16500 return -1;
16501 }
16502 *s = ch; /* Preserve the string */
16503 } while (plen == 0);
16504
16505 if (plen > 0)
16506 {
16507 /* Remeber the length of the new string. */
16508 *fnamelen = len = plen + slen;
16509 vim_free(*bufp);
16510 if (len > len2)
16511 {
16512 /* If there's not enough space in the currently allocated string,
16513 * then copy it to a buffer big enough.
16514 */
16515 *fname= *bufp = vim_strnsave(p, len);
16516 if (*fname == NULL)
16517 return -1;
16518 }
16519 else
16520 {
16521 /* Transfer pbuf2 to being the main buffer (it's big enough) */
16522 *fname = *bufp = pbuf2;
16523 if (p != pbuf2)
16524 strncpy(*fname, p, plen);
16525 pbuf2 = NULL;
16526 }
16527 /* Concat the next bit */
16528 strncpy(*fname + plen, s, slen);
16529 (*fname)[len] = '\0';
16530 }
16531 vim_free(pbuf3);
16532 vim_free(pbuf2);
16533 return 0;
16534}
16535
16536/*
16537 * Get a pathname for a partial path.
16538 */
16539 static int
16540shortpath_for_partial(fnamep, bufp, fnamelen)
16541 char_u **fnamep;
16542 char_u **bufp;
16543 int *fnamelen;
16544{
16545 int sepcount, len, tflen;
16546 char_u *p;
16547 char_u *pbuf, *tfname;
16548 int hasTilde;
16549
16550 /* Count up the path seperators from the RHS.. so we know which part
16551 * of the path to return.
16552 */
16553 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016554 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 if (vim_ispathsep(*p))
16556 ++sepcount;
16557
16558 /* Need full path first (use expand_env() to remove a "~/") */
16559 hasTilde = (**fnamep == '~');
16560 if (hasTilde)
16561 pbuf = tfname = expand_env_save(*fnamep);
16562 else
16563 pbuf = tfname = FullName_save(*fnamep, FALSE);
16564
16565 len = tflen = STRLEN(tfname);
16566
16567 if (!get_short_pathname(&tfname, &pbuf, &len))
16568 return -1;
16569
16570 if (len == 0)
16571 {
16572 /* Don't have a valid filename, so shorten the rest of the
16573 * path if we can. This CAN give us invalid 8.3 filenames, but
16574 * there's not a lot of point in guessing what it might be.
16575 */
16576 len = tflen;
16577 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
16578 return -1;
16579 }
16580
16581 /* Count the paths backward to find the beginning of the desired string. */
16582 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016583 {
16584#ifdef FEAT_MBYTE
16585 if (has_mbyte)
16586 p -= mb_head_off(tfname, p);
16587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 if (vim_ispathsep(*p))
16589 {
16590 if (sepcount == 0 || (hasTilde && sepcount == 1))
16591 break;
16592 else
16593 sepcount --;
16594 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 if (hasTilde)
16597 {
16598 --p;
16599 if (p >= tfname)
16600 *p = '~';
16601 else
16602 return -1;
16603 }
16604 else
16605 ++p;
16606
16607 /* Copy in the string - p indexes into tfname - allocated at pbuf */
16608 vim_free(*bufp);
16609 *fnamelen = (int)STRLEN(p);
16610 *bufp = pbuf;
16611 *fnamep = p;
16612
16613 return 0;
16614}
16615#endif /* WIN3264 */
16616
16617/*
16618 * Adjust a filename, according to a string of modifiers.
16619 * *fnamep must be NUL terminated when called. When returning, the length is
16620 * determined by *fnamelen.
16621 * Returns valid flags.
16622 * When there is an error, *fnamep is set to NULL.
16623 */
16624 int
16625modify_fname(src, usedlen, fnamep, bufp, fnamelen)
16626 char_u *src; /* string with modifiers */
16627 int *usedlen; /* characters after src that are used */
16628 char_u **fnamep; /* file name so far */
16629 char_u **bufp; /* buffer for allocated file name or NULL */
16630 int *fnamelen; /* length of fnamep */
16631{
16632 int valid = 0;
16633 char_u *tail;
16634 char_u *s, *p, *pbuf;
16635 char_u dirname[MAXPATHL];
16636 int c;
16637 int has_fullname = 0;
16638#ifdef WIN3264
16639 int has_shortname = 0;
16640#endif
16641
16642repeat:
16643 /* ":p" - full path/file_name */
16644 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
16645 {
16646 has_fullname = 1;
16647
16648 valid |= VALID_PATH;
16649 *usedlen += 2;
16650
16651 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
16652 if ((*fnamep)[0] == '~'
16653#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
16654 && ((*fnamep)[1] == '/'
16655# ifdef BACKSLASH_IN_FILENAME
16656 || (*fnamep)[1] == '\\'
16657# endif
16658 || (*fnamep)[1] == NUL)
16659
16660#endif
16661 )
16662 {
16663 *fnamep = expand_env_save(*fnamep);
16664 vim_free(*bufp); /* free any allocated file name */
16665 *bufp = *fnamep;
16666 if (*fnamep == NULL)
16667 return -1;
16668 }
16669
16670 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016671 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 {
16673 if (vim_ispathsep(*p)
16674 && p[1] == '.'
16675 && (p[2] == NUL
16676 || vim_ispathsep(p[2])
16677 || (p[2] == '.'
16678 && (p[3] == NUL || vim_ispathsep(p[3])))))
16679 break;
16680 }
16681
16682 /* FullName_save() is slow, don't use it when not needed. */
16683 if (*p != NUL || !vim_isAbsName(*fnamep))
16684 {
16685 *fnamep = FullName_save(*fnamep, *p != NUL);
16686 vim_free(*bufp); /* free any allocated file name */
16687 *bufp = *fnamep;
16688 if (*fnamep == NULL)
16689 return -1;
16690 }
16691
16692 /* Append a path separator to a directory. */
16693 if (mch_isdir(*fnamep))
16694 {
16695 /* Make room for one or two extra characters. */
16696 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
16697 vim_free(*bufp); /* free any allocated file name */
16698 *bufp = *fnamep;
16699 if (*fnamep == NULL)
16700 return -1;
16701 add_pathsep(*fnamep);
16702 }
16703 }
16704
16705 /* ":." - path relative to the current directory */
16706 /* ":~" - path relative to the home directory */
16707 /* ":8" - shortname path - postponed till after */
16708 while (src[*usedlen] == ':'
16709 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
16710 {
16711 *usedlen += 2;
16712 if (c == '8')
16713 {
16714#ifdef WIN3264
16715 has_shortname = 1; /* Postpone this. */
16716#endif
16717 continue;
16718 }
16719 pbuf = NULL;
16720 /* Need full path first (use expand_env() to remove a "~/") */
16721 if (!has_fullname)
16722 {
16723 if (c == '.' && **fnamep == '~')
16724 p = pbuf = expand_env_save(*fnamep);
16725 else
16726 p = pbuf = FullName_save(*fnamep, FALSE);
16727 }
16728 else
16729 p = *fnamep;
16730
16731 has_fullname = 0;
16732
16733 if (p != NULL)
16734 {
16735 if (c == '.')
16736 {
16737 mch_dirname(dirname, MAXPATHL);
16738 s = shorten_fname(p, dirname);
16739 if (s != NULL)
16740 {
16741 *fnamep = s;
16742 if (pbuf != NULL)
16743 {
16744 vim_free(*bufp); /* free any allocated file name */
16745 *bufp = pbuf;
16746 pbuf = NULL;
16747 }
16748 }
16749 }
16750 else
16751 {
16752 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
16753 /* Only replace it when it starts with '~' */
16754 if (*dirname == '~')
16755 {
16756 s = vim_strsave(dirname);
16757 if (s != NULL)
16758 {
16759 *fnamep = s;
16760 vim_free(*bufp);
16761 *bufp = s;
16762 }
16763 }
16764 }
16765 vim_free(pbuf);
16766 }
16767 }
16768
16769 tail = gettail(*fnamep);
16770 *fnamelen = (int)STRLEN(*fnamep);
16771
16772 /* ":h" - head, remove "/file_name", can be repeated */
16773 /* Don't remove the first "/" or "c:\" */
16774 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
16775 {
16776 valid |= VALID_HEAD;
16777 *usedlen += 2;
16778 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016779 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 --tail;
16781 *fnamelen = (int)(tail - *fnamep);
16782#ifdef VMS
16783 if (*fnamelen > 0)
16784 *fnamelen += 1; /* the path separator is part of the path */
16785#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016786 while (tail > s && !after_pathsep(s, tail))
16787 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 }
16789
16790 /* ":8" - shortname */
16791 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
16792 {
16793 *usedlen += 2;
16794#ifdef WIN3264
16795 has_shortname = 1;
16796#endif
16797 }
16798
16799#ifdef WIN3264
16800 /* Check shortname after we have done 'heads' and before we do 'tails'
16801 */
16802 if (has_shortname)
16803 {
16804 pbuf = NULL;
16805 /* Copy the string if it is shortened by :h */
16806 if (*fnamelen < (int)STRLEN(*fnamep))
16807 {
16808 p = vim_strnsave(*fnamep, *fnamelen);
16809 if (p == 0)
16810 return -1;
16811 vim_free(*bufp);
16812 *bufp = *fnamep = p;
16813 }
16814
16815 /* Split into two implementations - makes it easier. First is where
16816 * there isn't a full name already, second is where there is.
16817 */
16818 if (!has_fullname && !vim_isAbsName(*fnamep))
16819 {
16820 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
16821 return -1;
16822 }
16823 else
16824 {
16825 int l;
16826
16827 /* Simple case, already have the full-name
16828 * Nearly always shorter, so try first time. */
16829 l = *fnamelen;
16830 if (!get_short_pathname(fnamep, bufp, &l))
16831 return -1;
16832
16833 if (l == 0)
16834 {
16835 /* Couldn't find the filename.. search the paths.
16836 */
16837 l = *fnamelen;
16838 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
16839 return -1;
16840 }
16841 *fnamelen = l;
16842 }
16843 }
16844#endif /* WIN3264 */
16845
16846 /* ":t" - tail, just the basename */
16847 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
16848 {
16849 *usedlen += 2;
16850 *fnamelen -= (int)(tail - *fnamep);
16851 *fnamep = tail;
16852 }
16853
16854 /* ":e" - extension, can be repeated */
16855 /* ":r" - root, without extension, can be repeated */
16856 while (src[*usedlen] == ':'
16857 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
16858 {
16859 /* find a '.' in the tail:
16860 * - for second :e: before the current fname
16861 * - otherwise: The last '.'
16862 */
16863 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
16864 s = *fnamep - 2;
16865 else
16866 s = *fnamep + *fnamelen - 1;
16867 for ( ; s > tail; --s)
16868 if (s[0] == '.')
16869 break;
16870 if (src[*usedlen + 1] == 'e') /* :e */
16871 {
16872 if (s > tail)
16873 {
16874 *fnamelen += (int)(*fnamep - (s + 1));
16875 *fnamep = s + 1;
16876#ifdef VMS
16877 /* cut version from the extension */
16878 s = *fnamep + *fnamelen - 1;
16879 for ( ; s > *fnamep; --s)
16880 if (s[0] == ';')
16881 break;
16882 if (s > *fnamep)
16883 *fnamelen = s - *fnamep;
16884#endif
16885 }
16886 else if (*fnamep <= tail)
16887 *fnamelen = 0;
16888 }
16889 else /* :r */
16890 {
16891 if (s > tail) /* remove one extension */
16892 *fnamelen = (int)(s - *fnamep);
16893 }
16894 *usedlen += 2;
16895 }
16896
16897 /* ":s?pat?foo?" - substitute */
16898 /* ":gs?pat?foo?" - global substitute */
16899 if (src[*usedlen] == ':'
16900 && (src[*usedlen + 1] == 's'
16901 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
16902 {
16903 char_u *str;
16904 char_u *pat;
16905 char_u *sub;
16906 int sep;
16907 char_u *flags;
16908 int didit = FALSE;
16909
16910 flags = (char_u *)"";
16911 s = src + *usedlen + 2;
16912 if (src[*usedlen + 1] == 'g')
16913 {
16914 flags = (char_u *)"g";
16915 ++s;
16916 }
16917
16918 sep = *s++;
16919 if (sep)
16920 {
16921 /* find end of pattern */
16922 p = vim_strchr(s, sep);
16923 if (p != NULL)
16924 {
16925 pat = vim_strnsave(s, (int)(p - s));
16926 if (pat != NULL)
16927 {
16928 s = p + 1;
16929 /* find end of substitution */
16930 p = vim_strchr(s, sep);
16931 if (p != NULL)
16932 {
16933 sub = vim_strnsave(s, (int)(p - s));
16934 str = vim_strnsave(*fnamep, *fnamelen);
16935 if (sub != NULL && str != NULL)
16936 {
16937 *usedlen = (int)(p + 1 - src);
16938 s = do_string_sub(str, pat, sub, flags);
16939 if (s != NULL)
16940 {
16941 *fnamep = s;
16942 *fnamelen = (int)STRLEN(s);
16943 vim_free(*bufp);
16944 *bufp = s;
16945 didit = TRUE;
16946 }
16947 }
16948 vim_free(sub);
16949 vim_free(str);
16950 }
16951 vim_free(pat);
16952 }
16953 }
16954 /* after using ":s", repeat all the modifiers */
16955 if (didit)
16956 goto repeat;
16957 }
16958 }
16959
16960 return valid;
16961}
16962
16963/*
16964 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
16965 * "flags" can be "g" to do a global substitute.
16966 * Returns an allocated string, NULL for error.
16967 */
16968 char_u *
16969do_string_sub(str, pat, sub, flags)
16970 char_u *str;
16971 char_u *pat;
16972 char_u *sub;
16973 char_u *flags;
16974{
16975 int sublen;
16976 regmatch_T regmatch;
16977 int i;
16978 int do_all;
16979 char_u *tail;
16980 garray_T ga;
16981 char_u *ret;
16982 char_u *save_cpo;
16983
16984 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
16985 save_cpo = p_cpo;
16986 p_cpo = (char_u *)"";
16987
16988 ga_init2(&ga, 1, 200);
16989
16990 do_all = (flags[0] == 'g');
16991
16992 regmatch.rm_ic = p_ic;
16993 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16994 if (regmatch.regprog != NULL)
16995 {
16996 tail = str;
16997 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
16998 {
16999 /*
17000 * Get some space for a temporary buffer to do the substitution
17001 * into. It will contain:
17002 * - The text up to where the match is.
17003 * - The substituted text.
17004 * - The text after the match.
17005 */
17006 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
17007 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
17008 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
17009 {
17010 ga_clear(&ga);
17011 break;
17012 }
17013
17014 /* copy the text up to where the match is */
17015 i = (int)(regmatch.startp[0] - tail);
17016 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
17017 /* add the substituted text */
17018 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
17019 + ga.ga_len + i, TRUE, TRUE, FALSE);
17020 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 /* avoid getting stuck on a match with an empty string */
17022 if (tail == regmatch.endp[0])
17023 {
17024 if (*tail == NUL)
17025 break;
17026 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
17027 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 }
17029 else
17030 {
17031 tail = regmatch.endp[0];
17032 if (*tail == NUL)
17033 break;
17034 }
17035 if (!do_all)
17036 break;
17037 }
17038
17039 if (ga.ga_data != NULL)
17040 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
17041
17042 vim_free(regmatch.regprog);
17043 }
17044
17045 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
17046 ga_clear(&ga);
17047 p_cpo = save_cpo;
17048
17049 return ret;
17050}
17051
17052#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */