blob: 913f8ed0f2e7aca967650710c6d91e8c87786df2 [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 Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
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 Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000110
111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000126 * Array to hold the hashtab with variables local to each sourced script.
127 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000129typedef struct
130{
131 dictitem_T sv_var;
132 dict_T sv_dict;
133} scriptvar_T;
134
135static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
136#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
137#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139static int echo_attr = 0; /* attributes used for ":echo" */
140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000141/* Values for trans_function_name() argument: */
142#define TFN_INT 1 /* internal function name OK */
143#define TFN_QUIET 2 /* no error messages */
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
146 * Structure to hold info for a user function.
147 */
148typedef struct ufunc ufunc_T;
149
150struct ufunc
151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000152 int uf_varargs; /* variable nr of arguments */
153 int uf_flags;
154 int uf_calls; /* nr of active calls */
155 garray_T uf_args; /* arguments */
156 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000157#ifdef FEAT_PROFILE
158 int uf_profiling; /* TRUE when func is being profiled */
159 /* profiling the function as a whole */
160 int uf_tm_count; /* nr of calls */
161 proftime_T uf_tm_total; /* time spend in function + children */
162 proftime_T uf_tm_self; /* time spend in function itself */
163 proftime_T uf_tm_start; /* time at function call */
164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
167 proftime_T *uf_tml_total; /* time spend in a line + children */
168 proftime_T *uf_tml_self; /* time spend in a line itself */
169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000191static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000267#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T vv_di; /* value and name for key */
273 char vv_filler[16]; /* space for LONGEST name below!!! */
274 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
275} vimvars[VV_LEN] =
276{
277 /*
278 * The order here must match the VV_ defines in vim.h!
279 * Initializing a union does not work, leave tv.vval empty to get zero's.
280 */
281 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
282 {VV_NAME("count1", VAR_NUMBER), VV_RO},
283 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
284 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
285 {VV_NAME("warningmsg", VAR_STRING), 0},
286 {VV_NAME("statusmsg", VAR_STRING), 0},
287 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
288 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
289 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
290 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
291 {VV_NAME("termresponse", VAR_STRING), VV_RO},
292 {VV_NAME("fname", VAR_STRING), VV_RO},
293 {VV_NAME("lang", VAR_STRING), VV_RO},
294 {VV_NAME("lc_time", VAR_STRING), VV_RO},
295 {VV_NAME("ctype", VAR_STRING), VV_RO},
296 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
298 {VV_NAME("fname_in", VAR_STRING), VV_RO},
299 {VV_NAME("fname_out", VAR_STRING), VV_RO},
300 {VV_NAME("fname_new", VAR_STRING), VV_RO},
301 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
302 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
303 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
304 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
306 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("progname", VAR_STRING), VV_RO},
308 {VV_NAME("servername", VAR_STRING), VV_RO},
309 {VV_NAME("dying", VAR_NUMBER), VV_RO},
310 {VV_NAME("exception", VAR_STRING), VV_RO},
311 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
312 {VV_NAME("register", VAR_STRING), VV_RO},
313 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
314 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000315 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
316 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000317 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000318 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
319 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000320 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
321 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
322 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
323 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
324 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000325};
326
327/* shorthand */
328#define vv_type vv_di.di_tv.v_type
329#define vv_nr vv_di.di_tv.vval.v_number
330#define vv_str vv_di.di_tv.vval.v_string
331#define vv_tv vv_di.di_tv
332
333/*
334 * The v: variables are stored in dictionary "vimvardict".
335 * "vimvars_var" is the variable that is used for the "l:" scope.
336 */
337static dict_T vimvardict;
338static dictitem_T vimvars_var;
339#define vimvarht vimvardict.dv_hashtab
340
341static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
342static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
343static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000349static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000350static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
351static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static list_T *list_alloc __ARGS((void));
355static void list_unref __ARGS((list_T *l));
356static void list_free __ARGS((list_T *l));
357static listitem_T *listitem_alloc __ARGS((void));
358static void listitem_free __ARGS((listitem_T *item));
359static void listitem_remove __ARGS((list_T *l, listitem_T *item));
360static long list_len __ARGS((list_T *l));
361static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
362static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
363static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
364static int string_isa_number __ARGS((char_u *s));
365static listitem_T *list_find __ARGS((list_T *l, long n));
366static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000367static void list_append __ARGS((list_T *l, listitem_T *item));
368static int list_append_tv __ARGS((list_T *l, typval_T *tv));
369static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
370static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
371static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000372static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000373static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
374static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000375static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar33570922005-01-25 22:26:29 +0000377static void dict_unref __ARGS((dict_T *d));
378static void dict_free __ARGS((dict_T *d));
379static dictitem_T *dictitem_alloc __ARGS((char_u *key));
380static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
381static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
382static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000383static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000384static int dict_add __ARGS((dict_T *d, dictitem_T *item));
385static long dict_len __ARGS((dict_T *d));
386static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
387static char_u *dict2string __ARGS((typval_T *tv));
388static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389
390static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
391static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
392static char_u *string_quote __ARGS((char_u *str, int function));
393static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int find_internal_func __ARGS((char_u *name));
395static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
396static 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));
397static 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));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000398static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399
400static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
424static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000463static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000491static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000508static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512#ifdef vim_mkdir
513static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000520static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000538static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
544#ifdef HAVE_STRFTIME
545static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
546#endif
547static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000559static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000575static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576
577static win_T *find_win_by_nr __ARGS((typval_T *vp));
578static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
579static int get_env_len __ARGS((char_u **arg));
580static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000582static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
583#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
584#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
585 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000587static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000588static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
589static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static typval_T *alloc_tv __ARGS((void));
591static typval_T *alloc_string_tv __ARGS((char_u *string));
592static void free_tv __ARGS((typval_T *varp));
593static void clear_tv __ARGS((typval_T *varp));
594static void init_tv __ARGS((typval_T *varp));
595static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000596static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
598static char_u *get_tv_string __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000599static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000603static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000604static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
605static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
606static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
607static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
608static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
609static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
610static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000611static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000613static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
615static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
616static int eval_fname_script __ARGS((char_u *p));
617static int eval_fname_sid __ARGS((char_u *p));
618static void list_func_head __ARGS((ufunc_T *fp, int indent));
619static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
620static ufunc_T *find_func __ARGS((char_u *name));
621static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000622static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000623#ifdef FEAT_PROFILE
624static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000625static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
626static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
627static int
628# ifdef __BORLANDC__
629 _RTLENTRYF
630# endif
631 prof_total_cmp __ARGS((const void *s1, const void *s2));
632static int
633# ifdef __BORLANDC__
634 _RTLENTRYF
635# endif
636 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000637#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638static int script_autoload __ARGS((char_u *name));
639static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void func_free __ARGS((ufunc_T *fp));
641static void func_unref __ARGS((char_u *name));
642static void func_ref __ARGS((char_u *name));
643static 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));
644static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
645
646static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
647
648static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
649static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
650static char_u *skip_var_one __ARGS((char_u *arg));
651static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
652static void list_glob_vars __ARGS((void));
653static void list_buf_vars __ARGS((void));
654static void list_win_vars __ARGS((void));
655static void list_vim_vars __ARGS((void));
656static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
657static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
658static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000659static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void clear_lval __ARGS((lval_T *lp));
661static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
662static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
663static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
664static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
665static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000666static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000668static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
669static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000670static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000672/* Character used as separated in autoload function/variable names. */
673#define AUTOLOAD_CHAR '#'
674
Bram Moolenaar33570922005-01-25 22:26:29 +0000675/*
676 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000677 */
678 void
679eval_init()
680{
Bram Moolenaar33570922005-01-25 22:26:29 +0000681 int i;
682 struct vimvar *p;
683
684 init_var_dict(&globvardict, &globvars_var);
685 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000686 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000687 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000688
689 for (i = 0; i < VV_LEN; ++i)
690 {
691 p = &vimvars[i];
692 STRCPY(p->vv_di.di_key, p->vv_name);
693 if (p->vv_flags & VV_RO)
694 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
695 else if (p->vv_flags & VV_RO_SBX)
696 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
697 else
698 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000699
700 /* add to v: scope dict, unless the value is not always available */
701 if (p->vv_type != VAR_UNKNOWN)
702 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000704 /* add to compat scope dict */
705 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000706 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000707}
708
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 * Return the name of the executed function.
711 */
712 char_u *
713func_name(cookie)
714 void *cookie;
715{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000716 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718
719/*
720 * Return the address holding the next breakpoint line for a funccall cookie.
721 */
722 linenr_T *
723func_breakpoint(cookie)
724 void *cookie;
725{
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727}
728
729/*
730 * Return the address holding the debug tick for a funccall cookie.
731 */
732 int *
733func_dbg_tick(cookie)
734 void *cookie;
735{
Bram Moolenaar33570922005-01-25 22:26:29 +0000736 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
739/*
740 * Return the nesting level for a funccall cookie.
741 */
742 int
743func_level(cookie)
744 void *cookie;
745{
Bram Moolenaar33570922005-01-25 22:26:29 +0000746 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747}
748
749/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000750funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
752/*
753 * Return TRUE when a function was ended by a ":return" command.
754 */
755 int
756current_func_returned()
757{
758 return current_funccal->returned;
759}
760
761
762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 * Set an internal variable to a string value. Creates the variable if it does
764 * not already exist.
765 */
766 void
767set_internal_string_var(name, value)
768 char_u *name;
769 char_u *value;
770{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000771 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000772 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 val = vim_strsave(value);
775 if (val != NULL)
776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000777 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000778 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000780 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 }
784}
785
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000786static lval_T *redir_lval = NULL;
787static char_u *redir_endp = NULL;
788static char_u *redir_varname = NULL;
789
790/*
791 * Start recording command output to a variable
792 * Returns OK if successfully completed the setup. FAIL otherwise.
793 */
794 int
795var_redir_start(name, append)
796 char_u *name;
797 int append; /* append to an existing variable */
798{
799 int save_emsg;
800 int err;
801 typval_T tv;
802
803 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000804 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000805 {
806 EMSG(_(e_invarg));
807 return FAIL;
808 }
809
810 redir_varname = vim_strsave(name);
811 if (redir_varname == NULL)
812 return FAIL;
813
814 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
815 if (redir_lval == NULL)
816 {
817 var_redir_stop();
818 return FAIL;
819 }
820
821 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000822 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
823 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
825 {
826 if (redir_endp != NULL && *redir_endp != NUL)
827 /* Trailing characters are present after the variable name */
828 EMSG(_(e_trailing));
829 else
830 EMSG(_(e_invarg));
831 var_redir_stop();
832 return FAIL;
833 }
834
835 /* check if we can write to the variable: set it to or append an empty
836 * string */
837 save_emsg = did_emsg;
838 did_emsg = FALSE;
839 tv.v_type = VAR_STRING;
840 tv.vval.v_string = (char_u *)"";
841 if (append)
842 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
843 else
844 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
845 err = did_emsg;
846 did_emsg += save_emsg;
847 if (err)
848 {
849 var_redir_stop();
850 return FAIL;
851 }
852 if (redir_lval->ll_newkey != NULL)
853 {
854 /* Dictionary item was created, don't do it again. */
855 vim_free(redir_lval->ll_newkey);
856 redir_lval->ll_newkey = NULL;
857 }
858
859 return OK;
860}
861
862/*
863 * Append "value[len]" to the variable set by var_redir_start().
864 */
865 void
866var_redir_str(value, len)
867 char_u *value;
868 int len;
869{
870 char_u *val;
871 typval_T tv;
872 int save_emsg;
873 int err;
874
875 if (redir_lval == NULL)
876 return;
877
878 if (len == -1)
879 /* Append the entire string */
880 val = vim_strsave(value);
881 else
882 /* Append only the specified number of characters */
883 val = vim_strnsave(value, len);
884 if (val == NULL)
885 return;
886
887 tv.v_type = VAR_STRING;
888 tv.vval.v_string = val;
889
890 save_emsg = did_emsg;
891 did_emsg = FALSE;
892 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
893 err = did_emsg;
894 did_emsg += save_emsg;
895 if (err)
896 var_redir_stop();
897
898 vim_free(tv.vval.v_string);
899}
900
901/*
902 * Stop redirecting command output to a variable.
903 */
904 void
905var_redir_stop()
906{
907 if (redir_lval != NULL)
908 {
909 clear_lval(redir_lval);
910 vim_free(redir_lval);
911 redir_lval = NULL;
912 }
913 vim_free(redir_varname);
914 redir_varname = NULL;
915}
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917# if defined(FEAT_MBYTE) || defined(PROTO)
918 int
919eval_charconvert(enc_from, enc_to, fname_from, fname_to)
920 char_u *enc_from;
921 char_u *enc_to;
922 char_u *fname_from;
923 char_u *fname_to;
924{
925 int err = FALSE;
926
927 set_vim_var_string(VV_CC_FROM, enc_from, -1);
928 set_vim_var_string(VV_CC_TO, enc_to, -1);
929 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
930 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
931 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
932 err = TRUE;
933 set_vim_var_string(VV_CC_FROM, NULL, -1);
934 set_vim_var_string(VV_CC_TO, NULL, -1);
935 set_vim_var_string(VV_FNAME_IN, NULL, -1);
936 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
937
938 if (err)
939 return FAIL;
940 return OK;
941}
942# endif
943
944# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
945 int
946eval_printexpr(fname, args)
947 char_u *fname;
948 char_u *args;
949{
950 int err = FALSE;
951
952 set_vim_var_string(VV_FNAME_IN, fname, -1);
953 set_vim_var_string(VV_CMDARG, args, -1);
954 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
955 err = TRUE;
956 set_vim_var_string(VV_FNAME_IN, NULL, -1);
957 set_vim_var_string(VV_CMDARG, NULL, -1);
958
959 if (err)
960 {
961 mch_remove(fname);
962 return FAIL;
963 }
964 return OK;
965}
966# endif
967
968# if defined(FEAT_DIFF) || defined(PROTO)
969 void
970eval_diff(origfile, newfile, outfile)
971 char_u *origfile;
972 char_u *newfile;
973 char_u *outfile;
974{
975 int err = FALSE;
976
977 set_vim_var_string(VV_FNAME_IN, origfile, -1);
978 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
979 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
980 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
981 set_vim_var_string(VV_FNAME_IN, NULL, -1);
982 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
983 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
984}
985
986 void
987eval_patch(origfile, difffile, outfile)
988 char_u *origfile;
989 char_u *difffile;
990 char_u *outfile;
991{
992 int err;
993
994 set_vim_var_string(VV_FNAME_IN, origfile, -1);
995 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
996 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
997 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
998 set_vim_var_string(VV_FNAME_IN, NULL, -1);
999 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1000 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1001}
1002# endif
1003
1004/*
1005 * Top level evaluation function, returning a boolean.
1006 * Sets "error" to TRUE if there was an error.
1007 * Return TRUE or FALSE.
1008 */
1009 int
1010eval_to_bool(arg, error, nextcmd, skip)
1011 char_u *arg;
1012 int *error;
1013 char_u **nextcmd;
1014 int skip; /* only parse, don't execute */
1015{
Bram Moolenaar33570922005-01-25 22:26:29 +00001016 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int retval = FALSE;
1018
1019 if (skip)
1020 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 else
1024 {
1025 *error = FALSE;
1026 if (!skip)
1027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001028 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001029 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 }
1032 if (skip)
1033 --emsg_skip;
1034
1035 return retval;
1036}
1037
1038/*
1039 * Top level evaluation function, returning a string. If "skip" is TRUE,
1040 * only parsing to "nextcmd" is done, without reporting errors. Return
1041 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1042 */
1043 char_u *
1044eval_to_string_skip(arg, nextcmd, skip)
1045 char_u *arg;
1046 char_u **nextcmd;
1047 int skip; /* only parse, don't execute */
1048{
Bram Moolenaar33570922005-01-25 22:26:29 +00001049 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 char_u *retval;
1051
1052 if (skip)
1053 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 retval = NULL;
1056 else
1057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001058 retval = vim_strsave(get_tv_string(&tv));
1059 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
1061 if (skip)
1062 --emsg_skip;
1063
1064 return retval;
1065}
1066
1067/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001068 * Skip over an expression at "*pp".
1069 * Return FAIL for an error, OK otherwise.
1070 */
1071 int
1072skip_expr(pp)
1073 char_u **pp;
1074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001076
1077 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001078 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001079}
1080
1081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 * Top level evaluation function, returning a string.
1083 * Return pointer to allocated memory, or NULL for failure.
1084 */
1085 char_u *
1086eval_to_string(arg, nextcmd)
1087 char_u *arg;
1088 char_u **nextcmd;
1089{
Bram Moolenaar33570922005-01-25 22:26:29 +00001090 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 char_u *retval;
1092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 retval = NULL;
1095 else
1096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001097 retval = vim_strsave(get_tv_string(&tv));
1098 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100
1101 return retval;
1102}
1103
1104/*
1105 * Call eval_to_string() with "sandbox" set and not using local variables.
1106 */
1107 char_u *
1108eval_to_string_safe(arg, nextcmd)
1109 char_u *arg;
1110 char_u **nextcmd;
1111{
1112 char_u *retval;
1113 void *save_funccalp;
1114
1115 save_funccalp = save_funccal();
1116 ++sandbox;
1117 retval = eval_to_string(arg, nextcmd);
1118 --sandbox;
1119 restore_funccal(save_funccalp);
1120 return retval;
1121}
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123/*
1124 * Top level evaluation function, returning a number.
1125 * Evaluates "expr" silently.
1126 * Returns -1 for an error.
1127 */
1128 int
1129eval_to_number(expr)
1130 char_u *expr;
1131{
Bram Moolenaar33570922005-01-25 22:26:29 +00001132 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001134 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136 ++emsg_off;
1137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 retval = -1;
1140 else
1141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001142 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 --emsg_off;
1146
1147 return retval;
1148}
1149
1150#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1151/*
1152 * Call some vimL function and return the result as a string
1153 * Uses argv[argc] for the function arguments.
1154 */
1155 char_u *
1156call_vim_function(func, argc, argv, safe)
1157 char_u *func;
1158 int argc;
1159 char_u **argv;
1160 int safe; /* use the sandbox */
1161{
1162 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T rettv;
1164 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 long n;
1166 int len;
1167 int i;
1168 int doesrange;
1169 void *save_funccalp = NULL;
1170
Bram Moolenaar33570922005-01-25 22:26:29 +00001171 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (argvars == NULL)
1173 return NULL;
1174
1175 for (i = 0; i < argc; i++)
1176 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001177 /* Pass a NULL or empty argument as an empty string */
1178 if (argv[i] == NULL || *argv[i] == NUL)
1179 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001180 argvars[i].v_type = VAR_STRING;
1181 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001182 continue;
1183 }
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 /* Recognize a number argument, the others must be strings. */
1186 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1187 if (len != 0 && len == (int)STRLEN(argv[i]))
1188 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001189 argvars[i].v_type = VAR_NUMBER;
1190 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 else
1193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001194 argvars[i].v_type = VAR_STRING;
1195 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
1197 }
1198
1199 if (safe)
1200 {
1201 save_funccalp = save_funccal();
1202 ++sandbox;
1203 }
1204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1206 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001208 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 vim_free(argvars);
1213
1214 if (safe)
1215 {
1216 --sandbox;
1217 restore_funccal(save_funccalp);
1218 }
1219 return retval;
1220}
1221#endif
1222
1223/*
1224 * Save the current function call pointer, and set it to NULL.
1225 * Used when executing autocommands and for ":source".
1226 */
1227 void *
1228save_funccal()
1229{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001230 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 current_funccal = NULL;
1233 return (void *)fc;
1234}
1235
1236 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001237restore_funccal(vfc)
1238 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001240 funccall_T *fc = (funccall_T *)vfc;
1241
1242 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243}
1244
Bram Moolenaar05159a02005-02-26 23:04:13 +00001245#if defined(FEAT_PROFILE) || defined(PROTO)
1246/*
1247 * Prepare profiling for entering a child or something else that is not
1248 * counted for the script/function itself.
1249 * Should always be called in pair with prof_child_exit().
1250 */
1251 void
1252prof_child_enter(tm)
1253 proftime_T *tm; /* place to store waittime */
1254{
1255 funccall_T *fc = current_funccal;
1256
1257 if (fc != NULL && fc->func->uf_profiling)
1258 profile_start(&fc->prof_child);
1259 script_prof_save(tm);
1260}
1261
1262/*
1263 * Take care of time spent in a child.
1264 * Should always be called after prof_child_enter().
1265 */
1266 void
1267prof_child_exit(tm)
1268 proftime_T *tm; /* where waittime was stored */
1269{
1270 funccall_T *fc = current_funccal;
1271
1272 if (fc != NULL && fc->func->uf_profiling)
1273 {
1274 profile_end(&fc->prof_child);
1275 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1276 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1277 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1278 }
1279 script_prof_restore(tm);
1280}
1281#endif
1282
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#ifdef FEAT_FOLDING
1285/*
1286 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1287 * it in "*cp". Doesn't give error messages.
1288 */
1289 int
1290eval_foldexpr(arg, cp)
1291 char_u *arg;
1292 int *cp;
1293{
Bram Moolenaar33570922005-01-25 22:26:29 +00001294 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 int retval;
1296 char_u *s;
1297
1298 ++emsg_off;
1299 ++sandbox;
1300 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 retval = 0;
1303 else
1304 {
1305 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (tv.v_type == VAR_NUMBER)
1307 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001308 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 retval = 0;
1310 else
1311 {
1312 /* If the result is a string, check if there is a non-digit before
1313 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001314 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 if (!VIM_ISDIGIT(*s) && *s != '-')
1316 *cp = *s++;
1317 retval = atol((char *)s);
1318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 --emsg_off;
1322 --sandbox;
1323
1324 return retval;
1325}
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 * ":let" list all variable values
1330 * ":let var1 var2" list variable values
1331 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001332 * ":let var += expr" assignment command.
1333 * ":let var -= expr" assignment command.
1334 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 void
1338ex_let(eap)
1339 exarg_T *eap;
1340{
1341 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 int var_count = 0;
1346 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001347 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001349 expr = skip_var_list(arg, &var_count, &semicolon);
1350 if (expr == NULL)
1351 return;
1352 expr = vim_strchr(expr, '=');
1353 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001355 /*
1356 * ":let" without "=": list variables
1357 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001358 if (*arg == '[')
1359 EMSG(_(e_invarg));
1360 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 /* ":let var1 var2" */
1362 arg = list_arg_vars(eap, arg);
1363 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001366 list_glob_vars();
1367 list_buf_vars();
1368 list_win_vars();
1369 list_vim_vars();
1370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 eap->nextcmd = check_nextcmd(arg);
1372 }
1373 else
1374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001375 op[0] = '=';
1376 op[1] = NUL;
1377 if (expr > arg)
1378 {
1379 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1380 op[0] = expr[-1]; /* +=, -= or .= */
1381 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001382 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (eap->skip)
1385 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (eap->skip)
1388 {
1389 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 --emsg_skip;
1392 }
1393 else if (i != FAIL)
1394 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001395 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 }
1400}
1401
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402/*
1403 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1404 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 * When "nextchars" is not NULL it points to a string with characters that
1406 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1407 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 * Returns OK or FAIL;
1409 */
1410 static int
1411ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1412 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414 int copy; /* copy values from "tv", don't move */
1415 int semicolon; /* from skip_var_list() */
1416 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001418{
1419 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 listitem_T *item;
1423 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424
1425 if (*arg != '[')
1426 {
1427 /*
1428 * ":let var = expr" or ":for var in list"
1429 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 return FAIL;
1432 return OK;
1433 }
1434
1435 /*
1436 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1437 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001438 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001439 {
1440 EMSG(_(e_listreq));
1441 return FAIL;
1442 }
1443
1444 i = list_len(l);
1445 if (semicolon == 0 && var_count < i)
1446 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001447 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001448 return FAIL;
1449 }
1450 if (var_count - semicolon > i)
1451 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001452 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001453 return FAIL;
1454 }
1455
1456 item = l->lv_first;
1457 while (*arg != ']')
1458 {
1459 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001461 item = item->li_next;
1462 if (arg == NULL)
1463 return FAIL;
1464
1465 arg = skipwhite(arg);
1466 if (*arg == ';')
1467 {
1468 /* Put the rest of the list (may be empty) in the var after ';'.
1469 * Create a new list for this. */
1470 l = list_alloc();
1471 if (l == NULL)
1472 return FAIL;
1473 while (item != NULL)
1474 {
1475 list_append_tv(l, &item->li_tv);
1476 item = item->li_next;
1477 }
1478
1479 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001480 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 ltv.vval.v_list = l;
1482 l->lv_refcount = 1;
1483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1485 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001486 clear_tv(&ltv);
1487 if (arg == NULL)
1488 return FAIL;
1489 break;
1490 }
1491 else if (*arg != ',' && *arg != ']')
1492 {
1493 EMSG2(_(e_intern2), "ex_let_vars()");
1494 return FAIL;
1495 }
1496 }
1497
1498 return OK;
1499}
1500
1501/*
1502 * Skip over assignable variable "var" or list of variables "[var, var]".
1503 * Used for ":let varvar = expr" and ":for varvar in expr".
1504 * For "[var, var]" increment "*var_count" for each variable.
1505 * for "[var, var; var]" set "semicolon".
1506 * Return NULL for an error.
1507 */
1508 static char_u *
1509skip_var_list(arg, var_count, semicolon)
1510 char_u *arg;
1511 int *var_count;
1512 int *semicolon;
1513{
1514 char_u *p, *s;
1515
1516 if (*arg == '[')
1517 {
1518 /* "[var, var]": find the matching ']'. */
1519 p = arg;
1520 while (1)
1521 {
1522 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1523 s = skip_var_one(p);
1524 if (s == p)
1525 {
1526 EMSG2(_(e_invarg2), p);
1527 return NULL;
1528 }
1529 ++*var_count;
1530
1531 p = skipwhite(s);
1532 if (*p == ']')
1533 break;
1534 else if (*p == ';')
1535 {
1536 if (*semicolon == 1)
1537 {
1538 EMSG(_("Double ; in list of variables"));
1539 return NULL;
1540 }
1541 *semicolon = 1;
1542 }
1543 else if (*p != ',')
1544 {
1545 EMSG2(_(e_invarg2), p);
1546 return NULL;
1547 }
1548 }
1549 return p + 1;
1550 }
1551 else
1552 return skip_var_one(arg);
1553}
1554
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555/*
1556 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1557 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 static char_u *
1559skip_var_one(arg)
1560 char_u *arg;
1561{
1562 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1563 ++arg;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001564 return find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565}
1566
Bram Moolenaara7043832005-01-21 11:56:39 +00001567/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 * List variables for hashtab "ht" with prefix "prefix".
1569 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001570 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001572list_hashtable_vars(ht, prefix, empty)
1573 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001574 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001576{
Bram Moolenaar33570922005-01-25 22:26:29 +00001577 hashitem_T *hi;
1578 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001579 int todo;
1580
1581 todo = ht->ht_used;
1582 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1583 {
1584 if (!HASHITEM_EMPTY(hi))
1585 {
1586 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001587 di = HI2DI(hi);
1588 if (empty || di->di_tv.v_type != VAR_STRING
1589 || di->di_tv.vval.v_string != NULL)
1590 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001591 }
1592 }
1593}
1594
1595/*
1596 * List global variables.
1597 */
1598 static void
1599list_glob_vars()
1600{
Bram Moolenaar33570922005-01-25 22:26:29 +00001601 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001602}
1603
1604/*
1605 * List buffer variables.
1606 */
1607 static void
1608list_buf_vars()
1609{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001610 char_u numbuf[NUMBUFLEN];
1611
Bram Moolenaar33570922005-01-25 22:26:29 +00001612 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001613
1614 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1615 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001616}
1617
1618/*
1619 * List window variables.
1620 */
1621 static void
1622list_win_vars()
1623{
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001625}
1626
1627/*
1628 * List Vim variables.
1629 */
1630 static void
1631list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632{
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634}
1635
1636/*
1637 * List variables in "arg".
1638 */
1639 static char_u *
1640list_arg_vars(eap, arg)
1641 exarg_T *eap;
1642 char_u *arg;
1643{
1644 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001645 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001647 char_u *name_start;
1648 char_u *arg_subsc;
1649 char_u *tofree;
1650 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651
1652 while (!ends_excmd(*arg) && !got_int)
1653 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001654 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001656 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001657 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1658 {
1659 emsg_severe = TRUE;
1660 EMSG(_(e_trailing));
1661 break;
1662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001666 /* get_name_len() takes care of expanding curly braces */
1667 name_start = name = arg;
1668 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1669 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001671 /* This is mainly to keep test 49 working: when expanding
1672 * curly braces fails overrule the exception error message. */
1673 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001675 emsg_severe = TRUE;
1676 EMSG2(_(e_invarg2), arg);
1677 break;
1678 }
1679 error = TRUE;
1680 }
1681 else
1682 {
1683 if (tofree != NULL)
1684 name = tofree;
1685 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 else
1688 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001689 /* handle d.key, l[idx], f(expr) */
1690 arg_subsc = arg;
1691 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001692 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001695 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001696 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001697 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001698 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001699 case 'g': list_glob_vars(); break;
1700 case 'b': list_buf_vars(); break;
1701 case 'w': list_win_vars(); break;
1702 case 'v': list_vim_vars(); break;
1703 default:
1704 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001705 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001706 }
1707 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001708 {
1709 char_u numbuf[NUMBUFLEN];
1710 char_u *tf;
1711 int c;
1712 char_u *s;
1713
1714 s = echo_string(&tv, &tf, numbuf);
1715 c = *arg;
1716 *arg = NUL;
1717 list_one_var_a((char_u *)"",
1718 arg == arg_subsc ? name : name_start,
1719 tv.v_type, s == NULL ? (char_u *)"" : s);
1720 *arg = c;
1721 vim_free(tf);
1722 }
1723 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725 }
1726 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001727
1728 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001730
1731 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 }
1733
1734 return arg;
1735}
1736
1737/*
1738 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1739 * Returns a pointer to the char just after the var name.
1740 * Returns NULL if there is an error.
1741 */
1742 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001745 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749{
1750 int c1;
1751 char_u *name;
1752 char_u *p;
1753 char_u *arg_end = NULL;
1754 int len;
1755 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757
1758 /*
1759 * ":let $VAR = expr": Set environment variable.
1760 */
1761 if (*arg == '$')
1762 {
1763 /* Find the end of the name. */
1764 ++arg;
1765 name = arg;
1766 len = get_env_len(&arg);
1767 if (len == 0)
1768 EMSG2(_(e_invarg2), name - 1);
1769 else
1770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 if (op != NULL && (*op == '+' || *op == '-'))
1772 EMSG2(_(e_letwrong), op);
1773 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 EMSG(_(e_letunexp));
1776 else
1777 {
1778 c1 = name[len];
1779 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001780 p = get_tv_string_chk(tv);
1781 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 {
1783 int mustfree = FALSE;
1784 char_u *s = vim_getenv(name, &mustfree);
1785
1786 if (s != NULL)
1787 {
1788 p = tofree = concat_str(s, p);
1789 if (mustfree)
1790 vim_free(s);
1791 }
1792 }
1793 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001794 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001795 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001796 if (STRICMP(name, "HOME") == 0)
1797 init_homedir();
1798 else if (didset_vim && STRICMP(name, "VIM") == 0)
1799 didset_vim = FALSE;
1800 else if (didset_vimruntime
1801 && STRICMP(name, "VIMRUNTIME") == 0)
1802 didset_vimruntime = FALSE;
1803 arg_end = arg;
1804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 }
1808 }
1809 }
1810
1811 /*
1812 * ":let &option = expr": Set option value.
1813 * ":let &l:option = expr": Set local option value.
1814 * ":let &g:option = expr": Set global option value.
1815 */
1816 else if (*arg == '&')
1817 {
1818 /* Find the end of the name. */
1819 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 if (p == NULL || (endchars != NULL
1821 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 EMSG(_(e_letunexp));
1823 else
1824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 long n;
1826 int opt_type;
1827 long numval;
1828 char_u *stringval = NULL;
1829 char_u *s;
1830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 c1 = *p;
1832 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833
1834 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001835 s = get_tv_string_chk(tv); /* != NULL if number or string */
1836 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 {
1838 opt_type = get_option_value(arg, &numval,
1839 &stringval, opt_flags);
1840 if ((opt_type == 1 && *op == '.')
1841 || (opt_type == 0 && *op != '.'))
1842 EMSG2(_(e_letwrong), op);
1843 else
1844 {
1845 if (opt_type == 1) /* number */
1846 {
1847 if (*op == '+')
1848 n = numval + n;
1849 else
1850 n = numval - n;
1851 }
1852 else if (opt_type == 0 && stringval != NULL) /* string */
1853 {
1854 s = concat_str(stringval, s);
1855 vim_free(stringval);
1856 stringval = s;
1857 }
1858 }
1859 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001860 if (s != NULL)
1861 {
1862 set_option_value(arg, n, s, opt_flags);
1863 arg_end = p;
1864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 }
1868 }
1869
1870 /*
1871 * ":let @r = expr": Set register contents.
1872 */
1873 else if (*arg == '@')
1874 {
1875 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 if (op != NULL && (*op == '+' || *op == '-'))
1877 EMSG2(_(e_letwrong), op);
1878 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 EMSG(_(e_letunexp));
1881 else
1882 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 char_u *tofree = NULL;
1884 char_u *s;
1885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001886 p = get_tv_string_chk(tv);
1887 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00001889 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 if (s != NULL)
1891 {
1892 p = tofree = concat_str(s, p);
1893 vim_free(s);
1894 }
1895 }
1896 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001897 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001899 arg_end = arg + 1;
1900 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 }
1903 }
1904
1905 /*
1906 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001909 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001913 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1917 EMSG(_(e_letunexp));
1918 else
1919 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 arg_end = p;
1922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 }
1926
1927 else
1928 EMSG2(_(e_invarg2), arg);
1929
1930 return arg_end;
1931}
1932
1933/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001934 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1935 */
1936 static int
1937check_changedtick(arg)
1938 char_u *arg;
1939{
1940 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1941 {
1942 EMSG2(_(e_readonlyvar), arg);
1943 return TRUE;
1944 }
1945 return FALSE;
1946}
1947
1948/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 * Get an lval: variable, Dict item or List item that can be assigned a value
1950 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1951 * "name.key", "name.key[expr]" etc.
1952 * Indexing only works if "name" is an existing List or Dictionary.
1953 * "name" points to the start of the name.
1954 * If "rettv" is not NULL it points to the value to be assigned.
1955 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1956 * wrong; must end in space or cmd separator.
1957 *
1958 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 */
1962 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001963get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 typval_T *rettv;
1966 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 int unlet;
1968 int skip;
1969 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001970 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 char_u *expr_start, *expr_end;
1974 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 dictitem_T *v;
1976 typval_T var1;
1977 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001979 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986
1987 if (skip)
1988 {
1989 /* When skipping just find the end of the name. */
1990 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001991 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 }
1993
1994 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001995 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 if (expr_start != NULL)
1997 {
1998 /* Don't expand the name when we already know there is an error. */
1999 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2000 && *p != '[' && *p != '.')
2001 {
2002 EMSG(_(e_trailing));
2003 return NULL;
2004 }
2005
2006 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2007 if (lp->ll_exp_name == NULL)
2008 {
2009 /* Report an invalid expression in braces, unless the
2010 * expression evaluation has been cancelled due to an
2011 * aborting error, an interrupt, or an exception. */
2012 if (!aborting() && !quiet)
2013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002014 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 EMSG2(_(e_invarg2), name);
2016 return NULL;
2017 }
2018 }
2019 lp->ll_name = lp->ll_exp_name;
2020 }
2021 else
2022 lp->ll_name = name;
2023
2024 /* Without [idx] or .key we are done. */
2025 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2026 return p;
2027
2028 cc = *p;
2029 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002030 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 if (v == NULL && !quiet)
2032 EMSG2(_(e_undefvar), lp->ll_name);
2033 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 if (v == NULL)
2035 return NULL;
2036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 /*
2038 * Loop until no more [idx] or .key is following.
2039 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002040 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2044 && !(lp->ll_tv->v_type == VAR_DICT
2045 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 if (!quiet)
2048 EMSG(_("E689: Can only index a List or Dictionary"));
2049 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 if (!quiet)
2054 EMSG(_("E708: [:] must come last"));
2055 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002057
Bram Moolenaar8c711452005-01-14 21:53:12 +00002058 len = -1;
2059 if (*p == '.')
2060 {
2061 key = p + 1;
2062 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2063 ;
2064 if (len == 0)
2065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 if (!quiet)
2067 EMSG(_(e_emptykey));
2068 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002069 }
2070 p = key + len;
2071 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002072 else
2073 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002075 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002076 if (*p == ':')
2077 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002078 else
2079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002080 empty1 = FALSE;
2081 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002083 if (get_tv_string_chk(&var1) == NULL)
2084 {
2085 /* not a number or string */
2086 clear_tv(&var1);
2087 return NULL;
2088 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 }
2090
2091 /* Optionally get the second index [ :expr]. */
2092 if (*p == ':')
2093 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002098 if (!empty1)
2099 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002101 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 if (rettv != NULL && (rettv->v_type != VAR_LIST
2103 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 if (!quiet)
2106 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 if (!empty1)
2108 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 }
2111 p = skipwhite(p + 1);
2112 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 else
2115 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2118 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 if (!empty1)
2120 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 if (get_tv_string_chk(&var2) == NULL)
2124 {
2125 /* not a number or string */
2126 if (!empty1)
2127 clear_tv(&var1);
2128 clear_tv(&var2);
2129 return NULL;
2130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002133 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002136
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002138 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 if (!quiet)
2140 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002141 if (!empty1)
2142 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 }
2147
2148 /* Skip to past ']'. */
2149 ++p;
2150 }
2151
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002153 {
2154 if (len == -1)
2155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002157 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 if (*key == NUL)
2159 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 if (!quiet)
2161 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002162 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002164 }
2165 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 lp->ll_list = NULL;
2167 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002168 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002171 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002176 if (len == -1)
2177 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002179 }
2180 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002182 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002184 if (len == -1)
2185 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 p = NULL;
2188 break;
2189 }
2190 if (len == -1)
2191 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 }
2194 else
2195 {
2196 /*
2197 * Get the number and item for the only or first index of the List.
2198 */
2199 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 else
2202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002203 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 clear_tv(&var1);
2205 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 lp->ll_list = lp->ll_tv->vval.v_list;
2208 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2209 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (!quiet)
2212 EMSGN(_(e_listidx), lp->ll_n1);
2213 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 }
2217
2218 /*
2219 * May need to find the item or absolute index for the second
2220 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 * When no index given: "lp->ll_empty2" is TRUE.
2222 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002229 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 if (ni == NULL)
2232 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 if (!quiet)
2234 EMSGN(_(e_listidx), lp->ll_n2);
2235 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002236 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002238 }
2239
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2241 if (lp->ll_n1 < 0)
2242 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2243 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002244 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 if (!quiet)
2246 EMSGN(_(e_listidx), lp->ll_n2);
2247 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002248 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002249 }
2250
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 }
2254
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 return p;
2256}
2257
2258/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002259 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 */
2261 static void
2262clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002263 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264{
2265 vim_free(lp->ll_exp_name);
2266 vim_free(lp->ll_newkey);
2267}
2268
2269/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002270 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 */
2274 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002278 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281{
2282 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002283 listitem_T *ni;
2284 listitem_T *ri;
2285 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286
2287 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 cc = *endp;
2292 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 if (op != NULL && *op != '=')
2294 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002295 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002297 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2299 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 {
2301 if (tv_op(&tv, rettv, op) == OK)
2302 set_var(lp->ll_name, &tv, FALSE);
2303 clear_tv(&tv);
2304 }
2305 }
2306 else
2307 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002311 else if (tv_check_lock(lp->ll_newkey == NULL
2312 ? lp->ll_tv->v_lock
2313 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2314 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 else if (lp->ll_range)
2316 {
2317 /*
2318 * Assign the List values to the list items.
2319 */
2320 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002321 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 if (op != NULL && *op != '=')
2323 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2324 else
2325 {
2326 clear_tv(&lp->ll_li->li_tv);
2327 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2328 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 ri = ri->li_next;
2330 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2331 break;
2332 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002333 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 /* Need to add an empty item. */
2335 ni = listitem_alloc();
2336 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 ri = NULL;
2339 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002342 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 ni->li_tv.vval.v_number = 0;
2344 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 lp->ll_li = lp->ll_li->li_next;
2347 ++lp->ll_n1;
2348 }
2349 if (ri != NULL)
2350 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 else if (lp->ll_empty2
2352 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 : lp->ll_n1 != lp->ll_n2)
2354 EMSG(_("E711: List value has not enough items"));
2355 }
2356 else
2357 {
2358 /*
2359 * Assign to a List or Dictionary item.
2360 */
2361 if (lp->ll_newkey != NULL)
2362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 if (op != NULL && *op != '=')
2364 {
2365 EMSG2(_(e_letwrong), op);
2366 return;
2367 }
2368
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002370 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 if (di == NULL)
2372 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002373 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2374 {
2375 vim_free(di);
2376 return;
2377 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002379 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 else if (op != NULL && *op != '=')
2381 {
2382 tv_op(lp->ll_tv, rettv, op);
2383 return;
2384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 /*
2389 * Assign the value to the variable or list item.
2390 */
2391 if (copy)
2392 copy_tv(rettv, lp->ll_tv);
2393 else
2394 {
2395 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002396 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398 }
2399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400}
2401
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2404 * Returns OK or FAIL.
2405 */
2406 static int
2407tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002408 typval_T *tv1;
2409 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 char_u *op;
2411{
2412 long n;
2413 char_u numbuf[NUMBUFLEN];
2414 char_u *s;
2415
2416 /* Can't do anything with a Funcref or a Dict on the right. */
2417 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2418 {
2419 switch (tv1->v_type)
2420 {
2421 case VAR_DICT:
2422 case VAR_FUNC:
2423 break;
2424
2425 case VAR_LIST:
2426 if (*op != '+' || tv2->v_type != VAR_LIST)
2427 break;
2428 /* List += List */
2429 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2430 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2431 return OK;
2432
2433 case VAR_NUMBER:
2434 case VAR_STRING:
2435 if (tv2->v_type == VAR_LIST)
2436 break;
2437 if (*op == '+' || *op == '-')
2438 {
2439 /* nr += nr or nr -= nr*/
2440 n = get_tv_number(tv1);
2441 if (*op == '+')
2442 n += get_tv_number(tv2);
2443 else
2444 n -= get_tv_number(tv2);
2445 clear_tv(tv1);
2446 tv1->v_type = VAR_NUMBER;
2447 tv1->vval.v_number = n;
2448 }
2449 else
2450 {
2451 /* str .= str */
2452 s = get_tv_string(tv1);
2453 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2454 clear_tv(tv1);
2455 tv1->v_type = VAR_STRING;
2456 tv1->vval.v_string = s;
2457 }
2458 return OK;
2459 }
2460 }
2461
2462 EMSG2(_(e_letwrong), op);
2463 return FAIL;
2464}
2465
2466/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 * Add a watcher to a list.
2468 */
2469 static void
2470list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 list_T *l;
2472 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002473{
2474 lw->lw_next = l->lv_watch;
2475 l->lv_watch = lw;
2476}
2477
2478/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002479 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 * No warning when it isn't found...
2481 */
2482 static void
2483list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002484 list_T *l;
2485 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002486{
Bram Moolenaar33570922005-01-25 22:26:29 +00002487 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488
2489 lwp = &l->lv_watch;
2490 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2491 {
2492 if (lw == lwrem)
2493 {
2494 *lwp = lw->lw_next;
2495 break;
2496 }
2497 lwp = &lw->lw_next;
2498 }
2499}
2500
2501/*
2502 * Just before removing an item from a list: advance watchers to the next
2503 * item.
2504 */
2505 static void
2506list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 list_T *l;
2508 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509{
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002511
2512 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2513 if (lw->lw_item == item)
2514 lw->lw_item = item->li_next;
2515}
2516
2517/*
2518 * Evaluate the expression used in a ":for var in expr" command.
2519 * "arg" points to "var".
2520 * Set "*errp" to TRUE for an error, FALSE otherwise;
2521 * Return a pointer that holds the info. Null when there is an error.
2522 */
2523 void *
2524eval_for_line(arg, errp, nextcmdp, skip)
2525 char_u *arg;
2526 int *errp;
2527 char_u **nextcmdp;
2528 int skip;
2529{
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 typval_T tv;
2533 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534
2535 *errp = TRUE; /* default: there is an error */
2536
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002538 if (fi == NULL)
2539 return NULL;
2540
2541 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2542 if (expr == NULL)
2543 return fi;
2544
2545 expr = skipwhite(expr);
2546 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2547 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002548 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 return fi;
2550 }
2551
2552 if (skip)
2553 ++emsg_skip;
2554 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2555 {
2556 *errp = FALSE;
2557 if (!skip)
2558 {
2559 l = tv.vval.v_list;
2560 if (tv.v_type != VAR_LIST || l == NULL)
2561 EMSG(_(e_listreq));
2562 else
2563 {
2564 fi->fi_list = l;
2565 list_add_watch(l, &fi->fi_lw);
2566 fi->fi_lw.lw_item = l->lv_first;
2567 }
2568 }
2569 }
2570 if (skip)
2571 --emsg_skip;
2572
2573 return fi;
2574}
2575
2576/*
2577 * Use the first item in a ":for" list. Advance to the next.
2578 * Assign the values to the variable (list). "arg" points to the first one.
2579 * Return TRUE when a valid item was found, FALSE when at end of list or
2580 * something wrong.
2581 */
2582 int
2583next_for_item(fi_void, arg)
2584 void *fi_void;
2585 char_u *arg;
2586{
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002588 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002590
2591 item = fi->fi_lw.lw_item;
2592 if (item == NULL)
2593 result = FALSE;
2594 else
2595 {
2596 fi->fi_lw.lw_item = item->li_next;
2597 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2598 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2599 }
2600 return result;
2601}
2602
2603/*
2604 * Free the structure used to store info used by ":for".
2605 */
2606 void
2607free_for_info(fi_void)
2608 void *fi_void;
2609{
Bram Moolenaar33570922005-01-25 22:26:29 +00002610 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002611
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002612 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002613 list_rem_watch(fi->fi_list, &fi->fi_lw);
2614 vim_free(fi);
2615}
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2618
2619 void
2620set_context_for_expression(xp, arg, cmdidx)
2621 expand_T *xp;
2622 char_u *arg;
2623 cmdidx_T cmdidx;
2624{
2625 int got_eq = FALSE;
2626 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002627 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002629 if (cmdidx == CMD_let)
2630 {
2631 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002632 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002633 {
2634 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002635 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002636 {
2637 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002639 if (vim_iswhite(*p))
2640 break;
2641 }
2642 return;
2643 }
2644 }
2645 else
2646 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2647 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 while ((xp->xp_pattern = vim_strpbrk(arg,
2649 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2650 {
2651 c = *xp->xp_pattern;
2652 if (c == '&')
2653 {
2654 c = xp->xp_pattern[1];
2655 if (c == '&')
2656 {
2657 ++xp->xp_pattern;
2658 xp->xp_context = cmdidx != CMD_let || got_eq
2659 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2660 }
2661 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002664 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2665 xp->xp_pattern += 2;
2666
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 else if (c == '$')
2670 {
2671 /* environment variable */
2672 xp->xp_context = EXPAND_ENV_VARS;
2673 }
2674 else if (c == '=')
2675 {
2676 got_eq = TRUE;
2677 xp->xp_context = EXPAND_EXPRESSION;
2678 }
2679 else if (c == '<'
2680 && xp->xp_context == EXPAND_FUNCTIONS
2681 && vim_strchr(xp->xp_pattern, '(') == NULL)
2682 {
2683 /* Function name can start with "<SNR>" */
2684 break;
2685 }
2686 else if (cmdidx != CMD_let || got_eq)
2687 {
2688 if (c == '"') /* string */
2689 {
2690 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2691 if (c == '\\' && xp->xp_pattern[1] != NUL)
2692 ++xp->xp_pattern;
2693 xp->xp_context = EXPAND_NOTHING;
2694 }
2695 else if (c == '\'') /* literal string */
2696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2699 /* skip */ ;
2700 xp->xp_context = EXPAND_NOTHING;
2701 }
2702 else if (c == '|')
2703 {
2704 if (xp->xp_pattern[1] == '|')
2705 {
2706 ++xp->xp_pattern;
2707 xp->xp_context = EXPAND_EXPRESSION;
2708 }
2709 else
2710 xp->xp_context = EXPAND_COMMANDS;
2711 }
2712 else
2713 xp->xp_context = EXPAND_EXPRESSION;
2714 }
2715 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002716 /* Doesn't look like something valid, expand as an expression
2717 * anyway. */
2718 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 arg = xp->xp_pattern;
2720 if (*arg != NUL)
2721 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2722 /* skip */ ;
2723 }
2724 xp->xp_pattern = arg;
2725}
2726
2727#endif /* FEAT_CMDL_COMPL */
2728
2729/*
2730 * ":1,25call func(arg1, arg2)" function call.
2731 */
2732 void
2733ex_call(eap)
2734 exarg_T *eap;
2735{
2736 char_u *arg = eap->arg;
2737 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002739 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002741 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 linenr_T lnum;
2743 int doesrange;
2744 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002745 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002747 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2748 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002749 if (tofree == NULL)
2750 return;
2751
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002752 /* Increase refcount on dictionary, it could get deleted when evaluating
2753 * the arguments. */
2754 if (fudi.fd_dict != NULL)
2755 ++fudi.fd_dict->dv_refcount;
2756
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002757 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2758 len = STRLEN(tofree);
2759 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar532c7802005-01-27 14:44:31 +00002761 /* Skip white space to allow ":call func ()". Not good, but required for
2762 * backward compatibility. */
2763 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002764 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 if (*startarg != '(')
2767 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002768 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 goto end;
2770 }
2771
2772 /*
2773 * When skipping, evaluate the function once, to find the end of the
2774 * arguments.
2775 * When the function takes a range, this is discovered after the first
2776 * call, and the loop is broken.
2777 */
2778 if (eap->skip)
2779 {
2780 ++emsg_skip;
2781 lnum = eap->line2; /* do it once, also with an invalid range */
2782 }
2783 else
2784 lnum = eap->line1;
2785 for ( ; lnum <= eap->line2; ++lnum)
2786 {
2787 if (!eap->skip && eap->addr_count > 0)
2788 {
2789 curwin->w_cursor.lnum = lnum;
2790 curwin->w_cursor.col = 0;
2791 }
2792 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002794 eap->line1, eap->line2, &doesrange,
2795 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
2797 failed = TRUE;
2798 break;
2799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (doesrange || eap->skip)
2802 break;
2803 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002804 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002805 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002806 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (aborting())
2808 break;
2809 }
2810 if (eap->skip)
2811 --emsg_skip;
2812
2813 if (!failed)
2814 {
2815 /* Check for trailing illegal characters and a following command. */
2816 if (!ends_excmd(*arg))
2817 {
2818 emsg_severe = TRUE;
2819 EMSG(_(e_trailing));
2820 }
2821 else
2822 eap->nextcmd = check_nextcmd(arg);
2823 }
2824
2825end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002826 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002827 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828}
2829
2830/*
2831 * ":unlet[!] var1 ... " command.
2832 */
2833 void
2834ex_unlet(eap)
2835 exarg_T *eap;
2836{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002837 ex_unletlock(eap, eap->arg, 0);
2838}
2839
2840/*
2841 * ":lockvar" and ":unlockvar" commands
2842 */
2843 void
2844ex_lockvar(eap)
2845 exarg_T *eap;
2846{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 int deep = 2;
2849
2850 if (eap->forceit)
2851 deep = -1;
2852 else if (vim_isdigit(*arg))
2853 {
2854 deep = getdigits(&arg);
2855 arg = skipwhite(arg);
2856 }
2857
2858 ex_unletlock(eap, arg, deep);
2859}
2860
2861/*
2862 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2863 */
2864 static void
2865ex_unletlock(eap, argstart, deep)
2866 exarg_T *eap;
2867 char_u *argstart;
2868 int deep;
2869{
2870 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002873 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875 do
2876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002878 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
2879 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 if (lv.ll_name == NULL)
2881 error = TRUE; /* error but continue parsing */
2882 if (name_end == NULL || (!vim_iswhite(*name_end)
2883 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (name_end != NULL)
2886 {
2887 emsg_severe = TRUE;
2888 EMSG(_(e_trailing));
2889 }
2890 if (!(eap->skip || error))
2891 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 break;
2893 }
2894
2895 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 {
2897 if (eap->cmdidx == CMD_unlet)
2898 {
2899 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2900 error = TRUE;
2901 }
2902 else
2903 {
2904 if (do_lock_var(&lv, name_end, deep,
2905 eap->cmdidx == CMD_lockvar) == FAIL)
2906 error = TRUE;
2907 }
2908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (!eap->skip)
2911 clear_lval(&lv);
2912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 arg = skipwhite(name_end);
2914 } while (!ends_excmd(*arg));
2915
2916 eap->nextcmd = check_nextcmd(arg);
2917}
2918
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 int forceit;
2924{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 int ret = OK;
2926 int cc;
2927
2928 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 cc = *name_end;
2931 *name_end = NUL;
2932
2933 /* Normal name or expanded name. */
2934 if (check_changedtick(lp->ll_name))
2935 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002939 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2941 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 else if (lp->ll_range)
2943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945
2946 /* Delete a range of List items. */
2947 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2948 {
2949 li = lp->ll_li->li_next;
2950 listitem_remove(lp->ll_list, lp->ll_li);
2951 lp->ll_li = li;
2952 ++lp->ll_n1;
2953 }
2954 }
2955 else
2956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 /* unlet a List item. */
2959 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002962 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 }
2964
2965 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966}
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968/*
2969 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002970 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
2972 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 hashtab_T *ht;
2978 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002979 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 ht = find_var_ht(name, &varname);
2982 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 hi = hash_find(ht, varname);
2985 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002986 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002987 if (var_check_ro(HI2DI(hi)->di_flags, name))
2988 return FAIL;
2989 delete_var(ht, hi);
2990 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002993 if (forceit)
2994 return OK;
2995 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 return FAIL;
2997}
2998
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002999/*
3000 * Lock or unlock variable indicated by "lp".
3001 * "deep" is the levels to go (-1 for unlimited);
3002 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3003 */
3004 static int
3005do_lock_var(lp, name_end, deep, lock)
3006 lval_T *lp;
3007 char_u *name_end;
3008 int deep;
3009 int lock;
3010{
3011 int ret = OK;
3012 int cc;
3013 dictitem_T *di;
3014
3015 if (deep == 0) /* nothing to do */
3016 return OK;
3017
3018 if (lp->ll_tv == NULL)
3019 {
3020 cc = *name_end;
3021 *name_end = NUL;
3022
3023 /* Normal name or expanded name. */
3024 if (check_changedtick(lp->ll_name))
3025 ret = FAIL;
3026 else
3027 {
3028 di = find_var(lp->ll_name, NULL);
3029 if (di == NULL)
3030 ret = FAIL;
3031 else
3032 {
3033 if (lock)
3034 di->di_flags |= DI_FLAGS_LOCK;
3035 else
3036 di->di_flags &= ~DI_FLAGS_LOCK;
3037 item_lock(&di->di_tv, deep, lock);
3038 }
3039 }
3040 *name_end = cc;
3041 }
3042 else if (lp->ll_range)
3043 {
3044 listitem_T *li = lp->ll_li;
3045
3046 /* (un)lock a range of List items. */
3047 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3048 {
3049 item_lock(&li->li_tv, deep, lock);
3050 li = li->li_next;
3051 ++lp->ll_n1;
3052 }
3053 }
3054 else if (lp->ll_list != NULL)
3055 /* (un)lock a List item. */
3056 item_lock(&lp->ll_li->li_tv, deep, lock);
3057 else
3058 /* un(lock) a Dictionary item. */
3059 item_lock(&lp->ll_di->di_tv, deep, lock);
3060
3061 return ret;
3062}
3063
3064/*
3065 * Lock or unlock an item. "deep" is nr of levels to go.
3066 */
3067 static void
3068item_lock(tv, deep, lock)
3069 typval_T *tv;
3070 int deep;
3071 int lock;
3072{
3073 static int recurse = 0;
3074 list_T *l;
3075 listitem_T *li;
3076 dict_T *d;
3077 hashitem_T *hi;
3078 int todo;
3079
3080 if (recurse >= DICT_MAXNEST)
3081 {
3082 EMSG(_("E743: variable nested too deep for (un)lock"));
3083 return;
3084 }
3085 if (deep == 0)
3086 return;
3087 ++recurse;
3088
3089 /* lock/unlock the item itself */
3090 if (lock)
3091 tv->v_lock |= VAR_LOCKED;
3092 else
3093 tv->v_lock &= ~VAR_LOCKED;
3094
3095 switch (tv->v_type)
3096 {
3097 case VAR_LIST:
3098 if ((l = tv->vval.v_list) != NULL)
3099 {
3100 if (lock)
3101 l->lv_lock |= VAR_LOCKED;
3102 else
3103 l->lv_lock &= ~VAR_LOCKED;
3104 if (deep < 0 || deep > 1)
3105 /* recursive: lock/unlock the items the List contains */
3106 for (li = l->lv_first; li != NULL; li = li->li_next)
3107 item_lock(&li->li_tv, deep - 1, lock);
3108 }
3109 break;
3110 case VAR_DICT:
3111 if ((d = tv->vval.v_dict) != NULL)
3112 {
3113 if (lock)
3114 d->dv_lock |= VAR_LOCKED;
3115 else
3116 d->dv_lock &= ~VAR_LOCKED;
3117 if (deep < 0 || deep > 1)
3118 {
3119 /* recursive: lock/unlock the items the List contains */
3120 todo = d->dv_hashtab.ht_used;
3121 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3122 {
3123 if (!HASHITEM_EMPTY(hi))
3124 {
3125 --todo;
3126 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3127 }
3128 }
3129 }
3130 }
3131 }
3132 --recurse;
3133}
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3136/*
3137 * Delete all "menutrans_" variables.
3138 */
3139 void
3140del_menutrans_vars()
3141{
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003143 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 hash_lock(&globvarht);
3146 todo = globvarht.ht_used;
3147 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003148 {
3149 if (!HASHITEM_EMPTY(hi))
3150 {
3151 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3153 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003154 }
3155 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157}
3158#endif
3159
3160#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3161
3162/*
3163 * Local string buffer for the next two functions to store a variable name
3164 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3165 * get_user_var_name().
3166 */
3167
3168static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3169
3170static char_u *varnamebuf = NULL;
3171static int varnamebuflen = 0;
3172
3173/*
3174 * Function to concatenate a prefix and a variable name.
3175 */
3176 static char_u *
3177cat_prefix_varname(prefix, name)
3178 int prefix;
3179 char_u *name;
3180{
3181 int len;
3182
3183 len = (int)STRLEN(name) + 3;
3184 if (len > varnamebuflen)
3185 {
3186 vim_free(varnamebuf);
3187 len += 10; /* some additional space */
3188 varnamebuf = alloc(len);
3189 if (varnamebuf == NULL)
3190 {
3191 varnamebuflen = 0;
3192 return NULL;
3193 }
3194 varnamebuflen = len;
3195 }
3196 *varnamebuf = prefix;
3197 varnamebuf[1] = ':';
3198 STRCPY(varnamebuf + 2, name);
3199 return varnamebuf;
3200}
3201
3202/*
3203 * Function given to ExpandGeneric() to obtain the list of user defined
3204 * (global/buffer/window/built-in) variable names.
3205 */
3206/*ARGSUSED*/
3207 char_u *
3208get_user_var_name(xp, idx)
3209 expand_T *xp;
3210 int idx;
3211{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003212 static long_u gdone;
3213 static long_u bdone;
3214 static long_u wdone;
3215 static int vidx;
3216 static hashitem_T *hi;
3217 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003220 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003221
3222 /* Global variables */
3223 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003225 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003227 else
3228 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003229 while (HASHITEM_EMPTY(hi))
3230 ++hi;
3231 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3232 return cat_prefix_varname('g', hi->hi_key);
3233 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003235
3236 /* b: variables */
3237 ht = &curbuf->b_vars.dv_hashtab;
3238 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003240 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003242 else
3243 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003244 while (HASHITEM_EMPTY(hi))
3245 ++hi;
3246 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003250 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 return (char_u *)"b:changedtick";
3252 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003253
3254 /* w: variables */
3255 ht = &curwin->w_vars.dv_hashtab;
3256 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003258 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003260 else
3261 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003262 while (HASHITEM_EMPTY(hi))
3263 ++hi;
3264 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003266
3267 /* v: variables */
3268 if (vidx < VV_LEN)
3269 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 vim_free(varnamebuf);
3272 varnamebuf = NULL;
3273 varnamebuflen = 0;
3274 return NULL;
3275}
3276
3277#endif /* FEAT_CMDL_COMPL */
3278
3279/*
3280 * types for expressions.
3281 */
3282typedef enum
3283{
3284 TYPE_UNKNOWN = 0
3285 , TYPE_EQUAL /* == */
3286 , TYPE_NEQUAL /* != */
3287 , TYPE_GREATER /* > */
3288 , TYPE_GEQUAL /* >= */
3289 , TYPE_SMALLER /* < */
3290 , TYPE_SEQUAL /* <= */
3291 , TYPE_MATCH /* =~ */
3292 , TYPE_NOMATCH /* !~ */
3293} exptype_T;
3294
3295/*
3296 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3299 */
3300
3301/*
3302 * Handle zero level expression.
3303 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003304 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 * Return OK or FAIL.
3306 */
3307 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003308eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 char_u **nextcmd;
3312 int evaluate;
3313{
3314 int ret;
3315 char_u *p;
3316
3317 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003318 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (ret == FAIL || !ends_excmd(*p))
3320 {
3321 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 /*
3324 * Report the invalid expression unless the expression evaluation has
3325 * been cancelled due to an aborting error, an interrupt, or an
3326 * exception.
3327 */
3328 if (!aborting())
3329 EMSG2(_(e_invexpr2), arg);
3330 ret = FAIL;
3331 }
3332 if (nextcmd != NULL)
3333 *nextcmd = check_nextcmd(p);
3334
3335 return ret;
3336}
3337
3338/*
3339 * Handle top level expression:
3340 * expr1 ? expr0 : expr0
3341 *
3342 * "arg" must point to the first non-white of the expression.
3343 * "arg" is advanced to the next non-white after the recognized expression.
3344 *
3345 * Return OK or FAIL.
3346 */
3347 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 int evaluate;
3352{
3353 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 /*
3357 * Get the first variable.
3358 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 return FAIL;
3361
3362 if ((*arg)[0] == '?')
3363 {
3364 result = FALSE;
3365 if (evaluate)
3366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 int error = FALSE;
3368
3369 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003371 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003372 if (error)
3373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375
3376 /*
3377 * Get the second variable.
3378 */
3379 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 return FAIL;
3382
3383 /*
3384 * Check for the ":".
3385 */
3386 if ((*arg)[0] != ':')
3387 {
3388 EMSG(_("E109: Missing ':' after '?'"));
3389 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 return FAIL;
3392 }
3393
3394 /*
3395 * Get the third variable.
3396 */
3397 *arg = skipwhite(*arg + 1);
3398 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3399 {
3400 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return FAIL;
3403 }
3404 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407
3408 return OK;
3409}
3410
3411/*
3412 * Handle first level expression:
3413 * expr2 || expr2 || expr2 logical OR
3414 *
3415 * "arg" must point to the first non-white of the expression.
3416 * "arg" is advanced to the next non-white after the recognized expression.
3417 *
3418 * Return OK or FAIL.
3419 */
3420 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 int evaluate;
3425{
Bram Moolenaar33570922005-01-25 22:26:29 +00003426 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 long result;
3428 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 /*
3432 * Get the first variable.
3433 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003434 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 return FAIL;
3436
3437 /*
3438 * Repeat until there is no following "||".
3439 */
3440 first = TRUE;
3441 result = FALSE;
3442 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3443 {
3444 if (evaluate && first)
3445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003448 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003449 if (error)
3450 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 first = FALSE;
3452 }
3453
3454 /*
3455 * Get the second variable.
3456 */
3457 *arg = skipwhite(*arg + 2);
3458 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3459 return FAIL;
3460
3461 /*
3462 * Compute the result.
3463 */
3464 if (evaluate && !result)
3465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003466 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003468 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003469 if (error)
3470 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472 if (evaluate)
3473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003474 rettv->v_type = VAR_NUMBER;
3475 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477 }
3478
3479 return OK;
3480}
3481
3482/*
3483 * Handle second level expression:
3484 * expr3 && expr3 && expr3 logical AND
3485 *
3486 * "arg" must point to the first non-white of the expression.
3487 * "arg" is advanced to the next non-white after the recognized expression.
3488 *
3489 * Return OK or FAIL.
3490 */
3491 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 int evaluate;
3496{
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 long result;
3499 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003500 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 /*
3503 * Get the first variable.
3504 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003505 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 return FAIL;
3507
3508 /*
3509 * Repeat until there is no following "&&".
3510 */
3511 first = TRUE;
3512 result = TRUE;
3513 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3514 {
3515 if (evaluate && first)
3516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520 if (error)
3521 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 first = FALSE;
3523 }
3524
3525 /*
3526 * Get the second variable.
3527 */
3528 *arg = skipwhite(*arg + 2);
3529 if (eval4(arg, &var2, evaluate && result) == FAIL)
3530 return FAIL;
3531
3532 /*
3533 * Compute the result.
3534 */
3535 if (evaluate && result)
3536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003537 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003540 if (error)
3541 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543 if (evaluate)
3544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 rettv->v_type = VAR_NUMBER;
3546 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548 }
3549
3550 return OK;
3551}
3552
3553/*
3554 * Handle third level expression:
3555 * var1 == var2
3556 * var1 =~ var2
3557 * var1 != var2
3558 * var1 !~ var2
3559 * var1 > var2
3560 * var1 >= var2
3561 * var1 < var2
3562 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003563 * var1 is var2
3564 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 *
3566 * "arg" must point to the first non-white of the expression.
3567 * "arg" is advanced to the next non-white after the recognized expression.
3568 *
3569 * Return OK or FAIL.
3570 */
3571 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003572eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 int evaluate;
3576{
Bram Moolenaar33570922005-01-25 22:26:29 +00003577 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 char_u *p;
3579 int i;
3580 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003581 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 int len = 2;
3583 long n1, n2;
3584 char_u *s1, *s2;
3585 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3586 regmatch_T regmatch;
3587 int ic;
3588 char_u *save_cpo;
3589
3590 /*
3591 * Get the first variable.
3592 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003593 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 return FAIL;
3595
3596 p = *arg;
3597 switch (p[0])
3598 {
3599 case '=': if (p[1] == '=')
3600 type = TYPE_EQUAL;
3601 else if (p[1] == '~')
3602 type = TYPE_MATCH;
3603 break;
3604 case '!': if (p[1] == '=')
3605 type = TYPE_NEQUAL;
3606 else if (p[1] == '~')
3607 type = TYPE_NOMATCH;
3608 break;
3609 case '>': if (p[1] != '=')
3610 {
3611 type = TYPE_GREATER;
3612 len = 1;
3613 }
3614 else
3615 type = TYPE_GEQUAL;
3616 break;
3617 case '<': if (p[1] != '=')
3618 {
3619 type = TYPE_SMALLER;
3620 len = 1;
3621 }
3622 else
3623 type = TYPE_SEQUAL;
3624 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003625 case 'i': if (p[1] == 's')
3626 {
3627 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3628 len = 5;
3629 if (!vim_isIDc(p[len]))
3630 {
3631 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3632 type_is = TRUE;
3633 }
3634 }
3635 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
3638 /*
3639 * If there is a comparitive operator, use it.
3640 */
3641 if (type != TYPE_UNKNOWN)
3642 {
3643 /* extra question mark appended: ignore case */
3644 if (p[len] == '?')
3645 {
3646 ic = TRUE;
3647 ++len;
3648 }
3649 /* extra '#' appended: match case */
3650 else if (p[len] == '#')
3651 {
3652 ic = FALSE;
3653 ++len;
3654 }
3655 /* nothing appened: use 'ignorecase' */
3656 else
3657 ic = p_ic;
3658
3659 /*
3660 * Get the second variable.
3661 */
3662 *arg = skipwhite(p + len);
3663 if (eval5(arg, &var2, evaluate) == FAIL)
3664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 return FAIL;
3667 }
3668
3669 if (evaluate)
3670 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003671 if (type_is && rettv->v_type != var2.v_type)
3672 {
3673 /* For "is" a different type always means FALSE, for "notis"
3674 * it means TRUE. */
3675 n1 = (type == TYPE_NEQUAL);
3676 }
3677 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3678 {
3679 if (type_is)
3680 {
3681 n1 = (rettv->v_type == var2.v_type
3682 && rettv->vval.v_list == var2.vval.v_list);
3683 if (type == TYPE_NEQUAL)
3684 n1 = !n1;
3685 }
3686 else if (rettv->v_type != var2.v_type
3687 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3688 {
3689 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003690 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003691 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003692 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003693 clear_tv(rettv);
3694 clear_tv(&var2);
3695 return FAIL;
3696 }
3697 else
3698 {
3699 /* Compare two Lists for being equal or unequal. */
3700 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3701 if (type == TYPE_NEQUAL)
3702 n1 = !n1;
3703 }
3704 }
3705
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003706 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3707 {
3708 if (type_is)
3709 {
3710 n1 = (rettv->v_type == var2.v_type
3711 && rettv->vval.v_dict == var2.vval.v_dict);
3712 if (type == TYPE_NEQUAL)
3713 n1 = !n1;
3714 }
3715 else if (rettv->v_type != var2.v_type
3716 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3717 {
3718 if (rettv->v_type != var2.v_type)
3719 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3720 else
3721 EMSG(_("E736: Invalid operation for Dictionary"));
3722 clear_tv(rettv);
3723 clear_tv(&var2);
3724 return FAIL;
3725 }
3726 else
3727 {
3728 /* Compare two Dictionaries for being equal or unequal. */
3729 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3730 if (type == TYPE_NEQUAL)
3731 n1 = !n1;
3732 }
3733 }
3734
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003735 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3736 {
3737 if (rettv->v_type != var2.v_type
3738 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3739 {
3740 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003741 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003742 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003743 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003744 clear_tv(rettv);
3745 clear_tv(&var2);
3746 return FAIL;
3747 }
3748 else
3749 {
3750 /* Compare two Funcrefs for being equal or unequal. */
3751 if (rettv->vval.v_string == NULL
3752 || var2.vval.v_string == NULL)
3753 n1 = FALSE;
3754 else
3755 n1 = STRCMP(rettv->vval.v_string,
3756 var2.vval.v_string) == 0;
3757 if (type == TYPE_NEQUAL)
3758 n1 = !n1;
3759 }
3760 }
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 /*
3763 * If one of the two variables is a number, compare as a number.
3764 * When using "=~" or "!~", always compare as string.
3765 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003766 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 n1 = get_tv_number(rettv);
3770 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 switch (type)
3772 {
3773 case TYPE_EQUAL: n1 = (n1 == n2); break;
3774 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3775 case TYPE_GREATER: n1 = (n1 > n2); break;
3776 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3777 case TYPE_SMALLER: n1 = (n1 < n2); break;
3778 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3779 case TYPE_UNKNOWN:
3780 case TYPE_MATCH:
3781 case TYPE_NOMATCH: break; /* avoid gcc warning */
3782 }
3783 }
3784 else
3785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786 s1 = get_tv_string_buf(rettv, buf1);
3787 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3789 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3790 else
3791 i = 0;
3792 n1 = FALSE;
3793 switch (type)
3794 {
3795 case TYPE_EQUAL: n1 = (i == 0); break;
3796 case TYPE_NEQUAL: n1 = (i != 0); break;
3797 case TYPE_GREATER: n1 = (i > 0); break;
3798 case TYPE_GEQUAL: n1 = (i >= 0); break;
3799 case TYPE_SMALLER: n1 = (i < 0); break;
3800 case TYPE_SEQUAL: n1 = (i <= 0); break;
3801
3802 case TYPE_MATCH:
3803 case TYPE_NOMATCH:
3804 /* avoid 'l' flag in 'cpoptions' */
3805 save_cpo = p_cpo;
3806 p_cpo = (char_u *)"";
3807 regmatch.regprog = vim_regcomp(s2,
3808 RE_MAGIC + RE_STRING);
3809 regmatch.rm_ic = ic;
3810 if (regmatch.regprog != NULL)
3811 {
3812 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3813 vim_free(regmatch.regprog);
3814 if (type == TYPE_NOMATCH)
3815 n1 = !n1;
3816 }
3817 p_cpo = save_cpo;
3818 break;
3819
3820 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3821 }
3822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 clear_tv(rettv);
3824 clear_tv(&var2);
3825 rettv->v_type = VAR_NUMBER;
3826 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829
3830 return OK;
3831}
3832
3833/*
3834 * Handle fourth level expression:
3835 * + number addition
3836 * - number subtraction
3837 * . string concatenation
3838 *
3839 * "arg" must point to the first non-white of the expression.
3840 * "arg" is advanced to the next non-white after the recognized expression.
3841 *
3842 * Return OK or FAIL.
3843 */
3844 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 int evaluate;
3849{
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 typval_T var2;
3851 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 int op;
3853 long n1, n2;
3854 char_u *s1, *s2;
3855 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3856 char_u *p;
3857
3858 /*
3859 * Get the first variable.
3860 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 return FAIL;
3863
3864 /*
3865 * Repeat computing, until no '+', '-' or '.' is following.
3866 */
3867 for (;;)
3868 {
3869 op = **arg;
3870 if (op != '+' && op != '-' && op != '.')
3871 break;
3872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 if (op != '+' || rettv->v_type != VAR_LIST)
3874 {
3875 /* For "list + ...", an illegal use of the first operand as
3876 * a number cannot be determined before evaluating the 2nd
3877 * operand: if this is also a list, all is ok.
3878 * For "something . ...", "something - ..." or "non-list + ...",
3879 * we know that the first operand needs to be a string or number
3880 * without evaluating the 2nd operand. So check before to avoid
3881 * side effects after an error. */
3882 if (evaluate && get_tv_string_chk(rettv) == NULL)
3883 {
3884 clear_tv(rettv);
3885 return FAIL;
3886 }
3887 }
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 /*
3890 * Get the second variable.
3891 */
3892 *arg = skipwhite(*arg + 1);
3893 if (eval6(arg, &var2, evaluate) == FAIL)
3894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003895 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return FAIL;
3897 }
3898
3899 if (evaluate)
3900 {
3901 /*
3902 * Compute the result.
3903 */
3904 if (op == '.')
3905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003906 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3907 s2 = get_tv_string_buf_chk(&var2, buf2);
3908 if (s2 == NULL) /* type error ? */
3909 {
3910 clear_tv(rettv);
3911 clear_tv(&var2);
3912 return FAIL;
3913 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003914 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915 clear_tv(rettv);
3916 rettv->v_type = VAR_STRING;
3917 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003919 else if (op == '+' && rettv->v_type == VAR_LIST
3920 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003921 {
3922 /* concatenate Lists */
3923 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3924 &var3) == FAIL)
3925 {
3926 clear_tv(rettv);
3927 clear_tv(&var2);
3928 return FAIL;
3929 }
3930 clear_tv(rettv);
3931 *rettv = var3;
3932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 else
3934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003935 int error = FALSE;
3936
3937 n1 = get_tv_number_chk(rettv, &error);
3938 if (error)
3939 {
3940 /* This can only happen for "list + non-list".
3941 * For "non-list + ..." or "something - ...", we returned
3942 * before evaluating the 2nd operand. */
3943 clear_tv(rettv);
3944 return FAIL;
3945 }
3946 n2 = get_tv_number_chk(&var2, &error);
3947 if (error)
3948 {
3949 clear_tv(rettv);
3950 clear_tv(&var2);
3951 return FAIL;
3952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (op == '+')
3955 n1 = n1 + n2;
3956 else
3957 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003958 rettv->v_type = VAR_NUMBER;
3959 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963 }
3964 return OK;
3965}
3966
3967/*
3968 * Handle fifth level expression:
3969 * * number multiplication
3970 * / number division
3971 * % number modulo
3972 *
3973 * "arg" must point to the first non-white of the expression.
3974 * "arg" is advanced to the next non-white after the recognized expression.
3975 *
3976 * Return OK or FAIL.
3977 */
3978 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 int evaluate;
3983{
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 int op;
3986 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 /*
3990 * Get the first variable.
3991 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003992 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 return FAIL;
3994
3995 /*
3996 * Repeat computing, until no '*', '/' or '%' is following.
3997 */
3998 for (;;)
3999 {
4000 op = **arg;
4001 if (op != '*' && op != '/' && op != '%')
4002 break;
4003
4004 if (evaluate)
4005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004008 if (error)
4009 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011 else
4012 n1 = 0;
4013
4014 /*
4015 * Get the second variable.
4016 */
4017 *arg = skipwhite(*arg + 1);
4018 if (eval7(arg, &var2, evaluate) == FAIL)
4019 return FAIL;
4020
4021 if (evaluate)
4022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004023 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 if (error)
4026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
4028 /*
4029 * Compute the result.
4030 */
4031 if (op == '*')
4032 n1 = n1 * n2;
4033 else if (op == '/')
4034 {
4035 if (n2 == 0) /* give an error message? */
4036 n1 = 0x7fffffffL;
4037 else
4038 n1 = n1 / n2;
4039 }
4040 else
4041 {
4042 if (n2 == 0) /* give an error message? */
4043 n1 = 0;
4044 else
4045 n1 = n1 % n2;
4046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 rettv->v_type = VAR_NUMBER;
4048 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 }
4051
4052 return OK;
4053}
4054
4055/*
4056 * Handle sixth level expression:
4057 * number number constant
4058 * "string" string contstant
4059 * 'string' literal string contstant
4060 * &option-name option value
4061 * @r register contents
4062 * identifier variable value
4063 * function() function call
4064 * $VAR environment variable
4065 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004066 * [expr, expr] List
4067 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 *
4069 * Also handle:
4070 * ! in front logical NOT
4071 * - in front unary minus
4072 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 * trailing [] subscript in String or List
4074 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 *
4076 * "arg" must point to the first non-white of the expression.
4077 * "arg" is advanced to the next non-white after the recognized expression.
4078 *
4079 * Return OK or FAIL.
4080 */
4081 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 int evaluate;
4086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 long n;
4088 int len;
4089 char_u *s;
4090 int val;
4091 char_u *start_leader, *end_leader;
4092 int ret = OK;
4093 char_u *alias;
4094
4095 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 /*
4102 * Skip '!' and '-' characters. They are handled later.
4103 */
4104 start_leader = *arg;
4105 while (**arg == '!' || **arg == '-' || **arg == '+')
4106 *arg = skipwhite(*arg + 1);
4107 end_leader = *arg;
4108
4109 switch (**arg)
4110 {
4111 /*
4112 * Number constant.
4113 */
4114 case '0':
4115 case '1':
4116 case '2':
4117 case '3':
4118 case '4':
4119 case '5':
4120 case '6':
4121 case '7':
4122 case '8':
4123 case '9':
4124 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4125 *arg += len;
4126 if (evaluate)
4127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 rettv->v_type = VAR_NUMBER;
4129 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 break;
4132
4133 /*
4134 * String constant: "string".
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 break;
4138
4139 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004140 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004143 break;
4144
4145 /*
4146 * List: [expr, expr]
4147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 break;
4150
4151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152 * Dictionary: {key: val, key: val}
4153 */
4154 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4155 break;
4156
4157 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004158 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004160 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162
4163 /*
4164 * Environment variable: $VAR.
4165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168
4169 /*
4170 * Register contents: @r.
4171 */
4172 case '@': ++*arg;
4173 if (evaluate)
4174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 rettv->v_type = VAR_STRING;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00004176 rettv->vval.v_string = get_reg_contents(**arg,
4177 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 if (**arg != NUL)
4180 ++*arg;
4181 break;
4182
4183 /*
4184 * nested expression: (expression).
4185 */
4186 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 if (**arg == ')')
4189 ++*arg;
4190 else if (ret == OK)
4191 {
4192 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 ret = FAIL;
4195 }
4196 break;
4197
Bram Moolenaar8c711452005-01-14 21:53:12 +00004198 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 break;
4200 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004201
4202 if (ret == NOTDONE)
4203 {
4204 /*
4205 * Must be a variable or function name.
4206 * Can also be a curly-braces kind of name: {expr}.
4207 */
4208 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004209 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 if (alias != NULL)
4211 s = alias;
4212
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004213 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004214 ret = FAIL;
4215 else
4216 {
4217 if (**arg == '(') /* recursive! */
4218 {
4219 /* If "s" is the name of a variable of type VAR_FUNC
4220 * use its contents. */
4221 s = deref_func_name(s, &len);
4222
4223 /* Invoke the function. */
4224 ret = get_func_tv(s, len, rettv, arg,
4225 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004226 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004227 /* Stop the expression evaluation when immediately
4228 * aborting on error, or when an interrupt occurred or
4229 * an exception was thrown but not caught. */
4230 if (aborting())
4231 {
4232 if (ret == OK)
4233 clear_tv(rettv);
4234 ret = FAIL;
4235 }
4236 }
4237 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004238 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004239 else
4240 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 }
4242
4243 if (alias != NULL)
4244 vim_free(alias);
4245 }
4246
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 *arg = skipwhite(*arg);
4248
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004249 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4250 * expr(expr). */
4251 if (ret == OK)
4252 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 /*
4255 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4256 */
4257 if (ret == OK && evaluate && end_leader > start_leader)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
4260
4261 val = get_tv_number_chk(rettv, &error);
4262 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 clear_tv(rettv);
4265 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 else
4268 {
4269 while (end_leader > start_leader)
4270 {
4271 --end_leader;
4272 if (*end_leader == '!')
4273 val = !val;
4274 else if (*end_leader == '-')
4275 val = -val;
4276 }
4277 clear_tv(rettv);
4278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = val;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 return ret;
4284}
4285
4286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 * Evaluate an "[expr]" or "[expr:expr]" index.
4288 * "*arg" points to the '['.
4289 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4290 */
4291 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004292eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004293 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004296 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004297{
4298 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004300 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301 long len = -1;
4302 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004303 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004305
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004308 if (verbose)
4309 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004310 return FAIL;
4311 }
4312
Bram Moolenaar8c711452005-01-14 21:53:12 +00004313 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 /*
4316 * dict.name
4317 */
4318 key = *arg + 1;
4319 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4320 ;
4321 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004322 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004324 }
4325 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004326 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004327 /*
4328 * something[idx]
4329 *
4330 * Get the (first) variable from inside the [].
4331 */
4332 *arg = skipwhite(*arg + 1);
4333 if (**arg == ':')
4334 empty1 = TRUE;
4335 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4336 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4338 {
4339 /* not a number or string */
4340 clear_tv(&var1);
4341 return FAIL;
4342 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343
4344 /*
4345 * Get the second variable from inside the [:].
4346 */
4347 if (**arg == ':')
4348 {
4349 range = TRUE;
4350 *arg = skipwhite(*arg + 1);
4351 if (**arg == ']')
4352 empty2 = TRUE;
4353 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (!empty1)
4356 clear_tv(&var1);
4357 return FAIL;
4358 }
4359 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4360 {
4361 /* not a number or string */
4362 if (!empty1)
4363 clear_tv(&var1);
4364 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004365 return FAIL;
4366 }
4367 }
4368
4369 /* Check for the ']'. */
4370 if (**arg != ']')
4371 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004372 if (verbose)
4373 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 clear_tv(&var1);
4375 if (range)
4376 clear_tv(&var2);
4377 return FAIL;
4378 }
4379 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004380 }
4381
4382 if (evaluate)
4383 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384 n1 = 0;
4385 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 n1 = get_tv_number(&var1);
4388 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 }
4390 if (range)
4391 {
4392 if (empty2)
4393 n2 = -1;
4394 else
4395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 n2 = get_tv_number(&var2);
4397 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004398 }
4399 }
4400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004402 {
4403 case VAR_NUMBER:
4404 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406 len = (long)STRLEN(s);
4407 if (range)
4408 {
4409 /* The resulting variable is a substring. If the indexes
4410 * are out of range the result is empty. */
4411 if (n1 < 0)
4412 {
4413 n1 = len + n1;
4414 if (n1 < 0)
4415 n1 = 0;
4416 }
4417 if (n2 < 0)
4418 n2 = len + n2;
4419 else if (n2 >= len)
4420 n2 = len;
4421 if (n1 >= len || n2 < 0 || n1 > n2)
4422 s = NULL;
4423 else
4424 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4425 }
4426 else
4427 {
4428 /* The resulting variable is a string of a single
4429 * character. If the index is too big or negative the
4430 * result is empty. */
4431 if (n1 >= len || n1 < 0)
4432 s = NULL;
4433 else
4434 s = vim_strnsave(s + n1, 1);
4435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 clear_tv(rettv);
4437 rettv->v_type = VAR_STRING;
4438 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439 break;
4440
4441 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443 if (n1 < 0)
4444 n1 = len + n1;
4445 if (!empty1 && (n1 < 0 || n1 >= len))
4446 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004447 if (verbose)
4448 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 return FAIL;
4450 }
4451 if (range)
4452 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004453 list_T *l;
4454 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455
4456 if (n2 < 0)
4457 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004460 if (verbose)
4461 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 return FAIL;
4463 }
4464 l = list_alloc();
4465 if (l == NULL)
4466 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 n1 <= n2; ++n1)
4469 {
4470 if (list_append_tv(l, &item->li_tv) == FAIL)
4471 {
4472 list_free(l);
4473 return FAIL;
4474 }
4475 item = item->li_next;
4476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 clear_tv(rettv);
4478 rettv->v_type = VAR_LIST;
4479 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004480 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 }
4482 else
4483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
4487 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488 }
4489 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490
4491 case VAR_DICT:
4492 if (range)
4493 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004494 if (verbose)
4495 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 if (len == -1)
4497 clear_tv(&var1);
4498 return FAIL;
4499 }
4500 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004501 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502
4503 if (len == -1)
4504 {
4505 key = get_tv_string(&var1);
4506 if (*key == NUL)
4507 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004508 if (verbose)
4509 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 clear_tv(&var1);
4511 return FAIL;
4512 }
4513 }
4514
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004515 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004517 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004519 if (len == -1)
4520 clear_tv(&var1);
4521 if (item == NULL)
4522 return FAIL;
4523
4524 copy_tv(&item->di_tv, &var1);
4525 clear_tv(rettv);
4526 *rettv = var1;
4527 }
4528 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 }
4530 }
4531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 return OK;
4533}
4534
4535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 * Get an option value.
4537 * "arg" points to the '&' or '+' before the option name.
4538 * "arg" is advanced to character after the option name.
4539 * Return OK or FAIL.
4540 */
4541 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004542get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004544 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 int evaluate;
4546{
4547 char_u *option_end;
4548 long numval;
4549 char_u *stringval;
4550 int opt_type;
4551 int c;
4552 int working = (**arg == '+'); /* has("+option") */
4553 int ret = OK;
4554 int opt_flags;
4555
4556 /*
4557 * Isolate the option name and find its value.
4558 */
4559 option_end = find_option_end(arg, &opt_flags);
4560 if (option_end == NULL)
4561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 EMSG2(_("E112: Option name missing: %s"), *arg);
4564 return FAIL;
4565 }
4566
4567 if (!evaluate)
4568 {
4569 *arg = option_end;
4570 return OK;
4571 }
4572
4573 c = *option_end;
4574 *option_end = NUL;
4575 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 if (opt_type == -3) /* invalid name */
4579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 EMSG2(_("E113: Unknown option: %s"), *arg);
4582 ret = FAIL;
4583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 if (opt_type == -2) /* hidden string option */
4587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 rettv->v_type = VAR_STRING;
4589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 else if (opt_type == -1) /* hidden number option */
4592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 else if (opt_type == 1) /* number option */
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 rettv->v_type = VAR_NUMBER;
4599 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 else /* string option */
4602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 rettv->v_type = VAR_STRING;
4604 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607 else if (working && (opt_type == -2 || opt_type == -1))
4608 ret = FAIL;
4609
4610 *option_end = c; /* put back for error messages */
4611 *arg = option_end;
4612
4613 return ret;
4614}
4615
4616/*
4617 * Allocate a variable for a string constant.
4618 * Return OK or FAIL.
4619 */
4620 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 int evaluate;
4625{
4626 char_u *p;
4627 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 int extra = 0;
4629
4630 /*
4631 * Find the end of the string, skipping backslashed characters.
4632 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004633 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 if (*p == '\\' && p[1] != NUL)
4636 {
4637 ++p;
4638 /* A "\<x>" form occupies at least 4 characters, and produces up
4639 * to 6 characters: reserve space for 2 extra */
4640 if (*p == '<')
4641 extra += 2;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644
4645 if (*p != '"')
4646 {
4647 EMSG2(_("E114: Missing quote: %s"), *arg);
4648 return FAIL;
4649 }
4650
4651 /* If only parsing, set *arg and return here */
4652 if (!evaluate)
4653 {
4654 *arg = p + 1;
4655 return OK;
4656 }
4657
4658 /*
4659 * Copy the string into allocated memory, handling backslashed
4660 * characters.
4661 */
4662 name = alloc((unsigned)(p - *arg + extra));
4663 if (name == NULL)
4664 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 rettv->v_type = VAR_STRING;
4666 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 if (*p == '\\')
4671 {
4672 switch (*++p)
4673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 case 'b': *name++ = BS; ++p; break;
4675 case 'e': *name++ = ESC; ++p; break;
4676 case 'f': *name++ = FF; ++p; break;
4677 case 'n': *name++ = NL; ++p; break;
4678 case 'r': *name++ = CAR; ++p; break;
4679 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 case 'X': /* hex: "\x1", "\x12" */
4682 case 'x':
4683 case 'u': /* Unicode: "\u0023" */
4684 case 'U':
4685 if (vim_isxdigit(p[1]))
4686 {
4687 int n, nr;
4688 int c = toupper(*p);
4689
4690 if (c == 'X')
4691 n = 2;
4692 else
4693 n = 4;
4694 nr = 0;
4695 while (--n >= 0 && vim_isxdigit(p[1]))
4696 {
4697 ++p;
4698 nr = (nr << 4) + hex2nr(*p);
4699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701#ifdef FEAT_MBYTE
4702 /* For "\u" store the number according to
4703 * 'encoding'. */
4704 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 else
4707#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 break;
4711
4712 /* octal: "\1", "\12", "\123" */
4713 case '0':
4714 case '1':
4715 case '2':
4716 case '3':
4717 case '4':
4718 case '5':
4719 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004720 case '7': *name = *p++ - '0';
4721 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004723 *name = (*name << 3) + *p++ - '0';
4724 if (*p >= '0' && *p <= '7')
4725 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 break;
4729
4730 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (extra != 0)
4733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 break;
4736 }
4737 /* FALLTHROUGH */
4738
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 break;
4741 }
4742 }
4743 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 *arg = p + 1;
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return OK;
4751}
4752
4753/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 * Return OK or FAIL.
4756 */
4757 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 int evaluate;
4762{
4763 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004764 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004765 int reduce = 0;
4766
4767 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004769 */
4770 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4771 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004775 break;
4776 ++reduce;
4777 ++p;
4778 }
4779 }
4780
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004783 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004784 return FAIL;
4785 }
4786
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004788 if (!evaluate)
4789 {
4790 *arg = p + 1;
4791 return OK;
4792 }
4793
4794 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004796 */
4797 str = alloc((unsigned)((p - *arg) - reduce));
4798 if (str == NULL)
4799 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 rettv->v_type = VAR_STRING;
4801 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004802
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004808 break;
4809 ++p;
4810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004814 *arg = p + 1;
4815
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004816 return OK;
4817}
4818
4819/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 * Allocate a variable for a List and fill it from "*arg".
4821 * Return OK or FAIL.
4822 */
4823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004826 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 int evaluate;
4828{
Bram Moolenaar33570922005-01-25 22:26:29 +00004829 list_T *l = NULL;
4830 typval_T tv;
4831 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832
4833 if (evaluate)
4834 {
4835 l = list_alloc();
4836 if (l == NULL)
4837 return FAIL;
4838 }
4839
4840 *arg = skipwhite(*arg + 1);
4841 while (**arg != ']' && **arg != NUL)
4842 {
4843 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4844 goto failret;
4845 if (evaluate)
4846 {
4847 item = listitem_alloc();
4848 if (item != NULL)
4849 {
4850 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004851 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 list_append(l, item);
4853 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004854 else
4855 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 }
4857
4858 if (**arg == ']')
4859 break;
4860 if (**arg != ',')
4861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004862 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 goto failret;
4864 }
4865 *arg = skipwhite(*arg + 1);
4866 }
4867
4868 if (**arg != ']')
4869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004870 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871failret:
4872 if (evaluate)
4873 list_free(l);
4874 return FAIL;
4875 }
4876
4877 *arg = skipwhite(*arg + 1);
4878 if (evaluate)
4879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 rettv->v_type = VAR_LIST;
4881 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 ++l->lv_refcount;
4883 }
4884
4885 return OK;
4886}
4887
4888/*
4889 * Allocate an empty header for a list.
4890 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004891 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892list_alloc()
4893{
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004895}
4896
4897/*
4898 * Unreference a list: decrement the reference count and free it when it
4899 * becomes zero.
4900 */
4901 static void
4902list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004903 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004904{
4905 if (l != NULL && --l->lv_refcount <= 0)
4906 list_free(l);
4907}
4908
4909/*
4910 * Free a list, including all items it points to.
4911 * Ignores the reference count.
4912 */
4913 static void
4914list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004915 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916{
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 listitem_T *item;
4918 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919
4920 for (item = l->lv_first; item != NULL; item = next)
4921 {
4922 next = item->li_next;
4923 listitem_free(item);
4924 }
4925 vim_free(l);
4926}
4927
4928/*
4929 * Allocate a list item.
4930 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004931 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932listitem_alloc()
4933{
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935}
4936
4937/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004938 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939 */
4940 static void
4941listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004942 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 vim_free(item);
4946}
4947
4948/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004949 * Remove a list item from a List and free it. Also clears the value.
4950 */
4951 static void
4952listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004953 list_T *l;
4954 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004955{
4956 list_remove(l, item, item);
4957 listitem_free(item);
4958}
4959
4960/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004961 * Get the number of items in a list.
4962 */
4963 static long
4964list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004965 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 if (l == NULL)
4968 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004969 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970}
4971
4972/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004973 * Return TRUE when two lists have exactly the same values.
4974 */
4975 static int
4976list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004977 list_T *l1;
4978 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004979 int ic; /* ignore case for strings */
4980{
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004983 if (list_len(l1) != list_len(l2))
4984 return FALSE;
4985
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004986 for (item1 = l1->lv_first, item2 = l2->lv_first;
4987 item1 != NULL && item2 != NULL;
4988 item1 = item1->li_next, item2 = item2->li_next)
4989 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4990 return FALSE;
4991 return item1 == NULL && item2 == NULL;
4992}
4993
4994/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004995 * Return TRUE when two dictionaries have exactly the same key/values.
4996 */
4997 static int
4998dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004999 dict_T *d1;
5000 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005001 int ic; /* ignore case for strings */
5002{
Bram Moolenaar33570922005-01-25 22:26:29 +00005003 hashitem_T *hi;
5004 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005005 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005006
5007 if (dict_len(d1) != dict_len(d2))
5008 return FALSE;
5009
Bram Moolenaar33570922005-01-25 22:26:29 +00005010 todo = d1->dv_hashtab.ht_used;
5011 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005013 if (!HASHITEM_EMPTY(hi))
5014 {
5015 item2 = dict_find(d2, hi->hi_key, -1);
5016 if (item2 == NULL)
5017 return FALSE;
5018 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5019 return FALSE;
5020 --todo;
5021 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005022 }
5023 return TRUE;
5024}
5025
5026/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005027 * Return TRUE if "tv1" and "tv2" have the same value.
5028 * Compares the items just like "==" would compare them.
5029 */
5030 static int
5031tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005032 typval_T *tv1;
5033 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005034 int ic; /* ignore case */
5035{
5036 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005037 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005038
5039 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
5040 {
5041 /* recursive! */
5042 if (tv1->v_type != tv2->v_type
5043 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
5044 return FALSE;
5045 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005046 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
5047 {
5048 /* recursive! */
5049 if (tv1->v_type != tv2->v_type
5050 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
5051 return FALSE;
5052 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005053 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
5054 {
5055 if (tv1->v_type != tv2->v_type
5056 || tv1->vval.v_string == NULL
5057 || tv2->vval.v_string == NULL
5058 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
5059 return FALSE;
5060 }
5061 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
5062 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
5064 * Don't consider "4x" to be equal to 4. */
5065 if ((tv1->v_type == VAR_STRING
5066 && !string_isa_number(tv1->vval.v_string))
5067 || (tv2->v_type == VAR_STRING
5068 && !string_isa_number(tv2->vval.v_string)))
5069 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005070 if (get_tv_number(tv1) != get_tv_number(tv2))
5071 return FALSE;
5072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005073 else
5074 {
5075 s1 = get_tv_string_buf(tv1, buf1);
5076 s2 = get_tv_string_buf(tv2, buf2);
5077 if ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) != 0)
5078 return FALSE;
5079 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005080 return TRUE;
5081}
5082
5083/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 * Return TRUE if "tv" is a number without other non-white characters.
5085 */
5086 static int
5087string_isa_number(s)
5088 char_u *s;
5089{
5090 int len;
5091
5092 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
5093 return len > 0 && *skipwhite(s + len) == NUL;
5094}
5095
5096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 * Locate item with index "n" in list "l" and return it.
5098 * A negative index is counted from the end; -1 is the last item.
5099 * Returns NULL when "n" is out of range.
5100 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005101 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005103 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005104 long n;
5105{
Bram Moolenaar33570922005-01-25 22:26:29 +00005106 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 long idx;
5108
5109 if (l == NULL)
5110 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005111
5112 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005114 n = l->lv_len + n;
5115
5116 /* Check for index out of range. */
5117 if (n < 0 || n >= l->lv_len)
5118 return NULL;
5119
5120 /* When there is a cached index may start search from there. */
5121 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005123 if (n < l->lv_idx / 2)
5124 {
5125 /* closest to the start of the list */
5126 item = l->lv_first;
5127 idx = 0;
5128 }
5129 else if (n > (l->lv_idx + l->lv_len) / 2)
5130 {
5131 /* closest to the end of the list */
5132 item = l->lv_last;
5133 idx = l->lv_len - 1;
5134 }
5135 else
5136 {
5137 /* closest to the cached index */
5138 item = l->lv_idx_item;
5139 idx = l->lv_idx;
5140 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 }
5142 else
5143 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005144 if (n < l->lv_len / 2)
5145 {
5146 /* closest to the start of the list */
5147 item = l->lv_first;
5148 idx = 0;
5149 }
5150 else
5151 {
5152 /* closest to the end of the list */
5153 item = l->lv_last;
5154 idx = l->lv_len - 1;
5155 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005157
5158 while (n > idx)
5159 {
5160 /* search forward */
5161 item = item->li_next;
5162 ++idx;
5163 }
5164 while (n < idx)
5165 {
5166 /* search backward */
5167 item = item->li_prev;
5168 --idx;
5169 }
5170
5171 /* cache the used index */
5172 l->lv_idx = idx;
5173 l->lv_idx_item = item;
5174
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 return item;
5176}
5177
5178/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005179 * Locate "item" list "l" and return its index.
5180 * Returns -1 when "item" is not in the list.
5181 */
5182 static long
5183list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005184 list_T *l;
5185 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005186{
5187 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005189
5190 if (l == NULL)
5191 return -1;
5192 idx = 0;
5193 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5194 ++idx;
5195 if (li == NULL)
5196 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005197 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005198}
5199
5200/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201 * Append item "item" to the end of list "l".
5202 */
5203 static void
5204list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005205 list_T *l;
5206 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207{
5208 if (l->lv_last == NULL)
5209 {
5210 /* empty list */
5211 l->lv_first = item;
5212 l->lv_last = item;
5213 item->li_prev = NULL;
5214 }
5215 else
5216 {
5217 l->lv_last->li_next = item;
5218 item->li_prev = l->lv_last;
5219 l->lv_last = item;
5220 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005221 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 item->li_next = NULL;
5223}
5224
5225/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005226 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005227 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 */
5229 static int
5230list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005231 list_T *l;
5232 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005234 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235
Bram Moolenaar05159a02005-02-26 23:04:13 +00005236 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005238 copy_tv(tv, &li->li_tv);
5239 list_append(l, li);
5240 return OK;
5241}
5242
5243/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005244 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005245 * Return FAIL when out of memory.
5246 */
5247 int
5248list_append_dict(list, dict)
5249 list_T *list;
5250 dict_T *dict;
5251{
5252 listitem_T *li = listitem_alloc();
5253
5254 if (li == NULL)
5255 return FAIL;
5256 li->li_tv.v_type = VAR_DICT;
5257 li->li_tv.v_lock = 0;
5258 li->li_tv.vval.v_dict = dict;
5259 list_append(list, li);
5260 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 return OK;
5262}
5263
5264/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005266 * If "item" is NULL append at the end.
5267 * Return FAIL when out of memory.
5268 */
5269 static int
5270list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 list_T *l;
5272 typval_T *tv;
5273 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005274{
Bram Moolenaar33570922005-01-25 22:26:29 +00005275 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005276
5277 if (ni == NULL)
5278 return FAIL;
5279 copy_tv(tv, &ni->li_tv);
5280 if (item == NULL)
5281 /* Append new item at end of list. */
5282 list_append(l, ni);
5283 else
5284 {
5285 /* Insert new item before existing item. */
5286 ni->li_prev = item->li_prev;
5287 ni->li_next = item;
5288 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005289 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005290 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005291 ++l->lv_idx;
5292 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005293 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005294 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005295 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005296 l->lv_idx_item = NULL;
5297 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005298 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005299 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 }
5301 return OK;
5302}
5303
5304/*
5305 * Extend "l1" with "l2".
5306 * If "bef" is NULL append at the end, otherwise insert before this item.
5307 * Returns FAIL when out of memory.
5308 */
5309 static int
5310list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005311 list_T *l1;
5312 list_T *l2;
5313 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005314{
Bram Moolenaar33570922005-01-25 22:26:29 +00005315 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005316
5317 for (item = l2->lv_first; item != NULL; item = item->li_next)
5318 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5319 return FAIL;
5320 return OK;
5321}
5322
5323/*
5324 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5325 * Return FAIL when out of memory.
5326 */
5327 static int
5328list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005329 list_T *l1;
5330 list_T *l2;
5331 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005332{
Bram Moolenaar33570922005-01-25 22:26:29 +00005333 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005334
5335 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005336 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005337 if (l == NULL)
5338 return FAIL;
5339 tv->v_type = VAR_LIST;
5340 tv->vval.v_list = l;
5341
5342 /* append all items from the second list */
5343 return list_extend(l, l2, NULL);
5344}
5345
5346/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005347 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005349 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 * Returns NULL when out of memory.
5351 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005353list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005356 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357{
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 list_T *copy;
5359 listitem_T *item;
5360 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361
5362 if (orig == NULL)
5363 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364
5365 copy = list_alloc();
5366 if (copy != NULL)
5367 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005368 if (copyID != 0)
5369 {
5370 /* Do this before adding the items, because one of the items may
5371 * refer back to this list. */
5372 orig->lv_copyID = copyID;
5373 orig->lv_copylist = copy;
5374 }
5375 for (item = orig->lv_first; item != NULL && !got_int;
5376 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
5378 ni = listitem_alloc();
5379 if (ni == NULL)
5380 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005381 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005382 {
5383 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5384 {
5385 vim_free(ni);
5386 break;
5387 }
5388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 list_append(copy, ni);
5392 }
5393 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005394 if (item != NULL)
5395 {
5396 list_unref(copy);
5397 copy = NULL;
5398 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 }
5400
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return copy;
5402}
5403
5404/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005405 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005406 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005408 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005409list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 list_T *l;
5411 listitem_T *item;
5412 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005413{
Bram Moolenaar33570922005-01-25 22:26:29 +00005414 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416 /* notify watchers */
5417 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005419 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005420 list_fix_watch(l, ip);
5421 if (ip == item2)
5422 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005424
5425 if (item2->li_next == NULL)
5426 l->lv_last = item->li_prev;
5427 else
5428 item2->li_next->li_prev = item->li_prev;
5429 if (item->li_prev == NULL)
5430 l->lv_first = item2->li_next;
5431 else
5432 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005433 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434}
5435
5436/*
5437 * Return an allocated string with the string representation of a list.
5438 * May return NULL.
5439 */
5440 static char_u *
5441list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443{
5444 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445
5446 if (tv->vval.v_list == NULL)
5447 return NULL;
5448 ga_init2(&ga, (int)sizeof(char), 80);
5449 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005450 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5451 {
5452 vim_free(ga.ga_data);
5453 return NULL;
5454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 ga_append(&ga, ']');
5456 ga_append(&ga, NUL);
5457 return (char_u *)ga.ga_data;
5458}
5459
5460/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005461 * Join list "l" into a string in "*gap", using separator "sep".
5462 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005463 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005464 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005465 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005466list_join(gap, l, sep, echo)
5467 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005468 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005469 char_u *sep;
5470 int echo;
5471{
5472 int first = TRUE;
5473 char_u *tofree;
5474 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005476 char_u *s;
5477
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005478 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005479 {
5480 if (first)
5481 first = FALSE;
5482 else
5483 ga_concat(gap, sep);
5484
5485 if (echo)
5486 s = echo_string(&item->li_tv, &tofree, numbuf);
5487 else
5488 s = tv2string(&item->li_tv, &tofree, numbuf);
5489 if (s != NULL)
5490 ga_concat(gap, s);
5491 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005492 if (s == NULL)
5493 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005494 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005495 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005496}
5497
5498/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499 * Allocate an empty header for a dictionary.
5500 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005501 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502dict_alloc()
5503{
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005505
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005507 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005510 d->dv_lock = 0;
5511 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005512 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005513 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005514 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515}
5516
5517/*
5518 * Unreference a Dictionary: decrement the reference count and free it when it
5519 * becomes zero.
5520 */
5521 static void
5522dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524{
5525 if (d != NULL && --d->dv_refcount <= 0)
5526 dict_free(d);
5527}
5528
5529/*
5530 * Free a Dictionary, including all items it contains.
5531 * Ignores the reference count.
5532 */
5533 static void
5534dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005537 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005538 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005540 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 * hashtab. Must not try to resize the hashtab! */
5542 todo = d->dv_hashtab.ht_used;
5543 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005545 if (!HASHITEM_EMPTY(hi))
5546 {
5547 dictitem_free(HI2DI(hi));
5548 --todo;
5549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 vim_free(d);
5553}
5554
5555/*
5556 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005557 * The "key" is copied to the new item.
5558 * Note that the value of the item "di_tv" still needs to be initialized!
5559 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005561 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005562dictitem_alloc(key)
5563 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564{
Bram Moolenaar33570922005-01-25 22:26:29 +00005565 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005566
Bram Moolenaar33570922005-01-25 22:26:29 +00005567 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005568 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005569 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005570 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005571 di->di_flags = 0;
5572 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005573 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574}
5575
5576/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005577 * Make a copy of a Dictionary item.
5578 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005579 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005580dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005581 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005582{
Bram Moolenaar33570922005-01-25 22:26:29 +00005583 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005584
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005586 if (di != NULL)
5587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005588 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005589 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005590 copy_tv(&org->di_tv, &di->di_tv);
5591 }
5592 return di;
5593}
5594
5595/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005596 * Remove item "item" from Dictionary "dict" and free it.
5597 */
5598 static void
5599dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 dict_T *dict;
5601 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005602{
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005604
Bram Moolenaar33570922005-01-25 22:26:29 +00005605 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005606 if (HASHITEM_EMPTY(hi))
5607 EMSG2(_(e_intern2), "dictitem_remove()");
5608 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005610 dictitem_free(item);
5611}
5612
5613/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 * Free a dict item. Also clears the value.
5615 */
5616 static void
5617dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 clear_tv(&item->di_tv);
5621 vim_free(item);
5622}
5623
5624/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005625 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5626 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005627 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005628 * Returns NULL when out of memory.
5629 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005631dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005632 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005633 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005634 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005635{
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 dict_T *copy;
5637 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005638 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005640
5641 if (orig == NULL)
5642 return NULL;
5643
5644 copy = dict_alloc();
5645 if (copy != NULL)
5646 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005647 if (copyID != 0)
5648 {
5649 orig->dv_copyID = copyID;
5650 orig->dv_copydict = copy;
5651 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005652 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005653 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005654 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005655 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 --todo;
5658
5659 di = dictitem_alloc(hi->hi_key);
5660 if (di == NULL)
5661 break;
5662 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005663 {
5664 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5665 copyID) == FAIL)
5666 {
5667 vim_free(di);
5668 break;
5669 }
5670 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005671 else
5672 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5673 if (dict_add(copy, di) == FAIL)
5674 {
5675 dictitem_free(di);
5676 break;
5677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005678 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005679 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005680
Bram Moolenaare9a41262005-01-15 22:18:47 +00005681 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005682 if (todo > 0)
5683 {
5684 dict_unref(copy);
5685 copy = NULL;
5686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005687 }
5688
5689 return copy;
5690}
5691
5692/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005694 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005696 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005698 dict_T *d;
5699 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700{
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702}
5703
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005705 * Add a number or string entry to dictionary "d".
5706 * When "str" is NULL use number "nr", otherwise use "str".
5707 * Returns FAIL when out of memory and when key already exists.
5708 */
5709 int
5710dict_add_nr_str(d, key, nr, str)
5711 dict_T *d;
5712 char *key;
5713 long nr;
5714 char_u *str;
5715{
5716 dictitem_T *item;
5717
5718 item = dictitem_alloc((char_u *)key);
5719 if (item == NULL)
5720 return FAIL;
5721 item->di_tv.v_lock = 0;
5722 if (str == NULL)
5723 {
5724 item->di_tv.v_type = VAR_NUMBER;
5725 item->di_tv.vval.v_number = nr;
5726 }
5727 else
5728 {
5729 item->di_tv.v_type = VAR_STRING;
5730 item->di_tv.vval.v_string = vim_strsave(str);
5731 }
5732 if (dict_add(d, item) == FAIL)
5733 {
5734 dictitem_free(item);
5735 return FAIL;
5736 }
5737 return OK;
5738}
5739
5740/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005741 * Get the number of items in a Dictionary.
5742 */
5743 static long
5744dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005746{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005747 if (d == NULL)
5748 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005750}
5751
5752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 * Find item "key[len]" in Dictionary "d".
5754 * If "len" is negative use strlen(key).
5755 * Returns NULL when not found.
5756 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005758dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005759 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 char_u *key;
5761 int len;
5762{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005763#define AKEYLEN 200
5764 char_u buf[AKEYLEN];
5765 char_u *akey;
5766 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005767 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005769 if (len < 0)
5770 akey = key;
5771 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005772 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005773 tofree = akey = vim_strnsave(key, len);
5774 if (akey == NULL)
5775 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005776 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005777 else
5778 {
5779 /* Avoid a malloc/free by using buf[]. */
5780 STRNCPY(buf, key, len);
5781 buf[len] = NUL;
5782 akey = buf;
5783 }
5784
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005786 vim_free(tofree);
5787 if (HASHITEM_EMPTY(hi))
5788 return NULL;
5789 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790}
5791
5792/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005793 * Get a string item from a dictionary in allocated memory.
5794 * Returns NULL if the entry doesn't exist or out of memory.
5795 */
5796 char_u *
5797get_dict_string(d, key)
5798 dict_T *d;
5799 char_u *key;
5800{
5801 dictitem_T *di;
5802
5803 di = dict_find(d, key, -1);
5804 if (di == NULL)
5805 return NULL;
5806 return vim_strsave(get_tv_string(&di->di_tv));
5807}
5808
5809/*
5810 * Get a number item from a dictionary.
5811 * Returns 0 if the entry doesn't exist or out of memory.
5812 */
5813 long
5814get_dict_number(d, key)
5815 dict_T *d;
5816 char_u *key;
5817{
5818 dictitem_T *di;
5819
5820 di = dict_find(d, key, -1);
5821 if (di == NULL)
5822 return 0;
5823 return get_tv_number(&di->di_tv);
5824}
5825
5826/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 * Return an allocated string with the string representation of a Dictionary.
5828 * May return NULL.
5829 */
5830 static char_u *
5831dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833{
5834 garray_T ga;
5835 int first = TRUE;
5836 char_u *tofree;
5837 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005840 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005841 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005843 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 return NULL;
5845 ga_init2(&ga, (int)sizeof(char), 80);
5846 ga_append(&ga, '{');
5847
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005849 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005853 --todo;
5854
5855 if (first)
5856 first = FALSE;
5857 else
5858 ga_concat(&ga, (char_u *)", ");
5859
5860 tofree = string_quote(hi->hi_key, FALSE);
5861 if (tofree != NULL)
5862 {
5863 ga_concat(&ga, tofree);
5864 vim_free(tofree);
5865 }
5866 ga_concat(&ga, (char_u *)": ");
5867 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5868 if (s != NULL)
5869 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005871 if (s == NULL)
5872 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005875 if (todo > 0)
5876 {
5877 vim_free(ga.ga_data);
5878 return NULL;
5879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880
5881 ga_append(&ga, '}');
5882 ga_append(&ga, NUL);
5883 return (char_u *)ga.ga_data;
5884}
5885
5886/*
5887 * Allocate a variable for a Dictionary and fill it from "*arg".
5888 * Return OK or FAIL. Returns NOTDONE for {expr}.
5889 */
5890 static int
5891get_dict_tv(arg, rettv, evaluate)
5892 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 int evaluate;
5895{
Bram Moolenaar33570922005-01-25 22:26:29 +00005896 dict_T *d = NULL;
5897 typval_T tvkey;
5898 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005902 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903
5904 /*
5905 * First check if it's not a curly-braces thing: {expr}.
5906 * Must do this without evaluating, otherwise a function may be called
5907 * twice. Unfortunately this means we need to call eval1() twice for the
5908 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005909 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005911 if (*start != '}')
5912 {
5913 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5914 return FAIL;
5915 if (*start == '}')
5916 return NOTDONE;
5917 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918
5919 if (evaluate)
5920 {
5921 d = dict_alloc();
5922 if (d == NULL)
5923 return FAIL;
5924 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005925 tvkey.v_type = VAR_UNKNOWN;
5926 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927
5928 *arg = skipwhite(*arg + 1);
5929 while (**arg != '}' && **arg != NUL)
5930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005931 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 goto failret;
5933 if (**arg != ':')
5934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005935 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005936 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005937 goto failret;
5938 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005939 key = get_tv_string_buf_chk(&tvkey, buf);
5940 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005942 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
5943 if (key != NULL)
5944 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005945 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 goto failret;
5947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948
5949 *arg = skipwhite(*arg + 1);
5950 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005952 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 goto failret;
5954 }
5955 if (evaluate)
5956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005957 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958 if (item != NULL)
5959 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00005960 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005961 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962 clear_tv(&tv);
5963 goto failret;
5964 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005965 item = dictitem_alloc(key);
5966 clear_tv(&tvkey);
5967 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005969 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005970 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005971 if (dict_add(d, item) == FAIL)
5972 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 }
5974 }
5975
5976 if (**arg == '}')
5977 break;
5978 if (**arg != ',')
5979 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005980 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 goto failret;
5982 }
5983 *arg = skipwhite(*arg + 1);
5984 }
5985
5986 if (**arg != '}')
5987 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005988 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005989failret:
5990 if (evaluate)
5991 dict_free(d);
5992 return FAIL;
5993 }
5994
5995 *arg = skipwhite(*arg + 1);
5996 if (evaluate)
5997 {
5998 rettv->v_type = VAR_DICT;
5999 rettv->vval.v_dict = d;
6000 ++d->dv_refcount;
6001 }
6002
6003 return OK;
6004}
6005
Bram Moolenaar8c711452005-01-14 21:53:12 +00006006/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Return a string with the string representation of a variable.
6008 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 * May return NULL;
6012 */
6013 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006014echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006015 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006019 static int recurse = 0;
6020 char_u *r = NULL;
6021
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006024 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006025 *tofree = NULL;
6026 return NULL;
6027 }
6028 ++recurse;
6029
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 switch (tv->v_type)
6031 {
6032 case VAR_FUNC:
6033 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006034 r = tv->vval.v_string;
6035 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 case VAR_LIST:
6037 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006038 r = *tofree;
6039 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006040 case VAR_DICT:
6041 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006042 r = *tofree;
6043 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006044 case VAR_STRING:
6045 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006046 *tofree = NULL;
6047 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006049 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006050 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006051 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006052 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053
6054 --recurse;
6055 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056}
6057
6058/*
6059 * Return a string with the string representation of a variable.
6060 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6061 * "numbuf" is used for a number.
6062 * Puts quotes around strings, so that they can be parsed back by eval().
6063 * May return NULL;
6064 */
6065 static char_u *
6066tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006068 char_u **tofree;
6069 char_u *numbuf;
6070{
6071 switch (tv->v_type)
6072 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006073 case VAR_FUNC:
6074 *tofree = string_quote(tv->vval.v_string, TRUE);
6075 return *tofree;
6076 case VAR_STRING:
6077 *tofree = string_quote(tv->vval.v_string, FALSE);
6078 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006079 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006081 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006082 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006083 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006084 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006086 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087}
6088
6089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 * Return string "str" in ' quotes, doubling ' characters.
6091 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006093 */
6094 static char_u *
6095string_quote(str, function)
6096 char_u *str;
6097 int function;
6098{
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006100 char_u *p, *r, *s;
6101
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 len = (function ? 13 : 3);
6103 if (str != NULL)
6104 {
6105 len += STRLEN(str);
6106 for (p = str; *p != NUL; mb_ptr_adv(p))
6107 if (*p == '\'')
6108 ++len;
6109 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006110 s = r = alloc(len);
6111 if (r != NULL)
6112 {
6113 if (function)
6114 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006115 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006116 r += 10;
6117 }
6118 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006119 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 if (str != NULL)
6121 for (p = str; *p != NUL; )
6122 {
6123 if (*p == '\'')
6124 *r++ = '\'';
6125 MB_COPY_CHAR(p, r);
6126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006127 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006128 if (function)
6129 *r++ = ')';
6130 *r++ = NUL;
6131 }
6132 return s;
6133}
6134
6135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 * Get the value of an environment variable.
6137 * "arg" is pointing to the '$'. It is advanced to after the name.
6138 * If the environment variable was not set, silently assume it is empty.
6139 * Always return OK.
6140 */
6141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006142get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 int evaluate;
6146{
6147 char_u *string = NULL;
6148 int len;
6149 int cc;
6150 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006151 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153 ++*arg;
6154 name = *arg;
6155 len = get_env_len(arg);
6156 if (evaluate)
6157 {
6158 if (len != 0)
6159 {
6160 cc = name[len];
6161 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006162 /* first try vim_getenv(), fast for normal environment vars */
6163 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006165 {
6166 if (!mustfree)
6167 string = vim_strsave(string);
6168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 else
6170 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006171 if (mustfree)
6172 vim_free(string);
6173
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 /* next try expanding things like $VIM and ${HOME} */
6175 string = expand_env_save(name - 1);
6176 if (string != NULL && *string == '$')
6177 {
6178 vim_free(string);
6179 string = NULL;
6180 }
6181 }
6182 name[len] = cc;
6183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 rettv->v_type = VAR_STRING;
6185 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 }
6187
6188 return OK;
6189}
6190
6191/*
6192 * Array with names and number of arguments of all internal functions
6193 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6194 */
6195static struct fst
6196{
6197 char *f_name; /* function name */
6198 char f_min_argc; /* minimal number of arguments */
6199 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202} functions[] =
6203{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006204 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {"append", 2, 2, f_append},
6206 {"argc", 0, 0, f_argc},
6207 {"argidx", 0, 0, f_argidx},
6208 {"argv", 1, 1, f_argv},
6209 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006210 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {"bufexists", 1, 1, f_bufexists},
6212 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6213 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6214 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6215 {"buflisted", 1, 1, f_buflisted},
6216 {"bufloaded", 1, 1, f_bufloaded},
6217 {"bufname", 1, 1, f_bufname},
6218 {"bufnr", 1, 1, f_bufnr},
6219 {"bufwinnr", 1, 1, f_bufwinnr},
6220 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006221 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006222 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 {"char2nr", 1, 1, f_char2nr},
6224 {"cindent", 1, 1, f_cindent},
6225 {"col", 1, 1, f_col},
6226 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006228 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {"cscope_connection",0,3, f_cscope_connection},
6230 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006231 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {"delete", 1, 1, f_delete},
6233 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006234 {"diff_filler", 1, 1, f_diff_filler},
6235 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006236 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006238 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {"eventhandler", 0, 0, f_eventhandler},
6240 {"executable", 1, 1, f_executable},
6241 {"exists", 1, 1, f_exists},
6242 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6245 {"filereadable", 1, 1, f_filereadable},
6246 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006247 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006248 {"finddir", 1, 3, f_finddir},
6249 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {"fnamemodify", 2, 2, f_fnamemodify},
6251 {"foldclosed", 1, 1, f_foldclosed},
6252 {"foldclosedend", 1, 1, f_foldclosedend},
6253 {"foldlevel", 1, 1, f_foldlevel},
6254 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006255 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006258 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {"getbufvar", 2, 2, f_getbufvar},
6260 {"getchar", 0, 1, f_getchar},
6261 {"getcharmod", 0, 0, f_getcharmod},
6262 {"getcmdline", 0, 0, f_getcmdline},
6263 {"getcmdpos", 0, 0, f_getcmdpos},
6264 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006265 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006266 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {"getfsize", 1, 1, f_getfsize},
6268 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006269 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006270 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006271 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006272 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {"getregtype", 0, 1, f_getregtype},
6274 {"getwinposx", 0, 0, f_getwinposx},
6275 {"getwinposy", 0, 0, f_getwinposy},
6276 {"getwinvar", 2, 2, f_getwinvar},
6277 {"glob", 1, 1, f_glob},
6278 {"globpath", 2, 2, f_globpath},
6279 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006280 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {"hasmapto", 1, 2, f_hasmapto},
6282 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6283 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6284 {"histadd", 2, 2, f_histadd},
6285 {"histdel", 1, 2, f_histdel},
6286 {"histget", 1, 2, f_histget},
6287 {"histnr", 1, 1, f_histnr},
6288 {"hlID", 1, 1, f_hlID},
6289 {"hlexists", 1, 1, f_hlexists},
6290 {"hostname", 0, 0, f_hostname},
6291 {"iconv", 3, 3, f_iconv},
6292 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006293 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {"input", 1, 2, f_input},
6295 {"inputdialog", 1, 3, f_inputdialog},
6296 {"inputrestore", 0, 0, f_inputrestore},
6297 {"inputsave", 0, 0, f_inputsave},
6298 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006301 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006303 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006304 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {"libcall", 3, 3, f_libcall},
6308 {"libcallnr", 3, 3, f_libcallnr},
6309 {"line", 1, 1, f_line},
6310 {"line2byte", 1, 1, f_line2byte},
6311 {"lispindent", 1, 1, f_lispindent},
6312 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006313 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {"maparg", 1, 2, f_maparg},
6315 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006316 {"match", 2, 4, f_match},
6317 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006318 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006319 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006320 {"max", 1, 1, f_max},
6321 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006322#ifdef vim_mkdir
6323 {"mkdir", 1, 3, f_mkdir},
6324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {"mode", 0, 0, f_mode},
6326 {"nextnonblank", 1, 1, f_nextnonblank},
6327 {"nr2char", 1, 1, f_nr2char},
6328 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006330 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {"remote_expr", 2, 3, f_remote_expr},
6332 {"remote_foreground", 1, 1, f_remote_foreground},
6333 {"remote_peek", 1, 2, f_remote_peek},
6334 {"remote_read", 1, 1, f_remote_read},
6335 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006338 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006340 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {"search", 1, 2, f_search},
6342 {"searchpair", 3, 5, f_searchpair},
6343 {"server2client", 2, 2, f_server2client},
6344 {"serverlist", 0, 0, f_serverlist},
6345 {"setbufvar", 3, 3, f_setbufvar},
6346 {"setcmdpos", 1, 1, f_setcmdpos},
6347 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006348 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {"setreg", 2, 3, f_setreg},
6350 {"setwinvar", 3, 3, f_setwinvar},
6351 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006352 {"sort", 1, 2, f_sort},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006353 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354#ifdef HAVE_STRFTIME
6355 {"strftime", 1, 2, f_strftime},
6356#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {"strlen", 1, 1, f_strlen},
6360 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006361 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 {"strtrans", 1, 1, f_strtrans},
6363 {"submatch", 1, 1, f_submatch},
6364 {"substitute", 4, 4, f_substitute},
6365 {"synID", 3, 3, f_synID},
6366 {"synIDattr", 2, 3, f_synIDattr},
6367 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006368 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006369 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 {"tempname", 0, 0, f_tempname},
6371 {"tolower", 1, 1, f_tolower},
6372 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006373 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 {"virtcol", 1, 1, f_virtcol},
6377 {"visualmode", 0, 1, f_visualmode},
6378 {"winbufnr", 1, 1, f_winbufnr},
6379 {"wincol", 0, 0, f_wincol},
6380 {"winheight", 1, 1, f_winheight},
6381 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006382 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {"winrestcmd", 0, 0, f_winrestcmd},
6384 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006385 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386};
6387
6388#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6389
6390/*
6391 * Function given to ExpandGeneric() to obtain the list of internal
6392 * or user defined function names.
6393 */
6394 char_u *
6395get_function_name(xp, idx)
6396 expand_T *xp;
6397 int idx;
6398{
6399 static int intidx = -1;
6400 char_u *name;
6401
6402 if (idx == 0)
6403 intidx = -1;
6404 if (intidx < 0)
6405 {
6406 name = get_user_func_name(xp, idx);
6407 if (name != NULL)
6408 return name;
6409 }
6410 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6411 {
6412 STRCPY(IObuff, functions[intidx].f_name);
6413 STRCAT(IObuff, "(");
6414 if (functions[intidx].f_max_argc == 0)
6415 STRCAT(IObuff, ")");
6416 return IObuff;
6417 }
6418
6419 return NULL;
6420}
6421
6422/*
6423 * Function given to ExpandGeneric() to obtain the list of internal or
6424 * user defined variable or function names.
6425 */
6426/*ARGSUSED*/
6427 char_u *
6428get_expr_name(xp, idx)
6429 expand_T *xp;
6430 int idx;
6431{
6432 static int intidx = -1;
6433 char_u *name;
6434
6435 if (idx == 0)
6436 intidx = -1;
6437 if (intidx < 0)
6438 {
6439 name = get_function_name(xp, idx);
6440 if (name != NULL)
6441 return name;
6442 }
6443 return get_user_var_name(xp, ++intidx);
6444}
6445
6446#endif /* FEAT_CMDL_COMPL */
6447
6448/*
6449 * Find internal function in table above.
6450 * Return index, or -1 if not found
6451 */
6452 static int
6453find_internal_func(name)
6454 char_u *name; /* name of the function */
6455{
6456 int first = 0;
6457 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6458 int cmp;
6459 int x;
6460
6461 /*
6462 * Find the function name in the table. Binary search.
6463 */
6464 while (first <= last)
6465 {
6466 x = first + ((unsigned)(last - first) >> 1);
6467 cmp = STRCMP(name, functions[x].f_name);
6468 if (cmp < 0)
6469 last = x - 1;
6470 else if (cmp > 0)
6471 first = x + 1;
6472 else
6473 return x;
6474 }
6475 return -1;
6476}
6477
6478/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6480 * name it contains, otherwise return "name".
6481 */
6482 static char_u *
6483deref_func_name(name, lenp)
6484 char_u *name;
6485 int *lenp;
6486{
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 int cc;
6489
6490 cc = name[*lenp];
6491 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006492 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 {
6498 *lenp = 0;
6499 return (char_u *)""; /* just in case */
6500 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 *lenp = STRLEN(v->di_tv.vval.v_string);
6502 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 }
6504
6505 return name;
6506}
6507
6508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 * Allocate a variable for the result of a function.
6510 * Return OK or FAIL.
6511 */
6512 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6514 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 char_u *name; /* name of the function */
6516 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 char_u **arg; /* argument, pointing to the '(' */
6519 linenr_T firstline; /* first line of range */
6520 linenr_T lastline; /* last line of range */
6521 int *doesrange; /* return: function handled range */
6522 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 char_u *argp;
6526 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 int argcount = 0; /* number of arguments found */
6529
6530 /*
6531 * Get the arguments.
6532 */
6533 argp = *arg;
6534 while (argcount < MAX_FUNC_ARGS)
6535 {
6536 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6537 if (*argp == ')' || *argp == ',' || *argp == NUL)
6538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6540 {
6541 ret = FAIL;
6542 break;
6543 }
6544 ++argcount;
6545 if (*argp != ',')
6546 break;
6547 }
6548 if (*argp == ')')
6549 ++argp;
6550 else
6551 ret = FAIL;
6552
6553 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006554 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 {
6558 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006559 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006565 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567 *arg = skipwhite(argp);
6568 return ret;
6569}
6570
6571
6572/*
6573 * Call a function with its resolved parameters
6574 * Return OK or FAIL.
6575 */
6576 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 char_u *name; /* name of the function */
6580 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 linenr_T firstline; /* first line of range */
6585 linenr_T lastline; /* last line of range */
6586 int *doesrange; /* return: function handled range */
6587 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591#define ERROR_UNKNOWN 0
6592#define ERROR_TOOMANY 1
6593#define ERROR_TOOFEW 2
6594#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595#define ERROR_DICT 4
6596#define ERROR_NONE 5
6597#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 int error = ERROR_NONE;
6599 int i;
6600 int llen;
6601 ufunc_T *fp;
6602 int cc;
6603#define FLEN_FIXED 40
6604 char_u fname_buf[FLEN_FIXED + 1];
6605 char_u *fname;
6606
6607 /*
6608 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6609 * Change <SNR>123_name() to K_SNR 123_name().
6610 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6611 */
6612 cc = name[len];
6613 name[len] = NUL;
6614 llen = eval_fname_script(name);
6615 if (llen > 0)
6616 {
6617 fname_buf[0] = K_SPECIAL;
6618 fname_buf[1] = KS_EXTRA;
6619 fname_buf[2] = (int)KE_SNR;
6620 i = 3;
6621 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6622 {
6623 if (current_SID <= 0)
6624 error = ERROR_SCRIPT;
6625 else
6626 {
6627 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6628 i = (int)STRLEN(fname_buf);
6629 }
6630 }
6631 if (i + STRLEN(name + llen) < FLEN_FIXED)
6632 {
6633 STRCPY(fname_buf + i, name + llen);
6634 fname = fname_buf;
6635 }
6636 else
6637 {
6638 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6639 if (fname == NULL)
6640 error = ERROR_OTHER;
6641 else
6642 {
6643 mch_memmove(fname, fname_buf, (size_t)i);
6644 STRCPY(fname + i, name + llen);
6645 }
6646 }
6647 }
6648 else
6649 fname = name;
6650
6651 *doesrange = FALSE;
6652
6653
6654 /* execute the function if no errors detected and executing */
6655 if (evaluate && error == ERROR_NONE)
6656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006657 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 error = ERROR_UNKNOWN;
6659
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006660 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
6662 /*
6663 * User defined function.
6664 */
6665 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006666
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006668 /* Trigger FuncUndefined event, may load the function. */
6669 if (fp == NULL
6670 && apply_autocmds(EVENT_FUNCUNDEFINED,
6671 fname, fname, TRUE, NULL)
6672 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006674 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 fp = find_func(fname);
6676 }
6677#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006678 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006679 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006680 {
6681 /* loaded a package, search for the function again */
6682 fp = find_func(fname);
6683 }
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 if (fp != NULL)
6686 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006687 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006689 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006691 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006693 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else
6696 {
6697 /*
6698 * Call the user function.
6699 * Save and restore search patterns, script variables and
6700 * redo buffer.
6701 */
6702 save_search_patterns();
6703 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006704 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006705 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006707 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6708 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6709 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006710 /* Function was unreferenced while being used, free it
6711 * now. */
6712 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 restoreRedobuff();
6714 restore_search_patterns();
6715 error = ERROR_NONE;
6716 }
6717 }
6718 }
6719 else
6720 {
6721 /*
6722 * Find the function name in the table, call its implementation.
6723 */
6724 i = find_internal_func(fname);
6725 if (i >= 0)
6726 {
6727 if (argcount < functions[i].f_min_argc)
6728 error = ERROR_TOOFEW;
6729 else if (argcount > functions[i].f_max_argc)
6730 error = ERROR_TOOMANY;
6731 else
6732 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 error = ERROR_NONE;
6736 }
6737 }
6738 }
6739 /*
6740 * The function call (or "FuncUndefined" autocommand sequence) might
6741 * have been aborted by an error, an interrupt, or an explicitly thrown
6742 * exception that has not been caught so far. This situation can be
6743 * tested for by calling aborting(). For an error in an internal
6744 * function or for the "E132" error in call_user_func(), however, the
6745 * throw point at which the "force_abort" flag (temporarily reset by
6746 * emsg()) is normally updated has not been reached yet. We need to
6747 * update that flag first to make aborting() reliable.
6748 */
6749 update_force_abort();
6750 }
6751 if (error == ERROR_NONE)
6752 ret = OK;
6753
6754 /*
6755 * Report an error unless the argument evaluation or function call has been
6756 * cancelled due to an aborting error, an interrupt, or an exception.
6757 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 if (!aborting())
6759 {
6760 switch (error)
6761 {
6762 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006763 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764 break;
6765 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 break;
6768 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 name);
6771 break;
6772 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006773 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774 name);
6775 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006777 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778 name);
6779 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780 }
6781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 name[len] = cc;
6784 if (fname != name && fname != fname_buf)
6785 vim_free(fname);
6786
6787 return ret;
6788}
6789
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006790/*
6791 * Give an error message with a function name. Handle <SNR> things.
6792 */
6793 static void
6794emsg_funcname(msg, name)
6795 char *msg;
6796 char_u *name;
6797{
6798 char_u *p;
6799
6800 if (*name == K_SPECIAL)
6801 p = concat_str((char_u *)"<SNR>", name + 3);
6802 else
6803 p = name;
6804 EMSG2(_(msg), p);
6805 if (p != name)
6806 vim_free(p);
6807}
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809/*********************************************
6810 * Implementation of the built-in functions
6811 */
6812
6813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006814 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 */
6816 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006817f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 typval_T *argvars;
6819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006823 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006826 if ((l = argvars[0].vval.v_list) != NULL
6827 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6828 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 }
6831 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006832 EMSG(_(e_listreq));
6833}
6834
6835/*
6836 * "append(lnum, string/list)" function
6837 */
6838 static void
6839f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006840 typval_T *argvars;
6841 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006842{
6843 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006844 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00006845 list_T *l = NULL;
6846 listitem_T *li = NULL;
6847 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006848 long added = 0;
6849
Bram Moolenaar0d660222005-01-07 21:51:51 +00006850 lnum = get_tv_lnum(argvars);
6851 if (lnum >= 0
6852 && lnum <= curbuf->b_ml.ml_line_count
6853 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006855 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006857 l = argvars[1].vval.v_list;
6858 if (l == NULL)
6859 return;
6860 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006862 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00006863 for (;;)
6864 {
6865 if (l == NULL)
6866 tv = &argvars[1]; /* append a string */
6867 else if (li == NULL)
6868 break; /* end of list */
6869 else
6870 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006871 line = get_tv_string_chk(tv);
6872 if (line == NULL) /* type error */
6873 {
6874 rettv->vval.v_number = 1; /* Failed */
6875 break;
6876 }
6877 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00006878 ++added;
6879 if (l == NULL)
6880 break;
6881 li = li->li_next;
6882 }
6883
6884 appended_lines_mark(lnum, added);
6885 if (curwin->w_cursor.lnum > lnum)
6886 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006888 else
6889 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890}
6891
6892/*
6893 * "argc()" function
6894 */
6895/* ARGSUSED */
6896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006897f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 typval_T *argvars;
6899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006901 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902}
6903
6904/*
6905 * "argidx()" function
6906 */
6907/* ARGSUSED */
6908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909f_argidx(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 Moolenaarc70646c2005-01-04 21:52:38 +00006913 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914}
6915
6916/*
6917 * "argv(nr)" function
6918 */
6919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920f_argv(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 int idx;
6925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006926 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006928 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 rettv->vval.v_string = NULL;
6931 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932}
6933
6934/*
6935 * "browse(save, title, initdir, default)" function
6936 */
6937/* ARGSUSED */
6938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006939f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 typval_T *argvars;
6941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943#ifdef FEAT_BROWSE
6944 int save;
6945 char_u *title;
6946 char_u *initdir;
6947 char_u *defname;
6948 char_u buf[NUMBUFLEN];
6949 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006950 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006952 save = get_tv_number_chk(&argvars[0], &error);
6953 title = get_tv_string_chk(&argvars[1]);
6954 initdir = get_tv_string_buf_chk(&argvars[2], buf);
6955 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006957 if (error || title == NULL || initdir == NULL || defname == NULL)
6958 rettv->vval.v_string = NULL;
6959 else
6960 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006961 do_browse(save ? BROWSE_SAVE : 0,
6962 title, defname, NULL, initdir, NULL, curbuf);
6963#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006964 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006966 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006967}
6968
6969/*
6970 * "browsedir(title, initdir)" function
6971 */
6972/* ARGSUSED */
6973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006974f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 typval_T *argvars;
6976 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006977{
6978#ifdef FEAT_BROWSE
6979 char_u *title;
6980 char_u *initdir;
6981 char_u buf[NUMBUFLEN];
6982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006983 title = get_tv_string_chk(&argvars[0]);
6984 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006986 if (title == NULL || initdir == NULL)
6987 rettv->vval.v_string = NULL;
6988 else
6989 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006990 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995}
6996
Bram Moolenaar33570922005-01-25 22:26:29 +00006997static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999/*
7000 * Find a buffer by number or exact name.
7001 */
7002 static buf_T *
7003find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007008 if (avar->v_type == VAR_NUMBER)
7009 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007010 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007012 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007013 if (buf == NULL)
7014 {
7015 /* No full path name match, try a match with a URL or a "nofile"
7016 * buffer, these don't use the full path. */
7017 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7018 if (buf->b_fname != NULL
7019 && (path_with_url(buf->b_fname)
7020#ifdef FEAT_QUICKFIX
7021 || bt_nofile(buf)
7022#endif
7023 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007024 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007025 break;
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 }
7028 return buf;
7029}
7030
7031/*
7032 * "bufexists(expr)" function
7033 */
7034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007035f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 typval_T *argvars;
7037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007039 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040}
7041
7042/*
7043 * "buflisted(expr)" function
7044 */
7045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007046f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 typval_T *argvars;
7048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
7050 buf_T *buf;
7051
7052 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007053 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
7056/*
7057 * "bufloaded(expr)" function
7058 */
7059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007060f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 typval_T *argvars;
7062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 buf_T *buf;
7065
7066 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007067 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068}
7069
Bram Moolenaar33570922005-01-25 22:26:29 +00007070static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072/*
7073 * Get buffer by number or pattern.
7074 */
7075 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007076get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007079 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 int save_magic;
7081 char_u *save_cpo;
7082 buf_T *buf;
7083
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084 if (tv->v_type == VAR_NUMBER)
7085 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007086 if (tv->v_type != VAR_STRING)
7087 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (name == NULL || *name == NUL)
7089 return curbuf;
7090 if (name[0] == '$' && name[1] == NUL)
7091 return lastbuf;
7092
7093 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7094 save_magic = p_magic;
7095 p_magic = TRUE;
7096 save_cpo = p_cpo;
7097 p_cpo = (char_u *)"";
7098
7099 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7100 TRUE, FALSE));
7101
7102 p_magic = save_magic;
7103 p_cpo = save_cpo;
7104
7105 /* If not found, try expanding the name, like done for bufexists(). */
7106 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007107 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109 return buf;
7110}
7111
7112/*
7113 * "bufname(expr)" function
7114 */
7115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007116f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 typval_T *argvars;
7118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 buf_T *buf;
7121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007122 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007124 buf = get_buf_tv(&argvars[0]);
7125 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007127 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007129 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 --emsg_off;
7131}
7132
7133/*
7134 * "bufnr(expr)" function
7135 */
7136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007137f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007138 typval_T *argvars;
7139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 buf_T *buf;
7142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007143 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007145 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007147 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 --emsg_off;
7151}
7152
7153/*
7154 * "bufwinnr(nr)" function
7155 */
7156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 typval_T *argvars;
7159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161#ifdef FEAT_WINDOWS
7162 win_T *wp;
7163 int winnr = 0;
7164#endif
7165 buf_T *buf;
7166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007167 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007169 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170#ifdef FEAT_WINDOWS
7171 for (wp = firstwin; wp; wp = wp->w_next)
7172 {
7173 ++winnr;
7174 if (wp->w_buffer == buf)
7175 break;
7176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007177 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007179 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180#endif
7181 --emsg_off;
7182}
7183
7184/*
7185 * "byte2line(byte)" function
7186 */
7187/*ARGSUSED*/
7188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007189f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 typval_T *argvars;
7191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195#else
7196 long boff = 0;
7197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007198 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007202 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 (linenr_T)0, &boff);
7204#endif
7205}
7206
7207/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007208 * "byteidx()" function
7209 */
7210/*ARGSUSED*/
7211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007212f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 typval_T *argvars;
7214 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007215{
7216#ifdef FEAT_MBYTE
7217 char_u *t;
7218#endif
7219 char_u *str;
7220 long idx;
7221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007222 str = get_tv_string_chk(&argvars[0]);
7223 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007226 return;
7227
7228#ifdef FEAT_MBYTE
7229 t = str;
7230 for ( ; idx > 0; idx--)
7231 {
7232 if (*t == NUL) /* EOL reached */
7233 return;
7234 t += mb_ptr2len_check(t);
7235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007237#else
7238 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007240#endif
7241}
7242
7243/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007244 * "call(func, arglist)" function
7245 */
7246 static void
7247f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 typval_T *argvars;
7249 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007250{
7251 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007253 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007255 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007257
7258 rettv->vval.v_number = 0;
7259 if (argvars[1].v_type != VAR_LIST)
7260 {
7261 EMSG(_(e_listreq));
7262 return;
7263 }
7264 if (argvars[1].vval.v_list == NULL)
7265 return;
7266
7267 if (argvars[0].v_type == VAR_FUNC)
7268 func = argvars[0].vval.v_string;
7269 else
7270 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007271 if (*func == NUL)
7272 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007273
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 if (argvars[2].v_type != VAR_UNKNOWN)
7275 {
7276 if (argvars[2].v_type != VAR_DICT)
7277 {
7278 EMSG(_(e_dictreq));
7279 return;
7280 }
7281 selfdict = argvars[2].vval.v_dict;
7282 }
7283
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007284 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7285 item = item->li_next)
7286 {
7287 if (argc == MAX_FUNC_ARGS)
7288 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007289 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007290 break;
7291 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007292 /* Make a copy of each argument. This is needed to be able to set
7293 * v_lock to VAR_FIXED in the copy without changing the original list.
7294 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007295 copy_tv(&item->li_tv, &argv[argc++]);
7296 }
7297
7298 if (item == NULL)
7299 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7301 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007302
7303 /* Free the arguments. */
7304 while (argc > 0)
7305 clear_tv(&argv[--argc]);
7306}
7307
7308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 * "char2nr(string)" function
7310 */
7311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 typval_T *argvars;
7314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316#ifdef FEAT_MBYTE
7317 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 else
7320#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007321 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
7323
7324/*
7325 * "cindent(lnum)" function
7326 */
7327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007328f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 typval_T *argvars;
7330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332#ifdef FEAT_CINDENT
7333 pos_T pos;
7334 linenr_T lnum;
7335
7336 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007337 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7339 {
7340 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007341 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 curwin->w_cursor = pos;
7343 }
7344 else
7345#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347}
7348
7349/*
7350 * "col(string)" function
7351 */
7352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007353f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 typval_T *argvars;
7355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 colnr_T col = 0;
7358 pos_T *fp;
7359
7360 fp = var2fpos(&argvars[0], FALSE);
7361 if (fp != NULL)
7362 {
7363 if (fp->col == MAXCOL)
7364 {
7365 /* '> can be MAXCOL, get the length of the line then */
7366 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7367 col = STRLEN(ml_get(fp->lnum)) + 1;
7368 else
7369 col = MAXCOL;
7370 }
7371 else
7372 {
7373 col = fp->col + 1;
7374#ifdef FEAT_VIRTUALEDIT
7375 /* col(".") when the cursor is on the NUL at the end of the line
7376 * because of "coladd" can be seen as an extra column. */
7377 if (virtual_active() && fp == &curwin->w_cursor)
7378 {
7379 char_u *p = ml_get_cursor();
7380
7381 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7382 curwin->w_virtcol - curwin->w_cursor.coladd))
7383 {
7384# ifdef FEAT_MBYTE
7385 int l;
7386
7387 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7388 col += l;
7389# else
7390 if (*p != NUL && p[1] == NUL)
7391 ++col;
7392# endif
7393 }
7394 }
7395#endif
7396 }
7397 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007398 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401/*
7402 * "confirm(message, buttons[, default [, type]])" function
7403 */
7404/*ARGSUSED*/
7405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007406f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 typval_T *argvars;
7408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7411 char_u *message;
7412 char_u *buttons = NULL;
7413 char_u buf[NUMBUFLEN];
7414 char_u buf2[NUMBUFLEN];
7415 int def = 1;
7416 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007417 char_u *typestr;
7418 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007420 message = get_tv_string_chk(&argvars[0]);
7421 if (message == NULL)
7422 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007423 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007425 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7426 if (buttons == NULL)
7427 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007431 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007433 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7434 if (typestr == NULL)
7435 error = TRUE;
7436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007438 switch (TOUPPER_ASC(*typestr))
7439 {
7440 case 'E': type = VIM_ERROR; break;
7441 case 'Q': type = VIM_QUESTION; break;
7442 case 'I': type = VIM_INFO; break;
7443 case 'W': type = VIM_WARNING; break;
7444 case 'G': type = VIM_GENERIC; break;
7445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
7447 }
7448 }
7449 }
7450
7451 if (buttons == NULL || *buttons == NUL)
7452 buttons = (char_u *)_("&Ok");
7453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007454 if (error)
7455 rettv->vval.v_number = 0;
7456 else
7457 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 def, NULL);
7459#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461#endif
7462}
7463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464/*
7465 * "copy()" function
7466 */
7467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007468f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007469 typval_T *argvars;
7470 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007472 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007476 * "count()" function
7477 */
7478 static void
7479f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 typval_T *argvars;
7481 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007482{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007483 long n = 0;
7484 int ic = FALSE;
7485
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007487 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 listitem_T *li;
7489 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007490 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007491
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 if ((l = argvars[0].vval.v_list) != NULL)
7493 {
7494 li = l->lv_first;
7495 if (argvars[2].v_type != VAR_UNKNOWN)
7496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497 int error = FALSE;
7498
7499 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500 if (argvars[3].v_type != VAR_UNKNOWN)
7501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007502 idx = get_tv_number_chk(&argvars[3], &error);
7503 if (!error)
7504 {
7505 li = list_find(l, idx);
7506 if (li == NULL)
7507 EMSGN(_(e_listidx), idx);
7508 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007509 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007510 if (error)
7511 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 }
7513
7514 for ( ; li != NULL; li = li->li_next)
7515 if (tv_equal(&li->li_tv, &argvars[1], ic))
7516 ++n;
7517 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007519 else if (argvars[0].v_type == VAR_DICT)
7520 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 int todo;
7522 dict_T *d;
7523 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007524
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 if ((d = argvars[0].vval.v_dict) != NULL)
7526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 int error = FALSE;
7528
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 if (argvars[2].v_type != VAR_UNKNOWN)
7530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007531 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007532 if (argvars[3].v_type != VAR_UNKNOWN)
7533 EMSG(_(e_invarg));
7534 }
7535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007536 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538 {
7539 if (!HASHITEM_EMPTY(hi))
7540 {
7541 --todo;
7542 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7543 ++n;
7544 }
7545 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007546 }
7547 }
7548 else
7549 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007550 rettv->vval.v_number = n;
7551}
7552
7553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7555 *
7556 * Checks the existence of a cscope connection.
7557 */
7558/*ARGSUSED*/
7559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 typval_T *argvars;
7562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563{
7564#ifdef FEAT_CSCOPE
7565 int num = 0;
7566 char_u *dbpath = NULL;
7567 char_u *prepend = NULL;
7568 char_u buf[NUMBUFLEN];
7569
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570 if (argvars[0].v_type != VAR_UNKNOWN
7571 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 num = (int)get_tv_number(&argvars[0]);
7574 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
7578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582#endif
7583}
7584
7585/*
7586 * "cursor(lnum, col)" function
7587 *
7588 * Moves the cursor to the specified line and column
7589 */
7590/*ARGSUSED*/
7591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 typval_T *argvars;
7594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 long line, col;
7597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 col = get_tv_number_chk(&argvars[1], NULL);
7600 if (line < 0 || col < 0)
7601 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (line > 0)
7603 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (col > 0)
7605 curwin->w_cursor.col = col - 1;
7606#ifdef FEAT_VIRTUALEDIT
7607 curwin->w_cursor.coladd = 0;
7608#endif
7609
7610 /* Make sure the cursor is in a valid position. */
7611 check_cursor();
7612#ifdef FEAT_MBYTE
7613 /* Correct cursor for multi-byte character. */
7614 if (has_mbyte)
7615 mb_adjust_cursor();
7616#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007617
7618 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619}
7620
7621/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 */
7624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 typval_T *argvars;
7627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007629 static int copyID = 0;
7630 int noref = 0;
7631
7632 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007633 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007634 if (noref < 0 || noref > 1)
7635 EMSG(_(e_invarg));
7636 else
7637 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638}
7639
7640/*
7641 * "delete()" function
7642 */
7643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 typval_T *argvars;
7646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652}
7653
7654/*
7655 * "did_filetype()" function
7656 */
7657/*ARGSUSED*/
7658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 typval_T *argvars;
7661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667#endif
7668}
7669
7670/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007671 * "diff_filler()" function
7672 */
7673/*ARGSUSED*/
7674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 typval_T *argvars;
7677 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007678{
7679#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007681#endif
7682}
7683
7684/*
7685 * "diff_hlID()" function
7686 */
7687/*ARGSUSED*/
7688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 typval_T *argvars;
7691 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007692{
7693#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007695 static linenr_T prev_lnum = 0;
7696 static int changedtick = 0;
7697 static int fnum = 0;
7698 static int change_start = 0;
7699 static int change_end = 0;
7700 static enum hlf_value hlID = 0;
7701 int filler_lines;
7702 int col;
7703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007704 if (lnum < 0) /* ignore type error in {lnum} arg */
7705 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007706 if (lnum != prev_lnum
7707 || changedtick != curbuf->b_changedtick
7708 || fnum != curbuf->b_fnum)
7709 {
7710 /* New line, buffer, change: need to get the values. */
7711 filler_lines = diff_check(curwin, lnum);
7712 if (filler_lines < 0)
7713 {
7714 if (filler_lines == -1)
7715 {
7716 change_start = MAXCOL;
7717 change_end = -1;
7718 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7719 hlID = HLF_ADD; /* added line */
7720 else
7721 hlID = HLF_CHD; /* changed line */
7722 }
7723 else
7724 hlID = HLF_ADD; /* added line */
7725 }
7726 else
7727 hlID = (enum hlf_value)0;
7728 prev_lnum = lnum;
7729 changedtick = curbuf->b_changedtick;
7730 fnum = curbuf->b_fnum;
7731 }
7732
7733 if (hlID == HLF_CHD || hlID == HLF_TXD)
7734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007735 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00007736 if (col >= change_start && col <= change_end)
7737 hlID = HLF_TXD; /* changed text */
7738 else
7739 hlID = HLF_CHD; /* changed line */
7740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007742#endif
7743}
7744
7745/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007746 * "empty({expr})" function
7747 */
7748 static void
7749f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 typval_T *argvars;
7751 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007752{
7753 int n;
7754
7755 switch (argvars[0].v_type)
7756 {
7757 case VAR_STRING:
7758 case VAR_FUNC:
7759 n = argvars[0].vval.v_string == NULL
7760 || *argvars[0].vval.v_string == NUL;
7761 break;
7762 case VAR_NUMBER:
7763 n = argvars[0].vval.v_number == 0;
7764 break;
7765 case VAR_LIST:
7766 n = argvars[0].vval.v_list == NULL
7767 || argvars[0].vval.v_list->lv_first == NULL;
7768 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007769 case VAR_DICT:
7770 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007772 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007773 default:
7774 EMSG2(_(e_intern2), "f_empty()");
7775 n = 0;
7776 }
7777
7778 rettv->vval.v_number = n;
7779}
7780
7781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 * "escape({string}, {chars})" function
7783 */
7784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T *argvars;
7787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 char_u buf[NUMBUFLEN];
7790
Bram Moolenaar758711c2005-02-02 23:11:38 +00007791 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7792 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794}
7795
7796/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007797 * "eval()" function
7798 */
7799/*ARGSUSED*/
7800 static void
7801f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *argvars;
7803 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007804{
7805 char_u *s;
7806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007807 s = get_tv_string_chk(&argvars[0]);
7808 if (s != NULL)
7809 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007811 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
7812 {
7813 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007814 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007815 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007816 else if (*s != NUL)
7817 EMSG(_(e_trailing));
7818}
7819
7820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 * "eventhandler()" function
7822 */
7823/*ARGSUSED*/
7824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007826 typval_T *argvars;
7827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007829 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
7832/*
7833 * "executable()" function
7834 */
7835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007837 typval_T *argvars;
7838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841}
7842
7843/*
7844 * "exists()" function
7845 */
7846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007848 typval_T *argvars;
7849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
7851 char_u *p;
7852 char_u *name;
7853 int n = FALSE;
7854 int len = 0;
7855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (*p == '$') /* environment variable */
7858 {
7859 /* first try "normal" environment variables (fast) */
7860 if (mch_getenv(p + 1) != NULL)
7861 n = TRUE;
7862 else
7863 {
7864 /* try expanding things like $VIM and ${HOME} */
7865 p = expand_env_save(p);
7866 if (p != NULL && *p != '$')
7867 n = TRUE;
7868 vim_free(p);
7869 }
7870 }
7871 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 else if (*p == '*') /* internal or user defined function */
7874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 else if (*p == ':')
7878 {
7879 n = cmd_exists(p + 1);
7880 }
7881 else if (*p == '#')
7882 {
7883#ifdef FEAT_AUTOCMD
7884 name = p + 1;
7885 p = vim_strchr(name, '#');
7886 if (p != NULL)
7887 n = au_exists(name, p, p + 1);
7888 else
7889 n = au_exists(name, name + STRLEN(name), NULL);
7890#endif
7891 }
7892 else /* internal variable */
7893 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007894 char_u *tofree;
7895 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007897 /* get_name_len() takes care of expanding curly braces */
7898 name = p;
7899 len = get_name_len(&p, &tofree, TRUE, FALSE);
7900 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007902 if (tofree != NULL)
7903 name = tofree;
7904 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7905 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007907 /* handle d.key, l[idx], f(expr) */
7908 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7909 if (n)
7910 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
7912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007914 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918}
7919
7920/*
7921 * "expand()" function
7922 */
7923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 typval_T *argvars;
7926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 char_u *s;
7929 int len;
7930 char_u *errormsg;
7931 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7932 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->v_type = VAR_STRING;
7936 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (*s == '%' || *s == '#' || *s == '<')
7938 {
7939 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 --emsg_off;
7942 }
7943 else
7944 {
7945 /* When the optional second argument is non-zero, don't remove matches
7946 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007947 if (argvars[1].v_type != VAR_UNKNOWN
7948 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007950 if (!error)
7951 {
7952 ExpandInit(&xpc);
7953 xpc.xp_context = EXPAND_FILES;
7954 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
7955 ExpandCleanup(&xpc);
7956 }
7957 else
7958 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
7960}
7961
7962/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007963 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007964 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007965 */
7966 static void
7967f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 typval_T *argvars;
7969 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007970{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007971 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007972 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007973 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 list_T *l1, *l2;
7975 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007977 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007978
Bram Moolenaare9a41262005-01-15 22:18:47 +00007979 l1 = argvars[0].vval.v_list;
7980 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007981 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7982 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 {
7984 if (argvars[2].v_type != VAR_UNKNOWN)
7985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007986 before = get_tv_number_chk(&argvars[2], &error);
7987 if (error)
7988 return; /* type error; errmsg already given */
7989
Bram Moolenaar758711c2005-02-02 23:11:38 +00007990 if (before == l1->lv_len)
7991 item = NULL;
7992 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007993 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007994 item = list_find(l1, before);
7995 if (item == NULL)
7996 {
7997 EMSGN(_(e_listidx), before);
7998 return;
7999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000 }
8001 }
8002 else
8003 item = NULL;
8004 list_extend(l1, l2, item);
8005
8006 ++l1->lv_refcount;
8007 copy_tv(&argvars[0], rettv);
8008 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008009 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8011 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 dict_T *d1, *d2;
8013 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 char_u *action;
8015 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008017 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018
8019 d1 = argvars[0].vval.v_dict;
8020 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008021 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8022 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 {
8024 /* Check the third argument. */
8025 if (argvars[2].v_type != VAR_UNKNOWN)
8026 {
8027 static char *(av[]) = {"keep", "force", "error"};
8028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 action = get_tv_string_chk(&argvars[2]);
8030 if (action == NULL)
8031 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 for (i = 0; i < 3; ++i)
8033 if (STRCMP(action, av[i]) == 0)
8034 break;
8035 if (i == 3)
8036 {
8037 EMSGN(_(e_invarg2), action);
8038 return;
8039 }
8040 }
8041 else
8042 action = (char_u *)"force";
8043
8044 /* Go over all entries in the second dict and add them to the
8045 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 todo = d2->dv_hashtab.ht_used;
8047 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008048 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008049 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008050 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008051 --todo;
8052 di1 = dict_find(d1, hi2->hi_key, -1);
8053 if (di1 == NULL)
8054 {
8055 di1 = dictitem_copy(HI2DI(hi2));
8056 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8057 dictitem_free(di1);
8058 }
8059 else if (*action == 'e')
8060 {
8061 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8062 break;
8063 }
8064 else if (*action == 'f')
8065 {
8066 clear_tv(&di1->di_tv);
8067 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8068 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 }
8070 }
8071
8072 ++d1->dv_refcount;
8073 copy_tv(&argvars[0], rettv);
8074 }
8075 }
8076 else
8077 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008078}
8079
8080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 * "filereadable()" function
8082 */
8083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008084f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 typval_T *argvars;
8086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 FILE *fd;
8089 char_u *p;
8090 int n;
8091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8094 {
8095 n = TRUE;
8096 fclose(fd);
8097 }
8098 else
8099 n = FALSE;
8100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102}
8103
8104/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008105 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 * rights to write into.
8107 */
8108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 typval_T *argvars;
8111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008113 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114}
8115
Bram Moolenaar33570922005-01-25 22:26:29 +00008116static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008117
8118 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008119findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008120 typval_T *argvars;
8121 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008122 int dir;
8123{
8124#ifdef FEAT_SEARCHPATH
8125 char_u *fname;
8126 char_u *fresult = NULL;
8127 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8128 char_u *p;
8129 char_u pathbuf[NUMBUFLEN];
8130 int count = 1;
8131 int first = TRUE;
8132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008133 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008134
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008137 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8138 if (p == NULL)
8139 count = -1; /* error */
8140 else
8141 {
8142 if (*p != NUL)
8143 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008145 if (argvars[2].v_type != VAR_UNKNOWN)
8146 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8147 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008148 }
8149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008150 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 do
8153 {
8154 vim_free(fresult);
8155 fresult = find_file_in_path_option(first ? fname : NULL,
8156 first ? (int)STRLEN(fname) : 0,
8157 0, first, path, dir, NULL);
8158 first = FALSE;
8159 } while (--count > 0 && fresult != NULL);
8160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008161
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008163#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008167}
8168
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008169static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8170static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008171static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8172static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008173
8174/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008175 * Prepare v: variable "idx" to be used.
8176 * Save the current typeval in "save_tv".
8177 * When not used yet add the variable to the v: hashtable.
8178 */
8179 static void
8180prepare_vimvar(idx, save_tv)
8181 int idx;
8182 typval_T *save_tv;
8183{
8184 *save_tv = vimvars[idx].vv_tv;
8185 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8186 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8187}
8188
8189/*
8190 * Restore v: variable "idx" to typeval "save_tv".
8191 * When no longer defined, remove the variable from the v: hashtable.
8192 */
8193 static void
8194restore_vimvar(idx, save_tv)
8195 int idx;
8196 typval_T *save_tv;
8197{
8198 hashitem_T *hi;
8199
8200 clear_tv(&vimvars[idx].vv_tv);
8201 vimvars[idx].vv_tv = *save_tv;
8202 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8203 {
8204 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8205 if (HASHITEM_EMPTY(hi))
8206 EMSG2(_(e_intern2), "restore_vimvar()");
8207 else
8208 hash_remove(&vimvarht, hi);
8209 }
8210}
8211
8212/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008213 * Implementation of map() and filter().
8214 */
8215 static void
8216filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 typval_T *argvars;
8218 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008219 int map;
8220{
8221 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008222 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008223 listitem_T *li, *nli;
8224 list_T *l = NULL;
8225 dictitem_T *di;
8226 hashtab_T *ht;
8227 hashitem_T *hi;
8228 dict_T *d = NULL;
8229 typval_T save_val;
8230 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008232 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008233 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8234
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235
8236 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008237 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008239 if ((l = argvars[0].vval.v_list) == NULL
8240 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241 return;
8242 }
8243 else if (argvars[0].v_type == VAR_DICT)
8244 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008245 if ((d = argvars[0].vval.v_dict) == NULL
8246 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008247 return;
8248 }
8249 else
8250 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008251 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008252 return;
8253 }
8254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 expr = get_tv_string_buf_chk(&argvars[1], buf);
8256 /* On type errors, the preceding call has already displayed an error
8257 * message. Avoid a misleading error message for an empty string that
8258 * was not passed as argument. */
8259 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008261 prepare_vimvar(VV_VAL, &save_val);
8262 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008264 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008266 prepare_vimvar(VV_KEY, &save_key);
8267 vimvars[VV_KEY].vv_type = VAR_STRING;
8268
8269 ht = &d->dv_hashtab;
8270 hash_lock(ht);
8271 todo = ht->ht_used;
8272 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008274 if (!HASHITEM_EMPTY(hi))
8275 {
8276 --todo;
8277 di = HI2DI(hi);
8278 if (tv_check_lock(di->di_tv.v_lock, msg))
8279 break;
8280 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8281 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8282 break;
8283 if (!map && rem)
8284 dictitem_remove(d, di);
8285 clear_tv(&vimvars[VV_KEY].vv_tv);
8286 }
8287 }
8288 hash_unlock(ht);
8289
8290 restore_vimvar(VV_KEY, &save_key);
8291 }
8292 else
8293 {
8294 for (li = l->lv_first; li != NULL; li = nli)
8295 {
8296 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008297 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298 nli = li->li_next;
8299 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008300 break;
8301 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008302 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008303 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008304 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008306 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308
8309 copy_tv(&argvars[0], rettv);
8310}
8311
8312 static int
8313filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315 char_u *expr;
8316 int map;
8317 int *remp;
8318{
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008320 char_u *s;
8321
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323 s = expr;
8324 if (eval1(&s, &rettv, TRUE) == FAIL)
8325 return FAIL;
8326 if (*s != NUL) /* check for trailing chars after expr */
8327 {
8328 EMSG2(_(e_invexpr2), s);
8329 return FAIL;
8330 }
8331 if (map)
8332 {
8333 /* map(): replace the list item value */
8334 clear_tv(tv);
8335 *tv = rettv;
8336 }
8337 else
8338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339 int error = FALSE;
8340
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008342 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008344 /* On type error, nothing has been removed; return FAIL to stop the
8345 * loop. The error message was given by get_tv_number_chk(). */
8346 if (error)
8347 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008348 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008351}
8352
8353/*
8354 * "filter()" function
8355 */
8356 static void
8357f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T *argvars;
8359 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008360{
8361 filter_map(argvars, rettv, FALSE);
8362}
8363
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008365 * "finddir({fname}[, {path}[, {count}]])" function
8366 */
8367 static void
8368f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008371{
8372 findfilendir(argvars, rettv, TRUE);
8373}
8374
8375/*
8376 * "findfile({fname}[, {path}[, {count}]])" function
8377 */
8378 static void
8379f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 typval_T *argvars;
8381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008382{
8383 findfilendir(argvars, rettv, FALSE);
8384}
8385
8386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 * "fnamemodify({fname}, {mods})" function
8388 */
8389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 typval_T *argvars;
8392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
8394 char_u *fname;
8395 char_u *mods;
8396 int usedlen = 0;
8397 int len;
8398 char_u *fbuf = NULL;
8399 char_u buf[NUMBUFLEN];
8400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008401 fname = get_tv_string_chk(&argvars[0]);
8402 mods = get_tv_string_buf_chk(&argvars[1], buf);
8403 if (fname == NULL || mods == NULL)
8404 fname = NULL;
8405 else
8406 {
8407 len = (int)STRLEN(fname);
8408 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 vim_free(fbuf);
8417}
8418
Bram Moolenaar33570922005-01-25 22:26:29 +00008419static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420
8421/*
8422 * "foldclosed()" function
8423 */
8424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 typval_T *argvars;
8427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 int end;
8429{
8430#ifdef FEAT_FOLDING
8431 linenr_T lnum;
8432 linenr_T first, last;
8433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8436 {
8437 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8438 {
8439 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008442 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 return;
8444 }
8445 }
8446#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008447 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448}
8449
8450/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008451 * "foldclosed()" function
8452 */
8453 static void
8454f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008455 typval_T *argvars;
8456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008457{
8458 foldclosed_both(argvars, rettv, FALSE);
8459}
8460
8461/*
8462 * "foldclosedend()" function
8463 */
8464 static void
8465f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *argvars;
8467 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008468{
8469 foldclosed_both(argvars, rettv, TRUE);
8470}
8471
8472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 * "foldlevel()" function
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
8480#ifdef FEAT_FOLDING
8481 linenr_T lnum;
8482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008485 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 else
8487#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489}
8490
8491/*
8492 * "foldtext()" function
8493 */
8494/*ARGSUSED*/
8495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 typval_T *argvars;
8498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500#ifdef FEAT_FOLDING
8501 linenr_T lnum;
8502 char_u *s;
8503 char_u *r;
8504 int len;
8505 char *txt;
8506#endif
8507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 rettv->v_type = VAR_STRING;
8509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8512 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8513 <= curbuf->b_ml.ml_line_count
8514 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {
8516 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008517 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8518 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
8520 if (!linewhite(lnum))
8521 break;
8522 ++lnum;
8523 }
8524
8525 /* Find interesting text in this line. */
8526 s = skipwhite(ml_get(lnum));
8527 /* skip C comment-start */
8528 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008531 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008533 {
8534 s = skipwhite(ml_get(lnum + 1));
8535 if (*s == '*')
8536 s = skipwhite(s + 1);
8537 }
8538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 txt = _("+-%s%3ld lines: ");
8540 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008541 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 + 20 /* for %3ld */
8543 + STRLEN(s))); /* concatenated */
8544 if (r != NULL)
8545 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8547 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8548 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 len = (int)STRLEN(r);
8550 STRCAT(r, s);
8551 /* remove 'foldmarker' and 'commentstring' */
8552 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 }
8555 }
8556#endif
8557}
8558
8559/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008560 * "foldtextresult(lnum)" function
8561 */
8562/*ARGSUSED*/
8563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *argvars;
8566 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008567{
8568#ifdef FEAT_FOLDING
8569 linenr_T lnum;
8570 char_u *text;
8571 char_u buf[51];
8572 foldinfo_T foldinfo;
8573 int fold_count;
8574#endif
8575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 rettv->v_type = VAR_STRING;
8577 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008578#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 /* treat illegal types and illegal string values for {lnum} the same */
8581 if (lnum < 0)
8582 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008583 fold_count = foldedCount(curwin, lnum, &foldinfo);
8584 if (fold_count > 0)
8585 {
8586 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8587 &foldinfo, buf);
8588 if (text == buf)
8589 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008591 }
8592#endif
8593}
8594
8595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 * "foreground()" function
8597 */
8598/*ARGSUSED*/
8599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *argvars;
8602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605#ifdef FEAT_GUI
8606 if (gui.in_use)
8607 gui_mch_set_foreground();
8608#else
8609# ifdef WIN32
8610 win32_set_foreground();
8611# endif
8612#endif
8613}
8614
8615/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008616 * "function()" function
8617 */
8618/*ARGSUSED*/
8619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 typval_T *argvars;
8622 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008623{
8624 char_u *s;
8625
Bram Moolenaara7043832005-01-21 11:56:39 +00008626 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008628 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008629 EMSG2(_(e_invarg2), s);
8630 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008631 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008632 else
8633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_string = vim_strsave(s);
8635 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008636 }
8637}
8638
8639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008640 * "get()" function
8641 */
8642 static void
8643f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008644 typval_T *argvars;
8645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008646{
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 listitem_T *li;
8648 list_T *l;
8649 dictitem_T *di;
8650 dict_T *d;
8651 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008652
Bram Moolenaare9a41262005-01-15 22:18:47 +00008653 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008654 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008657 int error = FALSE;
8658
8659 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
8660 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008661 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008662 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008663 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008664 else if (argvars[0].v_type == VAR_DICT)
8665 {
8666 if ((d = argvars[0].vval.v_dict) != NULL)
8667 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008668 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008669 if (di != NULL)
8670 tv = &di->di_tv;
8671 }
8672 }
8673 else
8674 EMSG2(_(e_listdictarg), "get()");
8675
8676 if (tv == NULL)
8677 {
8678 if (argvars[2].v_type == VAR_UNKNOWN)
8679 rettv->vval.v_number = 0;
8680 else
8681 copy_tv(&argvars[2], rettv);
8682 }
8683 else
8684 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008685}
8686
8687/*
8688 * "getbufvar()" function
8689 */
8690 static void
8691f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008694{
8695 buf_T *buf;
8696 buf_T *save_curbuf;
8697 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
8701 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008702 ++emsg_off;
8703 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008704
8705 rettv->v_type = VAR_STRING;
8706 rettv->vval.v_string = NULL;
8707
8708 if (buf != NULL && varname != NULL)
8709 {
8710 if (*varname == '&') /* buffer-local-option */
8711 {
8712 /* set curbuf to be our buf, temporarily */
8713 save_curbuf = curbuf;
8714 curbuf = buf;
8715
8716 get_option_tv(&varname, rettv, TRUE);
8717
8718 /* restore previous notion of curbuf */
8719 curbuf = save_curbuf;
8720 }
8721 else
8722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723 if (*varname == NUL)
8724 /* let getbufvar({nr}, "") return the "b:" dictionary. The
8725 * scope prefix before the NUL byte is required by
8726 * find_var_in_ht(). */
8727 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008729 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008730 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 }
8733 }
8734
8735 --emsg_off;
8736}
8737
8738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 * "getchar()" function
8740 */
8741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008743 typval_T *argvars;
8744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
8746 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
8749 ++no_mapping;
8750 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008751 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 /* getchar(): blocking wait. */
8753 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 /* getchar(1): only check if char avail */
8756 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008757 else if (error || vpeekc() == NUL)
8758 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 n = 0;
8760 else
8761 /* getchar(0) and char avail: return char */
8762 n = safe_vgetc();
8763 --no_mapping;
8764 --allow_keys;
8765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 if (IS_SPECIAL(n) || mod_mask != 0)
8768 {
8769 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8770 int i = 0;
8771
8772 /* Turn a special key into three bytes, plus modifier. */
8773 if (mod_mask != 0)
8774 {
8775 temp[i++] = K_SPECIAL;
8776 temp[i++] = KS_MODIFIER;
8777 temp[i++] = mod_mask;
8778 }
8779 if (IS_SPECIAL(n))
8780 {
8781 temp[i++] = K_SPECIAL;
8782 temp[i++] = K_SECOND(n);
8783 temp[i++] = K_THIRD(n);
8784 }
8785#ifdef FEAT_MBYTE
8786 else if (has_mbyte)
8787 i += (*mb_char2bytes)(n, temp + i);
8788#endif
8789 else
8790 temp[i++] = n;
8791 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->v_type = VAR_STRING;
8793 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 }
8795}
8796
8797/*
8798 * "getcharmod()" function
8799 */
8800/*ARGSUSED*/
8801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *argvars;
8804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809/*
8810 * "getcmdline()" function
8811 */
8812/*ARGSUSED*/
8813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818 rettv->v_type = VAR_STRING;
8819 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
8822/*
8823 * "getcmdpos()" function
8824 */
8825/*ARGSUSED*/
8826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008828 typval_T *argvars;
8829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 * "getcwd()" function
8836 */
8837/*ARGSUSED*/
8838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 typval_T *argvars;
8841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 char_u cwd[MAXPATHL];
8844
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 else
8849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853#endif
8854 }
8855}
8856
8857/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008858 * "getfontname()" function
8859 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008860/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 typval_T *argvars;
8864 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 rettv->v_type = VAR_STRING;
8867 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008868#ifdef FEAT_GUI
8869 if (gui.in_use)
8870 {
8871 GuiFont font;
8872 char_u *name = NULL;
8873
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008874 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008875 {
8876 /* Get the "Normal" font. Either the name saved by
8877 * hl_set_font_name() or from the font ID. */
8878 font = gui.norm_font;
8879 name = hl_get_font_name();
8880 }
8881 else
8882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008884 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8885 return;
8886 font = gui_mch_get_font(name, FALSE);
8887 if (font == NOFONT)
8888 return; /* Invalid font name, return empty string. */
8889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008891 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008892 gui_mch_free_font(font);
8893 }
8894#endif
8895}
8896
8897/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008898 * "getfperm({fname})" function
8899 */
8900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *argvars;
8903 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008904{
8905 char_u *fname;
8906 struct stat st;
8907 char_u *perm = NULL;
8908 char_u flags[] = "rwx";
8909 int i;
8910
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008914 if (mch_stat((char *)fname, &st) >= 0)
8915 {
8916 perm = vim_strsave((char_u *)"---------");
8917 if (perm != NULL)
8918 {
8919 for (i = 0; i < 9; i++)
8920 {
8921 if (st.st_mode & (1 << (8 - i)))
8922 perm[i] = flags[i % 3];
8923 }
8924 }
8925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008927}
8928
8929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 * "getfsize({fname})" function
8931 */
8932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *argvars;
8935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 char_u *fname;
8938 struct stat st;
8939
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 if (mch_stat((char *)fname, &st) >= 0)
8945 {
8946 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953}
8954
8955/*
8956 * "getftime({fname})" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 char_u *fname;
8964 struct stat st;
8965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
8968 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972}
8973
8974/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008975 * "getftype({fname})" function
8976 */
8977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 typval_T *argvars;
8980 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008981{
8982 char_u *fname;
8983 struct stat st;
8984 char_u *type = NULL;
8985 char *t;
8986
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008990 if (mch_lstat((char *)fname, &st) >= 0)
8991 {
8992#ifdef S_ISREG
8993 if (S_ISREG(st.st_mode))
8994 t = "file";
8995 else if (S_ISDIR(st.st_mode))
8996 t = "dir";
8997# ifdef S_ISLNK
8998 else if (S_ISLNK(st.st_mode))
8999 t = "link";
9000# endif
9001# ifdef S_ISBLK
9002 else if (S_ISBLK(st.st_mode))
9003 t = "bdev";
9004# endif
9005# ifdef S_ISCHR
9006 else if (S_ISCHR(st.st_mode))
9007 t = "cdev";
9008# endif
9009# ifdef S_ISFIFO
9010 else if (S_ISFIFO(st.st_mode))
9011 t = "fifo";
9012# endif
9013# ifdef S_ISSOCK
9014 else if (S_ISSOCK(st.st_mode))
9015 t = "fifo";
9016# endif
9017 else
9018 t = "other";
9019#else
9020# ifdef S_IFMT
9021 switch (st.st_mode & S_IFMT)
9022 {
9023 case S_IFREG: t = "file"; break;
9024 case S_IFDIR: t = "dir"; break;
9025# ifdef S_IFLNK
9026 case S_IFLNK: t = "link"; break;
9027# endif
9028# ifdef S_IFBLK
9029 case S_IFBLK: t = "bdev"; break;
9030# endif
9031# ifdef S_IFCHR
9032 case S_IFCHR: t = "cdev"; break;
9033# endif
9034# ifdef S_IFIFO
9035 case S_IFIFO: t = "fifo"; break;
9036# endif
9037# ifdef S_IFSOCK
9038 case S_IFSOCK: t = "socket"; break;
9039# endif
9040 default: t = "other";
9041 }
9042# else
9043 if (mch_isdir(fname))
9044 t = "dir";
9045 else
9046 t = "file";
9047# endif
9048#endif
9049 type = vim_strsave((char_u *)t);
9050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009052}
9053
9054/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 * "getline(lnum)" function
9056 */
9057 static void
9058f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061{
9062 linenr_T lnum;
9063 linenr_T end;
9064 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 list_T *l;
9066 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009067
9068 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 if (lnum < 0)
9070 rettv->vval.v_number = 0; /* failure; error message already given */
9071 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 {
9073 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9074 p = ml_get(lnum);
9075 else
9076 p = (char_u *)"";
9077
9078 rettv->v_type = VAR_STRING;
9079 rettv->vval.v_string = vim_strsave(p);
9080 }
9081 else
9082 {
9083 end = get_tv_lnum(&argvars[1]);
9084 if (end < lnum)
9085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009086 if (end >= 0) /* else: error message already given */
9087 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 rettv->vval.v_number = 0;
9089 }
9090 else
9091 {
9092 l = list_alloc();
9093 if (l != NULL)
9094 {
9095 if (lnum < 1)
9096 lnum = 1;
9097 if (end > curbuf->b_ml.ml_line_count)
9098 end = curbuf->b_ml.ml_line_count;
9099 while (lnum <= end)
9100 {
9101 li = listitem_alloc();
9102 if (li == NULL)
9103 break;
9104 list_append(l, li);
9105 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009106 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009107 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9108 }
9109 rettv->vval.v_list = l;
9110 rettv->v_type = VAR_LIST;
9111 ++l->lv_refcount;
9112 }
9113 }
9114 }
9115}
9116
9117/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009118 * "getqflist()" function
9119 */
9120/*ARGSUSED*/
9121 static void
9122f_getqflist(argvars, rettv)
9123 typval_T *argvars;
9124 typval_T *rettv;
9125{
9126#ifdef FEAT_QUICKFIX
9127 list_T *l;
9128#endif
9129
9130 rettv->vval.v_number = FALSE;
9131#ifdef FEAT_QUICKFIX
9132 l = list_alloc();
9133 if (l != NULL)
9134 {
9135 if (get_errorlist(l) != FAIL)
9136 {
9137 rettv->vval.v_list = l;
9138 rettv->v_type = VAR_LIST;
9139 ++l->lv_refcount;
9140 }
9141 else
9142 list_free(l);
9143 }
9144#endif
9145}
9146
9147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 * "getreg()" function
9149 */
9150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009152 typval_T *argvars;
9153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 char_u *strregname;
9156 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009157 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009158 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009160 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 strregname = get_tv_string_chk(&argvars[0]);
9163 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009164 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 regname = (strregname == NULL ? '"' : *strregname);
9170 if (regname == 0)
9171 regname = '"';
9172
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 rettv->vval.v_string = error ? NULL :
9175 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176}
9177
9178/*
9179 * "getregtype()" function
9180 */
9181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009183 typval_T *argvars;
9184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 char_u *strregname;
9187 int regname;
9188 char_u buf[NUMBUFLEN + 2];
9189 long reglen = 0;
9190
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009191 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 {
9193 strregname = get_tv_string_chk(&argvars[0]);
9194 if (strregname == NULL) /* type error; errmsg already given */
9195 {
9196 rettv->v_type = VAR_STRING;
9197 rettv->vval.v_string = NULL;
9198 return;
9199 }
9200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 else
9202 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009203 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
9205 regname = (strregname == NULL ? '"' : *strregname);
9206 if (regname == 0)
9207 regname = '"';
9208
9209 buf[0] = NUL;
9210 buf[1] = NUL;
9211 switch (get_reg_type(regname, &reglen))
9212 {
9213 case MLINE: buf[0] = 'V'; break;
9214 case MCHAR: buf[0] = 'v'; break;
9215#ifdef FEAT_VISUAL
9216 case MBLOCK:
9217 buf[0] = Ctrl_V;
9218 sprintf((char *)buf + 1, "%ld", reglen + 1);
9219 break;
9220#endif
9221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->v_type = VAR_STRING;
9223 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224}
9225
9226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 * "getwinposx()" function
9228 */
9229/*ARGSUSED*/
9230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236#ifdef FEAT_GUI
9237 if (gui.in_use)
9238 {
9239 int x, y;
9240
9241 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 }
9244#endif
9245}
9246
9247/*
9248 * "getwinposy()" function
9249 */
9250/*ARGSUSED*/
9251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009253 typval_T *argvars;
9254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257#ifdef FEAT_GUI
9258 if (gui.in_use)
9259 {
9260 int x, y;
9261
9262 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
9265#endif
9266}
9267
9268/*
9269 * "getwinvar()" function
9270 */
9271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009272f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 typval_T *argvars;
9274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 win_T *win, *oldcurwin;
9277 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 varname = get_tv_string_chk(&argvars[1]);
9282 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->v_type = VAR_STRING;
9285 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286
9287 if (win != NULL && varname != NULL)
9288 {
9289 if (*varname == '&') /* window-local-option */
9290 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009291 /* Set curwin to be our win, temporarily. Also set curbuf, so
9292 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 oldcurwin = curwin;
9294 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009295 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009297 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
9299 /* restore previous notion of curwin */
9300 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009301 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303 else
9304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 if (*varname == NUL)
9306 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9307 * scope prefix before the NUL byte is required by
9308 * find_var_in_ht(). */
9309 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009311 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 }
9315 }
9316
9317 --emsg_off;
9318}
9319
9320/*
9321 * "glob()" function
9322 */
9323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 typval_T *argvars;
9326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 expand_T xpc;
9329
9330 ExpandInit(&xpc);
9331 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->v_type = VAR_STRING;
9333 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9335 ExpandCleanup(&xpc);
9336}
9337
9338/*
9339 * "globpath()" function
9340 */
9341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 typval_T *argvars;
9344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009347 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009350 if (file == NULL)
9351 rettv->vval.v_string = NULL;
9352 else
9353 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354}
9355
9356/*
9357 * "has()" function
9358 */
9359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009361 typval_T *argvars;
9362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 int i;
9365 char_u *name;
9366 int n = FALSE;
9367 static char *(has_list[]) =
9368 {
9369#ifdef AMIGA
9370 "amiga",
9371# ifdef FEAT_ARP
9372 "arp",
9373# endif
9374#endif
9375#ifdef __BEOS__
9376 "beos",
9377#endif
9378#ifdef MSDOS
9379# ifdef DJGPP
9380 "dos32",
9381# else
9382 "dos16",
9383# endif
9384#endif
9385#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9386 "mac",
9387#endif
9388#if defined(MACOS_X_UNIX)
9389 "macunix",
9390#endif
9391#ifdef OS2
9392 "os2",
9393#endif
9394#ifdef __QNX__
9395 "qnx",
9396#endif
9397#ifdef RISCOS
9398 "riscos",
9399#endif
9400#ifdef UNIX
9401 "unix",
9402#endif
9403#ifdef VMS
9404 "vms",
9405#endif
9406#ifdef WIN16
9407 "win16",
9408#endif
9409#ifdef WIN32
9410 "win32",
9411#endif
9412#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9413 "win32unix",
9414#endif
9415#ifdef WIN64
9416 "win64",
9417#endif
9418#ifdef EBCDIC
9419 "ebcdic",
9420#endif
9421#ifndef CASE_INSENSITIVE_FILENAME
9422 "fname_case",
9423#endif
9424#ifdef FEAT_ARABIC
9425 "arabic",
9426#endif
9427#ifdef FEAT_AUTOCMD
9428 "autocmd",
9429#endif
9430#ifdef FEAT_BEVAL
9431 "balloon_eval",
9432#endif
9433#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9434 "builtin_terms",
9435# ifdef ALL_BUILTIN_TCAPS
9436 "all_builtin_terms",
9437# endif
9438#endif
9439#ifdef FEAT_BYTEOFF
9440 "byte_offset",
9441#endif
9442#ifdef FEAT_CINDENT
9443 "cindent",
9444#endif
9445#ifdef FEAT_CLIENTSERVER
9446 "clientserver",
9447#endif
9448#ifdef FEAT_CLIPBOARD
9449 "clipboard",
9450#endif
9451#ifdef FEAT_CMDL_COMPL
9452 "cmdline_compl",
9453#endif
9454#ifdef FEAT_CMDHIST
9455 "cmdline_hist",
9456#endif
9457#ifdef FEAT_COMMENTS
9458 "comments",
9459#endif
9460#ifdef FEAT_CRYPT
9461 "cryptv",
9462#endif
9463#ifdef FEAT_CSCOPE
9464 "cscope",
9465#endif
9466#ifdef DEBUG
9467 "debug",
9468#endif
9469#ifdef FEAT_CON_DIALOG
9470 "dialog_con",
9471#endif
9472#ifdef FEAT_GUI_DIALOG
9473 "dialog_gui",
9474#endif
9475#ifdef FEAT_DIFF
9476 "diff",
9477#endif
9478#ifdef FEAT_DIGRAPHS
9479 "digraphs",
9480#endif
9481#ifdef FEAT_DND
9482 "dnd",
9483#endif
9484#ifdef FEAT_EMACS_TAGS
9485 "emacs_tags",
9486#endif
9487 "eval", /* always present, of course! */
9488#ifdef FEAT_EX_EXTRA
9489 "ex_extra",
9490#endif
9491#ifdef FEAT_SEARCH_EXTRA
9492 "extra_search",
9493#endif
9494#ifdef FEAT_FKMAP
9495 "farsi",
9496#endif
9497#ifdef FEAT_SEARCHPATH
9498 "file_in_path",
9499#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009500#if defined(UNIX) && !defined(USE_SYSTEM)
9501 "filterpipe",
9502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#ifdef FEAT_FIND_ID
9504 "find_in_path",
9505#endif
9506#ifdef FEAT_FOLDING
9507 "folding",
9508#endif
9509#ifdef FEAT_FOOTER
9510 "footer",
9511#endif
9512#if !defined(USE_SYSTEM) && defined(UNIX)
9513 "fork",
9514#endif
9515#ifdef FEAT_GETTEXT
9516 "gettext",
9517#endif
9518#ifdef FEAT_GUI
9519 "gui",
9520#endif
9521#ifdef FEAT_GUI_ATHENA
9522# ifdef FEAT_GUI_NEXTAW
9523 "gui_neXtaw",
9524# else
9525 "gui_athena",
9526# endif
9527#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009528#ifdef FEAT_GUI_KDE
9529 "gui_kde",
9530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531#ifdef FEAT_GUI_GTK
9532 "gui_gtk",
9533# ifdef HAVE_GTK2
9534 "gui_gtk2",
9535# endif
9536#endif
9537#ifdef FEAT_GUI_MAC
9538 "gui_mac",
9539#endif
9540#ifdef FEAT_GUI_MOTIF
9541 "gui_motif",
9542#endif
9543#ifdef FEAT_GUI_PHOTON
9544 "gui_photon",
9545#endif
9546#ifdef FEAT_GUI_W16
9547 "gui_win16",
9548#endif
9549#ifdef FEAT_GUI_W32
9550 "gui_win32",
9551#endif
9552#ifdef FEAT_HANGULIN
9553 "hangul_input",
9554#endif
9555#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9556 "iconv",
9557#endif
9558#ifdef FEAT_INS_EXPAND
9559 "insert_expand",
9560#endif
9561#ifdef FEAT_JUMPLIST
9562 "jumplist",
9563#endif
9564#ifdef FEAT_KEYMAP
9565 "keymap",
9566#endif
9567#ifdef FEAT_LANGMAP
9568 "langmap",
9569#endif
9570#ifdef FEAT_LIBCALL
9571 "libcall",
9572#endif
9573#ifdef FEAT_LINEBREAK
9574 "linebreak",
9575#endif
9576#ifdef FEAT_LISP
9577 "lispindent",
9578#endif
9579#ifdef FEAT_LISTCMDS
9580 "listcmds",
9581#endif
9582#ifdef FEAT_LOCALMAP
9583 "localmap",
9584#endif
9585#ifdef FEAT_MENU
9586 "menu",
9587#endif
9588#ifdef FEAT_SESSION
9589 "mksession",
9590#endif
9591#ifdef FEAT_MODIFY_FNAME
9592 "modify_fname",
9593#endif
9594#ifdef FEAT_MOUSE
9595 "mouse",
9596#endif
9597#ifdef FEAT_MOUSESHAPE
9598 "mouseshape",
9599#endif
9600#if defined(UNIX) || defined(VMS)
9601# ifdef FEAT_MOUSE_DEC
9602 "mouse_dec",
9603# endif
9604# ifdef FEAT_MOUSE_GPM
9605 "mouse_gpm",
9606# endif
9607# ifdef FEAT_MOUSE_JSB
9608 "mouse_jsbterm",
9609# endif
9610# ifdef FEAT_MOUSE_NET
9611 "mouse_netterm",
9612# endif
9613# ifdef FEAT_MOUSE_PTERM
9614 "mouse_pterm",
9615# endif
9616# ifdef FEAT_MOUSE_XTERM
9617 "mouse_xterm",
9618# endif
9619#endif
9620#ifdef FEAT_MBYTE
9621 "multi_byte",
9622#endif
9623#ifdef FEAT_MBYTE_IME
9624 "multi_byte_ime",
9625#endif
9626#ifdef FEAT_MULTI_LANG
9627 "multi_lang",
9628#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009629#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009630#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009631 "mzscheme",
9632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634#ifdef FEAT_OLE
9635 "ole",
9636#endif
9637#ifdef FEAT_OSFILETYPE
9638 "osfiletype",
9639#endif
9640#ifdef FEAT_PATH_EXTRA
9641 "path_extra",
9642#endif
9643#ifdef FEAT_PERL
9644#ifndef DYNAMIC_PERL
9645 "perl",
9646#endif
9647#endif
9648#ifdef FEAT_PYTHON
9649#ifndef DYNAMIC_PYTHON
9650 "python",
9651#endif
9652#endif
9653#ifdef FEAT_POSTSCRIPT
9654 "postscript",
9655#endif
9656#ifdef FEAT_PRINTER
9657 "printer",
9658#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009659#ifdef FEAT_PROFILE
9660 "profile",
9661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662#ifdef FEAT_QUICKFIX
9663 "quickfix",
9664#endif
9665#ifdef FEAT_RIGHTLEFT
9666 "rightleft",
9667#endif
9668#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9669 "ruby",
9670#endif
9671#ifdef FEAT_SCROLLBIND
9672 "scrollbind",
9673#endif
9674#ifdef FEAT_CMDL_INFO
9675 "showcmd",
9676 "cmdline_info",
9677#endif
9678#ifdef FEAT_SIGNS
9679 "signs",
9680#endif
9681#ifdef FEAT_SMARTINDENT
9682 "smartindent",
9683#endif
9684#ifdef FEAT_SNIFF
9685 "sniff",
9686#endif
9687#ifdef FEAT_STL_OPT
9688 "statusline",
9689#endif
9690#ifdef FEAT_SUN_WORKSHOP
9691 "sun_workshop",
9692#endif
9693#ifdef FEAT_NETBEANS_INTG
9694 "netbeans_intg",
9695#endif
9696#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009697 "spell",
9698#endif
9699#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 "syntax",
9701#endif
9702#if defined(USE_SYSTEM) || !defined(UNIX)
9703 "system",
9704#endif
9705#ifdef FEAT_TAG_BINS
9706 "tag_binary",
9707#endif
9708#ifdef FEAT_TAG_OLDSTATIC
9709 "tag_old_static",
9710#endif
9711#ifdef FEAT_TAG_ANYWHITE
9712 "tag_any_white",
9713#endif
9714#ifdef FEAT_TCL
9715# ifndef DYNAMIC_TCL
9716 "tcl",
9717# endif
9718#endif
9719#ifdef TERMINFO
9720 "terminfo",
9721#endif
9722#ifdef FEAT_TERMRESPONSE
9723 "termresponse",
9724#endif
9725#ifdef FEAT_TEXTOBJ
9726 "textobjects",
9727#endif
9728#ifdef HAVE_TGETENT
9729 "tgetent",
9730#endif
9731#ifdef FEAT_TITLE
9732 "title",
9733#endif
9734#ifdef FEAT_TOOLBAR
9735 "toolbar",
9736#endif
9737#ifdef FEAT_USR_CMDS
9738 "user-commands", /* was accidentally included in 5.4 */
9739 "user_commands",
9740#endif
9741#ifdef FEAT_VIMINFO
9742 "viminfo",
9743#endif
9744#ifdef FEAT_VERTSPLIT
9745 "vertsplit",
9746#endif
9747#ifdef FEAT_VIRTUALEDIT
9748 "virtualedit",
9749#endif
9750#ifdef FEAT_VISUAL
9751 "visual",
9752#endif
9753#ifdef FEAT_VISUALEXTRA
9754 "visualextra",
9755#endif
9756#ifdef FEAT_VREPLACE
9757 "vreplace",
9758#endif
9759#ifdef FEAT_WILDIGN
9760 "wildignore",
9761#endif
9762#ifdef FEAT_WILDMENU
9763 "wildmenu",
9764#endif
9765#ifdef FEAT_WINDOWS
9766 "windows",
9767#endif
9768#ifdef FEAT_WAK
9769 "winaltkeys",
9770#endif
9771#ifdef FEAT_WRITEBACKUP
9772 "writebackup",
9773#endif
9774#ifdef FEAT_XIM
9775 "xim",
9776#endif
9777#ifdef FEAT_XFONTSET
9778 "xfontset",
9779#endif
9780#ifdef USE_XSMP
9781 "xsmp",
9782#endif
9783#ifdef USE_XSMP_INTERACT
9784 "xsmp_interact",
9785#endif
9786#ifdef FEAT_XCLIPBOARD
9787 "xterm_clipboard",
9788#endif
9789#ifdef FEAT_XTERM_SAVE
9790 "xterm_save",
9791#endif
9792#if defined(UNIX) && defined(FEAT_X11)
9793 "X11",
9794#endif
9795 NULL
9796 };
9797
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 for (i = 0; has_list[i] != NULL; ++i)
9800 if (STRICMP(name, has_list[i]) == 0)
9801 {
9802 n = TRUE;
9803 break;
9804 }
9805
9806 if (n == FALSE)
9807 {
9808 if (STRNICMP(name, "patch", 5) == 0)
9809 n = has_patch(atoi((char *)name + 5));
9810 else if (STRICMP(name, "vim_starting") == 0)
9811 n = (starting != 0);
9812#ifdef DYNAMIC_TCL
9813 else if (STRICMP(name, "tcl") == 0)
9814 n = tcl_enabled(FALSE);
9815#endif
9816#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9817 else if (STRICMP(name, "iconv") == 0)
9818 n = iconv_enabled(FALSE);
9819#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009820#ifdef DYNAMIC_MZSCHEME
9821 else if (STRICMP(name, "mzscheme") == 0)
9822 n = mzscheme_enabled(FALSE);
9823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824#ifdef DYNAMIC_RUBY
9825 else if (STRICMP(name, "ruby") == 0)
9826 n = ruby_enabled(FALSE);
9827#endif
9828#ifdef DYNAMIC_PYTHON
9829 else if (STRICMP(name, "python") == 0)
9830 n = python_enabled(FALSE);
9831#endif
9832#ifdef DYNAMIC_PERL
9833 else if (STRICMP(name, "perl") == 0)
9834 n = perl_enabled(FALSE);
9835#endif
9836#ifdef FEAT_GUI
9837 else if (STRICMP(name, "gui_running") == 0)
9838 n = (gui.in_use || gui.starting);
9839# ifdef FEAT_GUI_W32
9840 else if (STRICMP(name, "gui_win32s") == 0)
9841 n = gui_is_win32s();
9842# endif
9843# ifdef FEAT_BROWSE
9844 else if (STRICMP(name, "browse") == 0)
9845 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9846# endif
9847#endif
9848#ifdef FEAT_SYN_HL
9849 else if (STRICMP(name, "syntax_items") == 0)
9850 n = syntax_present(curbuf);
9851#endif
9852#if defined(WIN3264)
9853 else if (STRICMP(name, "win95") == 0)
9854 n = mch_windows95();
9855#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009856#ifdef FEAT_NETBEANS_INTG
9857 else if (STRICMP(name, "netbeans_enabled") == 0)
9858 n = usingNetbeans;
9859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 }
9861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863}
9864
9865/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009866 * "has_key()" function
9867 */
9868 static void
9869f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 typval_T *argvars;
9871 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009872{
9873 rettv->vval.v_number = 0;
9874 if (argvars[0].v_type != VAR_DICT)
9875 {
9876 EMSG(_(e_dictreq));
9877 return;
9878 }
9879 if (argvars[0].vval.v_dict == NULL)
9880 return;
9881
9882 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009883 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884}
9885
9886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 * "hasmapto()" function
9888 */
9889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 typval_T *argvars;
9892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894 char_u *name;
9895 char_u *mode;
9896 char_u buf[NUMBUFLEN];
9897
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009899 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 mode = (char_u *)"nvo";
9901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
9904 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908}
9909
9910/*
9911 * "histadd()" function
9912 */
9913/*ARGSUSED*/
9914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009916 typval_T *argvars;
9917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
9919#ifdef FEAT_CMDHIST
9920 int histype;
9921 char_u *str;
9922 char_u buf[NUMBUFLEN];
9923#endif
9924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (check_restricted() || check_secure())
9927 return;
9928#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
9930 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 if (histype >= 0)
9932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 if (*str != NUL)
9935 {
9936 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 return;
9939 }
9940 }
9941#endif
9942}
9943
9944/*
9945 * "histdel()" function
9946 */
9947/*ARGSUSED*/
9948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009950 typval_T *argvars;
9951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952{
9953#ifdef FEAT_CMDHIST
9954 int n;
9955 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
9959 if (str == NULL)
9960 n = 0;
9961 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009963 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009964 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 else
9969 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 get_tv_string_buf(&argvars[1], buf));
9972 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975#endif
9976}
9977
9978/*
9979 * "histget()" function
9980 */
9981/*ARGSUSED*/
9982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *argvars;
9985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987#ifdef FEAT_CMDHIST
9988 int type;
9989 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
9993 if (str == NULL)
9994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 {
9997 type = get_histtype(str);
9998 if (argvars[1].v_type == VAR_UNKNOWN)
9999 idx = get_history_idx(type);
10000 else
10001 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10002 /* -1 on type error */
10003 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009}
10010
10011/*
10012 * "histnr()" function
10013 */
10014/*ARGSUSED*/
10015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010017 typval_T *argvars;
10018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020 int i;
10021
10022#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010023 char_u *history = get_tv_string_chk(&argvars[0]);
10024
10025 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (i >= HIST_CMD && i < HIST_COUNT)
10027 i = get_history_idx(i);
10028 else
10029#endif
10030 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032}
10033
10034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 * "highlightID(name)" function
10036 */
10037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *argvars;
10040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043}
10044
10045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010046 * "highlight_exists()" function
10047 */
10048 static void
10049f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010052{
10053 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10054}
10055
10056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 * "hostname()" function
10058 */
10059/*ARGSUSED*/
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
10065 char_u hostname[256];
10066
10067 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->v_type = VAR_STRING;
10069 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070}
10071
10072/*
10073 * iconv() function
10074 */
10075/*ARGSUSED*/
10076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010078 typval_T *argvars;
10079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
10081#ifdef FEAT_MBYTE
10082 char_u buf1[NUMBUFLEN];
10083 char_u buf2[NUMBUFLEN];
10084 char_u *from, *to, *str;
10085 vimconv_T vimconv;
10086#endif
10087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 rettv->v_type = VAR_STRING;
10089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
10091#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010092 str = get_tv_string(&argvars[0]);
10093 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10094 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 vimconv.vc_type = CONV_NONE;
10096 convert_setup(&vimconv, from, to);
10097
10098 /* If the encodings are equal, no conversion needed. */
10099 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103
10104 convert_setup(&vimconv, NULL, NULL);
10105 vim_free(from);
10106 vim_free(to);
10107#endif
10108}
10109
10110/*
10111 * "indent()" function
10112 */
10113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010115 typval_T *argvars;
10116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118 linenr_T lnum;
10119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125}
10126
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010127/*
10128 * "index()" function
10129 */
10130 static void
10131f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010132 typval_T *argvars;
10133 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010134{
Bram Moolenaar33570922005-01-25 22:26:29 +000010135 list_T *l;
10136 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010137 long idx = 0;
10138 int ic = FALSE;
10139
10140 rettv->vval.v_number = -1;
10141 if (argvars[0].v_type != VAR_LIST)
10142 {
10143 EMSG(_(e_listreq));
10144 return;
10145 }
10146 l = argvars[0].vval.v_list;
10147 if (l != NULL)
10148 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010149 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 int error = FALSE;
10153
Bram Moolenaar758711c2005-02-02 23:11:38 +000010154 /* Start at specified item. Use the cached index that list_find()
10155 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010157 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010158 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 ic = get_tv_number_chk(&argvars[3], &error);
10160 if (error)
10161 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010162 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010163
Bram Moolenaar758711c2005-02-02 23:11:38 +000010164 for ( ; item != NULL; item = item->li_next, ++idx)
10165 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010166 {
10167 rettv->vval.v_number = idx;
10168 break;
10169 }
10170 }
10171}
10172
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173static int inputsecret_flag = 0;
10174
10175/*
10176 * "input()" function
10177 * Also handles inputsecret() when inputsecret is set.
10178 */
10179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010181 typval_T *argvars;
10182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 char_u *p = NULL;
10186 int c;
10187 char_u buf[NUMBUFLEN];
10188 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
10193#ifdef NO_CONSOLE_INPUT
10194 /* While starting up, there is no place to enter text. */
10195 if (no_console_input())
10196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 return;
10199 }
10200#endif
10201
10202 cmd_silent = FALSE; /* Want to see the prompt. */
10203 if (prompt != NULL)
10204 {
10205 /* Only the part of the message after the last NL is considered as
10206 * prompt for the command line */
10207 p = vim_strrchr(prompt, '\n');
10208 if (p == NULL)
10209 p = prompt;
10210 else
10211 {
10212 ++p;
10213 c = *p;
10214 *p = NUL;
10215 msg_start();
10216 msg_clr_eos();
10217 msg_puts_attr(prompt, echo_attr);
10218 msg_didout = FALSE;
10219 msg_starthere();
10220 *p = c;
10221 }
10222 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 if (argvars[1].v_type != VAR_UNKNOWN)
10225 {
10226 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10227 if (defstr != NULL)
10228 stuffReadbuffSpec(defstr);
10229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 if (defstr != NULL)
10232 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 /* since the user typed this, no need to wait for return */
10236 need_wait_return = FALSE;
10237 msg_didout = FALSE;
10238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 cmd_silent = cmd_silent_save;
10240}
10241
10242/*
10243 * "inputdialog()" function
10244 */
10245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 typval_T *argvars;
10248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250#if defined(FEAT_GUI_TEXTDIALOG)
10251 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10252 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10253 {
10254 char_u *message;
10255 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010258 message = get_tv_string_chk(&argvars[0]);
10259 if (argvars[1].v_type != VAR_UNKNOWN
10260 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 STRNCPY(IObuff, defstr, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 IObuff[IOSIZE - 1] = NUL;
10264 }
10265 else
10266 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 if (message != NULL && defstr != NULL
10268 && do_dialog(VIM_QUESTION, NULL, message,
10269 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 else
10272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010273 if (message != NULL && defstr != NULL
10274 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010275 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010276 rettv->vval.v_string = vim_strsave(
10277 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 }
10283 else
10284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287
10288static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10289
10290/*
10291 * "inputrestore()" function
10292 */
10293/*ARGSUSED*/
10294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010296 typval_T *argvars;
10297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299 if (ga_userinput.ga_len > 0)
10300 {
10301 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10303 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 }
10306 else if (p_verbose > 1)
10307 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010308 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 }
10311}
10312
10313/*
10314 * "inputsave()" function
10315 */
10316/*ARGSUSED*/
10317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010319 typval_T *argvars;
10320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
10322 /* Add an entry to the stack of typehead storage. */
10323 if (ga_grow(&ga_userinput, 1) == OK)
10324 {
10325 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10326 + ga_userinput.ga_len);
10327 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 }
10330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332}
10333
10334/*
10335 * "inputsecret()" function
10336 */
10337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010339 typval_T *argvars;
10340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
10342 ++cmdline_star;
10343 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 --cmdline_star;
10346 --inputsecret_flag;
10347}
10348
10349/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010350 * "insert()" function
10351 */
10352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010353f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010354 typval_T *argvars;
10355 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010356{
10357 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 listitem_T *item;
10359 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010360 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010361
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010362 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010363 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010364 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010365 else if ((l = argvars[0].vval.v_list) != NULL
10366 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010367 {
10368 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010369 before = get_tv_number_chk(&argvars[2], &error);
10370 if (error)
10371 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010372
Bram Moolenaar758711c2005-02-02 23:11:38 +000010373 if (before == l->lv_len)
10374 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010375 else
10376 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010377 item = list_find(l, before);
10378 if (item == NULL)
10379 {
10380 EMSGN(_(e_listidx), before);
10381 l = NULL;
10382 }
10383 }
10384 if (l != NULL)
10385 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010386 list_insert_tv(l, &argvars[1], item);
10387 ++l->lv_refcount;
10388 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010389 }
10390 }
10391}
10392
10393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 * "isdirectory()" function
10395 */
10396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *argvars;
10399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402}
10403
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010404/*
10405 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10406 * it refers to a List or Dictionary that is locked.
10407 */
10408 static int
10409tv_islocked(tv)
10410 typval_T *tv;
10411{
10412 return (tv->v_lock & VAR_LOCKED)
10413 || (tv->v_type == VAR_LIST
10414 && tv->vval.v_list != NULL
10415 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10416 || (tv->v_type == VAR_DICT
10417 && tv->vval.v_dict != NULL
10418 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10419}
10420
10421/*
10422 * "islocked()" function
10423 */
10424 static void
10425f_islocked(argvars, rettv)
10426 typval_T *argvars;
10427 typval_T *rettv;
10428{
10429 lval_T lv;
10430 char_u *end;
10431 dictitem_T *di;
10432
10433 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010434 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10435 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010436 if (end != NULL && lv.ll_name != NULL)
10437 {
10438 if (*end != NUL)
10439 EMSG(_(e_trailing));
10440 else
10441 {
10442 if (lv.ll_tv == NULL)
10443 {
10444 if (check_changedtick(lv.ll_name))
10445 rettv->vval.v_number = 1; /* always locked */
10446 else
10447 {
10448 di = find_var(lv.ll_name, NULL);
10449 if (di != NULL)
10450 {
10451 /* Consider a variable locked when:
10452 * 1. the variable itself is locked
10453 * 2. the value of the variable is locked.
10454 * 3. the List or Dict value is locked.
10455 */
10456 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10457 || tv_islocked(&di->di_tv));
10458 }
10459 }
10460 }
10461 else if (lv.ll_range)
10462 EMSG(_("E745: Range not allowed"));
10463 else if (lv.ll_newkey != NULL)
10464 EMSG2(_(e_dictkey), lv.ll_newkey);
10465 else if (lv.ll_list != NULL)
10466 /* List item. */
10467 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10468 else
10469 /* Dictionary item. */
10470 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10471 }
10472 }
10473
10474 clear_lval(&lv);
10475}
10476
Bram Moolenaar33570922005-01-25 22:26:29 +000010477static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010478
10479/*
10480 * Turn a dict into a list:
10481 * "what" == 0: list of keys
10482 * "what" == 1: list of values
10483 * "what" == 2: list of items
10484 */
10485 static void
10486dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 typval_T *argvars;
10488 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010489 int what;
10490{
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 list_T *l;
10492 list_T *l2;
10493 dictitem_T *di;
10494 hashitem_T *hi;
10495 listitem_T *li;
10496 listitem_T *li2;
10497 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010498 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010499
10500 rettv->vval.v_number = 0;
10501 if (argvars[0].v_type != VAR_DICT)
10502 {
10503 EMSG(_(e_dictreq));
10504 return;
10505 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010506 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010507 return;
10508
10509 l = list_alloc();
10510 if (l == NULL)
10511 return;
10512 rettv->v_type = VAR_LIST;
10513 rettv->vval.v_list = l;
10514 ++l->lv_refcount;
10515
Bram Moolenaar33570922005-01-25 22:26:29 +000010516 todo = d->dv_hashtab.ht_used;
10517 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010519 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010521 --todo;
10522 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010523
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010524 li = listitem_alloc();
10525 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010526 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010527 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010529 if (what == 0)
10530 {
10531 /* keys() */
10532 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010533 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010534 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10535 }
10536 else if (what == 1)
10537 {
10538 /* values() */
10539 copy_tv(&di->di_tv, &li->li_tv);
10540 }
10541 else
10542 {
10543 /* items() */
10544 l2 = list_alloc();
10545 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010546 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010547 li->li_tv.vval.v_list = l2;
10548 if (l2 == NULL)
10549 break;
10550 ++l2->lv_refcount;
10551
10552 li2 = listitem_alloc();
10553 if (li2 == NULL)
10554 break;
10555 list_append(l2, li2);
10556 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010557 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010558 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10559
10560 li2 = listitem_alloc();
10561 if (li2 == NULL)
10562 break;
10563 list_append(l2, li2);
10564 copy_tv(&di->di_tv, &li2->li_tv);
10565 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010566 }
10567 }
10568}
10569
10570/*
10571 * "items(dict)" function
10572 */
10573 static void
10574f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *argvars;
10576 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010577{
10578 dict_list(argvars, rettv, 2);
10579}
10580
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010582 * "join()" function
10583 */
10584 static void
10585f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588{
10589 garray_T ga;
10590 char_u *sep;
10591
10592 rettv->vval.v_number = 0;
10593 if (argvars[0].v_type != VAR_LIST)
10594 {
10595 EMSG(_(e_listreq));
10596 return;
10597 }
10598 if (argvars[0].vval.v_list == NULL)
10599 return;
10600 if (argvars[1].v_type == VAR_UNKNOWN)
10601 sep = (char_u *)" ";
10602 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010604
10605 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606
10607 if (sep != NULL)
10608 {
10609 ga_init2(&ga, (int)sizeof(char), 80);
10610 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10611 ga_append(&ga, NUL);
10612 rettv->vval.v_string = (char_u *)ga.ga_data;
10613 }
10614 else
10615 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010616}
10617
10618/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010619 * "keys()" function
10620 */
10621 static void
10622f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010623 typval_T *argvars;
10624 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010625{
10626 dict_list(argvars, rettv, 0);
10627}
10628
10629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 * "last_buffer_nr()" function.
10631 */
10632/*ARGSUSED*/
10633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T *argvars;
10636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 int n = 0;
10639 buf_T *buf;
10640
10641 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10642 if (n < buf->b_fnum)
10643 n = buf->b_fnum;
10644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010646}
10647
10648/*
10649 * "len()" function
10650 */
10651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 typval_T *argvars;
10654 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010655{
10656 switch (argvars[0].v_type)
10657 {
10658 case VAR_STRING:
10659 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->vval.v_number = (varnumber_T)STRLEN(
10661 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010662 break;
10663 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010665 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 case VAR_DICT:
10667 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10668 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010669 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010670 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010671 break;
10672 }
10673}
10674
Bram Moolenaar33570922005-01-25 22:26:29 +000010675static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010676
10677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010679 typval_T *argvars;
10680 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010681 int type;
10682{
10683#ifdef FEAT_LIBCALL
10684 char_u *string_in;
10685 char_u **string_result;
10686 int nr_result;
10687#endif
10688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010690 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010694
10695 if (check_restricted() || check_secure())
10696 return;
10697
10698#ifdef FEAT_LIBCALL
10699 /* The first two args must be strings, otherwise its meaningless */
10700 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10701 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010702 string_in = NULL;
10703 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010704 string_in = argvars[2].vval.v_string;
10705 if (type == VAR_NUMBER)
10706 string_result = NULL;
10707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010709 if (mch_libcall(argvars[0].vval.v_string,
10710 argvars[1].vval.v_string,
10711 string_in,
10712 argvars[2].vval.v_number,
10713 string_result,
10714 &nr_result) == OK
10715 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010716 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010717 }
10718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719}
10720
10721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010722 * "libcall()" function
10723 */
10724 static void
10725f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010728{
10729 libcall_common(argvars, rettv, VAR_STRING);
10730}
10731
10732/*
10733 * "libcallnr()" function
10734 */
10735 static void
10736f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010739{
10740 libcall_common(argvars, rettv, VAR_NUMBER);
10741}
10742
10743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 * "line(string)" function
10745 */
10746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *argvars;
10749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750{
10751 linenr_T lnum = 0;
10752 pos_T *fp;
10753
10754 fp = var2fpos(&argvars[0], TRUE);
10755 if (fp != NULL)
10756 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758}
10759
10760/*
10761 * "line2byte(lnum)" function
10762 */
10763/*ARGSUSED*/
10764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010766 typval_T *argvars;
10767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768{
10769#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771#else
10772 linenr_T lnum;
10773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10779 if (rettv->vval.v_number >= 0)
10780 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781#endif
10782}
10783
10784/*
10785 * "lispindent(lnum)" function
10786 */
10787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *argvars;
10790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791{
10792#ifdef FEAT_LISP
10793 pos_T pos;
10794 linenr_T lnum;
10795
10796 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10799 {
10800 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 curwin->w_cursor = pos;
10803 }
10804 else
10805#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807}
10808
10809/*
10810 * "localtime()" function
10811 */
10812/*ARGSUSED*/
10813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010815 typval_T *argvars;
10816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819}
10820
Bram Moolenaar33570922005-01-25 22:26:29 +000010821static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822
10823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 typval_T *argvars;
10826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 int exact;
10828{
10829 char_u *keys;
10830 char_u *which;
10831 char_u buf[NUMBUFLEN];
10832 char_u *keys_buf = NULL;
10833 char_u *rhs;
10834 int mode;
10835 garray_T ga;
10836
10837 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->v_type = VAR_STRING;
10839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 if (*keys == NUL)
10843 return;
10844
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010845 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 else
10848 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 if (which == NULL)
10850 return;
10851
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 mode = get_map_mode(&which, 0);
10853
10854 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000010855 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 vim_free(keys_buf);
10857 if (rhs != NULL)
10858 {
10859 ga_init(&ga);
10860 ga.ga_itemsize = 1;
10861 ga.ga_growsize = 40;
10862
10863 while (*rhs != NUL)
10864 ga_concat(&ga, str2special(&rhs, FALSE));
10865
10866 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 }
10869}
10870
10871/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010872 * "map()" function
10873 */
10874 static void
10875f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010876 typval_T *argvars;
10877 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010878{
10879 filter_map(argvars, rettv, TRUE);
10880}
10881
10882/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 */
10885 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010886f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *argvars;
10888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010890 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 */
10896 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010901 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902}
10903
Bram Moolenaar33570922005-01-25 22:26:29 +000010904static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
10906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010908 typval_T *argvars;
10909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 int type;
10911{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010912 char_u *str = NULL;
10913 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 char_u *pat;
10915 regmatch_T regmatch;
10916 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010917 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 char_u *save_cpo;
10919 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010920 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010921 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 list_T *l = NULL;
10923 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010924 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010925 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926
10927 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10928 save_cpo = p_cpo;
10929 p_cpo = (char_u *)"";
10930
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010931 rettv->vval.v_number = -1;
10932 if (type == 3)
10933 {
10934 /* return empty list when there are no matches */
10935 if ((rettv->vval.v_list = list_alloc()) == NULL)
10936 goto theend;
10937 rettv->v_type = VAR_LIST;
10938 ++rettv->vval.v_list->lv_refcount;
10939 }
10940 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->v_type = VAR_STRING;
10943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010946 if (argvars[0].v_type == VAR_LIST)
10947 {
10948 if ((l = argvars[0].vval.v_list) == NULL)
10949 goto theend;
10950 li = l->lv_first;
10951 }
10952 else
10953 expr = str = get_tv_string(&argvars[0]);
10954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10956 if (pat == NULL)
10957 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010958
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 int error = FALSE;
10962
10963 start = get_tv_number_chk(&argvars[2], &error);
10964 if (error)
10965 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010966 if (l != NULL)
10967 {
10968 li = list_find(l, start);
10969 if (li == NULL)
10970 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010971 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010972 }
10973 else
10974 {
10975 if (start < 0)
10976 start = 0;
10977 if (start > (long)STRLEN(str))
10978 goto theend;
10979 str += start;
10980 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010981
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010982 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 nth = get_tv_number_chk(&argvars[3], &error);
10984 if (error)
10985 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 }
10987
10988 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10989 if (regmatch.regprog != NULL)
10990 {
10991 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010992
10993 while (1)
10994 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010995 if (l != NULL)
10996 {
10997 if (li == NULL)
10998 {
10999 match = FALSE;
11000 break;
11001 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011002 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011003 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011004 if (str == NULL)
11005 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011006 }
11007
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011008 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011009
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011010 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011011 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011012 if (l == NULL && !match)
11013 break;
11014
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011015 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011016 if (l != NULL)
11017 {
11018 li = li->li_next;
11019 ++idx;
11020 }
11021 else
11022 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011023#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011024 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011025#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011026 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011027#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011028 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011029 }
11030
11031 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011033 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011034 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011035 int i;
11036
11037 /* return list with matched string and submatches */
11038 for (i = 0; i < NSUBEXP; ++i)
11039 {
11040 if (regmatch.endp[i] == NULL)
11041 break;
11042 li = listitem_alloc();
11043 if (li == NULL)
11044 break;
11045 li->li_tv.v_type = VAR_STRING;
11046 li->li_tv.v_lock = 0;
11047 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11048 (int)(regmatch.endp[i] - regmatch.startp[i]));
11049 list_append(rettv->vval.v_list, li);
11050 }
11051 }
11052 else if (type == 2)
11053 {
11054 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011055 if (l != NULL)
11056 copy_tv(&li->li_tv, rettv);
11057 else
11058 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011060 }
11061 else if (l != NULL)
11062 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 else
11064 {
11065 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 (varnumber_T)(regmatch.startp[0] - str);
11068 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 }
11073 }
11074 vim_free(regmatch.regprog);
11075 }
11076
11077theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011078 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 p_cpo = save_cpo;
11080}
11081
11082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011083 * "match()" function
11084 */
11085 static void
11086f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 typval_T *argvars;
11088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011089{
11090 find_some_match(argvars, rettv, 1);
11091}
11092
11093/*
11094 * "matchend()" function
11095 */
11096 static void
11097f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 typval_T *argvars;
11099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100{
11101 find_some_match(argvars, rettv, 0);
11102}
11103
11104/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011105 * "matchlist()" function
11106 */
11107 static void
11108f_matchlist(argvars, rettv)
11109 typval_T *argvars;
11110 typval_T *rettv;
11111{
11112 find_some_match(argvars, rettv, 3);
11113}
11114
11115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011116 * "matchstr()" function
11117 */
11118 static void
11119f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 typval_T *argvars;
11121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122{
11123 find_some_match(argvars, rettv, 2);
11124}
11125
Bram Moolenaar33570922005-01-25 22:26:29 +000011126static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011127
11128 static void
11129max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011130 typval_T *argvars;
11131 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011132 int domax;
11133{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011134 long n = 0;
11135 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011137
11138 if (argvars[0].v_type == VAR_LIST)
11139 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011140 list_T *l;
11141 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011143 l = argvars[0].vval.v_list;
11144 if (l != NULL)
11145 {
11146 li = l->lv_first;
11147 if (li != NULL)
11148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011150 while (1)
11151 {
11152 li = li->li_next;
11153 if (li == NULL)
11154 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011156 if (domax ? i > n : i < n)
11157 n = i;
11158 }
11159 }
11160 }
11161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011162 else if (argvars[0].v_type == VAR_DICT)
11163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011164 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011165 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011167 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011168
11169 d = argvars[0].vval.v_dict;
11170 if (d != NULL)
11171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011172 todo = d->dv_hashtab.ht_used;
11173 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011174 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011175 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011177 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011179 if (first)
11180 {
11181 n = i;
11182 first = FALSE;
11183 }
11184 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 n = i;
11186 }
11187 }
11188 }
11189 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011190 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011191 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011193}
11194
11195/*
11196 * "max()" function
11197 */
11198 static void
11199f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011200 typval_T *argvars;
11201 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011202{
11203 max_min(argvars, rettv, TRUE);
11204}
11205
11206/*
11207 * "min()" function
11208 */
11209 static void
11210f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011213{
11214 max_min(argvars, rettv, FALSE);
11215}
11216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011217static int mkdir_recurse __ARGS((char_u *dir, int prot));
11218
11219/*
11220 * Create the directory in which "dir" is located, and higher levels when
11221 * needed.
11222 */
11223 static int
11224mkdir_recurse(dir, prot)
11225 char_u *dir;
11226 int prot;
11227{
11228 char_u *p;
11229 char_u *updir;
11230 int r = FAIL;
11231
11232 /* Get end of directory name in "dir".
11233 * We're done when it's "/" or "c:/". */
11234 p = gettail_sep(dir);
11235 if (p <= get_past_head(dir))
11236 return OK;
11237
11238 /* If the directory exists we're done. Otherwise: create it.*/
11239 updir = vim_strnsave(dir, (int)(p - dir));
11240 if (updir == NULL)
11241 return FAIL;
11242 if (mch_isdir(updir))
11243 r = OK;
11244 else if (mkdir_recurse(updir, prot) == OK)
11245 r = vim_mkdir_emsg(updir, prot);
11246 vim_free(updir);
11247 return r;
11248}
11249
11250#ifdef vim_mkdir
11251/*
11252 * "mkdir()" function
11253 */
11254 static void
11255f_mkdir(argvars, rettv)
11256 typval_T *argvars;
11257 typval_T *rettv;
11258{
11259 char_u *dir;
11260 char_u buf[NUMBUFLEN];
11261 int prot = 0755;
11262
11263 rettv->vval.v_number = FAIL;
11264 if (check_restricted() || check_secure())
11265 return;
11266
11267 dir = get_tv_string_buf(&argvars[0], buf);
11268 if (argvars[1].v_type != VAR_UNKNOWN)
11269 {
11270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 prot = get_tv_number_chk(&argvars[2], NULL);
11272 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011273 mkdir_recurse(dir, prot);
11274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011275 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011276}
11277#endif
11278
Bram Moolenaar0d660222005-01-07 21:51:51 +000011279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 * "mode()" function
11281 */
11282/*ARGSUSED*/
11283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
11288 char_u buf[2];
11289
11290#ifdef FEAT_VISUAL
11291 if (VIsual_active)
11292 {
11293 if (VIsual_select)
11294 buf[0] = VIsual_mode + 's' - 'v';
11295 else
11296 buf[0] = VIsual_mode;
11297 }
11298 else
11299#endif
11300 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11301 buf[0] = 'r';
11302 else if (State & INSERT)
11303 {
11304 if (State & REPLACE_FLAG)
11305 buf[0] = 'R';
11306 else
11307 buf[0] = 'i';
11308 }
11309 else if (State & CMDLINE)
11310 buf[0] = 'c';
11311 else
11312 buf[0] = 'n';
11313
11314 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 rettv->vval.v_string = vim_strsave(buf);
11316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317}
11318
11319/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011320 * "nextnonblank()" function
11321 */
11322 static void
11323f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *argvars;
11325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011326{
11327 linenr_T lnum;
11328
11329 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011332 {
11333 lnum = 0;
11334 break;
11335 }
11336 if (*skipwhite(ml_get(lnum)) != NUL)
11337 break;
11338 }
11339 rettv->vval.v_number = lnum;
11340}
11341
11342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 * "nr2char()" function
11344 */
11345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
11350 char_u buf[NUMBUFLEN];
11351
11352#ifdef FEAT_MBYTE
11353 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 else
11356#endif
11357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 buf[1] = NUL;
11360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->v_type = VAR_STRING;
11362 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363}
11364
11365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366 * "prevnonblank()" function
11367 */
11368 static void
11369f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372{
11373 linenr_T lnum;
11374
11375 lnum = get_tv_lnum(argvars);
11376 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11377 lnum = 0;
11378 else
11379 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11380 --lnum;
11381 rettv->vval.v_number = lnum;
11382}
11383
Bram Moolenaar8c711452005-01-14 21:53:12 +000011384/*
11385 * "range()" function
11386 */
11387 static void
11388f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011391{
11392 long start;
11393 long end;
11394 long stride = 1;
11395 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 list_T *l;
11397 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011401 if (argvars[1].v_type == VAR_UNKNOWN)
11402 {
11403 end = start - 1;
11404 start = 0;
11405 }
11406 else
11407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011409 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011411 }
11412
11413 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 if (error)
11415 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011416 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011417 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011418 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011419 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011420 else
11421 {
11422 l = list_alloc();
11423 if (l != NULL)
11424 {
11425 rettv->v_type = VAR_LIST;
11426 rettv->vval.v_list = l;
11427 ++l->lv_refcount;
11428
11429 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11430 {
11431 li = listitem_alloc();
11432 if (li == NULL)
11433 break;
11434 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011435 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011436 li->li_tv.vval.v_number = i;
11437 list_append(l, li);
11438 }
11439 }
11440 }
11441}
11442
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011443/*
11444 * "readfile()" function
11445 */
11446 static void
11447f_readfile(argvars, rettv)
11448 typval_T *argvars;
11449 typval_T *rettv;
11450{
11451 int binary = FALSE;
11452 char_u *fname;
11453 FILE *fd;
11454 list_T *l;
11455 listitem_T *li;
11456#define FREAD_SIZE 200 /* optimized for text lines */
11457 char_u buf[FREAD_SIZE];
11458 int readlen; /* size of last fread() */
11459 int buflen; /* nr of valid chars in buf[] */
11460 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11461 int tolist; /* first byte in buf[] still to be put in list */
11462 int chop; /* how many CR to chop off */
11463 char_u *prev = NULL; /* previously read bytes, if any */
11464 int prevlen = 0; /* length of "prev" if not NULL */
11465 char_u *s;
11466 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011467 long maxline = MAXLNUM;
11468 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011470 if (argvars[1].v_type != VAR_UNKNOWN)
11471 {
11472 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11473 binary = TRUE;
11474 if (argvars[2].v_type != VAR_UNKNOWN)
11475 maxline = get_tv_number(&argvars[2]);
11476 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011477
11478 l = list_alloc();
11479 if (l == NULL)
11480 return;
11481 rettv->v_type = VAR_LIST;
11482 rettv->vval.v_list = l;
11483 l->lv_refcount = 1;
11484
11485 /* Always open the file in binary mode, library functions have a mind of
11486 * their own about CR-LF conversion. */
11487 fname = get_tv_string(&argvars[0]);
11488 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11489 {
11490 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11491 return;
11492 }
11493
11494 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011495 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011496 {
11497 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11498 buflen = filtd + readlen;
11499 tolist = 0;
11500 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11501 {
11502 if (buf[filtd] == '\n' || readlen <= 0)
11503 {
11504 /* Only when in binary mode add an empty list item when the
11505 * last line ends in a '\n'. */
11506 if (!binary && readlen == 0 && filtd == 0)
11507 break;
11508
11509 /* Found end-of-line or end-of-file: add a text line to the
11510 * list. */
11511 chop = 0;
11512 if (!binary)
11513 while (filtd - chop - 1 >= tolist
11514 && buf[filtd - chop - 1] == '\r')
11515 ++chop;
11516 len = filtd - tolist - chop;
11517 if (prev == NULL)
11518 s = vim_strnsave(buf + tolist, len);
11519 else
11520 {
11521 s = alloc((unsigned)(prevlen + len + 1));
11522 if (s != NULL)
11523 {
11524 mch_memmove(s, prev, prevlen);
11525 vim_free(prev);
11526 prev = NULL;
11527 mch_memmove(s + prevlen, buf + tolist, len);
11528 s[prevlen + len] = NUL;
11529 }
11530 }
11531 tolist = filtd + 1;
11532
11533 li = listitem_alloc();
11534 if (li == NULL)
11535 {
11536 vim_free(s);
11537 break;
11538 }
11539 li->li_tv.v_type = VAR_STRING;
11540 li->li_tv.v_lock = 0;
11541 li->li_tv.vval.v_string = s;
11542 list_append(l, li);
11543
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011544 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011545 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011546 if (readlen <= 0)
11547 break;
11548 }
11549 else if (buf[filtd] == NUL)
11550 buf[filtd] = '\n';
11551 }
11552 if (readlen <= 0)
11553 break;
11554
11555 if (tolist == 0)
11556 {
11557 /* "buf" is full, need to move text to an allocated buffer */
11558 if (prev == NULL)
11559 {
11560 prev = vim_strnsave(buf, buflen);
11561 prevlen = buflen;
11562 }
11563 else
11564 {
11565 s = alloc((unsigned)(prevlen + buflen));
11566 if (s != NULL)
11567 {
11568 mch_memmove(s, prev, prevlen);
11569 mch_memmove(s + prevlen, buf, buflen);
11570 vim_free(prev);
11571 prev = s;
11572 prevlen += buflen;
11573 }
11574 }
11575 filtd = 0;
11576 }
11577 else
11578 {
11579 mch_memmove(buf, buf + tolist, buflen - tolist);
11580 filtd -= tolist;
11581 }
11582 }
11583
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011584 /*
11585 * For a negative line count use only the lines at the end of the file,
11586 * free the rest.
11587 */
11588 if (maxline < 0)
11589 while (cnt > -maxline)
11590 {
11591 listitem_remove(l, l->lv_first);
11592 --cnt;
11593 }
11594
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011595 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011596 fclose(fd);
11597}
11598
11599
Bram Moolenaar0d660222005-01-07 21:51:51 +000011600#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11601static void make_connection __ARGS((void));
11602static int check_connection __ARGS((void));
11603
11604 static void
11605make_connection()
11606{
11607 if (X_DISPLAY == NULL
11608# ifdef FEAT_GUI
11609 && !gui.in_use
11610# endif
11611 )
11612 {
11613 x_force_connect = TRUE;
11614 setup_term_clip();
11615 x_force_connect = FALSE;
11616 }
11617}
11618
11619 static int
11620check_connection()
11621{
11622 make_connection();
11623 if (X_DISPLAY == NULL)
11624 {
11625 EMSG(_("E240: No connection to Vim server"));
11626 return FAIL;
11627 }
11628 return OK;
11629}
11630#endif
11631
11632#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011633static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634
11635 static void
11636remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011637 typval_T *argvars;
11638 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011639 int expr;
11640{
11641 char_u *server_name;
11642 char_u *keys;
11643 char_u *r = NULL;
11644 char_u buf[NUMBUFLEN];
11645# ifdef WIN32
11646 HWND w;
11647# else
11648 Window w;
11649# endif
11650
11651 if (check_restricted() || check_secure())
11652 return;
11653
11654# ifdef FEAT_X11
11655 if (check_connection() == FAIL)
11656 return;
11657# endif
11658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659 server_name = get_tv_string_chk(&argvars[0]);
11660 if (server_name == NULL)
11661 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 keys = get_tv_string_buf(&argvars[1], buf);
11663# ifdef WIN32
11664 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11665# else
11666 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11667 < 0)
11668# endif
11669 {
11670 if (r != NULL)
11671 EMSG(r); /* sending worked but evaluation failed */
11672 else
11673 EMSG2(_("E241: Unable to send to %s"), server_name);
11674 return;
11675 }
11676
11677 rettv->vval.v_string = r;
11678
11679 if (argvars[2].v_type != VAR_UNKNOWN)
11680 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000011682 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684
11685 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 v.di_tv.v_type = VAR_STRING;
11687 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 idvar = get_tv_string_chk(&argvars[2]);
11689 if (idvar != NULL)
11690 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 }
11693}
11694#endif
11695
11696/*
11697 * "remote_expr()" function
11698 */
11699/*ARGSUSED*/
11700 static void
11701f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 typval_T *argvars;
11703 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011704{
11705 rettv->v_type = VAR_STRING;
11706 rettv->vval.v_string = NULL;
11707#ifdef FEAT_CLIENTSERVER
11708 remote_common(argvars, rettv, TRUE);
11709#endif
11710}
11711
11712/*
11713 * "remote_foreground()" function
11714 */
11715/*ARGSUSED*/
11716 static void
11717f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011720{
11721 rettv->vval.v_number = 0;
11722#ifdef FEAT_CLIENTSERVER
11723# ifdef WIN32
11724 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 {
11726 char_u *server_name = get_tv_string_chk(&argvars[0]);
11727
11728 if (server_name != NULL)
11729 serverForeground(server_name);
11730 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731# else
11732 /* Send a foreground() expression to the server. */
11733 argvars[1].v_type = VAR_STRING;
11734 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11735 argvars[2].v_type = VAR_UNKNOWN;
11736 remote_common(argvars, rettv, TRUE);
11737 vim_free(argvars[1].vval.v_string);
11738# endif
11739#endif
11740}
11741
11742/*ARGSUSED*/
11743 static void
11744f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011745 typval_T *argvars;
11746 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011747{
11748#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011749 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011750 char_u *s = NULL;
11751# ifdef WIN32
11752 int n = 0;
11753# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011755
11756 if (check_restricted() || check_secure())
11757 {
11758 rettv->vval.v_number = -1;
11759 return;
11760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 serverid = get_tv_string_chk(&argvars[0]);
11762 if (serverid == NULL)
11763 {
11764 rettv->vval.v_number = -1;
11765 return; /* type error; errmsg already given */
11766 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011767# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011769 if (n == 0)
11770 rettv->vval.v_number = -1;
11771 else
11772 {
11773 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11774 rettv->vval.v_number = (s != NULL);
11775 }
11776# else
11777 rettv->vval.v_number = 0;
11778 if (check_connection() == FAIL)
11779 return;
11780
11781 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011783# endif
11784
11785 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 char_u *retvar;
11788
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 v.di_tv.v_type = VAR_STRING;
11790 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011791 retvar = get_tv_string_chk(&argvars[1]);
11792 if (retvar != NULL)
11793 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795 }
11796#else
11797 rettv->vval.v_number = -1;
11798#endif
11799}
11800
11801/*ARGSUSED*/
11802 static void
11803f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011806{
11807 char_u *r = NULL;
11808
11809#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 char_u *serverid = get_tv_string_chk(&argvars[0]);
11811
11812 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000011813 {
11814# ifdef WIN32
11815 /* The server's HWND is encoded in the 'id' parameter */
11816 int n = 0;
11817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819 if (n != 0)
11820 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11821 if (r == NULL)
11822# else
11823 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011825# endif
11826 EMSG(_("E277: Unable to read a server reply"));
11827 }
11828#endif
11829 rettv->v_type = VAR_STRING;
11830 rettv->vval.v_string = r;
11831}
11832
11833/*
11834 * "remote_send()" function
11835 */
11836/*ARGSUSED*/
11837 static void
11838f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011841{
11842 rettv->v_type = VAR_STRING;
11843 rettv->vval.v_string = NULL;
11844#ifdef FEAT_CLIENTSERVER
11845 remote_common(argvars, rettv, FALSE);
11846#endif
11847}
11848
11849/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011850 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011851 */
11852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011854 typval_T *argvars;
11855 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011856{
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 list_T *l;
11858 listitem_T *item, *item2;
11859 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011860 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011861 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011862 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 dict_T *d;
11864 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011865
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011866 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011867 if (argvars[0].v_type == VAR_DICT)
11868 {
11869 if (argvars[2].v_type != VAR_UNKNOWN)
11870 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011871 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011872 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 key = get_tv_string_chk(&argvars[1]);
11875 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 di = dict_find(d, key, -1);
11878 if (di == NULL)
11879 EMSG2(_(e_dictkey), key);
11880 else
11881 {
11882 *rettv = di->di_tv;
11883 init_tv(&di->di_tv);
11884 dictitem_remove(d, di);
11885 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011887 }
11888 }
11889 else if (argvars[0].v_type != VAR_LIST)
11890 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011891 else if ((l = argvars[0].vval.v_list) != NULL
11892 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011894 int error = FALSE;
11895
11896 idx = get_tv_number_chk(&argvars[1], &error);
11897 if (error)
11898 ; /* type error: do nothing, errmsg already given */
11899 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011900 EMSGN(_(e_listidx), idx);
11901 else
11902 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011903 if (argvars[2].v_type == VAR_UNKNOWN)
11904 {
11905 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011906 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011907 *rettv = item->li_tv;
11908 vim_free(item);
11909 }
11910 else
11911 {
11912 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913 end = get_tv_number_chk(&argvars[2], &error);
11914 if (error)
11915 ; /* type error: do nothing */
11916 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011917 EMSGN(_(e_listidx), end);
11918 else
11919 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011920 int cnt = 0;
11921
11922 for (li = item; li != NULL; li = li->li_next)
11923 {
11924 ++cnt;
11925 if (li == item2)
11926 break;
11927 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011928 if (li == NULL) /* didn't find "item2" after "item" */
11929 EMSG(_(e_invrange));
11930 else
11931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011932 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011933 l = list_alloc();
11934 if (l != NULL)
11935 {
11936 rettv->v_type = VAR_LIST;
11937 rettv->vval.v_list = l;
11938 l->lv_first = item;
11939 l->lv_last = item2;
11940 l->lv_refcount = 1;
11941 item->li_prev = NULL;
11942 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011943 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011944 }
11945 }
11946 }
11947 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011948 }
11949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950}
11951
11952/*
11953 * "rename({from}, {to})" function
11954 */
11955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *argvars;
11958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959{
11960 char_u buf[NUMBUFLEN];
11961
11962 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11966 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967}
11968
11969/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011970 * "repeat()" function
11971 */
11972/*ARGSUSED*/
11973 static void
11974f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *argvars;
11976 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011977{
11978 char_u *p;
11979 int n;
11980 int slen;
11981 int len;
11982 char_u *r;
11983 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011984 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011985
11986 n = get_tv_number(&argvars[1]);
11987 if (argvars[0].v_type == VAR_LIST)
11988 {
11989 l = list_alloc();
11990 if (l != NULL && argvars[0].vval.v_list != NULL)
11991 {
11992 l->lv_refcount = 1;
11993 while (n-- > 0)
11994 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11995 break;
11996 }
11997 rettv->v_type = VAR_LIST;
11998 rettv->vval.v_list = l;
11999 }
12000 else
12001 {
12002 p = get_tv_string(&argvars[0]);
12003 rettv->v_type = VAR_STRING;
12004 rettv->vval.v_string = NULL;
12005
12006 slen = (int)STRLEN(p);
12007 len = slen * n;
12008 if (len <= 0)
12009 return;
12010
12011 r = alloc(len + 1);
12012 if (r != NULL)
12013 {
12014 for (i = 0; i < n; i++)
12015 mch_memmove(r + i * slen, p, (size_t)slen);
12016 r[len] = NUL;
12017 }
12018
12019 rettv->vval.v_string = r;
12020 }
12021}
12022
12023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 * "resolve()" function
12025 */
12026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 typval_T *argvars;
12029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
12031 char_u *p;
12032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034#ifdef FEAT_SHORTCUT
12035 {
12036 char_u *v = NULL;
12037
12038 v = mch_resolve_shortcut(p);
12039 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 }
12044#else
12045# ifdef HAVE_READLINK
12046 {
12047 char_u buf[MAXPATHL + 1];
12048 char_u *cpy;
12049 int len;
12050 char_u *remain = NULL;
12051 char_u *q;
12052 int is_relative_to_current = FALSE;
12053 int has_trailing_pathsep = FALSE;
12054 int limit = 100;
12055
12056 p = vim_strsave(p);
12057
12058 if (p[0] == '.' && (vim_ispathsep(p[1])
12059 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12060 is_relative_to_current = TRUE;
12061
12062 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012063 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 has_trailing_pathsep = TRUE;
12065
12066 q = getnextcomp(p);
12067 if (*q != NUL)
12068 {
12069 /* Separate the first path component in "p", and keep the
12070 * remainder (beginning with the path separator). */
12071 remain = vim_strsave(q - 1);
12072 q[-1] = NUL;
12073 }
12074
12075 for (;;)
12076 {
12077 for (;;)
12078 {
12079 len = readlink((char *)p, (char *)buf, MAXPATHL);
12080 if (len <= 0)
12081 break;
12082 buf[len] = NUL;
12083
12084 if (limit-- == 0)
12085 {
12086 vim_free(p);
12087 vim_free(remain);
12088 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 goto fail;
12091 }
12092
12093 /* Ensure that the result will have a trailing path separator
12094 * if the argument has one. */
12095 if (remain == NULL && has_trailing_pathsep)
12096 add_pathsep(buf);
12097
12098 /* Separate the first path component in the link value and
12099 * concatenate the remainders. */
12100 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12101 if (*q != NUL)
12102 {
12103 if (remain == NULL)
12104 remain = vim_strsave(q - 1);
12105 else
12106 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012107 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 if (cpy != NULL)
12109 {
12110 STRCAT(cpy, remain);
12111 vim_free(remain);
12112 remain = cpy;
12113 }
12114 }
12115 q[-1] = NUL;
12116 }
12117
12118 q = gettail(p);
12119 if (q > p && *q == NUL)
12120 {
12121 /* Ignore trailing path separator. */
12122 q[-1] = NUL;
12123 q = gettail(p);
12124 }
12125 if (q > p && !mch_isFullName(buf))
12126 {
12127 /* symlink is relative to directory of argument */
12128 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12129 if (cpy != NULL)
12130 {
12131 STRCPY(cpy, p);
12132 STRCPY(gettail(cpy), buf);
12133 vim_free(p);
12134 p = cpy;
12135 }
12136 }
12137 else
12138 {
12139 vim_free(p);
12140 p = vim_strsave(buf);
12141 }
12142 }
12143
12144 if (remain == NULL)
12145 break;
12146
12147 /* Append the first path component of "remain" to "p". */
12148 q = getnextcomp(remain + 1);
12149 len = q - remain - (*q != NUL);
12150 cpy = vim_strnsave(p, STRLEN(p) + len);
12151 if (cpy != NULL)
12152 {
12153 STRNCAT(cpy, remain, len);
12154 vim_free(p);
12155 p = cpy;
12156 }
12157 /* Shorten "remain". */
12158 if (*q != NUL)
12159 STRCPY(remain, q - 1);
12160 else
12161 {
12162 vim_free(remain);
12163 remain = NULL;
12164 }
12165 }
12166
12167 /* If the result is a relative path name, make it explicitly relative to
12168 * the current directory if and only if the argument had this form. */
12169 if (!vim_ispathsep(*p))
12170 {
12171 if (is_relative_to_current
12172 && *p != NUL
12173 && !(p[0] == '.'
12174 && (p[1] == NUL
12175 || vim_ispathsep(p[1])
12176 || (p[1] == '.'
12177 && (p[2] == NUL
12178 || vim_ispathsep(p[2]))))))
12179 {
12180 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012181 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 if (cpy != NULL)
12183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 vim_free(p);
12185 p = cpy;
12186 }
12187 }
12188 else if (!is_relative_to_current)
12189 {
12190 /* Strip leading "./". */
12191 q = p;
12192 while (q[0] == '.' && vim_ispathsep(q[1]))
12193 q += 2;
12194 if (q > p)
12195 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12196 }
12197 }
12198
12199 /* Ensure that the result will have no trailing path separator
12200 * if the argument had none. But keep "/" or "//". */
12201 if (!has_trailing_pathsep)
12202 {
12203 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012204 if (after_pathsep(p, q))
12205 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 }
12207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 }
12210# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212# endif
12213#endif
12214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216
12217#ifdef HAVE_READLINK
12218fail:
12219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012220 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221}
12222
12223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225 */
12226 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012227f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012228 typval_T *argvars;
12229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230{
Bram Moolenaar33570922005-01-25 22:26:29 +000012231 list_T *l;
12232 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233
Bram Moolenaar0d660222005-01-07 21:51:51 +000012234 rettv->vval.v_number = 0;
12235 if (argvars[0].v_type != VAR_LIST)
12236 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012237 else if ((l = argvars[0].vval.v_list) != NULL
12238 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012239 {
12240 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012241 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012242 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012243 while (li != NULL)
12244 {
12245 ni = li->li_prev;
12246 list_append(l, li);
12247 li = ni;
12248 }
12249 rettv->vval.v_list = l;
12250 rettv->v_type = VAR_LIST;
12251 ++l->lv_refcount;
12252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253}
12254
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012255#define SP_NOMOVE 1 /* don't move cursor */
12256#define SP_REPEAT 2 /* repeat to find outer pair */
12257#define SP_RETCOUNT 4 /* return matchcount */
12258
Bram Moolenaar33570922005-01-25 22:26:29 +000012259static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012260
12261/*
12262 * Get flags for a search function.
12263 * Possibly sets "p_ws".
12264 * Returns BACKWARD, FORWARD or zero (for an error).
12265 */
12266 static int
12267get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012269 int *flagsp;
12270{
12271 int dir = FORWARD;
12272 char_u *flags;
12273 char_u nbuf[NUMBUFLEN];
12274 int mask;
12275
12276 if (varp->v_type != VAR_UNKNOWN)
12277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 flags = get_tv_string_buf_chk(varp, nbuf);
12279 if (flags == NULL)
12280 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012281 while (*flags != NUL)
12282 {
12283 switch (*flags)
12284 {
12285 case 'b': dir = BACKWARD; break;
12286 case 'w': p_ws = TRUE; break;
12287 case 'W': p_ws = FALSE; break;
12288 default: mask = 0;
12289 if (flagsp != NULL)
12290 switch (*flags)
12291 {
12292 case 'n': mask = SP_NOMOVE; break;
12293 case 'r': mask = SP_REPEAT; break;
12294 case 'm': mask = SP_RETCOUNT; break;
12295 }
12296 if (mask == 0)
12297 {
12298 EMSG2(_(e_invarg2), flags);
12299 dir = 0;
12300 }
12301 else
12302 *flagsp |= mask;
12303 }
12304 if (dir == 0)
12305 break;
12306 ++flags;
12307 }
12308 }
12309 return dir;
12310}
12311
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312/*
12313 * "search()" function
12314 */
12315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012317 typval_T *argvars;
12318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319{
12320 char_u *pat;
12321 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012322 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 int save_p_ws = p_ws;
12324 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012325 int flags = 0;
12326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012330 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12331 if (dir == 0)
12332 goto theend;
12333 if ((flags & ~SP_NOMOVE) != 0)
12334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012336 goto theend;
12337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012339 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12341 SEARCH_KEEP, RE_SEARCH) != FAIL)
12342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 curwin->w_cursor = pos;
12345 /* "/$" will put the cursor after the end of the line, may need to
12346 * correct that here */
12347 check_cursor();
12348 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012349
12350 /* If 'n' flag is used: restore cursor position. */
12351 if (flags & SP_NOMOVE)
12352 curwin->w_cursor = save_cursor;
12353theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 p_ws = save_p_ws;
12355}
12356
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357/*
12358 * "searchpair()" function
12359 */
12360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012362 typval_T *argvars;
12363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365 char_u *spat, *mpat, *epat;
12366 char_u *skip;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367 char_u *pat, *pat2 = NULL, *pat3 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 pos_T pos;
12369 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012370 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 pos_T save_cursor;
12372 pos_T save_pos;
12373 int save_p_ws = p_ws;
12374 char_u *save_cpo;
12375 int dir;
12376 int flags = 0;
12377 char_u nbuf1[NUMBUFLEN];
12378 char_u nbuf2[NUMBUFLEN];
12379 char_u nbuf3[NUMBUFLEN];
12380 int n;
12381 int r;
12382 int nest = 1;
12383 int err;
12384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386
12387 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12388 save_cpo = p_cpo;
12389 p_cpo = (char_u *)"";
12390
12391 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012392 spat = get_tv_string_chk(&argvars[0]);
12393 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12394 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12395 if (spat == NULL || mpat == NULL || epat == NULL)
12396 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397
12398 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12399 * start/middle/end (pat3, for the top pair). */
12400 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12401 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12402 if (pat2 == NULL || pat3 == NULL)
12403 goto theend;
12404 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12405 if (*mpat == NUL)
12406 STRCPY(pat3, pat2);
12407 else
12408 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12409 spat, epat, mpat);
12410
12411 /* Handle the optional fourth argument: flags */
12412 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012413 if (dir == 0)
12414 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415
12416 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012417 if (argvars[3].v_type == VAR_UNKNOWN
12418 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 skip = (char_u *)"";
12420 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012421 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12422 if (skip == NULL)
12423 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424
12425 save_cursor = curwin->w_cursor;
12426 pos = curwin->w_cursor;
12427 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012428 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 pat = pat3;
12430 for (;;)
12431 {
12432 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12433 SEARCH_KEEP, RE_SEARCH);
12434 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12435 /* didn't find it or found the first match again: FAIL */
12436 break;
12437
12438 if (firstpos.lnum == 0)
12439 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012440 if (equalpos(pos, foundpos))
12441 {
12442 /* Found the same position again. Can happen with a pattern that
12443 * has "\zs" at the end and searching backwards. Advance one
12444 * character and try again. */
12445 if (dir == BACKWARD)
12446 decl(&pos);
12447 else
12448 incl(&pos);
12449 }
12450 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451
12452 /* If the skip pattern matches, ignore this match. */
12453 if (*skip != NUL)
12454 {
12455 save_pos = curwin->w_cursor;
12456 curwin->w_cursor = pos;
12457 r = eval_to_bool(skip, &err, NULL, FALSE);
12458 curwin->w_cursor = save_pos;
12459 if (err)
12460 {
12461 /* Evaluating {skip} caused an error, break here. */
12462 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 break;
12465 }
12466 if (r)
12467 continue;
12468 }
12469
12470 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12471 {
12472 /* Found end when searching backwards or start when searching
12473 * forward: nested pair. */
12474 ++nest;
12475 pat = pat2; /* nested, don't search for middle */
12476 }
12477 else
12478 {
12479 /* Found end when searching forward or start when searching
12480 * backward: end of (nested) pair; or found middle in outer pair. */
12481 if (--nest == 1)
12482 pat = pat3; /* outer level, search for middle */
12483 }
12484
12485 if (nest == 0)
12486 {
12487 /* Found the match: return matchcount or line number. */
12488 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 curwin->w_cursor = pos;
12493 if (!(flags & SP_REPEAT))
12494 break;
12495 nest = 1; /* search for next unmatched */
12496 }
12497 }
12498
12499 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 curwin->w_cursor = save_cursor;
12502
12503theend:
12504 vim_free(pat2);
12505 vim_free(pat3);
12506 p_ws = save_p_ws;
12507 p_cpo = save_cpo;
12508}
12509
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510/*ARGSUSED*/
12511 static void
12512f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012513 typval_T *argvars;
12514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516#ifdef FEAT_CLIENTSERVER
12517 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012518 char_u *server = get_tv_string_chk(&argvars[0]);
12519 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 if (server == NULL || reply == NULL)
12523 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 if (check_restricted() || check_secure())
12525 return;
12526# ifdef FEAT_X11
12527 if (check_connection() == FAIL)
12528 return;
12529# endif
12530
12531 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 EMSG(_("E258: Unable to send to client"));
12534 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012536 rettv->vval.v_number = 0;
12537#else
12538 rettv->vval.v_number = -1;
12539#endif
12540}
12541
12542/*ARGSUSED*/
12543 static void
12544f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012545 typval_T *argvars;
12546 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547{
12548 char_u *r = NULL;
12549
12550#ifdef FEAT_CLIENTSERVER
12551# ifdef WIN32
12552 r = serverGetVimNames();
12553# else
12554 make_connection();
12555 if (X_DISPLAY != NULL)
12556 r = serverGetVimNames(X_DISPLAY);
12557# endif
12558#endif
12559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561}
12562
12563/*
12564 * "setbufvar()" function
12565 */
12566/*ARGSUSED*/
12567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
12572 buf_T *buf;
12573#ifdef FEAT_AUTOCMD
12574 aco_save_T aco;
12575#else
12576 buf_T *save_curbuf;
12577#endif
12578 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012579 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 char_u nbuf[NUMBUFLEN];
12581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012582 rettv->vval.v_number = 0;
12583
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 if (check_restricted() || check_secure())
12585 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012586 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12587 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 varp = &argvars[2];
12591
12592 if (buf != NULL && varname != NULL && varp != NULL)
12593 {
12594 /* set curbuf to be our buf, temporarily */
12595#ifdef FEAT_AUTOCMD
12596 aucmd_prepbuf(&aco, buf);
12597#else
12598 save_curbuf = curbuf;
12599 curbuf = buf;
12600#endif
12601
12602 if (*varname == '&')
12603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 long numval;
12605 char_u *strval;
12606 int error = FALSE;
12607
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 --emsg_off;
12610 numval = get_tv_number_chk(varp, &error);
12611 strval = get_tv_string_buf_chk(varp, nbuf);
12612 ++emsg_off;
12613 if (!error && strval != NULL)
12614 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 }
12616 else
12617 {
12618 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12619 if (bufvarname != NULL)
12620 {
12621 STRCPY(bufvarname, "b:");
12622 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012623 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 vim_free(bufvarname);
12625 }
12626 }
12627
12628 /* reset notion of buffer */
12629#ifdef FEAT_AUTOCMD
12630 aucmd_restbuf(&aco);
12631#else
12632 curbuf = save_curbuf;
12633#endif
12634 }
12635 --emsg_off;
12636}
12637
12638/*
12639 * "setcmdpos()" function
12640 */
12641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012643 typval_T *argvars;
12644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 int pos = (int)get_tv_number(&argvars[0]) - 1;
12647
12648 if (pos >= 0)
12649 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650}
12651
12652/*
12653 * "setline()" function
12654 */
12655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 typval_T *argvars;
12658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659{
12660 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000012661 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012662 list_T *l = NULL;
12663 listitem_T *li = NULL;
12664 long added = 0;
12665 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012667 lnum = get_tv_lnum(&argvars[0]);
12668 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012670 l = argvars[1].vval.v_list;
12671 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012673 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012675
12676 rettv->vval.v_number = 0; /* OK */
12677 for (;;)
12678 {
12679 if (l != NULL)
12680 {
12681 /* list argument, get next string */
12682 if (li == NULL)
12683 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012684 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012685 li = li->li_next;
12686 }
12687
12688 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012690 break;
12691 if (lnum <= curbuf->b_ml.ml_line_count)
12692 {
12693 /* existing line, replace it */
12694 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
12695 {
12696 changed_bytes(lnum, 0);
12697 check_cursor_col();
12698 rettv->vval.v_number = 0; /* OK */
12699 }
12700 }
12701 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
12702 {
12703 /* lnum is one past the last line, append the line */
12704 ++added;
12705 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
12706 rettv->vval.v_number = 0; /* OK */
12707 }
12708
12709 if (l == NULL) /* only one string argument */
12710 break;
12711 ++lnum;
12712 }
12713
12714 if (added > 0)
12715 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716}
12717
12718/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012719 * "setqflist()" function
12720 */
12721/*ARGSUSED*/
12722 static void
12723f_setqflist(argvars, rettv)
12724 typval_T *argvars;
12725 typval_T *rettv;
12726{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012727 char_u *act;
12728 int action = ' ';
12729
Bram Moolenaar2641f772005-03-25 21:58:17 +000012730 rettv->vval.v_number = -1;
12731
12732#ifdef FEAT_QUICKFIX
12733 if (argvars[0].v_type != VAR_LIST)
12734 EMSG(_(e_listreq));
12735 else
12736 {
12737 list_T *l = argvars[0].vval.v_list;
12738
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012739 if (argvars[1].v_type == VAR_STRING)
12740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 act = get_tv_string_chk(&argvars[1]);
12742 if (act == NULL)
12743 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012744 if (*act == 'a' || *act == 'r')
12745 action = *act;
12746 }
12747
12748 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012749 rettv->vval.v_number = 0;
12750 }
12751#endif
12752}
12753
12754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 * "setreg()" function
12756 */
12757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012759 typval_T *argvars;
12760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
12762 int regname;
12763 char_u *strregname;
12764 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012765 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 int append;
12767 char_u yank_type;
12768 long block_len;
12769
12770 block_len = -1;
12771 yank_type = MAUTO;
12772 append = FALSE;
12773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012777 if (strregname == NULL)
12778 return; /* type error; errmsg already given */
12779 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 if (regname == 0 || regname == '@')
12781 regname = '"';
12782 else if (regname == '=')
12783 return;
12784
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012785 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 stropt = get_tv_string_chk(&argvars[2]);
12788 if (stropt == NULL)
12789 return; /* type error */
12790 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 switch (*stropt)
12792 {
12793 case 'a': case 'A': /* append */
12794 append = TRUE;
12795 break;
12796 case 'v': case 'c': /* character-wise selection */
12797 yank_type = MCHAR;
12798 break;
12799 case 'V': case 'l': /* line-wise selection */
12800 yank_type = MLINE;
12801 break;
12802#ifdef FEAT_VISUAL
12803 case 'b': case Ctrl_V: /* block-wise selection */
12804 yank_type = MBLOCK;
12805 if (VIM_ISDIGIT(stropt[1]))
12806 {
12807 ++stropt;
12808 block_len = getdigits(&stropt) - 1;
12809 --stropt;
12810 }
12811 break;
12812#endif
12813 }
12814 }
12815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012816 strval = get_tv_string_chk(&argvars[1]);
12817 if (strval != NULL)
12818 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821}
12822
12823
12824/*
12825 * "setwinvar(expr)" function
12826 */
12827/*ARGSUSED*/
12828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012830 typval_T *argvars;
12831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832{
12833 win_T *win;
12834#ifdef FEAT_WINDOWS
12835 win_T *save_curwin;
12836#endif
12837 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 char_u nbuf[NUMBUFLEN];
12840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841 rettv->vval.v_number = 0;
12842
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 if (check_restricted() || check_secure())
12844 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012846 varname = get_tv_string_chk(&argvars[1]);
12847 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 varp = &argvars[2];
12849
12850 if (win != NULL && varname != NULL && varp != NULL)
12851 {
12852#ifdef FEAT_WINDOWS
12853 /* set curwin to be our win, temporarily */
12854 save_curwin = curwin;
12855 curwin = win;
12856 curbuf = curwin->w_buffer;
12857#endif
12858
12859 if (*varname == '&')
12860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012861 long numval;
12862 char_u *strval;
12863 int error = FALSE;
12864
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 --emsg_off;
12867 numval = get_tv_number_chk(varp, &error);
12868 strval = get_tv_string_buf_chk(varp, nbuf);
12869 ++emsg_off;
12870 if (!error && strval != NULL)
12871 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 }
12873 else
12874 {
12875 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12876 if (winvarname != NULL)
12877 {
12878 STRCPY(winvarname, "w:");
12879 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012880 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 vim_free(winvarname);
12882 }
12883 }
12884
12885#ifdef FEAT_WINDOWS
12886 /* Restore current window, if it's still valid (autocomands can make
12887 * it invalid). */
12888 if (win_valid(save_curwin))
12889 {
12890 curwin = save_curwin;
12891 curbuf = curwin->w_buffer;
12892 }
12893#endif
12894 }
12895 --emsg_off;
12896}
12897
12898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012899 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 */
12901 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012902f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012903 typval_T *argvars;
12904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012906 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907
Bram Moolenaar0d660222005-01-07 21:51:51 +000012908 p = get_tv_string(&argvars[0]);
12909 rettv->vval.v_string = vim_strsave(p);
12910 simplify_filename(rettv->vval.v_string); /* simplify in place */
12911 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912}
12913
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914static int
12915#ifdef __BORLANDC__
12916 _RTLENTRYF
12917#endif
12918 item_compare __ARGS((const void *s1, const void *s2));
12919static int
12920#ifdef __BORLANDC__
12921 _RTLENTRYF
12922#endif
12923 item_compare2 __ARGS((const void *s1, const void *s2));
12924
12925static int item_compare_ic;
12926static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012927static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012928#define ITEM_COMPARE_FAIL 999
12929
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012933 static int
12934#ifdef __BORLANDC__
12935_RTLENTRYF
12936#endif
12937item_compare(s1, s2)
12938 const void *s1;
12939 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012941 char_u *p1, *p2;
12942 char_u *tofree1, *tofree2;
12943 int res;
12944 char_u numbuf1[NUMBUFLEN];
12945 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
Bram Moolenaar33570922005-01-25 22:26:29 +000012947 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12948 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012949 if (item_compare_ic)
12950 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012952 res = STRCMP(p1, p2);
12953 vim_free(tofree1);
12954 vim_free(tofree2);
12955 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956}
12957
12958 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012959#ifdef __BORLANDC__
12960_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012962item_compare2(s1, s2)
12963 const void *s1;
12964 const void *s2;
12965{
12966 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012967 typval_T rettv;
12968 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012969 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012971 /* shortcut after failure in previous call; compare all items equal */
12972 if (item_compare_func_err)
12973 return 0;
12974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012975 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12976 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012977 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12978 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979
12980 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12981 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012982 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012983 clear_tv(&argv[0]);
12984 clear_tv(&argv[1]);
12985
12986 if (res == FAIL)
12987 res = ITEM_COMPARE_FAIL;
12988 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 /* return value has wrong type */
12990 res = get_tv_number_chk(&rettv, &item_compare_func_err);
12991 if (item_compare_func_err)
12992 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012993 clear_tv(&rettv);
12994 return res;
12995}
12996
12997/*
12998 * "sort({list})" function
12999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 typval_T *argvars;
13003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004{
Bram Moolenaar33570922005-01-25 22:26:29 +000013005 list_T *l;
13006 listitem_T *li;
13007 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008 long len;
13009 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010
Bram Moolenaar0d660222005-01-07 21:51:51 +000013011 rettv->vval.v_number = 0;
13012 if (argvars[0].v_type != VAR_LIST)
13013 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 else
13015 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013016 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013017 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013018 return;
13019 rettv->vval.v_list = l;
13020 rettv->v_type = VAR_LIST;
13021 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023 len = list_len(l);
13024 if (len <= 1)
13025 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
Bram Moolenaar0d660222005-01-07 21:51:51 +000013027 item_compare_ic = FALSE;
13028 item_compare_func = NULL;
13029 if (argvars[1].v_type != VAR_UNKNOWN)
13030 {
13031 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013033 else
13034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 int error = FALSE;
13036
13037 i = get_tv_number_chk(&argvars[1], &error);
13038 if (error)
13039 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013040 if (i == 1)
13041 item_compare_ic = TRUE;
13042 else
13043 item_compare_func = get_tv_string(&argvars[1]);
13044 }
13045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013049 if (ptrs == NULL)
13050 return;
13051 i = 0;
13052 for (li = l->lv_first; li != NULL; li = li->li_next)
13053 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013056 /* test the compare function */
13057 if (item_compare_func != NULL
13058 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13059 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013060 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062 {
13063 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013064 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013065 item_compare_func == NULL ? item_compare : item_compare2);
13066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 if (!item_compare_func_err)
13068 {
13069 /* Clear the List and append the items in the sorted order. */
13070 l->lv_first = l->lv_last = NULL;
13071 l->lv_len = 0;
13072 for (i = 0; i < len; ++i)
13073 list_append(l, ptrs[i]);
13074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013075 }
13076
13077 vim_free(ptrs);
13078 }
13079}
13080
13081 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013082f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 typval_T *argvars;
13084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013085{
13086 char_u *str;
13087 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013088 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013089 regmatch_T regmatch;
13090 char_u patbuf[NUMBUFLEN];
13091 char_u *save_cpo;
13092 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 listitem_T *ni;
13094 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013095 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013096 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013098
13099 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13100 save_cpo = p_cpo;
13101 p_cpo = (char_u *)"";
13102
13103 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013104 if (argvars[1].v_type != VAR_UNKNOWN)
13105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013106 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13107 if (pat == NULL)
13108 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013110 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013111 }
13112 if (pat == NULL || *pat == NUL)
13113 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013114
13115 l = list_alloc();
13116 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013118 rettv->v_type = VAR_LIST;
13119 rettv->vval.v_list = l;
13120 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013121 if (typeerr)
13122 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123
Bram Moolenaar0d660222005-01-07 21:51:51 +000013124 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13125 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013127 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013128 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013129 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013130 if (*str == NUL)
13131 match = FALSE; /* empty item at the end */
13132 else
13133 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013134 if (match)
13135 end = regmatch.startp[0];
13136 else
13137 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013138 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13139 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013140 {
13141 ni = listitem_alloc();
13142 if (ni == NULL)
13143 break;
13144 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013145 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013146 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13147 list_append(l, ni);
13148 }
13149 if (!match)
13150 break;
13151 /* Advance to just after the match. */
13152 if (regmatch.endp[0] > str)
13153 col = 0;
13154 else
13155 {
13156 /* Don't get stuck at the same match. */
13157#ifdef FEAT_MBYTE
13158 col = mb_ptr2len_check(regmatch.endp[0]);
13159#else
13160 col = 1;
13161#endif
13162 }
13163 str = regmatch.endp[0];
13164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165
Bram Moolenaar0d660222005-01-07 21:51:51 +000013166 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168
Bram Moolenaar0d660222005-01-07 21:51:51 +000013169 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170}
13171
13172#ifdef HAVE_STRFTIME
13173/*
13174 * "strftime({format}[, {time}])" function
13175 */
13176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 typval_T *argvars;
13179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180{
13181 char_u result_buf[256];
13182 struct tm *curtime;
13183 time_t seconds;
13184 char_u *p;
13185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 seconds = time(NULL);
13191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 curtime = localtime(&seconds);
13194 /* MSVC returns NULL for an invalid value of seconds. */
13195 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 else
13198 {
13199# ifdef FEAT_MBYTE
13200 vimconv_T conv;
13201 char_u *enc;
13202
13203 conv.vc_type = CONV_NONE;
13204 enc = enc_locale();
13205 convert_setup(&conv, p_enc, enc);
13206 if (conv.vc_type != CONV_NONE)
13207 p = string_convert(&conv, p, NULL);
13208# endif
13209 if (p != NULL)
13210 (void)strftime((char *)result_buf, sizeof(result_buf),
13211 (char *)p, curtime);
13212 else
13213 result_buf[0] = NUL;
13214
13215# ifdef FEAT_MBYTE
13216 if (conv.vc_type != CONV_NONE)
13217 vim_free(p);
13218 convert_setup(&conv, enc, p_enc);
13219 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 else
13222# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224
13225# ifdef FEAT_MBYTE
13226 /* Release conversion descriptors */
13227 convert_setup(&conv, NULL, NULL);
13228 vim_free(enc);
13229# endif
13230 }
13231}
13232#endif
13233
13234/*
13235 * "stridx()" function
13236 */
13237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013239 typval_T *argvars;
13240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241{
13242 char_u buf[NUMBUFLEN];
13243 char_u *needle;
13244 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013245 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 needle = get_tv_string_chk(&argvars[1]);
13250 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013252 if (needle == NULL || haystack == NULL)
13253 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254
Bram Moolenaar33570922005-01-25 22:26:29 +000013255 if (argvars[2].v_type != VAR_UNKNOWN)
13256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013257 int error = FALSE;
13258
13259 start_idx = get_tv_number_chk(&argvars[2], &error);
13260 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013261 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013262 if (start_idx >= 0)
13263 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 }
13265
13266 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13267 if (pos != NULL)
13268 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269}
13270
13271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013272 * "string()" function
13273 */
13274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013275f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 typval_T *argvars;
13277 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013278{
13279 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013280 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013283 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013284 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286}
13287
13288/*
13289 * "strlen()" function
13290 */
13291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 typval_T *argvars;
13294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296 rettv->vval.v_number = (varnumber_T)(STRLEN(
13297 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298}
13299
13300/*
13301 * "strpart()" function
13302 */
13303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013305 typval_T *argvars;
13306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307{
13308 char_u *p;
13309 int n;
13310 int len;
13311 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013312 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 slen = (int)STRLEN(p);
13316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 n = get_tv_number_chk(&argvars[1], &error);
13318 if (error)
13319 len = 0;
13320 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 else
13323 len = slen - n; /* default len: all bytes that are available. */
13324
13325 /*
13326 * Only return the overlap between the specified part and the actual
13327 * string.
13328 */
13329 if (n < 0)
13330 {
13331 len += n;
13332 n = 0;
13333 }
13334 else if (n > slen)
13335 n = slen;
13336 if (len < 0)
13337 len = 0;
13338 else if (n + len > slen)
13339 len = slen - n;
13340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 rettv->v_type = VAR_STRING;
13342 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343}
13344
13345/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013346 * "strridx()" function
13347 */
13348 static void
13349f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013350 typval_T *argvars;
13351 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013352{
13353 char_u buf[NUMBUFLEN];
13354 char_u *needle;
13355 char_u *haystack;
13356 char_u *rest;
13357 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013358 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360 needle = get_tv_string_chk(&argvars[1]);
13361 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013362 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013363
13364 rettv->vval.v_number = -1;
13365 if (needle == NULL || haystack == NULL)
13366 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013367 if (argvars[2].v_type != VAR_UNKNOWN)
13368 {
13369 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013371 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013372 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013373 }
13374 else
13375 end_idx = haystack_len;
13376
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013378 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013379 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013380 lastmatch = haystack + end_idx;
13381 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013382 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013383 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013384 for (rest = haystack; *rest != '\0'; ++rest)
13385 {
13386 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013387 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013388 break;
13389 lastmatch = rest;
13390 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013391 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392
13393 if (lastmatch == NULL)
13394 rettv->vval.v_number = -1;
13395 else
13396 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13397}
13398
13399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 * "strtrans()" function
13401 */
13402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013404 typval_T *argvars;
13405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407 rettv->v_type = VAR_STRING;
13408 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409}
13410
13411/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013412 * "submatch()" function
13413 */
13414 static void
13415f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 typval_T *argvars;
13417 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013418{
13419 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420 rettv->vval.v_string =
13421 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013422}
13423
13424/*
13425 * "substitute()" function
13426 */
13427 static void
13428f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 typval_T *argvars;
13430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013431{
13432 char_u patbuf[NUMBUFLEN];
13433 char_u subbuf[NUMBUFLEN];
13434 char_u flagsbuf[NUMBUFLEN];
13435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 char_u *str = get_tv_string_chk(&argvars[0]);
13437 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13438 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13439 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13440
Bram Moolenaar0d660222005-01-07 21:51:51 +000013441 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13443 rettv->vval.v_string = NULL;
13444 else
13445 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446}
13447
13448/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013449 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 */
13451/*ARGSUSED*/
13452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013453f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013454 typval_T *argvars;
13455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456{
13457 int id = 0;
13458#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013459 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 long col;
13461 int trans;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013462 int transerr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 lnum = get_tv_lnum(argvars); /* -1 on type error */
13465 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13466 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013469 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13470 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471#endif
13472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474}
13475
13476/*
13477 * "synIDattr(id, what [, mode])" function
13478 */
13479/*ARGSUSED*/
13480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484{
13485 char_u *p = NULL;
13486#ifdef FEAT_SYN_HL
13487 int id;
13488 char_u *what;
13489 char_u *mode;
13490 char_u modebuf[NUMBUFLEN];
13491 int modec;
13492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 id = get_tv_number(&argvars[0]);
13494 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 modec = TOLOWER_ASC(mode[0]);
13499 if (modec != 't' && modec != 'c'
13500#ifdef FEAT_GUI
13501 && modec != 'g'
13502#endif
13503 )
13504 modec = 0; /* replace invalid with current */
13505 }
13506 else
13507 {
13508#ifdef FEAT_GUI
13509 if (gui.in_use)
13510 modec = 'g';
13511 else
13512#endif
13513 if (t_colors > 1)
13514 modec = 'c';
13515 else
13516 modec = 't';
13517 }
13518
13519
13520 switch (TOLOWER_ASC(what[0]))
13521 {
13522 case 'b':
13523 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13524 p = highlight_color(id, what, modec);
13525 else /* bold */
13526 p = highlight_has_attr(id, HL_BOLD, modec);
13527 break;
13528
13529 case 'f': /* fg[#] */
13530 p = highlight_color(id, what, modec);
13531 break;
13532
13533 case 'i':
13534 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13535 p = highlight_has_attr(id, HL_INVERSE, modec);
13536 else /* italic */
13537 p = highlight_has_attr(id, HL_ITALIC, modec);
13538 break;
13539
13540 case 'n': /* name */
13541 p = get_highlight_name(NULL, id - 1);
13542 break;
13543
13544 case 'r': /* reverse */
13545 p = highlight_has_attr(id, HL_INVERSE, modec);
13546 break;
13547
13548 case 's': /* standout */
13549 p = highlight_has_attr(id, HL_STANDOUT, modec);
13550 break;
13551
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013552 case 'u':
13553 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13554 /* underline */
13555 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13556 else
13557 /* undercurl */
13558 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 break;
13560 }
13561
13562 if (p != NULL)
13563 p = vim_strsave(p);
13564#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->v_type = VAR_STRING;
13566 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567}
13568
13569/*
13570 * "synIDtrans(id)" function
13571 */
13572/*ARGSUSED*/
13573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *argvars;
13576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577{
13578 int id;
13579
13580#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582
13583 if (id > 0)
13584 id = syn_get_final_id(id);
13585 else
13586#endif
13587 id = 0;
13588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590}
13591
13592/*
13593 * "system()" function
13594 */
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013600 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013602 char_u *infile = NULL;
13603 char_u buf[NUMBUFLEN];
13604 int err = FALSE;
13605 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013607 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013608 {
13609 /*
13610 * Write the string to a temp file, to be used for input of the shell
13611 * command.
13612 */
13613 if ((infile = vim_tempname('i')) == NULL)
13614 {
13615 EMSG(_(e_notmp));
13616 return;
13617 }
13618
13619 fd = mch_fopen((char *)infile, WRITEBIN);
13620 if (fd == NULL)
13621 {
13622 EMSG2(_(e_notopen), infile);
13623 goto done;
13624 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 p = get_tv_string_buf_chk(&argvars[1], buf);
13626 if (p == NULL)
13627 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013628 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13629 err = TRUE;
13630 if (fclose(fd) != 0)
13631 err = TRUE;
13632 if (err)
13633 {
13634 EMSG(_("E677: Error writing temp file"));
13635 goto done;
13636 }
13637 }
13638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013640
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#ifdef USE_CR
13642 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013643 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 {
13645 char_u *s;
13646
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013647 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 {
13649 if (*s == CAR)
13650 *s = NL;
13651 }
13652 }
13653#else
13654# ifdef USE_CRNL
13655 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013656 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 {
13658 char_u *s, *d;
13659
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013660 d = res;
13661 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 {
13663 if (s[0] == CAR && s[1] == NL)
13664 ++s;
13665 *d++ = *s;
13666 }
13667 *d = NUL;
13668 }
13669# endif
13670#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013671
13672done:
13673 if (infile != NULL)
13674 {
13675 mch_remove(infile);
13676 vim_free(infile);
13677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678 rettv->v_type = VAR_STRING;
13679 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680}
13681
13682/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013683 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013684 */
13685 static void
13686f_taglist(argvars, rettv)
13687 typval_T *argvars;
13688 typval_T *rettv;
13689{
13690 char_u *tag_pattern;
13691 list_T *l;
13692
13693 tag_pattern = get_tv_string(&argvars[0]);
13694
13695 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 if (*tag_pattern == NUL)
13697 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013698
13699 l = list_alloc();
13700 if (l != NULL)
13701 {
13702 if (get_tags(l, tag_pattern) != FAIL)
13703 {
13704 rettv->vval.v_list = l;
13705 rettv->v_type = VAR_LIST;
13706 ++l->lv_refcount;
13707 }
13708 else
13709 list_free(l);
13710 }
13711}
13712
13713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 * "tempname()" function
13715 */
13716/*ARGSUSED*/
13717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013719 typval_T *argvars;
13720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
13722 static int x = 'A';
13723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->v_type = VAR_STRING;
13725 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726
13727 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13728 * names. Skip 'I' and 'O', they are used for shell redirection. */
13729 do
13730 {
13731 if (x == 'Z')
13732 x = '0';
13733 else if (x == '9')
13734 x = 'A';
13735 else
13736 {
13737#ifdef EBCDIC
13738 if (x == 'I')
13739 x = 'J';
13740 else if (x == 'R')
13741 x = 'S';
13742 else
13743#endif
13744 ++x;
13745 }
13746 } while (x == 'I' || x == 'O');
13747}
13748
13749/*
13750 * "tolower(string)" function
13751 */
13752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *argvars;
13755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
13757 char_u *p;
13758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 p = vim_strsave(get_tv_string(&argvars[0]));
13760 rettv->v_type = VAR_STRING;
13761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762
13763 if (p != NULL)
13764 while (*p != NUL)
13765 {
13766#ifdef FEAT_MBYTE
13767 int l;
13768
13769 if (enc_utf8)
13770 {
13771 int c, lc;
13772
13773 c = utf_ptr2char(p);
13774 lc = utf_tolower(c);
13775 l = utf_ptr2len_check(p);
13776 /* TODO: reallocate string when byte count changes. */
13777 if (utf_char2len(lc) == l)
13778 utf_char2bytes(lc, p);
13779 p += l;
13780 }
13781 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13782 p += l; /* skip multi-byte character */
13783 else
13784#endif
13785 {
13786 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13787 ++p;
13788 }
13789 }
13790}
13791
13792/*
13793 * "toupper(string)" function
13794 */
13795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802}
13803
13804/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013805 * "tr(string, fromstr, tostr)" function
13806 */
13807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013809 typval_T *argvars;
13810 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013811{
13812 char_u *instr;
13813 char_u *fromstr;
13814 char_u *tostr;
13815 char_u *p;
13816#ifdef FEAT_MBYTE
13817 int inlen;
13818 int fromlen;
13819 int tolen;
13820 int idx;
13821 char_u *cpstr;
13822 int cplen;
13823 int first = TRUE;
13824#endif
13825 char_u buf[NUMBUFLEN];
13826 char_u buf2[NUMBUFLEN];
13827 garray_T ga;
13828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13831 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013832
13833 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 rettv->v_type = VAR_STRING;
13835 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013836 if (fromstr == NULL || tostr == NULL)
13837 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000013838 ga_init2(&ga, (int)sizeof(char), 80);
13839
13840#ifdef FEAT_MBYTE
13841 if (!has_mbyte)
13842#endif
13843 /* not multi-byte: fromstr and tostr must be the same length */
13844 if (STRLEN(fromstr) != STRLEN(tostr))
13845 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013846#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013847error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013848#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013849 EMSG2(_(e_invarg2), fromstr);
13850 ga_clear(&ga);
13851 return;
13852 }
13853
13854 /* fromstr and tostr have to contain the same number of chars */
13855 while (*instr != NUL)
13856 {
13857#ifdef FEAT_MBYTE
13858 if (has_mbyte)
13859 {
13860 inlen = mb_ptr2len_check(instr);
13861 cpstr = instr;
13862 cplen = inlen;
13863 idx = 0;
13864 for (p = fromstr; *p != NUL; p += fromlen)
13865 {
13866 fromlen = mb_ptr2len_check(p);
13867 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13868 {
13869 for (p = tostr; *p != NUL; p += tolen)
13870 {
13871 tolen = mb_ptr2len_check(p);
13872 if (idx-- == 0)
13873 {
13874 cplen = tolen;
13875 cpstr = p;
13876 break;
13877 }
13878 }
13879 if (*p == NUL) /* tostr is shorter than fromstr */
13880 goto error;
13881 break;
13882 }
13883 ++idx;
13884 }
13885
13886 if (first && cpstr == instr)
13887 {
13888 /* Check that fromstr and tostr have the same number of
13889 * (multi-byte) characters. Done only once when a character
13890 * of instr doesn't appear in fromstr. */
13891 first = FALSE;
13892 for (p = tostr; *p != NUL; p += tolen)
13893 {
13894 tolen = mb_ptr2len_check(p);
13895 --idx;
13896 }
13897 if (idx != 0)
13898 goto error;
13899 }
13900
13901 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013902 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013903 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013904
13905 instr += inlen;
13906 }
13907 else
13908#endif
13909 {
13910 /* When not using multi-byte chars we can do it faster. */
13911 p = vim_strchr(fromstr, *instr);
13912 if (p != NULL)
13913 ga_append(&ga, tostr[p - fromstr]);
13914 else
13915 ga_append(&ga, *instr);
13916 ++instr;
13917 }
13918 }
13919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013921}
13922
13923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 * "type(expr)" function
13925 */
13926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013928 typval_T *argvars;
13929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013931 int n;
13932
13933 switch (argvars[0].v_type)
13934 {
13935 case VAR_NUMBER: n = 0; break;
13936 case VAR_STRING: n = 1; break;
13937 case VAR_FUNC: n = 2; break;
13938 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013939 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013940 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13941 }
13942 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943}
13944
13945/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013946 * "values(dict)" function
13947 */
13948 static void
13949f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013950 typval_T *argvars;
13951 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013952{
13953 dict_list(argvars, rettv, 1);
13954}
13955
13956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 * "virtcol(string)" function
13958 */
13959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963{
13964 colnr_T vcol = 0;
13965 pos_T *fp;
13966
13967 fp = var2fpos(&argvars[0], FALSE);
13968 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13969 {
13970 getvvcol(curwin, fp, NULL, NULL, &vcol);
13971 ++vcol;
13972 }
13973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975}
13976
13977/*
13978 * "visualmode()" function
13979 */
13980/*ARGSUSED*/
13981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013983 typval_T *argvars;
13984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985{
13986#ifdef FEAT_VISUAL
13987 char_u str[2];
13988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 str[0] = curbuf->b_visual_mode_eval;
13991 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993
13994 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013995 if ((argvars[0].v_type == VAR_NUMBER
13996 && argvars[0].vval.v_number != 0)
13997 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 curbuf->b_visual_mode_eval = NUL;
14000#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002#endif
14003}
14004
14005/*
14006 * "winbufnr(nr)" function
14007 */
14008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *argvars;
14011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012{
14013 win_T *wp;
14014
14015 wp = find_win_by_nr(&argvars[0]);
14016 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020}
14021
14022/*
14023 * "wincol()" function
14024 */
14025/*ARGSUSED*/
14026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014028 typval_T *argvars;
14029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030{
14031 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033}
14034
14035/*
14036 * "winheight(nr)" function
14037 */
14038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 typval_T *argvars;
14041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042{
14043 win_T *wp;
14044
14045 wp = find_win_by_nr(&argvars[0]);
14046 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050}
14051
14052/*
14053 * "winline()" function
14054 */
14055/*ARGSUSED*/
14056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *argvars;
14059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060{
14061 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014062 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063}
14064
14065/*
14066 * "winnr()" function
14067 */
14068/* ARGSUSED */
14069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014071 typval_T *argvars;
14072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073{
14074 int nr = 1;
14075#ifdef FEAT_WINDOWS
14076 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014077 win_T *twin = curwin;
14078 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 arg = get_tv_string_chk(&argvars[0]);
14083 if (arg == NULL)
14084 nr = 0; /* type error; errmsg already given */
14085 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014086 twin = lastwin;
14087 else if (STRCMP(arg, "#") == 0)
14088 {
14089 twin = prevwin;
14090 if (prevwin == NULL)
14091 nr = 0;
14092 }
14093 else
14094 {
14095 EMSG2(_(e_invexpr2), arg);
14096 nr = 0;
14097 }
14098 }
14099
14100 if (nr > 0)
14101 for (wp = firstwin; wp != twin; wp = wp->w_next)
14102 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105}
14106
14107/*
14108 * "winrestcmd()" function
14109 */
14110/* ARGSUSED */
14111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 typval_T *argvars;
14114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115{
14116#ifdef FEAT_WINDOWS
14117 win_T *wp;
14118 int winnr = 1;
14119 garray_T ga;
14120 char_u buf[50];
14121
14122 ga_init2(&ga, (int)sizeof(char), 70);
14123 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14124 {
14125 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14126 ga_concat(&ga, buf);
14127# ifdef FEAT_VERTSPLIT
14128 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14129 ga_concat(&ga, buf);
14130# endif
14131 ++winnr;
14132 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014133 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140}
14141
14142/*
14143 * "winwidth(nr)" function
14144 */
14145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014147 typval_T *argvars;
14148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149{
14150 win_T *wp;
14151
14152 wp = find_win_by_nr(&argvars[0]);
14153 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 else
14156#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160#endif
14161}
14162
14163 static win_T *
14164find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166{
14167#ifdef FEAT_WINDOWS
14168 win_T *wp;
14169#endif
14170 int nr;
14171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 nr = get_tv_number_chk(vp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173
14174#ifdef FEAT_WINDOWS
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 if (nr < 0)
14176 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 if (nr == 0)
14178 return curwin;
14179
14180 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14181 if (--nr <= 0)
14182 break;
14183 return wp;
14184#else
14185 if (nr == 0 || nr == 1)
14186 return curwin;
14187 return NULL;
14188#endif
14189}
14190
14191/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014192 * "writefile()" function
14193 */
14194 static void
14195f_writefile(argvars, rettv)
14196 typval_T *argvars;
14197 typval_T *rettv;
14198{
14199 int binary = FALSE;
14200 char_u *fname;
14201 FILE *fd;
14202 listitem_T *li;
14203 char_u *s;
14204 int ret = 0;
14205 int c;
14206
14207 if (argvars[0].v_type != VAR_LIST)
14208 {
14209 EMSG2(_(e_listarg), "writefile()");
14210 return;
14211 }
14212 if (argvars[0].vval.v_list == NULL)
14213 return;
14214
14215 if (argvars[2].v_type != VAR_UNKNOWN
14216 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14217 binary = TRUE;
14218
14219 /* Always open the file in binary mode, library functions have a mind of
14220 * their own about CR-LF conversion. */
14221 fname = get_tv_string(&argvars[1]);
14222 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14223 {
14224 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14225 ret = -1;
14226 }
14227 else
14228 {
14229 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14230 li = li->li_next)
14231 {
14232 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14233 {
14234 if (*s == '\n')
14235 c = putc(NUL, fd);
14236 else
14237 c = putc(*s, fd);
14238 if (c == EOF)
14239 {
14240 ret = -1;
14241 break;
14242 }
14243 }
14244 if (!binary || li->li_next != NULL)
14245 if (putc('\n', fd) == EOF)
14246 {
14247 ret = -1;
14248 break;
14249 }
14250 if (ret < 0)
14251 {
14252 EMSG(_(e_write));
14253 break;
14254 }
14255 }
14256 fclose(fd);
14257 }
14258
14259 rettv->vval.v_number = ret;
14260}
14261
14262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 * Translate a String variable into a position.
14264 */
14265 static pos_T *
14266var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014267 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 int lnum; /* TRUE when $ is last line */
14269{
14270 char_u *name;
14271 static pos_T pos;
14272 pos_T *pp;
14273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 name = get_tv_string_chk(varp);
14275 if (name == NULL)
14276 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 if (name[0] == '.') /* cursor */
14278 return &curwin->w_cursor;
14279 if (name[0] == '\'') /* mark */
14280 {
14281 pp = getmark(name[1], FALSE);
14282 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14283 return NULL;
14284 return pp;
14285 }
14286 if (name[0] == '$') /* last column or line */
14287 {
14288 if (lnum)
14289 {
14290 pos.lnum = curbuf->b_ml.ml_line_count;
14291 pos.col = 0;
14292 }
14293 else
14294 {
14295 pos.lnum = curwin->w_cursor.lnum;
14296 pos.col = (colnr_T)STRLEN(ml_get_curline());
14297 }
14298 return &pos;
14299 }
14300 return NULL;
14301}
14302
14303/*
14304 * Get the length of an environment variable name.
14305 * Advance "arg" to the first character after the name.
14306 * Return 0 for error.
14307 */
14308 static int
14309get_env_len(arg)
14310 char_u **arg;
14311{
14312 char_u *p;
14313 int len;
14314
14315 for (p = *arg; vim_isIDc(*p); ++p)
14316 ;
14317 if (p == *arg) /* no name found */
14318 return 0;
14319
14320 len = (int)(p - *arg);
14321 *arg = p;
14322 return len;
14323}
14324
14325/*
14326 * Get the length of the name of a function or internal variable.
14327 * "arg" is advanced to the first non-white character after the name.
14328 * Return 0 if something is wrong.
14329 */
14330 static int
14331get_id_len(arg)
14332 char_u **arg;
14333{
14334 char_u *p;
14335 int len;
14336
14337 /* Find the end of the name. */
14338 for (p = *arg; eval_isnamec(*p); ++p)
14339 ;
14340 if (p == *arg) /* no name found */
14341 return 0;
14342
14343 len = (int)(p - *arg);
14344 *arg = skipwhite(p);
14345
14346 return len;
14347}
14348
14349/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014350 * Get the length of the name of a variable or function.
14351 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014353 * Return -1 if curly braces expansion failed.
14354 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 * If the name contains 'magic' {}'s, expand them and return the
14356 * expanded name in an allocated string via 'alias' - caller must free.
14357 */
14358 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014359get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 char_u **arg;
14361 char_u **alias;
14362 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014363 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364{
14365 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 char_u *p;
14367 char_u *expr_start;
14368 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369
14370 *alias = NULL; /* default to no alias */
14371
14372 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14373 && (*arg)[2] == (int)KE_SNR)
14374 {
14375 /* hard coded <SNR>, already translated */
14376 *arg += 3;
14377 return get_id_len(arg) + 3;
14378 }
14379 len = eval_fname_script(*arg);
14380 if (len > 0)
14381 {
14382 /* literal "<SID>", "s:" or "<SNR>" */
14383 *arg += len;
14384 }
14385
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014389 p = find_name_end(*arg, &expr_start, &expr_end,
14390 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (expr_start != NULL)
14392 {
14393 char_u *temp_string;
14394
14395 if (!evaluate)
14396 {
14397 len += (int)(p - *arg);
14398 *arg = skipwhite(p);
14399 return len;
14400 }
14401
14402 /*
14403 * Include any <SID> etc in the expanded string:
14404 * Thus the -len here.
14405 */
14406 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14407 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014408 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 *alias = temp_string;
14410 *arg = skipwhite(p);
14411 return (int)STRLEN(temp_string);
14412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413
14414 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014415 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 EMSG2(_(e_invexpr2), *arg);
14417
14418 return len;
14419}
14420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421/*
14422 * Find the end of a variable or function name, taking care of magic braces.
14423 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14424 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014425 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 * Return a pointer to just after the name. Equal to "arg" if there is no
14427 * valid name.
14428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014430find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 char_u *arg;
14432 char_u **expr_start;
14433 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014434 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 int mb_nest = 0;
14437 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 char_u *p;
14439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442 *expr_start = NULL;
14443 *expr_end = NULL;
14444 }
14445
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014446 /* Quick check for valid starting character. */
14447 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14448 return arg;
14449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450 for (p = arg; *p != NUL
14451 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014452 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014453 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 || mb_nest != 0
14455 || br_nest != 0); ++p)
14456 {
14457 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 if (*p == '[')
14460 ++br_nest;
14461 else if (*p == ']')
14462 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 if (*p == '{')
14467 {
14468 mb_nest++;
14469 if (expr_start != NULL && *expr_start == NULL)
14470 *expr_start = p;
14471 }
14472 else if (*p == '}')
14473 {
14474 mb_nest--;
14475 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14476 *expr_end = p;
14477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 }
14480
14481 return p;
14482}
14483
14484/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014485 * Expands out the 'magic' {}'s in a variable/function name.
14486 * Note that this can call itself recursively, to deal with
14487 * constructs like foo{bar}{baz}{bam}
14488 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14489 * "in_start" ^
14490 * "expr_start" ^
14491 * "expr_end" ^
14492 * "in_end" ^
14493 *
14494 * Returns a new allocated string, which the caller must free.
14495 * Returns NULL for failure.
14496 */
14497 static char_u *
14498make_expanded_name(in_start, expr_start, expr_end, in_end)
14499 char_u *in_start;
14500 char_u *expr_start;
14501 char_u *expr_end;
14502 char_u *in_end;
14503{
14504 char_u c1;
14505 char_u *retval = NULL;
14506 char_u *temp_result;
14507 char_u *nextcmd = NULL;
14508
14509 if (expr_end == NULL || in_end == NULL)
14510 return NULL;
14511 *expr_start = NUL;
14512 *expr_end = NUL;
14513 c1 = *in_end;
14514 *in_end = NUL;
14515
14516 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14517 if (temp_result != NULL && nextcmd == NULL)
14518 {
14519 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14520 + (in_end - expr_end) + 1));
14521 if (retval != NULL)
14522 {
14523 STRCPY(retval, in_start);
14524 STRCAT(retval, temp_result);
14525 STRCAT(retval, expr_end + 1);
14526 }
14527 }
14528 vim_free(temp_result);
14529
14530 *in_end = c1; /* put char back for error messages */
14531 *expr_start = '{';
14532 *expr_end = '}';
14533
14534 if (retval != NULL)
14535 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014536 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014537 if (expr_start != NULL)
14538 {
14539 /* Further expansion! */
14540 temp_result = make_expanded_name(retval, expr_start,
14541 expr_end, temp_result);
14542 vim_free(retval);
14543 retval = temp_result;
14544 }
14545 }
14546
14547 return retval;
14548}
14549
14550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014552 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 */
14554 static int
14555eval_isnamec(c)
14556 int c;
14557{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014558 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14559}
14560
14561/*
14562 * Return TRUE if character "c" can be used as the first character in a
14563 * variable or function name (excluding '{' and '}').
14564 */
14565 static int
14566eval_isnamec1(c)
14567 int c;
14568{
14569 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570}
14571
14572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 * Set number v: variable to "val".
14574 */
14575 void
14576set_vim_var_nr(idx, val)
14577 int idx;
14578 long val;
14579{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014580 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581}
14582
14583/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014584 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 */
14586 long
14587get_vim_var_nr(idx)
14588 int idx;
14589{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014590 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591}
14592
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014593#if defined(FEAT_AUTOCMD) || defined(PROTO)
14594/*
14595 * Get string v: variable value. Uses a static buffer, can only be used once.
14596 */
14597 char_u *
14598get_vim_var_str(idx)
14599 int idx;
14600{
14601 return get_tv_string(&vimvars[idx].vv_tv);
14602}
14603#endif
14604
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605/*
14606 * Set v:count, v:count1 and v:prevcount.
14607 */
14608 void
14609set_vcount(count, count1)
14610 long count;
14611 long count1;
14612{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014613 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14614 vimvars[VV_COUNT].vv_nr = count;
14615 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616}
14617
14618/*
14619 * Set string v: variable to a copy of "val".
14620 */
14621 void
14622set_vim_var_string(idx, val, len)
14623 int idx;
14624 char_u *val;
14625 int len; /* length of "val" to use or -1 (whole string) */
14626{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014627 /* Need to do this (at least) once, since we can't initialize a union.
14628 * Will always be invoked when "v:progname" is set. */
14629 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14630
Bram Moolenaare9a41262005-01-15 22:18:47 +000014631 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014633 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014635 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014637 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638}
14639
14640/*
14641 * Set v:register if needed.
14642 */
14643 void
14644set_reg_var(c)
14645 int c;
14646{
14647 char_u regname;
14648
14649 if (c == 0 || c == ' ')
14650 regname = '"';
14651 else
14652 regname = c;
14653 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014654 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 set_vim_var_string(VV_REG, &regname, 1);
14656}
14657
14658/*
14659 * Get or set v:exception. If "oldval" == NULL, return the current value.
14660 * Otherwise, restore the value to "oldval" and return NULL.
14661 * Must always be called in pairs to save and restore v:exception! Does not
14662 * take care of memory allocations.
14663 */
14664 char_u *
14665v_exception(oldval)
14666 char_u *oldval;
14667{
14668 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014669 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaare9a41262005-01-15 22:18:47 +000014671 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 return NULL;
14673}
14674
14675/*
14676 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14677 * Otherwise, restore the value to "oldval" and return NULL.
14678 * Must always be called in pairs to save and restore v:throwpoint! Does not
14679 * take care of memory allocations.
14680 */
14681 char_u *
14682v_throwpoint(oldval)
14683 char_u *oldval;
14684{
14685 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014686 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687
Bram Moolenaare9a41262005-01-15 22:18:47 +000014688 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 return NULL;
14690}
14691
14692#if defined(FEAT_AUTOCMD) || defined(PROTO)
14693/*
14694 * Set v:cmdarg.
14695 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14696 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14697 * Must always be called in pairs!
14698 */
14699 char_u *
14700set_cmdarg(eap, oldarg)
14701 exarg_T *eap;
14702 char_u *oldarg;
14703{
14704 char_u *oldval;
14705 char_u *newval;
14706 unsigned len;
14707
Bram Moolenaare9a41262005-01-15 22:18:47 +000014708 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014709 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014711 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014712 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014713 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714 }
14715
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014716 if (eap->force_bin == FORCE_BIN)
14717 len = 6;
14718 else if (eap->force_bin == FORCE_NOBIN)
14719 len = 8;
14720 else
14721 len = 0;
14722 if (eap->force_ff != 0)
14723 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14724# ifdef FEAT_MBYTE
14725 if (eap->force_enc != 0)
14726 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14727# endif
14728
14729 newval = alloc(len + 1);
14730 if (newval == NULL)
14731 return NULL;
14732
14733 if (eap->force_bin == FORCE_BIN)
14734 sprintf((char *)newval, " ++bin");
14735 else if (eap->force_bin == FORCE_NOBIN)
14736 sprintf((char *)newval, " ++nobin");
14737 else
14738 *newval = NUL;
14739 if (eap->force_ff != 0)
14740 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14741 eap->cmd + eap->force_ff);
14742# ifdef FEAT_MBYTE
14743 if (eap->force_enc != 0)
14744 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14745 eap->cmd + eap->force_enc);
14746# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014747 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014748 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749}
14750#endif
14751
14752/*
14753 * Get the value of internal variable "name".
14754 * Return OK or FAIL.
14755 */
14756 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 char_u *name;
14759 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014760 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014761 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762{
14763 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 typval_T *tv = NULL;
14765 typval_T atv;
14766 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768
14769 /* truncate the name, so that we can use strcmp() */
14770 cc = name[len];
14771 name[len] = NUL;
14772
14773 /*
14774 * Check for "b:changedtick".
14775 */
14776 if (STRCMP(name, "b:changedtick") == 0)
14777 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014778 atv.v_type = VAR_NUMBER;
14779 atv.vval.v_number = curbuf->b_changedtick;
14780 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 }
14782
14783 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 * Check for user-defined variables.
14785 */
14786 else
14787 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014788 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 }
14792
Bram Moolenaare9a41262005-01-15 22:18:47 +000014793 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014795 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797 ret = FAIL;
14798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014799 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014800 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801
14802 name[len] = cc;
14803
14804 return ret;
14805}
14806
14807/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014808 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14809 * Also handle function call with Funcref variable: func(expr)
14810 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14811 */
14812 static int
14813handle_subscript(arg, rettv, evaluate, verbose)
14814 char_u **arg;
14815 typval_T *rettv;
14816 int evaluate; /* do more than finding the end */
14817 int verbose; /* give error messages */
14818{
14819 int ret = OK;
14820 dict_T *selfdict = NULL;
14821 char_u *s;
14822 int len;
14823
14824 while (ret == OK
14825 && (**arg == '['
14826 || (**arg == '.' && rettv->v_type == VAR_DICT)
14827 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14828 && !vim_iswhite(*(*arg - 1)))
14829 {
14830 if (**arg == '(')
14831 {
14832 s = rettv->vval.v_string;
14833
14834 /* Invoke the function. Recursive! */
14835 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14836 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14837 &len, evaluate, selfdict);
14838
14839 /* Stop the expression evaluation when immediately aborting on
14840 * error, or when an interrupt occurred or an exception was thrown
14841 * but not caught. */
14842 if (aborting())
14843 {
14844 if (ret == OK)
14845 clear_tv(rettv);
14846 ret = FAIL;
14847 }
14848 dict_unref(selfdict);
14849 selfdict = NULL;
14850 }
14851 else /* **arg == '[' || **arg == '.' */
14852 {
14853 dict_unref(selfdict);
14854 if (rettv->v_type == VAR_DICT)
14855 {
14856 selfdict = rettv->vval.v_dict;
14857 if (selfdict != NULL)
14858 ++selfdict->dv_refcount;
14859 }
14860 else
14861 selfdict = NULL;
14862 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14863 {
14864 clear_tv(rettv);
14865 ret = FAIL;
14866 }
14867 }
14868 }
14869 dict_unref(selfdict);
14870 return ret;
14871}
14872
14873/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014874 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14875 * value).
14876 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014877 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879{
Bram Moolenaar33570922005-01-25 22:26:29 +000014880 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881}
14882
14883/*
14884 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 * The string "s" must have been allocated, it is consumed.
14886 * Return NULL for out of memory, the variable otherwise.
14887 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014888 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 char_u *s;
14891{
Bram Moolenaar33570922005-01-25 22:26:29 +000014892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 rettv = alloc_tv();
14895 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 rettv->v_type = VAR_STRING;
14898 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 }
14900 else
14901 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014902 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903}
14904
14905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014906 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 */
14908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014910 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911{
14912 if (varp != NULL)
14913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014914 switch (varp->v_type)
14915 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014916 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014917 func_unref(varp->vval.v_string);
14918 /*FALLTHROUGH*/
14919 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014920 vim_free(varp->vval.v_string);
14921 break;
14922 case VAR_LIST:
14923 list_unref(varp->vval.v_list);
14924 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014925 case VAR_DICT:
14926 dict_unref(varp->vval.v_dict);
14927 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014928 case VAR_NUMBER:
14929 case VAR_UNKNOWN:
14930 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014931 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014932 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014933 break;
14934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 vim_free(varp);
14936 }
14937}
14938
14939/*
14940 * Free the memory for a variable value and set the value to NULL or 0.
14941 */
14942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014943clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014944 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945{
14946 if (varp != NULL)
14947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014948 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014950 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014951 func_unref(varp->vval.v_string);
14952 /*FALLTHROUGH*/
14953 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014954 vim_free(varp->vval.v_string);
14955 varp->vval.v_string = NULL;
14956 break;
14957 case VAR_LIST:
14958 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014959 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014960 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014961 case VAR_DICT:
14962 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014963 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014964 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014965 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014966 varp->vval.v_number = 0;
14967 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014968 case VAR_UNKNOWN:
14969 break;
14970 default:
14971 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014973 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 }
14975}
14976
14977/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 * Set the value of a variable to NULL without freeing items.
14979 */
14980 static void
14981init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014982 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983{
14984 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986}
14987
14988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 * Get the number value of a variable.
14990 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014991 * For incompatible types, return 0.
14992 * get_tv_number_chk() is similar to get_tv_number(), but informs the
14993 * caller of incompatible types: it sets *denote to TRUE if "denote"
14994 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 */
14996 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014997get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014998 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000 int error = FALSE;
15001
15002 return get_tv_number_chk(varp, &error); /* return 0L on error */
15003}
15004
15005 static long
15006get_tv_number_chk(varp, denote)
15007 typval_T *varp;
15008 int *denote;
15009{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015010 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015012 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015014 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015016 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015017 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015018 break;
15019 case VAR_STRING:
15020 if (varp->vval.v_string != NULL)
15021 vim_str2nr(varp->vval.v_string, NULL, NULL,
15022 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015023 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015024 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015025 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015026 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015027 case VAR_DICT:
15028 EMSG(_("E728: Using a Dictionary as a number"));
15029 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015030 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015031 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 if (denote == NULL) /* useful for values that must be unsigned */
15035 n = -1;
15036 else
15037 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015038 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039}
15040
15041/*
15042 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015043 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 */
15045 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015046get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015047 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048{
Bram Moolenaar33570922005-01-25 22:26:29 +000015049 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 linenr_T lnum;
15051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015052 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 if (lnum == 0) /* no valid number, try using line() */
15054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 rettv.v_type = VAR_NUMBER;
15056 f_line(argvars, &rettv);
15057 lnum = rettv.vval.v_number;
15058 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 }
15060 return lnum;
15061}
15062
15063/*
15064 * Get the string value of a variable.
15065 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015066 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15067 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 * If the String variable has never been set, return an empty string.
15069 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15071 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072 */
15073 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015074get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015075 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076{
15077 static char_u mybuf[NUMBUFLEN];
15078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080}
15081
15082 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015084 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015085 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087 char_u *res = get_tv_string_buf_chk(varp, buf);
15088
15089 return res != NULL ? res : (char_u *)"";
15090}
15091
15092 static char_u *
15093get_tv_string_chk(varp)
15094 typval_T *varp;
15095{
15096 static char_u mybuf[NUMBUFLEN];
15097
15098 return get_tv_string_buf_chk(varp, mybuf);
15099}
15100
15101 static char_u *
15102get_tv_string_buf_chk(varp, buf)
15103 typval_T *varp;
15104 char_u *buf;
15105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015106 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015108 case VAR_NUMBER:
15109 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15110 return buf;
15111 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015112 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015113 break;
15114 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015115 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015116 break;
15117 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015118 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015119 break;
15120 case VAR_STRING:
15121 if (varp->vval.v_string != NULL)
15122 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015124 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015125 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129}
15130
15131/*
15132 * Find variable "name" in the list of variables.
15133 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015134 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015135 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015139find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015144 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145
Bram Moolenaara7043832005-01-21 11:56:39 +000015146 ht = find_var_ht(name, &varname);
15147 if (htp != NULL)
15148 *htp = ht;
15149 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015151 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152}
15153
15154/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015156 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015158 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015159find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015161 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015162 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015163{
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 hashitem_T *hi;
15165
15166 if (*varname == NUL)
15167 {
15168 /* Must be something like "s:", otherwise "ht" would be NULL. */
15169 switch (varname[-2])
15170 {
15171 case 's': return &SCRIPT_SV(current_SID).sv_var;
15172 case 'g': return &globvars_var;
15173 case 'v': return &vimvars_var;
15174 case 'b': return &curbuf->b_bufvar;
15175 case 'w': return &curwin->w_winvar;
15176 case 'l': return &current_funccal->l_vars_var;
15177 case 'a': return &current_funccal->l_avars_var;
15178 }
15179 return NULL;
15180 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015181
15182 hi = hash_find(ht, varname);
15183 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015184 {
15185 /* For global variables we may try auto-loading the script. If it
15186 * worked find the variable again. */
15187 if (ht == &globvarht && !writing
15188 && script_autoload(varname) && !aborting())
15189 hi = hash_find(ht, varname);
15190 if (HASHITEM_EMPTY(hi))
15191 return NULL;
15192 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015194}
15195
15196/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015198 * Set "varname" to the start of name without ':'.
15199 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015201find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 char_u *name;
15203 char_u **varname;
15204{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015205 hashitem_T *hi;
15206
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 if (name[1] != ':')
15208 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015209 /* The name must not start with a colon or #. */
15210 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 return NULL;
15212 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015213
15214 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015215 hi = hash_find(&compat_hashtab, name);
15216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015217 return &compat_hashtab;
15218
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015220 return &globvarht; /* global variable */
15221 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 }
15223 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015224 if (*name == 'g') /* global variable */
15225 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015226 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15227 */
15228 if (vim_strchr(name + 2, ':') != NULL
15229 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015230 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 if (*name == 'v') /* v: variable */
15236 return &vimvarht;
15237 if (*name == 'a' && current_funccal != NULL) /* function argument */
15238 return &current_funccal->l_avars.dv_hashtab;
15239 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15240 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 if (*name == 's' /* script variable */
15242 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15243 return &SCRIPT_VARS(current_SID);
15244 return NULL;
15245}
15246
15247/*
15248 * Get the string value of a (global/local) variable.
15249 * Returns NULL when it doesn't exist.
15250 */
15251 char_u *
15252get_var_value(name)
15253 char_u *name;
15254{
Bram Moolenaar33570922005-01-25 22:26:29 +000015255 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256
Bram Moolenaara7043832005-01-21 11:56:39 +000015257 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 if (v == NULL)
15259 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261}
15262
15263/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015264 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 * sourcing this script and when executing functions defined in the script.
15266 */
15267 void
15268new_script_vars(id)
15269 scid_T id;
15270{
Bram Moolenaara7043832005-01-21 11:56:39 +000015271 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 hashtab_T *ht;
15273 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015274
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15276 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015277 /* Re-allocating ga_data means that an ht_array pointing to
15278 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015279 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015280 for (i = 1; i <= ga_scripts.ga_len; ++i)
15281 {
15282 ht = &SCRIPT_VARS(i);
15283 if (ht->ht_mask == HT_INIT_SIZE - 1)
15284 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015285 sv = &SCRIPT_SV(i);
15286 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015287 }
15288
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 while (ga_scripts.ga_len < id)
15290 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015291 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15292 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 }
15295 }
15296}
15297
15298/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015299 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15300 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 */
15302 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015303init_var_dict(dict, dict_var)
15304 dict_T *dict;
15305 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306{
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 hash_init(&dict->dv_hashtab);
15308 dict->dv_refcount = 99999;
15309 dict_var->di_tv.vval.v_dict = dict;
15310 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015311 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015312 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15313 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314}
15315
15316/*
15317 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 * Frees all allocated variables and the value they contain.
15319 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 */
15321 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015322vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 hashtab_T *ht;
15324{
15325 vars_clear_ext(ht, TRUE);
15326}
15327
15328/*
15329 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15330 */
15331 static void
15332vars_clear_ext(ht, free_val)
15333 hashtab_T *ht;
15334 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335{
Bram Moolenaara7043832005-01-21 11:56:39 +000015336 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015337 hashitem_T *hi;
15338 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339
Bram Moolenaar33570922005-01-25 22:26:29 +000015340 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015341 todo = ht->ht_used;
15342 for (hi = ht->ht_array; todo > 0; ++hi)
15343 {
15344 if (!HASHITEM_EMPTY(hi))
15345 {
15346 --todo;
15347
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015349 * ht_array might change then. hash_clear() takes care of it
15350 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015351 v = HI2DI(hi);
15352 if (free_val)
15353 clear_tv(&v->di_tv);
15354 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15355 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015356 }
15357 }
15358 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359}
15360
Bram Moolenaara7043832005-01-21 11:56:39 +000015361/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015362 * Delete a variable from hashtab "ht" at item "hi".
15363 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015366delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015367 hashtab_T *ht;
15368 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369{
Bram Moolenaar33570922005-01-25 22:26:29 +000015370 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015371
15372 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015373 clear_tv(&di->di_tv);
15374 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375}
15376
15377/*
15378 * List the value of one internal variable.
15379 */
15380 static void
15381list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015382 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 char_u *prefix;
15384{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015385 char_u *tofree;
15386 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015387 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015388
Bram Moolenaar33570922005-01-25 22:26:29 +000015389 s = echo_string(&v->di_tv, &tofree, numbuf);
15390 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015391 s == NULL ? (char_u *)"" : s);
15392 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393}
15394
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 static void
15396list_one_var_a(prefix, name, type, string)
15397 char_u *prefix;
15398 char_u *name;
15399 int type;
15400 char_u *string;
15401{
15402 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15403 if (name != NULL) /* "a:" vars don't have a name stored */
15404 msg_puts(name);
15405 msg_putchar(' ');
15406 msg_advance(22);
15407 if (type == VAR_NUMBER)
15408 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015409 else if (type == VAR_FUNC)
15410 msg_putchar('*');
15411 else if (type == VAR_LIST)
15412 {
15413 msg_putchar('[');
15414 if (*string == '[')
15415 ++string;
15416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015417 else if (type == VAR_DICT)
15418 {
15419 msg_putchar('{');
15420 if (*string == '{')
15421 ++string;
15422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 else
15424 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015425
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015427
15428 if (type == VAR_FUNC)
15429 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
15432/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 * If the variable already exists, the value is updated.
15435 * Otherwise the variable is created.
15436 */
15437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015440 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442{
Bram Moolenaar33570922005-01-25 22:26:29 +000015443 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015445 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015448 {
15449 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15450 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15451 ? name[2] : name[0]))
15452 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015453 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015454 return;
15455 }
15456 if (function_exists(name))
15457 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015458 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15459 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015460 return;
15461 }
15462 }
15463
Bram Moolenaara7043832005-01-21 11:56:39 +000015464 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015466 {
15467 EMSG2(_("E461: Illegal variable name: %s"), name);
15468 return;
15469 }
15470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015471 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015474 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015475 if (var_check_ro(v->di_flags, name)
15476 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015477 return;
15478 if (v->di_tv.v_type != tv->v_type
15479 && !((v->di_tv.v_type == VAR_STRING
15480 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 && (tv->v_type == VAR_STRING
15482 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015483 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015484 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 return;
15486 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015487
15488 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015489 * Handle setting internal v: variables separately: we don't change
15490 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015491 */
15492 if (ht == &vimvarht)
15493 {
15494 if (v->di_tv.v_type == VAR_STRING)
15495 {
15496 vim_free(v->di_tv.vval.v_string);
15497 if (copy || tv->v_type != VAR_STRING)
15498 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15499 else
15500 {
15501 /* Take over the string to avoid an extra alloc/free. */
15502 v->di_tv.vval.v_string = tv->vval.v_string;
15503 tv->vval.v_string = NULL;
15504 }
15505 }
15506 else if (v->di_tv.v_type != VAR_NUMBER)
15507 EMSG2(_(e_intern2), "set_var()");
15508 else
15509 v->di_tv.vval.v_number = get_tv_number(tv);
15510 return;
15511 }
15512
15513 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 }
15515 else /* add a new variable */
15516 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015517 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15518 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015519 if (v == NULL)
15520 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015521 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015522 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015524 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 return;
15526 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015527 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015530 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015531 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015532 else
15533 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015534 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015535 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538}
15539
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015540/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015541 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15542 * Also give an error message.
15543 */
15544 static int
15545var_check_ro(flags, name)
15546 int flags;
15547 char_u *name;
15548{
15549 if (flags & DI_FLAGS_RO)
15550 {
15551 EMSG2(_(e_readonlyvar), name);
15552 return TRUE;
15553 }
15554 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15555 {
15556 EMSG2(_(e_readonlysbx), name);
15557 return TRUE;
15558 }
15559 return FALSE;
15560}
15561
15562/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015563 * Return TRUE if typeval "tv" is set to be locked (immutable).
15564 * Also give an error message, using "name".
15565 */
15566 static int
15567tv_check_lock(lock, name)
15568 int lock;
15569 char_u *name;
15570{
15571 if (lock & VAR_LOCKED)
15572 {
15573 EMSG2(_("E741: Value is locked: %s"),
15574 name == NULL ? (char_u *)_("Unknown") : name);
15575 return TRUE;
15576 }
15577 if (lock & VAR_FIXED)
15578 {
15579 EMSG2(_("E742: Cannot change value of %s"),
15580 name == NULL ? (char_u *)_("Unknown") : name);
15581 return TRUE;
15582 }
15583 return FALSE;
15584}
15585
15586/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015587 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015588 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015589 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 typval_T *from;
15594 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015596 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015597 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015598 switch (from->v_type)
15599 {
15600 case VAR_NUMBER:
15601 to->vval.v_number = from->vval.v_number;
15602 break;
15603 case VAR_STRING:
15604 case VAR_FUNC:
15605 if (from->vval.v_string == NULL)
15606 to->vval.v_string = NULL;
15607 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015609 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015610 if (from->v_type == VAR_FUNC)
15611 func_ref(to->vval.v_string);
15612 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015613 break;
15614 case VAR_LIST:
15615 if (from->vval.v_list == NULL)
15616 to->vval.v_list = NULL;
15617 else
15618 {
15619 to->vval.v_list = from->vval.v_list;
15620 ++to->vval.v_list->lv_refcount;
15621 }
15622 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015623 case VAR_DICT:
15624 if (from->vval.v_dict == NULL)
15625 to->vval.v_dict = NULL;
15626 else
15627 {
15628 to->vval.v_dict = from->vval.v_dict;
15629 ++to->vval.v_dict->dv_refcount;
15630 }
15631 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015632 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015634 break;
15635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636}
15637
15638/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015639 * Make a copy of an item.
15640 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015641 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15642 * reference to an already copied list/dict can be used.
15643 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015644 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015645 static int
15646item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015647 typval_T *from;
15648 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015649 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015650 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015651{
15652 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015653 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015654
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015656 {
15657 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015658 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015659 }
15660 ++recurse;
15661
15662 switch (from->v_type)
15663 {
15664 case VAR_NUMBER:
15665 case VAR_STRING:
15666 case VAR_FUNC:
15667 copy_tv(from, to);
15668 break;
15669 case VAR_LIST:
15670 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015671 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015672 if (from->vval.v_list == NULL)
15673 to->vval.v_list = NULL;
15674 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15675 {
15676 /* use the copy made earlier */
15677 to->vval.v_list = from->vval.v_list->lv_copylist;
15678 ++to->vval.v_list->lv_refcount;
15679 }
15680 else
15681 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15682 if (to->vval.v_list == NULL)
15683 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015684 break;
15685 case VAR_DICT:
15686 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015687 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015688 if (from->vval.v_dict == NULL)
15689 to->vval.v_dict = NULL;
15690 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15691 {
15692 /* use the copy made earlier */
15693 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15694 ++to->vval.v_dict->dv_refcount;
15695 }
15696 else
15697 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15698 if (to->vval.v_dict == NULL)
15699 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015700 break;
15701 default:
15702 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015703 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015704 }
15705 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015706 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015707}
15708
15709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 * ":echo expr1 ..." print each argument separated with a space, add a
15711 * newline at the end.
15712 * ":echon expr1 ..." print each argument plain.
15713 */
15714 void
15715ex_echo(eap)
15716 exarg_T *eap;
15717{
15718 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015720 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 char_u *p;
15722 int needclr = TRUE;
15723 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015724 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
15726 if (eap->skip)
15727 ++emsg_skip;
15728 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15729 {
15730 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 {
15733 /*
15734 * Report the invalid expression unless the expression evaluation
15735 * has been cancelled due to an aborting error, an interrupt, or an
15736 * exception.
15737 */
15738 if (!aborting())
15739 EMSG2(_(e_invexpr2), p);
15740 break;
15741 }
15742 if (!eap->skip)
15743 {
15744 if (atstart)
15745 {
15746 atstart = FALSE;
15747 /* Call msg_start() after eval1(), evaluating the expression
15748 * may cause a message to appear. */
15749 if (eap->cmdidx == CMD_echo)
15750 msg_start();
15751 }
15752 else if (eap->cmdidx == CMD_echo)
15753 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015754 p = echo_string(&rettv, &tofree, numbuf);
15755 if (p != NULL)
15756 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015758 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015760 if (*p != TAB && needclr)
15761 {
15762 /* remove any text still there from the command */
15763 msg_clr_eos();
15764 needclr = FALSE;
15765 }
15766 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 }
15768 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015769 {
15770#ifdef FEAT_MBYTE
15771 if (has_mbyte)
15772 {
15773 int i = (*mb_ptr2len_check)(p);
15774
15775 (void)msg_outtrans_len_attr(p, i, echo_attr);
15776 p += i - 1;
15777 }
15778 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015780 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015783 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 arg = skipwhite(arg);
15787 }
15788 eap->nextcmd = check_nextcmd(arg);
15789
15790 if (eap->skip)
15791 --emsg_skip;
15792 else
15793 {
15794 /* remove text that may still be there from the command */
15795 if (needclr)
15796 msg_clr_eos();
15797 if (eap->cmdidx == CMD_echo)
15798 msg_end();
15799 }
15800}
15801
15802/*
15803 * ":echohl {name}".
15804 */
15805 void
15806ex_echohl(eap)
15807 exarg_T *eap;
15808{
15809 int id;
15810
15811 id = syn_name2id(eap->arg);
15812 if (id == 0)
15813 echo_attr = 0;
15814 else
15815 echo_attr = syn_id2attr(id);
15816}
15817
15818/*
15819 * ":execute expr1 ..." execute the result of an expression.
15820 * ":echomsg expr1 ..." Print a message
15821 * ":echoerr expr1 ..." Print an error
15822 * Each gets spaces around each argument and a newline at the end for
15823 * echo commands
15824 */
15825 void
15826ex_execute(eap)
15827 exarg_T *eap;
15828{
15829 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 int ret = OK;
15832 char_u *p;
15833 garray_T ga;
15834 int len;
15835 int save_did_emsg;
15836
15837 ga_init2(&ga, 1, 80);
15838
15839 if (eap->skip)
15840 ++emsg_skip;
15841 while (*arg != NUL && *arg != '|' && *arg != '\n')
15842 {
15843 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015844 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 {
15846 /*
15847 * Report the invalid expression unless the expression evaluation
15848 * has been cancelled due to an aborting error, an interrupt, or an
15849 * exception.
15850 */
15851 if (!aborting())
15852 EMSG2(_(e_invexpr2), p);
15853 ret = FAIL;
15854 break;
15855 }
15856
15857 if (!eap->skip)
15858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015859 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 len = (int)STRLEN(p);
15861 if (ga_grow(&ga, len + 2) == FAIL)
15862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015863 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 ret = FAIL;
15865 break;
15866 }
15867 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 ga.ga_len += len;
15871 }
15872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015873 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 arg = skipwhite(arg);
15875 }
15876
15877 if (ret != FAIL && ga.ga_data != NULL)
15878 {
15879 if (eap->cmdidx == CMD_echomsg)
15880 MSG_ATTR(ga.ga_data, echo_attr);
15881 else if (eap->cmdidx == CMD_echoerr)
15882 {
15883 /* We don't want to abort following commands, restore did_emsg. */
15884 save_did_emsg = did_emsg;
15885 EMSG((char_u *)ga.ga_data);
15886 if (!force_abort)
15887 did_emsg = save_did_emsg;
15888 }
15889 else if (eap->cmdidx == CMD_execute)
15890 do_cmdline((char_u *)ga.ga_data,
15891 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15892 }
15893
15894 ga_clear(&ga);
15895
15896 if (eap->skip)
15897 --emsg_skip;
15898
15899 eap->nextcmd = check_nextcmd(arg);
15900}
15901
15902/*
15903 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15904 * "arg" points to the "&" or '+' when called, to "option" when returning.
15905 * Returns NULL when no option name found. Otherwise pointer to the char
15906 * after the option name.
15907 */
15908 static char_u *
15909find_option_end(arg, opt_flags)
15910 char_u **arg;
15911 int *opt_flags;
15912{
15913 char_u *p = *arg;
15914
15915 ++p;
15916 if (*p == 'g' && p[1] == ':')
15917 {
15918 *opt_flags = OPT_GLOBAL;
15919 p += 2;
15920 }
15921 else if (*p == 'l' && p[1] == ':')
15922 {
15923 *opt_flags = OPT_LOCAL;
15924 p += 2;
15925 }
15926 else
15927 *opt_flags = 0;
15928
15929 if (!ASCII_ISALPHA(*p))
15930 return NULL;
15931 *arg = p;
15932
15933 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15934 p += 4; /* termcap option */
15935 else
15936 while (ASCII_ISALPHA(*p))
15937 ++p;
15938 return p;
15939}
15940
15941/*
15942 * ":function"
15943 */
15944 void
15945ex_function(eap)
15946 exarg_T *eap;
15947{
15948 char_u *theline;
15949 int j;
15950 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 char_u *name = NULL;
15953 char_u *p;
15954 char_u *arg;
15955 garray_T newargs;
15956 garray_T newlines;
15957 int varargs = FALSE;
15958 int mustend = FALSE;
15959 int flags = 0;
15960 ufunc_T *fp;
15961 int indent;
15962 int nesting;
15963 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 dictitem_T *v;
15965 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015966 static int func_nr = 0; /* number for nameless function */
15967 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015968 hashtab_T *ht;
15969 int todo;
15970 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971
15972 /*
15973 * ":function" without argument: list functions.
15974 */
15975 if (ends_excmd(*eap->arg))
15976 {
15977 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015978 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015979 todo = func_hashtab.ht_used;
15980 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015981 {
15982 if (!HASHITEM_EMPTY(hi))
15983 {
15984 --todo;
15985 fp = HI2UF(hi);
15986 if (!isdigit(*fp->uf_name))
15987 list_func_head(fp, FALSE);
15988 }
15989 }
15990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 eap->nextcmd = check_nextcmd(eap->arg);
15992 return;
15993 }
15994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015995 /*
15996 * Get the function name. There are these situations:
15997 * func normal function name
15998 * "name" == func, "fudi.fd_dict" == NULL
15999 * dict.func new dictionary entry
16000 * "name" == NULL, "fudi.fd_dict" set,
16001 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16002 * dict.func existing dict entry with a Funcref
16003 * "name" == fname, "fudi.fd_dict" set,
16004 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16005 * dict.func existing dict entry that's not a Funcref
16006 * "name" == NULL, "fudi.fd_dict" set,
16007 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016010 name = trans_function_name(&p, eap->skip, 0, &fudi);
16011 paren = (vim_strchr(p, '(') != NULL);
16012 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 {
16014 /*
16015 * Return on an invalid expression in braces, unless the expression
16016 * evaluation has been cancelled due to an aborting error, an
16017 * interrupt, or an exception.
16018 */
16019 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016021 if (!eap->skip && fudi.fd_newkey != NULL)
16022 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016023 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 else
16027 eap->skip = TRUE;
16028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 /* An error in a function call during evaluation of an expression in magic
16030 * braces should not cause the function not to be defined. */
16031 saved_did_emsg = did_emsg;
16032 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033
16034 /*
16035 * ":function func" with only function name: list function.
16036 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016037 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 {
16039 if (!ends_excmd(*skipwhite(p)))
16040 {
16041 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016042 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 }
16044 eap->nextcmd = check_nextcmd(p);
16045 if (eap->nextcmd != NULL)
16046 *p = NUL;
16047 if (!eap->skip && !got_int)
16048 {
16049 fp = find_func(name);
16050 if (fp != NULL)
16051 {
16052 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016053 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 {
16055 msg_putchar('\n');
16056 msg_outnum((long)(j + 1));
16057 if (j < 9)
16058 msg_putchar(' ');
16059 if (j < 99)
16060 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016061 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 out_flush(); /* show a line at a time */
16063 ui_breakcheck();
16064 }
16065 if (!got_int)
16066 {
16067 msg_putchar('\n');
16068 msg_puts((char_u *)" endfunction");
16069 }
16070 }
16071 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016072 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016074 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 }
16076
16077 /*
16078 * ":function name(arg1, arg2)" Define function.
16079 */
16080 p = skipwhite(p);
16081 if (*p != '(')
16082 {
16083 if (!eap->skip)
16084 {
16085 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016086 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 }
16088 /* attempt to continue by skipping some text */
16089 if (vim_strchr(p, '(') != NULL)
16090 p = vim_strchr(p, '(');
16091 }
16092 p = skipwhite(p + 1);
16093
16094 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16095 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16096
16097 /*
16098 * Isolate the arguments: "arg1, arg2, ...)"
16099 */
16100 while (*p != ')')
16101 {
16102 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16103 {
16104 varargs = TRUE;
16105 p += 3;
16106 mustend = TRUE;
16107 }
16108 else
16109 {
16110 arg = p;
16111 while (ASCII_ISALNUM(*p) || *p == '_')
16112 ++p;
16113 if (arg == p || isdigit(*arg)
16114 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16115 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16116 {
16117 if (!eap->skip)
16118 EMSG2(_("E125: Illegal argument: %s"), arg);
16119 break;
16120 }
16121 if (ga_grow(&newargs, 1) == FAIL)
16122 goto erret;
16123 c = *p;
16124 *p = NUL;
16125 arg = vim_strsave(arg);
16126 if (arg == NULL)
16127 goto erret;
16128 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16129 *p = c;
16130 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 if (*p == ',')
16132 ++p;
16133 else
16134 mustend = TRUE;
16135 }
16136 p = skipwhite(p);
16137 if (mustend && *p != ')')
16138 {
16139 if (!eap->skip)
16140 EMSG2(_(e_invarg2), eap->arg);
16141 break;
16142 }
16143 }
16144 ++p; /* skip the ')' */
16145
Bram Moolenaare9a41262005-01-15 22:18:47 +000016146 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 for (;;)
16148 {
16149 p = skipwhite(p);
16150 if (STRNCMP(p, "range", 5) == 0)
16151 {
16152 flags |= FC_RANGE;
16153 p += 5;
16154 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016155 else if (STRNCMP(p, "dict", 4) == 0)
16156 {
16157 flags |= FC_DICT;
16158 p += 4;
16159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 else if (STRNCMP(p, "abort", 5) == 0)
16161 {
16162 flags |= FC_ABORT;
16163 p += 5;
16164 }
16165 else
16166 break;
16167 }
16168
16169 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16170 EMSG(_(e_trailing));
16171
16172 /*
16173 * Read the body of the function, until ":endfunction" is found.
16174 */
16175 if (KeyTyped)
16176 {
16177 /* Check if the function already exists, don't let the user type the
16178 * whole function before telling him it doesn't work! For a script we
16179 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016180 if (!eap->skip && !eap->forceit)
16181 {
16182 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16183 EMSG(_(e_funcdict));
16184 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016185 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
16188 msg_putchar('\n'); /* don't overwrite the function name */
16189 cmdline_row = msg_row;
16190 }
16191
16192 indent = 2;
16193 nesting = 0;
16194 for (;;)
16195 {
16196 msg_scroll = TRUE;
16197 need_wait_return = FALSE;
16198 if (eap->getline == NULL)
16199 theline = getcmdline(':', 0L, indent);
16200 else
16201 theline = eap->getline(':', eap->cookie, indent);
16202 if (KeyTyped)
16203 lines_left = Rows - 1;
16204 if (theline == NULL)
16205 {
16206 EMSG(_("E126: Missing :endfunction"));
16207 goto erret;
16208 }
16209
16210 if (skip_until != NULL)
16211 {
16212 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16213 * don't check for ":endfunc". */
16214 if (STRCMP(theline, skip_until) == 0)
16215 {
16216 vim_free(skip_until);
16217 skip_until = NULL;
16218 }
16219 }
16220 else
16221 {
16222 /* skip ':' and blanks*/
16223 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16224 ;
16225
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016226 /* Check for "endfunction". */
16227 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 {
16229 vim_free(theline);
16230 break;
16231 }
16232
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016233 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 * at "end". */
16235 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16236 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016237 else if (STRNCMP(p, "if", 2) == 0
16238 || STRNCMP(p, "wh", 2) == 0
16239 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 || STRNCMP(p, "try", 3) == 0)
16241 indent += 2;
16242
16243 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016244 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016246 if (*p == '!')
16247 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 p += eval_fname_script(p);
16249 if (ASCII_ISALPHA(*p))
16250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016251 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 if (*skipwhite(p) == '(')
16253 {
16254 ++nesting;
16255 indent += 2;
16256 }
16257 }
16258 }
16259
16260 /* Check for ":append" or ":insert". */
16261 p = skip_range(p, NULL);
16262 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16263 || (p[0] == 'i'
16264 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16265 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16266 skip_until = vim_strsave((char_u *)".");
16267
16268 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16269 arg = skipwhite(skiptowhite(p));
16270 if (arg[0] == '<' && arg[1] =='<'
16271 && ((p[0] == 'p' && p[1] == 'y'
16272 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16273 || (p[0] == 'p' && p[1] == 'e'
16274 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16275 || (p[0] == 't' && p[1] == 'c'
16276 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16277 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16278 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016279 || (p[0] == 'm' && p[1] == 'z'
16280 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 ))
16282 {
16283 /* ":python <<" continues until a dot, like ":append" */
16284 p = skipwhite(arg + 2);
16285 if (*p == NUL)
16286 skip_until = vim_strsave((char_u *)".");
16287 else
16288 skip_until = vim_strsave(p);
16289 }
16290 }
16291
16292 /* Add the line to the function. */
16293 if (ga_grow(&newlines, 1) == FAIL)
16294 goto erret;
16295 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16296 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 }
16298
16299 /* Don't define the function when skipping commands or when an error was
16300 * detected. */
16301 if (eap->skip || did_emsg)
16302 goto erret;
16303
16304 /*
16305 * If there are no errors, add the function
16306 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016307 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016308 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016309 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016311 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016312 emsg_funcname("E707: Function name conflicts with variable: %s",
16313 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016314 goto erret;
16315 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016316
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016317 fp = find_func(name);
16318 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016320 if (!eap->forceit)
16321 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016322 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016323 goto erret;
16324 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016325 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016326 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016327 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016328 name);
16329 goto erret;
16330 }
16331 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016332 ga_clear_strings(&(fp->uf_args));
16333 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016334 vim_free(name);
16335 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 }
16338 else
16339 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016340 char numbuf[20];
16341
16342 fp = NULL;
16343 if (fudi.fd_newkey == NULL && !eap->forceit)
16344 {
16345 EMSG(_(e_funcdict));
16346 goto erret;
16347 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016348 if (fudi.fd_di == NULL)
16349 {
16350 /* Can't add a function to a locked dictionary */
16351 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16352 goto erret;
16353 }
16354 /* Can't change an existing function if it is locked */
16355 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16356 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016357
16358 /* Give the function a sequential number. Can only be used with a
16359 * Funcref! */
16360 vim_free(name);
16361 sprintf(numbuf, "%d", ++func_nr);
16362 name = vim_strsave((char_u *)numbuf);
16363 if (name == NULL)
16364 goto erret;
16365 }
16366
16367 if (fp == NULL)
16368 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016369 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016370 {
16371 int slen, plen;
16372 char_u *scriptname;
16373
16374 /* Check that the autoload name matches the script name. */
16375 j = FAIL;
16376 if (sourcing_name != NULL)
16377 {
16378 scriptname = autoload_name(name);
16379 if (scriptname != NULL)
16380 {
16381 p = vim_strchr(scriptname, '/');
16382 plen = STRLEN(p);
16383 slen = STRLEN(sourcing_name);
16384 if (slen > plen && fnamecmp(p,
16385 sourcing_name + slen - plen) == 0)
16386 j = OK;
16387 vim_free(scriptname);
16388 }
16389 }
16390 if (j == FAIL)
16391 {
16392 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16393 goto erret;
16394 }
16395 }
16396
16397 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 if (fp == NULL)
16399 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016400
16401 if (fudi.fd_dict != NULL)
16402 {
16403 if (fudi.fd_di == NULL)
16404 {
16405 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016406 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016407 if (fudi.fd_di == NULL)
16408 {
16409 vim_free(fp);
16410 goto erret;
16411 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016412 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16413 {
16414 vim_free(fudi.fd_di);
16415 goto erret;
16416 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016417 }
16418 else
16419 /* overwrite existing dict entry */
16420 clear_tv(&fudi.fd_di->di_tv);
16421 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016422 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016423 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016424 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016425 }
16426
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016428 STRCPY(fp->uf_name, name);
16429 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016431 fp->uf_args = newargs;
16432 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016433#ifdef FEAT_PROFILE
16434 fp->uf_tml_count = NULL;
16435 fp->uf_tml_total = NULL;
16436 fp->uf_tml_self = NULL;
16437 fp->uf_profiling = FALSE;
16438 if (prof_def_func())
16439 func_do_profile(fp);
16440#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016441 fp->uf_varargs = varargs;
16442 fp->uf_flags = flags;
16443 fp->uf_calls = 0;
16444 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016445 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446
16447erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 ga_clear_strings(&newargs);
16449 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016450ret_free:
16451 vim_free(skip_until);
16452 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457/*
16458 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016459 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016461 * flags:
16462 * TFN_INT: internal function name OK
16463 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 * Advances "pp" to just after the function name (if no error).
16465 */
16466 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016467trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 char_u **pp;
16469 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016470 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016471 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016473 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 char_u *start;
16475 char_u *end;
16476 int lead;
16477 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016479 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016480
16481 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016484
16485 /* Check for hard coded <SNR>: already translated function ID (from a user
16486 * command). */
16487 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16488 && (*pp)[2] == (int)KE_SNR)
16489 {
16490 *pp += 3;
16491 len = get_id_len(pp) + 3;
16492 return vim_strnsave(start, len);
16493 }
16494
16495 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16496 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016498 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016500
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016501 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16502 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016503 if (end == start)
16504 {
16505 if (!skip)
16506 EMSG(_("E129: Function name required"));
16507 goto theend;
16508 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016509 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016510 {
16511 /*
16512 * Report an invalid expression in braces, unless the expression
16513 * evaluation has been cancelled due to an aborting error, an
16514 * interrupt, or an exception.
16515 */
16516 if (!aborting())
16517 {
16518 if (end != NULL)
16519 EMSG2(_(e_invarg2), start);
16520 }
16521 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016522 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016523 goto theend;
16524 }
16525
16526 if (lv.ll_tv != NULL)
16527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016528 if (fdp != NULL)
16529 {
16530 fdp->fd_dict = lv.ll_dict;
16531 fdp->fd_newkey = lv.ll_newkey;
16532 lv.ll_newkey = NULL;
16533 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016534 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016535 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16536 {
16537 name = vim_strsave(lv.ll_tv->vval.v_string);
16538 *pp = end;
16539 }
16540 else
16541 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016542 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16543 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016544 EMSG(_(e_funcref));
16545 else
16546 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016547 name = NULL;
16548 }
16549 goto theend;
16550 }
16551
16552 if (lv.ll_name == NULL)
16553 {
16554 /* Error found, but continue after the function name. */
16555 *pp = end;
16556 goto theend;
16557 }
16558
16559 if (lv.ll_exp_name != NULL)
16560 len = STRLEN(lv.ll_exp_name);
16561 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016562 {
16563 if (lead == 2) /* skip over "s:" */
16564 lv.ll_name += 2;
16565 len = (int)(end - lv.ll_name);
16566 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016567
16568 /*
16569 * Copy the function name to allocated memory.
16570 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16571 * Accept <SNR>123_name() outside a script.
16572 */
16573 if (skip)
16574 lead = 0; /* do nothing */
16575 else if (lead > 0)
16576 {
16577 lead = 3;
16578 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16579 {
16580 if (current_SID <= 0)
16581 {
16582 EMSG(_(e_usingsid));
16583 goto theend;
16584 }
16585 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16586 lead += (int)STRLEN(sid_buf);
16587 }
16588 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016589 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016590 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016591 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016592 goto theend;
16593 }
16594 name = alloc((unsigned)(len + lead + 1));
16595 if (name != NULL)
16596 {
16597 if (lead > 0)
16598 {
16599 name[0] = K_SPECIAL;
16600 name[1] = KS_EXTRA;
16601 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016602 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016603 STRCPY(name + 3, sid_buf);
16604 }
16605 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16606 name[len + lead] = NUL;
16607 }
16608 *pp = end;
16609
16610theend:
16611 clear_lval(&lv);
16612 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613}
16614
16615/*
16616 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16617 * Return 2 if "p" starts with "s:".
16618 * Return 0 otherwise.
16619 */
16620 static int
16621eval_fname_script(p)
16622 char_u *p;
16623{
16624 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16625 || STRNICMP(p + 1, "SNR>", 4) == 0))
16626 return 5;
16627 if (p[0] == 's' && p[1] == ':')
16628 return 2;
16629 return 0;
16630}
16631
16632/*
16633 * Return TRUE if "p" starts with "<SID>" or "s:".
16634 * Only works if eval_fname_script() returned non-zero for "p"!
16635 */
16636 static int
16637eval_fname_sid(p)
16638 char_u *p;
16639{
16640 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16641}
16642
16643/*
16644 * List the head of the function: "name(arg1, arg2)".
16645 */
16646 static void
16647list_func_head(fp, indent)
16648 ufunc_T *fp;
16649 int indent;
16650{
16651 int j;
16652
16653 msg_start();
16654 if (indent)
16655 MSG_PUTS(" ");
16656 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016657 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 {
16659 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016660 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 }
16662 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016663 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016665 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 {
16667 if (j)
16668 MSG_PUTS(", ");
16669 msg_puts(FUNCARG(fp, j));
16670 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016671 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 {
16673 if (j)
16674 MSG_PUTS(", ");
16675 MSG_PUTS("...");
16676 }
16677 msg_putchar(')');
16678}
16679
16680/*
16681 * Find a function by name, return pointer to it in ufuncs.
16682 * Return NULL for unknown function.
16683 */
16684 static ufunc_T *
16685find_func(name)
16686 char_u *name;
16687{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016688 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016690 hi = hash_find(&func_hashtab, name);
16691 if (!HASHITEM_EMPTY(hi))
16692 return HI2UF(hi);
16693 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694}
16695
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016696/*
16697 * Return TRUE if a function "name" exists.
16698 */
16699 static int
16700function_exists(name)
16701 char_u *name;
16702{
16703 char_u *p = name;
16704 int n = FALSE;
16705
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016706 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016707 if (p != NULL)
16708 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016709 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016710 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016711 else
16712 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016713 vim_free(p);
16714 }
16715 return n;
16716}
16717
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016718/*
16719 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016720 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016721 */
16722 static int
16723builtin_function(name)
16724 char_u *name;
16725{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016726 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
16727 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016728}
16729
Bram Moolenaar05159a02005-02-26 23:04:13 +000016730#if defined(FEAT_PROFILE) || defined(PROTO)
16731/*
16732 * Start profiling function "fp".
16733 */
16734 static void
16735func_do_profile(fp)
16736 ufunc_T *fp;
16737{
16738 fp->uf_tm_count = 0;
16739 profile_zero(&fp->uf_tm_self);
16740 profile_zero(&fp->uf_tm_total);
16741 if (fp->uf_tml_count == NULL)
16742 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16743 (sizeof(int) * fp->uf_lines.ga_len));
16744 if (fp->uf_tml_total == NULL)
16745 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16746 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16747 if (fp->uf_tml_self == NULL)
16748 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16749 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16750 fp->uf_tml_idx = -1;
16751 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16752 || fp->uf_tml_self == NULL)
16753 return; /* out of memory */
16754
16755 fp->uf_profiling = TRUE;
16756}
16757
16758/*
16759 * Dump the profiling results for all functions in file "fd".
16760 */
16761 void
16762func_dump_profile(fd)
16763 FILE *fd;
16764{
16765 hashitem_T *hi;
16766 int todo;
16767 ufunc_T *fp;
16768 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016769 ufunc_T **sorttab;
16770 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016771
16772 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016773 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16774
Bram Moolenaar05159a02005-02-26 23:04:13 +000016775 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16776 {
16777 if (!HASHITEM_EMPTY(hi))
16778 {
16779 --todo;
16780 fp = HI2UF(hi);
16781 if (fp->uf_profiling)
16782 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016783 if (sorttab != NULL)
16784 sorttab[st_len++] = fp;
16785
Bram Moolenaar05159a02005-02-26 23:04:13 +000016786 if (fp->uf_name[0] == K_SPECIAL)
16787 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16788 else
16789 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16790 if (fp->uf_tm_count == 1)
16791 fprintf(fd, "Called 1 time\n");
16792 else
16793 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16794 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16795 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16796 fprintf(fd, "\n");
16797 fprintf(fd, "count total (s) self (s)\n");
16798
16799 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16800 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016801 prof_func_line(fd, fp->uf_tml_count[i],
16802 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016803 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16804 }
16805 fprintf(fd, "\n");
16806 }
16807 }
16808 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016809
16810 if (sorttab != NULL && st_len > 0)
16811 {
16812 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16813 prof_total_cmp);
16814 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16815 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16816 prof_self_cmp);
16817 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16818 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016819}
Bram Moolenaar73830342005-02-28 22:48:19 +000016820
16821 static void
16822prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16823 FILE *fd;
16824 ufunc_T **sorttab;
16825 int st_len;
16826 char *title;
16827 int prefer_self; /* when equal print only self time */
16828{
16829 int i;
16830 ufunc_T *fp;
16831
16832 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16833 fprintf(fd, "count total (s) self (s) function\n");
16834 for (i = 0; i < 20 && i < st_len; ++i)
16835 {
16836 fp = sorttab[i];
16837 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16838 prefer_self);
16839 if (fp->uf_name[0] == K_SPECIAL)
16840 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16841 else
16842 fprintf(fd, " %s()\n", fp->uf_name);
16843 }
16844 fprintf(fd, "\n");
16845}
16846
16847/*
16848 * Print the count and times for one function or function line.
16849 */
16850 static void
16851prof_func_line(fd, count, total, self, prefer_self)
16852 FILE *fd;
16853 int count;
16854 proftime_T *total;
16855 proftime_T *self;
16856 int prefer_self; /* when equal print only self time */
16857{
16858 if (count > 0)
16859 {
16860 fprintf(fd, "%5d ", count);
16861 if (prefer_self && profile_equal(total, self))
16862 fprintf(fd, " ");
16863 else
16864 fprintf(fd, "%s ", profile_msg(total));
16865 if (!prefer_self && profile_equal(total, self))
16866 fprintf(fd, " ");
16867 else
16868 fprintf(fd, "%s ", profile_msg(self));
16869 }
16870 else
16871 fprintf(fd, " ");
16872}
16873
16874/*
16875 * Compare function for total time sorting.
16876 */
16877 static int
16878#ifdef __BORLANDC__
16879_RTLENTRYF
16880#endif
16881prof_total_cmp(s1, s2)
16882 const void *s1;
16883 const void *s2;
16884{
16885 ufunc_T *p1, *p2;
16886
16887 p1 = *(ufunc_T **)s1;
16888 p2 = *(ufunc_T **)s2;
16889 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16890}
16891
16892/*
16893 * Compare function for self time sorting.
16894 */
16895 static int
16896#ifdef __BORLANDC__
16897_RTLENTRYF
16898#endif
16899prof_self_cmp(s1, s2)
16900 const void *s1;
16901 const void *s2;
16902{
16903 ufunc_T *p1, *p2;
16904
16905 p1 = *(ufunc_T **)s1;
16906 p2 = *(ufunc_T **)s2;
16907 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16908}
16909
Bram Moolenaar05159a02005-02-26 23:04:13 +000016910#endif
16911
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016912/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016913 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016914 * Return TRUE if a package was loaded.
16915 */
16916 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016917script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016918 char_u *name;
16919{
16920 char_u *p;
16921 char_u *scriptname;
16922 int ret = FALSE;
16923
16924 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016925 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016926 if (p == NULL || p <= name + 2)
16927 return FALSE;
16928
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016929 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16930 scriptname = autoload_name(name);
16931 if (cmd_runtime(scriptname, FALSE) == OK)
16932 ret = TRUE;
16933
16934 vim_free(scriptname);
16935 return ret;
16936}
16937
16938/*
16939 * Return the autoload script name for a function or variable name.
16940 * Returns NULL when out of memory.
16941 */
16942 static char_u *
16943autoload_name(name)
16944 char_u *name;
16945{
16946 char_u *p;
16947 char_u *scriptname;
16948
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016949 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016950 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16951 if (scriptname == NULL)
16952 return FALSE;
16953 STRCPY(scriptname, "autoload/");
16954 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016955 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016956 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016957 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016958 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016959 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016960}
16961
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16963
16964/*
16965 * Function given to ExpandGeneric() to obtain the list of user defined
16966 * function names.
16967 */
16968 char_u *
16969get_user_func_name(xp, idx)
16970 expand_T *xp;
16971 int idx;
16972{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016973 static long_u done;
16974 static hashitem_T *hi;
16975 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
16977 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016979 done = 0;
16980 hi = func_hashtab.ht_array;
16981 }
16982 if (done < func_hashtab.ht_used)
16983 {
16984 if (done++ > 0)
16985 ++hi;
16986 while (HASHITEM_EMPTY(hi))
16987 ++hi;
16988 fp = HI2UF(hi);
16989
16990 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16991 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992
16993 cat_func_name(IObuff, fp);
16994 if (xp->xp_context != EXPAND_USER_FUNC)
16995 {
16996 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016997 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 STRCAT(IObuff, ")");
16999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 return IObuff;
17001 }
17002 return NULL;
17003}
17004
17005#endif /* FEAT_CMDL_COMPL */
17006
17007/*
17008 * Copy the function name of "fp" to buffer "buf".
17009 * "buf" must be able to hold the function name plus three bytes.
17010 * Takes care of script-local function names.
17011 */
17012 static void
17013cat_func_name(buf, fp)
17014 char_u *buf;
17015 ufunc_T *fp;
17016{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017017 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 {
17019 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017020 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 }
17022 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017023 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024}
17025
17026/*
17027 * ":delfunction {name}"
17028 */
17029 void
17030ex_delfunction(eap)
17031 exarg_T *eap;
17032{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017033 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 char_u *p;
17035 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017036 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037
17038 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017039 name = trans_function_name(&p, eap->skip, 0, &fudi);
17040 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017042 {
17043 if (fudi.fd_dict != NULL && !eap->skip)
17044 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 if (!ends_excmd(*skipwhite(p)))
17048 {
17049 vim_free(name);
17050 EMSG(_(e_trailing));
17051 return;
17052 }
17053 eap->nextcmd = check_nextcmd(p);
17054 if (eap->nextcmd != NULL)
17055 *p = NUL;
17056
17057 if (!eap->skip)
17058 fp = find_func(name);
17059 vim_free(name);
17060
17061 if (!eap->skip)
17062 {
17063 if (fp == NULL)
17064 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017065 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 return;
17067 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017068 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069 {
17070 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17071 return;
17072 }
17073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017074 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017076 /* Delete the dict item that refers to the function, it will
17077 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017078 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017080 else
17081 func_free(fp);
17082 }
17083}
17084
17085/*
17086 * Free a function and remove it from the list of functions.
17087 */
17088 static void
17089func_free(fp)
17090 ufunc_T *fp;
17091{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017092 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017093
17094 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017095 ga_clear_strings(&(fp->uf_args));
17096 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017097#ifdef FEAT_PROFILE
17098 vim_free(fp->uf_tml_count);
17099 vim_free(fp->uf_tml_total);
17100 vim_free(fp->uf_tml_self);
17101#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017102
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017103 /* remove the function from the function hashtable */
17104 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17105 if (HASHITEM_EMPTY(hi))
17106 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017107 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017108 hash_remove(&func_hashtab, hi);
17109
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017110 vim_free(fp);
17111}
17112
17113/*
17114 * Unreference a Function: decrement the reference count and free it when it
17115 * becomes zero. Only for numbered functions.
17116 */
17117 static void
17118func_unref(name)
17119 char_u *name;
17120{
17121 ufunc_T *fp;
17122
17123 if (name != NULL && isdigit(*name))
17124 {
17125 fp = find_func(name);
17126 if (fp == NULL)
17127 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017128 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017129 {
17130 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017131 * when "uf_calls" becomes zero. */
17132 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017133 func_free(fp);
17134 }
17135 }
17136}
17137
17138/*
17139 * Count a reference to a Function.
17140 */
17141 static void
17142func_ref(name)
17143 char_u *name;
17144{
17145 ufunc_T *fp;
17146
17147 if (name != NULL && isdigit(*name))
17148 {
17149 fp = find_func(name);
17150 if (fp == NULL)
17151 EMSG2(_(e_intern2), "func_ref()");
17152 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017153 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 }
17155}
17156
17157/*
17158 * Call a user function.
17159 */
17160 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017161call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 ufunc_T *fp; /* pointer to function */
17163 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 typval_T *argvars; /* arguments */
17165 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 linenr_T firstline; /* first line of range */
17167 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169{
Bram Moolenaar33570922005-01-25 22:26:29 +000017170 char_u *save_sourcing_name;
17171 linenr_T save_sourcing_lnum;
17172 scid_T save_current_SID;
17173 funccall_T fc;
17174 funccall_T *save_fcp = current_funccal;
17175 int save_did_emsg;
17176 static int depth = 0;
17177 dictitem_T *v;
17178 int fixvar_idx = 0; /* index in fixvar[] */
17179 int i;
17180 int ai;
17181 char_u numbuf[NUMBUFLEN];
17182 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017183#ifdef FEAT_PROFILE
17184 proftime_T wait_start;
17185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186
17187 /* If depth of calling is getting too high, don't execute the function */
17188 if (depth >= p_mfd)
17189 {
17190 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191 rettv->v_type = VAR_NUMBER;
17192 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 return;
17194 }
17195 ++depth;
17196
17197 line_breakcheck(); /* check for CTRL-C hit */
17198
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 fc.rettv = rettv;
17202 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 fc.linenr = 0;
17204 fc.returned = FALSE;
17205 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017207 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 fc.dbg_tick = debug_tick;
17209
Bram Moolenaar33570922005-01-25 22:26:29 +000017210 /*
17211 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17212 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17213 * each argument variable and saves a lot of time.
17214 */
17215 /*
17216 * Init l: variables.
17217 */
17218 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017219 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017220 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 /* Set l:self to "selfdict". */
17222 v = &fc.fixvar[fixvar_idx++].var;
17223 STRCPY(v->di_key, "self");
17224 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17225 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17226 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017227 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 v->di_tv.vval.v_dict = selfdict;
17229 ++selfdict->dv_refcount;
17230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017231
Bram Moolenaar33570922005-01-25 22:26:29 +000017232 /*
17233 * Init a: variables.
17234 * Set a:0 to "argcount".
17235 * Set a:000 to a list with room for the "..." arguments.
17236 */
17237 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17238 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017239 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 v = &fc.fixvar[fixvar_idx++].var;
17241 STRCPY(v->di_key, "000");
17242 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17243 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17244 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017245 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 v->di_tv.vval.v_list = &fc.l_varlist;
17247 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17248 fc.l_varlist.lv_refcount = 99999;
17249
17250 /*
17251 * Set a:firstline to "firstline" and a:lastline to "lastline".
17252 * Set a:name to named arguments.
17253 * Set a:N to the "..." arguments.
17254 */
17255 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17256 (varnumber_T)firstline);
17257 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17258 (varnumber_T)lastline);
17259 for (i = 0; i < argcount; ++i)
17260 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017261 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017262 if (ai < 0)
17263 /* named argument a:name */
17264 name = FUNCARG(fp, i);
17265 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017266 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017267 /* "..." argument a:1, a:2, etc. */
17268 sprintf((char *)numbuf, "%d", ai + 1);
17269 name = numbuf;
17270 }
17271 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17272 {
17273 v = &fc.fixvar[fixvar_idx++].var;
17274 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17275 }
17276 else
17277 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017278 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17279 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017280 if (v == NULL)
17281 break;
17282 v->di_flags = DI_FLAGS_RO;
17283 }
17284 STRCPY(v->di_key, name);
17285 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17286
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017287 /* Note: the values are copied directly to avoid alloc/free.
17288 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017290 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017291
17292 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17293 {
17294 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17295 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017296 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017297 }
17298 }
17299
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 /* Don't redraw while executing the function. */
17301 ++RedrawingDisabled;
17302 save_sourcing_name = sourcing_name;
17303 save_sourcing_lnum = sourcing_lnum;
17304 sourcing_lnum = 1;
17305 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017306 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 if (sourcing_name != NULL)
17308 {
17309 if (save_sourcing_name != NULL
17310 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17311 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17312 else
17313 STRCPY(sourcing_name, "function ");
17314 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17315
17316 if (p_verbose >= 12)
17317 {
17318 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017319 verbose_enter_scroll();
17320
Bram Moolenaar555b2802005-05-19 21:08:39 +000017321 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 if (p_verbose >= 14)
17323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017325 char_u numbuf[NUMBUFLEN];
17326 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327
17328 msg_puts((char_u *)"(");
17329 for (i = 0; i < argcount; ++i)
17330 {
17331 if (i > 0)
17332 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017333 if (argvars[i].v_type == VAR_NUMBER)
17334 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 else
17336 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017337 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017338 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017340 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 }
17342 }
17343 msg_puts((char_u *)")");
17344 }
17345 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017346
17347 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 --no_wait_return;
17349 }
17350 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017351#ifdef FEAT_PROFILE
17352 if (do_profiling)
17353 {
17354 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17355 func_do_profile(fp);
17356 if (fp->uf_profiling
17357 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17358 {
17359 ++fp->uf_tm_count;
17360 profile_start(&fp->uf_tm_start);
17361 profile_zero(&fp->uf_tm_children);
17362 }
17363 script_prof_save(&wait_start);
17364 }
17365#endif
17366
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017368 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 save_did_emsg = did_emsg;
17370 did_emsg = FALSE;
17371
17372 /* call do_cmdline() to execute the lines */
17373 do_cmdline(NULL, get_func_line, (void *)&fc,
17374 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17375
17376 --RedrawingDisabled;
17377
17378 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017379 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381 clear_tv(rettv);
17382 rettv->v_type = VAR_NUMBER;
17383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 }
17385
Bram Moolenaar05159a02005-02-26 23:04:13 +000017386#ifdef FEAT_PROFILE
17387 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17388 {
17389 profile_end(&fp->uf_tm_start);
17390 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17391 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17392 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17393 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
17394 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
17395 {
17396 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
17397 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
17398 }
17399 }
17400#endif
17401
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 /* when being verbose, mention the return value */
17403 if (p_verbose >= 12)
17404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017406 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017409 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017411 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17412 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017415 char_u buf[MSG_BUF_LEN];
17416 char_u numbuf[NUMBUFLEN];
17417 char_u *tofree;
17418
Bram Moolenaar555b2802005-05-19 21:08:39 +000017419 /* The value may be very long. Skip the middle part, so that we
17420 * have some idea how it starts and ends. smsg() would always
17421 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017422 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017423 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017424 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017425 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 }
17427 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017428
17429 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 --no_wait_return;
17431 }
17432
17433 vim_free(sourcing_name);
17434 sourcing_name = save_sourcing_name;
17435 sourcing_lnum = save_sourcing_lnum;
17436 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017437#ifdef FEAT_PROFILE
17438 if (do_profiling)
17439 script_prof_restore(&wait_start);
17440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441
17442 if (p_verbose >= 12 && sourcing_name != NULL)
17443 {
17444 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017445 verbose_enter_scroll();
17446
Bram Moolenaar555b2802005-05-19 21:08:39 +000017447 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017449
17450 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 --no_wait_return;
17452 }
17453
17454 did_emsg |= save_did_emsg;
17455 current_funccal = save_fcp;
17456
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 /* The a: variables typevals were not alloced, only free the allocated
17458 * variables. */
17459 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17460
17461 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 --depth;
17463}
17464
17465/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017466 * Add a number variable "name" to dict "dp" with value "nr".
17467 */
17468 static void
17469add_nr_var(dp, v, name, nr)
17470 dict_T *dp;
17471 dictitem_T *v;
17472 char *name;
17473 varnumber_T nr;
17474{
17475 STRCPY(v->di_key, name);
17476 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17477 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17478 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017479 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 v->di_tv.vval.v_number = nr;
17481}
17482
17483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 * ":return [expr]"
17485 */
17486 void
17487ex_return(eap)
17488 exarg_T *eap;
17489{
17490 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 int returning = FALSE;
17493
17494 if (current_funccal == NULL)
17495 {
17496 EMSG(_("E133: :return not inside a function"));
17497 return;
17498 }
17499
17500 if (eap->skip)
17501 ++emsg_skip;
17502
17503 eap->nextcmd = NULL;
17504 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 {
17507 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017510 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 }
17512 /* It's safer to return also on error. */
17513 else if (!eap->skip)
17514 {
17515 /*
17516 * Return unless the expression evaluation has been cancelled due to an
17517 * aborting error, an interrupt, or an exception.
17518 */
17519 if (!aborting())
17520 returning = do_return(eap, FALSE, TRUE, NULL);
17521 }
17522
17523 /* When skipping or the return gets pending, advance to the next command
17524 * in this line (!returning). Otherwise, ignore the rest of the line.
17525 * Following lines will be ignored by get_func_line(). */
17526 if (returning)
17527 eap->nextcmd = NULL;
17528 else if (eap->nextcmd == NULL) /* no argument */
17529 eap->nextcmd = check_nextcmd(arg);
17530
17531 if (eap->skip)
17532 --emsg_skip;
17533}
17534
17535/*
17536 * Return from a function. Possibly makes the return pending. Also called
17537 * for a pending return at the ":endtry" or after returning from an extra
17538 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017539 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017540 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 * FALSE when the return gets pending.
17542 */
17543 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017544do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 exarg_T *eap;
17546 int reanimate;
17547 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017548 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549{
17550 int idx;
17551 struct condstack *cstack = eap->cstack;
17552
17553 if (reanimate)
17554 /* Undo the return. */
17555 current_funccal->returned = FALSE;
17556
17557 /*
17558 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17559 * not in its finally clause (which then is to be executed next) is found.
17560 * In this case, make the ":return" pending for execution at the ":endtry".
17561 * Otherwise, return normally.
17562 */
17563 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17564 if (idx >= 0)
17565 {
17566 cstack->cs_pending[idx] = CSTP_RETURN;
17567
17568 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017569 /* A pending return again gets pending. "rettv" points to an
17570 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 else
17574 {
17575 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017576 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017578 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 {
17582 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017583 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 else
17586 EMSG(_(e_outofmem));
17587 }
17588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590
17591 if (reanimate)
17592 {
17593 /* The pending return value could be overwritten by a ":return"
17594 * without argument in a finally clause; reset the default
17595 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596 current_funccal->rettv->v_type = VAR_NUMBER;
17597 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 }
17599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017600 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 }
17602 else
17603 {
17604 current_funccal->returned = TRUE;
17605
17606 /* If the return is carried out now, store the return value. For
17607 * a return immediately after reanimation, the value is already
17608 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 }
17616 }
17617
17618 return idx < 0;
17619}
17620
17621/*
17622 * Free the variable with a pending return value.
17623 */
17624 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625discard_pending_return(rettv)
17626 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627{
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629}
17630
17631/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017632 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 * is an allocated string. Used by report_pending() for verbose messages.
17634 */
17635 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636get_return_cmd(rettv)
17637 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017639 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017640 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017641 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017643 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017644 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017645 if (s == NULL)
17646 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017647
17648 STRCPY(IObuff, ":return ");
17649 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17650 if (STRLEN(s) + 8 >= IOSIZE)
17651 STRCPY(IObuff + IOSIZE - 4, "...");
17652 vim_free(tofree);
17653 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654}
17655
17656/*
17657 * Get next function line.
17658 * Called by do_cmdline() to get the next line.
17659 * Returns allocated string, or NULL for end of function.
17660 */
17661/* ARGSUSED */
17662 char_u *
17663get_func_line(c, cookie, indent)
17664 int c; /* not used */
17665 void *cookie;
17666 int indent; /* not used */
17667{
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017669 ufunc_T *fp = fcp->func;
17670 char_u *retval;
17671 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672
17673 /* If breakpoints have been added/deleted need to check for it. */
17674 if (fcp->dbg_tick != debug_tick)
17675 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017676 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 sourcing_lnum);
17678 fcp->dbg_tick = debug_tick;
17679 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017680#ifdef FEAT_PROFILE
17681 if (do_profiling)
17682 func_line_end(cookie);
17683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684
Bram Moolenaar05159a02005-02-26 23:04:13 +000017685 gap = &fp->uf_lines;
17686 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 retval = NULL;
17688 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17689 retval = NULL;
17690 else
17691 {
17692 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17693 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017694#ifdef FEAT_PROFILE
17695 if (do_profiling)
17696 func_line_start(cookie);
17697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 }
17699
17700 /* Did we encounter a breakpoint? */
17701 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17702 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017703 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017705 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 sourcing_lnum);
17707 fcp->dbg_tick = debug_tick;
17708 }
17709
17710 return retval;
17711}
17712
Bram Moolenaar05159a02005-02-26 23:04:13 +000017713#if defined(FEAT_PROFILE) || defined(PROTO)
17714/*
17715 * Called when starting to read a function line.
17716 * "sourcing_lnum" must be correct!
17717 * When skipping lines it may not actually be executed, but we won't find out
17718 * until later and we need to store the time now.
17719 */
17720 void
17721func_line_start(cookie)
17722 void *cookie;
17723{
17724 funccall_T *fcp = (funccall_T *)cookie;
17725 ufunc_T *fp = fcp->func;
17726
17727 if (fp->uf_profiling && sourcing_lnum >= 1
17728 && sourcing_lnum <= fp->uf_lines.ga_len)
17729 {
17730 fp->uf_tml_idx = sourcing_lnum - 1;
17731 fp->uf_tml_execed = FALSE;
17732 profile_start(&fp->uf_tml_start);
17733 profile_zero(&fp->uf_tml_children);
17734 profile_get_wait(&fp->uf_tml_wait);
17735 }
17736}
17737
17738/*
17739 * Called when actually executing a function line.
17740 */
17741 void
17742func_line_exec(cookie)
17743 void *cookie;
17744{
17745 funccall_T *fcp = (funccall_T *)cookie;
17746 ufunc_T *fp = fcp->func;
17747
17748 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17749 fp->uf_tml_execed = TRUE;
17750}
17751
17752/*
17753 * Called when done with a function line.
17754 */
17755 void
17756func_line_end(cookie)
17757 void *cookie;
17758{
17759 funccall_T *fcp = (funccall_T *)cookie;
17760 ufunc_T *fp = fcp->func;
17761
17762 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17763 {
17764 if (fp->uf_tml_execed)
17765 {
17766 ++fp->uf_tml_count[fp->uf_tml_idx];
17767 profile_end(&fp->uf_tml_start);
17768 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17769 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17770 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17771 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17772 }
17773 fp->uf_tml_idx = -1;
17774 }
17775}
17776#endif
17777
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778/*
17779 * Return TRUE if the currently active function should be ended, because a
17780 * return was encountered or an error occured. Used inside a ":while".
17781 */
17782 int
17783func_has_ended(cookie)
17784 void *cookie;
17785{
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787
17788 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17789 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017790 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 || fcp->returned);
17792}
17793
17794/*
17795 * return TRUE if cookie indicates a function which "abort"s on errors.
17796 */
17797 int
17798func_has_abort(cookie)
17799 void *cookie;
17800{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017801 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802}
17803
17804#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17805typedef enum
17806{
17807 VAR_FLAVOUR_DEFAULT,
17808 VAR_FLAVOUR_SESSION,
17809 VAR_FLAVOUR_VIMINFO
17810} var_flavour_T;
17811
17812static var_flavour_T var_flavour __ARGS((char_u *varname));
17813
17814 static var_flavour_T
17815var_flavour(varname)
17816 char_u *varname;
17817{
17818 char_u *p = varname;
17819
17820 if (ASCII_ISUPPER(*p))
17821 {
17822 while (*(++p))
17823 if (ASCII_ISLOWER(*p))
17824 return VAR_FLAVOUR_SESSION;
17825 return VAR_FLAVOUR_VIMINFO;
17826 }
17827 else
17828 return VAR_FLAVOUR_DEFAULT;
17829}
17830#endif
17831
17832#if defined(FEAT_VIMINFO) || defined(PROTO)
17833/*
17834 * Restore global vars that start with a capital from the viminfo file
17835 */
17836 int
17837read_viminfo_varlist(virp, writing)
17838 vir_T *virp;
17839 int writing;
17840{
17841 char_u *tab;
17842 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844
17845 if (!writing && (find_viminfo_parameter('!') != NULL))
17846 {
17847 tab = vim_strchr(virp->vir_line + 1, '\t');
17848 if (tab != NULL)
17849 {
17850 *tab++ = '\0'; /* isolate the variable name */
17851 if (*tab == 'S') /* string var */
17852 is_string = TRUE;
17853
17854 tab = vim_strchr(tab, '\t');
17855 if (tab != NULL)
17856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 if (is_string)
17858 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017859 tv.v_type = VAR_STRING;
17860 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 }
17863 else
17864 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017865 tv.v_type = VAR_NUMBER;
17866 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017868 set_var(virp->vir_line + 1, &tv, FALSE);
17869 if (is_string)
17870 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 }
17872 }
17873 }
17874
17875 return viminfo_readline(virp);
17876}
17877
17878/*
17879 * Write global vars that start with a capital to the viminfo file
17880 */
17881 void
17882write_viminfo_varlist(fp)
17883 FILE *fp;
17884{
Bram Moolenaar33570922005-01-25 22:26:29 +000017885 hashitem_T *hi;
17886 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017887 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017888 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017889 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017891 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892
17893 if (find_viminfo_parameter('!') == NULL)
17894 return;
17895
17896 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017897
Bram Moolenaar33570922005-01-25 22:26:29 +000017898 todo = globvarht.ht_used;
17899 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017901 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017903 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017904 this_var = HI2DI(hi);
17905 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017907 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017908 {
17909 case VAR_STRING: s = "STR"; break;
17910 case VAR_NUMBER: s = "NUM"; break;
17911 default: continue;
17912 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017913 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017914 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17915 if (p != NULL)
17916 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017917 vim_free(tofree);
17918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 }
17920 }
17921}
17922#endif
17923
17924#if defined(FEAT_SESSION) || defined(PROTO)
17925 int
17926store_session_globals(fd)
17927 FILE *fd;
17928{
Bram Moolenaar33570922005-01-25 22:26:29 +000017929 hashitem_T *hi;
17930 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017931 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 char_u *p, *t;
17933
Bram Moolenaar33570922005-01-25 22:26:29 +000017934 todo = globvarht.ht_used;
17935 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017937 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017939 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017940 this_var = HI2DI(hi);
17941 if ((this_var->di_tv.v_type == VAR_NUMBER
17942 || this_var->di_tv.v_type == VAR_STRING)
17943 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017944 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017945 /* Escape special characters with a backslash. Turn a LF and
17946 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017948 (char_u *)"\\\"\n\r");
17949 if (p == NULL) /* out of memory */
17950 break;
17951 for (t = p; *t != NUL; ++t)
17952 if (*t == '\n')
17953 *t = 'n';
17954 else if (*t == '\r')
17955 *t = 'r';
17956 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017957 this_var->di_key,
17958 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17959 : ' ',
17960 p,
17961 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17962 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017963 || put_eol(fd) == FAIL)
17964 {
17965 vim_free(p);
17966 return FAIL;
17967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 vim_free(p);
17969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 }
17971 }
17972 return OK;
17973}
17974#endif
17975
17976#endif /* FEAT_EVAL */
17977
17978#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17979
17980
17981#ifdef WIN3264
17982/*
17983 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17984 */
17985static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17986static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17987static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17988
17989/*
17990 * Get the short pathname of a file.
17991 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17992 */
17993 static int
17994get_short_pathname(fnamep, bufp, fnamelen)
17995 char_u **fnamep;
17996 char_u **bufp;
17997 int *fnamelen;
17998{
17999 int l,len;
18000 char_u *newbuf;
18001
18002 len = *fnamelen;
18003
18004 l = GetShortPathName(*fnamep, *fnamep, len);
18005 if (l > len - 1)
18006 {
18007 /* If that doesn't work (not enough space), then save the string
18008 * and try again with a new buffer big enough
18009 */
18010 newbuf = vim_strnsave(*fnamep, l);
18011 if (newbuf == NULL)
18012 return 0;
18013
18014 vim_free(*bufp);
18015 *fnamep = *bufp = newbuf;
18016
18017 l = GetShortPathName(*fnamep,*fnamep,l+1);
18018
18019 /* Really should always succeed, as the buffer is big enough */
18020 }
18021
18022 *fnamelen = l;
18023 return 1;
18024}
18025
18026/*
18027 * Create a short path name. Returns the length of the buffer it needs.
18028 * Doesn't copy over the end of the buffer passed in.
18029 */
18030 static int
18031shortpath_for_invalid_fname(fname, bufp, fnamelen)
18032 char_u **fname;
18033 char_u **bufp;
18034 int *fnamelen;
18035{
18036 char_u *s, *p, *pbuf2, *pbuf3;
18037 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018038 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039
18040 /* Make a copy */
18041 len2 = *fnamelen;
18042 pbuf2 = vim_strnsave(*fname, len2);
18043 pbuf3 = NULL;
18044
18045 s = pbuf2 + len2 - 1; /* Find the end */
18046 slen = 1;
18047 plen = len2;
18048
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018049 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 {
18051 --s;
18052 ++slen;
18053 --plen;
18054 }
18055
18056 do
18057 {
18058 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018059 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 {
18061 --s;
18062 ++slen;
18063 --plen;
18064 }
18065 if (s <= pbuf2)
18066 break;
18067
18068 /* Remeber the character that is about to be blatted */
18069 ch = *s;
18070 *s = 0; /* get_short_pathname requires a null-terminated string */
18071
18072 /* Try it in situ */
18073 p = pbuf2;
18074 if (!get_short_pathname(&p, &pbuf3, &plen))
18075 {
18076 vim_free(pbuf2);
18077 return -1;
18078 }
18079 *s = ch; /* Preserve the string */
18080 } while (plen == 0);
18081
18082 if (plen > 0)
18083 {
18084 /* Remeber the length of the new string. */
18085 *fnamelen = len = plen + slen;
18086 vim_free(*bufp);
18087 if (len > len2)
18088 {
18089 /* If there's not enough space in the currently allocated string,
18090 * then copy it to a buffer big enough.
18091 */
18092 *fname= *bufp = vim_strnsave(p, len);
18093 if (*fname == NULL)
18094 return -1;
18095 }
18096 else
18097 {
18098 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18099 *fname = *bufp = pbuf2;
18100 if (p != pbuf2)
18101 strncpy(*fname, p, plen);
18102 pbuf2 = NULL;
18103 }
18104 /* Concat the next bit */
18105 strncpy(*fname + plen, s, slen);
18106 (*fname)[len] = '\0';
18107 }
18108 vim_free(pbuf3);
18109 vim_free(pbuf2);
18110 return 0;
18111}
18112
18113/*
18114 * Get a pathname for a partial path.
18115 */
18116 static int
18117shortpath_for_partial(fnamep, bufp, fnamelen)
18118 char_u **fnamep;
18119 char_u **bufp;
18120 int *fnamelen;
18121{
18122 int sepcount, len, tflen;
18123 char_u *p;
18124 char_u *pbuf, *tfname;
18125 int hasTilde;
18126
18127 /* Count up the path seperators from the RHS.. so we know which part
18128 * of the path to return.
18129 */
18130 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018131 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 if (vim_ispathsep(*p))
18133 ++sepcount;
18134
18135 /* Need full path first (use expand_env() to remove a "~/") */
18136 hasTilde = (**fnamep == '~');
18137 if (hasTilde)
18138 pbuf = tfname = expand_env_save(*fnamep);
18139 else
18140 pbuf = tfname = FullName_save(*fnamep, FALSE);
18141
18142 len = tflen = STRLEN(tfname);
18143
18144 if (!get_short_pathname(&tfname, &pbuf, &len))
18145 return -1;
18146
18147 if (len == 0)
18148 {
18149 /* Don't have a valid filename, so shorten the rest of the
18150 * path if we can. This CAN give us invalid 8.3 filenames, but
18151 * there's not a lot of point in guessing what it might be.
18152 */
18153 len = tflen;
18154 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18155 return -1;
18156 }
18157
18158 /* Count the paths backward to find the beginning of the desired string. */
18159 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018160 {
18161#ifdef FEAT_MBYTE
18162 if (has_mbyte)
18163 p -= mb_head_off(tfname, p);
18164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 if (vim_ispathsep(*p))
18166 {
18167 if (sepcount == 0 || (hasTilde && sepcount == 1))
18168 break;
18169 else
18170 sepcount --;
18171 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 if (hasTilde)
18174 {
18175 --p;
18176 if (p >= tfname)
18177 *p = '~';
18178 else
18179 return -1;
18180 }
18181 else
18182 ++p;
18183
18184 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18185 vim_free(*bufp);
18186 *fnamelen = (int)STRLEN(p);
18187 *bufp = pbuf;
18188 *fnamep = p;
18189
18190 return 0;
18191}
18192#endif /* WIN3264 */
18193
18194/*
18195 * Adjust a filename, according to a string of modifiers.
18196 * *fnamep must be NUL terminated when called. When returning, the length is
18197 * determined by *fnamelen.
18198 * Returns valid flags.
18199 * When there is an error, *fnamep is set to NULL.
18200 */
18201 int
18202modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18203 char_u *src; /* string with modifiers */
18204 int *usedlen; /* characters after src that are used */
18205 char_u **fnamep; /* file name so far */
18206 char_u **bufp; /* buffer for allocated file name or NULL */
18207 int *fnamelen; /* length of fnamep */
18208{
18209 int valid = 0;
18210 char_u *tail;
18211 char_u *s, *p, *pbuf;
18212 char_u dirname[MAXPATHL];
18213 int c;
18214 int has_fullname = 0;
18215#ifdef WIN3264
18216 int has_shortname = 0;
18217#endif
18218
18219repeat:
18220 /* ":p" - full path/file_name */
18221 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18222 {
18223 has_fullname = 1;
18224
18225 valid |= VALID_PATH;
18226 *usedlen += 2;
18227
18228 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18229 if ((*fnamep)[0] == '~'
18230#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18231 && ((*fnamep)[1] == '/'
18232# ifdef BACKSLASH_IN_FILENAME
18233 || (*fnamep)[1] == '\\'
18234# endif
18235 || (*fnamep)[1] == NUL)
18236
18237#endif
18238 )
18239 {
18240 *fnamep = expand_env_save(*fnamep);
18241 vim_free(*bufp); /* free any allocated file name */
18242 *bufp = *fnamep;
18243 if (*fnamep == NULL)
18244 return -1;
18245 }
18246
18247 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018248 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 {
18250 if (vim_ispathsep(*p)
18251 && p[1] == '.'
18252 && (p[2] == NUL
18253 || vim_ispathsep(p[2])
18254 || (p[2] == '.'
18255 && (p[3] == NUL || vim_ispathsep(p[3])))))
18256 break;
18257 }
18258
18259 /* FullName_save() is slow, don't use it when not needed. */
18260 if (*p != NUL || !vim_isAbsName(*fnamep))
18261 {
18262 *fnamep = FullName_save(*fnamep, *p != NUL);
18263 vim_free(*bufp); /* free any allocated file name */
18264 *bufp = *fnamep;
18265 if (*fnamep == NULL)
18266 return -1;
18267 }
18268
18269 /* Append a path separator to a directory. */
18270 if (mch_isdir(*fnamep))
18271 {
18272 /* Make room for one or two extra characters. */
18273 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18274 vim_free(*bufp); /* free any allocated file name */
18275 *bufp = *fnamep;
18276 if (*fnamep == NULL)
18277 return -1;
18278 add_pathsep(*fnamep);
18279 }
18280 }
18281
18282 /* ":." - path relative to the current directory */
18283 /* ":~" - path relative to the home directory */
18284 /* ":8" - shortname path - postponed till after */
18285 while (src[*usedlen] == ':'
18286 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18287 {
18288 *usedlen += 2;
18289 if (c == '8')
18290 {
18291#ifdef WIN3264
18292 has_shortname = 1; /* Postpone this. */
18293#endif
18294 continue;
18295 }
18296 pbuf = NULL;
18297 /* Need full path first (use expand_env() to remove a "~/") */
18298 if (!has_fullname)
18299 {
18300 if (c == '.' && **fnamep == '~')
18301 p = pbuf = expand_env_save(*fnamep);
18302 else
18303 p = pbuf = FullName_save(*fnamep, FALSE);
18304 }
18305 else
18306 p = *fnamep;
18307
18308 has_fullname = 0;
18309
18310 if (p != NULL)
18311 {
18312 if (c == '.')
18313 {
18314 mch_dirname(dirname, MAXPATHL);
18315 s = shorten_fname(p, dirname);
18316 if (s != NULL)
18317 {
18318 *fnamep = s;
18319 if (pbuf != NULL)
18320 {
18321 vim_free(*bufp); /* free any allocated file name */
18322 *bufp = pbuf;
18323 pbuf = NULL;
18324 }
18325 }
18326 }
18327 else
18328 {
18329 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18330 /* Only replace it when it starts with '~' */
18331 if (*dirname == '~')
18332 {
18333 s = vim_strsave(dirname);
18334 if (s != NULL)
18335 {
18336 *fnamep = s;
18337 vim_free(*bufp);
18338 *bufp = s;
18339 }
18340 }
18341 }
18342 vim_free(pbuf);
18343 }
18344 }
18345
18346 tail = gettail(*fnamep);
18347 *fnamelen = (int)STRLEN(*fnamep);
18348
18349 /* ":h" - head, remove "/file_name", can be repeated */
18350 /* Don't remove the first "/" or "c:\" */
18351 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18352 {
18353 valid |= VALID_HEAD;
18354 *usedlen += 2;
18355 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018356 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 --tail;
18358 *fnamelen = (int)(tail - *fnamep);
18359#ifdef VMS
18360 if (*fnamelen > 0)
18361 *fnamelen += 1; /* the path separator is part of the path */
18362#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018363 while (tail > s && !after_pathsep(s, tail))
18364 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 }
18366
18367 /* ":8" - shortname */
18368 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18369 {
18370 *usedlen += 2;
18371#ifdef WIN3264
18372 has_shortname = 1;
18373#endif
18374 }
18375
18376#ifdef WIN3264
18377 /* Check shortname after we have done 'heads' and before we do 'tails'
18378 */
18379 if (has_shortname)
18380 {
18381 pbuf = NULL;
18382 /* Copy the string if it is shortened by :h */
18383 if (*fnamelen < (int)STRLEN(*fnamep))
18384 {
18385 p = vim_strnsave(*fnamep, *fnamelen);
18386 if (p == 0)
18387 return -1;
18388 vim_free(*bufp);
18389 *bufp = *fnamep = p;
18390 }
18391
18392 /* Split into two implementations - makes it easier. First is where
18393 * there isn't a full name already, second is where there is.
18394 */
18395 if (!has_fullname && !vim_isAbsName(*fnamep))
18396 {
18397 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18398 return -1;
18399 }
18400 else
18401 {
18402 int l;
18403
18404 /* Simple case, already have the full-name
18405 * Nearly always shorter, so try first time. */
18406 l = *fnamelen;
18407 if (!get_short_pathname(fnamep, bufp, &l))
18408 return -1;
18409
18410 if (l == 0)
18411 {
18412 /* Couldn't find the filename.. search the paths.
18413 */
18414 l = *fnamelen;
18415 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18416 return -1;
18417 }
18418 *fnamelen = l;
18419 }
18420 }
18421#endif /* WIN3264 */
18422
18423 /* ":t" - tail, just the basename */
18424 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18425 {
18426 *usedlen += 2;
18427 *fnamelen -= (int)(tail - *fnamep);
18428 *fnamep = tail;
18429 }
18430
18431 /* ":e" - extension, can be repeated */
18432 /* ":r" - root, without extension, can be repeated */
18433 while (src[*usedlen] == ':'
18434 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18435 {
18436 /* find a '.' in the tail:
18437 * - for second :e: before the current fname
18438 * - otherwise: The last '.'
18439 */
18440 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18441 s = *fnamep - 2;
18442 else
18443 s = *fnamep + *fnamelen - 1;
18444 for ( ; s > tail; --s)
18445 if (s[0] == '.')
18446 break;
18447 if (src[*usedlen + 1] == 'e') /* :e */
18448 {
18449 if (s > tail)
18450 {
18451 *fnamelen += (int)(*fnamep - (s + 1));
18452 *fnamep = s + 1;
18453#ifdef VMS
18454 /* cut version from the extension */
18455 s = *fnamep + *fnamelen - 1;
18456 for ( ; s > *fnamep; --s)
18457 if (s[0] == ';')
18458 break;
18459 if (s > *fnamep)
18460 *fnamelen = s - *fnamep;
18461#endif
18462 }
18463 else if (*fnamep <= tail)
18464 *fnamelen = 0;
18465 }
18466 else /* :r */
18467 {
18468 if (s > tail) /* remove one extension */
18469 *fnamelen = (int)(s - *fnamep);
18470 }
18471 *usedlen += 2;
18472 }
18473
18474 /* ":s?pat?foo?" - substitute */
18475 /* ":gs?pat?foo?" - global substitute */
18476 if (src[*usedlen] == ':'
18477 && (src[*usedlen + 1] == 's'
18478 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18479 {
18480 char_u *str;
18481 char_u *pat;
18482 char_u *sub;
18483 int sep;
18484 char_u *flags;
18485 int didit = FALSE;
18486
18487 flags = (char_u *)"";
18488 s = src + *usedlen + 2;
18489 if (src[*usedlen + 1] == 'g')
18490 {
18491 flags = (char_u *)"g";
18492 ++s;
18493 }
18494
18495 sep = *s++;
18496 if (sep)
18497 {
18498 /* find end of pattern */
18499 p = vim_strchr(s, sep);
18500 if (p != NULL)
18501 {
18502 pat = vim_strnsave(s, (int)(p - s));
18503 if (pat != NULL)
18504 {
18505 s = p + 1;
18506 /* find end of substitution */
18507 p = vim_strchr(s, sep);
18508 if (p != NULL)
18509 {
18510 sub = vim_strnsave(s, (int)(p - s));
18511 str = vim_strnsave(*fnamep, *fnamelen);
18512 if (sub != NULL && str != NULL)
18513 {
18514 *usedlen = (int)(p + 1 - src);
18515 s = do_string_sub(str, pat, sub, flags);
18516 if (s != NULL)
18517 {
18518 *fnamep = s;
18519 *fnamelen = (int)STRLEN(s);
18520 vim_free(*bufp);
18521 *bufp = s;
18522 didit = TRUE;
18523 }
18524 }
18525 vim_free(sub);
18526 vim_free(str);
18527 }
18528 vim_free(pat);
18529 }
18530 }
18531 /* after using ":s", repeat all the modifiers */
18532 if (didit)
18533 goto repeat;
18534 }
18535 }
18536
18537 return valid;
18538}
18539
18540/*
18541 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18542 * "flags" can be "g" to do a global substitute.
18543 * Returns an allocated string, NULL for error.
18544 */
18545 char_u *
18546do_string_sub(str, pat, sub, flags)
18547 char_u *str;
18548 char_u *pat;
18549 char_u *sub;
18550 char_u *flags;
18551{
18552 int sublen;
18553 regmatch_T regmatch;
18554 int i;
18555 int do_all;
18556 char_u *tail;
18557 garray_T ga;
18558 char_u *ret;
18559 char_u *save_cpo;
18560
18561 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18562 save_cpo = p_cpo;
18563 p_cpo = (char_u *)"";
18564
18565 ga_init2(&ga, 1, 200);
18566
18567 do_all = (flags[0] == 'g');
18568
18569 regmatch.rm_ic = p_ic;
18570 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18571 if (regmatch.regprog != NULL)
18572 {
18573 tail = str;
18574 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18575 {
18576 /*
18577 * Get some space for a temporary buffer to do the substitution
18578 * into. It will contain:
18579 * - The text up to where the match is.
18580 * - The substituted text.
18581 * - The text after the match.
18582 */
18583 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18584 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18585 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18586 {
18587 ga_clear(&ga);
18588 break;
18589 }
18590
18591 /* copy the text up to where the match is */
18592 i = (int)(regmatch.startp[0] - tail);
18593 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18594 /* add the substituted text */
18595 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18596 + ga.ga_len + i, TRUE, TRUE, FALSE);
18597 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 /* avoid getting stuck on a match with an empty string */
18599 if (tail == regmatch.endp[0])
18600 {
18601 if (*tail == NUL)
18602 break;
18603 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18604 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 }
18606 else
18607 {
18608 tail = regmatch.endp[0];
18609 if (*tail == NUL)
18610 break;
18611 }
18612 if (!do_all)
18613 break;
18614 }
18615
18616 if (ga.ga_data != NULL)
18617 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18618
18619 vim_free(regmatch.regprog);
18620 }
18621
18622 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18623 ga_clear(&ga);
18624 p_cpo = save_cpo;
18625
18626 return ret;
18627}
18628
18629#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */