blob: 1ef824b15218aeb2645af2e0f24e5baf39889125 [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 Moolenaar33570922005-01-25 22:26:29 +0000119 * Array to hold the hashtab with variables local to each sourced script.
120 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000122typedef struct
123{
124 dictitem_T sv_var;
125 dict_T sv_dict;
126} scriptvar_T;
127
128static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
129#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
130#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
132static int echo_attr = 0; /* attributes used for ":echo" */
133
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000134/* Values for trans_function_name() argument: */
135#define TFN_INT 1 /* internal function name OK */
136#define TFN_QUIET 2 /* no error messages */
137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138/*
139 * Structure to hold info for a user function.
140 */
141typedef struct ufunc ufunc_T;
142
143struct ufunc
144{
145 ufunc_T *next; /* next function in list */
146 char_u *name; /* name of function; can start with <SNR>123_
147 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
148 int varargs; /* variable nr of arguments */
149 int flags;
150 int calls; /* nr of active calls */
151 garray_T args; /* arguments */
152 garray_T lines; /* function lines */
153 scid_T script_ID; /* ID of script where function was defined,
154 used for s: variables */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155 int refcount; /* for numbered function: reference count */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156};
157
158/* function flags */
159#define FC_ABORT 1 /* abort function on error */
160#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000161#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163/*
164 * All user-defined functions are found in the forward-linked function list.
165 * The first function is pointed at by firstfunc.
166 */
167ufunc_T *firstfunc = NULL;
168
169#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
170#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
171
Bram Moolenaar33570922005-01-25 22:26:29 +0000172#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
173#define VAR_SHORT_LEN 20 /* short variable name length */
174#define FIXVAR_CNT 12 /* number of fixed variables */
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000177typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178{
179 ufunc_T *func; /* function being called */
180 int linenr; /* next line to be executed */
181 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000182 struct /* fixed variables for arguments */
183 {
184 dictitem_T var; /* variable (without room for name) */
185 char_u room[VAR_SHORT_LEN]; /* room for the name */
186 } fixvar[FIXVAR_CNT];
187 dict_T l_vars; /* l: local function variables */
188 dictitem_T l_vars_var; /* variable for l: scope */
189 dict_T l_avars; /* a: argument variables */
190 dictitem_T l_avars_var; /* variable for a: scope */
191 list_T l_varlist; /* list for a:000 */
192 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
193 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 linenr_T breakpoint; /* next line with breakpoint or zero */
195 int dbg_tick; /* debug_tick when breakpoint was set */
196 int level; /* top nesting level of executed function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000197} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000200 * Info used by a ":for" loop.
201 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000202typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000203{
204 int fi_semicolon; /* TRUE if ending in '; var]' */
205 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000206 listwatch_T fi_lw; /* keep an eye on the item used. */
207 list_T *fi_list; /* list being used */
208} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000209
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000210/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000211 * Struct used by trans_function_name()
212 */
213typedef struct
214{
Bram Moolenaar33570922005-01-25 22:26:29 +0000215 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000216 char_u *fd_newkey; /* new key in "dict" */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217 dictitem_T *fd_di; /* Dictionary item used */
218} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000219
Bram Moolenaara7043832005-01-21 11:56:39 +0000220
221/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000222 * Array to hold the value of v: variables.
223 * The value is in a dictitem, so that it can also be used in the v: scope.
224 * The reason to use this table anyway is for very quick access to the
225 * variables with the VV_ defines.
226 */
227#include "version.h"
228
229/* values for vv_flags: */
230#define VV_COMPAT 1 /* compatible, also used without "v:" */
231#define VV_RO 2 /* read-only */
232#define VV_RO_SBX 4 /* read-only in the sandbox*/
233
234#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
235
236static struct vimvar
237{
238 char *vv_name; /* name of variable, without v: */
239 int vv_len; /* length of name */
240 dictitem_T vv_di; /* value and name for key */
241 char vv_filler[16]; /* space for LONGEST name below!!! */
242 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
243} vimvars[VV_LEN] =
244{
245 /*
246 * The order here must match the VV_ defines in vim.h!
247 * Initializing a union does not work, leave tv.vval empty to get zero's.
248 */
249 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
250 {VV_NAME("count1", VAR_NUMBER), VV_RO},
251 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
252 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
253 {VV_NAME("warningmsg", VAR_STRING), 0},
254 {VV_NAME("statusmsg", VAR_STRING), 0},
255 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
256 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
257 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
258 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
259 {VV_NAME("termresponse", VAR_STRING), VV_RO},
260 {VV_NAME("fname", VAR_STRING), VV_RO},
261 {VV_NAME("lang", VAR_STRING), VV_RO},
262 {VV_NAME("lc_time", VAR_STRING), VV_RO},
263 {VV_NAME("ctype", VAR_STRING), VV_RO},
264 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
265 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
266 {VV_NAME("fname_in", VAR_STRING), VV_RO},
267 {VV_NAME("fname_out", VAR_STRING), VV_RO},
268 {VV_NAME("fname_new", VAR_STRING), VV_RO},
269 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
270 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
271 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
272 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
273 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
274 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
275 {VV_NAME("progname", VAR_STRING), VV_RO},
276 {VV_NAME("servername", VAR_STRING), VV_RO},
277 {VV_NAME("dying", VAR_NUMBER), VV_RO},
278 {VV_NAME("exception", VAR_STRING), VV_RO},
279 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
280 {VV_NAME("register", VAR_STRING), VV_RO},
281 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
282 {VV_NAME("insertmode", VAR_STRING), VV_RO},
283 {VV_NAME("val", VAR_STRING), VV_RO},
284 {VV_NAME("key", VAR_STRING), VV_RO},
285};
286
287/* shorthand */
288#define vv_type vv_di.di_tv.v_type
289#define vv_nr vv_di.di_tv.vval.v_number
290#define vv_str vv_di.di_tv.vval.v_string
291#define vv_tv vv_di.di_tv
292
293/*
294 * The v: variables are stored in dictionary "vimvardict".
295 * "vimvars_var" is the variable that is used for the "l:" scope.
296 */
297static dict_T vimvardict;
298static dictitem_T vimvars_var;
299#define vimvarht vimvardict.dv_hashtab
300
301static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
302static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
303static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
304static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
305static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
306static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
307static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
308static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
309static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate));
310static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
311static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
312static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
313static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
314static list_T *list_alloc __ARGS((void));
315static void list_unref __ARGS((list_T *l));
316static void list_free __ARGS((list_T *l));
317static listitem_T *listitem_alloc __ARGS((void));
318static void listitem_free __ARGS((listitem_T *item));
319static void listitem_remove __ARGS((list_T *l, listitem_T *item));
320static long list_len __ARGS((list_T *l));
321static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
322static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
323static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
324static int string_isa_number __ARGS((char_u *s));
325static listitem_T *list_find __ARGS((list_T *l, long n));
326static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
327static listitem_T *list_find_ext __ARGS((list_T *l, long *ip));
328static void list_append __ARGS((list_T *l, listitem_T *item));
329static int list_append_tv __ARGS((list_T *l, typval_T *tv));
330static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
331static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
332static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
333static list_T *list_copy __ARGS((list_T *orig, int deep));
334static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
335static char_u *list2string __ARGS((typval_T *tv));
336static void list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
337
338static dict_T *dict_alloc __ARGS((void));
339static void dict_unref __ARGS((dict_T *d));
340static void dict_free __ARGS((dict_T *d));
341static dictitem_T *dictitem_alloc __ARGS((char_u *key));
342static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
343static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
344static void dictitem_free __ARGS((dictitem_T *item));
345static int dict_add __ARGS((dict_T *d, dictitem_T *item));
346static long dict_len __ARGS((dict_T *d));
347static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
348static char_u *dict2string __ARGS((typval_T *tv));
349static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
350
351static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
352static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
353static char_u *string_quote __ARGS((char_u *str, int function));
354static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355static int find_internal_func __ARGS((char_u *name));
356static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
357static 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));
358static 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));
359
360static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
361static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
362static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
363static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
364static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
365static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
366static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
367static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
368static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
369static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
370static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
371static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
372static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
373static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
374static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
375static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
376static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
377static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
378static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
379static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
380static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
381static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
382static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
383static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
384static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
385static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
386static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
387static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
388static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
389static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
390static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
391static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
392static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
393static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
394static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
395static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
396static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
397static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
398static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
399static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
400static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
496#ifdef HAVE_STRFTIME
497static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
499static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
526
527static win_T *find_win_by_nr __ARGS((typval_T *vp));
528static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
529static int get_env_len __ARGS((char_u **arg));
530static int get_id_len __ARGS((char_u **arg));
531static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate));
532static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
533static int eval_isnamec __ARGS((int c));
534static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv));
535static typval_T *alloc_tv __ARGS((void));
536static typval_T *alloc_string_tv __ARGS((char_u *string));
537static void free_tv __ARGS((typval_T *varp));
538static void clear_tv __ARGS((typval_T *varp));
539static void init_tv __ARGS((typval_T *varp));
540static long get_tv_number __ARGS((typval_T *varp));
541static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
542static char_u *get_tv_string __ARGS((typval_T *varp));
543static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
544static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
545static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname));
546static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
547static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
548static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
549static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
550static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
551static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
552static int var_check_ro __ARGS((int flags, char_u *name));
553static void copy_tv __ARGS((typval_T *from, typval_T *to));
554static void item_copy __ARGS((typval_T *from, typval_T *to, int deep));
555static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
556static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
557static int eval_fname_script __ARGS((char_u *p));
558static int eval_fname_sid __ARGS((char_u *p));
559static void list_func_head __ARGS((ufunc_T *fp, int indent));
560static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
561static ufunc_T *find_func __ARGS((char_u *name));
562static int function_exists __ARGS((char_u *name));
563static void func_free __ARGS((ufunc_T *fp));
564static void func_unref __ARGS((char_u *name));
565static void func_ref __ARGS((char_u *name));
566static 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));
567static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
568
569static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
570
571static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
572static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
573static char_u *skip_var_one __ARGS((char_u *arg));
574static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
575static void list_glob_vars __ARGS((void));
576static void list_buf_vars __ARGS((void));
577static void list_win_vars __ARGS((void));
578static void list_vim_vars __ARGS((void));
579static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
580static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
581static int check_changedtick __ARGS((char_u *arg));
582static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
583static void clear_lval __ARGS((lval_T *lp));
584static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
585static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
586static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
587static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
588static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
589static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
590
591/*
592 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000593 */
594 void
595eval_init()
596{
Bram Moolenaar33570922005-01-25 22:26:29 +0000597 int i;
598 struct vimvar *p;
599
600 init_var_dict(&globvardict, &globvars_var);
601 init_var_dict(&vimvardict, &vimvars_var);
602
603 for (i = 0; i < VV_LEN; ++i)
604 {
605 p = &vimvars[i];
606 STRCPY(p->vv_di.di_key, p->vv_name);
607 if (p->vv_flags & VV_RO)
608 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
609 else if (p->vv_flags & VV_RO_SBX)
610 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
611 else
612 p->vv_di.di_flags = DI_FLAGS_FIX;
613 /* add to v: scope dict */
614 hash_add(&vimvarht, p->vv_di.di_key);
615 if (p->vv_flags & VV_COMPAT)
616 /* add to g: scope dict */
617 hash_add(&globvardict.dv_hashtab, p->vv_di.di_key);
618 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000619}
620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 * Return the name of the executed function.
623 */
624 char_u *
625func_name(cookie)
626 void *cookie;
627{
Bram Moolenaar33570922005-01-25 22:26:29 +0000628 return ((funccall_T *)cookie)->func->name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629}
630
631/*
632 * Return the address holding the next breakpoint line for a funccall cookie.
633 */
634 linenr_T *
635func_breakpoint(cookie)
636 void *cookie;
637{
Bram Moolenaar33570922005-01-25 22:26:29 +0000638 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639}
640
641/*
642 * Return the address holding the debug tick for a funccall cookie.
643 */
644 int *
645func_dbg_tick(cookie)
646 void *cookie;
647{
Bram Moolenaar33570922005-01-25 22:26:29 +0000648 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649}
650
651/*
652 * Return the nesting level for a funccall cookie.
653 */
654 int
655func_level(cookie)
656 void *cookie;
657{
Bram Moolenaar33570922005-01-25 22:26:29 +0000658 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659}
660
661/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000662funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664/*
665 * Return TRUE when a function was ended by a ":return" command.
666 */
667 int
668current_func_returned()
669{
670 return current_funccal->returned;
671}
672
673
674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 * Set an internal variable to a string value. Creates the variable if it does
676 * not already exist.
677 */
678 void
679set_internal_string_var(name, value)
680 char_u *name;
681 char_u *value;
682{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000683 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000684 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 val = vim_strsave(value);
687 if (val != NULL)
688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000690 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000692 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000693 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695 }
696}
697
698# if defined(FEAT_MBYTE) || defined(PROTO)
699 int
700eval_charconvert(enc_from, enc_to, fname_from, fname_to)
701 char_u *enc_from;
702 char_u *enc_to;
703 char_u *fname_from;
704 char_u *fname_to;
705{
706 int err = FALSE;
707
708 set_vim_var_string(VV_CC_FROM, enc_from, -1);
709 set_vim_var_string(VV_CC_TO, enc_to, -1);
710 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
711 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
712 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
713 err = TRUE;
714 set_vim_var_string(VV_CC_FROM, NULL, -1);
715 set_vim_var_string(VV_CC_TO, NULL, -1);
716 set_vim_var_string(VV_FNAME_IN, NULL, -1);
717 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
718
719 if (err)
720 return FAIL;
721 return OK;
722}
723# endif
724
725# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
726 int
727eval_printexpr(fname, args)
728 char_u *fname;
729 char_u *args;
730{
731 int err = FALSE;
732
733 set_vim_var_string(VV_FNAME_IN, fname, -1);
734 set_vim_var_string(VV_CMDARG, args, -1);
735 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
736 err = TRUE;
737 set_vim_var_string(VV_FNAME_IN, NULL, -1);
738 set_vim_var_string(VV_CMDARG, NULL, -1);
739
740 if (err)
741 {
742 mch_remove(fname);
743 return FAIL;
744 }
745 return OK;
746}
747# endif
748
749# if defined(FEAT_DIFF) || defined(PROTO)
750 void
751eval_diff(origfile, newfile, outfile)
752 char_u *origfile;
753 char_u *newfile;
754 char_u *outfile;
755{
756 int err = FALSE;
757
758 set_vim_var_string(VV_FNAME_IN, origfile, -1);
759 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
760 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
761 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
762 set_vim_var_string(VV_FNAME_IN, NULL, -1);
763 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
764 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
765}
766
767 void
768eval_patch(origfile, difffile, outfile)
769 char_u *origfile;
770 char_u *difffile;
771 char_u *outfile;
772{
773 int err;
774
775 set_vim_var_string(VV_FNAME_IN, origfile, -1);
776 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
777 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
778 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
779 set_vim_var_string(VV_FNAME_IN, NULL, -1);
780 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
781 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
782}
783# endif
784
785/*
786 * Top level evaluation function, returning a boolean.
787 * Sets "error" to TRUE if there was an error.
788 * Return TRUE or FALSE.
789 */
790 int
791eval_to_bool(arg, error, nextcmd, skip)
792 char_u *arg;
793 int *error;
794 char_u **nextcmd;
795 int skip; /* only parse, don't execute */
796{
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 int retval = FALSE;
799
800 if (skip)
801 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 else
805 {
806 *error = FALSE;
807 if (!skip)
808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 retval = (get_tv_number(&tv) != 0);
810 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 }
813 if (skip)
814 --emsg_skip;
815
816 return retval;
817}
818
819/*
820 * Top level evaluation function, returning a string. If "skip" is TRUE,
821 * only parsing to "nextcmd" is done, without reporting errors. Return
822 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
823 */
824 char_u *
825eval_to_string_skip(arg, nextcmd, skip)
826 char_u *arg;
827 char_u **nextcmd;
828 int skip; /* only parse, don't execute */
829{
Bram Moolenaar33570922005-01-25 22:26:29 +0000830 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 char_u *retval;
832
833 if (skip)
834 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 retval = NULL;
837 else
838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000839 retval = vim_strsave(get_tv_string(&tv));
840 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 if (skip)
843 --emsg_skip;
844
845 return retval;
846}
847
848/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000849 * Skip over an expression at "*pp".
850 * Return FAIL for an error, OK otherwise.
851 */
852 int
853skip_expr(pp)
854 char_u **pp;
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000857
858 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000859 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000860}
861
862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * Top level evaluation function, returning a string.
864 * Return pointer to allocated memory, or NULL for failure.
865 */
866 char_u *
867eval_to_string(arg, nextcmd)
868 char_u *arg;
869 char_u **nextcmd;
870{
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 char_u *retval;
873
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 retval = NULL;
876 else
877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000878 retval = vim_strsave(get_tv_string(&tv));
879 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881
882 return retval;
883}
884
885/*
886 * Call eval_to_string() with "sandbox" set and not using local variables.
887 */
888 char_u *
889eval_to_string_safe(arg, nextcmd)
890 char_u *arg;
891 char_u **nextcmd;
892{
893 char_u *retval;
894 void *save_funccalp;
895
896 save_funccalp = save_funccal();
897 ++sandbox;
898 retval = eval_to_string(arg, nextcmd);
899 --sandbox;
900 restore_funccal(save_funccalp);
901 return retval;
902}
903
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904/*
905 * Top level evaluation function, returning a number.
906 * Evaluates "expr" silently.
907 * Returns -1 for an error.
908 */
909 int
910eval_to_number(expr)
911 char_u *expr;
912{
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000915 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 ++emsg_off;
918
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000919 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = -1;
921 else
922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 retval = get_tv_number(&rettv);
924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 --emsg_off;
927
928 return retval;
929}
930
931#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
932/*
933 * Call some vimL function and return the result as a string
934 * Uses argv[argc] for the function arguments.
935 */
936 char_u *
937call_vim_function(func, argc, argv, safe)
938 char_u *func;
939 int argc;
940 char_u **argv;
941 int safe; /* use the sandbox */
942{
943 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 typval_T rettv;
945 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 long n;
947 int len;
948 int i;
949 int doesrange;
950 void *save_funccalp = NULL;
951
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (argvars == NULL)
954 return NULL;
955
956 for (i = 0; i < argc; i++)
957 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000958 /* Pass a NULL or empty argument as an empty string */
959 if (argv[i] == NULL || *argv[i] == NUL)
960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000961 argvars[i].v_type = VAR_STRING;
962 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000963 continue;
964 }
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /* Recognize a number argument, the others must be strings. */
967 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
968 if (len != 0 && len == (int)STRLEN(argv[i]))
969 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000970 argvars[i].v_type = VAR_NUMBER;
971 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973 else
974 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000975 argvars[i].v_type = VAR_STRING;
976 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
978 }
979
980 if (safe)
981 {
982 save_funccalp = save_funccal();
983 ++sandbox;
984 }
985
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000986 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
987 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +0000989 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000990 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000992 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 vim_free(argvars);
994
995 if (safe)
996 {
997 --sandbox;
998 restore_funccal(save_funccalp);
999 }
1000 return retval;
1001}
1002#endif
1003
1004/*
1005 * Save the current function call pointer, and set it to NULL.
1006 * Used when executing autocommands and for ":source".
1007 */
1008 void *
1009save_funccal()
1010{
Bram Moolenaar33570922005-01-25 22:26:29 +00001011 funccall_T *fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 fc = current_funccal;
1014 current_funccal = NULL;
1015 return (void *)fc;
1016}
1017
1018 void
1019restore_funccal(fc)
1020 void *fc;
1021{
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 current_funccal = (funccall_T *)fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025#ifdef FEAT_FOLDING
1026/*
1027 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1028 * it in "*cp". Doesn't give error messages.
1029 */
1030 int
1031eval_foldexpr(arg, cp)
1032 char_u *arg;
1033 int *cp;
1034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int retval;
1037 char_u *s;
1038
1039 ++emsg_off;
1040 ++sandbox;
1041 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 retval = 0;
1044 else
1045 {
1046 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 if (tv.v_type == VAR_NUMBER)
1048 retval = tv.vval.v_number;
1049 else if (tv.v_type == VAR_UNKNOWN
1050 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 retval = 0;
1052 else
1053 {
1054 /* If the result is a string, check if there is a non-digit before
1055 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (!VIM_ISDIGIT(*s) && *s != '-')
1058 *cp = *s++;
1059 retval = atol((char *)s);
1060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001061 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 --emsg_off;
1064 --sandbox;
1065
1066 return retval;
1067}
1068#endif
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070/*
1071 * Expands out the 'magic' {}'s in a variable/function name.
1072 * Note that this can call itself recursively, to deal with
1073 * constructs like foo{bar}{baz}{bam}
1074 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1075 * "in_start" ^
1076 * "expr_start" ^
1077 * "expr_end" ^
1078 * "in_end" ^
1079 *
1080 * Returns a new allocated string, which the caller must free.
1081 * Returns NULL for failure.
1082 */
1083 static char_u *
1084make_expanded_name(in_start, expr_start, expr_end, in_end)
1085 char_u *in_start;
1086 char_u *expr_start;
1087 char_u *expr_end;
1088 char_u *in_end;
1089{
1090 char_u c1;
1091 char_u *retval = NULL;
1092 char_u *temp_result;
1093 char_u *nextcmd = NULL;
1094
1095 if (expr_end == NULL || in_end == NULL)
1096 return NULL;
1097 *expr_start = NUL;
1098 *expr_end = NUL;
1099 c1 = *in_end;
1100 *in_end = NUL;
1101
1102 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1103 if (temp_result != NULL && nextcmd == NULL)
1104 {
1105 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1106 + (in_end - expr_end) + 1));
1107
1108 if (retval != NULL)
1109 {
1110 STRCPY(retval, in_start);
1111 STRCAT(retval, temp_result);
1112 STRCAT(retval, expr_end + 1);
1113 }
1114 }
1115 vim_free(temp_result);
1116
1117 *in_end = c1; /* put char back for error messages */
1118 *expr_start = '{';
1119 *expr_end = '}';
1120
1121 if (retval != NULL)
1122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (expr_start != NULL)
1125 {
1126 /* Further expansion! */
1127 temp_result = make_expanded_name(retval, expr_start,
1128 expr_end, temp_result);
1129 vim_free(retval);
1130 retval = temp_result;
1131 }
1132 }
1133
1134 return retval;
1135
1136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 * ":let" list all variable values
1140 * ":let var1 var2" list variable values
1141 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001142 * ":let var += expr" assignment command.
1143 * ":let var -= expr" assignment command.
1144 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 */
1147 void
1148ex_let(eap)
1149 exarg_T *eap;
1150{
1151 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001153 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 int var_count = 0;
1156 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001157 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001159 expr = skip_var_list(arg, &var_count, &semicolon);
1160 if (expr == NULL)
1161 return;
1162 expr = vim_strchr(expr, '=');
1163 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001165 if (*arg == '[')
1166 EMSG(_(e_invarg));
1167 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 /* ":let var1 var2" */
1169 arg = list_arg_vars(eap, arg);
1170 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001173 list_glob_vars();
1174 list_buf_vars();
1175 list_win_vars();
1176 list_vim_vars();
1177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 eap->nextcmd = check_nextcmd(arg);
1179 }
1180 else
1181 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001182 op[0] = '=';
1183 op[1] = NUL;
1184 if (expr > arg)
1185 {
1186 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1187 op[0] = expr[-1]; /* +=, -= or .= */
1188 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001189 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (eap->skip)
1192 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001193 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (eap->skip)
1195 {
1196 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 --emsg_skip;
1199 }
1200 else if (i != FAIL)
1201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001202 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001203 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207}
1208
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001209/*
1210 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1211 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001212 * When "nextchars" is not NULL it points to a string with characters that
1213 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1214 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001215 * Returns OK or FAIL;
1216 */
1217 static int
1218ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1219 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001221 int copy; /* copy values from "tv", don't move */
1222 int semicolon; /* from skip_var_list() */
1223 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001225{
1226 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001227 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001229 listitem_T *item;
1230 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001231
1232 if (*arg != '[')
1233 {
1234 /*
1235 * ":let var = expr" or ":for var in list"
1236 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001238 return FAIL;
1239 return OK;
1240 }
1241
1242 /*
1243 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1244 */
1245 l = tv->vval.v_list;
1246 if (tv->v_type != VAR_LIST || l == NULL)
1247 {
1248 EMSG(_(e_listreq));
1249 return FAIL;
1250 }
1251
1252 i = list_len(l);
1253 if (semicolon == 0 && var_count < i)
1254 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001255 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001256 return FAIL;
1257 }
1258 if (var_count - semicolon > i)
1259 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001260 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001261 return FAIL;
1262 }
1263
1264 item = l->lv_first;
1265 while (*arg != ']')
1266 {
1267 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001269 item = item->li_next;
1270 if (arg == NULL)
1271 return FAIL;
1272
1273 arg = skipwhite(arg);
1274 if (*arg == ';')
1275 {
1276 /* Put the rest of the list (may be empty) in the var after ';'.
1277 * Create a new list for this. */
1278 l = list_alloc();
1279 if (l == NULL)
1280 return FAIL;
1281 while (item != NULL)
1282 {
1283 list_append_tv(l, &item->li_tv);
1284 item = item->li_next;
1285 }
1286
1287 ltv.v_type = VAR_LIST;
1288 ltv.vval.v_list = l;
1289 l->lv_refcount = 1;
1290
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001291 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1292 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001293 clear_tv(&ltv);
1294 if (arg == NULL)
1295 return FAIL;
1296 break;
1297 }
1298 else if (*arg != ',' && *arg != ']')
1299 {
1300 EMSG2(_(e_intern2), "ex_let_vars()");
1301 return FAIL;
1302 }
1303 }
1304
1305 return OK;
1306}
1307
1308/*
1309 * Skip over assignable variable "var" or list of variables "[var, var]".
1310 * Used for ":let varvar = expr" and ":for varvar in expr".
1311 * For "[var, var]" increment "*var_count" for each variable.
1312 * for "[var, var; var]" set "semicolon".
1313 * Return NULL for an error.
1314 */
1315 static char_u *
1316skip_var_list(arg, var_count, semicolon)
1317 char_u *arg;
1318 int *var_count;
1319 int *semicolon;
1320{
1321 char_u *p, *s;
1322
1323 if (*arg == '[')
1324 {
1325 /* "[var, var]": find the matching ']'. */
1326 p = arg;
1327 while (1)
1328 {
1329 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1330 s = skip_var_one(p);
1331 if (s == p)
1332 {
1333 EMSG2(_(e_invarg2), p);
1334 return NULL;
1335 }
1336 ++*var_count;
1337
1338 p = skipwhite(s);
1339 if (*p == ']')
1340 break;
1341 else if (*p == ';')
1342 {
1343 if (*semicolon == 1)
1344 {
1345 EMSG(_("Double ; in list of variables"));
1346 return NULL;
1347 }
1348 *semicolon = 1;
1349 }
1350 else if (*p != ',')
1351 {
1352 EMSG2(_(e_invarg2), p);
1353 return NULL;
1354 }
1355 }
1356 return p + 1;
1357 }
1358 else
1359 return skip_var_one(arg);
1360}
1361
1362 static char_u *
1363skip_var_one(arg)
1364 char_u *arg;
1365{
1366 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1367 ++arg;
1368 return find_name_end(arg, NULL, NULL, TRUE);
1369}
1370
Bram Moolenaara7043832005-01-21 11:56:39 +00001371/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 * List variables for hashtab "ht" with prefix "prefix".
1373 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001374 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001376list_hashtable_vars(ht, prefix, empty)
1377 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001378 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001379 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001380{
Bram Moolenaar33570922005-01-25 22:26:29 +00001381 hashitem_T *hi;
1382 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001383 int todo;
1384
1385 todo = ht->ht_used;
1386 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1387 {
1388 if (!HASHITEM_EMPTY(hi))
1389 {
1390 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 di = HI2DI(hi);
1392 if (empty || di->di_tv.v_type != VAR_STRING
1393 || di->di_tv.vval.v_string != NULL)
1394 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001395 }
1396 }
1397}
1398
1399/*
1400 * List global variables.
1401 */
1402 static void
1403list_glob_vars()
1404{
Bram Moolenaar33570922005-01-25 22:26:29 +00001405 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001406}
1407
1408/*
1409 * List buffer variables.
1410 */
1411 static void
1412list_buf_vars()
1413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001415}
1416
1417/*
1418 * List window variables.
1419 */
1420 static void
1421list_win_vars()
1422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001424}
1425
1426/*
1427 * List Vim variables.
1428 */
1429 static void
1430list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433}
1434
1435/*
1436 * List variables in "arg".
1437 */
1438 static char_u *
1439list_arg_vars(eap, arg)
1440 exarg_T *eap;
1441 char_u *arg;
1442{
1443 int error = FALSE;
1444 char_u *temp_string = NULL;
1445 int arg_len;
1446 char_u *expr_start;
1447 char_u *expr_end;
1448 char_u *name_end;
1449 int c1 = 0, c2;
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 dictitem_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 char_u *name;
1452
1453 while (!ends_excmd(*arg) && !got_int)
1454 {
1455 /* Find the end of the name. */
1456 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1457
1458 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1459 {
1460 emsg_severe = TRUE;
1461 EMSG(_(e_trailing));
1462 break;
1463 }
1464 if (!error && !eap->skip)
1465 {
1466 if (expr_start != NULL)
1467 {
1468 temp_string = make_expanded_name(arg, expr_start,
1469 expr_end, name_end);
1470 if (temp_string == NULL)
1471 {
1472 /*
1473 * Report an invalid expression in braces, unless
1474 * the expression evaluation has been cancelled due
1475 * to an aborting error, an interrupt, or an
1476 * exception.
1477 */
1478 if (!aborting())
1479 {
1480 emsg_severe = TRUE;
1481 EMSG2(_(e_invarg2), arg);
1482 break;
1483 }
1484 error = TRUE;
1485 arg = skipwhite(name_end);
1486 continue;
1487 }
1488 arg = temp_string;
1489 arg_len = STRLEN(temp_string);
1490 }
1491 else
1492 {
1493 c1 = *name_end;
1494 *name_end = NUL;
1495 arg_len = (int)(name_end - arg);
1496 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001497 if (arg_len == 2 && arg[1] == ':')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001499 switch (*arg)
1500 {
1501 case 'g': list_glob_vars(); break;
1502 case 'b': list_buf_vars(); break;
1503 case 'w': list_win_vars(); break;
1504 case 'v': list_vim_vars(); break;
1505 default:
1506 EMSG2(_("E738: Can't list variables for %s"), arg);
1507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 }
1509 else
1510 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001511 if (STRCMP("b:changedtick", arg) == 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001513 char_u numbuf[NUMBUFLEN];
1514
1515 sprintf((char *)numbuf, "%ld",
1516 (long)curbuf->b_changedtick);
1517 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1518 VAR_NUMBER, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519 }
1520 else
1521 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001522 varp = find_var(arg, NULL);
1523 if (varp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524 {
Bram Moolenaara7043832005-01-21 11:56:39 +00001525 /* Skip further arguments but do continue to
1526 * search for a trailing command. */
1527 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1528 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001529 }
1530 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001531 {
1532 name = vim_strchr(arg, ':');
1533 if (name != NULL)
1534 {
1535 /* "a:" vars have no name stored, use whole arg */
1536 if (arg[0] == 'a' && arg[1] == ':')
1537 c2 = NUL;
1538 else
1539 {
1540 c2 = *++name;
1541 *name = NUL;
1542 }
1543 list_one_var(varp, arg);
1544 if (c2 != NUL)
1545 *name = c2;
1546 }
1547 else
1548 list_one_var(varp, (char_u *)"");
1549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001550 }
1551 }
1552 if (expr_start != NULL)
1553 vim_free(temp_string);
1554 else
1555 *name_end = c1;
1556 }
1557 arg = skipwhite(name_end);
1558 }
1559
1560 return arg;
1561}
1562
1563/*
1564 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1565 * Returns a pointer to the char just after the var name.
1566 * Returns NULL if there is an error.
1567 */
1568 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001571 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001574 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575{
1576 int c1;
1577 char_u *name;
1578 char_u *p;
1579 char_u *arg_end = NULL;
1580 int len;
1581 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001582 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583
1584 /*
1585 * ":let $VAR = expr": Set environment variable.
1586 */
1587 if (*arg == '$')
1588 {
1589 /* Find the end of the name. */
1590 ++arg;
1591 name = arg;
1592 len = get_env_len(&arg);
1593 if (len == 0)
1594 EMSG2(_(e_invarg2), name - 1);
1595 else
1596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 if (op != NULL && (*op == '+' || *op == '-'))
1598 EMSG2(_(e_letwrong), op);
1599 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001600 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 EMSG(_(e_letunexp));
1602 else
1603 {
1604 c1 = name[len];
1605 name[len] = NUL;
1606 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 if (op != NULL && *op == '.')
1608 {
1609 int mustfree = FALSE;
1610 char_u *s = vim_getenv(name, &mustfree);
1611
1612 if (s != NULL)
1613 {
1614 p = tofree = concat_str(s, p);
1615 if (mustfree)
1616 vim_free(s);
1617 }
1618 }
1619 if (p != NULL)
1620 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621 if (STRICMP(name, "HOME") == 0)
1622 init_homedir();
1623 else if (didset_vim && STRICMP(name, "VIM") == 0)
1624 didset_vim = FALSE;
1625 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1626 didset_vimruntime = FALSE;
1627 name[len] = c1;
1628 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 }
1631 }
1632 }
1633
1634 /*
1635 * ":let &option = expr": Set option value.
1636 * ":let &l:option = expr": Set local option value.
1637 * ":let &g:option = expr": Set global option value.
1638 */
1639 else if (*arg == '&')
1640 {
1641 /* Find the end of the name. */
1642 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 if (p == NULL || (endchars != NULL
1644 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 EMSG(_(e_letunexp));
1646 else
1647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 long n;
1649 int opt_type;
1650 long numval;
1651 char_u *stringval = NULL;
1652 char_u *s;
1653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 c1 = *p;
1655 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001656
1657 n = get_tv_number(tv);
1658 s = get_tv_string(tv);
1659 if (op != NULL && *op != '=')
1660 {
1661 opt_type = get_option_value(arg, &numval,
1662 &stringval, opt_flags);
1663 if ((opt_type == 1 && *op == '.')
1664 || (opt_type == 0 && *op != '.'))
1665 EMSG2(_(e_letwrong), op);
1666 else
1667 {
1668 if (opt_type == 1) /* number */
1669 {
1670 if (*op == '+')
1671 n = numval + n;
1672 else
1673 n = numval - n;
1674 }
1675 else if (opt_type == 0 && stringval != NULL) /* string */
1676 {
1677 s = concat_str(stringval, s);
1678 vim_free(stringval);
1679 stringval = s;
1680 }
1681 }
1682 }
1683 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 *p = c1;
1685 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 }
1688 }
1689
1690 /*
1691 * ":let @r = expr": Set register contents.
1692 */
1693 else if (*arg == '@')
1694 {
1695 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696 if (op != NULL && (*op == '+' || *op == '-'))
1697 EMSG2(_(e_letwrong), op);
1698 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 EMSG(_(e_letunexp));
1701 else
1702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703 char_u *tofree = NULL;
1704 char_u *s;
1705
1706 p = get_tv_string(tv);
1707 if (op != NULL && *op == '.')
1708 {
1709 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1710 if (s != NULL)
1711 {
1712 p = tofree = concat_str(s, p);
1713 vim_free(s);
1714 }
1715 }
1716 if (p != NULL)
1717 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 }
1721 }
1722
1723 /*
1724 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001725 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001727 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001728 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001729 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001731 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1732 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001734 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1735 EMSG(_(e_letunexp));
1736 else
1737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001739 arg_end = p;
1740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001742 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 }
1744
1745 else
1746 EMSG2(_(e_invarg2), arg);
1747
1748 return arg_end;
1749}
1750
1751/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001752 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1753 */
1754 static int
1755check_changedtick(arg)
1756 char_u *arg;
1757{
1758 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1759 {
1760 EMSG2(_(e_readonlyvar), arg);
1761 return TRUE;
1762 }
1763 return FALSE;
1764}
1765
1766/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001767 * Get an lval: variable, Dict item or List item that can be assigned a value
1768 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1769 * "name.key", "name.key[expr]" etc.
1770 * Indexing only works if "name" is an existing List or Dictionary.
1771 * "name" points to the start of the name.
1772 * If "rettv" is not NULL it points to the value to be assigned.
1773 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1774 * wrong; must end in space or cmd separator.
1775 *
1776 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001777 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001778 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 */
1780 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001781get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T *rettv;
1784 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 int unlet;
1786 int skip;
1787 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 char_u *expr_start, *expr_end;
1791 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001792 dictitem_T *v;
1793 typval_T var1;
1794 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001795 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001797 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001798 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001799 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001802 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803
1804 if (skip)
1805 {
1806 /* When skipping just find the end of the name. */
1807 lp->ll_name = name;
1808 return find_name_end(name, NULL, NULL, TRUE);
1809 }
1810
1811 /* Find the end of the name. */
1812 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1813 if (expr_start != NULL)
1814 {
1815 /* Don't expand the name when we already know there is an error. */
1816 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1817 && *p != '[' && *p != '.')
1818 {
1819 EMSG(_(e_trailing));
1820 return NULL;
1821 }
1822
1823 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1824 if (lp->ll_exp_name == NULL)
1825 {
1826 /* Report an invalid expression in braces, unless the
1827 * expression evaluation has been cancelled due to an
1828 * aborting error, an interrupt, or an exception. */
1829 if (!aborting() && !quiet)
1830 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001831 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001832 EMSG2(_(e_invarg2), name);
1833 return NULL;
1834 }
1835 }
1836 lp->ll_name = lp->ll_exp_name;
1837 }
1838 else
1839 lp->ll_name = name;
1840
1841 /* Without [idx] or .key we are done. */
1842 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1843 return p;
1844
1845 cc = *p;
1846 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00001847 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 if (v == NULL && !quiet)
1849 EMSG2(_(e_undefvar), lp->ll_name);
1850 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 if (v == NULL)
1852 return NULL;
1853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 /*
1855 * Loop until no more [idx] or .key is following.
1856 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001858 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1861 && !(lp->ll_tv->v_type == VAR_DICT
1862 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 if (!quiet)
1865 EMSG(_("E689: Can only index a List or Dictionary"));
1866 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001870 if (!quiet)
1871 EMSG(_("E708: [:] must come last"));
1872 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001874
Bram Moolenaar8c711452005-01-14 21:53:12 +00001875 len = -1;
1876 if (*p == '.')
1877 {
1878 key = p + 1;
1879 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1880 ;
1881 if (len == 0)
1882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001883 if (!quiet)
1884 EMSG(_(e_emptykey));
1885 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001886 }
1887 p = key + len;
1888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001889 else
1890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001891 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001892 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001893 if (*p == ':')
1894 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001895 else
1896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001897 empty1 = FALSE;
1898 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001900 }
1901
1902 /* Optionally get the second index [ :expr]. */
1903 if (*p == ':')
1904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001906 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001909 if (!empty1)
1910 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 if (rettv != NULL && (rettv->v_type != VAR_LIST
1914 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 if (!quiet)
1917 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001918 if (!empty1)
1919 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001921 }
1922 p = skipwhite(p + 1);
1923 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001925 else
1926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001928 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1929 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001930 if (!empty1)
1931 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001933 }
1934 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001939
Bram Moolenaar8c711452005-01-14 21:53:12 +00001940 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 if (!quiet)
1943 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 if (!empty1)
1945 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001947 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001949 }
1950
1951 /* Skip to past ']'. */
1952 ++p;
1953 }
1954
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001956 {
1957 if (len == -1)
1958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001960 key = get_tv_string(&var1);
1961 if (*key == NUL)
1962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (!quiet)
1964 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 }
1968 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 lp->ll_list = NULL;
1970 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001971 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001974 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001979 if (len == -1)
1980 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001981 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001982 }
1983 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001985 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001987 if (len == -1)
1988 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001990 p = NULL;
1991 break;
1992 }
1993 if (len == -1)
1994 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 }
1997 else
1998 {
1999 /*
2000 * Get the number and item for the only or first index of the List.
2001 */
2002 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002004 else
2005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002007 clear_tv(&var1);
2008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 lp->ll_list = lp->ll_tv->vval.v_list;
2011 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2012 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 if (!quiet)
2015 EMSGN(_(e_listidx), lp->ll_n1);
2016 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 }
2020
2021 /*
2022 * May need to find the item or absolute index for the second
2023 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 * When no index given: "lp->ll_empty2" is TRUE.
2025 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002028 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034 if (ni == NULL)
2035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 if (!quiet)
2037 EMSGN(_(e_listidx), lp->ll_n2);
2038 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 }
2042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2044 if (lp->ll_n1 < 0)
2045 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2046 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 if (!quiet)
2049 EMSGN(_(e_listidx), lp->ll_n2);
2050 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002051 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002052 }
2053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 }
2057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 return p;
2059}
2060
2061/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002062 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 */
2064 static void
2065clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002066 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067{
2068 vim_free(lp->ll_exp_name);
2069 vim_free(lp->ll_newkey);
2070}
2071
2072/*
2073 * Set a variable that was parsed by get_lval().
2074 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 */
2077 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002078set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084{
2085 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 listitem_T *ni;
2087 listitem_T *ri;
2088 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089
2090 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 cc = *endp;
2095 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 if (op != NULL && *op != '=')
2097 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002098 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099
2100 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK)
2101 {
2102 if (tv_op(&tv, rettv, op) == OK)
2103 set_var(lp->ll_name, &tv, FALSE);
2104 clear_tv(&tv);
2105 }
2106 }
2107 else
2108 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 }
2112 else if (lp->ll_range)
2113 {
2114 /*
2115 * Assign the List values to the list items.
2116 */
2117 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002118 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002119 if (op != NULL && *op != '=')
2120 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2121 else
2122 {
2123 clear_tv(&lp->ll_li->li_tv);
2124 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2125 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 ri = ri->li_next;
2127 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2128 break;
2129 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002130 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 /* Need to add an empty item. */
2132 ni = listitem_alloc();
2133 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 ri = NULL;
2136 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002137 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 ni->li_tv.v_type = VAR_NUMBER;
2139 ni->li_tv.vval.v_number = 0;
2140 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002141 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 lp->ll_li = lp->ll_li->li_next;
2143 ++lp->ll_n1;
2144 }
2145 if (ri != NULL)
2146 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002147 else if (lp->ll_empty2
2148 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 : lp->ll_n1 != lp->ll_n2)
2150 EMSG(_("E711: List value has not enough items"));
2151 }
2152 else
2153 {
2154 /*
2155 * Assign to a List or Dictionary item.
2156 */
2157 if (lp->ll_newkey != NULL)
2158 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 if (op != NULL && *op != '=')
2160 {
2161 EMSG2(_(e_letwrong), op);
2162 return;
2163 }
2164
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002166 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 if (di == NULL)
2168 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002169 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2170 {
2171 vim_free(di);
2172 return;
2173 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 else if (op != NULL && *op != '=')
2177 {
2178 tv_op(lp->ll_tv, rettv, op);
2179 return;
2180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 /*
2185 * Assign the value to the variable or list item.
2186 */
2187 if (copy)
2188 copy_tv(rettv, lp->ll_tv);
2189 else
2190 {
2191 *lp->ll_tv = *rettv;
2192 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 }
2194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195}
2196
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002197/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2199 * Returns OK or FAIL.
2200 */
2201 static int
2202tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002203 typval_T *tv1;
2204 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 char_u *op;
2206{
2207 long n;
2208 char_u numbuf[NUMBUFLEN];
2209 char_u *s;
2210
2211 /* Can't do anything with a Funcref or a Dict on the right. */
2212 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2213 {
2214 switch (tv1->v_type)
2215 {
2216 case VAR_DICT:
2217 case VAR_FUNC:
2218 break;
2219
2220 case VAR_LIST:
2221 if (*op != '+' || tv2->v_type != VAR_LIST)
2222 break;
2223 /* List += List */
2224 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2225 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2226 return OK;
2227
2228 case VAR_NUMBER:
2229 case VAR_STRING:
2230 if (tv2->v_type == VAR_LIST)
2231 break;
2232 if (*op == '+' || *op == '-')
2233 {
2234 /* nr += nr or nr -= nr*/
2235 n = get_tv_number(tv1);
2236 if (*op == '+')
2237 n += get_tv_number(tv2);
2238 else
2239 n -= get_tv_number(tv2);
2240 clear_tv(tv1);
2241 tv1->v_type = VAR_NUMBER;
2242 tv1->vval.v_number = n;
2243 }
2244 else
2245 {
2246 /* str .= str */
2247 s = get_tv_string(tv1);
2248 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2249 clear_tv(tv1);
2250 tv1->v_type = VAR_STRING;
2251 tv1->vval.v_string = s;
2252 }
2253 return OK;
2254 }
2255 }
2256
2257 EMSG2(_(e_letwrong), op);
2258 return FAIL;
2259}
2260
2261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262 * Add a watcher to a list.
2263 */
2264 static void
2265list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002266 list_T *l;
2267 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002268{
2269 lw->lw_next = l->lv_watch;
2270 l->lv_watch = lw;
2271}
2272
2273/*
2274 * Remove a watches from a list.
2275 * No warning when it isn't found...
2276 */
2277 static void
2278list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002279 list_T *l;
2280 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002281{
Bram Moolenaar33570922005-01-25 22:26:29 +00002282 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002283
2284 lwp = &l->lv_watch;
2285 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2286 {
2287 if (lw == lwrem)
2288 {
2289 *lwp = lw->lw_next;
2290 break;
2291 }
2292 lwp = &lw->lw_next;
2293 }
2294}
2295
2296/*
2297 * Just before removing an item from a list: advance watchers to the next
2298 * item.
2299 */
2300 static void
2301list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002302 list_T *l;
2303 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002304{
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306
2307 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2308 if (lw->lw_item == item)
2309 lw->lw_item = item->li_next;
2310}
2311
2312/*
2313 * Evaluate the expression used in a ":for var in expr" command.
2314 * "arg" points to "var".
2315 * Set "*errp" to TRUE for an error, FALSE otherwise;
2316 * Return a pointer that holds the info. Null when there is an error.
2317 */
2318 void *
2319eval_for_line(arg, errp, nextcmdp, skip)
2320 char_u *arg;
2321 int *errp;
2322 char_u **nextcmdp;
2323 int skip;
2324{
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002326 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T tv;
2328 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329
2330 *errp = TRUE; /* default: there is an error */
2331
Bram Moolenaar33570922005-01-25 22:26:29 +00002332 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 if (fi == NULL)
2334 return NULL;
2335
2336 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2337 if (expr == NULL)
2338 return fi;
2339
2340 expr = skipwhite(expr);
2341 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2342 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002343 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344 return fi;
2345 }
2346
2347 if (skip)
2348 ++emsg_skip;
2349 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2350 {
2351 *errp = FALSE;
2352 if (!skip)
2353 {
2354 l = tv.vval.v_list;
2355 if (tv.v_type != VAR_LIST || l == NULL)
2356 EMSG(_(e_listreq));
2357 else
2358 {
2359 fi->fi_list = l;
2360 list_add_watch(l, &fi->fi_lw);
2361 fi->fi_lw.lw_item = l->lv_first;
2362 }
2363 }
2364 }
2365 if (skip)
2366 --emsg_skip;
2367
2368 return fi;
2369}
2370
2371/*
2372 * Use the first item in a ":for" list. Advance to the next.
2373 * Assign the values to the variable (list). "arg" points to the first one.
2374 * Return TRUE when a valid item was found, FALSE when at end of list or
2375 * something wrong.
2376 */
2377 int
2378next_for_item(fi_void, arg)
2379 void *fi_void;
2380 char_u *arg;
2381{
Bram Moolenaar33570922005-01-25 22:26:29 +00002382 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002384 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002385
2386 item = fi->fi_lw.lw_item;
2387 if (item == NULL)
2388 result = FALSE;
2389 else
2390 {
2391 fi->fi_lw.lw_item = item->li_next;
2392 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2393 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2394 }
2395 return result;
2396}
2397
2398/*
2399 * Free the structure used to store info used by ":for".
2400 */
2401 void
2402free_for_info(fi_void)
2403 void *fi_void;
2404{
Bram Moolenaar33570922005-01-25 22:26:29 +00002405 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002406
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002407 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002408 list_rem_watch(fi->fi_list, &fi->fi_lw);
2409 vim_free(fi);
2410}
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2413
2414 void
2415set_context_for_expression(xp, arg, cmdidx)
2416 expand_T *xp;
2417 char_u *arg;
2418 cmdidx_T cmdidx;
2419{
2420 int got_eq = FALSE;
2421 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 if (cmdidx == CMD_let)
2425 {
2426 xp->xp_context = EXPAND_USER_VARS;
2427 if (vim_strchr(arg, '=') == NULL)
2428 {
2429 /* ":let var1 var2 ...": find last space. */
2430 for (p = arg + STRLEN(arg); p > arg; )
2431 {
2432 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002433 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434 if (vim_iswhite(*p))
2435 break;
2436 }
2437 return;
2438 }
2439 }
2440 else
2441 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2442 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 while ((xp->xp_pattern = vim_strpbrk(arg,
2444 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2445 {
2446 c = *xp->xp_pattern;
2447 if (c == '&')
2448 {
2449 c = xp->xp_pattern[1];
2450 if (c == '&')
2451 {
2452 ++xp->xp_pattern;
2453 xp->xp_context = cmdidx != CMD_let || got_eq
2454 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2455 }
2456 else if (c != ' ')
2457 xp->xp_context = EXPAND_SETTINGS;
2458 }
2459 else if (c == '$')
2460 {
2461 /* environment variable */
2462 xp->xp_context = EXPAND_ENV_VARS;
2463 }
2464 else if (c == '=')
2465 {
2466 got_eq = TRUE;
2467 xp->xp_context = EXPAND_EXPRESSION;
2468 }
2469 else if (c == '<'
2470 && xp->xp_context == EXPAND_FUNCTIONS
2471 && vim_strchr(xp->xp_pattern, '(') == NULL)
2472 {
2473 /* Function name can start with "<SNR>" */
2474 break;
2475 }
2476 else if (cmdidx != CMD_let || got_eq)
2477 {
2478 if (c == '"') /* string */
2479 {
2480 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2481 if (c == '\\' && xp->xp_pattern[1] != NUL)
2482 ++xp->xp_pattern;
2483 xp->xp_context = EXPAND_NOTHING;
2484 }
2485 else if (c == '\'') /* literal string */
2486 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2489 /* skip */ ;
2490 xp->xp_context = EXPAND_NOTHING;
2491 }
2492 else if (c == '|')
2493 {
2494 if (xp->xp_pattern[1] == '|')
2495 {
2496 ++xp->xp_pattern;
2497 xp->xp_context = EXPAND_EXPRESSION;
2498 }
2499 else
2500 xp->xp_context = EXPAND_COMMANDS;
2501 }
2502 else
2503 xp->xp_context = EXPAND_EXPRESSION;
2504 }
2505 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506 /* Doesn't look like something valid, expand as an expression
2507 * anyway. */
2508 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 arg = xp->xp_pattern;
2510 if (*arg != NUL)
2511 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2512 /* skip */ ;
2513 }
2514 xp->xp_pattern = arg;
2515}
2516
2517#endif /* FEAT_CMDL_COMPL */
2518
2519/*
2520 * ":1,25call func(arg1, arg2)" function call.
2521 */
2522 void
2523ex_call(eap)
2524 exarg_T *eap;
2525{
2526 char_u *arg = eap->arg;
2527 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 linenr_T lnum;
2533 int doesrange;
2534 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002537 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2538 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 if (tofree == NULL)
2540 return;
2541
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002542 /* Increase refcount on dictionary, it could get deleted when evaluating
2543 * the arguments. */
2544 if (fudi.fd_dict != NULL)
2545 ++fudi.fd_dict->dv_refcount;
2546
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2548 len = STRLEN(tofree);
2549 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
2551 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 if (*startarg != '(')
2555 {
2556 EMSG2(_("E107: Missing braces: %s"), name);
2557 goto end;
2558 }
2559
2560 /*
2561 * When skipping, evaluate the function once, to find the end of the
2562 * arguments.
2563 * When the function takes a range, this is discovered after the first
2564 * call, and the loop is broken.
2565 */
2566 if (eap->skip)
2567 {
2568 ++emsg_skip;
2569 lnum = eap->line2; /* do it once, also with an invalid range */
2570 }
2571 else
2572 lnum = eap->line1;
2573 for ( ; lnum <= eap->line2; ++lnum)
2574 {
2575 if (!eap->skip && eap->addr_count > 0)
2576 {
2577 curwin->w_cursor.lnum = lnum;
2578 curwin->w_cursor.col = 0;
2579 }
2580 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002582 eap->line1, eap->line2, &doesrange,
2583 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 failed = TRUE;
2586 break;
2587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (doesrange || eap->skip)
2590 break;
2591 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002592 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002594 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (aborting())
2596 break;
2597 }
2598 if (eap->skip)
2599 --emsg_skip;
2600
2601 if (!failed)
2602 {
2603 /* Check for trailing illegal characters and a following command. */
2604 if (!ends_excmd(*arg))
2605 {
2606 emsg_severe = TRUE;
2607 EMSG(_(e_trailing));
2608 }
2609 else
2610 eap->nextcmd = check_nextcmd(arg);
2611 }
2612
2613end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002614 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002615 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616}
2617
2618/*
2619 * ":unlet[!] var1 ... " command.
2620 */
2621 void
2622ex_unlet(eap)
2623 exarg_T *eap;
2624{
2625 char_u *arg = eap->arg;
2626 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int error = FALSE;
2628
2629 do
2630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /* Parse the name and find the end. */
2634 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2635 if (lv.ll_name == NULL)
2636 error = TRUE; /* error but continue parsing */
2637 if (name_end == NULL || (!vim_iswhite(*name_end)
2638 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (name_end != NULL)
2641 {
2642 emsg_severe = TRUE;
2643 EMSG(_(e_trailing));
2644 }
2645 if (!(eap->skip || error))
2646 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648 }
2649
2650 if (!error && !eap->skip)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2652 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!eap->skip)
2655 clear_lval(&lv);
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 arg = skipwhite(name_end);
2658 } while (!ends_excmd(*arg));
2659
2660 eap->nextcmd = check_nextcmd(arg);
2661}
2662
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002666 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 int forceit;
2669{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 int ret = OK;
2671 int cc;
2672
2673 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 cc = *name_end;
2676 *name_end = NUL;
2677
2678 /* Normal name or expanded name. */
2679 if (check_changedtick(lp->ll_name))
2680 ret = FAIL;
2681 else if (do_unlet(lp->ll_name) == FAIL && !forceit)
2682 {
2683 EMSG2(_("E108: No such variable: \"%s\""), lp->ll_name);
2684 ret = FAIL;
2685 }
2686 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 else if (lp->ll_range)
2689 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002690 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691
2692 /* Delete a range of List items. */
2693 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2694 {
2695 li = lp->ll_li->li_next;
2696 listitem_remove(lp->ll_list, lp->ll_li);
2697 lp->ll_li = li;
2698 ++lp->ll_n1;
2699 }
2700 }
2701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* unlet a List item. */
2705 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 }
2710
2711 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712}
2713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714/*
2715 * "unlet" a variable. Return OK if it existed, FAIL if not.
2716 */
2717 int
2718do_unlet(name)
2719 char_u *name;
2720{
Bram Moolenaar33570922005-01-25 22:26:29 +00002721 hashtab_T *ht;
2722 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002723 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar33570922005-01-25 22:26:29 +00002725 ht = find_var_ht(name, &varname);
2726 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002728 hi = hash_find(ht, varname);
2729 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002730 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002731 if (!var_check_ro(HI2DI(hi)->di_flags, name))
Bram Moolenaara7043832005-01-21 11:56:39 +00002732 {
2733 delete_var(ht, hi);
2734 return OK;
2735 }
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738 return FAIL;
2739}
2740
2741#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2742/*
2743 * Delete all "menutrans_" variables.
2744 */
2745 void
2746del_menutrans_vars()
2747{
Bram Moolenaar33570922005-01-25 22:26:29 +00002748 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002749 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 hash_lock(&globvarht);
2752 todo = globvarht.ht_used;
2753 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00002754 {
2755 if (!HASHITEM_EMPTY(hi))
2756 {
2757 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002758 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2759 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00002760 }
2761 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002762 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763}
2764#endif
2765
2766#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2767
2768/*
2769 * Local string buffer for the next two functions to store a variable name
2770 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2771 * get_user_var_name().
2772 */
2773
2774static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2775
2776static char_u *varnamebuf = NULL;
2777static int varnamebuflen = 0;
2778
2779/*
2780 * Function to concatenate a prefix and a variable name.
2781 */
2782 static char_u *
2783cat_prefix_varname(prefix, name)
2784 int prefix;
2785 char_u *name;
2786{
2787 int len;
2788
2789 len = (int)STRLEN(name) + 3;
2790 if (len > varnamebuflen)
2791 {
2792 vim_free(varnamebuf);
2793 len += 10; /* some additional space */
2794 varnamebuf = alloc(len);
2795 if (varnamebuf == NULL)
2796 {
2797 varnamebuflen = 0;
2798 return NULL;
2799 }
2800 varnamebuflen = len;
2801 }
2802 *varnamebuf = prefix;
2803 varnamebuf[1] = ':';
2804 STRCPY(varnamebuf + 2, name);
2805 return varnamebuf;
2806}
2807
2808/*
2809 * Function given to ExpandGeneric() to obtain the list of user defined
2810 * (global/buffer/window/built-in) variable names.
2811 */
2812/*ARGSUSED*/
2813 char_u *
2814get_user_var_name(xp, idx)
2815 expand_T *xp;
2816 int idx;
2817{
Bram Moolenaara7043832005-01-21 11:56:39 +00002818 static int gdone;
2819 static int bdone;
2820 static int wdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 static int vidx;
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 static hashitem_T *hi;
2823 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00002826 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00002827
2828 /* Global variables */
2829 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002831 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002832 hi = globvarht.ht_array;
Bram Moolenaara7043832005-01-21 11:56:39 +00002833 while (HASHITEM_EMPTY(hi))
2834 ++hi;
2835 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2836 return cat_prefix_varname('g', hi->hi_key);
2837 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002839
2840 /* b: variables */
2841 ht = &curbuf->b_vars.dv_hashtab;
2842 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002844 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 hi = ht->ht_array;
Bram Moolenaara7043832005-01-21 11:56:39 +00002846 while (HASHITEM_EMPTY(hi))
2847 ++hi;
2848 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002852 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return (char_u *)"b:changedtick";
2854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002855
2856 /* w: variables */
2857 ht = &curwin->w_vars.dv_hashtab;
2858 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002860 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 hi = ht->ht_array;
Bram Moolenaara7043832005-01-21 11:56:39 +00002862 while (HASHITEM_EMPTY(hi))
2863 ++hi;
2864 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002866
2867 /* v: variables */
2868 if (vidx < VV_LEN)
2869 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 vim_free(varnamebuf);
2872 varnamebuf = NULL;
2873 varnamebuflen = 0;
2874 return NULL;
2875}
2876
2877#endif /* FEAT_CMDL_COMPL */
2878
2879/*
2880 * types for expressions.
2881 */
2882typedef enum
2883{
2884 TYPE_UNKNOWN = 0
2885 , TYPE_EQUAL /* == */
2886 , TYPE_NEQUAL /* != */
2887 , TYPE_GREATER /* > */
2888 , TYPE_GEQUAL /* >= */
2889 , TYPE_SMALLER /* < */
2890 , TYPE_SEQUAL /* <= */
2891 , TYPE_MATCH /* =~ */
2892 , TYPE_NOMATCH /* !~ */
2893} exptype_T;
2894
2895/*
2896 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2899 */
2900
2901/*
2902 * Handle zero level expression.
2903 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 * Return OK or FAIL.
2906 */
2907 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 char_u **nextcmd;
2912 int evaluate;
2913{
2914 int ret;
2915 char_u *p;
2916
2917 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (ret == FAIL || !ends_excmd(*p))
2920 {
2921 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 /*
2924 * Report the invalid expression unless the expression evaluation has
2925 * been cancelled due to an aborting error, an interrupt, or an
2926 * exception.
2927 */
2928 if (!aborting())
2929 EMSG2(_(e_invexpr2), arg);
2930 ret = FAIL;
2931 }
2932 if (nextcmd != NULL)
2933 *nextcmd = check_nextcmd(p);
2934
2935 return ret;
2936}
2937
2938/*
2939 * Handle top level expression:
2940 * expr1 ? expr0 : expr0
2941 *
2942 * "arg" must point to the first non-white of the expression.
2943 * "arg" is advanced to the next non-white after the recognized expression.
2944 *
2945 * Return OK or FAIL.
2946 */
2947 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 int evaluate;
2952{
2953 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002954 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
2956 /*
2957 * Get the first variable.
2958 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 return FAIL;
2961
2962 if ((*arg)[0] == '?')
2963 {
2964 result = FALSE;
2965 if (evaluate)
2966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971
2972 /*
2973 * Get the second variable.
2974 */
2975 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 return FAIL;
2978
2979 /*
2980 * Check for the ":".
2981 */
2982 if ((*arg)[0] != ':')
2983 {
2984 EMSG(_("E109: Missing ':' after '?'"));
2985 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002986 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 return FAIL;
2988 }
2989
2990 /*
2991 * Get the third variable.
2992 */
2993 *arg = skipwhite(*arg + 1);
2994 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2995 {
2996 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 return FAIL;
2999 }
3000 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
3004 return OK;
3005}
3006
3007/*
3008 * Handle first level expression:
3009 * expr2 || expr2 || expr2 logical OR
3010 *
3011 * "arg" must point to the first non-white of the expression.
3012 * "arg" is advanced to the next non-white after the recognized expression.
3013 *
3014 * Return OK or FAIL.
3015 */
3016 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 int evaluate;
3021{
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 long result;
3024 int first;
3025
3026 /*
3027 * Get the first variable.
3028 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003029 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 return FAIL;
3031
3032 /*
3033 * Repeat until there is no following "||".
3034 */
3035 first = TRUE;
3036 result = FALSE;
3037 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3038 {
3039 if (evaluate && first)
3040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 first = FALSE;
3045 }
3046
3047 /*
3048 * Get the second variable.
3049 */
3050 *arg = skipwhite(*arg + 2);
3051 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3052 return FAIL;
3053
3054 /*
3055 * Compute the result.
3056 */
3057 if (evaluate && !result)
3058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
3063 if (evaluate)
3064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 rettv->v_type = VAR_NUMBER;
3066 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 }
3068 }
3069
3070 return OK;
3071}
3072
3073/*
3074 * Handle second level expression:
3075 * expr3 && expr3 && expr3 logical AND
3076 *
3077 * "arg" must point to the first non-white of the expression.
3078 * "arg" is advanced to the next non-white after the recognized expression.
3079 *
3080 * Return OK or FAIL.
3081 */
3082 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int evaluate;
3087{
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 long result;
3090 int first;
3091
3092 /*
3093 * Get the first variable.
3094 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 return FAIL;
3097
3098 /*
3099 * Repeat until there is no following "&&".
3100 */
3101 first = TRUE;
3102 result = TRUE;
3103 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3104 {
3105 if (evaluate && first)
3106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 first = FALSE;
3111 }
3112
3113 /*
3114 * Get the second variable.
3115 */
3116 *arg = skipwhite(*arg + 2);
3117 if (eval4(arg, &var2, evaluate && result) == FAIL)
3118 return FAIL;
3119
3120 /*
3121 * Compute the result.
3122 */
3123 if (evaluate && result)
3124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003127 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 if (evaluate)
3130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003131 rettv->v_type = VAR_NUMBER;
3132 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
3134 }
3135
3136 return OK;
3137}
3138
3139/*
3140 * Handle third level expression:
3141 * var1 == var2
3142 * var1 =~ var2
3143 * var1 != var2
3144 * var1 !~ var2
3145 * var1 > var2
3146 * var1 >= var2
3147 * var1 < var2
3148 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003149 * var1 is var2
3150 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 *
3152 * "arg" must point to the first non-white of the expression.
3153 * "arg" is advanced to the next non-white after the recognized expression.
3154 *
3155 * Return OK or FAIL.
3156 */
3157 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003158eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 int evaluate;
3162{
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 char_u *p;
3165 int i;
3166 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003167 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int len = 2;
3169 long n1, n2;
3170 char_u *s1, *s2;
3171 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3172 regmatch_T regmatch;
3173 int ic;
3174 char_u *save_cpo;
3175
3176 /*
3177 * Get the first variable.
3178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 return FAIL;
3181
3182 p = *arg;
3183 switch (p[0])
3184 {
3185 case '=': if (p[1] == '=')
3186 type = TYPE_EQUAL;
3187 else if (p[1] == '~')
3188 type = TYPE_MATCH;
3189 break;
3190 case '!': if (p[1] == '=')
3191 type = TYPE_NEQUAL;
3192 else if (p[1] == '~')
3193 type = TYPE_NOMATCH;
3194 break;
3195 case '>': if (p[1] != '=')
3196 {
3197 type = TYPE_GREATER;
3198 len = 1;
3199 }
3200 else
3201 type = TYPE_GEQUAL;
3202 break;
3203 case '<': if (p[1] != '=')
3204 {
3205 type = TYPE_SMALLER;
3206 len = 1;
3207 }
3208 else
3209 type = TYPE_SEQUAL;
3210 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003211 case 'i': if (p[1] == 's')
3212 {
3213 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3214 len = 5;
3215 if (!vim_isIDc(p[len]))
3216 {
3217 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3218 type_is = TRUE;
3219 }
3220 }
3221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223
3224 /*
3225 * If there is a comparitive operator, use it.
3226 */
3227 if (type != TYPE_UNKNOWN)
3228 {
3229 /* extra question mark appended: ignore case */
3230 if (p[len] == '?')
3231 {
3232 ic = TRUE;
3233 ++len;
3234 }
3235 /* extra '#' appended: match case */
3236 else if (p[len] == '#')
3237 {
3238 ic = FALSE;
3239 ++len;
3240 }
3241 /* nothing appened: use 'ignorecase' */
3242 else
3243 ic = p_ic;
3244
3245 /*
3246 * Get the second variable.
3247 */
3248 *arg = skipwhite(p + len);
3249 if (eval5(arg, &var2, evaluate) == FAIL)
3250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003251 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 return FAIL;
3253 }
3254
3255 if (evaluate)
3256 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003257 if (type_is && rettv->v_type != var2.v_type)
3258 {
3259 /* For "is" a different type always means FALSE, for "notis"
3260 * it means TRUE. */
3261 n1 = (type == TYPE_NEQUAL);
3262 }
3263 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3264 {
3265 if (type_is)
3266 {
3267 n1 = (rettv->v_type == var2.v_type
3268 && rettv->vval.v_list == var2.vval.v_list);
3269 if (type == TYPE_NEQUAL)
3270 n1 = !n1;
3271 }
3272 else if (rettv->v_type != var2.v_type
3273 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3274 {
3275 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003276 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003277 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003278 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003279 clear_tv(rettv);
3280 clear_tv(&var2);
3281 return FAIL;
3282 }
3283 else
3284 {
3285 /* Compare two Lists for being equal or unequal. */
3286 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3287 if (type == TYPE_NEQUAL)
3288 n1 = !n1;
3289 }
3290 }
3291
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003292 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3293 {
3294 if (type_is)
3295 {
3296 n1 = (rettv->v_type == var2.v_type
3297 && rettv->vval.v_dict == var2.vval.v_dict);
3298 if (type == TYPE_NEQUAL)
3299 n1 = !n1;
3300 }
3301 else if (rettv->v_type != var2.v_type
3302 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3303 {
3304 if (rettv->v_type != var2.v_type)
3305 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3306 else
3307 EMSG(_("E736: Invalid operation for Dictionary"));
3308 clear_tv(rettv);
3309 clear_tv(&var2);
3310 return FAIL;
3311 }
3312 else
3313 {
3314 /* Compare two Dictionaries for being equal or unequal. */
3315 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3316 if (type == TYPE_NEQUAL)
3317 n1 = !n1;
3318 }
3319 }
3320
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003321 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3322 {
3323 if (rettv->v_type != var2.v_type
3324 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3325 {
3326 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003327 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003328 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003329 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003330 clear_tv(rettv);
3331 clear_tv(&var2);
3332 return FAIL;
3333 }
3334 else
3335 {
3336 /* Compare two Funcrefs for being equal or unequal. */
3337 if (rettv->vval.v_string == NULL
3338 || var2.vval.v_string == NULL)
3339 n1 = FALSE;
3340 else
3341 n1 = STRCMP(rettv->vval.v_string,
3342 var2.vval.v_string) == 0;
3343 if (type == TYPE_NEQUAL)
3344 n1 = !n1;
3345 }
3346 }
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 /*
3349 * If one of the two variables is a number, compare as a number.
3350 * When using "=~" or "!~", always compare as string.
3351 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003352 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 n1 = get_tv_number(rettv);
3356 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 switch (type)
3358 {
3359 case TYPE_EQUAL: n1 = (n1 == n2); break;
3360 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3361 case TYPE_GREATER: n1 = (n1 > n2); break;
3362 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3363 case TYPE_SMALLER: n1 = (n1 < n2); break;
3364 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3365 case TYPE_UNKNOWN:
3366 case TYPE_MATCH:
3367 case TYPE_NOMATCH: break; /* avoid gcc warning */
3368 }
3369 }
3370 else
3371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003372 s1 = get_tv_string_buf(rettv, buf1);
3373 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3375 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3376 else
3377 i = 0;
3378 n1 = FALSE;
3379 switch (type)
3380 {
3381 case TYPE_EQUAL: n1 = (i == 0); break;
3382 case TYPE_NEQUAL: n1 = (i != 0); break;
3383 case TYPE_GREATER: n1 = (i > 0); break;
3384 case TYPE_GEQUAL: n1 = (i >= 0); break;
3385 case TYPE_SMALLER: n1 = (i < 0); break;
3386 case TYPE_SEQUAL: n1 = (i <= 0); break;
3387
3388 case TYPE_MATCH:
3389 case TYPE_NOMATCH:
3390 /* avoid 'l' flag in 'cpoptions' */
3391 save_cpo = p_cpo;
3392 p_cpo = (char_u *)"";
3393 regmatch.regprog = vim_regcomp(s2,
3394 RE_MAGIC + RE_STRING);
3395 regmatch.rm_ic = ic;
3396 if (regmatch.regprog != NULL)
3397 {
3398 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3399 vim_free(regmatch.regprog);
3400 if (type == TYPE_NOMATCH)
3401 n1 = !n1;
3402 }
3403 p_cpo = save_cpo;
3404 break;
3405
3406 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3407 }
3408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 clear_tv(rettv);
3410 clear_tv(&var2);
3411 rettv->v_type = VAR_NUMBER;
3412 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 }
3415
3416 return OK;
3417}
3418
3419/*
3420 * Handle fourth level expression:
3421 * + number addition
3422 * - number subtraction
3423 * . string concatenation
3424 *
3425 * "arg" must point to the first non-white of the expression.
3426 * "arg" is advanced to the next non-white after the recognized expression.
3427 *
3428 * Return OK or FAIL.
3429 */
3430 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 int evaluate;
3435{
Bram Moolenaar33570922005-01-25 22:26:29 +00003436 typval_T var2;
3437 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 int op;
3439 long n1, n2;
3440 char_u *s1, *s2;
3441 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3442 char_u *p;
3443
3444 /*
3445 * Get the first variable.
3446 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003447 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 return FAIL;
3449
3450 /*
3451 * Repeat computing, until no '+', '-' or '.' is following.
3452 */
3453 for (;;)
3454 {
3455 op = **arg;
3456 if (op != '+' && op != '-' && op != '.')
3457 break;
3458
3459 /*
3460 * Get the second variable.
3461 */
3462 *arg = skipwhite(*arg + 1);
3463 if (eval6(arg, &var2, evaluate) == FAIL)
3464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return FAIL;
3467 }
3468
3469 if (evaluate)
3470 {
3471 /*
3472 * Compute the result.
3473 */
3474 if (op == '.')
3475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 s1 = get_tv_string_buf(rettv, buf1);
3477 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003479 clear_tv(rettv);
3480 rettv->v_type = VAR_STRING;
3481 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003483 else if (op == '+' && rettv->v_type == VAR_LIST
3484 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003485 {
3486 /* concatenate Lists */
3487 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3488 &var3) == FAIL)
3489 {
3490 clear_tv(rettv);
3491 clear_tv(&var2);
3492 return FAIL;
3493 }
3494 clear_tv(rettv);
3495 *rettv = var3;
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 else
3498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 n1 = get_tv_number(rettv);
3500 n2 = get_tv_number(&var2);
3501 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (op == '+')
3503 n1 = n1 + n2;
3504 else
3505 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003506 rettv->v_type = VAR_NUMBER;
3507 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 }
3512 return OK;
3513}
3514
3515/*
3516 * Handle fifth level expression:
3517 * * number multiplication
3518 * / number division
3519 * % number modulo
3520 *
3521 * "arg" must point to the first non-white of the expression.
3522 * "arg" is advanced to the next non-white after the recognized expression.
3523 *
3524 * Return OK or FAIL.
3525 */
3526 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 int evaluate;
3531{
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int op;
3534 long n1, n2;
3535
3536 /*
3537 * Get the first variable.
3538 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541
3542 /*
3543 * Repeat computing, until no '*', '/' or '%' is following.
3544 */
3545 for (;;)
3546 {
3547 op = **arg;
3548 if (op != '*' && op != '/' && op != '%')
3549 break;
3550
3551 if (evaluate)
3552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 n1 = get_tv_number(rettv);
3554 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556 else
3557 n1 = 0;
3558
3559 /*
3560 * Get the second variable.
3561 */
3562 *arg = skipwhite(*arg + 1);
3563 if (eval7(arg, &var2, evaluate) == FAIL)
3564 return FAIL;
3565
3566 if (evaluate)
3567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003568 n2 = get_tv_number(&var2);
3569 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571 /*
3572 * Compute the result.
3573 */
3574 if (op == '*')
3575 n1 = n1 * n2;
3576 else if (op == '/')
3577 {
3578 if (n2 == 0) /* give an error message? */
3579 n1 = 0x7fffffffL;
3580 else
3581 n1 = n1 / n2;
3582 }
3583 else
3584 {
3585 if (n2 == 0) /* give an error message? */
3586 n1 = 0;
3587 else
3588 n1 = n1 % n2;
3589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003590 rettv->v_type = VAR_NUMBER;
3591 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593 }
3594
3595 return OK;
3596}
3597
3598/*
3599 * Handle sixth level expression:
3600 * number number constant
3601 * "string" string contstant
3602 * 'string' literal string contstant
3603 * &option-name option value
3604 * @r register contents
3605 * identifier variable value
3606 * function() function call
3607 * $VAR environment variable
3608 * (expression) nested expression
3609 *
3610 * Also handle:
3611 * ! in front logical NOT
3612 * - in front unary minus
3613 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 * trailing [] subscript in String or List
3615 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 *
3617 * "arg" must point to the first non-white of the expression.
3618 * "arg" is advanced to the next non-white after the recognized expression.
3619 *
3620 * Return OK or FAIL.
3621 */
3622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 int evaluate;
3627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 long n;
3629 int len;
3630 char_u *s;
3631 int val;
3632 char_u *start_leader, *end_leader;
3633 int ret = OK;
3634 char_u *alias;
Bram Moolenaar33570922005-01-25 22:26:29 +00003635 dict_T *selfdict;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003639 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 /*
3644 * Skip '!' and '-' characters. They are handled later.
3645 */
3646 start_leader = *arg;
3647 while (**arg == '!' || **arg == '-' || **arg == '+')
3648 *arg = skipwhite(*arg + 1);
3649 end_leader = *arg;
3650
3651 switch (**arg)
3652 {
3653 /*
3654 * Number constant.
3655 */
3656 case '0':
3657 case '1':
3658 case '2':
3659 case '3':
3660 case '4':
3661 case '5':
3662 case '6':
3663 case '7':
3664 case '8':
3665 case '9':
3666 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3667 *arg += len;
3668 if (evaluate)
3669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 rettv->v_type = VAR_NUMBER;
3671 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 break;
3674
3675 /*
3676 * String constant: "string".
3677 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 break;
3680
3681 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003682 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003685 break;
3686
3687 /*
3688 * List: [expr, expr]
3689 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 break;
3692
3693 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 * Dictionary: {key: val, key: val}
3695 */
3696 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3697 break;
3698
3699 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003700 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003702 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 break;
3704
3705 /*
3706 * Environment variable: $VAR.
3707 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 break;
3710
3711 /*
3712 * Register contents: @r.
3713 */
3714 case '@': ++*arg;
3715 if (evaluate)
3716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717 rettv->v_type = VAR_STRING;
3718 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 if (**arg != NUL)
3721 ++*arg;
3722 break;
3723
3724 /*
3725 * nested expression: (expression).
3726 */
3727 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (**arg == ')')
3730 ++*arg;
3731 else if (ret == OK)
3732 {
3733 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 ret = FAIL;
3736 }
3737 break;
3738
Bram Moolenaar8c711452005-01-14 21:53:12 +00003739 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 break;
3741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003742
3743 if (ret == NOTDONE)
3744 {
3745 /*
3746 * Must be a variable or function name.
3747 * Can also be a curly-braces kind of name: {expr}.
3748 */
3749 s = *arg;
Bram Moolenaara7043832005-01-21 11:56:39 +00003750 len = get_name_len(arg, &alias, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003751 if (alias != NULL)
3752 s = alias;
3753
3754 if (len == 0)
3755 ret = FAIL;
3756 else
3757 {
3758 if (**arg == '(') /* recursive! */
3759 {
3760 /* If "s" is the name of a variable of type VAR_FUNC
3761 * use its contents. */
3762 s = deref_func_name(s, &len);
3763
3764 /* Invoke the function. */
3765 ret = get_func_tv(s, len, rettv, arg,
3766 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003767 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003768 /* Stop the expression evaluation when immediately
3769 * aborting on error, or when an interrupt occurred or
3770 * an exception was thrown but not caught. */
3771 if (aborting())
3772 {
3773 if (ret == OK)
3774 clear_tv(rettv);
3775 ret = FAIL;
3776 }
3777 }
3778 else if (evaluate)
3779 ret = get_var_tv(s, len, rettv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003780 else
3781 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003782 }
3783
3784 if (alias != NULL)
3785 vim_free(alias);
3786 }
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 *arg = skipwhite(*arg);
3789
3790 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003791 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaare9a41262005-01-15 22:18:47 +00003792 * Also handle function call with Funcref variable: func(expr)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003793 * Can all be combined: dict.func(expr)[idx]['func'](expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003795 selfdict = NULL;
3796 while (ret == OK
3797 && (**arg == '['
3798 || (**arg == '.' && rettv->v_type == VAR_DICT)
3799 || (**arg == '(' && rettv->v_type == VAR_FUNC))
3800 && !vim_iswhite(*(*arg - 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003802 if (**arg == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003804 s = rettv->vval.v_string;
3805
3806 /* Invoke the function. Recursive! */
3807 ret = get_func_tv(s, STRLEN(s), rettv, arg,
3808 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3809 &len, evaluate, selfdict);
3810
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003811 /* Stop the expression evaluation when immediately aborting on
3812 * error, or when an interrupt occurred or an exception was thrown
3813 * but not caught. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003814 if (aborting())
3815 {
3816 if (ret == OK)
3817 clear_tv(rettv);
3818 ret = FAIL;
3819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003820 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003821 selfdict = NULL;
3822 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003823 else /* **arg == '[' || **arg == '.' */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003824 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003825 dict_unref(selfdict);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003826 if (rettv->v_type == VAR_DICT)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003827 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00003828 selfdict = rettv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003829 if (selfdict != NULL)
3830 ++selfdict->dv_refcount;
3831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003832 else
3833 selfdict = NULL;
3834 if (eval_index(arg, rettv, evaluate) == FAIL)
3835 {
3836 clear_tv(rettv);
3837 ret = FAIL;
3838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003841 dict_unref(selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 /*
3844 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3845 */
3846 if (ret == OK && evaluate && end_leader > start_leader)
3847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 while (end_leader > start_leader)
3850 {
3851 --end_leader;
3852 if (*end_leader == '!')
3853 val = !val;
3854 else if (*end_leader == '-')
3855 val = -val;
3856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 clear_tv(rettv);
3858 rettv->v_type = VAR_NUMBER;
3859 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861
3862 return ret;
3863}
3864
3865/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003866 * Evaluate an "[expr]" or "[expr:expr]" index.
3867 * "*arg" points to the '['.
3868 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3869 */
3870 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003872 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003874 int evaluate;
3875{
3876 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003877 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003878 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003879 long len = -1;
3880 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003881 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003882 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003885 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003886 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003887 return FAIL;
3888 }
3889
Bram Moolenaar8c711452005-01-14 21:53:12 +00003890 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003892 /*
3893 * dict.name
3894 */
3895 key = *arg + 1;
3896 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3897 ;
3898 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003899 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003900 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003901 }
3902 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003904 /*
3905 * something[idx]
3906 *
3907 * Get the (first) variable from inside the [].
3908 */
3909 *arg = skipwhite(*arg + 1);
3910 if (**arg == ':')
3911 empty1 = TRUE;
3912 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3913 return FAIL;
3914
3915 /*
3916 * Get the second variable from inside the [:].
3917 */
3918 if (**arg == ':')
3919 {
3920 range = TRUE;
3921 *arg = skipwhite(*arg + 1);
3922 if (**arg == ']')
3923 empty2 = TRUE;
3924 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3925 {
3926 clear_tv(&var1);
3927 return FAIL;
3928 }
3929 }
3930
3931 /* Check for the ']'. */
3932 if (**arg != ']')
3933 {
3934 EMSG(_(e_missbrac));
3935 clear_tv(&var1);
3936 if (range)
3937 clear_tv(&var2);
3938 return FAIL;
3939 }
3940 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003941 }
3942
3943 if (evaluate)
3944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003945 n1 = 0;
3946 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 n1 = get_tv_number(&var1);
3949 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003950 }
3951 if (range)
3952 {
3953 if (empty2)
3954 n2 = -1;
3955 else
3956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 n2 = get_tv_number(&var2);
3958 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003959 }
3960 }
3961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003963 {
3964 case VAR_NUMBER:
3965 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967 len = (long)STRLEN(s);
3968 if (range)
3969 {
3970 /* The resulting variable is a substring. If the indexes
3971 * are out of range the result is empty. */
3972 if (n1 < 0)
3973 {
3974 n1 = len + n1;
3975 if (n1 < 0)
3976 n1 = 0;
3977 }
3978 if (n2 < 0)
3979 n2 = len + n2;
3980 else if (n2 >= len)
3981 n2 = len;
3982 if (n1 >= len || n2 < 0 || n1 > n2)
3983 s = NULL;
3984 else
3985 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3986 }
3987 else
3988 {
3989 /* The resulting variable is a string of a single
3990 * character. If the index is too big or negative the
3991 * result is empty. */
3992 if (n1 >= len || n1 < 0)
3993 s = NULL;
3994 else
3995 s = vim_strnsave(s + n1, 1);
3996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 clear_tv(rettv);
3998 rettv->v_type = VAR_STRING;
3999 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000 break;
4001
4002 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004003 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004004 if (n1 < 0)
4005 n1 = len + n1;
4006 if (!empty1 && (n1 < 0 || n1 >= len))
4007 {
4008 EMSGN(_(e_listidx), n1);
4009 return FAIL;
4010 }
4011 if (range)
4012 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 list_T *l;
4014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004015
4016 if (n2 < 0)
4017 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004019 {
4020 EMSGN(_(e_listidx), n2);
4021 return FAIL;
4022 }
4023 l = list_alloc();
4024 if (l == NULL)
4025 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004027 n1 <= n2; ++n1)
4028 {
4029 if (list_append_tv(l, &item->li_tv) == FAIL)
4030 {
4031 list_free(l);
4032 return FAIL;
4033 }
4034 item = item->li_next;
4035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 clear_tv(rettv);
4037 rettv->v_type = VAR_LIST;
4038 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004039 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004040 }
4041 else
4042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004044 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 clear_tv(rettv);
4046 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004047 }
4048 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049
4050 case VAR_DICT:
4051 if (range)
4052 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004053 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054 if (len == -1)
4055 clear_tv(&var1);
4056 return FAIL;
4057 }
4058 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060
4061 if (len == -1)
4062 {
4063 key = get_tv_string(&var1);
4064 if (*key == NUL)
4065 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004066 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067 clear_tv(&var1);
4068 return FAIL;
4069 }
4070 }
4071
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004072 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073
4074 if (item == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004075 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 if (len == -1)
4077 clear_tv(&var1);
4078 if (item == NULL)
4079 return FAIL;
4080
4081 copy_tv(&item->di_tv, &var1);
4082 clear_tv(rettv);
4083 *rettv = var1;
4084 }
4085 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004086 }
4087 }
4088
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004089 return OK;
4090}
4091
4092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * Get an option value.
4094 * "arg" points to the '&' or '+' before the option name.
4095 * "arg" is advanced to character after the option name.
4096 * Return OK or FAIL.
4097 */
4098 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004101 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 int evaluate;
4103{
4104 char_u *option_end;
4105 long numval;
4106 char_u *stringval;
4107 int opt_type;
4108 int c;
4109 int working = (**arg == '+'); /* has("+option") */
4110 int ret = OK;
4111 int opt_flags;
4112
4113 /*
4114 * Isolate the option name and find its value.
4115 */
4116 option_end = find_option_end(arg, &opt_flags);
4117 if (option_end == NULL)
4118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 EMSG2(_("E112: Option name missing: %s"), *arg);
4121 return FAIL;
4122 }
4123
4124 if (!evaluate)
4125 {
4126 *arg = option_end;
4127 return OK;
4128 }
4129
4130 c = *option_end;
4131 *option_end = NUL;
4132 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
4135 if (opt_type == -3) /* invalid name */
4136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 EMSG2(_("E113: Unknown option: %s"), *arg);
4139 ret = FAIL;
4140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
4143 if (opt_type == -2) /* hidden string option */
4144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 rettv->v_type = VAR_STRING;
4146 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148 else if (opt_type == -1) /* hidden number option */
4149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 rettv->v_type = VAR_NUMBER;
4151 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 else if (opt_type == 1) /* number option */
4154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 rettv->v_type = VAR_NUMBER;
4156 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158 else /* string option */
4159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 rettv->v_type = VAR_STRING;
4161 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 }
4164 else if (working && (opt_type == -2 || opt_type == -1))
4165 ret = FAIL;
4166
4167 *option_end = c; /* put back for error messages */
4168 *arg = option_end;
4169
4170 return ret;
4171}
4172
4173/*
4174 * Allocate a variable for a string constant.
4175 * Return OK or FAIL.
4176 */
4177 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int evaluate;
4182{
4183 char_u *p;
4184 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 int extra = 0;
4186
4187 /*
4188 * Find the end of the string, skipping backslashed characters.
4189 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004190 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192 if (*p == '\\' && p[1] != NUL)
4193 {
4194 ++p;
4195 /* A "\<x>" form occupies at least 4 characters, and produces up
4196 * to 6 characters: reserve space for 2 extra */
4197 if (*p == '<')
4198 extra += 2;
4199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201
4202 if (*p != '"')
4203 {
4204 EMSG2(_("E114: Missing quote: %s"), *arg);
4205 return FAIL;
4206 }
4207
4208 /* If only parsing, set *arg and return here */
4209 if (!evaluate)
4210 {
4211 *arg = p + 1;
4212 return OK;
4213 }
4214
4215 /*
4216 * Copy the string into allocated memory, handling backslashed
4217 * characters.
4218 */
4219 name = alloc((unsigned)(p - *arg + extra));
4220 if (name == NULL)
4221 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004222 rettv->v_type = VAR_STRING;
4223 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
Bram Moolenaar8c711452005-01-14 21:53:12 +00004225 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 if (*p == '\\')
4228 {
4229 switch (*++p)
4230 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 case 'b': *name++ = BS; ++p; break;
4232 case 'e': *name++ = ESC; ++p; break;
4233 case 'f': *name++ = FF; ++p; break;
4234 case 'n': *name++ = NL; ++p; break;
4235 case 'r': *name++ = CAR; ++p; break;
4236 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 case 'X': /* hex: "\x1", "\x12" */
4239 case 'x':
4240 case 'u': /* Unicode: "\u0023" */
4241 case 'U':
4242 if (vim_isxdigit(p[1]))
4243 {
4244 int n, nr;
4245 int c = toupper(*p);
4246
4247 if (c == 'X')
4248 n = 2;
4249 else
4250 n = 4;
4251 nr = 0;
4252 while (--n >= 0 && vim_isxdigit(p[1]))
4253 {
4254 ++p;
4255 nr = (nr << 4) + hex2nr(*p);
4256 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004257 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#ifdef FEAT_MBYTE
4259 /* For "\u" store the number according to
4260 * 'encoding'. */
4261 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 else
4264#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004265 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268
4269 /* octal: "\1", "\12", "\123" */
4270 case '0':
4271 case '1':
4272 case '2':
4273 case '3':
4274 case '4':
4275 case '5':
4276 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004277 case '7': *name = *p++ - '0';
4278 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004280 *name = (*name << 3) + *p++ - '0';
4281 if (*p >= '0' && *p <= '7')
4282 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004284 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 break;
4286
4287 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004288 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (extra != 0)
4290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004291 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293 }
4294 /* FALLTHROUGH */
4295
Bram Moolenaar8c711452005-01-14 21:53:12 +00004296 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 break;
4298 }
4299 }
4300 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 *arg = p + 1;
4306
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return OK;
4308}
4309
4310/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * Return OK or FAIL.
4313 */
4314 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 int evaluate;
4319{
4320 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004321 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004322 int reduce = 0;
4323
4324 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004326 */
4327 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4328 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004329 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004330 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004331 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004332 break;
4333 ++reduce;
4334 ++p;
4335 }
4336 }
4337
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004340 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004341 return FAIL;
4342 }
4343
Bram Moolenaar8c711452005-01-14 21:53:12 +00004344 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004345 if (!evaluate)
4346 {
4347 *arg = p + 1;
4348 return OK;
4349 }
4350
4351 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004353 */
4354 str = alloc((unsigned)((p - *arg) - reduce));
4355 if (str == NULL)
4356 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004357 rettv->v_type = VAR_STRING;
4358 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004359
Bram Moolenaar8c711452005-01-14 21:53:12 +00004360 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004363 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004364 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004365 break;
4366 ++p;
4367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004369 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004371 *arg = p + 1;
4372
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004373 return OK;
4374}
4375
4376/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004377 * Allocate a variable for a List and fill it from "*arg".
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004382 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004384 int evaluate;
4385{
Bram Moolenaar33570922005-01-25 22:26:29 +00004386 list_T *l = NULL;
4387 typval_T tv;
4388 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389
4390 if (evaluate)
4391 {
4392 l = list_alloc();
4393 if (l == NULL)
4394 return FAIL;
4395 }
4396
4397 *arg = skipwhite(*arg + 1);
4398 while (**arg != ']' && **arg != NUL)
4399 {
4400 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4401 goto failret;
4402 if (evaluate)
4403 {
4404 item = listitem_alloc();
4405 if (item != NULL)
4406 {
4407 item->li_tv = tv;
4408 list_append(l, item);
4409 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004410 else
4411 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004412 }
4413
4414 if (**arg == ']')
4415 break;
4416 if (**arg != ',')
4417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004419 goto failret;
4420 }
4421 *arg = skipwhite(*arg + 1);
4422 }
4423
4424 if (**arg != ']')
4425 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004426 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427failret:
4428 if (evaluate)
4429 list_free(l);
4430 return FAIL;
4431 }
4432
4433 *arg = skipwhite(*arg + 1);
4434 if (evaluate)
4435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 rettv->v_type = VAR_LIST;
4437 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 ++l->lv_refcount;
4439 }
4440
4441 return OK;
4442}
4443
4444/*
4445 * Allocate an empty header for a list.
4446 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004447 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448list_alloc()
4449{
Bram Moolenaar33570922005-01-25 22:26:29 +00004450 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451}
4452
4453/*
4454 * Unreference a list: decrement the reference count and free it when it
4455 * becomes zero.
4456 */
4457 static void
4458list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004459 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460{
4461 if (l != NULL && --l->lv_refcount <= 0)
4462 list_free(l);
4463}
4464
4465/*
4466 * Free a list, including all items it points to.
4467 * Ignores the reference count.
4468 */
4469 static void
4470list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004471 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004472{
Bram Moolenaar33570922005-01-25 22:26:29 +00004473 listitem_T *item;
4474 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475
4476 for (item = l->lv_first; item != NULL; item = next)
4477 {
4478 next = item->li_next;
4479 listitem_free(item);
4480 }
4481 vim_free(l);
4482}
4483
4484/*
4485 * Allocate a list item.
4486 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004487 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488listitem_alloc()
4489{
Bram Moolenaar33570922005-01-25 22:26:29 +00004490 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491}
4492
4493/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004494 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 */
4496 static void
4497listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004498 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 vim_free(item);
4502}
4503
4504/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004505 * Remove a list item from a List and free it. Also clears the value.
4506 */
4507 static void
4508listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004509 list_T *l;
4510 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004511{
4512 list_remove(l, item, item);
4513 listitem_free(item);
4514}
4515
4516/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 * Get the number of items in a list.
4518 */
4519 static long
4520list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004521 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522{
Bram Moolenaar33570922005-01-25 22:26:29 +00004523 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 long len = 0;
4525
4526 if (l == NULL)
4527 return 0L;
4528 for (item = l->lv_first; item != NULL; item = item->li_next)
4529 ++len;
4530 return len;
4531}
4532
4533/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 * Return TRUE when two lists have exactly the same values.
4535 */
4536 static int
4537list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004538 list_T *l1;
4539 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540 int ic; /* ignore case for strings */
4541{
Bram Moolenaar33570922005-01-25 22:26:29 +00004542 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004544 if (list_len(l1) != list_len(l2))
4545 return FALSE;
4546
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 for (item1 = l1->lv_first, item2 = l2->lv_first;
4548 item1 != NULL && item2 != NULL;
4549 item1 = item1->li_next, item2 = item2->li_next)
4550 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4551 return FALSE;
4552 return item1 == NULL && item2 == NULL;
4553}
4554
4555/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004556 * Return TRUE when two dictionaries have exactly the same key/values.
4557 */
4558 static int
4559dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004560 dict_T *d1;
4561 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004562 int ic; /* ignore case for strings */
4563{
Bram Moolenaar33570922005-01-25 22:26:29 +00004564 hashitem_T *hi;
4565 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004566 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004567
4568 if (dict_len(d1) != dict_len(d2))
4569 return FALSE;
4570
Bram Moolenaar33570922005-01-25 22:26:29 +00004571 todo = d1->dv_hashtab.ht_used;
4572 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004574 if (!HASHITEM_EMPTY(hi))
4575 {
4576 item2 = dict_find(d2, hi->hi_key, -1);
4577 if (item2 == NULL)
4578 return FALSE;
4579 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4580 return FALSE;
4581 --todo;
4582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004583 }
4584 return TRUE;
4585}
4586
4587/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004588 * Return TRUE if "tv1" and "tv2" have the same value.
4589 * Compares the items just like "==" would compare them.
4590 */
4591 static int
4592tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T *tv1;
4594 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004595 int ic; /* ignore case */
4596{
4597 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4598
4599 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4600 {
4601 /* recursive! */
4602 if (tv1->v_type != tv2->v_type
4603 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4604 return FALSE;
4605 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004606 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4607 {
4608 /* recursive! */
4609 if (tv1->v_type != tv2->v_type
4610 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4611 return FALSE;
4612 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004613 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4614 {
4615 if (tv1->v_type != tv2->v_type
4616 || tv1->vval.v_string == NULL
4617 || tv2->vval.v_string == NULL
4618 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4619 return FALSE;
4620 }
4621 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4622 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004623 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4624 * Don't consider "4x" to be equal to 4. */
4625 if ((tv1->v_type == VAR_STRING
4626 && !string_isa_number(tv1->vval.v_string))
4627 || (tv2->v_type == VAR_STRING
4628 && !string_isa_number(tv2->vval.v_string)))
4629 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004630 if (get_tv_number(tv1) != get_tv_number(tv2))
4631 return FALSE;
4632 }
4633 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4634 get_tv_string_buf(tv2, buf2)) != 0)
4635 return FALSE;
4636 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4637 get_tv_string_buf(tv2, buf2)) != 0)
4638 return FALSE;
4639 return TRUE;
4640}
4641
4642/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004643 * Return TRUE if "tv" is a number without other non-white characters.
4644 */
4645 static int
4646string_isa_number(s)
4647 char_u *s;
4648{
4649 int len;
4650
4651 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4652 return len > 0 && *skipwhite(s + len) == NUL;
4653}
4654
4655/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656 * Locate item with index "n" in list "l" and return it.
4657 * A negative index is counted from the end; -1 is the last item.
4658 * Returns NULL when "n" is out of range.
4659 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004662 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004663 long n;
4664{
Bram Moolenaar33570922005-01-25 22:26:29 +00004665 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 long idx;
4667
4668 if (l == NULL)
4669 return NULL;
4670 if (n < 0)
4671 {
4672 idx = -1; /* search from the end */
4673 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4674 --idx;
4675 }
4676 else
4677 {
4678 idx = 0; /* search from the start */
4679 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4680 ++idx;
4681 }
4682 if (idx != n)
4683 return NULL;
4684 return item;
4685}
4686
4687/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004688 * Locate "item" list "l" and return its index.
4689 * Returns -1 when "item" is not in the list.
4690 */
4691 static long
4692list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 list_T *l;
4694 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004695{
4696 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00004697 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004698
4699 if (l == NULL)
4700 return -1;
4701 idx = 0;
4702 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4703 ++idx;
4704 if (li == NULL)
4705 return -1;
4706 return idx;;
4707}
4708
4709/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004710 * Like list_find(), but also find an item just past the end.
4711 * "*ip" is the item to find.
4712 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4713 * Returns NULL when item not found or item is just past the end.
4714 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004715 static listitem_T *
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004716list_find_ext(l, ip)
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004718 long *ip;
4719{
4720 long n;
Bram Moolenaar33570922005-01-25 22:26:29 +00004721 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004722
4723 if (*ip < 0)
4724 {
4725 /* Count from the end: -1 is before last item. */
4726 item = l->lv_last;
4727 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4728 item = item->li_prev;
4729 if (item == NULL)
4730 n = 1; /* error! */
4731 }
4732 else
4733 {
4734 item = l->lv_first;
4735 for (n = *ip; n > 0 && item != NULL; --n)
4736 item = item->li_next;
4737 }
4738 *ip = n;
4739 return item;
4740}
4741
4742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 * Append item "item" to the end of list "l".
4744 */
4745 static void
4746list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004747 list_T *l;
4748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749{
4750 if (l->lv_last == NULL)
4751 {
4752 /* empty list */
4753 l->lv_first = item;
4754 l->lv_last = item;
4755 item->li_prev = NULL;
4756 }
4757 else
4758 {
4759 l->lv_last->li_next = item;
4760 item->li_prev = l->lv_last;
4761 l->lv_last = item;
4762 }
4763 item->li_next = NULL;
4764}
4765
4766/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004768 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 */
4770 static int
4771list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004772 list_T *l;
4773 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774{
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 listitem_T *ni = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776
4777 if (ni == NULL)
4778 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 list_append(l, ni);
4781 return OK;
4782}
4783
4784/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004786 * If "item" is NULL append at the end.
4787 * Return FAIL when out of memory.
4788 */
4789 static int
4790list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004791 list_T *l;
4792 typval_T *tv;
4793 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004794{
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004796
4797 if (ni == NULL)
4798 return FAIL;
4799 copy_tv(tv, &ni->li_tv);
4800 if (item == NULL)
4801 /* Append new item at end of list. */
4802 list_append(l, ni);
4803 else
4804 {
4805 /* Insert new item before existing item. */
4806 ni->li_prev = item->li_prev;
4807 ni->li_next = item;
4808 if (item->li_prev == NULL)
4809 l->lv_first = ni;
4810 else
4811 item->li_prev->li_next = ni;
4812 item->li_prev = ni;
4813 }
4814 return OK;
4815}
4816
4817/*
4818 * Extend "l1" with "l2".
4819 * If "bef" is NULL append at the end, otherwise insert before this item.
4820 * Returns FAIL when out of memory.
4821 */
4822 static int
4823list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00004824 list_T *l1;
4825 list_T *l2;
4826 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004827{
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004829
4830 for (item = l2->lv_first; item != NULL; item = item->li_next)
4831 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4832 return FAIL;
4833 return OK;
4834}
4835
4836/*
4837 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4838 * Return FAIL when out of memory.
4839 */
4840 static int
4841list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004842 list_T *l1;
4843 list_T *l2;
4844 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004845{
Bram Moolenaar33570922005-01-25 22:26:29 +00004846 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004847
4848 /* make a copy of the first list. */
4849 l = list_copy(l1, FALSE);
4850 if (l == NULL)
4851 return FAIL;
4852 tv->v_type = VAR_LIST;
4853 tv->vval.v_list = l;
4854
4855 /* append all items from the second list */
4856 return list_extend(l, l2, NULL);
4857}
4858
4859/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004860 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 * The refcount of the new list is set to 1.
4862 * Returns NULL when out of memory.
4863 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004864 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865list_copy(orig, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +00004866 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004867 int deep;
4868{
Bram Moolenaar33570922005-01-25 22:26:29 +00004869 list_T *copy;
4870 listitem_T *item;
4871 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872
4873 if (orig == NULL)
4874 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875
4876 copy = list_alloc();
4877 if (copy != NULL)
4878 {
4879 for (item = orig->lv_first; item != NULL; item = item->li_next)
4880 {
4881 ni = listitem_alloc();
4882 if (ni == NULL)
4883 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004884 if (deep)
4885 item_copy(&item->li_tv, &ni->li_tv, deep);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 list_append(copy, ni);
4889 }
4890 ++copy->lv_refcount;
4891 }
4892
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 return copy;
4894}
4895
4896/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004897 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004898 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004900 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004901list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00004902 list_T *l;
4903 listitem_T *item;
4904 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004905{
Bram Moolenaar33570922005-01-25 22:26:29 +00004906 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004908 /* notify watchers */
4909 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004911 list_fix_watch(l, ip);
4912 if (ip == item2)
4913 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004915
4916 if (item2->li_next == NULL)
4917 l->lv_last = item->li_prev;
4918 else
4919 item2->li_next->li_prev = item->li_prev;
4920 if (item->li_prev == NULL)
4921 l->lv_first = item2->li_next;
4922 else
4923 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924}
4925
4926/*
4927 * Return an allocated string with the string representation of a list.
4928 * May return NULL.
4929 */
4930 static char_u *
4931list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004932 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933{
4934 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935
4936 if (tv->vval.v_list == NULL)
4937 return NULL;
4938 ga_init2(&ga, (int)sizeof(char), 80);
4939 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004940 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 ga_append(&ga, ']');
4942 ga_append(&ga, NUL);
4943 return (char_u *)ga.ga_data;
4944}
4945
4946/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004947 * Join list "l" into a string in "*gap", using separator "sep".
4948 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4949 */
4950 static void
4951list_join(gap, l, sep, echo)
4952 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00004953 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004954 char_u *sep;
4955 int echo;
4956{
4957 int first = TRUE;
4958 char_u *tofree;
4959 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00004960 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004961 char_u *s;
4962
4963 for (item = l->lv_first; item != NULL; item = item->li_next)
4964 {
4965 if (first)
4966 first = FALSE;
4967 else
4968 ga_concat(gap, sep);
4969
4970 if (echo)
4971 s = echo_string(&item->li_tv, &tofree, numbuf);
4972 else
4973 s = tv2string(&item->li_tv, &tofree, numbuf);
4974 if (s != NULL)
4975 ga_concat(gap, s);
4976 vim_free(tofree);
4977 }
4978}
4979
4980/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 * Allocate an empty header for a dictionary.
4982 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 static dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984dict_alloc()
4985{
Bram Moolenaar33570922005-01-25 22:26:29 +00004986 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004987
Bram Moolenaar33570922005-01-25 22:26:29 +00004988 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004989 if (d != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00004990 hash_init(&d->dv_hashtab);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004991 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992}
4993
4994/*
4995 * Unreference a Dictionary: decrement the reference count and free it when it
4996 * becomes zero.
4997 */
4998 static void
4999dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005000 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001{
5002 if (d != NULL && --d->dv_refcount <= 0)
5003 dict_free(d);
5004}
5005
5006/*
5007 * Free a Dictionary, including all items it contains.
5008 * Ignores the reference count.
5009 */
5010 static void
5011dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005012 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005014 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005015 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005017 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005018 * hashtab. Must not try to resize the hashtab! */
5019 todo = d->dv_hashtab.ht_used;
5020 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005022 if (!HASHITEM_EMPTY(hi))
5023 {
5024 dictitem_free(HI2DI(hi));
5025 --todo;
5026 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005028 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 vim_free(d);
5030}
5031
5032/*
5033 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005034 * The "key" is copied to the new item.
5035 * Note that the value of the item "di_tv" still needs to be initialized!
5036 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005038 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005039dictitem_alloc(key)
5040 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041{
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005043
Bram Moolenaar33570922005-01-25 22:26:29 +00005044 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005045 if (di != NULL)
5046 STRCPY(di->di_key, key);
5047 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048}
5049
5050/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005051 * Make a copy of a Dictionary item.
5052 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005053 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005054dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005056{
Bram Moolenaar33570922005-01-25 22:26:29 +00005057 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005058
Bram Moolenaar33570922005-01-25 22:26:29 +00005059 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 if (di != NULL)
5061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005062 STRCPY(di->di_key, org->di_key);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005063 copy_tv(&org->di_tv, &di->di_tv);
5064 }
5065 return di;
5066}
5067
5068/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005069 * Remove item "item" from Dictionary "dict" and free it.
5070 */
5071 static void
5072dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005073 dict_T *dict;
5074 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005075{
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005077
Bram Moolenaar33570922005-01-25 22:26:29 +00005078 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005079 if (HASHITEM_EMPTY(hi))
5080 EMSG2(_(e_intern2), "dictitem_remove()");
5081 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005082 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005083 dictitem_free(item);
5084}
5085
5086/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 * Free a dict item. Also clears the value.
5088 */
5089 static void
5090dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005091 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 clear_tv(&item->di_tv);
5094 vim_free(item);
5095}
5096
5097/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5099 * The refcount of the new dict is set to 1.
5100 * Returns NULL when out of memory.
5101 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005102 static dict_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103dict_copy(orig, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +00005104 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005105 int deep;
5106{
Bram Moolenaar33570922005-01-25 22:26:29 +00005107 dict_T *copy;
5108 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005109 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005110 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111
5112 if (orig == NULL)
5113 return NULL;
5114
5115 copy = dict_alloc();
5116 if (copy != NULL)
5117 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005118 todo = orig->dv_hashtab.ht_used;
5119 for (hi = orig->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005120 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005121 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005123 --todo;
5124
5125 di = dictitem_alloc(hi->hi_key);
5126 if (di == NULL)
5127 break;
5128 if (deep)
5129 item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep);
5130 else
5131 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5132 if (dict_add(copy, di) == FAIL)
5133 {
5134 dictitem_free(di);
5135 break;
5136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005138 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005139
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 ++copy->dv_refcount;
5141 }
5142
5143 return copy;
5144}
5145
5146/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005148 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005150 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005152 dict_T *d;
5153 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154{
Bram Moolenaar33570922005-01-25 22:26:29 +00005155 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156}
5157
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 * Get the number of items in a Dictionary.
5160 */
5161 static long
5162dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005164{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005165 if (d == NULL)
5166 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005167 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168}
5169
5170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Find item "key[len]" in Dictionary "d".
5172 * If "len" is negative use strlen(key).
5173 * Returns NULL when not found.
5174 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005175 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005176dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005177 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 char_u *key;
5179 int len;
5180{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005181#define AKEYLEN 200
5182 char_u buf[AKEYLEN];
5183 char_u *akey;
5184 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005187 if (len < 0)
5188 akey = key;
5189 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005190 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005191 tofree = akey = vim_strnsave(key, len);
5192 if (akey == NULL)
5193 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005195 else
5196 {
5197 /* Avoid a malloc/free by using buf[]. */
5198 STRNCPY(buf, key, len);
5199 buf[len] = NUL;
5200 akey = buf;
5201 }
5202
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005204 vim_free(tofree);
5205 if (HASHITEM_EMPTY(hi))
5206 return NULL;
5207 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208}
5209
5210/*
5211 * Return an allocated string with the string representation of a Dictionary.
5212 * May return NULL.
5213 */
5214 static char_u *
5215dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005216 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217{
5218 garray_T ga;
5219 int first = TRUE;
5220 char_u *tofree;
5221 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005225 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005227 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 return NULL;
5229 ga_init2(&ga, (int)sizeof(char), 80);
5230 ga_append(&ga, '{');
5231
Bram Moolenaar33570922005-01-25 22:26:29 +00005232 todo = d->dv_hashtab.ht_used;
5233 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005235 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005237 --todo;
5238
5239 if (first)
5240 first = FALSE;
5241 else
5242 ga_concat(&ga, (char_u *)", ");
5243
5244 tofree = string_quote(hi->hi_key, FALSE);
5245 if (tofree != NULL)
5246 {
5247 ga_concat(&ga, tofree);
5248 vim_free(tofree);
5249 }
5250 ga_concat(&ga, (char_u *)": ");
5251 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5252 if (s != NULL)
5253 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 vim_free(tofree);
5255 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 }
5257
5258 ga_append(&ga, '}');
5259 ga_append(&ga, NUL);
5260 return (char_u *)ga.ga_data;
5261}
5262
5263/*
5264 * Allocate a variable for a Dictionary and fill it from "*arg".
5265 * Return OK or FAIL. Returns NOTDONE for {expr}.
5266 */
5267 static int
5268get_dict_tv(arg, rettv, evaluate)
5269 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005270 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 int evaluate;
5272{
Bram Moolenaar33570922005-01-25 22:26:29 +00005273 dict_T *d = NULL;
5274 typval_T tvkey;
5275 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005277 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005279 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280
5281 /*
5282 * First check if it's not a curly-braces thing: {expr}.
5283 * Must do this without evaluating, otherwise a function may be called
5284 * twice. Unfortunately this means we need to call eval1() twice for the
5285 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005286 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005288 if (*start != '}')
5289 {
5290 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5291 return FAIL;
5292 if (*start == '}')
5293 return NOTDONE;
5294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295
5296 if (evaluate)
5297 {
5298 d = dict_alloc();
5299 if (d == NULL)
5300 return FAIL;
5301 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005302 tvkey.v_type = VAR_UNKNOWN;
5303 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304
5305 *arg = skipwhite(*arg + 1);
5306 while (**arg != '}' && **arg != NUL)
5307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005308 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 goto failret;
5310 if (**arg != ':')
5311 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005313 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314 goto failret;
5315 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005316 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 if (*key == NUL)
5318 {
5319 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005320 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 goto failret;
5322 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323
5324 *arg = skipwhite(*arg + 1);
5325 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5326 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005327 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 goto failret;
5329 }
5330 if (evaluate)
5331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005332 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 if (item != NULL)
5334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005335 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005336 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337 clear_tv(&tv);
5338 goto failret;
5339 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005340 item = dictitem_alloc(key);
5341 clear_tv(&tvkey);
5342 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 item->di_tv = tv;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005345 if (dict_add(d, item) == FAIL)
5346 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 }
5348 }
5349
5350 if (**arg == '}')
5351 break;
5352 if (**arg != ',')
5353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005354 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 goto failret;
5356 }
5357 *arg = skipwhite(*arg + 1);
5358 }
5359
5360 if (**arg != '}')
5361 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005362 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363failret:
5364 if (evaluate)
5365 dict_free(d);
5366 return FAIL;
5367 }
5368
5369 *arg = skipwhite(*arg + 1);
5370 if (evaluate)
5371 {
5372 rettv->v_type = VAR_DICT;
5373 rettv->vval.v_dict = d;
5374 ++d->dv_refcount;
5375 }
5376
5377 return OK;
5378}
5379
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 * Return a string with the string representation of a variable.
5382 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005384 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 * May return NULL;
5386 */
5387 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005388echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005391 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005393 static int recurse = 0;
5394 char_u *r = NULL;
5395
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005397 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005398 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005399 *tofree = NULL;
5400 return NULL;
5401 }
5402 ++recurse;
5403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 switch (tv->v_type)
5405 {
5406 case VAR_FUNC:
5407 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005408 r = tv->vval.v_string;
5409 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 case VAR_LIST:
5411 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005412 r = *tofree;
5413 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414 case VAR_DICT:
5415 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005416 r = *tofree;
5417 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 case VAR_STRING:
5419 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005420 *tofree = NULL;
5421 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005423 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005424 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005425 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005427
5428 --recurse;
5429 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005430}
5431
5432/*
5433 * Return a string with the string representation of a variable.
5434 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5435 * "numbuf" is used for a number.
5436 * Puts quotes around strings, so that they can be parsed back by eval().
5437 * May return NULL;
5438 */
5439 static char_u *
5440tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005442 char_u **tofree;
5443 char_u *numbuf;
5444{
5445 switch (tv->v_type)
5446 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005447 case VAR_FUNC:
5448 *tofree = string_quote(tv->vval.v_string, TRUE);
5449 return *tofree;
5450 case VAR_STRING:
5451 *tofree = string_quote(tv->vval.v_string, FALSE);
5452 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005453 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005454 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005456 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005457 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005460 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461}
5462
5463/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 * Return string "str" in ' quotes, doubling ' characters.
5465 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005467 */
5468 static char_u *
5469string_quote(str, function)
5470 char_u *str;
5471 int function;
5472{
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005474 char_u *p, *r, *s;
5475
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 len = (function ? 13 : 3);
5477 if (str != NULL)
5478 {
5479 len += STRLEN(str);
5480 for (p = str; *p != NUL; mb_ptr_adv(p))
5481 if (*p == '\'')
5482 ++len;
5483 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005484 s = r = alloc(len);
5485 if (r != NULL)
5486 {
5487 if (function)
5488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005490 r += 10;
5491 }
5492 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 if (str != NULL)
5495 for (p = str; *p != NUL; )
5496 {
5497 if (*p == '\'')
5498 *r++ = '\'';
5499 MB_COPY_CHAR(p, r);
5500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005502 if (function)
5503 *r++ = ')';
5504 *r++ = NUL;
5505 }
5506 return s;
5507}
5508
5509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 * Get the value of an environment variable.
5511 * "arg" is pointing to the '$'. It is advanced to after the name.
5512 * If the environment variable was not set, silently assume it is empty.
5513 * Always return OK.
5514 */
5515 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 int evaluate;
5520{
5521 char_u *string = NULL;
5522 int len;
5523 int cc;
5524 char_u *name;
5525
5526 ++*arg;
5527 name = *arg;
5528 len = get_env_len(arg);
5529 if (evaluate)
5530 {
5531 if (len != 0)
5532 {
5533 cc = name[len];
5534 name[len] = NUL;
5535 /* first try mch_getenv(), fast for normal environment vars */
5536 string = mch_getenv(name);
5537 if (string != NULL && *string != NUL)
5538 string = vim_strsave(string);
5539 else
5540 {
5541 /* next try expanding things like $VIM and ${HOME} */
5542 string = expand_env_save(name - 1);
5543 if (string != NULL && *string == '$')
5544 {
5545 vim_free(string);
5546 string = NULL;
5547 }
5548 }
5549 name[len] = cc;
5550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 rettv->v_type = VAR_STRING;
5552 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554
5555 return OK;
5556}
5557
5558/*
5559 * Array with names and number of arguments of all internal functions
5560 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5561 */
5562static struct fst
5563{
5564 char *f_name; /* function name */
5565 char f_min_argc; /* minimal number of arguments */
5566 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00005567 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569} functions[] =
5570{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005571 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {"append", 2, 2, f_append},
5573 {"argc", 0, 0, f_argc},
5574 {"argidx", 0, 0, f_argidx},
5575 {"argv", 1, 1, f_argv},
5576 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005577 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {"bufexists", 1, 1, f_bufexists},
5579 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5580 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5581 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5582 {"buflisted", 1, 1, f_buflisted},
5583 {"bufloaded", 1, 1, f_bufloaded},
5584 {"bufname", 1, 1, f_bufname},
5585 {"bufnr", 1, 1, f_bufnr},
5586 {"bufwinnr", 1, 1, f_bufwinnr},
5587 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005588 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005589 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {"char2nr", 1, 1, f_char2nr},
5591 {"cindent", 1, 1, f_cindent},
5592 {"col", 1, 1, f_col},
5593 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005595 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {"cscope_connection",0,3, f_cscope_connection},
5597 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {"delete", 1, 1, f_delete},
5600 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005601 {"diff_filler", 1, 1, f_diff_filler},
5602 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005603 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {"eventhandler", 0, 0, f_eventhandler},
5607 {"executable", 1, 1, f_executable},
5608 {"exists", 1, 1, f_exists},
5609 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005610 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5612 {"filereadable", 1, 1, f_filereadable},
5613 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005614 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005615 {"finddir", 1, 3, f_finddir},
5616 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {"fnamemodify", 2, 2, f_fnamemodify},
5618 {"foldclosed", 1, 1, f_foldclosed},
5619 {"foldclosedend", 1, 1, f_foldclosedend},
5620 {"foldlevel", 1, 1, f_foldlevel},
5621 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005622 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005625 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {"getbufvar", 2, 2, f_getbufvar},
5627 {"getchar", 0, 1, f_getchar},
5628 {"getcharmod", 0, 0, f_getcharmod},
5629 {"getcmdline", 0, 0, f_getcmdline},
5630 {"getcmdpos", 0, 0, f_getcmdpos},
5631 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005632 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005633 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {"getfsize", 1, 1, f_getfsize},
5635 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005636 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005637 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {"getreg", 0, 1, f_getreg},
5639 {"getregtype", 0, 1, f_getregtype},
5640 {"getwinposx", 0, 0, f_getwinposx},
5641 {"getwinposy", 0, 0, f_getwinposy},
5642 {"getwinvar", 2, 2, f_getwinvar},
5643 {"glob", 1, 1, f_glob},
5644 {"globpath", 2, 2, f_globpath},
5645 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005646 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {"hasmapto", 1, 2, f_hasmapto},
5648 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5649 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5650 {"histadd", 2, 2, f_histadd},
5651 {"histdel", 1, 2, f_histdel},
5652 {"histget", 1, 2, f_histget},
5653 {"histnr", 1, 1, f_histnr},
5654 {"hlID", 1, 1, f_hlID},
5655 {"hlexists", 1, 1, f_hlexists},
5656 {"hostname", 0, 0, f_hostname},
5657 {"iconv", 3, 3, f_iconv},
5658 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005659 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {"input", 1, 2, f_input},
5661 {"inputdialog", 1, 3, f_inputdialog},
5662 {"inputrestore", 0, 0, f_inputrestore},
5663 {"inputsave", 0, 0, f_inputsave},
5664 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {"libcall", 3, 3, f_libcall},
5673 {"libcallnr", 3, 3, f_libcallnr},
5674 {"line", 1, 1, f_line},
5675 {"line2byte", 1, 1, f_line2byte},
5676 {"lispindent", 1, 1, f_lispindent},
5677 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {"maparg", 1, 2, f_maparg},
5680 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005681 {"match", 2, 4, f_match},
5682 {"matchend", 2, 4, f_matchend},
5683 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005684 {"max", 1, 1, f_max},
5685 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 {"mode", 0, 0, f_mode},
5687 {"nextnonblank", 1, 1, f_nextnonblank},
5688 {"nr2char", 1, 1, f_nr2char},
5689 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {"remote_expr", 2, 3, f_remote_expr},
5692 {"remote_foreground", 1, 1, f_remote_foreground},
5693 {"remote_peek", 1, 2, f_remote_peek},
5694 {"remote_read", 1, 1, f_remote_read},
5695 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005698 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005700 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {"search", 1, 2, f_search},
5702 {"searchpair", 3, 5, f_searchpair},
5703 {"server2client", 2, 2, f_server2client},
5704 {"serverlist", 0, 0, f_serverlist},
5705 {"setbufvar", 3, 3, f_setbufvar},
5706 {"setcmdpos", 1, 1, f_setcmdpos},
5707 {"setline", 2, 2, f_setline},
5708 {"setreg", 2, 3, f_setreg},
5709 {"setwinvar", 3, 3, f_setwinvar},
5710 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005711 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713#ifdef HAVE_STRFTIME
5714 {"strftime", 1, 2, f_strftime},
5715#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00005716 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {"strlen", 1, 1, f_strlen},
5719 {"strpart", 2, 3, f_strpart},
5720 {"strridx", 2, 2, f_strridx},
5721 {"strtrans", 1, 1, f_strtrans},
5722 {"submatch", 1, 1, f_submatch},
5723 {"substitute", 4, 4, f_substitute},
5724 {"synID", 3, 3, f_synID},
5725 {"synIDattr", 2, 3, f_synIDattr},
5726 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005727 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {"tempname", 0, 0, f_tempname},
5729 {"tolower", 1, 1, f_tolower},
5730 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005731 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {"virtcol", 1, 1, f_virtcol},
5735 {"visualmode", 0, 1, f_visualmode},
5736 {"winbufnr", 1, 1, f_winbufnr},
5737 {"wincol", 0, 0, f_wincol},
5738 {"winheight", 1, 1, f_winheight},
5739 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005740 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {"winrestcmd", 0, 0, f_winrestcmd},
5742 {"winwidth", 1, 1, f_winwidth},
5743};
5744
5745#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5746
5747/*
5748 * Function given to ExpandGeneric() to obtain the list of internal
5749 * or user defined function names.
5750 */
5751 char_u *
5752get_function_name(xp, idx)
5753 expand_T *xp;
5754 int idx;
5755{
5756 static int intidx = -1;
5757 char_u *name;
5758
5759 if (idx == 0)
5760 intidx = -1;
5761 if (intidx < 0)
5762 {
5763 name = get_user_func_name(xp, idx);
5764 if (name != NULL)
5765 return name;
5766 }
5767 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5768 {
5769 STRCPY(IObuff, functions[intidx].f_name);
5770 STRCAT(IObuff, "(");
5771 if (functions[intidx].f_max_argc == 0)
5772 STRCAT(IObuff, ")");
5773 return IObuff;
5774 }
5775
5776 return NULL;
5777}
5778
5779/*
5780 * Function given to ExpandGeneric() to obtain the list of internal or
5781 * user defined variable or function names.
5782 */
5783/*ARGSUSED*/
5784 char_u *
5785get_expr_name(xp, idx)
5786 expand_T *xp;
5787 int idx;
5788{
5789 static int intidx = -1;
5790 char_u *name;
5791
5792 if (idx == 0)
5793 intidx = -1;
5794 if (intidx < 0)
5795 {
5796 name = get_function_name(xp, idx);
5797 if (name != NULL)
5798 return name;
5799 }
5800 return get_user_var_name(xp, ++intidx);
5801}
5802
5803#endif /* FEAT_CMDL_COMPL */
5804
5805/*
5806 * Find internal function in table above.
5807 * Return index, or -1 if not found
5808 */
5809 static int
5810find_internal_func(name)
5811 char_u *name; /* name of the function */
5812{
5813 int first = 0;
5814 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5815 int cmp;
5816 int x;
5817
5818 /*
5819 * Find the function name in the table. Binary search.
5820 */
5821 while (first <= last)
5822 {
5823 x = first + ((unsigned)(last - first) >> 1);
5824 cmp = STRCMP(name, functions[x].f_name);
5825 if (cmp < 0)
5826 last = x - 1;
5827 else if (cmp > 0)
5828 first = x + 1;
5829 else
5830 return x;
5831 }
5832 return -1;
5833}
5834
5835/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5837 * name it contains, otherwise return "name".
5838 */
5839 static char_u *
5840deref_func_name(name, lenp)
5841 char_u *name;
5842 int *lenp;
5843{
Bram Moolenaar33570922005-01-25 22:26:29 +00005844 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 int cc;
5846
5847 cc = name[*lenp];
5848 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00005849 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 {
5855 *lenp = 0;
5856 return (char_u *)""; /* just in case */
5857 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 *lenp = STRLEN(v->di_tv.vval.v_string);
5859 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 }
5861
5862 return name;
5863}
5864
5865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 * Allocate a variable for the result of a function.
5867 * Return OK or FAIL.
5868 */
5869 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00005870get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
5871 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 char_u *name; /* name of the function */
5873 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 char_u **arg; /* argument, pointing to the '(' */
5876 linenr_T firstline; /* first line of range */
5877 linenr_T lastline; /* last line of range */
5878 int *doesrange; /* return: function handled range */
5879 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 char_u *argp;
5883 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00005884 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 int argcount = 0; /* number of arguments found */
5886
5887 /*
5888 * Get the arguments.
5889 */
5890 argp = *arg;
5891 while (argcount < MAX_FUNC_ARGS)
5892 {
5893 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5894 if (*argp == ')' || *argp == ',' || *argp == NUL)
5895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5897 {
5898 ret = FAIL;
5899 break;
5900 }
5901 ++argcount;
5902 if (*argp != ',')
5903 break;
5904 }
5905 if (*argp == ')')
5906 ++argp;
5907 else
5908 ret = FAIL;
5909
5910 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005911 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 {
5915 if (argcount == MAX_FUNC_ARGS)
5916 EMSG2(_("E740: Too many arguments for function %s"), name);
5917 else
5918 EMSG2(_("E116: Invalid arguments for function %s"), name);
5919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005922 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
5924 *arg = skipwhite(argp);
5925 return ret;
5926}
5927
5928
5929/*
5930 * Call a function with its resolved parameters
5931 * Return OK or FAIL.
5932 */
5933 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 char_u *name; /* name of the function */
5937 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 linenr_T firstline; /* first line of range */
5942 linenr_T lastline; /* last line of range */
5943 int *doesrange; /* return: function handled range */
5944 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
5947 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948#define ERROR_UNKNOWN 0
5949#define ERROR_TOOMANY 1
5950#define ERROR_TOOFEW 2
5951#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00005952#define ERROR_DICT 4
5953#define ERROR_NONE 5
5954#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 int error = ERROR_NONE;
5956 int i;
5957 int llen;
5958 ufunc_T *fp;
5959 int cc;
5960#define FLEN_FIXED 40
5961 char_u fname_buf[FLEN_FIXED + 1];
5962 char_u *fname;
5963
5964 /*
5965 * In a script change <SID>name() and s:name() to K_SNR 123_name().
5966 * Change <SNR>123_name() to K_SNR 123_name().
5967 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
5968 */
5969 cc = name[len];
5970 name[len] = NUL;
5971 llen = eval_fname_script(name);
5972 if (llen > 0)
5973 {
5974 fname_buf[0] = K_SPECIAL;
5975 fname_buf[1] = KS_EXTRA;
5976 fname_buf[2] = (int)KE_SNR;
5977 i = 3;
5978 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
5979 {
5980 if (current_SID <= 0)
5981 error = ERROR_SCRIPT;
5982 else
5983 {
5984 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
5985 i = (int)STRLEN(fname_buf);
5986 }
5987 }
5988 if (i + STRLEN(name + llen) < FLEN_FIXED)
5989 {
5990 STRCPY(fname_buf + i, name + llen);
5991 fname = fname_buf;
5992 }
5993 else
5994 {
5995 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
5996 if (fname == NULL)
5997 error = ERROR_OTHER;
5998 else
5999 {
6000 mch_memmove(fname, fname_buf, (size_t)i);
6001 STRCPY(fname + i, name + llen);
6002 }
6003 }
6004 }
6005 else
6006 fname = name;
6007
6008 *doesrange = FALSE;
6009
6010
6011 /* execute the function if no errors detected and executing */
6012 if (evaluate && error == ERROR_NONE)
6013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006014 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 error = ERROR_UNKNOWN;
6016
6017 if (!ASCII_ISLOWER(fname[0]))
6018 {
6019 /*
6020 * User defined function.
6021 */
6022 fp = find_func(fname);
6023#ifdef FEAT_AUTOCMD
6024 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
6025 fname, fname, TRUE, NULL)
6026#ifdef FEAT_EVAL
6027 && !aborting()
6028#endif
6029 )
6030 {
6031 /* executed an autocommand, search for function again */
6032 fp = find_func(fname);
6033 }
6034#endif
6035 if (fp != NULL)
6036 {
6037 if (fp->flags & FC_RANGE)
6038 *doesrange = TRUE;
6039 if (argcount < fp->args.ga_len)
6040 error = ERROR_TOOFEW;
6041 else if (!fp->varargs && argcount > fp->args.ga_len)
6042 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006043 else if ((fp->flags & FC_DICT) && selfdict == NULL)
6044 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 else
6046 {
6047 /*
6048 * Call the user function.
6049 * Save and restore search patterns, script variables and
6050 * redo buffer.
6051 */
6052 save_search_patterns();
6053 saveRedobuff();
6054 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 firstline, lastline,
6057 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 if (--fp->calls <= 0 && isdigit(*fp->name)
6059 && fp->refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 /* Function was unreferenced while being used, free it
6061 * now. */
6062 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 restoreRedobuff();
6064 restore_search_patterns();
6065 error = ERROR_NONE;
6066 }
6067 }
6068 }
6069 else
6070 {
6071 /*
6072 * Find the function name in the table, call its implementation.
6073 */
6074 i = find_internal_func(fname);
6075 if (i >= 0)
6076 {
6077 if (argcount < functions[i].f_min_argc)
6078 error = ERROR_TOOFEW;
6079 else if (argcount > functions[i].f_max_argc)
6080 error = ERROR_TOOMANY;
6081 else
6082 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006084 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 error = ERROR_NONE;
6086 }
6087 }
6088 }
6089 /*
6090 * The function call (or "FuncUndefined" autocommand sequence) might
6091 * have been aborted by an error, an interrupt, or an explicitly thrown
6092 * exception that has not been caught so far. This situation can be
6093 * tested for by calling aborting(). For an error in an internal
6094 * function or for the "E132" error in call_user_func(), however, the
6095 * throw point at which the "force_abort" flag (temporarily reset by
6096 * emsg()) is normally updated has not been reached yet. We need to
6097 * update that flag first to make aborting() reliable.
6098 */
6099 update_force_abort();
6100 }
6101 if (error == ERROR_NONE)
6102 ret = OK;
6103
6104 /*
6105 * Report an error unless the argument evaluation or function call has been
6106 * cancelled due to an aborting error, an interrupt, or an exception.
6107 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006108 if (!aborting())
6109 {
6110 switch (error)
6111 {
6112 case ERROR_UNKNOWN:
6113 EMSG2(_("E117: Unknown function: %s"), name);
6114 break;
6115 case ERROR_TOOMANY:
6116 EMSG2(_(e_toomanyarg), name);
6117 break;
6118 case ERROR_TOOFEW:
6119 EMSG2(_("E119: Not enough arguments for function: %s"),
6120 name);
6121 break;
6122 case ERROR_SCRIPT:
6123 EMSG2(_("E120: Using <SID> not in a script context: %s"),
6124 name);
6125 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006126 case ERROR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006127 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00006128 name);
6129 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006130 }
6131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 name[len] = cc;
6134 if (fname != name && fname != fname_buf)
6135 vim_free(fname);
6136
6137 return ret;
6138}
6139
6140/*********************************************
6141 * Implementation of the built-in functions
6142 */
6143
6144/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006145 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 */
6147 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006148f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 typval_T *argvars;
6150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006154 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006157 l = argvars[0].vval.v_list;
6158 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 }
6161 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006162 EMSG(_(e_listreq));
6163}
6164
6165/*
6166 * "append(lnum, string/list)" function
6167 */
6168 static void
6169f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 typval_T *argvars;
6171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006172{
6173 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 list_T *l = NULL;
6175 listitem_T *li = NULL;
6176 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006177 long added = 0;
6178
6179 rettv->vval.v_number = 1; /* Default: Failed */
6180 lnum = get_tv_lnum(argvars);
6181 if (lnum >= 0
6182 && lnum <= curbuf->b_ml.ml_line_count
6183 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006185 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006186 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006187 l = argvars[1].vval.v_list;
6188 if (l == NULL)
6189 return;
6190 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006192 for (;;)
6193 {
6194 if (l == NULL)
6195 tv = &argvars[1]; /* append a string */
6196 else if (li == NULL)
6197 break; /* end of list */
6198 else
6199 tv = &li->li_tv; /* append item from list */
6200 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6201 ++added;
6202 if (l == NULL)
6203 break;
6204 li = li->li_next;
6205 }
6206
6207 appended_lines_mark(lnum, added);
6208 if (curwin->w_cursor.lnum > lnum)
6209 curwin->w_cursor.lnum += added;
6210 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
6212}
6213
6214/*
6215 * "argc()" function
6216 */
6217/* ARGSUSED */
6218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006219f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 typval_T *argvars;
6221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006223 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224}
6225
6226/*
6227 * "argidx()" function
6228 */
6229/* ARGSUSED */
6230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006231f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 typval_T *argvars;
6233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006235 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236}
6237
6238/*
6239 * "argv(nr)" function
6240 */
6241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006242f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 typval_T *argvars;
6244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 int idx;
6247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006248 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006250 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006252 rettv->vval.v_string = NULL;
6253 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254}
6255
6256/*
6257 * "browse(save, title, initdir, default)" function
6258 */
6259/* ARGSUSED */
6260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006261f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 typval_T *argvars;
6263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265#ifdef FEAT_BROWSE
6266 int save;
6267 char_u *title;
6268 char_u *initdir;
6269 char_u *defname;
6270 char_u buf[NUMBUFLEN];
6271 char_u buf2[NUMBUFLEN];
6272
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006273 save = get_tv_number(&argvars[0]);
6274 title = get_tv_string(&argvars[1]);
6275 initdir = get_tv_string_buf(&argvars[2], buf);
6276 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006278 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006279 do_browse(save ? BROWSE_SAVE : 0,
6280 title, defname, NULL, initdir, NULL, curbuf);
6281#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006282 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006283#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006284 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006285}
6286
6287/*
6288 * "browsedir(title, initdir)" function
6289 */
6290/* ARGSUSED */
6291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006292f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 typval_T *argvars;
6294 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006295{
6296#ifdef FEAT_BROWSE
6297 char_u *title;
6298 char_u *initdir;
6299 char_u buf[NUMBUFLEN];
6300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006301 title = get_tv_string(&argvars[0]);
6302 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006304 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006305 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006307 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006309 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
6311
Bram Moolenaar33570922005-01-25 22:26:29 +00006312static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314/*
6315 * Find a buffer by number or exact name.
6316 */
6317 static buf_T *
6318find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006319 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 if (avar->v_type == VAR_NUMBER)
6324 buf = buflist_findnr((int)avar->vval.v_number);
6325 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006328 if (buf == NULL)
6329 {
6330 /* No full path name match, try a match with a URL or a "nofile"
6331 * buffer, these don't use the full path. */
6332 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6333 if (buf->b_fname != NULL
6334 && (path_with_url(buf->b_fname)
6335#ifdef FEAT_QUICKFIX
6336 || bt_nofile(buf)
6337#endif
6338 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006340 break;
6341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
6343 return buf;
6344}
6345
6346/*
6347 * "bufexists(expr)" function
6348 */
6349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 typval_T *argvars;
6352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006354 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355}
6356
6357/*
6358 * "buflisted(expr)" function
6359 */
6360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006361f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006362 typval_T *argvars;
6363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 buf_T *buf;
6366
6367 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006368 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369}
6370
6371/*
6372 * "bufloaded(expr)" function
6373 */
6374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006375f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 typval_T *argvars;
6377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 buf_T *buf;
6380
6381 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006382 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383}
6384
Bram Moolenaar33570922005-01-25 22:26:29 +00006385static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006386
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387/*
6388 * Get buffer by number or pattern.
6389 */
6390 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006394 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 int save_magic;
6396 char_u *save_cpo;
6397 buf_T *buf;
6398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006399 if (tv->v_type == VAR_NUMBER)
6400 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (name == NULL || *name == NUL)
6402 return curbuf;
6403 if (name[0] == '$' && name[1] == NUL)
6404 return lastbuf;
6405
6406 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6407 save_magic = p_magic;
6408 p_magic = TRUE;
6409 save_cpo = p_cpo;
6410 p_cpo = (char_u *)"";
6411
6412 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6413 TRUE, FALSE));
6414
6415 p_magic = save_magic;
6416 p_cpo = save_cpo;
6417
6418 /* If not found, try expanding the name, like done for bufexists(). */
6419 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 return buf;
6423}
6424
6425/*
6426 * "bufname(expr)" function
6427 */
6428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006429f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 typval_T *argvars;
6431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 buf_T *buf;
6434
6435 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006436 buf = get_buf_tv(&argvars[0]);
6437 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006439 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 --emsg_off;
6443}
6444
6445/*
6446 * "bufnr(expr)" function
6447 */
6448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006449f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 typval_T *argvars;
6451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 buf_T *buf;
6454
6455 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006458 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 --emsg_off;
6462}
6463
6464/*
6465 * "bufwinnr(nr)" function
6466 */
6467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006468f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 typval_T *argvars;
6470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472#ifdef FEAT_WINDOWS
6473 win_T *wp;
6474 int winnr = 0;
6475#endif
6476 buf_T *buf;
6477
6478 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006479 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480#ifdef FEAT_WINDOWS
6481 for (wp = firstwin; wp; wp = wp->w_next)
6482 {
6483 ++winnr;
6484 if (wp->w_buffer == buf)
6485 break;
6486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006489 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490#endif
6491 --emsg_off;
6492}
6493
6494/*
6495 * "byte2line(byte)" function
6496 */
6497/*ARGSUSED*/
6498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006499f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 typval_T *argvars;
6501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#else
6506 long boff = 0;
6507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006512 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 (linenr_T)0, &boff);
6514#endif
6515}
6516
6517/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006518 * "byteidx()" function
6519 */
6520/*ARGSUSED*/
6521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006522f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 typval_T *argvars;
6524 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006525{
6526#ifdef FEAT_MBYTE
6527 char_u *t;
6528#endif
6529 char_u *str;
6530 long idx;
6531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 str = get_tv_string(&argvars[0]);
6533 idx = get_tv_number(&argvars[1]);
6534 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006535 if (idx < 0)
6536 return;
6537
6538#ifdef FEAT_MBYTE
6539 t = str;
6540 for ( ; idx > 0; idx--)
6541 {
6542 if (*t == NUL) /* EOL reached */
6543 return;
6544 t += mb_ptr2len_check(t);
6545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006547#else
6548 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006549 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006550#endif
6551}
6552
6553/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 * "call(func, arglist)" function
6555 */
6556 static void
6557f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 typval_T *argvars;
6559 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
6561 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567
6568 rettv->vval.v_number = 0;
6569 if (argvars[1].v_type != VAR_LIST)
6570 {
6571 EMSG(_(e_listreq));
6572 return;
6573 }
6574 if (argvars[1].vval.v_list == NULL)
6575 return;
6576
6577 if (argvars[0].v_type == VAR_FUNC)
6578 func = argvars[0].vval.v_string;
6579 else
6580 func = get_tv_string(&argvars[0]);
6581
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582 if (argvars[2].v_type != VAR_UNKNOWN)
6583 {
6584 if (argvars[2].v_type != VAR_DICT)
6585 {
6586 EMSG(_(e_dictreq));
6587 return;
6588 }
6589 selfdict = argvars[2].vval.v_dict;
6590 }
6591
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6593 item = item->li_next)
6594 {
6595 if (argc == MAX_FUNC_ARGS)
6596 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006597 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598 break;
6599 }
6600 /* Make a copy of each argument (is this really needed?) */
6601 copy_tv(&item->li_tv, &argv[argc++]);
6602 }
6603
6604 if (item == NULL)
6605 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006606 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6607 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006608
6609 /* Free the arguments. */
6610 while (argc > 0)
6611 clear_tv(&argv[--argc]);
6612}
6613
6614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 * "char2nr(string)" function
6616 */
6617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 typval_T *argvars;
6620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622#ifdef FEAT_MBYTE
6623 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 rettv->vval.v_number =
6625 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 else
6627#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006628 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630
6631/*
6632 * "cindent(lnum)" function
6633 */
6634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006635f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 typval_T *argvars;
6637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639#ifdef FEAT_CINDENT
6640 pos_T pos;
6641 linenr_T lnum;
6642
6643 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006644 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6646 {
6647 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 curwin->w_cursor = pos;
6650 }
6651 else
6652#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006653 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654}
6655
6656/*
6657 * "col(string)" function
6658 */
6659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006660f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006661 typval_T *argvars;
6662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 colnr_T col = 0;
6665 pos_T *fp;
6666
6667 fp = var2fpos(&argvars[0], FALSE);
6668 if (fp != NULL)
6669 {
6670 if (fp->col == MAXCOL)
6671 {
6672 /* '> can be MAXCOL, get the length of the line then */
6673 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6674 col = STRLEN(ml_get(fp->lnum)) + 1;
6675 else
6676 col = MAXCOL;
6677 }
6678 else
6679 {
6680 col = fp->col + 1;
6681#ifdef FEAT_VIRTUALEDIT
6682 /* col(".") when the cursor is on the NUL at the end of the line
6683 * because of "coladd" can be seen as an extra column. */
6684 if (virtual_active() && fp == &curwin->w_cursor)
6685 {
6686 char_u *p = ml_get_cursor();
6687
6688 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6689 curwin->w_virtcol - curwin->w_cursor.coladd))
6690 {
6691# ifdef FEAT_MBYTE
6692 int l;
6693
6694 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6695 col += l;
6696# else
6697 if (*p != NUL && p[1] == NUL)
6698 ++col;
6699# endif
6700 }
6701 }
6702#endif
6703 }
6704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006705 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706}
6707
6708/*
6709 * "confirm(message, buttons[, default [, type]])" function
6710 */
6711/*ARGSUSED*/
6712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 typval_T *argvars;
6715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6718 char_u *message;
6719 char_u *buttons = NULL;
6720 char_u buf[NUMBUFLEN];
6721 char_u buf2[NUMBUFLEN];
6722 int def = 1;
6723 int type = VIM_GENERIC;
6724 int c;
6725
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006726 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006729 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaara7043832005-01-21 11:56:39 +00006735 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 switch (TOUPPER_ASC(c))
6738 {
6739 case 'E': type = VIM_ERROR; break;
6740 case 'Q': type = VIM_QUESTION; break;
6741 case 'I': type = VIM_INFO; break;
6742 case 'W': type = VIM_WARNING; break;
6743 case 'G': type = VIM_GENERIC; break;
6744 }
6745 }
6746 }
6747 }
6748
6749 if (buttons == NULL || *buttons == NUL)
6750 buttons = (char_u *)_("&Ok");
6751
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006752 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 def, NULL);
6754#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006755 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756#endif
6757}
6758
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759/*
6760 * "copy()" function
6761 */
6762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006763f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006764 typval_T *argvars;
6765 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006766{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006767 item_copy(&argvars[0], rettv, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006771 * "count()" function
6772 */
6773 static void
6774f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006775 typval_T *argvars;
6776 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006777{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006778 long n = 0;
6779 int ic = FALSE;
6780
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006782 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 listitem_T *li;
6784 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006785 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786
Bram Moolenaare9a41262005-01-15 22:18:47 +00006787 if ((l = argvars[0].vval.v_list) != NULL)
6788 {
6789 li = l->lv_first;
6790 if (argvars[2].v_type != VAR_UNKNOWN)
6791 {
6792 ic = get_tv_number(&argvars[2]);
6793 if (argvars[3].v_type != VAR_UNKNOWN)
6794 {
6795 idx = get_tv_number(&argvars[3]);
6796 li = list_find(l, idx);
6797 if (li == NULL)
6798 EMSGN(_(e_listidx), idx);
6799 }
6800 }
6801
6802 for ( ; li != NULL; li = li->li_next)
6803 if (tv_equal(&li->li_tv, &argvars[1], ic))
6804 ++n;
6805 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006807 else if (argvars[0].v_type == VAR_DICT)
6808 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 int todo;
6810 dict_T *d;
6811 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006813 if ((d = argvars[0].vval.v_dict) != NULL)
6814 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 if (argvars[2].v_type != VAR_UNKNOWN)
6816 {
6817 ic = get_tv_number(&argvars[2]);
6818 if (argvars[3].v_type != VAR_UNKNOWN)
6819 EMSG(_(e_invarg));
6820 }
6821
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 todo = d->dv_hashtab.ht_used;
6823 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006824 {
6825 if (!HASHITEM_EMPTY(hi))
6826 {
6827 --todo;
6828 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
6829 ++n;
6830 }
6831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 }
6833 }
6834 else
6835 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006836 rettv->vval.v_number = n;
6837}
6838
6839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6841 *
6842 * Checks the existence of a cscope connection.
6843 */
6844/*ARGSUSED*/
6845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006846f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006847 typval_T *argvars;
6848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850#ifdef FEAT_CSCOPE
6851 int num = 0;
6852 char_u *dbpath = NULL;
6853 char_u *prepend = NULL;
6854 char_u buf[NUMBUFLEN];
6855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 if (argvars[0].v_type != VAR_UNKNOWN
6857 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006859 num = (int)get_tv_number(&argvars[0]);
6860 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006865 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#endif
6869}
6870
6871/*
6872 * "cursor(lnum, col)" function
6873 *
6874 * Moves the cursor to the specified line and column
6875 */
6876/*ARGSUSED*/
6877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 typval_T *argvars;
6880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 long line, col;
6883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006884 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (line > 0)
6886 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006887 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 if (col > 0)
6889 curwin->w_cursor.col = col - 1;
6890#ifdef FEAT_VIRTUALEDIT
6891 curwin->w_cursor.coladd = 0;
6892#endif
6893
6894 /* Make sure the cursor is in a valid position. */
6895 check_cursor();
6896#ifdef FEAT_MBYTE
6897 /* Correct cursor for multi-byte character. */
6898 if (has_mbyte)
6899 mb_adjust_cursor();
6900#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006901
6902 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903}
6904
6905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006906 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 */
6908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 typval_T *argvars;
6911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006913 item_copy(&argvars[0], rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914}
6915
6916/*
6917 * "delete()" function
6918 */
6919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 typval_T *argvars;
6922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006927 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929
6930/*
6931 * "did_filetype()" function
6932 */
6933/*ARGSUSED*/
6934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 typval_T *argvars;
6937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006940 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943#endif
6944}
6945
6946/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00006947 * "diff_filler()" function
6948 */
6949/*ARGSUSED*/
6950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006951f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 typval_T *argvars;
6953 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006954{
6955#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006956 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00006957#endif
6958}
6959
6960/*
6961 * "diff_hlID()" function
6962 */
6963/*ARGSUSED*/
6964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006965f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 typval_T *argvars;
6967 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006968{
6969#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006970 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00006971 static linenr_T prev_lnum = 0;
6972 static int changedtick = 0;
6973 static int fnum = 0;
6974 static int change_start = 0;
6975 static int change_end = 0;
6976 static enum hlf_value hlID = 0;
6977 int filler_lines;
6978 int col;
6979
6980 if (lnum != prev_lnum
6981 || changedtick != curbuf->b_changedtick
6982 || fnum != curbuf->b_fnum)
6983 {
6984 /* New line, buffer, change: need to get the values. */
6985 filler_lines = diff_check(curwin, lnum);
6986 if (filler_lines < 0)
6987 {
6988 if (filler_lines == -1)
6989 {
6990 change_start = MAXCOL;
6991 change_end = -1;
6992 if (diff_find_change(curwin, lnum, &change_start, &change_end))
6993 hlID = HLF_ADD; /* added line */
6994 else
6995 hlID = HLF_CHD; /* changed line */
6996 }
6997 else
6998 hlID = HLF_ADD; /* added line */
6999 }
7000 else
7001 hlID = (enum hlf_value)0;
7002 prev_lnum = lnum;
7003 changedtick = curbuf->b_changedtick;
7004 fnum = curbuf->b_fnum;
7005 }
7006
7007 if (hlID == HLF_CHD || hlID == HLF_TXD)
7008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007010 if (col >= change_start && col <= change_end)
7011 hlID = HLF_TXD; /* changed text */
7012 else
7013 hlID = HLF_CHD; /* changed line */
7014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007015 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007016#endif
7017}
7018
7019/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007020 * "empty({expr})" function
7021 */
7022 static void
7023f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 typval_T *argvars;
7025 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007026{
7027 int n;
7028
7029 switch (argvars[0].v_type)
7030 {
7031 case VAR_STRING:
7032 case VAR_FUNC:
7033 n = argvars[0].vval.v_string == NULL
7034 || *argvars[0].vval.v_string == NUL;
7035 break;
7036 case VAR_NUMBER:
7037 n = argvars[0].vval.v_number == 0;
7038 break;
7039 case VAR_LIST:
7040 n = argvars[0].vval.v_list == NULL
7041 || argvars[0].vval.v_list->lv_first == NULL;
7042 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007043 case VAR_DICT:
7044 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007046 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007047 default:
7048 EMSG2(_(e_intern2), "f_empty()");
7049 n = 0;
7050 }
7051
7052 rettv->vval.v_number = n;
7053}
7054
7055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 * "escape({string}, {chars})" function
7057 */
7058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007059f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 typval_T *argvars;
7061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 char_u buf[NUMBUFLEN];
7064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007065 rettv->vval.v_string =
7066 vim_strsave_escaped(get_tv_string(&argvars[0]),
7067 get_tv_string_buf(&argvars[1], buf));
7068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069}
7070
7071/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007072 * "eval()" function
7073 */
7074/*ARGSUSED*/
7075 static void
7076f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 typval_T *argvars;
7078 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007079{
7080 char_u *s;
7081
7082 s = get_tv_string(&argvars[0]);
7083 s = skipwhite(s);
7084
7085 if (eval1(&s, rettv, TRUE) == FAIL)
7086 rettv->vval.v_number = 0;
7087 else if (*s != NUL)
7088 EMSG(_(e_trailing));
7089}
7090
7091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 * "eventhandler()" function
7093 */
7094/*ARGSUSED*/
7095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007096f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 typval_T *argvars;
7098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}
7102
7103/*
7104 * "executable()" function
7105 */
7106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007107f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 typval_T *argvars;
7109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007111 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112}
7113
7114/*
7115 * "exists()" function
7116 */
7117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007118f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 typval_T *argvars;
7120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 char_u *p;
7123 char_u *name;
7124 int n = FALSE;
7125 int len = 0;
7126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007127 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 if (*p == '$') /* environment variable */
7129 {
7130 /* first try "normal" environment variables (fast) */
7131 if (mch_getenv(p + 1) != NULL)
7132 n = TRUE;
7133 else
7134 {
7135 /* try expanding things like $VIM and ${HOME} */
7136 p = expand_env_save(p);
7137 if (p != NULL && *p != '$')
7138 n = TRUE;
7139 vim_free(p);
7140 }
7141 }
7142 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007143 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 else if (*p == '*') /* internal or user defined function */
7145 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007146 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
7148 else if (*p == ':')
7149 {
7150 n = cmd_exists(p + 1);
7151 }
7152 else if (*p == '#')
7153 {
7154#ifdef FEAT_AUTOCMD
7155 name = p + 1;
7156 p = vim_strchr(name, '#');
7157 if (p != NULL)
7158 n = au_exists(name, p, p + 1);
7159 else
7160 n = au_exists(name, name + STRLEN(name), NULL);
7161#endif
7162 }
7163 else /* internal variable */
7164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 char_u *expr_start;
7166 char_u *expr_end;
7167 char_u *temp_string = NULL;
7168 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 name = p;
7170
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007172 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 if (expr_start != NULL)
7174 {
7175 temp_string = make_expanded_name(name, expr_start, expr_end, s);
7176 if (temp_string != NULL)
7177 {
7178 len = STRLEN(temp_string);
7179 name = temp_string;
7180 }
7181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (len == 0)
7183 len = get_id_len(&p);
7184 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007185 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007190 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191}
7192
7193/*
7194 * "expand()" function
7195 */
7196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007197f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 typval_T *argvars;
7199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 char_u *s;
7202 int len;
7203 char_u *errormsg;
7204 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7205 expand_T xpc;
7206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007207 rettv->v_type = VAR_STRING;
7208 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (*s == '%' || *s == '#' || *s == '<')
7210 {
7211 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007212 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 --emsg_off;
7214 }
7215 else
7216 {
7217 /* When the optional second argument is non-zero, don't remove matches
7218 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007219 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 flags |= WILD_KEEP_ALL;
7221 ExpandInit(&xpc);
7222 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 ExpandCleanup(&xpc);
7225 }
7226}
7227
7228/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007229 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007231 */
7232 static void
7233f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 typval_T *argvars;
7235 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007236{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007237 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007239 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 list_T *l1, *l2;
7241 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 long before;
7243 long n;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007244
Bram Moolenaare9a41262005-01-15 22:18:47 +00007245 l1 = argvars[0].vval.v_list;
7246 l2 = argvars[1].vval.v_list;
7247 if (l1 != NULL && l2 != NULL)
7248 {
7249 if (argvars[2].v_type != VAR_UNKNOWN)
7250 {
7251 n = before = get_tv_number(&argvars[2]);
7252 item = list_find_ext(l1, &n);
7253 if (n != 0)
7254 {
7255 EMSGN(_(e_listidx), before);
7256 return;
7257 }
7258 }
7259 else
7260 item = NULL;
7261 list_extend(l1, l2, item);
7262
7263 ++l1->lv_refcount;
7264 copy_tv(&argvars[0], rettv);
7265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007267 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7268 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 dict_T *d1, *d2;
7270 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007271 char_u *action;
7272 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275
7276 d1 = argvars[0].vval.v_dict;
7277 d2 = argvars[1].vval.v_dict;
7278 if (d1 != NULL && d2 != NULL)
7279 {
7280 /* Check the third argument. */
7281 if (argvars[2].v_type != VAR_UNKNOWN)
7282 {
7283 static char *(av[]) = {"keep", "force", "error"};
7284
7285 action = get_tv_string(&argvars[2]);
7286 for (i = 0; i < 3; ++i)
7287 if (STRCMP(action, av[i]) == 0)
7288 break;
7289 if (i == 3)
7290 {
7291 EMSGN(_(e_invarg2), action);
7292 return;
7293 }
7294 }
7295 else
7296 action = (char_u *)"force";
7297
7298 /* Go over all entries in the second dict and add them to the
7299 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 todo = d2->dv_hashtab.ht_used;
7301 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 --todo;
7306 di1 = dict_find(d1, hi2->hi_key, -1);
7307 if (di1 == NULL)
7308 {
7309 di1 = dictitem_copy(HI2DI(hi2));
7310 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7311 dictitem_free(di1);
7312 }
7313 else if (*action == 'e')
7314 {
7315 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7316 break;
7317 }
7318 else if (*action == 'f')
7319 {
7320 clear_tv(&di1->di_tv);
7321 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 }
7324 }
7325
7326 ++d1->dv_refcount;
7327 copy_tv(&argvars[0], rettv);
7328 }
7329 }
7330 else
7331 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007332}
7333
7334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 * "filereadable()" function
7336 */
7337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007338f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 typval_T *argvars;
7340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 FILE *fd;
7343 char_u *p;
7344 int n;
7345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7348 {
7349 n = TRUE;
7350 fclose(fd);
7351 }
7352 else
7353 n = FALSE;
7354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007355 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356}
7357
7358/*
7359 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7360 * rights to write into.
7361 */
7362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007363f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 typval_T *argvars;
7365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367 char_u *p;
7368 int retval = 0;
7369#if defined(UNIX) || defined(VMS)
7370 int perm = 0;
7371#endif
7372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374#if defined(UNIX) || defined(VMS)
7375 perm = mch_getperm(p);
7376#endif
7377#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7378 if (
7379# ifdef WIN3264
7380 mch_writable(p) &&
7381# else
7382# if defined(UNIX) || defined(VMS)
7383 (perm & 0222) &&
7384# endif
7385# endif
7386 mch_access((char *)p, W_OK) == 0
7387 )
7388#endif
7389 {
7390 ++retval;
7391 if (mch_isdir(p))
7392 ++retval;
7393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007394 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395}
7396
Bram Moolenaar33570922005-01-25 22:26:29 +00007397static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007398
7399 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007400findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 typval_T *argvars;
7402 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007403 int dir;
7404{
7405#ifdef FEAT_SEARCHPATH
7406 char_u *fname;
7407 char_u *fresult = NULL;
7408 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7409 char_u *p;
7410 char_u pathbuf[NUMBUFLEN];
7411 int count = 1;
7412 int first = TRUE;
7413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007414 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007419 if (*p != NUL)
7420 path = p;
7421
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007424 }
7425
7426 do
7427 {
7428 vim_free(fresult);
7429 fresult = find_file_in_path_option(first ? fname : NULL,
7430 first ? (int)STRLEN(fname) : 0,
7431 0, first, path, dir, NULL);
7432 first = FALSE;
7433 } while (--count > 0 && fresult != NULL);
7434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007435 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007436#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007438#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007440}
7441
Bram Moolenaar33570922005-01-25 22:26:29 +00007442static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7443static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007444
7445/*
7446 * Implementation of map() and filter().
7447 */
7448 static void
7449filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 typval_T *argvars;
7451 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007452 int map;
7453{
7454 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 listitem_T *li, *nli;
7457 list_T *l = NULL;
7458 dictitem_T *di;
7459 hashtab_T *ht;
7460 hashitem_T *hi;
7461 dict_T *d = NULL;
7462 typval_T save_val;
7463 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007465 int todo;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007466
7467 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007469 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 if ((l = argvars[0].vval.v_list) == NULL)
7471 return;
7472 }
7473 else if (argvars[0].v_type == VAR_DICT)
7474 {
7475 if ((d = argvars[0].vval.v_dict) == NULL)
7476 return;
7477 }
7478 else
7479 {
7480 EMSG2(_(e_listdictarg), map ? "map()" : "filter()");
7481 return;
7482 }
7483
7484 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 save_val = vimvars[VV_VAL].vv_tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486
7487 if (argvars[0].v_type == VAR_DICT)
7488 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007489 save_key = vimvars[VV_KEY].vv_tv;
7490 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00007493 hash_lock(ht);
7494 todo = ht->ht_used;
7495 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007497 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007498 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007499 --todo;
7500 di = HI2DI(hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
7503 break;
7504 if (!map && rem)
7505 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007507 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007508 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007509 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 clear_tv(&vimvars[VV_KEY].vv_tv);
7512 vimvars[VV_KEY].vv_tv = save_key;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007513 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007514 else
7515 {
7516 for (li = l->lv_first; li != NULL; li = nli)
7517 {
7518 nli = li->li_next;
7519 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7520 break;
7521 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007522 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007523 }
7524 }
7525
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 clear_tv(&vimvars[VV_VAL].vv_tv);
7527 vimvars[VV_VAL].vv_tv = save_val;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528
7529 copy_tv(&argvars[0], rettv);
7530}
7531
7532 static int
7533filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007535 char_u *expr;
7536 int map;
7537 int *remp;
7538{
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007540 char_u *s;
7541
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543 s = expr;
7544 if (eval1(&s, &rettv, TRUE) == FAIL)
7545 return FAIL;
7546 if (*s != NUL) /* check for trailing chars after expr */
7547 {
7548 EMSG2(_(e_invexpr2), s);
7549 return FAIL;
7550 }
7551 if (map)
7552 {
7553 /* map(): replace the list item value */
7554 clear_tv(tv);
7555 *tv = rettv;
7556 }
7557 else
7558 {
7559 /* filter(): when expr is zero remove the item */
7560 *remp = (get_tv_number(&rettv) == 0);
7561 clear_tv(&rettv);
7562 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007565}
7566
7567/*
7568 * "filter()" function
7569 */
7570 static void
7571f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 typval_T *argvars;
7573 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574{
7575 filter_map(argvars, rettv, FALSE);
7576}
7577
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007579 * "finddir({fname}[, {path}[, {count}]])" function
7580 */
7581 static void
7582f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 typval_T *argvars;
7584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007585{
7586 findfilendir(argvars, rettv, TRUE);
7587}
7588
7589/*
7590 * "findfile({fname}[, {path}[, {count}]])" function
7591 */
7592 static void
7593f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 typval_T *argvars;
7595 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007596{
7597 findfilendir(argvars, rettv, FALSE);
7598}
7599
7600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 * "fnamemodify({fname}, {mods})" function
7602 */
7603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 typval_T *argvars;
7606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 char_u *fname;
7609 char_u *mods;
7610 int usedlen = 0;
7611 int len;
7612 char_u *fbuf = NULL;
7613 char_u buf[NUMBUFLEN];
7614
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007615 fname = get_tv_string(&argvars[0]);
7616 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 len = (int)STRLEN(fname);
7618
7619 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 vim_free(fbuf);
7627}
7628
Bram Moolenaar33570922005-01-25 22:26:29 +00007629static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630
7631/*
7632 * "foldclosed()" function
7633 */
7634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *argvars;
7637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 int end;
7639{
7640#ifdef FEAT_FOLDING
7641 linenr_T lnum;
7642 linenr_T first, last;
7643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7646 {
7647 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7648 {
7649 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 return;
7654 }
7655 }
7656#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
7660/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 * "foldclosed()" function
7662 */
7663 static void
7664f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 typval_T *argvars;
7666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007667{
7668 foldclosed_both(argvars, rettv, FALSE);
7669}
7670
7671/*
7672 * "foldclosedend()" function
7673 */
7674 static void
7675f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 typval_T *argvars;
7677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007678{
7679 foldclosed_both(argvars, rettv, TRUE);
7680}
7681
7682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 * "foldlevel()" function
7684 */
7685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 typval_T *argvars;
7688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
7690#ifdef FEAT_FOLDING
7691 linenr_T lnum;
7692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 else
7697#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699}
7700
7701/*
7702 * "foldtext()" function
7703 */
7704/*ARGSUSED*/
7705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 typval_T *argvars;
7708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
7710#ifdef FEAT_FOLDING
7711 linenr_T lnum;
7712 char_u *s;
7713 char_u *r;
7714 int len;
7715 char *txt;
7716#endif
7717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->v_type = VAR_STRING;
7719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007721 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7722 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7723 <= curbuf->b_ml.ml_line_count
7724 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
7726 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007727 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
7728 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {
7730 if (!linewhite(lnum))
7731 break;
7732 ++lnum;
7733 }
7734
7735 /* Find interesting text in this line. */
7736 s = skipwhite(ml_get(lnum));
7737 /* skip C comment-start */
7738 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007741 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007743 {
7744 s = skipwhite(ml_get(lnum + 1));
7745 if (*s == '*')
7746 s = skipwhite(s + 1);
7747 }
7748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 txt = _("+-%s%3ld lines: ");
7750 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007751 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 + 20 /* for %3ld */
7753 + STRLEN(s))); /* concatenated */
7754 if (r != NULL)
7755 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
7757 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
7758 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 len = (int)STRLEN(r);
7760 STRCAT(r, s);
7761 /* remove 'foldmarker' and 'commentstring' */
7762 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 }
7765 }
7766#endif
7767}
7768
7769/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007770 * "foldtextresult(lnum)" function
7771 */
7772/*ARGSUSED*/
7773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *argvars;
7776 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007777{
7778#ifdef FEAT_FOLDING
7779 linenr_T lnum;
7780 char_u *text;
7781 char_u buf[51];
7782 foldinfo_T foldinfo;
7783 int fold_count;
7784#endif
7785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 rettv->v_type = VAR_STRING;
7787 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007788#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007790 fold_count = foldedCount(curwin, lnum, &foldinfo);
7791 if (fold_count > 0)
7792 {
7793 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7794 &foldinfo, buf);
7795 if (text == buf)
7796 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007798 }
7799#endif
7800}
7801
7802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 * "foreground()" function
7804 */
7805/*ARGSUSED*/
7806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *argvars;
7809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812#ifdef FEAT_GUI
7813 if (gui.in_use)
7814 gui_mch_set_foreground();
7815#else
7816# ifdef WIN32
7817 win32_set_foreground();
7818# endif
7819#endif
7820}
7821
7822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 * "function()" function
7824 */
7825/*ARGSUSED*/
7826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007828 typval_T *argvars;
7829 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830{
7831 char_u *s;
7832
Bram Moolenaara7043832005-01-21 11:56:39 +00007833 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007835 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836 EMSG2(_(e_invarg2), s);
7837 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007838 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 else
7840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->vval.v_string = vim_strsave(s);
7842 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 }
7844}
7845
7846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007847 * "get()" function
7848 */
7849 static void
7850f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007851 typval_T *argvars;
7852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007853{
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 listitem_T *li;
7855 list_T *l;
7856 dictitem_T *di;
7857 dict_T *d;
7858 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007859
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007861 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007863 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 li = list_find(l, get_tv_number(&argvars[1]));
7865 if (li != NULL)
7866 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007867 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00007868 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 else if (argvars[0].v_type == VAR_DICT)
7870 {
7871 if ((d = argvars[0].vval.v_dict) != NULL)
7872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007873 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 if (di != NULL)
7875 tv = &di->di_tv;
7876 }
7877 }
7878 else
7879 EMSG2(_(e_listdictarg), "get()");
7880
7881 if (tv == NULL)
7882 {
7883 if (argvars[2].v_type == VAR_UNKNOWN)
7884 rettv->vval.v_number = 0;
7885 else
7886 copy_tv(&argvars[2], rettv);
7887 }
7888 else
7889 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007890}
7891
7892/*
7893 * "getbufvar()" function
7894 */
7895 static void
7896f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 typval_T *argvars;
7898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007899{
7900 buf_T *buf;
7901 buf_T *save_curbuf;
7902 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007904
7905 ++emsg_off;
7906 buf = get_buf_tv(&argvars[0]);
7907 varname = get_tv_string(&argvars[1]);
7908
7909 rettv->v_type = VAR_STRING;
7910 rettv->vval.v_string = NULL;
7911
7912 if (buf != NULL && varname != NULL)
7913 {
7914 if (*varname == '&') /* buffer-local-option */
7915 {
7916 /* set curbuf to be our buf, temporarily */
7917 save_curbuf = curbuf;
7918 curbuf = buf;
7919
7920 get_option_tv(&varname, rettv, TRUE);
7921
7922 /* restore previous notion of curbuf */
7923 curbuf = save_curbuf;
7924 }
7925 else
7926 {
7927 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007929 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 }
7932 }
7933
7934 --emsg_off;
7935}
7936
7937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 * "getchar()" function
7939 */
7940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 typval_T *argvars;
7943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 varnumber_T n;
7946
7947 ++no_mapping;
7948 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 /* getchar(): blocking wait. */
7951 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 /* getchar(1): only check if char avail */
7954 n = vpeekc();
7955 else if (vpeekc() == NUL)
7956 /* getchar(0) and no char avail: return zero */
7957 n = 0;
7958 else
7959 /* getchar(0) and char avail: return char */
7960 n = safe_vgetc();
7961 --no_mapping;
7962 --allow_keys;
7963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (IS_SPECIAL(n) || mod_mask != 0)
7966 {
7967 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
7968 int i = 0;
7969
7970 /* Turn a special key into three bytes, plus modifier. */
7971 if (mod_mask != 0)
7972 {
7973 temp[i++] = K_SPECIAL;
7974 temp[i++] = KS_MODIFIER;
7975 temp[i++] = mod_mask;
7976 }
7977 if (IS_SPECIAL(n))
7978 {
7979 temp[i++] = K_SPECIAL;
7980 temp[i++] = K_SECOND(n);
7981 temp[i++] = K_THIRD(n);
7982 }
7983#ifdef FEAT_MBYTE
7984 else if (has_mbyte)
7985 i += (*mb_char2bytes)(n, temp + i);
7986#endif
7987 else
7988 temp[i++] = n;
7989 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007990 rettv->v_type = VAR_STRING;
7991 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 }
7993}
7994
7995/*
7996 * "getcharmod()" function
7997 */
7998/*ARGSUSED*/
7999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008000f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 typval_T *argvars;
8002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005}
8006
8007/*
8008 * "getcmdline()" function
8009 */
8010/*ARGSUSED*/
8011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008013 typval_T *argvars;
8014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016 rettv->v_type = VAR_STRING;
8017 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018}
8019
8020/*
8021 * "getcmdpos()" function
8022 */
8023/*ARGSUSED*/
8024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 typval_T *argvars;
8027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
8032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 * "getcwd()" function
8034 */
8035/*ARGSUSED*/
8036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008037f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 typval_T *argvars;
8039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 char_u cwd[MAXPATHL];
8042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 else
8047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051#endif
8052 }
8053}
8054
8055/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008056 * "getfontname()" function
8057 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008058/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008060f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 typval_T *argvars;
8062 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->v_type = VAR_STRING;
8065 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008066#ifdef FEAT_GUI
8067 if (gui.in_use)
8068 {
8069 GuiFont font;
8070 char_u *name = NULL;
8071
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008072 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008073 {
8074 /* Get the "Normal" font. Either the name saved by
8075 * hl_set_font_name() or from the font ID. */
8076 font = gui.norm_font;
8077 name = hl_get_font_name();
8078 }
8079 else
8080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008082 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8083 return;
8084 font = gui_mch_get_font(name, FALSE);
8085 if (font == NOFONT)
8086 return; /* Invalid font name, return empty string. */
8087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008088 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008090 gui_mch_free_font(font);
8091 }
8092#endif
8093}
8094
8095/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008096 * "getfperm({fname})" function
8097 */
8098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 typval_T *argvars;
8101 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008102{
8103 char_u *fname;
8104 struct stat st;
8105 char_u *perm = NULL;
8106 char_u flags[] = "rwx";
8107 int i;
8108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008110
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008112 if (mch_stat((char *)fname, &st) >= 0)
8113 {
8114 perm = vim_strsave((char_u *)"---------");
8115 if (perm != NULL)
8116 {
8117 for (i = 0; i < 9; i++)
8118 {
8119 if (st.st_mode & (1 << (8 - i)))
8120 perm[i] = flags[i % 3];
8121 }
8122 }
8123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008125}
8126
8127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 * "getfsize({fname})" function
8129 */
8130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008131f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 typval_T *argvars;
8133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
8135 char_u *fname;
8136 struct stat st;
8137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008138 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008140 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141
8142 if (mch_stat((char *)fname, &st) >= 0)
8143 {
8144 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008147 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
8149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008150 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151}
8152
8153/*
8154 * "getftime({fname})" function
8155 */
8156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 typval_T *argvars;
8159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161 char_u *fname;
8162 struct stat st;
8163
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170}
8171
8172/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008173 * "getftype({fname})" function
8174 */
8175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008176f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 typval_T *argvars;
8178 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008179{
8180 char_u *fname;
8181 struct stat st;
8182 char_u *type = NULL;
8183 char *t;
8184
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008185 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008187 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008188 if (mch_lstat((char *)fname, &st) >= 0)
8189 {
8190#ifdef S_ISREG
8191 if (S_ISREG(st.st_mode))
8192 t = "file";
8193 else if (S_ISDIR(st.st_mode))
8194 t = "dir";
8195# ifdef S_ISLNK
8196 else if (S_ISLNK(st.st_mode))
8197 t = "link";
8198# endif
8199# ifdef S_ISBLK
8200 else if (S_ISBLK(st.st_mode))
8201 t = "bdev";
8202# endif
8203# ifdef S_ISCHR
8204 else if (S_ISCHR(st.st_mode))
8205 t = "cdev";
8206# endif
8207# ifdef S_ISFIFO
8208 else if (S_ISFIFO(st.st_mode))
8209 t = "fifo";
8210# endif
8211# ifdef S_ISSOCK
8212 else if (S_ISSOCK(st.st_mode))
8213 t = "fifo";
8214# endif
8215 else
8216 t = "other";
8217#else
8218# ifdef S_IFMT
8219 switch (st.st_mode & S_IFMT)
8220 {
8221 case S_IFREG: t = "file"; break;
8222 case S_IFDIR: t = "dir"; break;
8223# ifdef S_IFLNK
8224 case S_IFLNK: t = "link"; break;
8225# endif
8226# ifdef S_IFBLK
8227 case S_IFBLK: t = "bdev"; break;
8228# endif
8229# ifdef S_IFCHR
8230 case S_IFCHR: t = "cdev"; break;
8231# endif
8232# ifdef S_IFIFO
8233 case S_IFIFO: t = "fifo"; break;
8234# endif
8235# ifdef S_IFSOCK
8236 case S_IFSOCK: t = "socket"; break;
8237# endif
8238 default: t = "other";
8239 }
8240# else
8241 if (mch_isdir(fname))
8242 t = "dir";
8243 else
8244 t = "file";
8245# endif
8246#endif
8247 type = vim_strsave((char_u *)t);
8248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008250}
8251
8252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008253 * "getline(lnum)" function
8254 */
8255 static void
8256f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 typval_T *argvars;
8258 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008259{
8260 linenr_T lnum;
8261 linenr_T end;
8262 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 list_T *l;
8264 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008265
8266 lnum = get_tv_lnum(argvars);
8267
8268 if (argvars[1].v_type == VAR_UNKNOWN)
8269 {
8270 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8271 p = ml_get(lnum);
8272 else
8273 p = (char_u *)"";
8274
8275 rettv->v_type = VAR_STRING;
8276 rettv->vval.v_string = vim_strsave(p);
8277 }
8278 else
8279 {
8280 end = get_tv_lnum(&argvars[1]);
8281 if (end < lnum)
8282 {
8283 EMSG(_(e_invrange));
8284 rettv->vval.v_number = 0;
8285 }
8286 else
8287 {
8288 l = list_alloc();
8289 if (l != NULL)
8290 {
8291 if (lnum < 1)
8292 lnum = 1;
8293 if (end > curbuf->b_ml.ml_line_count)
8294 end = curbuf->b_ml.ml_line_count;
8295 while (lnum <= end)
8296 {
8297 li = listitem_alloc();
8298 if (li == NULL)
8299 break;
8300 list_append(l, li);
8301 li->li_tv.v_type = VAR_STRING;
8302 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8303 }
8304 rettv->vval.v_list = l;
8305 rettv->v_type = VAR_LIST;
8306 ++l->lv_refcount;
8307 }
8308 }
8309 }
8310}
8311
8312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 * "getreg()" function
8314 */
8315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 typval_T *argvars;
8318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 char_u *strregname;
8321 int regname;
8322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008324 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 regname = (strregname == NULL ? '"' : *strregname);
8328 if (regname == 0)
8329 regname = '"';
8330
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331 rettv->v_type = VAR_STRING;
8332 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333}
8334
8335/*
8336 * "getregtype()" function
8337 */
8338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 typval_T *argvars;
8341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343 char_u *strregname;
8344 int regname;
8345 char_u buf[NUMBUFLEN + 2];
8346 long reglen = 0;
8347
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 else
8351 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008352 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 regname = (strregname == NULL ? '"' : *strregname);
8355 if (regname == 0)
8356 regname = '"';
8357
8358 buf[0] = NUL;
8359 buf[1] = NUL;
8360 switch (get_reg_type(regname, &reglen))
8361 {
8362 case MLINE: buf[0] = 'V'; break;
8363 case MCHAR: buf[0] = 'v'; break;
8364#ifdef FEAT_VISUAL
8365 case MBLOCK:
8366 buf[0] = Ctrl_V;
8367 sprintf((char *)buf + 1, "%ld", reglen + 1);
8368 break;
8369#endif
8370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371 rettv->v_type = VAR_STRING;
8372 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373}
8374
8375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * "getwinposx()" function
8377 */
8378/*ARGSUSED*/
8379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008381 typval_T *argvars;
8382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385#ifdef FEAT_GUI
8386 if (gui.in_use)
8387 {
8388 int x, y;
8389
8390 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 }
8393#endif
8394}
8395
8396/*
8397 * "getwinposy()" function
8398 */
8399/*ARGSUSED*/
8400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *argvars;
8403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#ifdef FEAT_GUI
8407 if (gui.in_use)
8408 {
8409 int x, y;
8410
8411 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414#endif
8415}
8416
8417/*
8418 * "getwinvar()" function
8419 */
8420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 typval_T *argvars;
8423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 win_T *win, *oldcurwin;
8426 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008427 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
8429 ++emsg_off;
8430 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008431 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433 rettv->v_type = VAR_STRING;
8434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 if (win != NULL && varname != NULL)
8437 {
8438 if (*varname == '&') /* window-local-option */
8439 {
8440 /* set curwin to be our win, temporarily */
8441 oldcurwin = curwin;
8442 curwin = win;
8443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
8446 /* restore previous notion of curwin */
8447 curwin = oldcurwin;
8448 }
8449 else
8450 {
8451 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008454 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 }
8457
8458 --emsg_off;
8459}
8460
8461/*
8462 * "glob()" function
8463 */
8464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *argvars;
8467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 expand_T xpc;
8470
8471 ExpandInit(&xpc);
8472 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473 rettv->v_type = VAR_STRING;
8474 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8476 ExpandCleanup(&xpc);
8477}
8478
8479/*
8480 * "globpath()" function
8481 */
8482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 typval_T *argvars;
8485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 char_u buf1[NUMBUFLEN];
8488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489 rettv->v_type = VAR_STRING;
8490 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8491 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
8494/*
8495 * "has()" function
8496 */
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 int i;
8503 char_u *name;
8504 int n = FALSE;
8505 static char *(has_list[]) =
8506 {
8507#ifdef AMIGA
8508 "amiga",
8509# ifdef FEAT_ARP
8510 "arp",
8511# endif
8512#endif
8513#ifdef __BEOS__
8514 "beos",
8515#endif
8516#ifdef MSDOS
8517# ifdef DJGPP
8518 "dos32",
8519# else
8520 "dos16",
8521# endif
8522#endif
8523#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8524 "mac",
8525#endif
8526#if defined(MACOS_X_UNIX)
8527 "macunix",
8528#endif
8529#ifdef OS2
8530 "os2",
8531#endif
8532#ifdef __QNX__
8533 "qnx",
8534#endif
8535#ifdef RISCOS
8536 "riscos",
8537#endif
8538#ifdef UNIX
8539 "unix",
8540#endif
8541#ifdef VMS
8542 "vms",
8543#endif
8544#ifdef WIN16
8545 "win16",
8546#endif
8547#ifdef WIN32
8548 "win32",
8549#endif
8550#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8551 "win32unix",
8552#endif
8553#ifdef WIN64
8554 "win64",
8555#endif
8556#ifdef EBCDIC
8557 "ebcdic",
8558#endif
8559#ifndef CASE_INSENSITIVE_FILENAME
8560 "fname_case",
8561#endif
8562#ifdef FEAT_ARABIC
8563 "arabic",
8564#endif
8565#ifdef FEAT_AUTOCMD
8566 "autocmd",
8567#endif
8568#ifdef FEAT_BEVAL
8569 "balloon_eval",
8570#endif
8571#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8572 "builtin_terms",
8573# ifdef ALL_BUILTIN_TCAPS
8574 "all_builtin_terms",
8575# endif
8576#endif
8577#ifdef FEAT_BYTEOFF
8578 "byte_offset",
8579#endif
8580#ifdef FEAT_CINDENT
8581 "cindent",
8582#endif
8583#ifdef FEAT_CLIENTSERVER
8584 "clientserver",
8585#endif
8586#ifdef FEAT_CLIPBOARD
8587 "clipboard",
8588#endif
8589#ifdef FEAT_CMDL_COMPL
8590 "cmdline_compl",
8591#endif
8592#ifdef FEAT_CMDHIST
8593 "cmdline_hist",
8594#endif
8595#ifdef FEAT_COMMENTS
8596 "comments",
8597#endif
8598#ifdef FEAT_CRYPT
8599 "cryptv",
8600#endif
8601#ifdef FEAT_CSCOPE
8602 "cscope",
8603#endif
8604#ifdef DEBUG
8605 "debug",
8606#endif
8607#ifdef FEAT_CON_DIALOG
8608 "dialog_con",
8609#endif
8610#ifdef FEAT_GUI_DIALOG
8611 "dialog_gui",
8612#endif
8613#ifdef FEAT_DIFF
8614 "diff",
8615#endif
8616#ifdef FEAT_DIGRAPHS
8617 "digraphs",
8618#endif
8619#ifdef FEAT_DND
8620 "dnd",
8621#endif
8622#ifdef FEAT_EMACS_TAGS
8623 "emacs_tags",
8624#endif
8625 "eval", /* always present, of course! */
8626#ifdef FEAT_EX_EXTRA
8627 "ex_extra",
8628#endif
8629#ifdef FEAT_SEARCH_EXTRA
8630 "extra_search",
8631#endif
8632#ifdef FEAT_FKMAP
8633 "farsi",
8634#endif
8635#ifdef FEAT_SEARCHPATH
8636 "file_in_path",
8637#endif
8638#ifdef FEAT_FIND_ID
8639 "find_in_path",
8640#endif
8641#ifdef FEAT_FOLDING
8642 "folding",
8643#endif
8644#ifdef FEAT_FOOTER
8645 "footer",
8646#endif
8647#if !defined(USE_SYSTEM) && defined(UNIX)
8648 "fork",
8649#endif
8650#ifdef FEAT_GETTEXT
8651 "gettext",
8652#endif
8653#ifdef FEAT_GUI
8654 "gui",
8655#endif
8656#ifdef FEAT_GUI_ATHENA
8657# ifdef FEAT_GUI_NEXTAW
8658 "gui_neXtaw",
8659# else
8660 "gui_athena",
8661# endif
8662#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008663#ifdef FEAT_GUI_KDE
8664 "gui_kde",
8665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#ifdef FEAT_GUI_GTK
8667 "gui_gtk",
8668# ifdef HAVE_GTK2
8669 "gui_gtk2",
8670# endif
8671#endif
8672#ifdef FEAT_GUI_MAC
8673 "gui_mac",
8674#endif
8675#ifdef FEAT_GUI_MOTIF
8676 "gui_motif",
8677#endif
8678#ifdef FEAT_GUI_PHOTON
8679 "gui_photon",
8680#endif
8681#ifdef FEAT_GUI_W16
8682 "gui_win16",
8683#endif
8684#ifdef FEAT_GUI_W32
8685 "gui_win32",
8686#endif
8687#ifdef FEAT_HANGULIN
8688 "hangul_input",
8689#endif
8690#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8691 "iconv",
8692#endif
8693#ifdef FEAT_INS_EXPAND
8694 "insert_expand",
8695#endif
8696#ifdef FEAT_JUMPLIST
8697 "jumplist",
8698#endif
8699#ifdef FEAT_KEYMAP
8700 "keymap",
8701#endif
8702#ifdef FEAT_LANGMAP
8703 "langmap",
8704#endif
8705#ifdef FEAT_LIBCALL
8706 "libcall",
8707#endif
8708#ifdef FEAT_LINEBREAK
8709 "linebreak",
8710#endif
8711#ifdef FEAT_LISP
8712 "lispindent",
8713#endif
8714#ifdef FEAT_LISTCMDS
8715 "listcmds",
8716#endif
8717#ifdef FEAT_LOCALMAP
8718 "localmap",
8719#endif
8720#ifdef FEAT_MENU
8721 "menu",
8722#endif
8723#ifdef FEAT_SESSION
8724 "mksession",
8725#endif
8726#ifdef FEAT_MODIFY_FNAME
8727 "modify_fname",
8728#endif
8729#ifdef FEAT_MOUSE
8730 "mouse",
8731#endif
8732#ifdef FEAT_MOUSESHAPE
8733 "mouseshape",
8734#endif
8735#if defined(UNIX) || defined(VMS)
8736# ifdef FEAT_MOUSE_DEC
8737 "mouse_dec",
8738# endif
8739# ifdef FEAT_MOUSE_GPM
8740 "mouse_gpm",
8741# endif
8742# ifdef FEAT_MOUSE_JSB
8743 "mouse_jsbterm",
8744# endif
8745# ifdef FEAT_MOUSE_NET
8746 "mouse_netterm",
8747# endif
8748# ifdef FEAT_MOUSE_PTERM
8749 "mouse_pterm",
8750# endif
8751# ifdef FEAT_MOUSE_XTERM
8752 "mouse_xterm",
8753# endif
8754#endif
8755#ifdef FEAT_MBYTE
8756 "multi_byte",
8757#endif
8758#ifdef FEAT_MBYTE_IME
8759 "multi_byte_ime",
8760#endif
8761#ifdef FEAT_MULTI_LANG
8762 "multi_lang",
8763#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008764#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00008765#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008766 "mzscheme",
8767#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769#ifdef FEAT_OLE
8770 "ole",
8771#endif
8772#ifdef FEAT_OSFILETYPE
8773 "osfiletype",
8774#endif
8775#ifdef FEAT_PATH_EXTRA
8776 "path_extra",
8777#endif
8778#ifdef FEAT_PERL
8779#ifndef DYNAMIC_PERL
8780 "perl",
8781#endif
8782#endif
8783#ifdef FEAT_PYTHON
8784#ifndef DYNAMIC_PYTHON
8785 "python",
8786#endif
8787#endif
8788#ifdef FEAT_POSTSCRIPT
8789 "postscript",
8790#endif
8791#ifdef FEAT_PRINTER
8792 "printer",
8793#endif
8794#ifdef FEAT_QUICKFIX
8795 "quickfix",
8796#endif
8797#ifdef FEAT_RIGHTLEFT
8798 "rightleft",
8799#endif
8800#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8801 "ruby",
8802#endif
8803#ifdef FEAT_SCROLLBIND
8804 "scrollbind",
8805#endif
8806#ifdef FEAT_CMDL_INFO
8807 "showcmd",
8808 "cmdline_info",
8809#endif
8810#ifdef FEAT_SIGNS
8811 "signs",
8812#endif
8813#ifdef FEAT_SMARTINDENT
8814 "smartindent",
8815#endif
8816#ifdef FEAT_SNIFF
8817 "sniff",
8818#endif
8819#ifdef FEAT_STL_OPT
8820 "statusline",
8821#endif
8822#ifdef FEAT_SUN_WORKSHOP
8823 "sun_workshop",
8824#endif
8825#ifdef FEAT_NETBEANS_INTG
8826 "netbeans_intg",
8827#endif
8828#ifdef FEAT_SYN_HL
8829 "syntax",
8830#endif
8831#if defined(USE_SYSTEM) || !defined(UNIX)
8832 "system",
8833#endif
8834#ifdef FEAT_TAG_BINS
8835 "tag_binary",
8836#endif
8837#ifdef FEAT_TAG_OLDSTATIC
8838 "tag_old_static",
8839#endif
8840#ifdef FEAT_TAG_ANYWHITE
8841 "tag_any_white",
8842#endif
8843#ifdef FEAT_TCL
8844# ifndef DYNAMIC_TCL
8845 "tcl",
8846# endif
8847#endif
8848#ifdef TERMINFO
8849 "terminfo",
8850#endif
8851#ifdef FEAT_TERMRESPONSE
8852 "termresponse",
8853#endif
8854#ifdef FEAT_TEXTOBJ
8855 "textobjects",
8856#endif
8857#ifdef HAVE_TGETENT
8858 "tgetent",
8859#endif
8860#ifdef FEAT_TITLE
8861 "title",
8862#endif
8863#ifdef FEAT_TOOLBAR
8864 "toolbar",
8865#endif
8866#ifdef FEAT_USR_CMDS
8867 "user-commands", /* was accidentally included in 5.4 */
8868 "user_commands",
8869#endif
8870#ifdef FEAT_VIMINFO
8871 "viminfo",
8872#endif
8873#ifdef FEAT_VERTSPLIT
8874 "vertsplit",
8875#endif
8876#ifdef FEAT_VIRTUALEDIT
8877 "virtualedit",
8878#endif
8879#ifdef FEAT_VISUAL
8880 "visual",
8881#endif
8882#ifdef FEAT_VISUALEXTRA
8883 "visualextra",
8884#endif
8885#ifdef FEAT_VREPLACE
8886 "vreplace",
8887#endif
8888#ifdef FEAT_WILDIGN
8889 "wildignore",
8890#endif
8891#ifdef FEAT_WILDMENU
8892 "wildmenu",
8893#endif
8894#ifdef FEAT_WINDOWS
8895 "windows",
8896#endif
8897#ifdef FEAT_WAK
8898 "winaltkeys",
8899#endif
8900#ifdef FEAT_WRITEBACKUP
8901 "writebackup",
8902#endif
8903#ifdef FEAT_XIM
8904 "xim",
8905#endif
8906#ifdef FEAT_XFONTSET
8907 "xfontset",
8908#endif
8909#ifdef USE_XSMP
8910 "xsmp",
8911#endif
8912#ifdef USE_XSMP_INTERACT
8913 "xsmp_interact",
8914#endif
8915#ifdef FEAT_XCLIPBOARD
8916 "xterm_clipboard",
8917#endif
8918#ifdef FEAT_XTERM_SAVE
8919 "xterm_save",
8920#endif
8921#if defined(UNIX) && defined(FEAT_X11)
8922 "X11",
8923#endif
8924 NULL
8925 };
8926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 for (i = 0; has_list[i] != NULL; ++i)
8929 if (STRICMP(name, has_list[i]) == 0)
8930 {
8931 n = TRUE;
8932 break;
8933 }
8934
8935 if (n == FALSE)
8936 {
8937 if (STRNICMP(name, "patch", 5) == 0)
8938 n = has_patch(atoi((char *)name + 5));
8939 else if (STRICMP(name, "vim_starting") == 0)
8940 n = (starting != 0);
8941#ifdef DYNAMIC_TCL
8942 else if (STRICMP(name, "tcl") == 0)
8943 n = tcl_enabled(FALSE);
8944#endif
8945#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
8946 else if (STRICMP(name, "iconv") == 0)
8947 n = iconv_enabled(FALSE);
8948#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008949#ifdef DYNAMIC_MZSCHEME
8950 else if (STRICMP(name, "mzscheme") == 0)
8951 n = mzscheme_enabled(FALSE);
8952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#ifdef DYNAMIC_RUBY
8954 else if (STRICMP(name, "ruby") == 0)
8955 n = ruby_enabled(FALSE);
8956#endif
8957#ifdef DYNAMIC_PYTHON
8958 else if (STRICMP(name, "python") == 0)
8959 n = python_enabled(FALSE);
8960#endif
8961#ifdef DYNAMIC_PERL
8962 else if (STRICMP(name, "perl") == 0)
8963 n = perl_enabled(FALSE);
8964#endif
8965#ifdef FEAT_GUI
8966 else if (STRICMP(name, "gui_running") == 0)
8967 n = (gui.in_use || gui.starting);
8968# ifdef FEAT_GUI_W32
8969 else if (STRICMP(name, "gui_win32s") == 0)
8970 n = gui_is_win32s();
8971# endif
8972# ifdef FEAT_BROWSE
8973 else if (STRICMP(name, "browse") == 0)
8974 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
8975# endif
8976#endif
8977#ifdef FEAT_SYN_HL
8978 else if (STRICMP(name, "syntax_items") == 0)
8979 n = syntax_present(curbuf);
8980#endif
8981#if defined(WIN3264)
8982 else if (STRICMP(name, "win95") == 0)
8983 n = mch_windows95();
8984#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00008985#ifdef FEAT_NETBEANS_INTG
8986 else if (STRICMP(name, "netbeans_enabled") == 0)
8987 n = usingNetbeans;
8988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992}
8993
8994/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 * "has_key()" function
8996 */
8997 static void
8998f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001{
9002 rettv->vval.v_number = 0;
9003 if (argvars[0].v_type != VAR_DICT)
9004 {
9005 EMSG(_(e_dictreq));
9006 return;
9007 }
9008 if (argvars[0].vval.v_dict == NULL)
9009 return;
9010
9011 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009012 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013}
9014
9015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 * "hasmapto()" function
9017 */
9018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 typval_T *argvars;
9021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 char_u *name;
9024 char_u *mode;
9025 char_u buf[NUMBUFLEN];
9026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 mode = (char_u *)"nvo";
9030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
9033 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037}
9038
9039/*
9040 * "histadd()" function
9041 */
9042/*ARGSUSED*/
9043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 typval_T *argvars;
9046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048#ifdef FEAT_CMDHIST
9049 int histype;
9050 char_u *str;
9051 char_u buf[NUMBUFLEN];
9052#endif
9053
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 if (check_restricted() || check_secure())
9056 return;
9057#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 if (histype >= 0)
9060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (*str != NUL)
9063 {
9064 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 return;
9067 }
9068 }
9069#endif
9070}
9071
9072/*
9073 * "histdel()" function
9074 */
9075/*ARGSUSED*/
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081#ifdef FEAT_CMDHIST
9082 int n;
9083 char_u buf[NUMBUFLEN];
9084
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009085 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009088 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9091 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 else
9093 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9095 get_tv_string_buf(&argvars[1], buf));
9096 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099#endif
9100}
9101
9102/*
9103 * "histget()" function
9104 */
9105/*ARGSUSED*/
9106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *argvars;
9109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111#ifdef FEAT_CMDHIST
9112 int type;
9113 int idx;
9114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009116 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 idx = get_history_idx(type);
9118 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119 idx = (int)get_tv_number(&argvars[1]);
9120 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * "histnr()" function
9129 */
9130/*ARGSUSED*/
9131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009133 typval_T *argvars;
9134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 int i;
9137
9138#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 if (i >= HIST_CMD && i < HIST_COUNT)
9141 i = get_history_idx(i);
9142 else
9143#endif
9144 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146}
9147
9148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 * "highlightID(name)" function
9150 */
9151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
9159/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 * "highlight_exists()" function
9161 */
9162 static void
9163f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166{
9167 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9168}
9169
9170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 * "hostname()" function
9172 */
9173/*ARGSUSED*/
9174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 char_u hostname[256];
9180
9181 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->v_type = VAR_STRING;
9183 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
9187 * iconv() function
9188 */
9189/*ARGSUSED*/
9190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009192 typval_T *argvars;
9193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195#ifdef FEAT_MBYTE
9196 char_u buf1[NUMBUFLEN];
9197 char_u buf2[NUMBUFLEN];
9198 char_u *from, *to, *str;
9199 vimconv_T vimconv;
9200#endif
9201
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->v_type = VAR_STRING;
9203 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
9205#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 str = get_tv_string(&argvars[0]);
9207 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9208 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 vimconv.vc_type = CONV_NONE;
9210 convert_setup(&vimconv, from, to);
9211
9212 /* If the encodings are equal, no conversion needed. */
9213 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217
9218 convert_setup(&vimconv, NULL, NULL);
9219 vim_free(from);
9220 vim_free(to);
9221#endif
9222}
9223
9224/*
9225 * "indent()" function
9226 */
9227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009228f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 typval_T *argvars;
9230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 linenr_T lnum;
9233
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009241/*
9242 * "index()" function
9243 */
9244 static void
9245f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009246 typval_T *argvars;
9247 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009248{
Bram Moolenaar33570922005-01-25 22:26:29 +00009249 list_T *l;
9250 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009251 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009252 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009253 int ic = FALSE;
9254
9255 rettv->vval.v_number = -1;
9256 if (argvars[0].v_type != VAR_LIST)
9257 {
9258 EMSG(_(e_listreq));
9259 return;
9260 }
9261 l = argvars[0].vval.v_list;
9262 if (l != NULL)
9263 {
9264 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009265 {
9266 min_idx = get_tv_number(&argvars[2]);
9267 if (argvars[3].v_type != VAR_UNKNOWN)
9268 ic = get_tv_number(&argvars[3]);
9269 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009270
9271 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009272 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009273 {
9274 rettv->vval.v_number = idx;
9275 break;
9276 }
9277 }
9278}
9279
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280static int inputsecret_flag = 0;
9281
9282/*
9283 * "input()" function
9284 * Also handles inputsecret() when inputsecret is set.
9285 */
9286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287f_input(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 char_u *p = NULL;
9293 int c;
9294 char_u buf[NUMBUFLEN];
9295 int cmd_silent_save = cmd_silent;
9296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009297 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
9299#ifdef NO_CONSOLE_INPUT
9300 /* While starting up, there is no place to enter text. */
9301 if (no_console_input())
9302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 return;
9305 }
9306#endif
9307
9308 cmd_silent = FALSE; /* Want to see the prompt. */
9309 if (prompt != NULL)
9310 {
9311 /* Only the part of the message after the last NL is considered as
9312 * prompt for the command line */
9313 p = vim_strrchr(prompt, '\n');
9314 if (p == NULL)
9315 p = prompt;
9316 else
9317 {
9318 ++p;
9319 c = *p;
9320 *p = NUL;
9321 msg_start();
9322 msg_clr_eos();
9323 msg_puts_attr(prompt, echo_attr);
9324 msg_didout = FALSE;
9325 msg_starthere();
9326 *p = c;
9327 }
9328 cmdline_row = msg_row;
9329 }
9330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009331 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9336
9337 /* since the user typed this, no need to wait for return */
9338 need_wait_return = FALSE;
9339 msg_didout = FALSE;
9340 cmd_silent = cmd_silent_save;
9341}
9342
9343/*
9344 * "inputdialog()" function
9345 */
9346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009348 typval_T *argvars;
9349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351#if defined(FEAT_GUI_TEXTDIALOG)
9352 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9353 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9354 {
9355 char_u *message;
9356 char_u buf[NUMBUFLEN];
9357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 IObuff[IOSIZE - 1] = NUL;
9363 }
9364 else
9365 IObuff[0] = NUL;
9366 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9367 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 else
9370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371 if (argvars[1].v_type != VAR_UNKNOWN
9372 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_string = vim_strsave(
9374 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380 else
9381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383}
9384
9385static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9386
9387/*
9388 * "inputrestore()" function
9389 */
9390/*ARGSUSED*/
9391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009393 typval_T *argvars;
9394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 if (ga_userinput.ga_len > 0)
9397 {
9398 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9400 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 }
9403 else if (p_verbose > 1)
9404 {
9405 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 }
9408}
9409
9410/*
9411 * "inputsave()" function
9412 */
9413/*ARGSUSED*/
9414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419 /* Add an entry to the stack of typehead storage. */
9420 if (ga_grow(&ga_userinput, 1) == OK)
9421 {
9422 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9423 + ga_userinput.ga_len);
9424 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429}
9430
9431/*
9432 * "inputsecret()" function
9433 */
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 ++cmdline_star;
9440 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 --cmdline_star;
9443 --inputsecret_flag;
9444}
9445
9446/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009447 * "insert()" function
9448 */
9449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009453{
9454 long before = 0;
9455 long n;
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 listitem_T *item;
9457 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009458
9459 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009460 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009461 else if ((l = argvars[0].vval.v_list) != NULL)
9462 {
9463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009465
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009466 n = before;
9467 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009468 if (n > 0)
9469 EMSGN(_(e_listidx), before);
9470 else
9471 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009472 list_insert_tv(l, &argvars[1], item);
9473 ++l->lv_refcount;
9474 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009475 }
9476 }
9477}
9478
9479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 * "isdirectory()" function
9481 */
9482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 typval_T *argvars;
9485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488}
9489
Bram Moolenaar33570922005-01-25 22:26:29 +00009490static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +00009491
9492/*
9493 * Turn a dict into a list:
9494 * "what" == 0: list of keys
9495 * "what" == 1: list of values
9496 * "what" == 2: list of items
9497 */
9498 static void
9499dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 typval_T *argvars;
9501 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009502 int what;
9503{
Bram Moolenaar33570922005-01-25 22:26:29 +00009504 list_T *l;
9505 list_T *l2;
9506 dictitem_T *di;
9507 hashitem_T *hi;
9508 listitem_T *li;
9509 listitem_T *li2;
9510 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009511 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009512
9513 rettv->vval.v_number = 0;
9514 if (argvars[0].v_type != VAR_DICT)
9515 {
9516 EMSG(_(e_dictreq));
9517 return;
9518 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009519 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009520 return;
9521
9522 l = list_alloc();
9523 if (l == NULL)
9524 return;
9525 rettv->v_type = VAR_LIST;
9526 rettv->vval.v_list = l;
9527 ++l->lv_refcount;
9528
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 todo = d->dv_hashtab.ht_used;
9530 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009532 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00009533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009534 --todo;
9535 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009536
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009537 li = listitem_alloc();
9538 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009539 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009540 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009541
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009542 if (what == 0)
9543 {
9544 /* keys() */
9545 li->li_tv.v_type = VAR_STRING;
9546 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9547 }
9548 else if (what == 1)
9549 {
9550 /* values() */
9551 copy_tv(&di->di_tv, &li->li_tv);
9552 }
9553 else
9554 {
9555 /* items() */
9556 l2 = list_alloc();
9557 li->li_tv.v_type = VAR_LIST;
9558 li->li_tv.vval.v_list = l2;
9559 if (l2 == NULL)
9560 break;
9561 ++l2->lv_refcount;
9562
9563 li2 = listitem_alloc();
9564 if (li2 == NULL)
9565 break;
9566 list_append(l2, li2);
9567 li2->li_tv.v_type = VAR_STRING;
9568 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9569
9570 li2 = listitem_alloc();
9571 if (li2 == NULL)
9572 break;
9573 list_append(l2, li2);
9574 copy_tv(&di->di_tv, &li2->li_tv);
9575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00009576 }
9577 }
9578}
9579
9580/*
9581 * "items(dict)" function
9582 */
9583 static void
9584f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 typval_T *argvars;
9586 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009587{
9588 dict_list(argvars, rettv, 2);
9589}
9590
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009592 * "join()" function
9593 */
9594 static void
9595f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009596 typval_T *argvars;
9597 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009598{
9599 garray_T ga;
9600 char_u *sep;
9601
9602 rettv->vval.v_number = 0;
9603 if (argvars[0].v_type != VAR_LIST)
9604 {
9605 EMSG(_(e_listreq));
9606 return;
9607 }
9608 if (argvars[0].vval.v_list == NULL)
9609 return;
9610 if (argvars[1].v_type == VAR_UNKNOWN)
9611 sep = (char_u *)" ";
9612 else
9613 sep = get_tv_string(&argvars[1]);
9614
9615 ga_init2(&ga, (int)sizeof(char), 80);
9616 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9617 ga_append(&ga, NUL);
9618
9619 rettv->v_type = VAR_STRING;
9620 rettv->vval.v_string = (char_u *)ga.ga_data;
9621}
9622
9623/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009624 * "keys()" function
9625 */
9626 static void
9627f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009628 typval_T *argvars;
9629 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009630{
9631 dict_list(argvars, rettv, 0);
9632}
9633
9634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 * "last_buffer_nr()" function.
9636 */
9637/*ARGSUSED*/
9638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 typval_T *argvars;
9641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642{
9643 int n = 0;
9644 buf_T *buf;
9645
9646 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9647 if (n < buf->b_fnum)
9648 n = buf->b_fnum;
9649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009651}
9652
9653/*
9654 * "len()" function
9655 */
9656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 typval_T *argvars;
9659 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009660{
9661 switch (argvars[0].v_type)
9662 {
9663 case VAR_STRING:
9664 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 rettv->vval.v_number = (varnumber_T)STRLEN(
9666 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009667 break;
9668 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009670 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009671 case VAR_DICT:
9672 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
9673 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009674 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009675 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009676 break;
9677 }
9678}
9679
Bram Moolenaar33570922005-01-25 22:26:29 +00009680static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009681
9682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +00009684 typval_T *argvars;
9685 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 int type;
9687{
9688#ifdef FEAT_LIBCALL
9689 char_u *string_in;
9690 char_u **string_result;
9691 int nr_result;
9692#endif
9693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009695 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699
9700 if (check_restricted() || check_secure())
9701 return;
9702
9703#ifdef FEAT_LIBCALL
9704 /* The first two args must be strings, otherwise its meaningless */
9705 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
9706 {
9707 if (argvars[2].v_type == VAR_NUMBER)
9708 string_in = NULL;
9709 else
9710 string_in = argvars[2].vval.v_string;
9711 if (type == VAR_NUMBER)
9712 string_result = NULL;
9713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009715 if (mch_libcall(argvars[0].vval.v_string,
9716 argvars[1].vval.v_string,
9717 string_in,
9718 argvars[2].vval.v_number,
9719 string_result,
9720 &nr_result) == OK
9721 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009723 }
9724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009728 * "libcall()" function
9729 */
9730 static void
9731f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 typval_T *argvars;
9733 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009734{
9735 libcall_common(argvars, rettv, VAR_STRING);
9736}
9737
9738/*
9739 * "libcallnr()" function
9740 */
9741 static void
9742f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009743 typval_T *argvars;
9744 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009745{
9746 libcall_common(argvars, rettv, VAR_NUMBER);
9747}
9748
9749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 * "line(string)" function
9751 */
9752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009754 typval_T *argvars;
9755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757 linenr_T lnum = 0;
9758 pos_T *fp;
9759
9760 fp = var2fpos(&argvars[0], TRUE);
9761 if (fp != NULL)
9762 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764}
9765
9766/*
9767 * "line2byte(lnum)" function
9768 */
9769/*ARGSUSED*/
9770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009771f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009772 typval_T *argvars;
9773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
9775#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777#else
9778 linenr_T lnum;
9779
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
9785 if (rettv->vval.v_number >= 0)
9786 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#endif
9788}
9789
9790/*
9791 * "lispindent(lnum)" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798#ifdef FEAT_LISP
9799 pos_T pos;
9800 linenr_T lnum;
9801
9802 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9805 {
9806 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 curwin->w_cursor = pos;
9809 }
9810 else
9811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813}
9814
9815/*
9816 * "localtime()" function
9817 */
9818/*ARGSUSED*/
9819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009821 typval_T *argvars;
9822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825}
9826
Bram Moolenaar33570922005-01-25 22:26:29 +00009827static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
9829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 int exact;
9834{
9835 char_u *keys;
9836 char_u *which;
9837 char_u buf[NUMBUFLEN];
9838 char_u *keys_buf = NULL;
9839 char_u *rhs;
9840 int mode;
9841 garray_T ga;
9842
9843 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->v_type = VAR_STRING;
9845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 if (*keys == NUL)
9849 return;
9850
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009851 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 else
9854 which = (char_u *)"";
9855 mode = get_map_mode(&which, 0);
9856
9857 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9858 rhs = check_map(keys, mode, exact);
9859 vim_free(keys_buf);
9860 if (rhs != NULL)
9861 {
9862 ga_init(&ga);
9863 ga.ga_itemsize = 1;
9864 ga.ga_growsize = 40;
9865
9866 while (*rhs != NUL)
9867 ga_concat(&ga, str2special(&rhs, FALSE));
9868
9869 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 }
9872}
9873
9874/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009875 * "map()" function
9876 */
9877 static void
9878f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009881{
9882 filter_map(argvars, rettv, TRUE);
9883}
9884
9885/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009886 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 */
9888 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009889f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 typval_T *argvars;
9891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009893 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
9896/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009897 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 */
9899 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009900f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 typval_T *argvars;
9902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009904 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
Bram Moolenaar33570922005-01-25 22:26:29 +00009907static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908
9909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 typval_T *argvars;
9912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 int type;
9914{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009915 char_u *str = NULL;
9916 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 char_u *pat;
9918 regmatch_T regmatch;
9919 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009920 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 char_u *save_cpo;
9922 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009923 long nth = 1;
9924 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +00009925 list_T *l = NULL;
9926 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009927 long idx = 0;
9928 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
9930 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9931 save_cpo = p_cpo;
9932 p_cpo = (char_u *)"";
9933
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 if (type == 2)
9935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 rettv->v_type = VAR_STRING;
9937 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 }
9939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009942 if (argvars[0].v_type == VAR_LIST)
9943 {
9944 if ((l = argvars[0].vval.v_list) == NULL)
9945 goto theend;
9946 li = l->lv_first;
9947 }
9948 else
9949 expr = str = get_tv_string(&argvars[0]);
9950
9951 pat = get_tv_string_buf(&argvars[1], patbuf);
9952
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009953 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009956 if (l != NULL)
9957 {
9958 li = list_find(l, start);
9959 if (li == NULL)
9960 goto theend;
9961 if (start < 0)
9962 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 listitem_T *ni;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009964
9965 /* Need to compute the index. */
9966 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
9967 ++idx;
9968 }
9969 else
9970 idx = start;
9971 }
9972 else
9973 {
9974 if (start < 0)
9975 start = 0;
9976 if (start > (long)STRLEN(str))
9977 goto theend;
9978 str += start;
9979 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009980
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009981 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 }
9984
9985 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9986 if (regmatch.regprog != NULL)
9987 {
9988 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009989
9990 while (1)
9991 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009992 if (l != NULL)
9993 {
9994 if (li == NULL)
9995 {
9996 match = FALSE;
9997 break;
9998 }
9999 str = echo_string(&li->li_tv, &tofree, strbuf);
10000 }
10001
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010002 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010003
10004 if (l != NULL)
10005 vim_free(tofree);
10006 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010007 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010008 if (l == NULL && !match)
10009 break;
10010
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010011 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010012 if (l != NULL)
10013 {
10014 li = li->li_next;
10015 ++idx;
10016 }
10017 else
10018 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010019#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010020 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010021#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010022 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010023#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010024 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010025 }
10026
10027 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 {
10029 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010030 {
10031 if (l != NULL)
10032 copy_tv(&li->li_tv, rettv);
10033 else
10034 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010036 }
10037 else if (l != NULL)
10038 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 else
10040 {
10041 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 (varnumber_T)(regmatch.startp[0] - str);
10044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049 }
10050 vim_free(regmatch.regprog);
10051 }
10052
10053theend:
10054 p_cpo = save_cpo;
10055}
10056
10057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010058 * "match()" function
10059 */
10060 static void
10061f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010064{
10065 find_some_match(argvars, rettv, 1);
10066}
10067
10068/*
10069 * "matchend()" function
10070 */
10071 static void
10072f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010073 typval_T *argvars;
10074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010075{
10076 find_some_match(argvars, rettv, 0);
10077}
10078
10079/*
10080 * "matchstr()" function
10081 */
10082 static void
10083f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010084 typval_T *argvars;
10085 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010086{
10087 find_some_match(argvars, rettv, 2);
10088}
10089
Bram Moolenaar33570922005-01-25 22:26:29 +000010090static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010091
10092 static void
10093max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010094 typval_T *argvars;
10095 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010096 int domax;
10097{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010098 long n = 0;
10099 long i;
10100
10101 if (argvars[0].v_type == VAR_LIST)
10102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010103 list_T *l;
10104 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010105
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010106 l = argvars[0].vval.v_list;
10107 if (l != NULL)
10108 {
10109 li = l->lv_first;
10110 if (li != NULL)
10111 {
10112 n = get_tv_number(&li->li_tv);
10113 while (1)
10114 {
10115 li = li->li_next;
10116 if (li == NULL)
10117 break;
10118 i = get_tv_number(&li->li_tv);
10119 if (domax ? i > n : i < n)
10120 n = i;
10121 }
10122 }
10123 }
10124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010125 else if (argvars[0].v_type == VAR_DICT)
10126 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010127 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010128 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010129 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010130 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010131
10132 d = argvars[0].vval.v_dict;
10133 if (d != NULL)
10134 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010135 todo = d->dv_hashtab.ht_used;
10136 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010138 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010140 --todo;
10141 i = get_tv_number(&HI2DI(hi)->di_tv);
10142 if (first)
10143 {
10144 n = i;
10145 first = FALSE;
10146 }
10147 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010148 n = i;
10149 }
10150 }
10151 }
10152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010153 else
10154 EMSG(_(e_listreq));
10155 rettv->vval.v_number = n;
10156}
10157
10158/*
10159 * "max()" function
10160 */
10161 static void
10162f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010163 typval_T *argvars;
10164 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010165{
10166 max_min(argvars, rettv, TRUE);
10167}
10168
10169/*
10170 * "min()" function
10171 */
10172 static void
10173f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010174 typval_T *argvars;
10175 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010176{
10177 max_min(argvars, rettv, FALSE);
10178}
10179
Bram Moolenaar0d660222005-01-07 21:51:51 +000010180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 * "mode()" function
10182 */
10183/*ARGSUSED*/
10184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010186 typval_T *argvars;
10187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188{
10189 char_u buf[2];
10190
10191#ifdef FEAT_VISUAL
10192 if (VIsual_active)
10193 {
10194 if (VIsual_select)
10195 buf[0] = VIsual_mode + 's' - 'v';
10196 else
10197 buf[0] = VIsual_mode;
10198 }
10199 else
10200#endif
10201 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10202 buf[0] = 'r';
10203 else if (State & INSERT)
10204 {
10205 if (State & REPLACE_FLAG)
10206 buf[0] = 'R';
10207 else
10208 buf[0] = 'i';
10209 }
10210 else if (State & CMDLINE)
10211 buf[0] = 'c';
10212 else
10213 buf[0] = 'n';
10214
10215 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->vval.v_string = vim_strsave(buf);
10217 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218}
10219
10220/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010221 * "nextnonblank()" function
10222 */
10223 static void
10224f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010225 typval_T *argvars;
10226 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010227{
10228 linenr_T lnum;
10229
10230 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10231 {
10232 if (lnum > curbuf->b_ml.ml_line_count)
10233 {
10234 lnum = 0;
10235 break;
10236 }
10237 if (*skipwhite(ml_get(lnum)) != NUL)
10238 break;
10239 }
10240 rettv->vval.v_number = lnum;
10241}
10242
10243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 * "nr2char()" function
10245 */
10246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010248 typval_T *argvars;
10249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
10251 char_u buf[NUMBUFLEN];
10252
10253#ifdef FEAT_MBYTE
10254 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 else
10257#endif
10258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010259 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 buf[1] = NUL;
10261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010262 rettv->v_type = VAR_STRING;
10263 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010264}
10265
10266/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010267 * "prevnonblank()" function
10268 */
10269 static void
10270f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010271 typval_T *argvars;
10272 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010273{
10274 linenr_T lnum;
10275
10276 lnum = get_tv_lnum(argvars);
10277 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10278 lnum = 0;
10279 else
10280 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10281 --lnum;
10282 rettv->vval.v_number = lnum;
10283}
10284
Bram Moolenaar8c711452005-01-14 21:53:12 +000010285/*
10286 * "range()" function
10287 */
10288 static void
10289f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010290 typval_T *argvars;
10291 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010292{
10293 long start;
10294 long end;
10295 long stride = 1;
10296 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 list_T *l;
10298 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010299
10300 start = get_tv_number(&argvars[0]);
10301 if (argvars[1].v_type == VAR_UNKNOWN)
10302 {
10303 end = start - 1;
10304 start = 0;
10305 }
10306 else
10307 {
10308 end = get_tv_number(&argvars[1]);
10309 if (argvars[2].v_type != VAR_UNKNOWN)
10310 stride = get_tv_number(&argvars[2]);
10311 }
10312
10313 rettv->vval.v_number = 0;
10314 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010315 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010316 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010317 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010318 else
10319 {
10320 l = list_alloc();
10321 if (l != NULL)
10322 {
10323 rettv->v_type = VAR_LIST;
10324 rettv->vval.v_list = l;
10325 ++l->lv_refcount;
10326
10327 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10328 {
10329 li = listitem_alloc();
10330 if (li == NULL)
10331 break;
10332 li->li_tv.v_type = VAR_NUMBER;
10333 li->li_tv.vval.v_number = i;
10334 list_append(l, li);
10335 }
10336 }
10337 }
10338}
10339
Bram Moolenaar0d660222005-01-07 21:51:51 +000010340#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10341static void make_connection __ARGS((void));
10342static int check_connection __ARGS((void));
10343
10344 static void
10345make_connection()
10346{
10347 if (X_DISPLAY == NULL
10348# ifdef FEAT_GUI
10349 && !gui.in_use
10350# endif
10351 )
10352 {
10353 x_force_connect = TRUE;
10354 setup_term_clip();
10355 x_force_connect = FALSE;
10356 }
10357}
10358
10359 static int
10360check_connection()
10361{
10362 make_connection();
10363 if (X_DISPLAY == NULL)
10364 {
10365 EMSG(_("E240: No connection to Vim server"));
10366 return FAIL;
10367 }
10368 return OK;
10369}
10370#endif
10371
10372#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010373static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000010374
10375 static void
10376remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010379 int expr;
10380{
10381 char_u *server_name;
10382 char_u *keys;
10383 char_u *r = NULL;
10384 char_u buf[NUMBUFLEN];
10385# ifdef WIN32
10386 HWND w;
10387# else
10388 Window w;
10389# endif
10390
10391 if (check_restricted() || check_secure())
10392 return;
10393
10394# ifdef FEAT_X11
10395 if (check_connection() == FAIL)
10396 return;
10397# endif
10398
10399 server_name = get_tv_string(&argvars[0]);
10400 keys = get_tv_string_buf(&argvars[1], buf);
10401# ifdef WIN32
10402 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10403# else
10404 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10405 < 0)
10406# endif
10407 {
10408 if (r != NULL)
10409 EMSG(r); /* sending worked but evaluation failed */
10410 else
10411 EMSG2(_("E241: Unable to send to %s"), server_name);
10412 return;
10413 }
10414
10415 rettv->vval.v_string = r;
10416
10417 if (argvars[2].v_type != VAR_UNKNOWN)
10418 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010420 char_u str[30];
10421
10422 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000010423 v.di_tv.v_type = VAR_STRING;
10424 v.di_tv.vval.v_string = vim_strsave(str);
10425 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
10426 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010427 }
10428}
10429#endif
10430
10431/*
10432 * "remote_expr()" function
10433 */
10434/*ARGSUSED*/
10435 static void
10436f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 typval_T *argvars;
10438 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010439{
10440 rettv->v_type = VAR_STRING;
10441 rettv->vval.v_string = NULL;
10442#ifdef FEAT_CLIENTSERVER
10443 remote_common(argvars, rettv, TRUE);
10444#endif
10445}
10446
10447/*
10448 * "remote_foreground()" function
10449 */
10450/*ARGSUSED*/
10451 static void
10452f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010453 typval_T *argvars;
10454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010455{
10456 rettv->vval.v_number = 0;
10457#ifdef FEAT_CLIENTSERVER
10458# ifdef WIN32
10459 /* On Win32 it's done in this application. */
10460 serverForeground(get_tv_string(&argvars[0]));
10461# else
10462 /* Send a foreground() expression to the server. */
10463 argvars[1].v_type = VAR_STRING;
10464 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10465 argvars[2].v_type = VAR_UNKNOWN;
10466 remote_common(argvars, rettv, TRUE);
10467 vim_free(argvars[1].vval.v_string);
10468# endif
10469#endif
10470}
10471
10472/*ARGSUSED*/
10473 static void
10474f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010475 typval_T *argvars;
10476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010477{
10478#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010479 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010480 char_u *s = NULL;
10481# ifdef WIN32
10482 int n = 0;
10483# endif
10484
10485 if (check_restricted() || check_secure())
10486 {
10487 rettv->vval.v_number = -1;
10488 return;
10489 }
10490# ifdef WIN32
10491 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10492 if (n == 0)
10493 rettv->vval.v_number = -1;
10494 else
10495 {
10496 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
10497 rettv->vval.v_number = (s != NULL);
10498 }
10499# else
10500 rettv->vval.v_number = 0;
10501 if (check_connection() == FAIL)
10502 return;
10503
10504 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10505 serverStrToWin(get_tv_string(&argvars[0])), &s);
10506# endif
10507
10508 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10509 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010510 v.di_tv.v_type = VAR_STRING;
10511 v.di_tv.vval.v_string = vim_strsave(s);
10512 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
10513 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010514 }
10515#else
10516 rettv->vval.v_number = -1;
10517#endif
10518}
10519
10520/*ARGSUSED*/
10521 static void
10522f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010523 typval_T *argvars;
10524 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010525{
10526 char_u *r = NULL;
10527
10528#ifdef FEAT_CLIENTSERVER
10529 if (!check_restricted() && !check_secure())
10530 {
10531# ifdef WIN32
10532 /* The server's HWND is encoded in the 'id' parameter */
10533 int n = 0;
10534
10535 sscanf(get_tv_string(&argvars[0]), "%x", &n);
10536 if (n != 0)
10537 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
10538 if (r == NULL)
10539# else
10540 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
10541 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
10542# endif
10543 EMSG(_("E277: Unable to read a server reply"));
10544 }
10545#endif
10546 rettv->v_type = VAR_STRING;
10547 rettv->vval.v_string = r;
10548}
10549
10550/*
10551 * "remote_send()" function
10552 */
10553/*ARGSUSED*/
10554 static void
10555f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 typval_T *argvars;
10557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010558{
10559 rettv->v_type = VAR_STRING;
10560 rettv->vval.v_string = NULL;
10561#ifdef FEAT_CLIENTSERVER
10562 remote_common(argvars, rettv, FALSE);
10563#endif
10564}
10565
10566/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010567 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010568 */
10569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010573{
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 list_T *l;
10575 listitem_T *item, *item2;
10576 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010577 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010578 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010579 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 dict_T *d;
10581 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010582
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010583 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010584 if (argvars[0].v_type == VAR_DICT)
10585 {
10586 if (argvars[2].v_type != VAR_UNKNOWN)
10587 EMSG2(_(e_toomanyarg), "remove()");
10588 else if ((d = argvars[0].vval.v_dict) != NULL)
10589 {
10590 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010591 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010592 if (di == NULL)
10593 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010594 else
10595 {
10596 *rettv = di->di_tv;
10597 init_tv(&di->di_tv);
10598 dictitem_remove(d, di);
10599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010600 }
10601 }
10602 else if (argvars[0].v_type != VAR_LIST)
10603 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010604 else if ((l = argvars[0].vval.v_list) != NULL)
10605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010606 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010607 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010608 if (item == NULL)
10609 EMSGN(_(e_listidx), idx);
10610 else
10611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010612 if (argvars[2].v_type == VAR_UNKNOWN)
10613 {
10614 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010615 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010616 *rettv = item->li_tv;
10617 vim_free(item);
10618 }
10619 else
10620 {
10621 /* Remove range of items, return list with values. */
10622 end = get_tv_number(&argvars[2]);
10623 item2 = list_find(l, end);
10624 if (item2 == NULL)
10625 EMSGN(_(e_listidx), end);
10626 else
10627 {
10628 for (li = item; li != item2 && li != NULL; li = li->li_next)
10629 ;
10630 if (li == NULL) /* didn't find "item2" after "item" */
10631 EMSG(_(e_invrange));
10632 else
10633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000010634 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010635 l = list_alloc();
10636 if (l != NULL)
10637 {
10638 rettv->v_type = VAR_LIST;
10639 rettv->vval.v_list = l;
10640 l->lv_first = item;
10641 l->lv_last = item2;
10642 l->lv_refcount = 1;
10643 item->li_prev = NULL;
10644 item2->li_next = NULL;
10645 }
10646 }
10647 }
10648 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010649 }
10650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651}
10652
10653/*
10654 * "rename({from}, {to})" function
10655 */
10656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 typval_T *argvars;
10659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
10661 char_u buf[NUMBUFLEN];
10662
10663 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
10667 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668}
10669
10670/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010671 * "repeat()" function
10672 */
10673/*ARGSUSED*/
10674 static void
10675f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010678{
10679 char_u *p;
10680 int n;
10681 int slen;
10682 int len;
10683 char_u *r;
10684 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010686
10687 n = get_tv_number(&argvars[1]);
10688 if (argvars[0].v_type == VAR_LIST)
10689 {
10690 l = list_alloc();
10691 if (l != NULL && argvars[0].vval.v_list != NULL)
10692 {
10693 l->lv_refcount = 1;
10694 while (n-- > 0)
10695 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
10696 break;
10697 }
10698 rettv->v_type = VAR_LIST;
10699 rettv->vval.v_list = l;
10700 }
10701 else
10702 {
10703 p = get_tv_string(&argvars[0]);
10704 rettv->v_type = VAR_STRING;
10705 rettv->vval.v_string = NULL;
10706
10707 slen = (int)STRLEN(p);
10708 len = slen * n;
10709 if (len <= 0)
10710 return;
10711
10712 r = alloc(len + 1);
10713 if (r != NULL)
10714 {
10715 for (i = 0; i < n; i++)
10716 mch_memmove(r + i * slen, p, (size_t)slen);
10717 r[len] = NUL;
10718 }
10719
10720 rettv->vval.v_string = r;
10721 }
10722}
10723
10724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 * "resolve()" function
10726 */
10727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 typval_T *argvars;
10730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
10732 char_u *p;
10733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735#ifdef FEAT_SHORTCUT
10736 {
10737 char_u *v = NULL;
10738
10739 v = mch_resolve_shortcut(p);
10740 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 }
10745#else
10746# ifdef HAVE_READLINK
10747 {
10748 char_u buf[MAXPATHL + 1];
10749 char_u *cpy;
10750 int len;
10751 char_u *remain = NULL;
10752 char_u *q;
10753 int is_relative_to_current = FALSE;
10754 int has_trailing_pathsep = FALSE;
10755 int limit = 100;
10756
10757 p = vim_strsave(p);
10758
10759 if (p[0] == '.' && (vim_ispathsep(p[1])
10760 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10761 is_relative_to_current = TRUE;
10762
10763 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010764 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 has_trailing_pathsep = TRUE;
10766
10767 q = getnextcomp(p);
10768 if (*q != NUL)
10769 {
10770 /* Separate the first path component in "p", and keep the
10771 * remainder (beginning with the path separator). */
10772 remain = vim_strsave(q - 1);
10773 q[-1] = NUL;
10774 }
10775
10776 for (;;)
10777 {
10778 for (;;)
10779 {
10780 len = readlink((char *)p, (char *)buf, MAXPATHL);
10781 if (len <= 0)
10782 break;
10783 buf[len] = NUL;
10784
10785 if (limit-- == 0)
10786 {
10787 vim_free(p);
10788 vim_free(remain);
10789 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 goto fail;
10792 }
10793
10794 /* Ensure that the result will have a trailing path separator
10795 * if the argument has one. */
10796 if (remain == NULL && has_trailing_pathsep)
10797 add_pathsep(buf);
10798
10799 /* Separate the first path component in the link value and
10800 * concatenate the remainders. */
10801 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10802 if (*q != NUL)
10803 {
10804 if (remain == NULL)
10805 remain = vim_strsave(q - 1);
10806 else
10807 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010808 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 if (cpy != NULL)
10810 {
10811 STRCAT(cpy, remain);
10812 vim_free(remain);
10813 remain = cpy;
10814 }
10815 }
10816 q[-1] = NUL;
10817 }
10818
10819 q = gettail(p);
10820 if (q > p && *q == NUL)
10821 {
10822 /* Ignore trailing path separator. */
10823 q[-1] = NUL;
10824 q = gettail(p);
10825 }
10826 if (q > p && !mch_isFullName(buf))
10827 {
10828 /* symlink is relative to directory of argument */
10829 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10830 if (cpy != NULL)
10831 {
10832 STRCPY(cpy, p);
10833 STRCPY(gettail(cpy), buf);
10834 vim_free(p);
10835 p = cpy;
10836 }
10837 }
10838 else
10839 {
10840 vim_free(p);
10841 p = vim_strsave(buf);
10842 }
10843 }
10844
10845 if (remain == NULL)
10846 break;
10847
10848 /* Append the first path component of "remain" to "p". */
10849 q = getnextcomp(remain + 1);
10850 len = q - remain - (*q != NUL);
10851 cpy = vim_strnsave(p, STRLEN(p) + len);
10852 if (cpy != NULL)
10853 {
10854 STRNCAT(cpy, remain, len);
10855 vim_free(p);
10856 p = cpy;
10857 }
10858 /* Shorten "remain". */
10859 if (*q != NUL)
10860 STRCPY(remain, q - 1);
10861 else
10862 {
10863 vim_free(remain);
10864 remain = NULL;
10865 }
10866 }
10867
10868 /* If the result is a relative path name, make it explicitly relative to
10869 * the current directory if and only if the argument had this form. */
10870 if (!vim_ispathsep(*p))
10871 {
10872 if (is_relative_to_current
10873 && *p != NUL
10874 && !(p[0] == '.'
10875 && (p[1] == NUL
10876 || vim_ispathsep(p[1])
10877 || (p[1] == '.'
10878 && (p[2] == NUL
10879 || vim_ispathsep(p[2]))))))
10880 {
10881 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010882 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 if (cpy != NULL)
10884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 vim_free(p);
10886 p = cpy;
10887 }
10888 }
10889 else if (!is_relative_to_current)
10890 {
10891 /* Strip leading "./". */
10892 q = p;
10893 while (q[0] == '.' && vim_ispathsep(q[1]))
10894 q += 2;
10895 if (q > p)
10896 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10897 }
10898 }
10899
10900 /* Ensure that the result will have no trailing path separator
10901 * if the argument had none. But keep "/" or "//". */
10902 if (!has_trailing_pathsep)
10903 {
10904 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010905 if (after_pathsep(p, q))
10906 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 }
10908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 }
10911# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913# endif
10914#endif
10915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917
10918#ifdef HAVE_READLINK
10919fail:
10920#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922}
10923
10924/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010925 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 */
10927 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010929 typval_T *argvars;
10930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
Bram Moolenaar33570922005-01-25 22:26:29 +000010932 list_T *l;
10933 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934
Bram Moolenaar0d660222005-01-07 21:51:51 +000010935 rettv->vval.v_number = 0;
10936 if (argvars[0].v_type != VAR_LIST)
10937 EMSG2(_(e_listarg), "reverse()");
10938 else if ((l = argvars[0].vval.v_list) != NULL)
10939 {
10940 li = l->lv_last;
10941 l->lv_first = l->lv_last = li;
10942 while (li != NULL)
10943 {
10944 ni = li->li_prev;
10945 list_append(l, li);
10946 li = ni;
10947 }
10948 rettv->vval.v_list = l;
10949 rettv->v_type = VAR_LIST;
10950 ++l->lv_refcount;
10951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952}
10953
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010954#define SP_NOMOVE 1 /* don't move cursor */
10955#define SP_REPEAT 2 /* repeat to find outer pair */
10956#define SP_RETCOUNT 4 /* return matchcount */
10957
Bram Moolenaar33570922005-01-25 22:26:29 +000010958static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959
10960/*
10961 * Get flags for a search function.
10962 * Possibly sets "p_ws".
10963 * Returns BACKWARD, FORWARD or zero (for an error).
10964 */
10965 static int
10966get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010967 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010968 int *flagsp;
10969{
10970 int dir = FORWARD;
10971 char_u *flags;
10972 char_u nbuf[NUMBUFLEN];
10973 int mask;
10974
10975 if (varp->v_type != VAR_UNKNOWN)
10976 {
10977 flags = get_tv_string_buf(varp, nbuf);
10978 while (*flags != NUL)
10979 {
10980 switch (*flags)
10981 {
10982 case 'b': dir = BACKWARD; break;
10983 case 'w': p_ws = TRUE; break;
10984 case 'W': p_ws = FALSE; break;
10985 default: mask = 0;
10986 if (flagsp != NULL)
10987 switch (*flags)
10988 {
10989 case 'n': mask = SP_NOMOVE; break;
10990 case 'r': mask = SP_REPEAT; break;
10991 case 'm': mask = SP_RETCOUNT; break;
10992 }
10993 if (mask == 0)
10994 {
10995 EMSG2(_(e_invarg2), flags);
10996 dir = 0;
10997 }
10998 else
10999 *flagsp |= mask;
11000 }
11001 if (dir == 0)
11002 break;
11003 ++flags;
11004 }
11005 }
11006 return dir;
11007}
11008
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009/*
11010 * "search()" function
11011 */
11012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016{
11017 char_u *pat;
11018 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011019 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 int save_p_ws = p_ws;
11021 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011022 int flags = 0;
11023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011027 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11028 if (dir == 0)
11029 goto theend;
11030 if ((flags & ~SP_NOMOVE) != 0)
11031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011033 goto theend;
11034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011036 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11038 SEARCH_KEEP, RE_SEARCH) != FAIL)
11039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 curwin->w_cursor = pos;
11042 /* "/$" will put the cursor after the end of the line, may need to
11043 * correct that here */
11044 check_cursor();
11045 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011046
11047 /* If 'n' flag is used: restore cursor position. */
11048 if (flags & SP_NOMOVE)
11049 curwin->w_cursor = save_cursor;
11050theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 p_ws = save_p_ws;
11052}
11053
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054/*
11055 * "searchpair()" function
11056 */
11057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011059 typval_T *argvars;
11060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062 char_u *spat, *mpat, *epat;
11063 char_u *skip;
11064 char_u *pat, *pat2, *pat3;
11065 pos_T pos;
11066 pos_T firstpos;
11067 pos_T save_cursor;
11068 pos_T save_pos;
11069 int save_p_ws = p_ws;
11070 char_u *save_cpo;
11071 int dir;
11072 int flags = 0;
11073 char_u nbuf1[NUMBUFLEN];
11074 char_u nbuf2[NUMBUFLEN];
11075 char_u nbuf3[NUMBUFLEN];
11076 int n;
11077 int r;
11078 int nest = 1;
11079 int err;
11080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082
11083 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11084 save_cpo = p_cpo;
11085 p_cpo = (char_u *)"";
11086
11087 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 spat = get_tv_string(&argvars[0]);
11089 mpat = get_tv_string_buf(&argvars[1], nbuf1);
11090 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091
11092 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11093 * start/middle/end (pat3, for the top pair). */
11094 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
11095 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
11096 if (pat2 == NULL || pat3 == NULL)
11097 goto theend;
11098 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
11099 if (*mpat == NUL)
11100 STRCPY(pat3, pat2);
11101 else
11102 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11103 spat, epat, mpat);
11104
11105 /* Handle the optional fourth argument: flags */
11106 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011107 if (dir == 0)
11108 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
11110 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011111 if (argvars[3].v_type == VAR_UNKNOWN
11112 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 skip = (char_u *)"";
11114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116
11117 save_cursor = curwin->w_cursor;
11118 pos = curwin->w_cursor;
11119 firstpos.lnum = 0;
11120 pat = pat3;
11121 for (;;)
11122 {
11123 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11124 SEARCH_KEEP, RE_SEARCH);
11125 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11126 /* didn't find it or found the first match again: FAIL */
11127 break;
11128
11129 if (firstpos.lnum == 0)
11130 firstpos = pos;
11131
11132 /* If the skip pattern matches, ignore this match. */
11133 if (*skip != NUL)
11134 {
11135 save_pos = curwin->w_cursor;
11136 curwin->w_cursor = pos;
11137 r = eval_to_bool(skip, &err, NULL, FALSE);
11138 curwin->w_cursor = save_pos;
11139 if (err)
11140 {
11141 /* Evaluating {skip} caused an error, break here. */
11142 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 break;
11145 }
11146 if (r)
11147 continue;
11148 }
11149
11150 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11151 {
11152 /* Found end when searching backwards or start when searching
11153 * forward: nested pair. */
11154 ++nest;
11155 pat = pat2; /* nested, don't search for middle */
11156 }
11157 else
11158 {
11159 /* Found end when searching forward or start when searching
11160 * backward: end of (nested) pair; or found middle in outer pair. */
11161 if (--nest == 1)
11162 pat = pat3; /* outer level, search for middle */
11163 }
11164
11165 if (nest == 0)
11166 {
11167 /* Found the match: return matchcount or line number. */
11168 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 curwin->w_cursor = pos;
11173 if (!(flags & SP_REPEAT))
11174 break;
11175 nest = 1; /* search for next unmatched */
11176 }
11177 }
11178
11179 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 curwin->w_cursor = save_cursor;
11182
11183theend:
11184 vim_free(pat2);
11185 vim_free(pat3);
11186 p_ws = save_p_ws;
11187 p_cpo = save_cpo;
11188}
11189
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190/*ARGSUSED*/
11191 static void
11192f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011193 typval_T *argvars;
11194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011196#ifdef FEAT_CLIENTSERVER
11197 char_u buf[NUMBUFLEN];
11198 char_u *server = get_tv_string(&argvars[0]);
11199 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201 rettv->vval.v_number = -1;
11202 if (check_restricted() || check_secure())
11203 return;
11204# ifdef FEAT_X11
11205 if (check_connection() == FAIL)
11206 return;
11207# endif
11208
11209 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211 EMSG(_("E258: Unable to send to client"));
11212 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214 rettv->vval.v_number = 0;
11215#else
11216 rettv->vval.v_number = -1;
11217#endif
11218}
11219
11220/*ARGSUSED*/
11221 static void
11222f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 typval_T *argvars;
11224 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011225{
11226 char_u *r = NULL;
11227
11228#ifdef FEAT_CLIENTSERVER
11229# ifdef WIN32
11230 r = serverGetVimNames();
11231# else
11232 make_connection();
11233 if (X_DISPLAY != NULL)
11234 r = serverGetVimNames(X_DISPLAY);
11235# endif
11236#endif
11237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239}
11240
11241/*
11242 * "setbufvar()" function
11243 */
11244/*ARGSUSED*/
11245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 typval_T *argvars;
11248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249{
11250 buf_T *buf;
11251#ifdef FEAT_AUTOCMD
11252 aco_save_T aco;
11253#else
11254 buf_T *save_curbuf;
11255#endif
11256 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 char_u nbuf[NUMBUFLEN];
11259
11260 if (check_restricted() || check_secure())
11261 return;
11262 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 buf = get_buf_tv(&argvars[0]);
11264 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 varp = &argvars[2];
11266
11267 if (buf != NULL && varname != NULL && varp != NULL)
11268 {
11269 /* set curbuf to be our buf, temporarily */
11270#ifdef FEAT_AUTOCMD
11271 aucmd_prepbuf(&aco, buf);
11272#else
11273 save_curbuf = curbuf;
11274 curbuf = buf;
11275#endif
11276
11277 if (*varname == '&')
11278 {
11279 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 set_option_value(varname, get_tv_number(varp),
11281 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 }
11283 else
11284 {
11285 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11286 if (bufvarname != NULL)
11287 {
11288 STRCPY(bufvarname, "b:");
11289 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011290 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 vim_free(bufvarname);
11292 }
11293 }
11294
11295 /* reset notion of buffer */
11296#ifdef FEAT_AUTOCMD
11297 aucmd_restbuf(&aco);
11298#else
11299 curbuf = save_curbuf;
11300#endif
11301 }
11302 --emsg_off;
11303}
11304
11305/*
11306 * "setcmdpos()" function
11307 */
11308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011310 typval_T *argvars;
11311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->vval.v_number = set_cmdline_pos(
11314 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315}
11316
11317/*
11318 * "setline()" function
11319 */
11320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325 linenr_T lnum;
11326 char_u *line;
11327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 lnum = get_tv_lnum(argvars);
11329 line = get_tv_string(&argvars[1]);
11330 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331
11332 if (lnum >= 1
11333 && lnum <= curbuf->b_ml.ml_line_count
11334 && u_savesub(lnum) == OK
11335 && ml_replace(lnum, line, TRUE) == OK)
11336 {
11337 changed_bytes(lnum, 0);
11338 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 }
11341}
11342
11343/*
11344 * "setreg()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
11351 int regname;
11352 char_u *strregname;
11353 char_u *stropt;
11354 int append;
11355 char_u yank_type;
11356 long block_len;
11357
11358 block_len = -1;
11359 yank_type = MAUTO;
11360 append = FALSE;
11361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 strregname = get_tv_string(argvars);
11363 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364
11365 regname = (strregname == NULL ? '"' : *strregname);
11366 if (regname == 0 || regname == '@')
11367 regname = '"';
11368 else if (regname == '=')
11369 return;
11370
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011371 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 switch (*stropt)
11375 {
11376 case 'a': case 'A': /* append */
11377 append = TRUE;
11378 break;
11379 case 'v': case 'c': /* character-wise selection */
11380 yank_type = MCHAR;
11381 break;
11382 case 'V': case 'l': /* line-wise selection */
11383 yank_type = MLINE;
11384 break;
11385#ifdef FEAT_VISUAL
11386 case 'b': case Ctrl_V: /* block-wise selection */
11387 yank_type = MBLOCK;
11388 if (VIM_ISDIGIT(stropt[1]))
11389 {
11390 ++stropt;
11391 block_len = getdigits(&stropt) - 1;
11392 --stropt;
11393 }
11394 break;
11395#endif
11396 }
11397 }
11398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403
11404
11405/*
11406 * "setwinvar(expr)" function
11407 */
11408/*ARGSUSED*/
11409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414 win_T *win;
11415#ifdef FEAT_WINDOWS
11416 win_T *save_curwin;
11417#endif
11418 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011419 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 char_u nbuf[NUMBUFLEN];
11421
11422 if (check_restricted() || check_secure())
11423 return;
11424 ++emsg_off;
11425 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 varp = &argvars[2];
11428
11429 if (win != NULL && varname != NULL && varp != NULL)
11430 {
11431#ifdef FEAT_WINDOWS
11432 /* set curwin to be our win, temporarily */
11433 save_curwin = curwin;
11434 curwin = win;
11435 curbuf = curwin->w_buffer;
11436#endif
11437
11438 if (*varname == '&')
11439 {
11440 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 set_option_value(varname, get_tv_number(varp),
11442 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 }
11444 else
11445 {
11446 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11447 if (winvarname != NULL)
11448 {
11449 STRCPY(winvarname, "w:");
11450 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011451 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 vim_free(winvarname);
11453 }
11454 }
11455
11456#ifdef FEAT_WINDOWS
11457 /* Restore current window, if it's still valid (autocomands can make
11458 * it invalid). */
11459 if (win_valid(save_curwin))
11460 {
11461 curwin = save_curwin;
11462 curbuf = curwin->w_buffer;
11463 }
11464#endif
11465 }
11466 --emsg_off;
11467}
11468
11469/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011470 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 */
11472 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011473f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 typval_T *argvars;
11475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011477 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
Bram Moolenaar0d660222005-01-07 21:51:51 +000011479 p = get_tv_string(&argvars[0]);
11480 rettv->vval.v_string = vim_strsave(p);
11481 simplify_filename(rettv->vval.v_string); /* simplify in place */
11482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483}
11484
Bram Moolenaar0d660222005-01-07 21:51:51 +000011485static int
11486#ifdef __BORLANDC__
11487 _RTLENTRYF
11488#endif
11489 item_compare __ARGS((const void *s1, const void *s2));
11490static int
11491#ifdef __BORLANDC__
11492 _RTLENTRYF
11493#endif
11494 item_compare2 __ARGS((const void *s1, const void *s2));
11495
11496static int item_compare_ic;
11497static char_u *item_compare_func;
11498#define ITEM_COMPARE_FAIL 999
11499
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 static int
11504#ifdef __BORLANDC__
11505_RTLENTRYF
11506#endif
11507item_compare(s1, s2)
11508 const void *s1;
11509 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511 char_u *p1, *p2;
11512 char_u *tofree1, *tofree2;
11513 int res;
11514 char_u numbuf1[NUMBUFLEN];
11515 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
11518 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011519 if (item_compare_ic)
11520 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522 res = STRCMP(p1, p2);
11523 vim_free(tofree1);
11524 vim_free(tofree2);
11525 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526}
11527
11528 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529#ifdef __BORLANDC__
11530_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532item_compare2(s1, s2)
11533 const void *s1;
11534 const void *s2;
11535{
11536 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 typval_T rettv;
11538 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000011539 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541 /* copy the values (is this really needed?) */
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
11543 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544
11545 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11546 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000011547 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548 clear_tv(&argv[0]);
11549 clear_tv(&argv[1]);
11550
11551 if (res == FAIL)
11552 res = ITEM_COMPARE_FAIL;
11553 else
11554 res = get_tv_number(&rettv);
11555 clear_tv(&rettv);
11556 return res;
11557}
11558
11559/*
11560 * "sort({list})" function
11561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011563f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 typval_T *argvars;
11565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566{
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 list_T *l;
11568 listitem_T *li;
11569 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011570 long len;
11571 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572
Bram Moolenaar0d660222005-01-07 21:51:51 +000011573 rettv->vval.v_number = 0;
11574 if (argvars[0].v_type != VAR_LIST)
11575 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 else
11577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011578 l = argvars[0].vval.v_list;
11579 if (l == NULL)
11580 return;
11581 rettv->vval.v_list = l;
11582 rettv->v_type = VAR_LIST;
11583 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585 len = list_len(l);
11586 if (len <= 1)
11587 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589 item_compare_ic = FALSE;
11590 item_compare_func = NULL;
11591 if (argvars[1].v_type != VAR_UNKNOWN)
11592 {
11593 if (argvars[1].v_type == VAR_FUNC)
11594 item_compare_func = argvars[0].vval.v_string;
11595 else
11596 {
11597 i = get_tv_number(&argvars[1]);
11598 if (i == 1)
11599 item_compare_ic = TRUE;
11600 else
11601 item_compare_func = get_tv_string(&argvars[1]);
11602 }
11603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604
Bram Moolenaar0d660222005-01-07 21:51:51 +000011605 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607 if (ptrs == NULL)
11608 return;
11609 i = 0;
11610 for (li = l->lv_first; li != NULL; li = li->li_next)
11611 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613 /* test the compare function */
11614 if (item_compare_func != NULL
11615 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11616 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011617 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000011619 {
11620 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000011622 item_compare_func == NULL ? item_compare : item_compare2);
11623
11624 /* Clear the List and append the items in the sorted order. */
11625 l->lv_first = l->lv_last = NULL;
11626 for (i = 0; i < len; ++i)
11627 list_append(l, ptrs[i]);
11628 }
11629
11630 vim_free(ptrs);
11631 }
11632}
11633
11634 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011635f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011636 typval_T *argvars;
11637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011638{
11639 char_u *str;
11640 char_u *end;
11641 char_u *pat;
11642 regmatch_T regmatch;
11643 char_u patbuf[NUMBUFLEN];
11644 char_u *save_cpo;
11645 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000011646 listitem_T *ni;
11647 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011648 colnr_T col = 0;
11649
11650 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11651 save_cpo = p_cpo;
11652 p_cpo = (char_u *)"";
11653
11654 str = get_tv_string(&argvars[0]);
11655 if (argvars[1].v_type == VAR_UNKNOWN)
11656 pat = (char_u *)"[\\x01- ]\\+";
11657 else
11658 pat = get_tv_string_buf(&argvars[1], patbuf);
11659
11660 l = list_alloc();
11661 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663 rettv->v_type = VAR_LIST;
11664 rettv->vval.v_list = l;
11665 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11668 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 regmatch.rm_ic = FALSE;
11671 while (*str != NUL)
11672 {
11673 match = vim_regexec_nl(&regmatch, str, col);
11674 if (match)
11675 end = regmatch.startp[0];
11676 else
11677 end = str + STRLEN(str);
11678 if (end > str)
11679 {
11680 ni = listitem_alloc();
11681 if (ni == NULL)
11682 break;
11683 ni->li_tv.v_type = VAR_STRING;
11684 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
11685 list_append(l, ni);
11686 }
11687 if (!match)
11688 break;
11689 /* Advance to just after the match. */
11690 if (regmatch.endp[0] > str)
11691 col = 0;
11692 else
11693 {
11694 /* Don't get stuck at the same match. */
11695#ifdef FEAT_MBYTE
11696 col = mb_ptr2len_check(regmatch.endp[0]);
11697#else
11698 col = 1;
11699#endif
11700 }
11701 str = regmatch.endp[0];
11702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703
Bram Moolenaar0d660222005-01-07 21:51:51 +000011704 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708}
11709
11710#ifdef HAVE_STRFTIME
11711/*
11712 * "strftime({format}[, {time}])" function
11713 */
11714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 typval_T *argvars;
11717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718{
11719 char_u result_buf[256];
11720 struct tm *curtime;
11721 time_t seconds;
11722 char_u *p;
11723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011727 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 seconds = time(NULL);
11729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 curtime = localtime(&seconds);
11732 /* MSVC returns NULL for an invalid value of seconds. */
11733 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 else
11736 {
11737# ifdef FEAT_MBYTE
11738 vimconv_T conv;
11739 char_u *enc;
11740
11741 conv.vc_type = CONV_NONE;
11742 enc = enc_locale();
11743 convert_setup(&conv, p_enc, enc);
11744 if (conv.vc_type != CONV_NONE)
11745 p = string_convert(&conv, p, NULL);
11746# endif
11747 if (p != NULL)
11748 (void)strftime((char *)result_buf, sizeof(result_buf),
11749 (char *)p, curtime);
11750 else
11751 result_buf[0] = NUL;
11752
11753# ifdef FEAT_MBYTE
11754 if (conv.vc_type != CONV_NONE)
11755 vim_free(p);
11756 convert_setup(&conv, enc, p_enc);
11757 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 else
11760# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762
11763# ifdef FEAT_MBYTE
11764 /* Release conversion descriptors */
11765 convert_setup(&conv, NULL, NULL);
11766 vim_free(enc);
11767# endif
11768 }
11769}
11770#endif
11771
11772/*
11773 * "stridx()" function
11774 */
11775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *argvars;
11778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779{
11780 char_u buf[NUMBUFLEN];
11781 char_u *needle;
11782 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000011785 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000011788 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
11789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 if (argvars[2].v_type != VAR_UNKNOWN)
11792 {
11793 start_idx = get_tv_number(&argvars[2]);
11794 if (start_idx < 0 || start_idx >= (int)STRLEN(haystack))
11795 return;
11796 haystack += start_idx;
11797 }
11798
11799 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11800 if (pos != NULL)
11801 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802}
11803
11804/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011805 * "string()" function
11806 */
11807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011809 typval_T *argvars;
11810 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811{
11812 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011813 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011816 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819}
11820
11821/*
11822 * "strlen()" function
11823 */
11824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *argvars;
11827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = (varnumber_T)(STRLEN(
11830 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831}
11832
11833/*
11834 * "strpart()" function
11835 */
11836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 typval_T *argvars;
11839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840{
11841 char_u *p;
11842 int n;
11843 int len;
11844 int slen;
11845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 slen = (int)STRLEN(p);
11848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 else
11853 len = slen - n; /* default len: all bytes that are available. */
11854
11855 /*
11856 * Only return the overlap between the specified part and the actual
11857 * string.
11858 */
11859 if (n < 0)
11860 {
11861 len += n;
11862 n = 0;
11863 }
11864 else if (n > slen)
11865 n = slen;
11866 if (len < 0)
11867 len = 0;
11868 else if (n + len > slen)
11869 len = slen - n;
11870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->v_type = VAR_STRING;
11872 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873}
11874
11875/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011876 * "strridx()" function
11877 */
11878 static void
11879f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 typval_T *argvars;
11881 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882{
11883 char_u buf[NUMBUFLEN];
11884 char_u *needle;
11885 char_u *haystack;
11886 char_u *rest;
11887 char_u *lastmatch = NULL;
11888
11889 needle = get_tv_string(&argvars[1]);
11890 haystack = get_tv_string_buf(&argvars[0], buf);
11891 if (*needle == NUL)
11892 /* Empty string matches past the end. */
11893 lastmatch = haystack + STRLEN(haystack);
11894 else
11895 for (rest = haystack; *rest != '\0'; ++rest)
11896 {
11897 rest = (char_u *)strstr((char *)rest, (char *)needle);
11898 if (rest == NULL)
11899 break;
11900 lastmatch = rest;
11901 }
11902
11903 if (lastmatch == NULL)
11904 rettv->vval.v_number = -1;
11905 else
11906 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11907}
11908
11909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 * "strtrans()" function
11911 */
11912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011914 typval_T *argvars;
11915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917 rettv->v_type = VAR_STRING;
11918 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919}
11920
11921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011922 * "submatch()" function
11923 */
11924 static void
11925f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 typval_T *argvars;
11927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928{
11929 rettv->v_type = VAR_STRING;
11930 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11931}
11932
11933/*
11934 * "substitute()" function
11935 */
11936 static void
11937f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *argvars;
11939 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011940{
11941 char_u patbuf[NUMBUFLEN];
11942 char_u subbuf[NUMBUFLEN];
11943 char_u flagsbuf[NUMBUFLEN];
11944
11945 rettv->v_type = VAR_STRING;
11946 rettv->vval.v_string = do_string_sub(
11947 get_tv_string(&argvars[0]),
11948 get_tv_string_buf(&argvars[1], patbuf),
11949 get_tv_string_buf(&argvars[2], subbuf),
11950 get_tv_string_buf(&argvars[3], flagsbuf));
11951}
11952
11953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 * "synID(line, col, trans)" function
11955 */
11956/*ARGSUSED*/
11957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 typval_T *argvars;
11960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
11962 int id = 0;
11963#ifdef FEAT_SYN_HL
11964 long line;
11965 long col;
11966 int trans;
11967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 line = get_tv_lnum(argvars);
11969 col = get_tv_number(&argvars[1]) - 1;
11970 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971
11972 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
11973 && col >= 0 && col < (long)STRLEN(ml_get(line)))
11974 id = syn_get_id(line, col, trans);
11975#endif
11976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978}
11979
11980/*
11981 * "synIDattr(id, what [, mode])" function
11982 */
11983/*ARGSUSED*/
11984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 typval_T *argvars;
11987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988{
11989 char_u *p = NULL;
11990#ifdef FEAT_SYN_HL
11991 int id;
11992 char_u *what;
11993 char_u *mode;
11994 char_u modebuf[NUMBUFLEN];
11995 int modec;
11996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 id = get_tv_number(&argvars[0]);
11998 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 modec = TOLOWER_ASC(mode[0]);
12003 if (modec != 't' && modec != 'c'
12004#ifdef FEAT_GUI
12005 && modec != 'g'
12006#endif
12007 )
12008 modec = 0; /* replace invalid with current */
12009 }
12010 else
12011 {
12012#ifdef FEAT_GUI
12013 if (gui.in_use)
12014 modec = 'g';
12015 else
12016#endif
12017 if (t_colors > 1)
12018 modec = 'c';
12019 else
12020 modec = 't';
12021 }
12022
12023
12024 switch (TOLOWER_ASC(what[0]))
12025 {
12026 case 'b':
12027 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12028 p = highlight_color(id, what, modec);
12029 else /* bold */
12030 p = highlight_has_attr(id, HL_BOLD, modec);
12031 break;
12032
12033 case 'f': /* fg[#] */
12034 p = highlight_color(id, what, modec);
12035 break;
12036
12037 case 'i':
12038 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12039 p = highlight_has_attr(id, HL_INVERSE, modec);
12040 else /* italic */
12041 p = highlight_has_attr(id, HL_ITALIC, modec);
12042 break;
12043
12044 case 'n': /* name */
12045 p = get_highlight_name(NULL, id - 1);
12046 break;
12047
12048 case 'r': /* reverse */
12049 p = highlight_has_attr(id, HL_INVERSE, modec);
12050 break;
12051
12052 case 's': /* standout */
12053 p = highlight_has_attr(id, HL_STANDOUT, modec);
12054 break;
12055
12056 case 'u': /* underline */
12057 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12058 break;
12059 }
12060
12061 if (p != NULL)
12062 p = vim_strsave(p);
12063#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->v_type = VAR_STRING;
12065 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066}
12067
12068/*
12069 * "synIDtrans(id)" function
12070 */
12071/*ARGSUSED*/
12072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012074 typval_T *argvars;
12075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076{
12077 int id;
12078
12079#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081
12082 if (id > 0)
12083 id = syn_get_final_id(id);
12084 else
12085#endif
12086 id = 0;
12087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089}
12090
12091/*
12092 * "system()" function
12093 */
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012099 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012101 char_u *infile = NULL;
12102 char_u buf[NUMBUFLEN];
12103 int err = FALSE;
12104 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012106 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012107 {
12108 /*
12109 * Write the string to a temp file, to be used for input of the shell
12110 * command.
12111 */
12112 if ((infile = vim_tempname('i')) == NULL)
12113 {
12114 EMSG(_(e_notmp));
12115 return;
12116 }
12117
12118 fd = mch_fopen((char *)infile, WRITEBIN);
12119 if (fd == NULL)
12120 {
12121 EMSG2(_(e_notopen), infile);
12122 goto done;
12123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012124 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012125 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12126 err = TRUE;
12127 if (fclose(fd) != 0)
12128 err = TRUE;
12129 if (err)
12130 {
12131 EMSG(_("E677: Error writing temp file"));
12132 goto done;
12133 }
12134 }
12135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012137
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138#ifdef USE_CR
12139 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012140 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 {
12142 char_u *s;
12143
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012144 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 {
12146 if (*s == CAR)
12147 *s = NL;
12148 }
12149 }
12150#else
12151# ifdef USE_CRNL
12152 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012153 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 {
12155 char_u *s, *d;
12156
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012157 d = res;
12158 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 {
12160 if (s[0] == CAR && s[1] == NL)
12161 ++s;
12162 *d++ = *s;
12163 }
12164 *d = NUL;
12165 }
12166# endif
12167#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012168
12169done:
12170 if (infile != NULL)
12171 {
12172 mch_remove(infile);
12173 vim_free(infile);
12174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 rettv->v_type = VAR_STRING;
12176 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177}
12178
12179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 * "tempname()" function
12181 */
12182/*ARGSUSED*/
12183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012185 typval_T *argvars;
12186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187{
12188 static int x = 'A';
12189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->v_type = VAR_STRING;
12191 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192
12193 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12194 * names. Skip 'I' and 'O', they are used for shell redirection. */
12195 do
12196 {
12197 if (x == 'Z')
12198 x = '0';
12199 else if (x == '9')
12200 x = 'A';
12201 else
12202 {
12203#ifdef EBCDIC
12204 if (x == 'I')
12205 x = 'J';
12206 else if (x == 'R')
12207 x = 'S';
12208 else
12209#endif
12210 ++x;
12211 }
12212 } while (x == 'I' || x == 'O');
12213}
12214
12215/*
12216 * "tolower(string)" function
12217 */
12218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012220 typval_T *argvars;
12221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
12223 char_u *p;
12224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 p = vim_strsave(get_tv_string(&argvars[0]));
12226 rettv->v_type = VAR_STRING;
12227 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228
12229 if (p != NULL)
12230 while (*p != NUL)
12231 {
12232#ifdef FEAT_MBYTE
12233 int l;
12234
12235 if (enc_utf8)
12236 {
12237 int c, lc;
12238
12239 c = utf_ptr2char(p);
12240 lc = utf_tolower(c);
12241 l = utf_ptr2len_check(p);
12242 /* TODO: reallocate string when byte count changes. */
12243 if (utf_char2len(lc) == l)
12244 utf_char2bytes(lc, p);
12245 p += l;
12246 }
12247 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12248 p += l; /* skip multi-byte character */
12249 else
12250#endif
12251 {
12252 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12253 ++p;
12254 }
12255 }
12256}
12257
12258/*
12259 * "toupper(string)" function
12260 */
12261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012263 typval_T *argvars;
12264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265{
12266 char_u *p;
12267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 p = vim_strsave(get_tv_string(&argvars[0]));
12269 rettv->v_type = VAR_STRING;
12270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271
12272 if (p != NULL)
12273 while (*p != NUL)
12274 {
12275#ifdef FEAT_MBYTE
12276 int l;
12277
12278 if (enc_utf8)
12279 {
12280 int c, uc;
12281
12282 c = utf_ptr2char(p);
12283 uc = utf_toupper(c);
12284 l = utf_ptr2len_check(p);
12285 /* TODO: reallocate string when byte count changes. */
12286 if (utf_char2len(uc) == l)
12287 utf_char2bytes(uc, p);
12288 p += l;
12289 }
12290 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12291 p += l; /* skip multi-byte character */
12292 else
12293#endif
12294 {
12295 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12296 p++;
12297 }
12298 }
12299}
12300
12301/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012302 * "tr(string, fromstr, tostr)" function
12303 */
12304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012305f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012306 typval_T *argvars;
12307 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012308{
12309 char_u *instr;
12310 char_u *fromstr;
12311 char_u *tostr;
12312 char_u *p;
12313#ifdef FEAT_MBYTE
12314 int inlen;
12315 int fromlen;
12316 int tolen;
12317 int idx;
12318 char_u *cpstr;
12319 int cplen;
12320 int first = TRUE;
12321#endif
12322 char_u buf[NUMBUFLEN];
12323 char_u buf2[NUMBUFLEN];
12324 garray_T ga;
12325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 instr = get_tv_string(&argvars[0]);
12327 fromstr = get_tv_string_buf(&argvars[1], buf);
12328 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012329
12330 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331 rettv->v_type = VAR_STRING;
12332 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012333 ga_init2(&ga, (int)sizeof(char), 80);
12334
12335#ifdef FEAT_MBYTE
12336 if (!has_mbyte)
12337#endif
12338 /* not multi-byte: fromstr and tostr must be the same length */
12339 if (STRLEN(fromstr) != STRLEN(tostr))
12340 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012341#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012342error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012343#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012344 EMSG2(_(e_invarg2), fromstr);
12345 ga_clear(&ga);
12346 return;
12347 }
12348
12349 /* fromstr and tostr have to contain the same number of chars */
12350 while (*instr != NUL)
12351 {
12352#ifdef FEAT_MBYTE
12353 if (has_mbyte)
12354 {
12355 inlen = mb_ptr2len_check(instr);
12356 cpstr = instr;
12357 cplen = inlen;
12358 idx = 0;
12359 for (p = fromstr; *p != NUL; p += fromlen)
12360 {
12361 fromlen = mb_ptr2len_check(p);
12362 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12363 {
12364 for (p = tostr; *p != NUL; p += tolen)
12365 {
12366 tolen = mb_ptr2len_check(p);
12367 if (idx-- == 0)
12368 {
12369 cplen = tolen;
12370 cpstr = p;
12371 break;
12372 }
12373 }
12374 if (*p == NUL) /* tostr is shorter than fromstr */
12375 goto error;
12376 break;
12377 }
12378 ++idx;
12379 }
12380
12381 if (first && cpstr == instr)
12382 {
12383 /* Check that fromstr and tostr have the same number of
12384 * (multi-byte) characters. Done only once when a character
12385 * of instr doesn't appear in fromstr. */
12386 first = FALSE;
12387 for (p = tostr; *p != NUL; p += tolen)
12388 {
12389 tolen = mb_ptr2len_check(p);
12390 --idx;
12391 }
12392 if (idx != 0)
12393 goto error;
12394 }
12395
12396 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012397 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012398 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012399
12400 instr += inlen;
12401 }
12402 else
12403#endif
12404 {
12405 /* When not using multi-byte chars we can do it faster. */
12406 p = vim_strchr(fromstr, *instr);
12407 if (p != NULL)
12408 ga_append(&ga, tostr[p - fromstr]);
12409 else
12410 ga_append(&ga, *instr);
12411 ++instr;
12412 }
12413 }
12414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012416}
12417
12418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 * "type(expr)" function
12420 */
12421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012423 typval_T *argvars;
12424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012426 int n;
12427
12428 switch (argvars[0].v_type)
12429 {
12430 case VAR_NUMBER: n = 0; break;
12431 case VAR_STRING: n = 1; break;
12432 case VAR_FUNC: n = 2; break;
12433 case VAR_LIST: n = 3; break;
12434 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12435 }
12436 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437}
12438
12439/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012440 * "values(dict)" function
12441 */
12442 static void
12443f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012444 typval_T *argvars;
12445 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012446{
12447 dict_list(argvars, rettv, 1);
12448}
12449
12450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451 * "virtcol(string)" function
12452 */
12453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012455 typval_T *argvars;
12456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457{
12458 colnr_T vcol = 0;
12459 pos_T *fp;
12460
12461 fp = var2fpos(&argvars[0], FALSE);
12462 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
12463 {
12464 getvvcol(curwin, fp, NULL, NULL, &vcol);
12465 ++vcol;
12466 }
12467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469}
12470
12471/*
12472 * "visualmode()" function
12473 */
12474/*ARGSUSED*/
12475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012477 typval_T *argvars;
12478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479{
12480#ifdef FEAT_VISUAL
12481 char_u str[2];
12482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 str[0] = curbuf->b_visual_mode_eval;
12485 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487
12488 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012489 if ((argvars[0].v_type == VAR_NUMBER
12490 && argvars[0].vval.v_number != 0)
12491 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 curbuf->b_visual_mode_eval = NUL;
12494#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496#endif
12497}
12498
12499/*
12500 * "winbufnr(nr)" function
12501 */
12502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012504 typval_T *argvars;
12505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506{
12507 win_T *wp;
12508
12509 wp = find_win_by_nr(&argvars[0]);
12510 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514}
12515
12516/*
12517 * "wincol()" function
12518 */
12519/*ARGSUSED*/
12520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *argvars;
12523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524{
12525 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527}
12528
12529/*
12530 * "winheight(nr)" function
12531 */
12532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
12537 win_T *wp;
12538
12539 wp = find_win_by_nr(&argvars[0]);
12540 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544}
12545
12546/*
12547 * "winline()" function
12548 */
12549/*ARGSUSED*/
12550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012552 typval_T *argvars;
12553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554{
12555 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558
12559/*
12560 * "winnr()" function
12561 */
12562/* ARGSUSED */
12563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012565 typval_T *argvars;
12566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567{
12568 int nr = 1;
12569#ifdef FEAT_WINDOWS
12570 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012571 win_T *twin = curwin;
12572 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012574 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012577 if (STRCMP(arg, "$") == 0)
12578 twin = lastwin;
12579 else if (STRCMP(arg, "#") == 0)
12580 {
12581 twin = prevwin;
12582 if (prevwin == NULL)
12583 nr = 0;
12584 }
12585 else
12586 {
12587 EMSG2(_(e_invexpr2), arg);
12588 nr = 0;
12589 }
12590 }
12591
12592 if (nr > 0)
12593 for (wp = firstwin; wp != twin; wp = wp->w_next)
12594 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597}
12598
12599/*
12600 * "winrestcmd()" function
12601 */
12602/* ARGSUSED */
12603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 typval_T *argvars;
12606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607{
12608#ifdef FEAT_WINDOWS
12609 win_T *wp;
12610 int winnr = 1;
12611 garray_T ga;
12612 char_u buf[50];
12613
12614 ga_init2(&ga, (int)sizeof(char), 70);
12615 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12616 {
12617 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12618 ga_concat(&ga, buf);
12619# ifdef FEAT_VERTSPLIT
12620 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12621 ga_concat(&ga, buf);
12622# endif
12623 ++winnr;
12624 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000012625 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632}
12633
12634/*
12635 * "winwidth(nr)" function
12636 */
12637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641{
12642 win_T *wp;
12643
12644 wp = find_win_by_nr(&argvars[0]);
12645 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 else
12648#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#endif
12653}
12654
12655 static win_T *
12656find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
12659#ifdef FEAT_WINDOWS
12660 win_T *wp;
12661#endif
12662 int nr;
12663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
12666#ifdef FEAT_WINDOWS
12667 if (nr == 0)
12668 return curwin;
12669
12670 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12671 if (--nr <= 0)
12672 break;
12673 return wp;
12674#else
12675 if (nr == 0 || nr == 1)
12676 return curwin;
12677 return NULL;
12678#endif
12679}
12680
12681/*
12682 * Translate a String variable into a position.
12683 */
12684 static pos_T *
12685var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 int lnum; /* TRUE when $ is last line */
12688{
12689 char_u *name;
12690 static pos_T pos;
12691 pos_T *pp;
12692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 if (name[0] == '.') /* cursor */
12695 return &curwin->w_cursor;
12696 if (name[0] == '\'') /* mark */
12697 {
12698 pp = getmark(name[1], FALSE);
12699 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
12700 return NULL;
12701 return pp;
12702 }
12703 if (name[0] == '$') /* last column or line */
12704 {
12705 if (lnum)
12706 {
12707 pos.lnum = curbuf->b_ml.ml_line_count;
12708 pos.col = 0;
12709 }
12710 else
12711 {
12712 pos.lnum = curwin->w_cursor.lnum;
12713 pos.col = (colnr_T)STRLEN(ml_get_curline());
12714 }
12715 return &pos;
12716 }
12717 return NULL;
12718}
12719
12720/*
12721 * Get the length of an environment variable name.
12722 * Advance "arg" to the first character after the name.
12723 * Return 0 for error.
12724 */
12725 static int
12726get_env_len(arg)
12727 char_u **arg;
12728{
12729 char_u *p;
12730 int len;
12731
12732 for (p = *arg; vim_isIDc(*p); ++p)
12733 ;
12734 if (p == *arg) /* no name found */
12735 return 0;
12736
12737 len = (int)(p - *arg);
12738 *arg = p;
12739 return len;
12740}
12741
12742/*
12743 * Get the length of the name of a function or internal variable.
12744 * "arg" is advanced to the first non-white character after the name.
12745 * Return 0 if something is wrong.
12746 */
12747 static int
12748get_id_len(arg)
12749 char_u **arg;
12750{
12751 char_u *p;
12752 int len;
12753
12754 /* Find the end of the name. */
12755 for (p = *arg; eval_isnamec(*p); ++p)
12756 ;
12757 if (p == *arg) /* no name found */
12758 return 0;
12759
12760 len = (int)(p - *arg);
12761 *arg = skipwhite(p);
12762
12763 return len;
12764}
12765
12766/*
Bram Moolenaara7043832005-01-21 11:56:39 +000012767 * Get the length of the name of a variable or function.
12768 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 * "arg" is advanced to the first non-white character after the name.
12770 * Return 0 if something is wrong.
12771 * If the name contains 'magic' {}'s, expand them and return the
12772 * expanded name in an allocated string via 'alias' - caller must free.
12773 */
12774 static int
Bram Moolenaara7043832005-01-21 11:56:39 +000012775get_name_len(arg, alias, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 char_u **arg;
12777 char_u **alias;
12778 int evaluate;
12779{
12780 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 char_u *p;
12782 char_u *expr_start;
12783 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784
12785 *alias = NULL; /* default to no alias */
12786
12787 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
12788 && (*arg)[2] == (int)KE_SNR)
12789 {
12790 /* hard coded <SNR>, already translated */
12791 *arg += 3;
12792 return get_id_len(arg) + 3;
12793 }
12794 len = eval_fname_script(*arg);
12795 if (len > 0)
12796 {
12797 /* literal "<SID>", "s:" or "<SNR>" */
12798 *arg += len;
12799 }
12800
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 if (expr_start != NULL)
12806 {
12807 char_u *temp_string;
12808
12809 if (!evaluate)
12810 {
12811 len += (int)(p - *arg);
12812 *arg = skipwhite(p);
12813 return len;
12814 }
12815
12816 /*
12817 * Include any <SID> etc in the expanded string:
12818 * Thus the -len here.
12819 */
12820 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
12821 if (temp_string == NULL)
12822 return 0;
12823 *alias = temp_string;
12824 *arg = skipwhite(p);
12825 return (int)STRLEN(temp_string);
12826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827
12828 len += get_id_len(arg);
12829 if (len == 0)
12830 EMSG2(_(e_invexpr2), *arg);
12831
12832 return len;
12833}
12834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835/*
12836 * Find the end of a variable or function name, taking care of magic braces.
12837 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
12838 * start and end of the first magic braces item.
12839 * Return a pointer to just after the name. Equal to "arg" if there is no
12840 * valid name.
12841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 char_u *arg;
12845 char_u **expr_start;
12846 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012847 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 int mb_nest = 0;
12850 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 char_u *p;
12852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 *expr_start = NULL;
12856 *expr_end = NULL;
12857 }
12858
12859 for (p = arg; *p != NUL
12860 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012861 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000012862 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 || mb_nest != 0
12864 || br_nest != 0); ++p)
12865 {
12866 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 if (*p == '[')
12869 ++br_nest;
12870 else if (*p == ']')
12871 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 if (*p == '{')
12876 {
12877 mb_nest++;
12878 if (expr_start != NULL && *expr_start == NULL)
12879 *expr_start = p;
12880 }
12881 else if (*p == '}')
12882 {
12883 mb_nest--;
12884 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12885 *expr_end = p;
12886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 }
12889
12890 return p;
12891}
12892
12893/*
12894 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000012895 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 */
12897 static int
12898eval_isnamec(c)
12899 int c;
12900{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012901 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902}
12903
12904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 * Set number v: variable to "val".
12906 */
12907 void
12908set_vim_var_nr(idx, val)
12909 int idx;
12910 long val;
12911{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012912 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913}
12914
12915/*
12916 * Get number v: variable value;
12917 */
12918 long
12919get_vim_var_nr(idx)
12920 int idx;
12921{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012922 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923}
12924
12925/*
12926 * Set v:count, v:count1 and v:prevcount.
12927 */
12928 void
12929set_vcount(count, count1)
12930 long count;
12931 long count1;
12932{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012933 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
12934 vimvars[VV_COUNT].vv_nr = count;
12935 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936}
12937
12938/*
12939 * Set string v: variable to a copy of "val".
12940 */
12941 void
12942set_vim_var_string(idx, val, len)
12943 int idx;
12944 char_u *val;
12945 int len; /* length of "val" to use or -1 (whole string) */
12946{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012947 /* Need to do this (at least) once, since we can't initialize a union.
12948 * Will always be invoked when "v:progname" is set. */
12949 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
12950
Bram Moolenaare9a41262005-01-15 22:18:47 +000012951 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012953 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012955 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012957 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958}
12959
12960/*
12961 * Set v:register if needed.
12962 */
12963 void
12964set_reg_var(c)
12965 int c;
12966{
12967 char_u regname;
12968
12969 if (c == 0 || c == ' ')
12970 regname = '"';
12971 else
12972 regname = c;
12973 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012974 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 set_vim_var_string(VV_REG, &regname, 1);
12976}
12977
12978/*
12979 * Get or set v:exception. If "oldval" == NULL, return the current value.
12980 * Otherwise, restore the value to "oldval" and return NULL.
12981 * Must always be called in pairs to save and restore v:exception! Does not
12982 * take care of memory allocations.
12983 */
12984 char_u *
12985v_exception(oldval)
12986 char_u *oldval;
12987{
12988 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012989 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990
Bram Moolenaare9a41262005-01-15 22:18:47 +000012991 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 return NULL;
12993}
12994
12995/*
12996 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
12997 * Otherwise, restore the value to "oldval" and return NULL.
12998 * Must always be called in pairs to save and restore v:throwpoint! Does not
12999 * take care of memory allocations.
13000 */
13001 char_u *
13002v_throwpoint(oldval)
13003 char_u *oldval;
13004{
13005 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013006 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007
Bram Moolenaare9a41262005-01-15 22:18:47 +000013008 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 return NULL;
13010}
13011
13012#if defined(FEAT_AUTOCMD) || defined(PROTO)
13013/*
13014 * Set v:cmdarg.
13015 * If "eap" != NULL, use "eap" to generate the value and return the old value.
13016 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
13017 * Must always be called in pairs!
13018 */
13019 char_u *
13020set_cmdarg(eap, oldarg)
13021 exarg_T *eap;
13022 char_u *oldarg;
13023{
13024 char_u *oldval;
13025 char_u *newval;
13026 unsigned len;
13027
Bram Moolenaare9a41262005-01-15 22:18:47 +000013028 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013029 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013031 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000013032 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013033 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 }
13035
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013036 if (eap->force_bin == FORCE_BIN)
13037 len = 6;
13038 else if (eap->force_bin == FORCE_NOBIN)
13039 len = 8;
13040 else
13041 len = 0;
13042 if (eap->force_ff != 0)
13043 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
13044# ifdef FEAT_MBYTE
13045 if (eap->force_enc != 0)
13046 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
13047# endif
13048
13049 newval = alloc(len + 1);
13050 if (newval == NULL)
13051 return NULL;
13052
13053 if (eap->force_bin == FORCE_BIN)
13054 sprintf((char *)newval, " ++bin");
13055 else if (eap->force_bin == FORCE_NOBIN)
13056 sprintf((char *)newval, " ++nobin");
13057 else
13058 *newval = NUL;
13059 if (eap->force_ff != 0)
13060 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
13061 eap->cmd + eap->force_ff);
13062# ifdef FEAT_MBYTE
13063 if (eap->force_enc != 0)
13064 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
13065 eap->cmd + eap->force_enc);
13066# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000013067 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013068 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069}
13070#endif
13071
13072/*
13073 * Get the value of internal variable "name".
13074 * Return OK or FAIL.
13075 */
13076 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 char_u *name;
13079 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000013080 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081{
13082 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 typval_T *tv = NULL;
13084 typval_T atv;
13085 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087
13088 /* truncate the name, so that we can use strcmp() */
13089 cc = name[len];
13090 name[len] = NUL;
13091
13092 /*
13093 * Check for "b:changedtick".
13094 */
13095 if (STRCMP(name, "b:changedtick") == 0)
13096 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013097 atv.v_type = VAR_NUMBER;
13098 atv.vval.v_number = curbuf->b_changedtick;
13099 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100 }
13101
13102 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 * Check for user-defined variables.
13104 */
13105 else
13106 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013107 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013109 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 }
13111
Bram Moolenaare9a41262005-01-15 22:18:47 +000013112 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114 if (rettv != NULL)
13115 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 ret = FAIL;
13117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013119 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120
13121 name[len] = cc;
13122
13123 return ret;
13124}
13125
13126/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013127 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13128 * value).
13129 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013132{
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013134}
13135
13136/*
13137 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 * The string "s" must have been allocated, it is consumed.
13139 * Return NULL for out of memory, the variable otherwise.
13140 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013141 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 char_u *s;
13144{
Bram Moolenaar33570922005-01-25 22:26:29 +000013145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv = alloc_tv();
13148 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013150 rettv->v_type = VAR_STRING;
13151 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 }
13153 else
13154 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156}
13157
13158/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013159 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 */
13161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164{
13165 if (varp != NULL)
13166 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 switch (varp->v_type)
13168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013170 func_unref(varp->vval.v_string);
13171 /*FALLTHROUGH*/
13172 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013173 vim_free(varp->vval.v_string);
13174 break;
13175 case VAR_LIST:
13176 list_unref(varp->vval.v_list);
13177 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013178 case VAR_DICT:
13179 dict_unref(varp->vval.v_dict);
13180 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013181 default:
13182 break;
13183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 vim_free(varp);
13185 }
13186}
13187
13188/*
13189 * Free the memory for a variable value and set the value to NULL or 0.
13190 */
13191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013193 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194{
13195 if (varp != NULL)
13196 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013199 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013200 func_unref(varp->vval.v_string);
13201 /*FALLTHROUGH*/
13202 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 vim_free(varp->vval.v_string);
13204 varp->vval.v_string = NULL;
13205 break;
13206 case VAR_LIST:
13207 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013208 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013210 case VAR_DICT:
13211 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013212 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013213 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013215 varp->vval.v_number = 0;
13216 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217 case VAR_UNKNOWN:
13218 break;
13219 default:
13220 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 }
13223}
13224
13225/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226 * Set the value of a variable to NULL without freeing items.
13227 */
13228 static void
13229init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013230 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231{
13232 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234}
13235
13236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 * Get the number value of a variable.
13238 * If it is a String variable, uses vim_str2nr().
13239 */
13240 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013246 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013248 case VAR_NUMBER:
13249 n = (long)(varp->vval.v_number);
13250 break;
13251 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013252 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013253 break;
13254 case VAR_STRING:
13255 if (varp->vval.v_string != NULL)
13256 vim_str2nr(varp->vval.v_string, NULL, NULL,
13257 TRUE, TRUE, &n, NULL);
13258 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013259 case VAR_LIST:
13260 EMSG(_("E703: Using a List as a number"));
13261 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013262 case VAR_DICT:
13263 EMSG(_("E728: Using a Dictionary as a number"));
13264 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013265 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013266 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013269 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270}
13271
13272/*
13273 * Get the lnum from the first argument. Also accepts ".", "$", etc.
13274 */
13275 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278{
Bram Moolenaar33570922005-01-25 22:26:29 +000013279 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 linenr_T lnum;
13281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 if (lnum == 0) /* no valid number, try using line() */
13284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 rettv.v_type = VAR_NUMBER;
13286 f_line(argvars, &rettv);
13287 lnum = rettv.vval.v_number;
13288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 }
13290 return lnum;
13291}
13292
13293/*
13294 * Get the string value of a variable.
13295 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000013296 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
13297 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 * If the String variable has never been set, return an empty string.
13299 * Never returns NULL;
13300 */
13301 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013303 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304{
13305 static char_u mybuf[NUMBUFLEN];
13306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308}
13309
13310 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013313 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013315 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013317 case VAR_NUMBER:
13318 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
13319 return buf;
13320 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013321 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013322 break;
13323 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013324 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013325 break;
13326 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013327 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013328 break;
13329 case VAR_STRING:
13330 if (varp->vval.v_string != NULL)
13331 return varp->vval.v_string;
13332 break;
13333 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013335 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013337 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338}
13339
13340/*
13341 * Find variable "name" in the list of variables.
13342 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013343 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000013344 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000013348find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000013350 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351{
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354
Bram Moolenaara7043832005-01-21 11:56:39 +000013355 ht = find_var_ht(name, &varname);
13356 if (htp != NULL)
13357 *htp = ht;
13358 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 return NULL;
Bram Moolenaara7043832005-01-21 11:56:39 +000013360 return find_var_in_ht(ht, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361}
13362
13363/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000013365 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000013368find_var_in_ht(ht, varname)
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000013370 char_u *varname;
13371{
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 hashitem_T *hi;
13373
13374 if (*varname == NUL)
13375 {
13376 /* Must be something like "s:", otherwise "ht" would be NULL. */
13377 switch (varname[-2])
13378 {
13379 case 's': return &SCRIPT_SV(current_SID).sv_var;
13380 case 'g': return &globvars_var;
13381 case 'v': return &vimvars_var;
13382 case 'b': return &curbuf->b_bufvar;
13383 case 'w': return &curwin->w_winvar;
13384 case 'l': return &current_funccal->l_vars_var;
13385 case 'a': return &current_funccal->l_avars_var;
13386 }
13387 return NULL;
13388 }
Bram Moolenaara7043832005-01-21 11:56:39 +000013389
13390 hi = hash_find(ht, varname);
13391 if (HASHITEM_EMPTY(hi))
13392 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000013394}
13395
13396/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000013398 * Set "varname" to the start of name without ':'.
13399 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000013401find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 char_u *name;
13403 char_u **varname;
13404{
13405 if (name[1] != ':')
13406 {
13407 /* If not "x:name" there must not be any ":" in the name. */
13408 if (vim_strchr(name, ':') != NULL)
13409 return NULL;
13410 *varname = name;
13411 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013412 return &globvarht; /* global variable */
13413 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 }
13415 *varname = name + 2;
13416 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 if (*name == 'g') /* global variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 return &globvarht;
13422 if (*name == 'v') /* v: variable */
13423 return &vimvarht;
13424 if (*name == 'a' && current_funccal != NULL) /* function argument */
13425 return &current_funccal->l_avars.dv_hashtab;
13426 if (*name == 'l' && current_funccal != NULL) /* local function variable */
13427 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 if (*name == 's' /* script variable */
13429 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
13430 return &SCRIPT_VARS(current_SID);
13431 return NULL;
13432}
13433
13434/*
13435 * Get the string value of a (global/local) variable.
13436 * Returns NULL when it doesn't exist.
13437 */
13438 char_u *
13439get_var_value(name)
13440 char_u *name;
13441{
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443
Bram Moolenaara7043832005-01-21 11:56:39 +000013444 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 if (v == NULL)
13446 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448}
13449
13450/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 * sourcing this script and when executing functions defined in the script.
13453 */
13454 void
13455new_script_vars(id)
13456 scid_T id;
13457{
Bram Moolenaara7043832005-01-21 11:56:39 +000013458 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000013459 hashtab_T *ht;
13460 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000013461
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
13463 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013464 /* Re-allocating ga_data means that an ht_array pointing to
13465 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000013466 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000013467 for (i = 1; i <= ga_scripts.ga_len; ++i)
13468 {
13469 ht = &SCRIPT_VARS(i);
13470 if (ht->ht_mask == HT_INIT_SIZE - 1)
13471 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000013472 sv = &SCRIPT_SV(i);
13473 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000013474 }
13475
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 while (ga_scripts.ga_len < id)
13477 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
13479 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 }
13482 }
13483}
13484
13485/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
13487 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 */
13489 void
Bram Moolenaar33570922005-01-25 22:26:29 +000013490init_var_dict(dict, dict_var)
13491 dict_T *dict;
13492 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493{
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 hash_init(&dict->dv_hashtab);
13495 dict->dv_refcount = 99999;
13496 dict_var->di_tv.vval.v_dict = dict;
13497 dict_var->di_tv.v_type = VAR_DICT;
13498 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
13499 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500}
13501
13502/*
13503 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 * Frees all allocated variables and the value they contain.
13505 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 */
13507 void
Bram Moolenaara7043832005-01-21 11:56:39 +000013508vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 hashtab_T *ht;
13510{
13511 vars_clear_ext(ht, TRUE);
13512}
13513
13514/*
13515 * Like vars_clear(), but only free the value if "free_val" is TRUE.
13516 */
13517 static void
13518vars_clear_ext(ht, free_val)
13519 hashtab_T *ht;
13520 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521{
Bram Moolenaara7043832005-01-21 11:56:39 +000013522 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 hashitem_T *hi;
13524 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000013527 todo = ht->ht_used;
13528 for (hi = ht->ht_array; todo > 0; ++hi)
13529 {
13530 if (!HASHITEM_EMPTY(hi))
13531 {
13532 --todo;
13533
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000013535 * ht_array might change then. hash_clear() takes care of it
13536 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 v = HI2DI(hi);
13538 if (free_val)
13539 clear_tv(&v->di_tv);
13540 if ((v->di_flags & DI_FLAGS_FIX) == 0)
13541 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000013542 }
13543 }
13544 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545}
13546
Bram Moolenaara7043832005-01-21 11:56:39 +000013547/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 * Delete a variable from hashtab "ht" at item "hi".
13549 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000013550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000013552delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 hashtab_T *ht;
13554 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000013557
13558 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 clear_tv(&di->di_tv);
13560 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561}
13562
13563/*
13564 * List the value of one internal variable.
13565 */
13566 static void
13567list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000013568 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 char_u *prefix;
13570{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013571 char_u *tofree;
13572 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013573 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013574
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 s = echo_string(&v->di_tv, &tofree, numbuf);
13576 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013577 s == NULL ? (char_u *)"" : s);
13578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579}
13580
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 static void
13582list_one_var_a(prefix, name, type, string)
13583 char_u *prefix;
13584 char_u *name;
13585 int type;
13586 char_u *string;
13587{
13588 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
13589 if (name != NULL) /* "a:" vars don't have a name stored */
13590 msg_puts(name);
13591 msg_putchar(' ');
13592 msg_advance(22);
13593 if (type == VAR_NUMBER)
13594 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013595 else if (type == VAR_FUNC)
13596 msg_putchar('*');
13597 else if (type == VAR_LIST)
13598 {
13599 msg_putchar('[');
13600 if (*string == '[')
13601 ++string;
13602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013603 else if (type == VAR_DICT)
13604 {
13605 msg_putchar('{');
13606 if (*string == '{')
13607 ++string;
13608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 else
13610 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013611
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013613
13614 if (type == VAR_FUNC)
13615 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616}
13617
13618/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 * If the variable already exists, the value is updated.
13621 * Otherwise the variable is created.
13622 */
13623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013631 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634 {
13635 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
13636 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
13637 ? name[2] : name[0]))
13638 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013639 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013640 return;
13641 }
13642 if (function_exists(name))
13643 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013644 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013645 return;
13646 }
13647 }
13648
Bram Moolenaara7043832005-01-21 11:56:39 +000013649 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000013650 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000013651 {
13652 EMSG2(_("E461: Illegal variable name: %s"), name);
13653 return;
13654 }
13655
13656 v = find_var_in_ht(ht, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000013657 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 /* existing variable, need to clear the value */
13660 if (var_check_ro(v->di_flags, name))
13661 return;
13662 if (v->di_tv.v_type != tv->v_type
13663 && !((v->di_tv.v_type == VAR_STRING
13664 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 && (tv->v_type == VAR_STRING
13666 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013667 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013668 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013669 return;
13670 }
Bram Moolenaar33570922005-01-25 22:26:29 +000013671
13672 /*
13673 * Handle setting internal v: variables separately: we keep the type.
13674 */
13675 if (ht == &vimvarht)
13676 {
13677 if (v->di_tv.v_type == VAR_STRING)
13678 {
13679 vim_free(v->di_tv.vval.v_string);
13680 if (copy || tv->v_type != VAR_STRING)
13681 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
13682 else
13683 {
13684 /* Take over the string to avoid an extra alloc/free. */
13685 v->di_tv.vval.v_string = tv->vval.v_string;
13686 tv->vval.v_string = NULL;
13687 }
13688 }
13689 else if (v->di_tv.v_type != VAR_NUMBER)
13690 EMSG2(_(e_intern2), "set_var()");
13691 else
13692 v->di_tv.vval.v_number = get_tv_number(tv);
13693 return;
13694 }
13695
13696 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 }
13698 else /* add a new variable */
13699 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000013701 if (v == NULL)
13702 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 STRCPY(v->di_key, varname);
13704 v->di_flags = 0;
13705 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013707 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 return;
13709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 }
Bram Moolenaara7043832005-01-21 11:56:39 +000013711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000013713 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013714 else
13715 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013716 v->di_tv = *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719}
13720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013721/*
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 * Return TRUE if di_flags "flags" indicate read-only variable "name".
13723 * Also give an error message.
13724 */
13725 static int
13726var_check_ro(flags, name)
13727 int flags;
13728 char_u *name;
13729{
13730 if (flags & DI_FLAGS_RO)
13731 {
13732 EMSG2(_(e_readonlyvar), name);
13733 return TRUE;
13734 }
13735 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
13736 {
13737 EMSG2(_(e_readonlysbx), name);
13738 return TRUE;
13739 }
13740 return FALSE;
13741}
13742
13743/*
13744 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013745 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013746 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 typval_T *from;
13751 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013753 to->v_type = from->v_type;
13754 switch (from->v_type)
13755 {
13756 case VAR_NUMBER:
13757 to->vval.v_number = from->vval.v_number;
13758 break;
13759 case VAR_STRING:
13760 case VAR_FUNC:
13761 if (from->vval.v_string == NULL)
13762 to->vval.v_string = NULL;
13763 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013764 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013765 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013766 if (from->v_type == VAR_FUNC)
13767 func_ref(to->vval.v_string);
13768 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013769 break;
13770 case VAR_LIST:
13771 if (from->vval.v_list == NULL)
13772 to->vval.v_list = NULL;
13773 else
13774 {
13775 to->vval.v_list = from->vval.v_list;
13776 ++to->vval.v_list->lv_refcount;
13777 }
13778 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013779 case VAR_DICT:
13780 if (from->vval.v_dict == NULL)
13781 to->vval.v_dict = NULL;
13782 else
13783 {
13784 to->vval.v_dict = from->vval.v_dict;
13785 ++to->vval.v_dict->dv_refcount;
13786 }
13787 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013788 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013789 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013790 break;
13791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792}
13793
13794/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013795 * Make a copy of an item.
13796 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
13797 */
13798 static void
13799item_copy(from, to, deep)
Bram Moolenaar33570922005-01-25 22:26:29 +000013800 typval_T *from;
13801 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013802 int deep;
13803{
13804 static int recurse = 0;
13805
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013807 {
13808 EMSG(_("E698: variable nested too deep for making a copy"));
13809 return;
13810 }
13811 ++recurse;
13812
13813 switch (from->v_type)
13814 {
13815 case VAR_NUMBER:
13816 case VAR_STRING:
13817 case VAR_FUNC:
13818 copy_tv(from, to);
13819 break;
13820 case VAR_LIST:
13821 to->v_type = VAR_LIST;
13822 to->vval.v_list = list_copy(from->vval.v_list, deep);
13823 break;
13824 case VAR_DICT:
13825 to->v_type = VAR_DICT;
13826 to->vval.v_dict = dict_copy(from->vval.v_dict, deep);
13827 break;
13828 default:
13829 EMSG2(_(e_intern2), "item_copy()");
13830 }
13831 --recurse;
13832}
13833
13834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 * ":echo expr1 ..." print each argument separated with a space, add a
13836 * newline at the end.
13837 * ":echon expr1 ..." print each argument plain.
13838 */
13839 void
13840ex_echo(eap)
13841 exarg_T *eap;
13842{
13843 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013845 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 char_u *p;
13847 int needclr = TRUE;
13848 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013849 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
13851 if (eap->skip)
13852 ++emsg_skip;
13853 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
13854 {
13855 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013856 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 {
13858 /*
13859 * Report the invalid expression unless the expression evaluation
13860 * has been cancelled due to an aborting error, an interrupt, or an
13861 * exception.
13862 */
13863 if (!aborting())
13864 EMSG2(_(e_invexpr2), p);
13865 break;
13866 }
13867 if (!eap->skip)
13868 {
13869 if (atstart)
13870 {
13871 atstart = FALSE;
13872 /* Call msg_start() after eval1(), evaluating the expression
13873 * may cause a message to appear. */
13874 if (eap->cmdidx == CMD_echo)
13875 msg_start();
13876 }
13877 else if (eap->cmdidx == CMD_echo)
13878 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013879 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013880 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 if (*p == '\n' || *p == '\r' || *p == TAB)
13882 {
13883 if (*p != TAB && needclr)
13884 {
13885 /* remove any text still there from the command */
13886 msg_clr_eos();
13887 needclr = FALSE;
13888 }
13889 msg_putchar_attr(*p, echo_attr);
13890 }
13891 else
13892 {
13893#ifdef FEAT_MBYTE
13894 if (has_mbyte)
13895 {
13896 int i = (*mb_ptr2len_check)(p);
13897
13898 (void)msg_outtrans_len_attr(p, i, echo_attr);
13899 p += i - 1;
13900 }
13901 else
13902#endif
13903 (void)msg_outtrans_len_attr(p, 1, echo_attr);
13904 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013905 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 arg = skipwhite(arg);
13909 }
13910 eap->nextcmd = check_nextcmd(arg);
13911
13912 if (eap->skip)
13913 --emsg_skip;
13914 else
13915 {
13916 /* remove text that may still be there from the command */
13917 if (needclr)
13918 msg_clr_eos();
13919 if (eap->cmdidx == CMD_echo)
13920 msg_end();
13921 }
13922}
13923
13924/*
13925 * ":echohl {name}".
13926 */
13927 void
13928ex_echohl(eap)
13929 exarg_T *eap;
13930{
13931 int id;
13932
13933 id = syn_name2id(eap->arg);
13934 if (id == 0)
13935 echo_attr = 0;
13936 else
13937 echo_attr = syn_id2attr(id);
13938}
13939
13940/*
13941 * ":execute expr1 ..." execute the result of an expression.
13942 * ":echomsg expr1 ..." Print a message
13943 * ":echoerr expr1 ..." Print an error
13944 * Each gets spaces around each argument and a newline at the end for
13945 * echo commands
13946 */
13947 void
13948ex_execute(eap)
13949 exarg_T *eap;
13950{
13951 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000013952 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 int ret = OK;
13954 char_u *p;
13955 garray_T ga;
13956 int len;
13957 int save_did_emsg;
13958
13959 ga_init2(&ga, 1, 80);
13960
13961 if (eap->skip)
13962 ++emsg_skip;
13963 while (*arg != NUL && *arg != '|' && *arg != '\n')
13964 {
13965 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013966 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 {
13968 /*
13969 * Report the invalid expression unless the expression evaluation
13970 * has been cancelled due to an aborting error, an interrupt, or an
13971 * exception.
13972 */
13973 if (!aborting())
13974 EMSG2(_(e_invexpr2), p);
13975 ret = FAIL;
13976 break;
13977 }
13978
13979 if (!eap->skip)
13980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 len = (int)STRLEN(p);
13983 if (ga_grow(&ga, len + 2) == FAIL)
13984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 ret = FAIL;
13987 break;
13988 }
13989 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 ga.ga_len += len;
13993 }
13994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 arg = skipwhite(arg);
13997 }
13998
13999 if (ret != FAIL && ga.ga_data != NULL)
14000 {
14001 if (eap->cmdidx == CMD_echomsg)
14002 MSG_ATTR(ga.ga_data, echo_attr);
14003 else if (eap->cmdidx == CMD_echoerr)
14004 {
14005 /* We don't want to abort following commands, restore did_emsg. */
14006 save_did_emsg = did_emsg;
14007 EMSG((char_u *)ga.ga_data);
14008 if (!force_abort)
14009 did_emsg = save_did_emsg;
14010 }
14011 else if (eap->cmdidx == CMD_execute)
14012 do_cmdline((char_u *)ga.ga_data,
14013 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
14014 }
14015
14016 ga_clear(&ga);
14017
14018 if (eap->skip)
14019 --emsg_skip;
14020
14021 eap->nextcmd = check_nextcmd(arg);
14022}
14023
14024/*
14025 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
14026 * "arg" points to the "&" or '+' when called, to "option" when returning.
14027 * Returns NULL when no option name found. Otherwise pointer to the char
14028 * after the option name.
14029 */
14030 static char_u *
14031find_option_end(arg, opt_flags)
14032 char_u **arg;
14033 int *opt_flags;
14034{
14035 char_u *p = *arg;
14036
14037 ++p;
14038 if (*p == 'g' && p[1] == ':')
14039 {
14040 *opt_flags = OPT_GLOBAL;
14041 p += 2;
14042 }
14043 else if (*p == 'l' && p[1] == ':')
14044 {
14045 *opt_flags = OPT_LOCAL;
14046 p += 2;
14047 }
14048 else
14049 *opt_flags = 0;
14050
14051 if (!ASCII_ISALPHA(*p))
14052 return NULL;
14053 *arg = p;
14054
14055 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
14056 p += 4; /* termcap option */
14057 else
14058 while (ASCII_ISALPHA(*p))
14059 ++p;
14060 return p;
14061}
14062
14063/*
14064 * ":function"
14065 */
14066 void
14067ex_function(eap)
14068 exarg_T *eap;
14069{
14070 char_u *theline;
14071 int j;
14072 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 char_u *name = NULL;
14075 char_u *p;
14076 char_u *arg;
14077 garray_T newargs;
14078 garray_T newlines;
14079 int varargs = FALSE;
14080 int mustend = FALSE;
14081 int flags = 0;
14082 ufunc_T *fp;
14083 int indent;
14084 int nesting;
14085 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 dictitem_T *v;
14087 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014088 static int func_nr = 0; /* number for nameless function */
14089 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090
14091 /*
14092 * ":function" without argument: list functions.
14093 */
14094 if (ends_excmd(*eap->arg))
14095 {
14096 if (!eap->skip)
14097 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014098 if (!isdigit(*fp->name))
14099 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 eap->nextcmd = check_nextcmd(eap->arg);
14101 return;
14102 }
14103
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014104 /*
14105 * Get the function name. There are these situations:
14106 * func normal function name
14107 * "name" == func, "fudi.fd_dict" == NULL
14108 * dict.func new dictionary entry
14109 * "name" == NULL, "fudi.fd_dict" set,
14110 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
14111 * dict.func existing dict entry with a Funcref
14112 * "name" == fname, "fudi.fd_dict" set,
14113 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14114 * dict.func existing dict entry that's not a Funcref
14115 * "name" == NULL, "fudi.fd_dict" set,
14116 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014119 name = trans_function_name(&p, eap->skip, 0, &fudi);
14120 paren = (vim_strchr(p, '(') != NULL);
14121 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 {
14123 /*
14124 * Return on an invalid expression in braces, unless the expression
14125 * evaluation has been cancelled due to an aborting error, an
14126 * interrupt, or an exception.
14127 */
14128 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014129 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014130 if (!eap->skip && fudi.fd_newkey != NULL)
14131 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014132 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 else
14136 eap->skip = TRUE;
14137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 /* An error in a function call during evaluation of an expression in magic
14139 * braces should not cause the function not to be defined. */
14140 saved_did_emsg = did_emsg;
14141 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142
14143 /*
14144 * ":function func" with only function name: list function.
14145 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014146 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 {
14148 if (!ends_excmd(*skipwhite(p)))
14149 {
14150 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014151 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 }
14153 eap->nextcmd = check_nextcmd(p);
14154 if (eap->nextcmd != NULL)
14155 *p = NUL;
14156 if (!eap->skip && !got_int)
14157 {
14158 fp = find_func(name);
14159 if (fp != NULL)
14160 {
14161 list_func_head(fp, TRUE);
14162 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
14163 {
14164 msg_putchar('\n');
14165 msg_outnum((long)(j + 1));
14166 if (j < 9)
14167 msg_putchar(' ');
14168 if (j < 99)
14169 msg_putchar(' ');
14170 msg_prt_line(FUNCLINE(fp, j));
14171 out_flush(); /* show a line at a time */
14172 ui_breakcheck();
14173 }
14174 if (!got_int)
14175 {
14176 msg_putchar('\n');
14177 msg_puts((char_u *)" endfunction");
14178 }
14179 }
14180 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014181 EMSG2(_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014183 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 }
14185
14186 /*
14187 * ":function name(arg1, arg2)" Define function.
14188 */
14189 p = skipwhite(p);
14190 if (*p != '(')
14191 {
14192 if (!eap->skip)
14193 {
14194 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014195 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 }
14197 /* attempt to continue by skipping some text */
14198 if (vim_strchr(p, '(') != NULL)
14199 p = vim_strchr(p, '(');
14200 }
14201 p = skipwhite(p + 1);
14202
14203 ga_init2(&newargs, (int)sizeof(char_u *), 3);
14204 ga_init2(&newlines, (int)sizeof(char_u *), 3);
14205
14206 /*
14207 * Isolate the arguments: "arg1, arg2, ...)"
14208 */
14209 while (*p != ')')
14210 {
14211 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
14212 {
14213 varargs = TRUE;
14214 p += 3;
14215 mustend = TRUE;
14216 }
14217 else
14218 {
14219 arg = p;
14220 while (ASCII_ISALNUM(*p) || *p == '_')
14221 ++p;
14222 if (arg == p || isdigit(*arg)
14223 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
14224 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
14225 {
14226 if (!eap->skip)
14227 EMSG2(_("E125: Illegal argument: %s"), arg);
14228 break;
14229 }
14230 if (ga_grow(&newargs, 1) == FAIL)
14231 goto erret;
14232 c = *p;
14233 *p = NUL;
14234 arg = vim_strsave(arg);
14235 if (arg == NULL)
14236 goto erret;
14237 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
14238 *p = c;
14239 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 if (*p == ',')
14241 ++p;
14242 else
14243 mustend = TRUE;
14244 }
14245 p = skipwhite(p);
14246 if (mustend && *p != ')')
14247 {
14248 if (!eap->skip)
14249 EMSG2(_(e_invarg2), eap->arg);
14250 break;
14251 }
14252 }
14253 ++p; /* skip the ')' */
14254
Bram Moolenaare9a41262005-01-15 22:18:47 +000014255 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 for (;;)
14257 {
14258 p = skipwhite(p);
14259 if (STRNCMP(p, "range", 5) == 0)
14260 {
14261 flags |= FC_RANGE;
14262 p += 5;
14263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014264 else if (STRNCMP(p, "dict", 4) == 0)
14265 {
14266 flags |= FC_DICT;
14267 p += 4;
14268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 else if (STRNCMP(p, "abort", 5) == 0)
14270 {
14271 flags |= FC_ABORT;
14272 p += 5;
14273 }
14274 else
14275 break;
14276 }
14277
14278 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
14279 EMSG(_(e_trailing));
14280
14281 /*
14282 * Read the body of the function, until ":endfunction" is found.
14283 */
14284 if (KeyTyped)
14285 {
14286 /* Check if the function already exists, don't let the user type the
14287 * whole function before telling him it doesn't work! For a script we
14288 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014289 if (!eap->skip && !eap->forceit)
14290 {
14291 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
14292 EMSG(_(e_funcdict));
14293 else if (name != NULL && find_func(name) != NULL)
14294 EMSG2(_(e_funcexts), name);
14295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296
14297 msg_putchar('\n'); /* don't overwrite the function name */
14298 cmdline_row = msg_row;
14299 }
14300
14301 indent = 2;
14302 nesting = 0;
14303 for (;;)
14304 {
14305 msg_scroll = TRUE;
14306 need_wait_return = FALSE;
14307 if (eap->getline == NULL)
14308 theline = getcmdline(':', 0L, indent);
14309 else
14310 theline = eap->getline(':', eap->cookie, indent);
14311 if (KeyTyped)
14312 lines_left = Rows - 1;
14313 if (theline == NULL)
14314 {
14315 EMSG(_("E126: Missing :endfunction"));
14316 goto erret;
14317 }
14318
14319 if (skip_until != NULL)
14320 {
14321 /* between ":append" and "." and between ":python <<EOF" and "EOF"
14322 * don't check for ":endfunc". */
14323 if (STRCMP(theline, skip_until) == 0)
14324 {
14325 vim_free(skip_until);
14326 skip_until = NULL;
14327 }
14328 }
14329 else
14330 {
14331 /* skip ':' and blanks*/
14332 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
14333 ;
14334
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014335 /* Check for "endfunction". */
14336 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 {
14338 vim_free(theline);
14339 break;
14340 }
14341
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014342 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 * at "end". */
14344 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
14345 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014346 else if (STRNCMP(p, "if", 2) == 0
14347 || STRNCMP(p, "wh", 2) == 0
14348 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 || STRNCMP(p, "try", 3) == 0)
14350 indent += 2;
14351
14352 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014353 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014355 if (*p == '!')
14356 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 p += eval_fname_script(p);
14358 if (ASCII_ISALPHA(*p))
14359 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014360 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 if (*skipwhite(p) == '(')
14362 {
14363 ++nesting;
14364 indent += 2;
14365 }
14366 }
14367 }
14368
14369 /* Check for ":append" or ":insert". */
14370 p = skip_range(p, NULL);
14371 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
14372 || (p[0] == 'i'
14373 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
14374 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
14375 skip_until = vim_strsave((char_u *)".");
14376
14377 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
14378 arg = skipwhite(skiptowhite(p));
14379 if (arg[0] == '<' && arg[1] =='<'
14380 && ((p[0] == 'p' && p[1] == 'y'
14381 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
14382 || (p[0] == 'p' && p[1] == 'e'
14383 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
14384 || (p[0] == 't' && p[1] == 'c'
14385 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
14386 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
14387 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014388 || (p[0] == 'm' && p[1] == 'z'
14389 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 ))
14391 {
14392 /* ":python <<" continues until a dot, like ":append" */
14393 p = skipwhite(arg + 2);
14394 if (*p == NUL)
14395 skip_until = vim_strsave((char_u *)".");
14396 else
14397 skip_until = vim_strsave(p);
14398 }
14399 }
14400
14401 /* Add the line to the function. */
14402 if (ga_grow(&newlines, 1) == FAIL)
14403 goto erret;
14404 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
14405 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 }
14407
14408 /* Don't define the function when skipping commands or when an error was
14409 * detected. */
14410 if (eap->skip || did_emsg)
14411 goto erret;
14412
14413 /*
14414 * If there are no errors, add the function
14415 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014416 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014417 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014418 v = find_var(name, NULL);
Bram Moolenaar33570922005-01-25 22:26:29 +000014419 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014420 {
14421 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
14422 goto erret;
14423 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014425 fp = find_func(name);
14426 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014428 if (!eap->forceit)
14429 {
14430 EMSG2(_(e_funcexts), name);
14431 goto erret;
14432 }
14433 if (fp->calls > 0)
14434 {
14435 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
14436 name);
14437 goto erret;
14438 }
14439 /* redefine existing function */
14440 ga_clear_strings(&(fp->args));
14441 ga_clear_strings(&(fp->lines));
14442 vim_free(name);
14443 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 }
14446 else
14447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014448 char numbuf[20];
14449
14450 fp = NULL;
14451 if (fudi.fd_newkey == NULL && !eap->forceit)
14452 {
14453 EMSG(_(e_funcdict));
14454 goto erret;
14455 }
14456
14457 /* Give the function a sequential number. Can only be used with a
14458 * Funcref! */
14459 vim_free(name);
14460 sprintf(numbuf, "%d", ++func_nr);
14461 name = vim_strsave((char_u *)numbuf);
14462 if (name == NULL)
14463 goto erret;
14464 }
14465
14466 if (fp == NULL)
14467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14469 if (fp == NULL)
14470 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014471
14472 if (fudi.fd_dict != NULL)
14473 {
14474 if (fudi.fd_di == NULL)
14475 {
14476 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014477 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014478 if (fudi.fd_di == NULL)
14479 {
14480 vim_free(fp);
14481 goto erret;
14482 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014483 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
14484 {
14485 vim_free(fudi.fd_di);
14486 goto erret;
14487 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014488 }
14489 else
14490 /* overwrite existing dict entry */
14491 clear_tv(&fudi.fd_di->di_tv);
14492 fudi.fd_di->di_tv.v_type = VAR_FUNC;
14493 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
14494 fp->refcount = 1;
14495 }
14496
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014498 fp->name = name;
14499 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 fp->next = firstfunc;
14501 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 }
14503 fp->args = newargs;
14504 fp->lines = newlines;
14505 fp->varargs = varargs;
14506 fp->flags = flags;
14507 fp->calls = 0;
14508 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014509 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
14511erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 ga_clear_strings(&newargs);
14513 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014514ret_free:
14515 vim_free(skip_until);
14516 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
14521/*
14522 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000014523 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014525 * flags:
14526 * TFN_INT: internal function name OK
14527 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 * Advances "pp" to just after the function name (if no error).
14529 */
14530 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014531trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 char_u **pp;
14533 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014534 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014537 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 char_u *start;
14539 char_u *end;
14540 int lead;
14541 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014544
14545 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000014548
14549 /* Check for hard coded <SNR>: already translated function ID (from a user
14550 * command). */
14551 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
14552 && (*pp)[2] == (int)KE_SNR)
14553 {
14554 *pp += 3;
14555 len = get_id_len(pp) + 3;
14556 return vim_strnsave(start, len);
14557 }
14558
14559 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
14560 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000014562 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014564
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014565 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014566 if (end == start)
14567 {
14568 if (!skip)
14569 EMSG(_("E129: Function name required"));
14570 goto theend;
14571 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014572 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014573 {
14574 /*
14575 * Report an invalid expression in braces, unless the expression
14576 * evaluation has been cancelled due to an aborting error, an
14577 * interrupt, or an exception.
14578 */
14579 if (!aborting())
14580 {
14581 if (end != NULL)
14582 EMSG2(_(e_invarg2), start);
14583 }
14584 else
14585 *pp = find_name_end(start, NULL, NULL, TRUE);
14586 goto theend;
14587 }
14588
14589 if (lv.ll_tv != NULL)
14590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014591 if (fdp != NULL)
14592 {
14593 fdp->fd_dict = lv.ll_dict;
14594 fdp->fd_newkey = lv.ll_newkey;
14595 lv.ll_newkey = NULL;
14596 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014598 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14599 {
14600 name = vim_strsave(lv.ll_tv->vval.v_string);
14601 *pp = end;
14602 }
14603 else
14604 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014605 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
14606 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014607 EMSG(_(e_funcref));
14608 else
14609 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014610 name = NULL;
14611 }
14612 goto theend;
14613 }
14614
14615 if (lv.ll_name == NULL)
14616 {
14617 /* Error found, but continue after the function name. */
14618 *pp = end;
14619 goto theend;
14620 }
14621
14622 if (lv.ll_exp_name != NULL)
14623 len = STRLEN(lv.ll_exp_name);
14624 else
Bram Moolenaara7043832005-01-21 11:56:39 +000014625 {
14626 if (lead == 2) /* skip over "s:" */
14627 lv.ll_name += 2;
14628 len = (int)(end - lv.ll_name);
14629 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014630
14631 /*
14632 * Copy the function name to allocated memory.
14633 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14634 * Accept <SNR>123_name() outside a script.
14635 */
14636 if (skip)
14637 lead = 0; /* do nothing */
14638 else if (lead > 0)
14639 {
14640 lead = 3;
14641 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14642 {
14643 if (current_SID <= 0)
14644 {
14645 EMSG(_(e_usingsid));
14646 goto theend;
14647 }
14648 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14649 lead += (int)STRLEN(sid_buf);
14650 }
14651 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014652 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014653 {
14654 EMSG2(_("E128: Function name must start with a capital: %s"),
14655 lv.ll_name);
14656 goto theend;
14657 }
14658 name = alloc((unsigned)(len + lead + 1));
14659 if (name != NULL)
14660 {
14661 if (lead > 0)
14662 {
14663 name[0] = K_SPECIAL;
14664 name[1] = KS_EXTRA;
14665 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000014666 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014667 STRCPY(name + 3, sid_buf);
14668 }
14669 mch_memmove(name + lead, lv.ll_name, (size_t)len);
14670 name[len + lead] = NUL;
14671 }
14672 *pp = end;
14673
14674theend:
14675 clear_lval(&lv);
14676 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677}
14678
14679/*
14680 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14681 * Return 2 if "p" starts with "s:".
14682 * Return 0 otherwise.
14683 */
14684 static int
14685eval_fname_script(p)
14686 char_u *p;
14687{
14688 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
14689 || STRNICMP(p + 1, "SNR>", 4) == 0))
14690 return 5;
14691 if (p[0] == 's' && p[1] == ':')
14692 return 2;
14693 return 0;
14694}
14695
14696/*
14697 * Return TRUE if "p" starts with "<SID>" or "s:".
14698 * Only works if eval_fname_script() returned non-zero for "p"!
14699 */
14700 static int
14701eval_fname_sid(p)
14702 char_u *p;
14703{
14704 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
14705}
14706
14707/*
14708 * List the head of the function: "name(arg1, arg2)".
14709 */
14710 static void
14711list_func_head(fp, indent)
14712 ufunc_T *fp;
14713 int indent;
14714{
14715 int j;
14716
14717 msg_start();
14718 if (indent)
14719 MSG_PUTS(" ");
14720 MSG_PUTS("function ");
14721 if (fp->name[0] == K_SPECIAL)
14722 {
14723 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
14724 msg_puts(fp->name + 3);
14725 }
14726 else
14727 msg_puts(fp->name);
14728 msg_putchar('(');
14729 for (j = 0; j < fp->args.ga_len; ++j)
14730 {
14731 if (j)
14732 MSG_PUTS(", ");
14733 msg_puts(FUNCARG(fp, j));
14734 }
14735 if (fp->varargs)
14736 {
14737 if (j)
14738 MSG_PUTS(", ");
14739 MSG_PUTS("...");
14740 }
14741 msg_putchar(')');
14742}
14743
14744/*
14745 * Find a function by name, return pointer to it in ufuncs.
14746 * Return NULL for unknown function.
14747 */
14748 static ufunc_T *
14749find_func(name)
14750 char_u *name;
14751{
14752 ufunc_T *fp;
14753
14754 for (fp = firstfunc; fp != NULL; fp = fp->next)
14755 if (STRCMP(name, fp->name) == 0)
14756 break;
14757 return fp;
14758}
14759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014760/*
14761 * Return TRUE if a function "name" exists.
14762 */
14763 static int
14764function_exists(name)
14765 char_u *name;
14766{
14767 char_u *p = name;
14768 int n = FALSE;
14769
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014770 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014771 if (p != NULL)
14772 {
14773 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14774 n = (find_func(p) != NULL);
14775 else if (ASCII_ISLOWER(*p))
14776 n = (find_internal_func(p) >= 0);
14777 vim_free(p);
14778 }
14779 return n;
14780}
14781
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
14783
14784/*
14785 * Function given to ExpandGeneric() to obtain the list of user defined
14786 * function names.
14787 */
14788 char_u *
14789get_user_func_name(xp, idx)
14790 expand_T *xp;
14791 int idx;
14792{
14793 static ufunc_T *fp = NULL;
14794
14795 if (idx == 0)
14796 fp = firstfunc;
14797 if (fp != NULL)
14798 {
14799 if (STRLEN(fp->name) + 4 >= IOSIZE)
14800 return fp->name; /* prevents overflow */
14801
14802 cat_func_name(IObuff, fp);
14803 if (xp->xp_context != EXPAND_USER_FUNC)
14804 {
14805 STRCAT(IObuff, "(");
14806 if (!fp->varargs && fp->args.ga_len == 0)
14807 STRCAT(IObuff, ")");
14808 }
14809
14810 fp = fp->next;
14811 return IObuff;
14812 }
14813 return NULL;
14814}
14815
14816#endif /* FEAT_CMDL_COMPL */
14817
14818/*
14819 * Copy the function name of "fp" to buffer "buf".
14820 * "buf" must be able to hold the function name plus three bytes.
14821 * Takes care of script-local function names.
14822 */
14823 static void
14824cat_func_name(buf, fp)
14825 char_u *buf;
14826 ufunc_T *fp;
14827{
14828 if (fp->name[0] == K_SPECIAL)
14829 {
14830 STRCPY(buf, "<SNR>");
14831 STRCAT(buf, fp->name + 3);
14832 }
14833 else
14834 STRCPY(buf, fp->name);
14835}
14836
14837/*
14838 * ":delfunction {name}"
14839 */
14840 void
14841ex_delfunction(eap)
14842 exarg_T *eap;
14843{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014844 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 char_u *p;
14846 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848
14849 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014850 name = trans_function_name(&p, eap->skip, 0, &fudi);
14851 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014853 {
14854 if (fudi.fd_dict != NULL && !eap->skip)
14855 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 if (!ends_excmd(*skipwhite(p)))
14859 {
14860 vim_free(name);
14861 EMSG(_(e_trailing));
14862 return;
14863 }
14864 eap->nextcmd = check_nextcmd(p);
14865 if (eap->nextcmd != NULL)
14866 *p = NUL;
14867
14868 if (!eap->skip)
14869 fp = find_func(name);
14870 vim_free(name);
14871
14872 if (!eap->skip)
14873 {
14874 if (fp == NULL)
14875 {
14876 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14877 return;
14878 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014879 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 {
14881 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14882 return;
14883 }
14884
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014885 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014887 /* Delete the dict item that refers to the function, it will
14888 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014889 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014891 else
14892 func_free(fp);
14893 }
14894}
14895
14896/*
14897 * Free a function and remove it from the list of functions.
14898 */
14899 static void
14900func_free(fp)
14901 ufunc_T *fp;
14902{
14903 ufunc_T *pfp;
14904
14905 /* clear this function */
14906 vim_free(fp->name);
14907 ga_clear_strings(&(fp->args));
14908 ga_clear_strings(&(fp->lines));
14909
14910 /* remove the function from the function list */
14911 if (firstfunc == fp)
14912 firstfunc = fp->next;
14913 else
14914 {
14915 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
14916 if (pfp->next == fp)
14917 {
14918 pfp->next = fp->next;
14919 break;
14920 }
14921 }
14922 vim_free(fp);
14923}
14924
14925/*
14926 * Unreference a Function: decrement the reference count and free it when it
14927 * becomes zero. Only for numbered functions.
14928 */
14929 static void
14930func_unref(name)
14931 char_u *name;
14932{
14933 ufunc_T *fp;
14934
14935 if (name != NULL && isdigit(*name))
14936 {
14937 fp = find_func(name);
14938 if (fp == NULL)
14939 EMSG2(_(e_intern2), "func_unref()");
14940 else if (--fp->refcount <= 0)
14941 {
14942 /* Only delete it when it's not being used. Otherwise it's done
14943 * when "calls" becomes zero. */
14944 if (fp->calls == 0)
14945 func_free(fp);
14946 }
14947 }
14948}
14949
14950/*
14951 * Count a reference to a Function.
14952 */
14953 static void
14954func_ref(name)
14955 char_u *name;
14956{
14957 ufunc_T *fp;
14958
14959 if (name != NULL && isdigit(*name))
14960 {
14961 fp = find_func(name);
14962 if (fp == NULL)
14963 EMSG2(_(e_intern2), "func_ref()");
14964 else
14965 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 }
14967}
14968
14969/*
14970 * Call a user function.
14971 */
14972 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000014973call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 ufunc_T *fp; /* pointer to function */
14975 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars; /* arguments */
14977 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 linenr_T firstline; /* first line of range */
14979 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981{
Bram Moolenaar33570922005-01-25 22:26:29 +000014982 char_u *save_sourcing_name;
14983 linenr_T save_sourcing_lnum;
14984 scid_T save_current_SID;
14985 funccall_T fc;
14986 funccall_T *save_fcp = current_funccal;
14987 int save_did_emsg;
14988 static int depth = 0;
14989 dictitem_T *v;
14990 int fixvar_idx = 0; /* index in fixvar[] */
14991 int i;
14992 int ai;
14993 char_u numbuf[NUMBUFLEN];
14994 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
14996 /* If depth of calling is getting too high, don't execute the function */
14997 if (depth >= p_mfd)
14998 {
14999 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 rettv->v_type = VAR_NUMBER;
15001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 return;
15003 }
15004 ++depth;
15005
15006 line_breakcheck(); /* check for CTRL-C hit */
15007
Bram Moolenaar33570922005-01-25 22:26:29 +000015008 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 fc.rettv = rettv;
15011 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 fc.linenr = 0;
15013 fc.returned = FALSE;
15014 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 /* Check if this function has a breakpoint. */
15016 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
15017 fc.dbg_tick = debug_tick;
15018
Bram Moolenaar33570922005-01-25 22:26:29 +000015019 /*
15020 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
15021 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
15022 * each argument variable and saves a lot of time.
15023 */
15024 /*
15025 * Init l: variables.
15026 */
15027 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000015028 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015029 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 /* Set l:self to "selfdict". */
15031 v = &fc.fixvar[fixvar_idx++].var;
15032 STRCPY(v->di_key, "self");
15033 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
15034 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
15035 v->di_tv.v_type = VAR_DICT;
15036 v->di_tv.vval.v_dict = selfdict;
15037 ++selfdict->dv_refcount;
15038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015039
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 /*
15041 * Init a: variables.
15042 * Set a:0 to "argcount".
15043 * Set a:000 to a list with room for the "..." arguments.
15044 */
15045 init_var_dict(&fc.l_avars, &fc.l_avars_var);
15046 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
15047 (varnumber_T)(argcount - fp->args.ga_len));
15048 v = &fc.fixvar[fixvar_idx++].var;
15049 STRCPY(v->di_key, "000");
15050 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15051 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15052 v->di_tv.v_type = VAR_LIST;
15053 v->di_tv.vval.v_list = &fc.l_varlist;
15054 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
15055 fc.l_varlist.lv_refcount = 99999;
15056
15057 /*
15058 * Set a:firstline to "firstline" and a:lastline to "lastline".
15059 * Set a:name to named arguments.
15060 * Set a:N to the "..." arguments.
15061 */
15062 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
15063 (varnumber_T)firstline);
15064 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
15065 (varnumber_T)lastline);
15066 for (i = 0; i < argcount; ++i)
15067 {
15068 ai = i - fp->args.ga_len;
15069 if (ai < 0)
15070 /* named argument a:name */
15071 name = FUNCARG(fp, i);
15072 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015073 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 /* "..." argument a:1, a:2, etc. */
15075 sprintf((char *)numbuf, "%d", ai + 1);
15076 name = numbuf;
15077 }
15078 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
15079 {
15080 v = &fc.fixvar[fixvar_idx++].var;
15081 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15082 }
15083 else
15084 {
15085 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(name)));
15086 if (v == NULL)
15087 break;
15088 v->di_flags = DI_FLAGS_RO;
15089 }
15090 STRCPY(v->di_key, name);
15091 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15092
15093 /* Note: the values are copied directly to avoid alloc/free. */
15094 v->di_tv = argvars[i];
15095
15096 if (ai >= 0 && ai < MAX_FUNC_ARGS)
15097 {
15098 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
15099 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaare9a41262005-01-15 22:18:47 +000015100 }
15101 }
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 /* Don't redraw while executing the function. */
15104 ++RedrawingDisabled;
15105 save_sourcing_name = sourcing_name;
15106 save_sourcing_lnum = sourcing_lnum;
15107 sourcing_lnum = 1;
15108 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
15109 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
15110 if (sourcing_name != NULL)
15111 {
15112 if (save_sourcing_name != NULL
15113 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
15114 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
15115 else
15116 STRCPY(sourcing_name, "function ");
15117 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
15118
15119 if (p_verbose >= 12)
15120 {
15121 ++no_wait_return;
15122 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15123 msg_str((char_u *)_("calling %s"), sourcing_name);
15124 if (p_verbose >= 14)
15125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 char_u buf[MSG_BUF_LEN];
15127
15128 msg_puts((char_u *)"(");
15129 for (i = 0; i < argcount; ++i)
15130 {
15131 if (i > 0)
15132 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015133 if (argvars[i].v_type == VAR_NUMBER)
15134 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 else
15136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 buf, MSG_BUF_LEN);
15139 msg_puts((char_u *)"\"");
15140 msg_puts(buf);
15141 msg_puts((char_u *)"\"");
15142 }
15143 }
15144 msg_puts((char_u *)")");
15145 }
15146 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15147 cmdline_row = msg_row;
15148 --no_wait_return;
15149 }
15150 }
15151 save_current_SID = current_SID;
15152 current_SID = fp->script_ID;
15153 save_did_emsg = did_emsg;
15154 did_emsg = FALSE;
15155
15156 /* call do_cmdline() to execute the lines */
15157 do_cmdline(NULL, get_func_line, (void *)&fc,
15158 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
15159
15160 --RedrawingDisabled;
15161
15162 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 clear_tv(rettv);
15166 rettv->v_type = VAR_NUMBER;
15167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 }
15169
15170 /* when being verbose, mention the return value */
15171 if (p_verbose >= 12)
15172 {
15173 char_u *sn, *val;
15174
15175 ++no_wait_return;
15176 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15177
15178 /* Make sure the output fits in IObuff. */
15179 sn = sourcing_name;
15180 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
15181 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
15182
15183 if (aborting())
15184 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015185 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187 (long)fc.rettv->vval.v_number);
15188 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015190 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 if (STRLEN(val) > IOSIZE / 2 - 50)
15192 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
15193 smsg((char_u *)_("%s returning \"%s\""), sn, val);
15194 }
15195 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15196 cmdline_row = msg_row;
15197 --no_wait_return;
15198 }
15199
15200 vim_free(sourcing_name);
15201 sourcing_name = save_sourcing_name;
15202 sourcing_lnum = save_sourcing_lnum;
15203 current_SID = save_current_SID;
15204
15205 if (p_verbose >= 12 && sourcing_name != NULL)
15206 {
15207 ++no_wait_return;
15208 msg_scroll = TRUE; /* always scroll up, don't overwrite */
15209 msg_str((char_u *)_("continuing in %s"), sourcing_name);
15210 msg_puts((char_u *)"\n"); /* don't overwrite this either */
15211 cmdline_row = msg_row;
15212 --no_wait_return;
15213 }
15214
15215 did_emsg |= save_did_emsg;
15216 current_funccal = save_fcp;
15217
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 /* The a: variables typevals were not alloced, only free the allocated
15219 * variables. */
15220 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
15221
15222 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 --depth;
15224}
15225
15226/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015227 * Add a number variable "name" to dict "dp" with value "nr".
15228 */
15229 static void
15230add_nr_var(dp, v, name, nr)
15231 dict_T *dp;
15232 dictitem_T *v;
15233 char *name;
15234 varnumber_T nr;
15235{
15236 STRCPY(v->di_key, name);
15237 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15238 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
15239 v->di_tv.v_type = VAR_NUMBER;
15240 v->di_tv.vval.v_number = nr;
15241}
15242
15243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 * ":return [expr]"
15245 */
15246 void
15247ex_return(eap)
15248 exarg_T *eap;
15249{
15250 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 int returning = FALSE;
15253
15254 if (current_funccal == NULL)
15255 {
15256 EMSG(_("E133: :return not inside a function"));
15257 return;
15258 }
15259
15260 if (eap->skip)
15261 ++emsg_skip;
15262
15263 eap->nextcmd = NULL;
15264 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 {
15267 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015270 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271 }
15272 /* It's safer to return also on error. */
15273 else if (!eap->skip)
15274 {
15275 /*
15276 * Return unless the expression evaluation has been cancelled due to an
15277 * aborting error, an interrupt, or an exception.
15278 */
15279 if (!aborting())
15280 returning = do_return(eap, FALSE, TRUE, NULL);
15281 }
15282
15283 /* When skipping or the return gets pending, advance to the next command
15284 * in this line (!returning). Otherwise, ignore the rest of the line.
15285 * Following lines will be ignored by get_func_line(). */
15286 if (returning)
15287 eap->nextcmd = NULL;
15288 else if (eap->nextcmd == NULL) /* no argument */
15289 eap->nextcmd = check_nextcmd(arg);
15290
15291 if (eap->skip)
15292 --emsg_skip;
15293}
15294
15295/*
15296 * Return from a function. Possibly makes the return pending. Also called
15297 * for a pending return at the ":endtry" or after returning from an extra
15298 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000015299 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 * FALSE when the return gets pending.
15302 */
15303 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 exarg_T *eap;
15306 int reanimate;
15307 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309{
15310 int idx;
15311 struct condstack *cstack = eap->cstack;
15312
15313 if (reanimate)
15314 /* Undo the return. */
15315 current_funccal->returned = FALSE;
15316
15317 /*
15318 * Cleanup (and inactivate) conditionals, but stop when a try conditional
15319 * not in its finally clause (which then is to be executed next) is found.
15320 * In this case, make the ":return" pending for execution at the ":endtry".
15321 * Otherwise, return normally.
15322 */
15323 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
15324 if (idx >= 0)
15325 {
15326 cstack->cs_pending[idx] = CSTP_RETURN;
15327
15328 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015329 /* A pending return again gets pending. "rettv" points to an
15330 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 else
15334 {
15335 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015340 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 {
15342 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015343 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015344 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 else
15346 EMSG(_(e_outofmem));
15347 }
15348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350
15351 if (reanimate)
15352 {
15353 /* The pending return value could be overwritten by a ":return"
15354 * without argument in a finally clause; reset the default
15355 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 current_funccal->rettv->v_type = VAR_NUMBER;
15357 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 }
15359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 }
15362 else
15363 {
15364 current_funccal->returned = TRUE;
15365
15366 /* If the return is carried out now, store the return value. For
15367 * a return immediately after reanimation, the value is already
15368 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000015372 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 }
15376 }
15377
15378 return idx < 0;
15379}
15380
15381/*
15382 * Free the variable with a pending return value.
15383 */
15384 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385discard_pending_return(rettv)
15386 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387{
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389}
15390
15391/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 * is an allocated string. Used by report_pending() for verbose messages.
15394 */
15395 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396get_return_cmd(rettv)
15397 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 char_u *s;
15400 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015401 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 if (rettv == NULL)
15404 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 else
Bram Moolenaar33570922005-01-25 22:26:29 +000015406 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407
15408 STRCPY(IObuff, ":return ");
15409 STRNCPY(IObuff + 8, s, IOSIZE - 8);
15410 if (STRLEN(s) + 8 >= IOSIZE)
15411 STRCPY(IObuff + IOSIZE - 4, "...");
15412 vim_free(tofree);
15413 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414}
15415
15416/*
15417 * Get next function line.
15418 * Called by do_cmdline() to get the next line.
15419 * Returns allocated string, or NULL for end of function.
15420 */
15421/* ARGSUSED */
15422 char_u *
15423get_func_line(c, cookie, indent)
15424 int c; /* not used */
15425 void *cookie;
15426 int indent; /* not used */
15427{
Bram Moolenaar33570922005-01-25 22:26:29 +000015428 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 char_u *retval;
15430 garray_T *gap; /* growarray with function lines */
15431
15432 /* If breakpoints have been added/deleted need to check for it. */
15433 if (fcp->dbg_tick != debug_tick)
15434 {
15435 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15436 sourcing_lnum);
15437 fcp->dbg_tick = debug_tick;
15438 }
15439
15440 gap = &fcp->func->lines;
15441 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15442 retval = NULL;
15443 else if (fcp->returned || fcp->linenr >= gap->ga_len)
15444 retval = NULL;
15445 else
15446 {
15447 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
15448 sourcing_lnum = fcp->linenr;
15449 }
15450
15451 /* Did we encounter a breakpoint? */
15452 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
15453 {
15454 dbg_breakpoint(fcp->func->name, sourcing_lnum);
15455 /* Find next breakpoint. */
15456 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
15457 sourcing_lnum);
15458 fcp->dbg_tick = debug_tick;
15459 }
15460
15461 return retval;
15462}
15463
15464/*
15465 * Return TRUE if the currently active function should be ended, because a
15466 * return was encountered or an error occured. Used inside a ":while".
15467 */
15468 int
15469func_has_ended(cookie)
15470 void *cookie;
15471{
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473
15474 /* Ignore the "abort" flag if the abortion behavior has been changed due to
15475 * an error inside a try conditional. */
15476 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
15477 || fcp->returned);
15478}
15479
15480/*
15481 * return TRUE if cookie indicates a function which "abort"s on errors.
15482 */
15483 int
15484func_has_abort(cookie)
15485 void *cookie;
15486{
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 return ((funccall_T *)cookie)->func->flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488}
15489
15490#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
15491typedef enum
15492{
15493 VAR_FLAVOUR_DEFAULT,
15494 VAR_FLAVOUR_SESSION,
15495 VAR_FLAVOUR_VIMINFO
15496} var_flavour_T;
15497
15498static var_flavour_T var_flavour __ARGS((char_u *varname));
15499
15500 static var_flavour_T
15501var_flavour(varname)
15502 char_u *varname;
15503{
15504 char_u *p = varname;
15505
15506 if (ASCII_ISUPPER(*p))
15507 {
15508 while (*(++p))
15509 if (ASCII_ISLOWER(*p))
15510 return VAR_FLAVOUR_SESSION;
15511 return VAR_FLAVOUR_VIMINFO;
15512 }
15513 else
15514 return VAR_FLAVOUR_DEFAULT;
15515}
15516#endif
15517
15518#if defined(FEAT_VIMINFO) || defined(PROTO)
15519/*
15520 * Restore global vars that start with a capital from the viminfo file
15521 */
15522 int
15523read_viminfo_varlist(virp, writing)
15524 vir_T *virp;
15525 int writing;
15526{
15527 char_u *tab;
15528 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015529 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530
15531 if (!writing && (find_viminfo_parameter('!') != NULL))
15532 {
15533 tab = vim_strchr(virp->vir_line + 1, '\t');
15534 if (tab != NULL)
15535 {
15536 *tab++ = '\0'; /* isolate the variable name */
15537 if (*tab == 'S') /* string var */
15538 is_string = TRUE;
15539
15540 tab = vim_strchr(tab, '\t');
15541 if (tab != NULL)
15542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 if (is_string)
15544 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015545 tv.v_type = VAR_STRING;
15546 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 }
15549 else
15550 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015551 tv.v_type = VAR_NUMBER;
15552 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015554 set_var(virp->vir_line + 1, &tv, FALSE);
15555 if (is_string)
15556 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 }
15558 }
15559 }
15560
15561 return viminfo_readline(virp);
15562}
15563
15564/*
15565 * Write global vars that start with a capital to the viminfo file
15566 */
15567 void
15568write_viminfo_varlist(fp)
15569 FILE *fp;
15570{
Bram Moolenaar33570922005-01-25 22:26:29 +000015571 hashitem_T *hi;
15572 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000015573 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 char *s;
15575 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015576 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577
15578 if (find_viminfo_parameter('!') == NULL)
15579 return;
15580
15581 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000015582
Bram Moolenaar33570922005-01-25 22:26:29 +000015583 todo = globvarht.ht_used;
15584 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015586 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015588 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015589 this_var = HI2DI(hi);
15590 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000015593 {
15594 case VAR_STRING: s = "STR"; break;
15595 case VAR_NUMBER: s = "NUM"; break;
15596 default: continue;
15597 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015598 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
15599 viminfo_writestring(fp, echo_string(&this_var->di_tv,
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015600 &tofree, numbuf));
Bram Moolenaara7043832005-01-21 11:56:39 +000015601 vim_free(tofree);
15602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 }
15604 }
15605}
15606#endif
15607
15608#if defined(FEAT_SESSION) || defined(PROTO)
15609 int
15610store_session_globals(fd)
15611 FILE *fd;
15612{
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 hashitem_T *hi;
15614 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000015615 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 char_u *p, *t;
15617
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 todo = globvarht.ht_used;
15619 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015621 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015623 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 this_var = HI2DI(hi);
15625 if ((this_var->di_tv.v_type == VAR_NUMBER
15626 || this_var->di_tv.v_type == VAR_STRING)
15627 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000015628 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015629 /* Escape special characters with a backslash. Turn a LF and
15630 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000015632 (char_u *)"\\\"\n\r");
15633 if (p == NULL) /* out of memory */
15634 break;
15635 for (t = p; *t != NUL; ++t)
15636 if (*t == '\n')
15637 *t = 'n';
15638 else if (*t == '\r')
15639 *t = 'r';
15640 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000015641 this_var->di_key,
15642 (this_var->di_tv.v_type == VAR_STRING) ? '"'
15643 : ' ',
15644 p,
15645 (this_var->di_tv.v_type == VAR_STRING) ? '"'
15646 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000015647 || put_eol(fd) == FAIL)
15648 {
15649 vim_free(p);
15650 return FAIL;
15651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 vim_free(p);
15653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 }
15655 }
15656 return OK;
15657}
15658#endif
15659
15660#endif /* FEAT_EVAL */
15661
15662#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
15663
15664
15665#ifdef WIN3264
15666/*
15667 * Functions for ":8" filename modifier: get 8.3 version of a filename.
15668 */
15669static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15670static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
15671static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
15672
15673/*
15674 * Get the short pathname of a file.
15675 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
15676 */
15677 static int
15678get_short_pathname(fnamep, bufp, fnamelen)
15679 char_u **fnamep;
15680 char_u **bufp;
15681 int *fnamelen;
15682{
15683 int l,len;
15684 char_u *newbuf;
15685
15686 len = *fnamelen;
15687
15688 l = GetShortPathName(*fnamep, *fnamep, len);
15689 if (l > len - 1)
15690 {
15691 /* If that doesn't work (not enough space), then save the string
15692 * and try again with a new buffer big enough
15693 */
15694 newbuf = vim_strnsave(*fnamep, l);
15695 if (newbuf == NULL)
15696 return 0;
15697
15698 vim_free(*bufp);
15699 *fnamep = *bufp = newbuf;
15700
15701 l = GetShortPathName(*fnamep,*fnamep,l+1);
15702
15703 /* Really should always succeed, as the buffer is big enough */
15704 }
15705
15706 *fnamelen = l;
15707 return 1;
15708}
15709
15710/*
15711 * Create a short path name. Returns the length of the buffer it needs.
15712 * Doesn't copy over the end of the buffer passed in.
15713 */
15714 static int
15715shortpath_for_invalid_fname(fname, bufp, fnamelen)
15716 char_u **fname;
15717 char_u **bufp;
15718 int *fnamelen;
15719{
15720 char_u *s, *p, *pbuf2, *pbuf3;
15721 char_u ch;
15722 int l,len,len2,plen,slen;
15723
15724 /* Make a copy */
15725 len2 = *fnamelen;
15726 pbuf2 = vim_strnsave(*fname, len2);
15727 pbuf3 = NULL;
15728
15729 s = pbuf2 + len2 - 1; /* Find the end */
15730 slen = 1;
15731 plen = len2;
15732
15733 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015734 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 {
15736 --s;
15737 ++slen;
15738 --plen;
15739 }
15740
15741 do
15742 {
15743 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015744 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 {
15746 --s;
15747 ++slen;
15748 --plen;
15749 }
15750 if (s <= pbuf2)
15751 break;
15752
15753 /* Remeber the character that is about to be blatted */
15754 ch = *s;
15755 *s = 0; /* get_short_pathname requires a null-terminated string */
15756
15757 /* Try it in situ */
15758 p = pbuf2;
15759 if (!get_short_pathname(&p, &pbuf3, &plen))
15760 {
15761 vim_free(pbuf2);
15762 return -1;
15763 }
15764 *s = ch; /* Preserve the string */
15765 } while (plen == 0);
15766
15767 if (plen > 0)
15768 {
15769 /* Remeber the length of the new string. */
15770 *fnamelen = len = plen + slen;
15771 vim_free(*bufp);
15772 if (len > len2)
15773 {
15774 /* If there's not enough space in the currently allocated string,
15775 * then copy it to a buffer big enough.
15776 */
15777 *fname= *bufp = vim_strnsave(p, len);
15778 if (*fname == NULL)
15779 return -1;
15780 }
15781 else
15782 {
15783 /* Transfer pbuf2 to being the main buffer (it's big enough) */
15784 *fname = *bufp = pbuf2;
15785 if (p != pbuf2)
15786 strncpy(*fname, p, plen);
15787 pbuf2 = NULL;
15788 }
15789 /* Concat the next bit */
15790 strncpy(*fname + plen, s, slen);
15791 (*fname)[len] = '\0';
15792 }
15793 vim_free(pbuf3);
15794 vim_free(pbuf2);
15795 return 0;
15796}
15797
15798/*
15799 * Get a pathname for a partial path.
15800 */
15801 static int
15802shortpath_for_partial(fnamep, bufp, fnamelen)
15803 char_u **fnamep;
15804 char_u **bufp;
15805 int *fnamelen;
15806{
15807 int sepcount, len, tflen;
15808 char_u *p;
15809 char_u *pbuf, *tfname;
15810 int hasTilde;
15811
15812 /* Count up the path seperators from the RHS.. so we know which part
15813 * of the path to return.
15814 */
15815 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015816 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 if (vim_ispathsep(*p))
15818 ++sepcount;
15819
15820 /* Need full path first (use expand_env() to remove a "~/") */
15821 hasTilde = (**fnamep == '~');
15822 if (hasTilde)
15823 pbuf = tfname = expand_env_save(*fnamep);
15824 else
15825 pbuf = tfname = FullName_save(*fnamep, FALSE);
15826
15827 len = tflen = STRLEN(tfname);
15828
15829 if (!get_short_pathname(&tfname, &pbuf, &len))
15830 return -1;
15831
15832 if (len == 0)
15833 {
15834 /* Don't have a valid filename, so shorten the rest of the
15835 * path if we can. This CAN give us invalid 8.3 filenames, but
15836 * there's not a lot of point in guessing what it might be.
15837 */
15838 len = tflen;
15839 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
15840 return -1;
15841 }
15842
15843 /* Count the paths backward to find the beginning of the desired string. */
15844 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015845 {
15846#ifdef FEAT_MBYTE
15847 if (has_mbyte)
15848 p -= mb_head_off(tfname, p);
15849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 if (vim_ispathsep(*p))
15851 {
15852 if (sepcount == 0 || (hasTilde && sepcount == 1))
15853 break;
15854 else
15855 sepcount --;
15856 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 if (hasTilde)
15859 {
15860 --p;
15861 if (p >= tfname)
15862 *p = '~';
15863 else
15864 return -1;
15865 }
15866 else
15867 ++p;
15868
15869 /* Copy in the string - p indexes into tfname - allocated at pbuf */
15870 vim_free(*bufp);
15871 *fnamelen = (int)STRLEN(p);
15872 *bufp = pbuf;
15873 *fnamep = p;
15874
15875 return 0;
15876}
15877#endif /* WIN3264 */
15878
15879/*
15880 * Adjust a filename, according to a string of modifiers.
15881 * *fnamep must be NUL terminated when called. When returning, the length is
15882 * determined by *fnamelen.
15883 * Returns valid flags.
15884 * When there is an error, *fnamep is set to NULL.
15885 */
15886 int
15887modify_fname(src, usedlen, fnamep, bufp, fnamelen)
15888 char_u *src; /* string with modifiers */
15889 int *usedlen; /* characters after src that are used */
15890 char_u **fnamep; /* file name so far */
15891 char_u **bufp; /* buffer for allocated file name or NULL */
15892 int *fnamelen; /* length of fnamep */
15893{
15894 int valid = 0;
15895 char_u *tail;
15896 char_u *s, *p, *pbuf;
15897 char_u dirname[MAXPATHL];
15898 int c;
15899 int has_fullname = 0;
15900#ifdef WIN3264
15901 int has_shortname = 0;
15902#endif
15903
15904repeat:
15905 /* ":p" - full path/file_name */
15906 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
15907 {
15908 has_fullname = 1;
15909
15910 valid |= VALID_PATH;
15911 *usedlen += 2;
15912
15913 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
15914 if ((*fnamep)[0] == '~'
15915#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
15916 && ((*fnamep)[1] == '/'
15917# ifdef BACKSLASH_IN_FILENAME
15918 || (*fnamep)[1] == '\\'
15919# endif
15920 || (*fnamep)[1] == NUL)
15921
15922#endif
15923 )
15924 {
15925 *fnamep = expand_env_save(*fnamep);
15926 vim_free(*bufp); /* free any allocated file name */
15927 *bufp = *fnamep;
15928 if (*fnamep == NULL)
15929 return -1;
15930 }
15931
15932 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015933 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 {
15935 if (vim_ispathsep(*p)
15936 && p[1] == '.'
15937 && (p[2] == NUL
15938 || vim_ispathsep(p[2])
15939 || (p[2] == '.'
15940 && (p[3] == NUL || vim_ispathsep(p[3])))))
15941 break;
15942 }
15943
15944 /* FullName_save() is slow, don't use it when not needed. */
15945 if (*p != NUL || !vim_isAbsName(*fnamep))
15946 {
15947 *fnamep = FullName_save(*fnamep, *p != NUL);
15948 vim_free(*bufp); /* free any allocated file name */
15949 *bufp = *fnamep;
15950 if (*fnamep == NULL)
15951 return -1;
15952 }
15953
15954 /* Append a path separator to a directory. */
15955 if (mch_isdir(*fnamep))
15956 {
15957 /* Make room for one or two extra characters. */
15958 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
15959 vim_free(*bufp); /* free any allocated file name */
15960 *bufp = *fnamep;
15961 if (*fnamep == NULL)
15962 return -1;
15963 add_pathsep(*fnamep);
15964 }
15965 }
15966
15967 /* ":." - path relative to the current directory */
15968 /* ":~" - path relative to the home directory */
15969 /* ":8" - shortname path - postponed till after */
15970 while (src[*usedlen] == ':'
15971 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
15972 {
15973 *usedlen += 2;
15974 if (c == '8')
15975 {
15976#ifdef WIN3264
15977 has_shortname = 1; /* Postpone this. */
15978#endif
15979 continue;
15980 }
15981 pbuf = NULL;
15982 /* Need full path first (use expand_env() to remove a "~/") */
15983 if (!has_fullname)
15984 {
15985 if (c == '.' && **fnamep == '~')
15986 p = pbuf = expand_env_save(*fnamep);
15987 else
15988 p = pbuf = FullName_save(*fnamep, FALSE);
15989 }
15990 else
15991 p = *fnamep;
15992
15993 has_fullname = 0;
15994
15995 if (p != NULL)
15996 {
15997 if (c == '.')
15998 {
15999 mch_dirname(dirname, MAXPATHL);
16000 s = shorten_fname(p, dirname);
16001 if (s != NULL)
16002 {
16003 *fnamep = s;
16004 if (pbuf != NULL)
16005 {
16006 vim_free(*bufp); /* free any allocated file name */
16007 *bufp = pbuf;
16008 pbuf = NULL;
16009 }
16010 }
16011 }
16012 else
16013 {
16014 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
16015 /* Only replace it when it starts with '~' */
16016 if (*dirname == '~')
16017 {
16018 s = vim_strsave(dirname);
16019 if (s != NULL)
16020 {
16021 *fnamep = s;
16022 vim_free(*bufp);
16023 *bufp = s;
16024 }
16025 }
16026 }
16027 vim_free(pbuf);
16028 }
16029 }
16030
16031 tail = gettail(*fnamep);
16032 *fnamelen = (int)STRLEN(*fnamep);
16033
16034 /* ":h" - head, remove "/file_name", can be repeated */
16035 /* Don't remove the first "/" or "c:\" */
16036 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
16037 {
16038 valid |= VALID_HEAD;
16039 *usedlen += 2;
16040 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016041 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 --tail;
16043 *fnamelen = (int)(tail - *fnamep);
16044#ifdef VMS
16045 if (*fnamelen > 0)
16046 *fnamelen += 1; /* the path separator is part of the path */
16047#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016048 while (tail > s && !after_pathsep(s, tail))
16049 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 }
16051
16052 /* ":8" - shortname */
16053 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
16054 {
16055 *usedlen += 2;
16056#ifdef WIN3264
16057 has_shortname = 1;
16058#endif
16059 }
16060
16061#ifdef WIN3264
16062 /* Check shortname after we have done 'heads' and before we do 'tails'
16063 */
16064 if (has_shortname)
16065 {
16066 pbuf = NULL;
16067 /* Copy the string if it is shortened by :h */
16068 if (*fnamelen < (int)STRLEN(*fnamep))
16069 {
16070 p = vim_strnsave(*fnamep, *fnamelen);
16071 if (p == 0)
16072 return -1;
16073 vim_free(*bufp);
16074 *bufp = *fnamep = p;
16075 }
16076
16077 /* Split into two implementations - makes it easier. First is where
16078 * there isn't a full name already, second is where there is.
16079 */
16080 if (!has_fullname && !vim_isAbsName(*fnamep))
16081 {
16082 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
16083 return -1;
16084 }
16085 else
16086 {
16087 int l;
16088
16089 /* Simple case, already have the full-name
16090 * Nearly always shorter, so try first time. */
16091 l = *fnamelen;
16092 if (!get_short_pathname(fnamep, bufp, &l))
16093 return -1;
16094
16095 if (l == 0)
16096 {
16097 /* Couldn't find the filename.. search the paths.
16098 */
16099 l = *fnamelen;
16100 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
16101 return -1;
16102 }
16103 *fnamelen = l;
16104 }
16105 }
16106#endif /* WIN3264 */
16107
16108 /* ":t" - tail, just the basename */
16109 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
16110 {
16111 *usedlen += 2;
16112 *fnamelen -= (int)(tail - *fnamep);
16113 *fnamep = tail;
16114 }
16115
16116 /* ":e" - extension, can be repeated */
16117 /* ":r" - root, without extension, can be repeated */
16118 while (src[*usedlen] == ':'
16119 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
16120 {
16121 /* find a '.' in the tail:
16122 * - for second :e: before the current fname
16123 * - otherwise: The last '.'
16124 */
16125 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
16126 s = *fnamep - 2;
16127 else
16128 s = *fnamep + *fnamelen - 1;
16129 for ( ; s > tail; --s)
16130 if (s[0] == '.')
16131 break;
16132 if (src[*usedlen + 1] == 'e') /* :e */
16133 {
16134 if (s > tail)
16135 {
16136 *fnamelen += (int)(*fnamep - (s + 1));
16137 *fnamep = s + 1;
16138#ifdef VMS
16139 /* cut version from the extension */
16140 s = *fnamep + *fnamelen - 1;
16141 for ( ; s > *fnamep; --s)
16142 if (s[0] == ';')
16143 break;
16144 if (s > *fnamep)
16145 *fnamelen = s - *fnamep;
16146#endif
16147 }
16148 else if (*fnamep <= tail)
16149 *fnamelen = 0;
16150 }
16151 else /* :r */
16152 {
16153 if (s > tail) /* remove one extension */
16154 *fnamelen = (int)(s - *fnamep);
16155 }
16156 *usedlen += 2;
16157 }
16158
16159 /* ":s?pat?foo?" - substitute */
16160 /* ":gs?pat?foo?" - global substitute */
16161 if (src[*usedlen] == ':'
16162 && (src[*usedlen + 1] == 's'
16163 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
16164 {
16165 char_u *str;
16166 char_u *pat;
16167 char_u *sub;
16168 int sep;
16169 char_u *flags;
16170 int didit = FALSE;
16171
16172 flags = (char_u *)"";
16173 s = src + *usedlen + 2;
16174 if (src[*usedlen + 1] == 'g')
16175 {
16176 flags = (char_u *)"g";
16177 ++s;
16178 }
16179
16180 sep = *s++;
16181 if (sep)
16182 {
16183 /* find end of pattern */
16184 p = vim_strchr(s, sep);
16185 if (p != NULL)
16186 {
16187 pat = vim_strnsave(s, (int)(p - s));
16188 if (pat != NULL)
16189 {
16190 s = p + 1;
16191 /* find end of substitution */
16192 p = vim_strchr(s, sep);
16193 if (p != NULL)
16194 {
16195 sub = vim_strnsave(s, (int)(p - s));
16196 str = vim_strnsave(*fnamep, *fnamelen);
16197 if (sub != NULL && str != NULL)
16198 {
16199 *usedlen = (int)(p + 1 - src);
16200 s = do_string_sub(str, pat, sub, flags);
16201 if (s != NULL)
16202 {
16203 *fnamep = s;
16204 *fnamelen = (int)STRLEN(s);
16205 vim_free(*bufp);
16206 *bufp = s;
16207 didit = TRUE;
16208 }
16209 }
16210 vim_free(sub);
16211 vim_free(str);
16212 }
16213 vim_free(pat);
16214 }
16215 }
16216 /* after using ":s", repeat all the modifiers */
16217 if (didit)
16218 goto repeat;
16219 }
16220 }
16221
16222 return valid;
16223}
16224
16225/*
16226 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
16227 * "flags" can be "g" to do a global substitute.
16228 * Returns an allocated string, NULL for error.
16229 */
16230 char_u *
16231do_string_sub(str, pat, sub, flags)
16232 char_u *str;
16233 char_u *pat;
16234 char_u *sub;
16235 char_u *flags;
16236{
16237 int sublen;
16238 regmatch_T regmatch;
16239 int i;
16240 int do_all;
16241 char_u *tail;
16242 garray_T ga;
16243 char_u *ret;
16244 char_u *save_cpo;
16245
16246 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
16247 save_cpo = p_cpo;
16248 p_cpo = (char_u *)"";
16249
16250 ga_init2(&ga, 1, 200);
16251
16252 do_all = (flags[0] == 'g');
16253
16254 regmatch.rm_ic = p_ic;
16255 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16256 if (regmatch.regprog != NULL)
16257 {
16258 tail = str;
16259 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
16260 {
16261 /*
16262 * Get some space for a temporary buffer to do the substitution
16263 * into. It will contain:
16264 * - The text up to where the match is.
16265 * - The substituted text.
16266 * - The text after the match.
16267 */
16268 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
16269 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
16270 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
16271 {
16272 ga_clear(&ga);
16273 break;
16274 }
16275
16276 /* copy the text up to where the match is */
16277 i = (int)(regmatch.startp[0] - tail);
16278 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
16279 /* add the substituted text */
16280 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
16281 + ga.ga_len + i, TRUE, TRUE, FALSE);
16282 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283 /* avoid getting stuck on a match with an empty string */
16284 if (tail == regmatch.endp[0])
16285 {
16286 if (*tail == NUL)
16287 break;
16288 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
16289 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 }
16291 else
16292 {
16293 tail = regmatch.endp[0];
16294 if (*tail == NUL)
16295 break;
16296 }
16297 if (!do_all)
16298 break;
16299 }
16300
16301 if (ga.ga_data != NULL)
16302 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
16303
16304 vim_free(regmatch.regprog);
16305 }
16306
16307 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
16308 ga_clear(&ga);
16309 p_cpo = save_cpo;
16310
16311 return ret;
16312}
16313
16314#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */